blob: 07a57d024f9529c7c75359db1d19844c21351396 [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>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020045#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040046#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050047#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040048#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040050#include "netns.h"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020051#include "pnfs.h"
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define NFSDDBG_FACILITY NFSDDBG_PROC
54
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050055#define all_ones {{~0,~0},~0}
56static const stateid_t one_stateid = {
57 .si_generation = ~0,
58 .si_opaque = all_ones,
59};
60static const stateid_t zero_stateid = {
61 /* all fields zero */
62};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010063static const stateid_t currentstateid = {
64 .si_generation = 1,
65};
Trond Myklebustfb500a72017-11-03 08:00:12 -040066static const stateid_t close_stateid = {
67 .si_generation = 0xffffffffU,
68};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050069
Andy Adamsonec6b5d72009-04-03 08:28:28 +030070static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070071
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050072#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
73#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010074#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Andrew Elbleae254dac2017-11-09 13:41:10 -050075#define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040078static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040079static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
J. Bruce Fields8b671b82009-02-22 14:51:34 -080081/* Locking: */
82
J. Bruce Fields8b671b82009-02-22 14:51:34 -080083/*
84 * Currently used for the del_recall_lru and file hash table. In an
85 * effort to decrease the scope of the client_mutex, this spinlock may
86 * eventually cover more:
87 */
Benny Halevycdc975052014-05-30 09:09:30 -040088static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080089
Andrew Elble4f34bd02017-11-08 17:29:51 -050090enum nfsd4_st_mutex_lock_subclass {
91 OPEN_STATEID_MUTEX = 0,
92 LOCK_STATEID_MUTEX = 1,
93};
94
Jeff Laytonb401be222014-07-29 21:34:33 -040095/*
96 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
97 * the refcount on the open stateid to drop.
98 */
99static DECLARE_WAIT_QUEUE_HEAD(close_wq);
100
Jeff Layton9258a2d2018-03-16 09:47:22 -0400101static struct kmem_cache *client_slab;
Christoph Hellwigabf11352014-05-21 07:43:03 -0700102static struct kmem_cache *openowner_slab;
103static struct kmem_cache *lockowner_slab;
104static struct kmem_cache *file_slab;
105static struct kmem_cache *stateid_slab;
106static struct kmem_cache *deleg_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +0200107static struct kmem_cache *odstate_slab;
NeilBrowne60d4392005-06-23 22:03:01 -0700108
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400109static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800110
Julia Lawallc4cb8972015-11-21 22:57:39 +0100111static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
Jeff Layton76d348f2016-09-16 16:28:24 -0400112static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200113
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400114static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800115{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400116 return ses->se_flags & NFS4_SESSION_DEAD;
117}
118
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400119static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
120{
121 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400122 return nfserr_jukebox;
123 ses->se_flags |= NFS4_SESSION_DEAD;
124 return nfs_ok;
125}
126
J. Bruce Fields221a6872013-04-01 22:23:49 -0400127static bool is_client_expired(struct nfs4_client *clp)
128{
129 return clp->cl_time == 0;
130}
131
J. Bruce Fields221a6872013-04-01 22:23:49 -0400132static __be32 get_client_locked(struct nfs4_client *clp)
133{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400134 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
135
136 lockdep_assert_held(&nn->client_lock);
137
J. Bruce Fields221a6872013-04-01 22:23:49 -0400138 if (is_client_expired(clp))
139 return nfserr_expired;
140 atomic_inc(&clp->cl_refcount);
141 return nfs_ok;
142}
143
144/* must be called under the client_lock */
145static inline void
146renew_client_locked(struct nfs4_client *clp)
147{
148 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
149
150 if (is_client_expired(clp)) {
151 WARN_ON(1);
152 printk("%s: client (clientid %08x/%08x) already expired\n",
153 __func__,
154 clp->cl_clientid.cl_boot,
155 clp->cl_clientid.cl_id);
156 return;
157 }
158
159 dprintk("renewing client (clientid %08x/%08x)\n",
160 clp->cl_clientid.cl_boot,
161 clp->cl_clientid.cl_id);
162 list_move_tail(&clp->cl_lru, &nn->client_lru);
163 clp->cl_time = get_seconds();
164}
165
Fengguang Wuba138432013-04-16 22:14:15 -0400166static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400167{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400168 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
169
170 lockdep_assert_held(&nn->client_lock);
171
J. Bruce Fields221a6872013-04-01 22:23:49 -0400172 if (!atomic_dec_and_test(&clp->cl_refcount))
173 return;
174 if (!is_client_expired(clp))
175 renew_client_locked(clp);
176}
177
Jeff Layton4b24ca72014-06-30 11:48:44 -0400178static void put_client_renew(struct nfs4_client *clp)
179{
180 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
181
Jeff Laytond6c249b2014-07-08 14:02:50 -0400182 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
183 return;
184 if (!is_client_expired(clp))
185 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400186 spin_unlock(&nn->client_lock);
187}
188
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400189static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
190{
191 __be32 status;
192
193 if (is_session_dead(ses))
194 return nfserr_badsession;
195 status = get_client_locked(ses->se_client);
196 if (status)
197 return status;
198 atomic_inc(&ses->se_ref);
199 return nfs_ok;
200}
201
202static void nfsd4_put_session_locked(struct nfsd4_session *ses)
203{
204 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400205 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
206
207 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400208
209 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
210 free_session(ses);
211 put_client_renew_locked(clp);
212}
213
214static void nfsd4_put_session(struct nfsd4_session *ses)
215{
216 struct nfs4_client *clp = ses->se_client;
217 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
218
219 spin_lock(&nn->client_lock);
220 nfsd4_put_session_locked(ses);
221 spin_unlock(&nn->client_lock);
222}
223
Jeff Layton76d348f2016-09-16 16:28:24 -0400224static struct nfsd4_blocked_lock *
225find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
226 struct nfsd_net *nn)
227{
228 struct nfsd4_blocked_lock *cur, *found = NULL;
229
Jeff Layton0cc11a62016-10-20 09:34:31 -0400230 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400231 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
232 if (fh_match(fh, &cur->nbl_fh)) {
233 list_del_init(&cur->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -0400234 list_del_init(&cur->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400235 found = cur;
236 break;
237 }
238 }
Jeff Layton0cc11a62016-10-20 09:34:31 -0400239 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400240 if (found)
241 posix_unblock_lock(&found->nbl_lock);
242 return found;
243}
244
245static struct nfsd4_blocked_lock *
246find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
247 struct nfsd_net *nn)
248{
249 struct nfsd4_blocked_lock *nbl;
250
251 nbl = find_blocked_lock(lo, fh, nn);
252 if (!nbl) {
253 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
254 if (nbl) {
255 fh_copy_shallow(&nbl->nbl_fh, fh);
256 locks_init_lock(&nbl->nbl_lock);
257 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
258 &nfsd4_cb_notify_lock_ops,
259 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
260 }
261 }
262 return nbl;
263}
264
265static void
266free_blocked_lock(struct nfsd4_blocked_lock *nbl)
267{
268 locks_release_private(&nbl->nbl_lock);
269 kfree(nbl);
270}
271
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400272static void
273remove_blocked_locks(struct nfs4_lockowner *lo)
274{
275 struct nfs4_client *clp = lo->lo_owner.so_client;
276 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
277 struct nfsd4_blocked_lock *nbl;
278 LIST_HEAD(reaplist);
279
280 /* Dequeue all blocked locks */
281 spin_lock(&nn->blocked_locks_lock);
282 while (!list_empty(&lo->lo_blocked)) {
283 nbl = list_first_entry(&lo->lo_blocked,
284 struct nfsd4_blocked_lock,
285 nbl_list);
286 list_del_init(&nbl->nbl_list);
287 list_move(&nbl->nbl_lru, &reaplist);
288 }
289 spin_unlock(&nn->blocked_locks_lock);
290
291 /* Now free them */
292 while (!list_empty(&reaplist)) {
293 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
294 nbl_lru);
295 list_del_init(&nbl->nbl_lru);
296 posix_unblock_lock(&nbl->nbl_lock);
297 free_blocked_lock(nbl);
298 }
299}
300
Jeff Layton76d348f2016-09-16 16:28:24 -0400301static int
302nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
303{
304 /*
305 * Since this is just an optimization, we don't try very hard if it
306 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
307 * just quit trying on anything else.
308 */
309 switch (task->tk_status) {
310 case -NFS4ERR_DELAY:
311 rpc_delay(task, 1 * HZ);
312 return 0;
313 default:
314 return 1;
315 }
316}
317
318static void
319nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
320{
321 struct nfsd4_blocked_lock *nbl = container_of(cb,
322 struct nfsd4_blocked_lock, nbl_cb);
323
324 free_blocked_lock(nbl);
325}
326
327static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
328 .done = nfsd4_cb_notify_lock_done,
329 .release = nfsd4_cb_notify_lock_release,
330};
331
Kinglong Meeb5971af2014-08-22 10:18:43 -0400332static inline struct nfs4_stateowner *
333nfs4_get_stateowner(struct nfs4_stateowner *sop)
334{
335 atomic_inc(&sop->so_count);
336 return sop;
337}
338
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400339static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400340same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400341{
342 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400343 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400344}
345
346static struct nfs4_openowner *
347find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400348 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400349{
350 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400351
Trond Myklebustd4f04892014-07-29 21:34:36 -0400352 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400353
Trond Myklebustd4f04892014-07-29 21:34:36 -0400354 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
355 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400356 if (!so->so_is_open_owner)
357 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400358 if (same_owner_str(so, &open->op_owner))
359 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400360 }
361 return NULL;
362}
363
364static struct nfs4_openowner *
365find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400366 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400367{
368 struct nfs4_openowner *oo;
369
Trond Myklebustd4f04892014-07-29 21:34:36 -0400370 spin_lock(&clp->cl_lock);
371 oo = find_openstateowner_str_locked(hashval, open, clp);
372 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400373 return oo;
374}
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376static inline u32
377opaque_hashval(const void *ptr, int nbytes)
378{
379 unsigned char *cptr = (unsigned char *) ptr;
380
381 u32 x = 0;
382 while (nbytes--) {
383 x *= 37;
384 x += *cptr++;
385 }
386 return x;
387}
388
Jeff Layton5b095e92014-10-23 08:01:02 -0400389static void nfsd4_free_file_rcu(struct rcu_head *rcu)
J. Bruce Fields32513b42011-10-13 16:00:16 -0400390{
Jeff Layton5b095e92014-10-23 08:01:02 -0400391 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
392
393 kmem_cache_free(file_slab, fp);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400394}
395
Christoph Hellwige6ba76e2014-08-14 08:50:16 +0200396void
NeilBrown13cd2182005-06-23 22:03:10 -0700397put_nfs4_file(struct nfs4_file *fi)
398{
Jeff Layton02e12152014-07-16 10:31:57 -0400399 might_lock(&state_lock);
400
Elena Reshetova818a34e2017-10-20 12:53:30 +0300401 if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton5b095e92014-10-23 08:01:02 -0400402 hlist_del_rcu(&fi->fi_hash);
Benny Halevycdc975052014-05-30 09:09:30 -0400403 spin_unlock(&state_lock);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200404 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
Jeff Layton5b095e92014-10-23 08:01:02 -0400405 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
406 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800407 }
NeilBrown13cd2182005-06-23 22:03:10 -0700408}
409
Trond Myklebustde186432014-07-10 14:07:26 -0400410static struct file *
411__nfs4_get_fd(struct nfs4_file *f, int oflag)
412{
413 if (f->fi_fds[oflag])
414 return get_file(f->fi_fds[oflag]);
415 return NULL;
416}
417
418static struct file *
419find_writeable_file_locked(struct nfs4_file *f)
420{
421 struct file *ret;
422
423 lockdep_assert_held(&f->fi_lock);
424
425 ret = __nfs4_get_fd(f, O_WRONLY);
426 if (!ret)
427 ret = __nfs4_get_fd(f, O_RDWR);
428 return ret;
429}
430
431static struct file *
432find_writeable_file(struct nfs4_file *f)
433{
434 struct file *ret;
435
436 spin_lock(&f->fi_lock);
437 ret = find_writeable_file_locked(f);
438 spin_unlock(&f->fi_lock);
439
440 return ret;
441}
442
443static struct file *find_readable_file_locked(struct nfs4_file *f)
444{
445 struct file *ret;
446
447 lockdep_assert_held(&f->fi_lock);
448
449 ret = __nfs4_get_fd(f, O_RDONLY);
450 if (!ret)
451 ret = __nfs4_get_fd(f, O_RDWR);
452 return ret;
453}
454
455static struct file *
456find_readable_file(struct nfs4_file *f)
457{
458 struct file *ret;
459
460 spin_lock(&f->fi_lock);
461 ret = find_readable_file_locked(f);
462 spin_unlock(&f->fi_lock);
463
464 return ret;
465}
466
Christoph Hellwig4d227fc2014-08-17 07:40:00 -0500467struct file *
Trond Myklebustde186432014-07-10 14:07:26 -0400468find_any_file(struct nfs4_file *f)
469{
470 struct file *ret;
471
472 spin_lock(&f->fi_lock);
473 ret = __nfs4_get_fd(f, O_RDWR);
474 if (!ret) {
475 ret = __nfs4_get_fd(f, O_WRONLY);
476 if (!ret)
477 ret = __nfs4_get_fd(f, O_RDONLY);
478 }
479 spin_unlock(&f->fi_lock);
480 return ret;
481}
482
Trond Myklebust02a35082014-07-25 07:34:22 -0400483static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800484unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700485
486/*
487 * Open owner state (share locks)
488 */
489
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500490/* hash tables for lock and open owners */
491#define OWNER_HASH_BITS 8
492#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
493#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700494
Trond Myklebustd4f04892014-07-29 21:34:36 -0400495static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400496{
497 unsigned int ret;
498
499 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500500 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400501}
NeilBrownef0f3392006-04-10 22:55:41 -0700502
NeilBrownef0f3392006-04-10 22:55:41 -0700503/* hash table for nfs4_file */
504#define FILE_HASH_BITS 8
505#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800506
Trond Myklebustca943212014-07-23 16:17:39 -0400507static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400508{
Trond Myklebustca943212014-07-23 16:17:39 -0400509 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
510}
511
512static unsigned int file_hashval(struct knfsd_fh *fh)
513{
514 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
515}
516
Jeff Layton89876f82013-04-02 09:01:59 -0400517static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700518
Jeff Layton12659652014-07-10 14:07:28 -0400519static void
520__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400521{
Jeff Layton7214e862014-07-10 14:07:33 -0400522 lockdep_assert_held(&fp->fi_lock);
523
Jeff Layton12659652014-07-10 14:07:28 -0400524 if (access & NFS4_SHARE_ACCESS_WRITE)
525 atomic_inc(&fp->fi_access[O_WRONLY]);
526 if (access & NFS4_SHARE_ACCESS_READ)
527 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400528}
529
Jeff Layton12659652014-07-10 14:07:28 -0400530static __be32
531nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400532{
Jeff Layton7214e862014-07-10 14:07:33 -0400533 lockdep_assert_held(&fp->fi_lock);
534
Jeff Layton12659652014-07-10 14:07:28 -0400535 /* Does this access mode make sense? */
536 if (access & ~NFS4_SHARE_ACCESS_BOTH)
537 return nfserr_inval;
538
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400539 /* Does it conflict with a deny mode already set? */
540 if ((access & fp->fi_share_deny) != 0)
541 return nfserr_share_denied;
542
Jeff Layton12659652014-07-10 14:07:28 -0400543 __nfs4_file_get_access(fp, access);
544 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400545}
546
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400547static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
548{
549 /* Common case is that there is no deny mode. */
550 if (deny) {
551 /* Does this deny mode make sense? */
552 if (deny & ~NFS4_SHARE_DENY_BOTH)
553 return nfserr_inval;
554
555 if ((deny & NFS4_SHARE_DENY_READ) &&
556 atomic_read(&fp->fi_access[O_RDONLY]))
557 return nfserr_share_denied;
558
559 if ((deny & NFS4_SHARE_DENY_WRITE) &&
560 atomic_read(&fp->fi_access[O_WRONLY]))
561 return nfserr_share_denied;
562 }
563 return nfs_ok;
564}
565
J. Bruce Fields998db522010-08-07 09:21:41 -0400566static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400567{
Trond Myklebustde186432014-07-10 14:07:26 -0400568 might_lock(&fp->fi_lock);
569
570 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
571 struct file *f1 = NULL;
572 struct file *f2 = NULL;
573
Jeff Layton6d338b52014-07-10 14:07:29 -0400574 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400575 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400576 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400577 spin_unlock(&fp->fi_lock);
578 if (f1)
579 fput(f1);
580 if (f2)
581 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400582 }
583}
584
Jeff Layton12659652014-07-10 14:07:28 -0400585static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400586{
Jeff Layton12659652014-07-10 14:07:28 -0400587 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
588
589 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400590 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400591 if (access & NFS4_SHARE_ACCESS_READ)
592 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400593}
594
Sachin Bhamare8287f002015-04-27 14:50:14 +0200595/*
596 * Allocate a new open/delegation state counter. This is needed for
597 * pNFS for proper return on close semantics.
598 *
599 * Note that we only allocate it for pNFS-enabled exports, otherwise
600 * all pointers to struct nfs4_clnt_odstate are always NULL.
601 */
602static struct nfs4_clnt_odstate *
603alloc_clnt_odstate(struct nfs4_client *clp)
604{
605 struct nfs4_clnt_odstate *co;
606
607 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
608 if (co) {
609 co->co_client = clp;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300610 refcount_set(&co->co_odcount, 1);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200611 }
612 return co;
613}
614
615static void
616hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
617{
618 struct nfs4_file *fp = co->co_file;
619
620 lockdep_assert_held(&fp->fi_lock);
621 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
622}
623
624static inline void
625get_clnt_odstate(struct nfs4_clnt_odstate *co)
626{
627 if (co)
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300628 refcount_inc(&co->co_odcount);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200629}
630
631static void
632put_clnt_odstate(struct nfs4_clnt_odstate *co)
633{
634 struct nfs4_file *fp;
635
636 if (!co)
637 return;
638
639 fp = co->co_file;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300640 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
Sachin Bhamare8287f002015-04-27 14:50:14 +0200641 list_del(&co->co_perfile);
642 spin_unlock(&fp->fi_lock);
643
644 nfsd4_return_all_file_layouts(co->co_client, fp);
645 kmem_cache_free(odstate_slab, co);
646 }
647}
648
649static struct nfs4_clnt_odstate *
650find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
651{
652 struct nfs4_clnt_odstate *co;
653 struct nfs4_client *cl;
654
655 if (!new)
656 return NULL;
657
658 cl = new->co_client;
659
660 spin_lock(&fp->fi_lock);
661 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
662 if (co->co_client == cl) {
663 get_clnt_odstate(co);
664 goto out;
665 }
666 }
667 co = new;
668 co->co_file = fp;
669 hash_clnt_odstate_locked(new);
670out:
671 spin_unlock(&fp->fi_lock);
672 return co;
673}
674
Kinglong Meed19fb702017-01-18 19:04:42 +0800675struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
676 void (*sc_free)(struct nfs4_stid *))
J. Bruce Fields996e0932011-10-17 11:14:48 -0400677{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500678 struct nfs4_stid *stid;
679 int new_id;
680
Trond Myklebustf8338832014-07-25 07:34:19 -0400681 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500682 if (!stid)
683 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400684
Jeff Layton4770d722014-07-29 21:34:10 -0400685 idr_preload(GFP_KERNEL);
686 spin_lock(&cl->cl_lock);
687 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
688 spin_unlock(&cl->cl_lock);
689 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700690 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500691 goto out_free;
Kinglong Meed19fb702017-01-18 19:04:42 +0800692
693 stid->sc_free = sc_free;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500694 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500695 stid->sc_stateid.si_opaque.so_id = new_id;
696 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
697 /* Will be incremented before return to client: */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300698 refcount_set(&stid->sc_count, 1);
Jeff Layton9767feb2015-10-01 09:05:50 -0400699 spin_lock_init(&stid->sc_lock);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500700
J. Bruce Fields996e0932011-10-17 11:14:48 -0400701 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500702 * It shouldn't be a problem to reuse an opaque stateid value.
703 * I don't think it is for 4.1. But with 4.0 I worry that, for
704 * example, a stray write retransmission could be accepted by
705 * the server when it should have been rejected. Therefore,
706 * adopt a trick from the sctp code to attempt to maximize the
707 * amount of time until an id is reused, by ensuring they always
708 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400709 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500710 return stid;
711out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800712 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500713 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400714}
715
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400716/*
717 * Create a unique stateid_t to represent each COPY.
718 */
719int nfs4_init_cp_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
720{
721 int new_id;
722
723 idr_preload(GFP_KERNEL);
724 spin_lock(&nn->s2s_cp_lock);
725 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, copy, 0, 0, GFP_NOWAIT);
726 spin_unlock(&nn->s2s_cp_lock);
727 idr_preload_end();
728 if (new_id < 0)
729 return 0;
730 copy->cp_stateid.si_opaque.so_id = new_id;
731 copy->cp_stateid.si_opaque.so_clid.cl_boot = nn->boot_time;
732 copy->cp_stateid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
733 return 1;
734}
735
736void nfs4_free_cp_state(struct nfsd4_copy *copy)
737{
738 struct nfsd_net *nn;
739
740 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
741 spin_lock(&nn->s2s_cp_lock);
742 idr_remove(&nn->s2s_cp_stateids, copy->cp_stateid.si_opaque.so_id);
743 spin_unlock(&nn->s2s_cp_lock);
744}
745
Jeff Laytonb49e0842014-07-29 21:34:11 -0400746static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400747{
Trond Myklebust60116952014-07-29 21:34:06 -0400748 struct nfs4_stid *stid;
Trond Myklebust60116952014-07-29 21:34:06 -0400749
Kinglong Meed19fb702017-01-18 19:04:42 +0800750 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
Trond Myklebust60116952014-07-29 21:34:06 -0400751 if (!stid)
752 return NULL;
753
Kinglong Meed19fb702017-01-18 19:04:42 +0800754 return openlockstateid(stid);
Trond Myklebust60116952014-07-29 21:34:06 -0400755}
756
757static void nfs4_free_deleg(struct nfs4_stid *stid)
758{
Trond Myklebust60116952014-07-29 21:34:06 -0400759 kmem_cache_free(deleg_slab, stid);
760 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400761}
762
NeilBrown6282cd52014-06-04 17:39:26 +1000763/*
764 * When we recall a delegation, we should be careful not to hand it
765 * out again straight away.
766 * To ensure this we keep a pair of bloom filters ('new' and 'old')
767 * in which the filehandles of recalled delegations are "stored".
768 * If a filehandle appear in either filter, a delegation is blocked.
769 * When a delegation is recalled, the filehandle is stored in the "new"
770 * filter.
771 * Every 30 seconds we swap the filters and clear the "new" one,
772 * unless both are empty of course.
773 *
774 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
775 * low 3 bytes as hash-table indices.
776 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400777 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000778 * is used to manage concurrent access. Testing does not need the lock
779 * except when swapping the two filters.
780 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400781static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000782static struct bloom_pair {
783 int entries, old_entries;
784 time_t swap_time;
785 int new; /* index into 'set' */
786 DECLARE_BITMAP(set[2], 256);
787} blocked_delegations;
788
789static int delegation_blocked(struct knfsd_fh *fh)
790{
791 u32 hash;
792 struct bloom_pair *bd = &blocked_delegations;
793
794 if (bd->entries == 0)
795 return 0;
796 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400797 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000798 if (seconds_since_boot() - bd->swap_time > 30) {
799 bd->entries -= bd->old_entries;
800 bd->old_entries = bd->entries;
801 memset(bd->set[bd->new], 0,
802 sizeof(bd->set[0]));
803 bd->new = 1-bd->new;
804 bd->swap_time = seconds_since_boot();
805 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400806 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000807 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100808 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000809 if (test_bit(hash&255, bd->set[0]) &&
810 test_bit((hash>>8)&255, bd->set[0]) &&
811 test_bit((hash>>16)&255, bd->set[0]))
812 return 1;
813
814 if (test_bit(hash&255, bd->set[1]) &&
815 test_bit((hash>>8)&255, bd->set[1]) &&
816 test_bit((hash>>16)&255, bd->set[1]))
817 return 1;
818
819 return 0;
820}
821
822static void block_delegations(struct knfsd_fh *fh)
823{
824 u32 hash;
825 struct bloom_pair *bd = &blocked_delegations;
826
Daniel Borkmann87545892014-12-10 16:33:11 +0100827 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000828
Jeff Laytonf54fe962014-07-25 07:34:26 -0400829 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000830 __set_bit(hash&255, bd->set[bd->new]);
831 __set_bit((hash>>8)&255, bd->set[bd->new]);
832 __set_bit((hash>>16)&255, bd->set[bd->new]);
833 if (bd->entries == 0)
834 bd->swap_time = seconds_since_boot();
835 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400836 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000837}
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839static struct nfs4_delegation *
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500840alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
841 struct svc_fh *current_fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +0200842 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400845 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400848 n = atomic_long_inc_return(&num_delegations);
849 if (n < 0 || n > max_delegations)
850 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000851 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400852 goto out_dec;
Kinglong Meed19fb702017-01-18 19:04:42 +0800853 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700854 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400855 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400856
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400857 /*
858 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400859 * meaning of seqid 0 isn't meaningful, really, but let's avoid
860 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400861 */
862 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700863 INIT_LIST_HEAD(&dp->dl_perfile);
864 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200866 dp->dl_clnt_odstate = odstate;
867 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400868 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200869 dp->dl_retries = 1;
870 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200871 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500872 get_nfs4_file(fp);
873 dp->dl_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400875out_dec:
876 atomic_long_dec(&num_delegations);
877 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
880void
Trond Myklebust60116952014-07-29 21:34:06 -0400881nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Trond Myklebust11b91642014-07-29 21:34:08 -0400883 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400884 struct nfs4_client *clp = s->sc_client;
885
Jeff Layton4770d722014-07-29 21:34:10 -0400886 might_lock(&clp->cl_lock);
887
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300888 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
Jeff Laytonb401be222014-07-29 21:34:33 -0400889 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400890 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400891 }
Trond Myklebust60116952014-07-29 21:34:06 -0400892 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400893 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400894 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400895 if (fp)
896 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
Jeff Layton9767feb2015-10-01 09:05:50 -0400899void
900nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
901{
902 stateid_t *src = &stid->sc_stateid;
903
904 spin_lock(&stid->sc_lock);
905 if (unlikely(++src->si_generation == 0))
906 src->si_generation = 1;
907 memcpy(dst, src, sizeof(*dst));
908 spin_unlock(&stid->sc_lock);
909}
910
J. Bruce Fields353601e2018-02-16 14:29:42 -0500911static void put_deleg_file(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Jeff Layton6bcc0342014-08-09 10:22:40 -0400913 struct file *filp = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -0500914
915 spin_lock(&fp->fi_lock);
916 if (--fp->fi_delegees == 0)
917 swap(filp, fp->fi_deleg_file);
918 spin_unlock(&fp->fi_lock);
919
920 if (filp)
921 fput(filp);
922}
923
924static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
925{
J. Bruce Fieldscba7b3d2018-02-15 15:50:49 -0500926 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fields353601e2018-02-16 14:29:42 -0500927 struct file *filp = fp->fi_deleg_file;
Jeff Layton417c6622014-07-21 09:34:57 -0400928
J. Bruce Fieldsb8232d32018-02-15 15:29:15 -0500929 WARN_ON_ONCE(!fp->fi_delegees);
930
J. Bruce Fields353601e2018-02-16 14:29:42 -0500931 vfs_setlease(filp, F_UNLCK, NULL, (void **)&dp);
932 put_deleg_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
J. Bruce Fields0af6e692018-02-21 15:11:03 -0500935static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
936{
937 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -0500938 nfs4_unlock_deleg_lease(dp);
J. Bruce Fields0af6e692018-02-21 15:11:03 -0500939 nfs4_put_stid(&dp->dl_stid);
940}
941
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200942void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400943{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500944 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400945}
946
Andrew Elble34ed9872015-10-15 12:07:28 -0400947/**
J. Bruce Fields68b18f52018-02-19 11:38:33 -0500948 * nfs4_delegation_exists - Discover if this delegation already exists
Andrew Elble34ed9872015-10-15 12:07:28 -0400949 * @clp: a pointer to the nfs4_client we're granting a delegation to
950 * @fp: a pointer to the nfs4_file we're granting a delegation on
951 *
952 * Return:
J. Bruce Fields68b18f52018-02-19 11:38:33 -0500953 * On success: true iff an existing delegation is found
Andrew Elble34ed9872015-10-15 12:07:28 -0400954 */
955
J. Bruce Fields68b18f52018-02-19 11:38:33 -0500956static bool
957nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
Benny Halevy931ee562014-05-30 09:09:27 -0400958{
Andrew Elble34ed9872015-10-15 12:07:28 -0400959 struct nfs4_delegation *searchdp = NULL;
960 struct nfs4_client *searchclp = NULL;
961
Benny Halevycdc975052014-05-30 09:09:30 -0400962 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400963 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400964
Andrew Elble34ed9872015-10-15 12:07:28 -0400965 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
966 searchclp = searchdp->dl_stid.sc_client;
967 if (clp == searchclp) {
Fengguang Wu51d87bc2018-03-22 13:37:20 +0800968 return true;
Andrew Elble34ed9872015-10-15 12:07:28 -0400969 }
970 }
Fengguang Wu51d87bc2018-03-22 13:37:20 +0800971 return false;
Andrew Elble34ed9872015-10-15 12:07:28 -0400972}
973
974/**
975 * hash_delegation_locked - Add a delegation to the appropriate lists
976 * @dp: a pointer to the nfs4_delegation we are adding.
977 * @fp: a pointer to the nfs4_file we're granting a delegation on
978 *
979 * Return:
980 * On success: NULL if the delegation was successfully hashed.
981 *
982 * On error: -EAGAIN if one was previously granted to this
983 * nfs4_client for this nfs4_file. Delegation is not hashed.
984 *
985 */
986
987static int
988hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
989{
Andrew Elble34ed9872015-10-15 12:07:28 -0400990 struct nfs4_client *clp = dp->dl_stid.sc_client;
991
992 lockdep_assert_held(&state_lock);
993 lockdep_assert_held(&fp->fi_lock);
994
J. Bruce Fields68b18f52018-02-19 11:38:33 -0500995 if (nfs4_delegation_exists(clp, fp))
996 return -EAGAIN;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300997 refcount_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400998 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400999 list_add(&dp->dl_perfile, &fp->fi_delegations);
Andrew Elble34ed9872015-10-15 12:07:28 -04001000 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1001 return 0;
Benny Halevy931ee562014-05-30 09:09:27 -04001002}
1003
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001004static bool
Jeff Layton42690672014-07-25 07:34:20 -04001005unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Trond Myklebust11b91642014-07-29 21:34:08 -04001007 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -04001008
Jeff Layton42690672014-07-25 07:34:20 -04001009 lockdep_assert_held(&state_lock);
1010
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001011 if (list_empty(&dp->dl_perfile))
1012 return false;
1013
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04001014 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -04001015 /* Ensure that deleg break won't try to requeue it */
1016 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -04001017 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001018 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -04001020 list_del_init(&dp->dl_perfile);
1021 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001022 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001023}
1024
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001025static void destroy_delegation(struct nfs4_delegation *dp)
1026{
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001027 bool unhashed;
1028
Jeff Layton42690672014-07-25 07:34:20 -04001029 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001030 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -04001031 spin_unlock(&state_lock);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001032 if (unhashed)
1033 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001034}
1035
1036static void revoke_delegation(struct nfs4_delegation *dp)
1037{
1038 struct nfs4_client *clp = dp->dl_stid.sc_client;
1039
Jeff Layton2d4a5322014-07-25 07:34:21 -04001040 WARN_ON(!list_empty(&dp->dl_recall_lru));
1041
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001042 if (clp->cl_minorversion) {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001043 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001044 refcount_inc(&dp->dl_stid.sc_count);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001045 spin_lock(&clp->cl_lock);
1046 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1047 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001048 }
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001049 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001050}
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052/*
1053 * SETCLIENTID state
1054 */
1055
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001056static unsigned int clientid_hashval(u32 id)
1057{
1058 return id & CLIENT_HASH_MASK;
1059}
1060
1061static unsigned int clientstr_hashval(const char *name)
1062{
1063 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
1064}
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001067 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1068 * st_{access,deny}_bmap field of the stateid, in order to track not
1069 * only what share bits are currently in force, but also what
1070 * combinations of share bits previous opens have used. This allows us
1071 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1072 * return an error if the client attempt to downgrade to a combination
1073 * of share bits not explicable by closing some of its previous opens.
1074 *
1075 * XXX: This enforcement is actually incomplete, since we don't keep
1076 * track of access/deny bit combinations; so, e.g., we allow:
1077 *
1078 * OPEN allow read, deny write
1079 * OPEN allow both, deny none
1080 * DOWNGRADE allow read, deny none
1081 *
1082 * which we should reject.
1083 */
Jeff Layton5ae037e2012-05-11 09:45:11 -04001084static unsigned int
1085bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001086 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -04001087 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001088
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001089 for (i = 1; i < 4; i++) {
1090 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -04001091 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001092 }
Jeff Layton5ae037e2012-05-11 09:45:11 -04001093 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001094}
1095
Jeff Layton82c5ff12012-05-11 09:45:13 -04001096/* set share access for a given stateid */
1097static inline void
1098set_access(u32 access, struct nfs4_ol_stateid *stp)
1099{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001100 unsigned char mask = 1 << access;
1101
1102 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1103 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001104}
1105
1106/* clear share access for a given stateid */
1107static inline void
1108clear_access(u32 access, struct nfs4_ol_stateid *stp)
1109{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001110 unsigned char mask = 1 << access;
1111
1112 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1113 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001114}
1115
1116/* test whether a given stateid has access */
1117static inline bool
1118test_access(u32 access, struct nfs4_ol_stateid *stp)
1119{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001120 unsigned char mask = 1 << access;
1121
1122 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001123}
1124
Jeff Laytonce0fc432012-05-11 09:45:14 -04001125/* set share deny for a given stateid */
1126static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001127set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001128{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001129 unsigned char mask = 1 << deny;
1130
1131 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1132 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001133}
1134
1135/* clear share deny for a given stateid */
1136static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001137clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001138{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001139 unsigned char mask = 1 << deny;
1140
1141 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1142 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001143}
1144
1145/* test whether a given stateid is denying specific access */
1146static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -04001147test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001148{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001149 unsigned char mask = 1 << deny;
1150
1151 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001152}
1153
1154static int nfs4_access_to_omode(u32 access)
1155{
J. Bruce Fields8f34a432010-09-02 15:23:16 -04001156 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001157 case NFS4_SHARE_ACCESS_READ:
1158 return O_RDONLY;
1159 case NFS4_SHARE_ACCESS_WRITE:
1160 return O_WRONLY;
1161 case NFS4_SHARE_ACCESS_BOTH:
1162 return O_RDWR;
1163 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001164 WARN_ON_ONCE(1);
1165 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001166}
1167
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001168/*
1169 * A stateid that had a deny mode associated with it is being released
1170 * or downgraded. Recalculate the deny mode on the file.
1171 */
1172static void
1173recalculate_deny_mode(struct nfs4_file *fp)
1174{
1175 struct nfs4_ol_stateid *stp;
1176
1177 spin_lock(&fp->fi_lock);
1178 fp->fi_share_deny = 0;
1179 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1180 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1181 spin_unlock(&fp->fi_lock);
1182}
1183
1184static void
1185reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1186{
1187 int i;
1188 bool change = false;
1189
1190 for (i = 1; i < 4; i++) {
1191 if ((i & deny) != i) {
1192 change = true;
1193 clear_deny(i, stp);
1194 }
1195 }
1196
1197 /* Recalculate per-file deny mode if there was a change */
1198 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -04001199 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001200}
1201
Jeff Layton82c5ff12012-05-11 09:45:13 -04001202/* release all access and file references for a given stateid */
1203static void
1204release_all_access(struct nfs4_ol_stateid *stp)
1205{
1206 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -04001207 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001208
1209 if (fp && stp->st_deny_bmap != 0)
1210 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001211
1212 for (i = 1; i < 4; i++) {
1213 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -04001214 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001215 clear_access(i, stp);
1216 }
1217}
1218
Kinglong Meed50ffde2015-07-16 12:05:07 +08001219static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1220{
1221 kfree(sop->so_owner.data);
1222 sop->so_ops->so_free(sop);
1223}
1224
Jeff Layton6b180f02014-07-29 21:34:26 -04001225static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1226{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001227 struct nfs4_client *clp = sop->so_client;
1228
1229 might_lock(&clp->cl_lock);
1230
1231 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001232 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001233 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001234 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001235 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001236}
1237
Jeff Laytone8568732015-08-24 12:41:47 -04001238static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001239{
Trond Myklebust11b91642014-07-29 21:34:08 -04001240 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001241
Jeff Layton1c755dc2014-07-29 21:34:12 -04001242 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1243
Jeff Laytone8568732015-08-24 12:41:47 -04001244 if (list_empty(&stp->st_perfile))
1245 return false;
1246
Trond Myklebust1d31a252014-07-10 14:07:25 -04001247 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001248 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001249 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001250 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001251 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001252}
1253
Trond Myklebust60116952014-07-29 21:34:06 -04001254static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001255{
Trond Myklebust60116952014-07-29 21:34:06 -04001256 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001257
Sachin Bhamare8287f002015-04-27 14:50:14 +02001258 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001259 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001260 if (stp->st_stateowner)
1261 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -04001262 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001263}
1264
Jeff Laytonb49e0842014-07-29 21:34:11 -04001265static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001266{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001267 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1268 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001269 struct file *file;
1270
Jeff Laytonb49e0842014-07-29 21:34:11 -04001271 file = find_any_file(stp->st_stid.sc_file);
1272 if (file)
1273 filp_close(file, (fl_owner_t)lo);
1274 nfs4_free_ol_stateid(stid);
1275}
1276
Jeff Layton2c41beb2014-07-29 21:34:41 -04001277/*
1278 * Put the persistent reference to an already unhashed generic stateid, while
1279 * holding the cl_lock. If it's the last reference, then put it onto the
1280 * reaplist for later destruction.
1281 */
1282static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1283 struct list_head *reaplist)
1284{
1285 struct nfs4_stid *s = &stp->st_stid;
1286 struct nfs4_client *clp = s->sc_client;
1287
1288 lockdep_assert_held(&clp->cl_lock);
1289
1290 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1291
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001292 if (!refcount_dec_and_test(&s->sc_count)) {
Jeff Layton2c41beb2014-07-29 21:34:41 -04001293 wake_up_all(&close_wq);
1294 return;
1295 }
1296
1297 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1298 list_add(&stp->st_locks, reaplist);
1299}
1300
Jeff Laytone8568732015-08-24 12:41:47 -04001301static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001302{
Chuck Leverf46c4452016-10-29 18:19:03 -04001303 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001304
1305 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001306 nfs4_unhash_stid(&stp->st_stid);
Jeff Laytone8568732015-08-24 12:41:47 -04001307 return unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001308}
1309
Jeff Layton5adfd882014-07-29 21:34:31 -04001310static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001311{
Chuck Leverf46c4452016-10-29 18:19:03 -04001312 struct nfs4_client *clp = stp->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04001313 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001314
Chuck Leverf46c4452016-10-29 18:19:03 -04001315 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001316 unhashed = unhash_lock_stateid(stp);
Chuck Leverf46c4452016-10-29 18:19:03 -04001317 spin_unlock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001318 if (unhashed)
1319 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001320}
1321
Trond Myklebustc58c6612014-07-29 21:34:35 -04001322static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001323{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001324 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001325
Trond Myklebustd4f04892014-07-29 21:34:36 -04001326 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001327
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001328 list_del_init(&lo->lo_owner.so_strhash);
1329}
1330
Jeff Layton2c41beb2014-07-29 21:34:41 -04001331/*
1332 * Free a list of generic stateids that were collected earlier after being
1333 * fully unhashed.
1334 */
1335static void
1336free_ol_stateid_reaplist(struct list_head *reaplist)
1337{
1338 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001339 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001340
1341 might_sleep();
1342
1343 while (!list_empty(reaplist)) {
1344 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1345 st_locks);
1346 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001347 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001348 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001349 if (fp)
1350 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001351 }
1352}
1353
Jeff Laytond83017f2014-07-29 21:34:42 -04001354static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1355 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001356{
1357 struct nfs4_ol_stateid *stp;
1358
Jeff Laytone8568732015-08-24 12:41:47 -04001359 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1360
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001361 while (!list_empty(&open_stp->st_locks)) {
1362 stp = list_entry(open_stp->st_locks.next,
1363 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001364 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001365 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001366 }
1367}
1368
Jeff Laytone8568732015-08-24 12:41:47 -04001369static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001370 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001371{
Jeff Laytone8568732015-08-24 12:41:47 -04001372 bool unhashed;
1373
Jeff Layton2c41beb2014-07-29 21:34:41 -04001374 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1375
Jeff Laytone8568732015-08-24 12:41:47 -04001376 unhashed = unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001377 release_open_stateid_locks(stp, reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04001378 return unhashed;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001379}
1380
1381static void release_open_stateid(struct nfs4_ol_stateid *stp)
1382{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001383 LIST_HEAD(reaplist);
1384
1385 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001386 if (unhash_open_stateid(stp, &reaplist))
1387 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001388 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1389 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001390}
1391
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001392static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001393{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001394 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001395
Trond Myklebustd4f04892014-07-29 21:34:36 -04001396 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001397
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001398 list_del_init(&oo->oo_owner.so_strhash);
1399 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001400}
1401
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001402static void release_last_closed_stateid(struct nfs4_openowner *oo)
1403{
Jeff Layton217526e2014-07-30 08:27:11 -04001404 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1405 nfsd_net_id);
1406 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001407
Jeff Layton217526e2014-07-30 08:27:11 -04001408 spin_lock(&nn->client_lock);
1409 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001410 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001411 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001412 oo->oo_last_closed_stid = NULL;
1413 }
Jeff Layton217526e2014-07-30 08:27:11 -04001414 spin_unlock(&nn->client_lock);
1415 if (s)
1416 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001417}
1418
Jeff Layton2c41beb2014-07-29 21:34:41 -04001419static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001420{
1421 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001422 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001423 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001424
Jeff Layton2c41beb2014-07-29 21:34:41 -04001425 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001426
Trond Myklebustd4f04892014-07-29 21:34:36 -04001427 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001428 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001429 while (!list_empty(&oo->oo_owner.so_stateids)) {
1430 stp = list_first_entry(&oo->oo_owner.so_stateids,
1431 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001432 if (unhash_open_stateid(stp, &reaplist))
1433 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001434 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001435 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001436 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001437 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001438 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001439}
1440
Marc Eshel5282fd72009-04-03 08:27:52 +03001441static inline int
1442hash_sessionid(struct nfs4_sessionid *sessionid)
1443{
1444 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1445
1446 return sid->sequence % SESSION_HASH_SIZE;
1447}
1448
Mark Salter135dd002015-04-06 09:46:00 -04001449#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001450static inline void
1451dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1452{
1453 u32 *ptr = (u32 *)(&sessionid->data[0]);
1454 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1455}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001456#else
1457static inline void
1458dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1459{
1460}
1461#endif
1462
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001463/*
1464 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1465 * won't be used for replay.
1466 */
1467void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1468{
1469 struct nfs4_stateowner *so = cstate->replay_owner;
1470
1471 if (nfserr == nfserr_replay_me)
1472 return;
1473
1474 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001475 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001476 return;
1477 }
1478 if (!so)
1479 return;
1480 if (so->so_is_open_owner)
1481 release_last_closed_stateid(openowner(so));
1482 so->so_seqid++;
1483 return;
1484}
Marc Eshel5282fd72009-04-03 08:27:52 +03001485
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001486static void
1487gen_sessionid(struct nfsd4_session *ses)
1488{
1489 struct nfs4_client *clp = ses->se_client;
1490 struct nfsd4_sessionid *sid;
1491
1492 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1493 sid->clientid = clp->cl_clientid;
1494 sid->sequence = current_sessionid++;
1495 sid->reserved = 0;
1496}
1497
1498/*
Andy Adamsona649637c72009-08-28 08:45:01 -04001499 * The protocol defines ca_maxresponssize_cached to include the size of
1500 * the rpc header, but all we need to cache is the data starting after
1501 * the end of the initial SEQUENCE operation--the rest we regenerate
1502 * each time. Therefore we can advertise a ca_maxresponssize_cached
1503 * value that is the number of bytes in our cache plus a few additional
1504 * bytes. In order to stay on the safe side, and not promise more than
1505 * we can cache, those additional bytes must be the minimum possible: 24
1506 * bytes of rpc header (xid through accept state, with AUTH_NULL
1507 * verifier), 12 for the compound header (with zero-length tag), and 44
1508 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001509 */
Andy Adamsona649637c72009-08-28 08:45:01 -04001510#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1511
Andy Adamson557ce262009-08-28 08:45:04 -04001512static void
1513free_session_slots(struct nfsd4_session *ses)
1514{
1515 int i;
1516
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001517 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1518 free_svc_cred(&ses->se_slots[i]->sl_cred);
Andy Adamson557ce262009-08-28 08:45:04 -04001519 kfree(ses->se_slots[i]);
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001520 }
Andy Adamson557ce262009-08-28 08:45:04 -04001521}
1522
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001523/*
1524 * We don't actually need to cache the rpc and session headers, so we
1525 * can allocate a little less for each slot:
1526 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001527static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001528{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001529 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001530
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001531 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1532 size = 0;
1533 else
1534 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1535 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001536}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001537
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001538/*
1539 * XXX: If we run out of reserved DRC memory we could (up to a point)
1540 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001541 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001542 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001543static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001544{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001545 u32 slotsize = slot_bytes(ca);
1546 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001547 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001548
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001549 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001550 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1551 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001552 /*
1553 * Never use more than a third of the remaining memory,
1554 * unless it's the only way to give this client a slot:
1555 */
1556 avail = clamp_t(int, avail, slotsize, avail/3);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001557 num = min_t(int, num, avail / slotsize);
1558 nfsd_drc_mem_used += num * slotsize;
1559 spin_unlock(&nfsd_drc_lock);
1560
1561 return num;
1562}
1563
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001564static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001565{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001566 int slotsize = slot_bytes(ca);
1567
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001568 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001569 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001570 spin_unlock(&nfsd_drc_lock);
1571}
1572
Kinglong Mee60810e52014-01-01 00:35:47 +08001573static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1574 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001575{
Kinglong Mee60810e52014-01-01 00:35:47 +08001576 int numslots = fattrs->maxreqs;
1577 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001578 struct nfsd4_session *new;
1579 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001580
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001581 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001582 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1583 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001584
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001585 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001586 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001587 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001588 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001589 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001590 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001591 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001592 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001593 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001594
1595 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1596 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1597
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001598 return new;
1599out_free:
1600 while (i--)
1601 kfree(new->se_slots[i]);
1602 kfree(new);
1603 return NULL;
1604}
1605
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001606static void free_conn(struct nfsd4_conn *c)
1607{
1608 svc_xprt_put(c->cn_xprt);
1609 kfree(c);
1610}
1611
1612static void nfsd4_conn_lost(struct svc_xpt_user *u)
1613{
1614 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1615 struct nfs4_client *clp = c->cn_session->se_client;
1616
1617 spin_lock(&clp->cl_lock);
1618 if (!list_empty(&c->cn_persession)) {
1619 list_del(&c->cn_persession);
1620 free_conn(c);
1621 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001622 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001623 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001624}
1625
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001626static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001627{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001628 struct nfsd4_conn *conn;
1629
1630 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1631 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001632 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001633 svc_xprt_get(rqstp->rq_xprt);
1634 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001635 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001636 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1637 return conn;
1638}
1639
J. Bruce Fields328ead22010-09-29 16:11:06 -04001640static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1641{
1642 conn->cn_session = ses;
1643 list_add(&conn->cn_persession, &ses->se_conns);
1644}
1645
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001646static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1647{
1648 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001649
1650 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001651 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001652 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001653}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001654
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001655static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001656{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001657 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001658 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001659}
1660
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001661static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001662{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001663 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001664
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001665 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001666 ret = nfsd4_register_conn(conn);
1667 if (ret)
1668 /* oops; xprt is already down: */
1669 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001670 /* We may have gained or lost a callback channel: */
1671 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001672}
1673
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001674static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001675{
1676 u32 dir = NFS4_CDFC4_FORE;
1677
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001678 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001679 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001680 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001681}
1682
1683/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001684static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001685{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001686 struct nfs4_client *clp = s->se_client;
1687 struct nfsd4_conn *c;
1688
1689 spin_lock(&clp->cl_lock);
1690 while (!list_empty(&s->se_conns)) {
1691 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1692 list_del_init(&c->cn_persession);
1693 spin_unlock(&clp->cl_lock);
1694
1695 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1696 free_conn(c);
1697
1698 spin_lock(&clp->cl_lock);
1699 }
1700 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001701}
1702
J. Bruce Fields1377b692012-09-11 21:42:40 -04001703static void __free_session(struct nfsd4_session *ses)
1704{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001705 free_session_slots(ses);
1706 kfree(ses);
1707}
1708
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001709static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001710{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001711 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001712 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001713 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001714}
Andy Adamson557ce262009-08-28 08:45:04 -04001715
Fengguang Wu135ae822012-11-10 07:20:25 -05001716static 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 -04001717{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001718 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001719 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001720
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001721 new->se_client = clp;
1722 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001723
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001724 INIT_LIST_HEAD(&new->se_conns);
1725
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001726 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001727 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001728 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001729 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001730 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001731 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001732 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001733 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001734 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001735 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001736
Chuck Leverb0d2e422014-08-22 15:10:59 -04001737 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001738 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001739 /*
1740 * This is a little silly; with sessions there's no real
1741 * use for the callback address. Use the peer address
1742 * as a reasonable default for now, but consider fixing
1743 * the rpc client not to require an address in the
1744 * future:
1745 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001746 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1747 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001748 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001749}
1750
Benny Halevy9089f1b2010-05-12 00:12:26 +03001751/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001752static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001753__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001754{
1755 struct nfsd4_session *elem;
1756 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001757 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001758
Trond Myklebust0a880a22014-07-30 08:27:10 -04001759 lockdep_assert_held(&nn->client_lock);
1760
Marc Eshel5282fd72009-04-03 08:27:52 +03001761 dump_sessionid(__func__, sessionid);
1762 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001763 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001764 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001765 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1766 NFS4_MAX_SESSIONID_LEN)) {
1767 return elem;
1768 }
1769 }
1770
1771 dprintk("%s: session not found\n", __func__);
1772 return NULL;
1773}
1774
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001775static struct nfsd4_session *
1776find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1777 __be32 *ret)
1778{
1779 struct nfsd4_session *session;
1780 __be32 status = nfserr_badsession;
1781
1782 session = __find_in_sessionid_hashtbl(sessionid, net);
1783 if (!session)
1784 goto out;
1785 status = nfsd4_get_session_locked(session);
1786 if (status)
1787 session = NULL;
1788out:
1789 *ret = status;
1790 return session;
1791}
1792
Benny Halevy9089f1b2010-05-12 00:12:26 +03001793/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001794static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001795unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001796{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001797 struct nfs4_client *clp = ses->se_client;
1798 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1799
1800 lockdep_assert_held(&nn->client_lock);
1801
Andy Adamson7116ed62009-04-03 08:27:43 +03001802 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001803 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001804 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001805 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001806}
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1809static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001810STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001812 /*
1813 * We're assuming the clid was not given out from a boot
1814 * precisely 2^32 (about 136 years) before this one. That seems
1815 * a safe assumption:
1816 */
1817 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001819 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001820 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return 1;
1822}
1823
1824/*
1825 * XXX Should we use a slab cache ?
1826 * This type of memory management is somewhat inefficient, but we use it
1827 * anyway since SETCLIENTID is not a common operation.
1828 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001829static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
1831 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001832 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Jeff Layton9258a2d2018-03-16 09:47:22 -04001834 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001835 if (clp == NULL)
1836 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001837 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001838 if (clp->cl_name.data == NULL)
1839 goto err_no_name;
Kees Cook6da2ec52018-06-12 13:55:00 -07001840 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1841 sizeof(struct list_head),
1842 GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001843 if (!clp->cl_ownerstr_hashtbl)
1844 goto err_no_hashtbl;
1845 for (i = 0; i < OWNER_HASH_SIZE; i++)
1846 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001847 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001848 INIT_LIST_HEAD(&clp->cl_sessions);
1849 idr_init(&clp->cl_stateids);
1850 atomic_set(&clp->cl_refcount, 0);
1851 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1852 INIT_LIST_HEAD(&clp->cl_idhash);
1853 INIT_LIST_HEAD(&clp->cl_openowners);
1854 INIT_LIST_HEAD(&clp->cl_delegations);
1855 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04001856 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001857#ifdef CONFIG_NFSD_PNFS
1858 INIT_LIST_HEAD(&clp->cl_lo_states);
1859#endif
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001860 INIT_LIST_HEAD(&clp->async_copies);
1861 spin_lock_init(&clp->async_lock);
Trond Myklebust5694c932014-04-18 14:43:56 -04001862 spin_lock_init(&clp->cl_lock);
1863 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001865err_no_hashtbl:
1866 kfree(clp->cl_name.data);
1867err_no_name:
Jeff Layton9258a2d2018-03-16 09:47:22 -04001868 kmem_cache_free(client_slab, clp);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001869 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870}
1871
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001872static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873free_client(struct nfs4_client *clp)
1874{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001875 while (!list_empty(&clp->cl_sessions)) {
1876 struct nfsd4_session *ses;
1877 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1878 se_perclnt);
1879 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001880 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1881 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001882 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001883 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001884 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001885 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001887 idr_destroy(&clp->cl_stateids);
Jeff Layton9258a2d2018-03-16 09:47:22 -04001888 kmem_cache_free(client_slab, clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889}
1890
Benny Halevy84d38ac2010-05-12 00:13:16 +03001891/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001892static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001893unhash_client_locked(struct nfs4_client *clp)
1894{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001895 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001896 struct nfsd4_session *ses;
1897
Trond Myklebust0a880a22014-07-30 08:27:10 -04001898 lockdep_assert_held(&nn->client_lock);
1899
Trond Myklebust4beb3452014-07-30 08:27:02 -04001900 /* Mark the client as expired! */
1901 clp->cl_time = 0;
1902 /* Make it invisible */
1903 if (!list_empty(&clp->cl_idhash)) {
1904 list_del_init(&clp->cl_idhash);
1905 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1906 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1907 else
1908 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1909 }
1910 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001911 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001912 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1913 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001914 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001915}
1916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001918unhash_client(struct nfs4_client *clp)
1919{
1920 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1921
1922 spin_lock(&nn->client_lock);
1923 unhash_client_locked(clp);
1924 spin_unlock(&nn->client_lock);
1925}
1926
Jeff Layton97403d92014-07-30 08:27:12 -04001927static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1928{
1929 if (atomic_read(&clp->cl_refcount))
1930 return nfserr_jukebox;
1931 unhash_client_locked(clp);
1932 return nfs_ok;
1933}
1934
Trond Myklebust4beb3452014-07-30 08:27:02 -04001935static void
1936__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
Jeff Layton68ef3bc2018-03-16 11:32:02 -04001938 int i;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001939 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 struct list_head reaplist;
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04001944 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001945 while (!list_empty(&clp->cl_delegations)) {
1946 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001947 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04001948 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 }
Benny Halevycdc975052014-05-30 09:09:30 -04001950 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 while (!list_empty(&reaplist)) {
1952 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001953 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001954 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001956 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05001957 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001958 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001959 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001960 }
NeilBrownea1da632005-06-23 22:04:17 -07001961 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001962 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04001963 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001964 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
Jeff Layton68ef3bc2018-03-16 11:32:02 -04001966 for (i = 0; i < OWNER_HASH_SIZE; i++) {
1967 struct nfs4_stateowner *so, *tmp;
1968
1969 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
1970 so_strhash) {
1971 /* Should be no openowners at this point */
1972 WARN_ON_ONCE(so->so_is_open_owner);
1973 remove_blocked_locks(lockowner(so));
1974 }
1975 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001976 nfsd4_return_all_client_layouts(clp);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001977 nfsd4_shutdown_copy(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001978 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001979 if (clp->cl_cb_conn.cb_xprt)
1980 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001981 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
Trond Myklebust4beb3452014-07-30 08:27:02 -04001984static void
1985destroy_client(struct nfs4_client *clp)
1986{
1987 unhash_client(clp);
1988 __destroy_client(clp);
1989}
1990
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001991static void expire_client(struct nfs4_client *clp)
1992{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001993 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001994 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001995 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001996}
1997
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001998static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1999{
2000 memcpy(target->cl_verifier.data, source->data,
2001 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002}
2003
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002004static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2005{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2007 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2008}
2009
J. Bruce Fields50043852015-11-20 15:58:37 -05002010static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2011{
NeilBrown2f10fdc2017-03-23 16:57:36 +08002012 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2013 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2014 GFP_KERNEL);
Chuck Lever9abdda52018-08-16 12:05:59 -04002015 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2016 if ((source->cr_principal && !target->cr_principal) ||
2017 (source->cr_raw_principal && !target->cr_raw_principal) ||
2018 (source->cr_targ_princ && !target->cr_targ_princ))
NeilBrown2f10fdc2017-03-23 16:57:36 +08002019 return -ENOMEM;
J. Bruce Fields50043852015-11-20 15:58:37 -05002020
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04002021 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 target->cr_uid = source->cr_uid;
2023 target->cr_gid = source->cr_gid;
2024 target->cr_group_info = source->cr_group_info;
2025 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04002026 target->cr_gss_mech = source->cr_gss_mech;
2027 if (source->cr_gss_mech)
2028 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002029 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002032static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002033compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2034{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002035 if (o1->len < o2->len)
2036 return -1;
2037 if (o1->len > o2->len)
2038 return 1;
2039 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002040}
2041
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002042static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002043{
NeilBrowna55370a2005-06-23 22:03:52 -07002044 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045}
2046
2047static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002048same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2049{
2050 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051}
2052
2053static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002054same_clid(clientid_t *cl1, clientid_t *cl2)
2055{
2056 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057}
2058
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002059static bool groups_equal(struct group_info *g1, struct group_info *g2)
2060{
2061 int i;
2062
2063 if (g1->ngroups != g2->ngroups)
2064 return false;
2065 for (i=0; i<g1->ngroups; i++)
Alexey Dobriyan81243ea2016-10-07 17:03:12 -07002066 if (!gid_eq(g1->gid[i], g2->gid[i]))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002067 return false;
2068 return true;
2069}
2070
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002071/*
2072 * RFC 3530 language requires clid_inuse be returned when the
2073 * "principal" associated with a requests differs from that previously
2074 * used. We use uid, gid's, and gss principal string as our best
2075 * approximation. We also don't want to allow non-gss use of a client
2076 * established using gss: in theory cr_principal should catch that
2077 * change, but in practice cr_principal can be null even in the gss case
2078 * since gssd doesn't always pass down a principal string.
2079 */
2080static bool is_gss_cred(struct svc_cred *cr)
2081{
2082 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2083 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2084}
2085
2086
Vivek Trivedi5559b502012-07-24 21:18:20 +05302087static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002088same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2089{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002090 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002091 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2092 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002093 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2094 return false;
Chuck Lever9abdda52018-08-16 12:05:59 -04002095 /* XXX: check that cr_targ_princ fields match ? */
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002096 if (cr1->cr_principal == cr2->cr_principal)
2097 return true;
2098 if (!cr1->cr_principal || !cr2->cr_principal)
2099 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05302100 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
2102
J. Bruce Fields57266a62013-04-13 14:27:29 -04002103static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2104{
2105 struct svc_cred *cr = &rqstp->rq_cred;
2106 u32 service;
2107
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04002108 if (!cr->cr_gss_mech)
2109 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002110 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2111 return service == RPC_GSS_SVC_INTEGRITY ||
2112 service == RPC_GSS_SVC_PRIVACY;
2113}
2114
Andrew Elblededeb132016-06-15 12:52:08 -04002115bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
J. Bruce Fields57266a62013-04-13 14:27:29 -04002116{
2117 struct svc_cred *cr = &rqstp->rq_cred;
2118
2119 if (!cl->cl_mach_cred)
2120 return true;
2121 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2122 return false;
2123 if (!svc_rqst_integrity_protected(rqstp))
2124 return false;
J. Bruce Fields414ca012015-11-20 10:48:02 -05002125 if (cl->cl_cred.cr_raw_principal)
2126 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2127 cr->cr_raw_principal);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002128 if (!cr->cr_principal)
2129 return false;
2130 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2131}
2132
Jeff Layton294ac322014-07-30 08:27:15 -04002133static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05002134{
Chuck Leverab4684d2012-03-02 17:13:50 -05002135 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Jeff Laytonf4199922014-06-17 07:44:11 -04002137 /*
2138 * This is opaque to client, so no need to byte-swap. Use
2139 * __force to keep sparse happy
2140 */
2141 verf[0] = (__force __be32)get_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08002142 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05002143 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
Jeff Layton294ac322014-07-30 08:27:15 -04002146static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2147{
2148 clp->cl_clientid.cl_boot = nn->boot_time;
2149 clp->cl_clientid.cl_id = nn->clientid_counter++;
2150 gen_confirm(clp, nn);
2151}
2152
Jeff Layton4770d722014-07-29 21:34:10 -04002153static struct nfs4_stid *
2154find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04002155{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002156 struct nfs4_stid *ret;
2157
2158 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2159 if (!ret || !ret->sc_type)
2160 return NULL;
2161 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04002162}
2163
Jeff Layton4770d722014-07-29 21:34:10 -04002164static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04002165find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002166{
2167 struct nfs4_stid *s;
2168
Jeff Layton4770d722014-07-29 21:34:10 -04002169 spin_lock(&cl->cl_lock);
2170 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002171 if (s != NULL) {
2172 if (typemask & s->sc_type)
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03002173 refcount_inc(&s->sc_count);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002174 else
2175 s = NULL;
2176 }
Jeff Layton4770d722014-07-29 21:34:10 -04002177 spin_unlock(&cl->cl_lock);
2178 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04002179}
2180
Jeff Layton2216d442012-11-12 15:00:57 -05002181static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002182 struct svc_rqst *rqstp, nfs4_verifier *verf)
2183{
2184 struct nfs4_client *clp;
2185 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002186 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002187 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002188
2189 clp = alloc_client(name);
2190 if (clp == NULL)
2191 return NULL;
2192
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002193 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2194 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002195 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002196 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002197 }
Christoph Hellwig0162ac22014-09-24 12:19:19 +02002198 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Benny Halevy07cd4902010-05-12 00:13:41 +03002199 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002200 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002201 copy_verf(clp, verf);
2202 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04002203 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002204 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002205 return clp;
2206}
2207
NeilBrownfd39ca92005-06-23 22:04:03 -07002208static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002209add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2210{
2211 struct rb_node **new = &(root->rb_node), *parent = NULL;
2212 struct nfs4_client *clp;
2213
2214 while (*new) {
2215 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2216 parent = *new;
2217
2218 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2219 new = &((*new)->rb_left);
2220 else
2221 new = &((*new)->rb_right);
2222 }
2223
2224 rb_link_node(&new_clp->cl_namenode, parent, new);
2225 rb_insert_color(&new_clp->cl_namenode, root);
2226}
2227
2228static struct nfs4_client *
2229find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2230{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002231 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002232 struct rb_node *node = root->rb_node;
2233 struct nfs4_client *clp;
2234
2235 while (node) {
2236 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2237 cmp = compare_blob(&clp->cl_name, name);
2238 if (cmp > 0)
2239 node = node->rb_left;
2240 else if (cmp < 0)
2241 node = node->rb_right;
2242 else
2243 return clp;
2244 }
2245 return NULL;
2246}
2247
2248static void
2249add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
2251 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002252 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Trond Myklebust0a880a22014-07-30 08:27:10 -04002254 lockdep_assert_held(&nn->client_lock);
2255
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002256 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002257 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002259 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002260 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261}
2262
NeilBrownfd39ca92005-06-23 22:04:03 -07002263static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264move_to_confirmed(struct nfs4_client *clp)
2265{
2266 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002267 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Trond Myklebust0a880a22014-07-30 08:27:10 -04002269 lockdep_assert_held(&nn->client_lock);
2270
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002272 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002273 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002274 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002275 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002276 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277}
2278
2279static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002280find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
2282 struct nfs4_client *clp;
2283 unsigned int idhashval = clientid_hashval(clid->cl_id);
2284
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002285 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002286 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002287 if ((bool)clp->cl_minorversion != sessions)
2288 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002289 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 }
2293 return NULL;
2294}
2295
2296static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002297find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2298{
2299 struct list_head *tbl = nn->conf_id_hashtbl;
2300
Trond Myklebust0a880a22014-07-30 08:27:10 -04002301 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002302 return find_client_in_id_table(tbl, clid, sessions);
2303}
2304
2305static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002306find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002308 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Trond Myklebust0a880a22014-07-30 08:27:10 -04002310 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002311 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312}
2313
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002314static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002315{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002316 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002317}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002318
NeilBrown28ce6052005-06-23 22:03:56 -07002319static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002320find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002321{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002322 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002323 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002324}
2325
2326static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002327find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002328{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002329 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002330 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002331}
2332
NeilBrownfd39ca92005-06-23 22:04:03 -07002333static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002334gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002336 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002337 struct sockaddr *sa = svc_addr(rqstp);
2338 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002339 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Jeff Layton7077ecb2009-08-14 12:57:58 -04002341 /* Currently, we only support tcp and tcp6 for the callback channel */
2342 if (se->se_callback_netid_len == 3 &&
2343 !memcmp(se->se_callback_netid_val, "tcp", 3))
2344 expected_family = AF_INET;
2345 else if (se->se_callback_netid_len == 4 &&
2346 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2347 expected_family = AF_INET6;
2348 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 goto out_err;
2350
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002351 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002352 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002353 (struct sockaddr *)&conn->cb_addr,
2354 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002355
J. Bruce Fields07263f12010-05-31 19:09:40 -04002356 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002358
J. Bruce Fields07263f12010-05-31 19:09:40 -04002359 if (conn->cb_addr.ss_family == AF_INET6)
2360 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002361
J. Bruce Fields07263f12010-05-31 19:09:40 -04002362 conn->cb_prog = se->se_callback_prog;
2363 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002364 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 return;
2366out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002367 conn->cb_addr.ss_family = AF_UNSPEC;
2368 conn->cb_addrlen = 0;
Rasmus Villemoes4ab495b2017-02-24 01:15:55 +01002369 dprintk("NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 "will not receive delegations\n",
2371 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 return;
2374}
2375
Andy Adamson074fe892009-04-03 08:28:15 +03002376/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002377 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002378 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002379static void
Andy Adamson074fe892009-04-03 08:28:15 +03002380nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2381{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002382 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002383 struct nfsd4_slot *slot = resp->cstate.slot;
2384 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002385
Andy Adamson557ce262009-08-28 08:45:04 -04002386 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002387
J. Bruce Fields085def32017-10-18 16:17:18 -04002388 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamson557ce262009-08-28 08:45:04 -04002389 slot->sl_opcnt = resp->opcnt;
2390 slot->sl_status = resp->cstate.status;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04002391 free_svc_cred(&slot->sl_cred);
2392 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
Andy Adamsonbf864a32009-04-03 08:28:35 +03002393
J. Bruce Fields085def32017-10-18 16:17:18 -04002394 if (!nfsd4_cache_this(resp)) {
2395 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002396 return;
2397 }
J. Bruce Fields085def32017-10-18 16:17:18 -04002398 slot->sl_flags |= NFSD4_SLOT_CACHED;
2399
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002400 base = resp->cstate.data_offset;
2401 slot->sl_datalen = buf->len - base;
2402 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Dan Carpenterd3f03402015-11-21 13:28:50 +03002403 WARN(1, "%s: sessions DRC could not cache compound\n",
2404 __func__);
Andy Adamson557ce262009-08-28 08:45:04 -04002405 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002406}
2407
2408/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002409 * Encode the replay sequence operation from the slot values.
2410 * If cachethis is FALSE encode the uncached rep error on the next
2411 * operation which sets resp->p and increments resp->opcnt for
2412 * nfs4svc_encode_compoundres.
2413 *
Andy Adamson074fe892009-04-03 08:28:15 +03002414 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002415static __be32
2416nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2417 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002418{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002419 struct nfsd4_op *op;
2420 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002421
Andy Adamsonabfabf82009-07-23 19:02:18 -04002422 /* Encode the replayed sequence operation */
2423 op = &args->ops[resp->opcnt - 1];
2424 nfsd4_encode_operation(resp, op);
2425
J. Bruce Fields085def32017-10-18 16:17:18 -04002426 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2427 return op->status;
2428 if (args->opcnt == 1) {
2429 /*
2430 * The original operation wasn't a solo sequence--we
2431 * always cache those--so this retry must not match the
2432 * original:
2433 */
2434 op->status = nfserr_seq_false_retry;
2435 } else {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002436 op = &args->ops[resp->opcnt++];
2437 op->status = nfserr_retry_uncached_rep;
2438 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002439 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002440 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002441}
2442
2443/*
Andy Adamson557ce262009-08-28 08:45:04 -04002444 * The sequence operation is not cached because we can use the slot and
2445 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002446 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002447static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002448nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2449 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002450{
Andy Adamson557ce262009-08-28 08:45:04 -04002451 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002452 struct xdr_stream *xdr = &resp->xdr;
2453 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002454 __be32 status;
2455
Andy Adamson557ce262009-08-28 08:45:04 -04002456 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002457
Andy Adamsonabfabf82009-07-23 19:02:18 -04002458 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002459 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002460 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002461
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002462 p = xdr_reserve_space(xdr, slot->sl_datalen);
2463 if (!p) {
2464 WARN_ON_ONCE(1);
2465 return nfserr_serverfault;
2466 }
2467 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2468 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002469
Andy Adamson557ce262009-08-28 08:45:04 -04002470 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002471 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002472}
2473
Andy Adamson0733d212009-04-03 08:28:01 +03002474/*
2475 * Set the exchange_id flags returned by the server.
2476 */
2477static void
2478nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2479{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002480#ifdef CONFIG_NFSD_PNFS
2481 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2482#else
Andy Adamson0733d212009-04-03 08:28:01 +03002483 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002484#endif
Andy Adamson0733d212009-04-03 08:28:01 +03002485
2486 /* Referrals are supported, Migration is not. */
2487 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2488
2489 /* set the wire flags to return to client. */
2490 clid->flags = new->cl_exchange_flags;
2491}
2492
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002493static bool client_has_openowners(struct nfs4_client *clp)
2494{
2495 struct nfs4_openowner *oo;
2496
2497 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2498 if (!list_empty(&oo->oo_owner.so_stateids))
2499 return true;
2500 }
2501 return false;
2502}
2503
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002504static bool client_has_state(struct nfs4_client *clp)
2505{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002506 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08002507#ifdef CONFIG_NFSD_PNFS
2508 || !list_empty(&clp->cl_lo_states)
2509#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002510 || !list_empty(&clp->cl_delegations)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002511 || !list_empty(&clp->cl_sessions)
2512 || !list_empty(&clp->async_copies);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002513}
2514
Al Virob37ad282006-10-19 23:28:59 -07002515__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02002516nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2517 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03002518{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002519 struct nfsd4_exchange_id *exid = &u->exchange_id;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002520 struct nfs4_client *conf, *new;
2521 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002522 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002523 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002524 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002525 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002526 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002527 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002528
Jeff Layton363168b2009-08-14 12:57:56 -04002529 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002530 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002531 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002532 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002533 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002534
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002535 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002536 return nfserr_inval;
2537
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002538 new = create_client(exid->clname, rqstp, &verf);
2539 if (new == NULL)
2540 return nfserr_jukebox;
2541
Andy Adamson0733d212009-04-03 08:28:01 +03002542 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002543 case SP4_MACH_CRED:
Andrew Elbleed941642016-06-15 12:52:09 -04002544 exid->spo_must_enforce[0] = 0;
2545 exid->spo_must_enforce[1] = (
2546 1 << (OP_BIND_CONN_TO_SESSION - 32) |
2547 1 << (OP_EXCHANGE_ID - 32) |
2548 1 << (OP_CREATE_SESSION - 32) |
2549 1 << (OP_DESTROY_SESSION - 32) |
2550 1 << (OP_DESTROY_CLIENTID - 32));
2551
2552 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
2553 1 << (OP_OPEN_DOWNGRADE) |
2554 1 << (OP_LOCKU) |
2555 1 << (OP_DELEGRETURN));
2556
2557 exid->spo_must_allow[1] &= (
2558 1 << (OP_TEST_STATEID - 32) |
2559 1 << (OP_FREE_STATEID - 32));
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002560 if (!svc_rqst_integrity_protected(rqstp)) {
2561 status = nfserr_inval;
2562 goto out_nolock;
2563 }
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002564 /*
2565 * Sometimes userspace doesn't give us a principal.
2566 * Which is a bug, really. Anyway, we can't enforce
2567 * MACH_CRED in that case, better to give up now:
2568 */
J. Bruce Fields414ca012015-11-20 10:48:02 -05002569 if (!new->cl_cred.cr_principal &&
2570 !new->cl_cred.cr_raw_principal) {
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002571 status = nfserr_serverfault;
2572 goto out_nolock;
2573 }
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002574 new->cl_mach_cred = true;
Andy Adamson0733d212009-04-03 08:28:01 +03002575 case SP4_NONE:
2576 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002577 default: /* checked by xdr code */
2578 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002579 case SP4_SSV:
Kinglong Mee8edf4b02016-02-26 22:36:42 +08002580 status = nfserr_encr_alg_unsupp;
2581 goto out_nolock;
Andy Adamson0733d212009-04-03 08:28:01 +03002582 }
2583
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002584 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002585 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002586 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002587 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002588 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2589 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2590
J. Bruce Fields136e6582012-05-12 20:37:23 -04002591 if (update) {
2592 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002593 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002594 goto out;
2595 }
Andrew Elblededeb132016-06-15 12:52:08 -04002596 if (!nfsd4_mach_creds_match(conf, rqstp)) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002597 status = nfserr_wrong_cred;
2598 goto out;
2599 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002600 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002601 status = nfserr_perm;
2602 goto out;
2603 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002604 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002605 status = nfserr_not_same;
2606 goto out;
2607 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002608 /* case 6 */
2609 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002610 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002611 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002612 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002613 if (client_has_state(conf)) {
2614 status = nfserr_clid_inuse;
2615 goto out;
2616 }
Andy Adamson0733d212009-04-03 08:28:01 +03002617 goto out_new;
2618 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002619 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002620 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002621 goto out_copy;
2622 }
2623 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002624 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002625 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002626 }
2627
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002628 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002629 status = nfserr_noent;
2630 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002631 }
2632
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002633 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002634 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002635 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002636
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002637 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002638out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002639 if (conf) {
2640 status = mark_client_expired_locked(conf);
2641 if (status)
2642 goto out;
2643 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002644 new->cl_minorversion = cstate->minorversion;
Andrew Elbleed941642016-06-15 12:52:09 -04002645 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
2646 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
Andy Adamson0733d212009-04-03 08:28:01 +03002647
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002648 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002649 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002650 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002651out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002652 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2653 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002654
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002655 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2656 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002657
2658 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002659 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002660 status = nfs_ok;
2661
2662out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002663 spin_unlock(&nn->client_lock);
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002664out_nolock:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002665 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002666 expire_client(new);
2667 if (unconf)
2668 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002669 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002670}
2671
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002672static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002673check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002674{
Andy Adamson88e588d2009-07-23 19:02:15 -04002675 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2676 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002677
2678 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002679 if (slot_inuse) {
2680 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002681 return nfserr_jukebox;
2682 else
2683 return nfserr_seq_misordered;
2684 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002685 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002686 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002687 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002688 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002689 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002690 return nfserr_seq_misordered;
2691}
2692
Andy Adamson49557cc2009-07-23 19:02:16 -04002693/*
2694 * Cache the create session result into the create session single DRC
2695 * slot cache by saving the xdr structure. sl_seqid has been set.
2696 * Do this for solo or embedded create session operations.
2697 */
2698static void
2699nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002700 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002701{
2702 slot->sl_status = nfserr;
2703 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2704}
2705
2706static __be32
2707nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2708 struct nfsd4_clid_slot *slot)
2709{
2710 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2711 return slot->sl_status;
2712}
2713
Mi Jinlong1b74c252011-07-14 14:50:17 +08002714#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2715 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2716 1 + /* MIN tag is length with zero, only length */ \
2717 3 + /* version, opcount, opcode */ \
2718 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2719 /* seqid, slotID, slotID, cache */ \
2720 4 ) * sizeof(__be32))
2721
2722#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2723 2 + /* verifier: AUTH_NULL, length 0 */\
2724 1 + /* status */ \
2725 1 + /* MIN tag is length with zero, only length */ \
2726 3 + /* opcount, opcode, opstatus*/ \
2727 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2728 /* seqid, slotID, slotID, slotID, status */ \
2729 5 ) * sizeof(__be32))
2730
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002731static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002732{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002733 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2734
J. Bruce Fields373cd402013-04-08 15:42:12 -04002735 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2736 return nfserr_toosmall;
2737 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2738 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002739 ca->headerpadsz = 0;
2740 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2741 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2742 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2743 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2744 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2745 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2746 /*
2747 * Note decreasing slot size below client's request may make it
2748 * difficult for client to function correctly, whereas
2749 * decreasing the number of slots will (just?) affect
2750 * performance. When short on memory we therefore prefer to
2751 * decrease number of slots instead of their size. Clients that
2752 * request larger slots than they need will get poor results:
2753 */
2754 ca->maxreqs = nfsd4_get_drc_mem(ca);
2755 if (!ca->maxreqs)
2756 return nfserr_jukebox;
2757
J. Bruce Fields373cd402013-04-08 15:42:12 -04002758 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002759}
2760
Chuck Lever45006322016-03-01 13:06:02 -05002761/*
2762 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
2763 * These are based on similar macros in linux/sunrpc/msg_prot.h .
2764 */
2765#define RPC_MAX_HEADER_WITH_AUTH_SYS \
2766 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
2767
2768#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
2769 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
2770
Kinglong Mee8a891632013-12-23 18:11:02 +08002771#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05002772 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08002773#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05002774 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
2775 sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08002776
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002777static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2778{
2779 ca->headerpadsz = 0;
2780
Kinglong Mee8a891632013-12-23 18:11:02 +08002781 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002782 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002783 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002784 return nfserr_toosmall;
2785 ca->maxresp_cached = 0;
2786 if (ca->maxops < 2)
2787 return nfserr_toosmall;
2788
2789 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002790}
2791
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002792static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2793{
2794 switch (cbs->flavor) {
2795 case RPC_AUTH_NULL:
2796 case RPC_AUTH_UNIX:
2797 return nfs_ok;
2798 default:
2799 /*
2800 * GSS case: the spec doesn't allow us to return this
2801 * error. But it also doesn't allow us not to support
2802 * GSS.
2803 * I'd rather this fail hard than return some error the
2804 * client might think it can already handle:
2805 */
2806 return nfserr_encr_alg_unsupp;
2807 }
2808}
2809
Andy Adamson069b6ad2009-04-03 08:27:58 +03002810__be32
2811nfsd4_create_session(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02002812 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03002813{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002814 struct nfsd4_create_session *cr_ses = &u->create_session;
Jeff Layton363168b2009-08-14 12:57:56 -04002815 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002816 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002817 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04002818 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002819 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002820 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002821 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002822 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002823
Mi Jinlonga62573d2011-03-23 17:57:07 +08002824 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2825 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002826 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2827 if (status)
2828 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002829 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002830 if (status)
2831 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002832 status = check_backchannel_attrs(&cr_ses->back_channel);
2833 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002834 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002835 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002836 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002837 if (!new)
2838 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002839 conn = alloc_conn_from_crses(rqstp, cr_ses);
2840 if (!conn)
2841 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002842
Jeff Laytond20c11d2014-07-30 08:27:07 -04002843 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002844 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002845 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002846 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002847
2848 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002849 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002850 if (!nfsd4_mach_creds_match(conf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002851 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002852 cs_slot = &conf->cl_cs_slot;
2853 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08002854 if (status) {
2855 if (status == nfserr_replay_cache)
2856 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002857 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002858 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002859 } else if (unconf) {
2860 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002861 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002862 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002863 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002864 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002865 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002866 if (!nfsd4_mach_creds_match(unconf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002867 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002868 cs_slot = &unconf->cl_cs_slot;
2869 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002870 if (status) {
2871 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002872 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002873 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002874 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002875 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002876 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002877 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002878 if (status) {
2879 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002880 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002881 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002882 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002883 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002884 conf = unconf;
2885 } else {
2886 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002887 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002888 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002889 status = nfs_ok;
Chuck Lever4ce85c82016-03-01 13:05:27 -05002890 /* Persistent sessions are not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002891 cr_ses->flags &= ~SESSION4_PERSIST;
Chuck Lever4ce85c82016-03-01 13:05:27 -05002892 /* Upshifting from TCP to RDMA is not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002893 cr_ses->flags &= ~SESSION4_RDMA;
2894
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002895 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002896 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002897
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04002898 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002899 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002900 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002901 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002902
Jeff Laytond20c11d2014-07-30 08:27:07 -04002903 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002904 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002905 spin_unlock(&nn->client_lock);
2906 /* init connection and backchannel */
2907 nfsd4_init_conn(rqstp, conn, new);
2908 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002909 if (old)
2910 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002911 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002912out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002913 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002914 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002915 if (old)
2916 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002917out_free_session:
2918 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002919out_release_drc_mem:
2920 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002921 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002922}
2923
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002924static __be32 nfsd4_map_bcts_dir(u32 *dir)
2925{
2926 switch (*dir) {
2927 case NFS4_CDFC4_FORE:
2928 case NFS4_CDFC4_BACK:
2929 return nfs_ok;
2930 case NFS4_CDFC4_FORE_OR_BOTH:
2931 case NFS4_CDFC4_BACK_OR_BOTH:
2932 *dir = NFS4_CDFC4_BOTH;
2933 return nfs_ok;
2934 };
2935 return nfserr_inval;
2936}
2937
Christoph Hellwigeb698532017-05-08 20:58:35 +02002938__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
2939 struct nfsd4_compound_state *cstate,
2940 union nfsd4_op_u *u)
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002941{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002942 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002943 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002944 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002945 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002946
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002947 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2948 if (status)
2949 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002950 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002951 session->se_cb_prog = bc->bc_cb_program;
2952 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002953 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002954
2955 nfsd4_probe_callback(session->se_client);
2956
2957 return nfs_ok;
2958}
2959
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002960__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2961 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02002962 union nfsd4_op_u *u)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002963{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002964 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002965 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002966 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002967 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002968 struct net *net = SVC_NET(rqstp);
2969 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002970
2971 if (!nfsd4_last_compound_op(rqstp))
2972 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002973 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002974 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002975 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002976 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002977 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002978 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002979 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002980 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002981 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002982 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002983 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002984 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002985 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002986 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002987 goto out;
2988 nfsd4_init_conn(rqstp, conn, session);
2989 status = nfs_ok;
2990out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002991 nfsd4_put_session(session);
2992out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002993 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002994}
2995
J. Bruce Fields665d5072018-04-11 16:53:36 -04002996static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002997{
J. Bruce Fields665d5072018-04-11 16:53:36 -04002998 if (!cstate->session)
Fengguang Wu51d87bc2018-03-22 13:37:20 +08002999 return false;
J. Bruce Fields665d5072018-04-11 16:53:36 -04003000 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003001}
3002
Andy Adamson069b6ad2009-04-03 08:27:58 +03003003__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003004nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3005 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003006{
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003007 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003008 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003009 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003010 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003011 struct net *net = SVC_NET(r);
3012 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003013
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003014 status = nfserr_not_only_op;
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003015 if (nfsd4_compound_in_session(cstate, sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04003016 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003017 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003018 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04003019 }
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003020 dump_sessionid(__func__, sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003021 spin_lock(&nn->client_lock);
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003022 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003023 if (!ses)
3024 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003025 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003026 if (!nfsd4_mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003027 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003028 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003029 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003030 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003031 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003032 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003033
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05003034 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04003035
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003036 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003037 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003038out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003039 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003040out_client_lock:
3041 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003042out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03003043 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003044}
3045
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003046static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003047{
3048 struct nfsd4_conn *c;
3049
3050 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003051 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04003052 return c;
3053 }
3054 }
3055 return NULL;
3056}
3057
J. Bruce Fields57266a62013-04-13 14:27:29 -04003058static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003059{
3060 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003061 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003062 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003063 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003064
3065 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003066 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003067 if (c)
3068 goto out_free;
3069 status = nfserr_conn_not_bound_to_session;
3070 if (clp->cl_mach_cred)
3071 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003072 __nfsd4_hash_conn(new, ses);
3073 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003074 ret = nfsd4_register_conn(new);
3075 if (ret)
3076 /* oops; xprt is already down: */
3077 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003078 return nfs_ok;
3079out_free:
3080 spin_unlock(&clp->cl_lock);
3081 free_conn(new);
3082 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003083}
3084
Mi Jinlong868b89c2011-04-27 09:09:58 +08003085static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3086{
3087 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3088
3089 return args->opcnt > session->se_fchannel.maxops;
3090}
3091
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003092static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3093 struct nfsd4_session *session)
3094{
3095 struct xdr_buf *xb = &rqstp->rq_arg;
3096
3097 return xb->len > session->se_fchannel.maxreq_sz;
3098}
3099
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003100static bool replay_matches_cache(struct svc_rqst *rqstp,
3101 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3102{
3103 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3104
3105 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3106 (bool)seq->cachethis)
3107 return false;
3108 /*
3109 * If there's an error than the reply can have fewer ops than
3110 * the call. But if we cached a reply with *more* ops than the
3111 * call you're sending us now, then this new call is clearly not
3112 * really a replay of the old one:
3113 */
3114 if (slot->sl_opcnt < argp->opcnt)
3115 return false;
3116 /* This is the only check explicitly called by spec: */
3117 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3118 return false;
3119 /*
3120 * There may be more comparisons we could actually do, but the
3121 * spec doesn't require us to catch every case where the calls
3122 * don't match (that would require caching the call as well as
3123 * the reply), so we don't bother.
3124 */
3125 return true;
3126}
3127
Andy Adamson069b6ad2009-04-03 08:27:58 +03003128__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003129nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3130 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003131{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003132 struct nfsd4_sequence *seq = &u->sequence;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003133 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003134 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003135 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003136 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003137 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003138 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003139 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003140 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003141 struct net *net = SVC_NET(rqstp);
3142 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003143
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003144 if (resp->opcnt != 1)
3145 return nfserr_sequence_pos;
3146
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003147 /*
3148 * Will be either used or freed by nfsd4_sequence_check_conn
3149 * below.
3150 */
3151 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3152 if (!conn)
3153 return nfserr_jukebox;
3154
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003155 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003156 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003157 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003158 goto out_no_session;
3159 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003160
Mi Jinlong868b89c2011-04-27 09:09:58 +08003161 status = nfserr_too_many_ops;
3162 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003163 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003164
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003165 status = nfserr_req_too_big;
3166 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003167 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003168
Benny Halevyb85d4c02009-04-03 08:28:08 +03003169 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003170 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003171 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003172
Andy Adamson557ce262009-08-28 08:45:04 -04003173 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003174 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3175
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003176 /* We do not negotiate the number of slots yet, so set the
3177 * maxslots to the session maxreqs which is used to encode
3178 * sr_highest_slotid and the sr_target_slot id to maxslots */
3179 seq->maxslots = session->se_fchannel.maxreqs;
3180
J. Bruce Fields73e79482012-02-13 16:39:00 -05003181 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3182 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003183 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003184 status = nfserr_seq_misordered;
3185 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003186 goto out_put_session;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003187 status = nfserr_seq_false_retry;
3188 if (!replay_matches_cache(rqstp, seq, slot))
3189 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003190 cstate->slot = slot;
3191 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003192 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003193 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003194 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003195 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003196 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003197 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003198 }
3199 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003200 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003201
J. Bruce Fields57266a62013-04-13 14:27:29 -04003202 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003203 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003204 if (status)
3205 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003206
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003207 buflen = (seq->cachethis) ?
3208 session->se_fchannel.maxresp_cached :
3209 session->se_fchannel.maxresp_sz;
3210 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3211 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003212 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003213 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003214 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003215
3216 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003217 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003218 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003219 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003220 if (seq->cachethis)
3221 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003222 else
3223 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003224
3225 cstate->slot = slot;
3226 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003227 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003228
Benny Halevyb85d4c02009-04-03 08:28:08 +03003229out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003230 switch (clp->cl_cb_state) {
3231 case NFSD4_CB_DOWN:
3232 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3233 break;
3234 case NFSD4_CB_FAULT:
3235 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3236 break;
3237 default:
3238 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003239 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003240 if (!list_empty(&clp->cl_revoked))
3241 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003242out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08003243 if (conn)
3244 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003245 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003246 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003247out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003248 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003249 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003250}
3251
Trond Myklebustb6076642014-06-30 11:48:35 -04003252void
3253nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3254{
3255 struct nfsd4_compound_state *cs = &resp->cstate;
3256
3257 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04003258 if (cs->status != nfserr_replay_cache) {
3259 nfsd4_store_cache_entry(resp);
3260 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3261 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003262 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04003263 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003264 } else if (cs->clp)
3265 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04003266}
3267
Mi Jinlong345c2842011-10-20 17:51:39 +08003268__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003269nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3270 struct nfsd4_compound_state *cstate,
3271 union nfsd4_op_u *u)
Mi Jinlong345c2842011-10-20 17:51:39 +08003272{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003273 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003274 struct nfs4_client *conf, *unconf;
3275 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003276 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003277 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08003278
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003279 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003280 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003281 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003282 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08003283
3284 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04003285 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08003286 status = nfserr_clientid_busy;
3287 goto out;
3288 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04003289 status = mark_client_expired_locked(conf);
3290 if (status)
3291 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003292 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08003293 } else if (unconf)
3294 clp = unconf;
3295 else {
3296 status = nfserr_stale_clientid;
3297 goto out;
3298 }
Andrew Elblededeb132016-06-15 12:52:08 -04003299 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003300 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003301 status = nfserr_wrong_cred;
3302 goto out;
3303 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003304 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003305out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003306 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003307 if (clp)
3308 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003309 return status;
3310}
3311
Andy Adamson069b6ad2009-04-03 08:27:58 +03003312__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003313nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3314 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003315{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003316 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003317 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003318
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003319 if (rc->rca_one_fs) {
3320 if (!cstate->current_fh.fh_dentry)
3321 return nfserr_nofilehandle;
3322 /*
3323 * We don't take advantage of the rca_one_fs case.
3324 * That's OK, it's optional, we can safely ignore it.
3325 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02003326 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003327 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003328
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003329 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04003330 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3331 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003332 goto out;
3333
3334 status = nfserr_stale_clientid;
3335 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003336 /*
3337 * The following error isn't really legal.
3338 * But we only get here if the client just explicitly
3339 * destroyed the client. Surely it no longer cares what
3340 * error it gets back on an operation for the dead
3341 * client.
3342 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003343 goto out;
3344
3345 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04003346 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003347out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003348 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003349}
3350
3351__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003352nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003353 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003355 struct nfsd4_setclientid *setclid = &u->setclientid;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003356 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003358 struct nfs4_client *conf, *new;
3359 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003360 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003361 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3362
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003363 new = create_client(clname, rqstp, &clverifier);
3364 if (new == NULL)
3365 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003366 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003367 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003368 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04003369 if (conf && client_has_state(conf)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003370 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003372 if (clp_used_exchangeid(conf))
3373 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003374 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04003375 char addr_str[INET6_ADDRSTRLEN];
3376 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3377 sizeof(addr_str));
3378 dprintk("NFSD: setclientid: string in use by client "
3379 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 goto out;
3381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003383 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003384 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003385 unhash_client_locked(unconf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003386 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003387 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003389 gen_confirm(new, nn);
3390 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003391 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003392 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003393 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003394 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3396 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3397 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003398 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 status = nfs_ok;
3400out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003401 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003402 if (new)
3403 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003404 if (unconf)
3405 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 return status;
3407}
3408
3409
Al Virob37ad282006-10-19 23:28:59 -07003410__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003411nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003412 struct nfsd4_compound_state *cstate,
3413 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003415 struct nfsd4_setclientid_confirm *setclientid_confirm =
3416 &u->setclientid_confirm;
NeilBrown21ab45a2005-06-23 22:04:14 -07003417 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003418 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3420 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003421 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003422 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003424 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003426
Jeff Laytond20c11d2014-07-30 08:27:07 -04003427 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003428 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003429 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003430 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003431 * We try hard to give out unique clientid's, so if we get an
3432 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003433 * the client may be buggy; this should never happen.
3434 *
3435 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003436 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003437 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04003438 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3439 goto out;
3440 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3441 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003442 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003443 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003444 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3445 /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 status = nfs_ok;
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003447 } else /* case 4: client hasn't noticed we rebooted yet? */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003448 status = nfserr_stale_clientid;
3449 goto out;
3450 }
3451 status = nfs_ok;
3452 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003453 old = unconf;
3454 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003455 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003456 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003457 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3458 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04003459 status = nfserr_clid_inuse;
3460 if (client_has_state(old)
3461 && !same_creds(&unconf->cl_cred,
3462 &old->cl_cred))
3463 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003464 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003465 if (status) {
3466 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003467 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003468 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003469 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003470 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003471 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003472 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003473 get_client_locked(conf);
3474 spin_unlock(&nn->client_lock);
3475 nfsd4_probe_callback(conf);
3476 spin_lock(&nn->client_lock);
3477 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003479 spin_unlock(&nn->client_lock);
3480 if (old)
3481 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 return status;
3483}
3484
J. Bruce Fields32513b42011-10-13 16:00:16 -04003485static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003487 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3488}
3489
3490/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003491static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3492 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003493{
Trond Myklebust950e0112014-06-30 11:48:31 -04003494 lockdep_assert_held(&state_lock);
3495
Elena Reshetova818a34e2017-10-20 12:53:30 +03003496 refcount_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003497 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003498 INIT_LIST_HEAD(&fp->fi_stateids);
3499 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02003500 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003501 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003502 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003503 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003504 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003505 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3506 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003507#ifdef CONFIG_NFSD_PNFS
3508 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02003509 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003510#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04003511 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512}
3513
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003514void
NeilBrowne60d4392005-06-23 22:03:01 -07003515nfsd4_free_slabs(void)
3516{
Jeff Layton9258a2d2018-03-16 09:47:22 -04003517 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003518 kmem_cache_destroy(openowner_slab);
3519 kmem_cache_destroy(lockowner_slab);
3520 kmem_cache_destroy(file_slab);
3521 kmem_cache_destroy(stateid_slab);
3522 kmem_cache_destroy(deleg_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04003523 kmem_cache_destroy(odstate_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003524}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525
Bryan Schumaker72083392011-11-01 15:24:59 -04003526int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527nfsd4_init_slabs(void)
3528{
Jeff Layton9258a2d2018-03-16 09:47:22 -04003529 client_slab = kmem_cache_create("nfsd4_clients",
3530 sizeof(struct nfs4_client), 0, 0, NULL);
3531 if (client_slab == NULL)
3532 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003533 openowner_slab = kmem_cache_create("nfsd4_openowners",
3534 sizeof(struct nfs4_openowner), 0, 0, NULL);
3535 if (openowner_slab == NULL)
Jeff Layton9258a2d2018-03-16 09:47:22 -04003536 goto out_free_client_slab;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003537 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003538 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003539 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003540 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003541 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003542 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003543 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003544 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003545 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003546 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003547 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003548 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003549 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003550 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003551 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003552 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003553 odstate_slab = kmem_cache_create("nfsd4_odstate",
3554 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3555 if (odstate_slab == NULL)
3556 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003558
Sachin Bhamare8287f002015-04-27 14:50:14 +02003559out_free_deleg_slab:
3560 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003561out_free_stateid_slab:
3562 kmem_cache_destroy(stateid_slab);
3563out_free_file_slab:
3564 kmem_cache_destroy(file_slab);
3565out_free_lockowner_slab:
3566 kmem_cache_destroy(lockowner_slab);
3567out_free_openowner_slab:
3568 kmem_cache_destroy(openowner_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04003569out_free_client_slab:
3570 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003571out:
NeilBrowne60d4392005-06-23 22:03:01 -07003572 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3573 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574}
3575
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003576static void init_nfs4_replay(struct nfs4_replay *rp)
3577{
3578 rp->rp_status = nfserr_serverfault;
3579 rp->rp_buflen = 0;
3580 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003581 mutex_init(&rp->rp_mutex);
3582}
3583
3584static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3585 struct nfs4_stateowner *so)
3586{
3587 if (!nfsd4_has_session(cstate)) {
3588 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003589 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04003590 }
3591}
3592
3593void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3594{
3595 struct nfs4_stateowner *so = cstate->replay_owner;
3596
3597 if (so != NULL) {
3598 cstate->replay_owner = NULL;
3599 mutex_unlock(&so->so_replay.rp_mutex);
3600 nfs4_put_stateowner(so);
3601 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003602}
3603
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003604static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605{
3606 struct nfs4_stateowner *sop;
3607
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003608 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003609 if (!sop)
3610 return NULL;
3611
3612 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3613 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003614 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003615 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003617 sop->so_owner.len = owner->len;
3618
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003619 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003620 sop->so_client = clp;
3621 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003622 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003623 return sop;
3624}
3625
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003626static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003627{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003628 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003629
Trond Myklebustd4f04892014-07-29 21:34:36 -04003630 list_add(&oo->oo_owner.so_strhash,
3631 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003632 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633}
3634
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003635static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3636{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003637 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003638}
3639
Jeff Layton6b180f02014-07-29 21:34:26 -04003640static void nfs4_free_openowner(struct nfs4_stateowner *so)
3641{
3642 struct nfs4_openowner *oo = openowner(so);
3643
3644 kmem_cache_free(openowner_slab, oo);
3645}
3646
3647static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003648 .so_unhash = nfs4_unhash_openowner,
3649 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003650};
3651
Andrew Elble7fc05642015-11-05 20:42:43 -05003652static struct nfs4_ol_stateid *
3653nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3654{
3655 struct nfs4_ol_stateid *local, *ret = NULL;
3656 struct nfs4_openowner *oo = open->op_openowner;
3657
3658 lockdep_assert_held(&fp->fi_lock);
3659
3660 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3661 /* ignore lock owners */
3662 if (local->st_stateowner->so_is_open_owner == 0)
3663 continue;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003664 if (local->st_stateowner != &oo->oo_owner)
3665 continue;
3666 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
Andrew Elble7fc05642015-11-05 20:42:43 -05003667 ret = local;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03003668 refcount_inc(&ret->st_stid.sc_count);
Andrew Elble7fc05642015-11-05 20:42:43 -05003669 break;
3670 }
3671 }
3672 return ret;
3673}
3674
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003675static __be32
3676nfsd4_verify_open_stid(struct nfs4_stid *s)
3677{
3678 __be32 ret = nfs_ok;
3679
3680 switch (s->sc_type) {
3681 default:
3682 break;
Trond Myklebust4f176412018-01-12 17:42:30 -05003683 case 0:
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003684 case NFS4_CLOSED_STID:
3685 case NFS4_CLOSED_DELEG_STID:
3686 ret = nfserr_bad_stateid;
3687 break;
3688 case NFS4_REVOKED_DELEG_STID:
3689 ret = nfserr_deleg_revoked;
3690 }
3691 return ret;
3692}
3693
3694/* Lock the stateid st_mutex, and deal with races with CLOSE */
3695static __be32
3696nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
3697{
3698 __be32 ret;
3699
Andrew Elble4f34bd02017-11-08 17:29:51 -05003700 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003701 ret = nfsd4_verify_open_stid(&stp->st_stid);
3702 if (ret != nfs_ok)
3703 mutex_unlock(&stp->st_mutex);
3704 return ret;
3705}
3706
3707static struct nfs4_ol_stateid *
3708nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3709{
3710 struct nfs4_ol_stateid *stp;
3711 for (;;) {
3712 spin_lock(&fp->fi_lock);
3713 stp = nfsd4_find_existing_open(fp, open);
3714 spin_unlock(&fp->fi_lock);
3715 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
3716 break;
3717 nfs4_put_stid(&stp->st_stid);
3718 }
3719 return stp;
3720}
3721
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003722static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003723alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003724 struct nfsd4_compound_state *cstate)
3725{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003726 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003727 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003729 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3730 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003732 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003733 oo->oo_owner.so_is_open_owner = 1;
3734 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003735 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003736 if (nfsd4_has_session(cstate))
3737 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003738 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003739 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003740 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003741 spin_lock(&clp->cl_lock);
3742 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003743 if (ret == NULL) {
3744 hash_openowner(oo, clp, strhashval);
3745 ret = oo;
3746 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08003747 nfs4_free_stateowner(&oo->oo_owner);
3748
Trond Myklebustd4f04892014-07-29 21:34:36 -04003749 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04003750 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751}
3752
Andrew Elble7fc05642015-11-05 20:42:43 -05003753static struct nfs4_ol_stateid *
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003754init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
Andrew Elble7fc05642015-11-05 20:42:43 -05003755{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
Andrew Elble7fc05642015-11-05 20:42:43 -05003757 struct nfs4_openowner *oo = open->op_openowner;
3758 struct nfs4_ol_stateid *retstp = NULL;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003759 struct nfs4_ol_stateid *stp;
Andrew Elble7fc05642015-11-05 20:42:43 -05003760
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003761 stp = open->op_stp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003762 /* We are moving these outside of the spinlocks to avoid the warnings */
3763 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05003764 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003765
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003766retry:
Andrew Elble7fc05642015-11-05 20:42:43 -05003767 spin_lock(&oo->oo_owner.so_client->cl_lock);
3768 spin_lock(&fp->fi_lock);
3769
3770 retstp = nfsd4_find_existing_open(fp, open);
3771 if (retstp)
3772 goto out_unlock;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003773
3774 open->op_stp = NULL;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03003775 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003776 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003777 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003778 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07003779 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003780 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 stp->st_access_bmap = 0;
3782 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003783 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003784 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003785 list_add(&stp->st_perfile, &fp->fi_stateids);
Andrew Elble7fc05642015-11-05 20:42:43 -05003786
3787out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04003788 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003789 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003790 if (retstp) {
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003791 /* Handle races with CLOSE */
3792 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
3793 nfs4_put_stid(&retstp->st_stid);
3794 goto retry;
3795 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003796 /* To keep mutex tracking happy */
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003797 mutex_unlock(&stp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003798 stp = retstp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003799 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003800 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801}
3802
Jeff Laytond3134b12014-07-29 21:34:32 -04003803/*
3804 * In the 4.0 case we need to keep the owners around a little while to handle
3805 * CLOSE replay. We still do need to release any file access that is held by
3806 * them before returning however.
3807 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003809move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810{
Jeff Layton217526e2014-07-30 08:27:11 -04003811 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003812 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3813 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3814 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003815
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003816 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
Jeff Laytonb401be222014-07-29 21:34:33 -04003818 /*
3819 * We know that we hold one reference via nfsd4_close, and another
3820 * "persistent" reference for the client. If the refcount is higher
3821 * than 2, then there are still calls in progress that are using this
3822 * stateid. We can't put the sc_file reference until they are finished.
3823 * Wait for the refcount to drop to 2. Since it has been unhashed,
3824 * there should be no danger of the refcount going back up again at
3825 * this point.
3826 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03003827 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
Jeff Laytonb401be222014-07-29 21:34:33 -04003828
Jeff Laytond3134b12014-07-29 21:34:32 -04003829 release_all_access(s);
3830 if (s->st_stid.sc_file) {
3831 put_nfs4_file(s->st_stid.sc_file);
3832 s->st_stid.sc_file = NULL;
3833 }
Jeff Layton217526e2014-07-30 08:27:11 -04003834
3835 spin_lock(&nn->client_lock);
3836 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003837 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003838 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003839 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003840 spin_unlock(&nn->client_lock);
3841 if (last)
3842 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843}
3844
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845/* search file_hashtbl[] for file */
3846static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04003847find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 struct nfs4_file *fp;
3850
Jeff Layton5b095e92014-10-23 08:01:02 -04003851 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02003852 if (fh_match(&fp->fi_fhandle, fh)) {
Elena Reshetova818a34e2017-10-20 12:53:30 +03003853 if (refcount_inc_not_zero(&fp->fi_ref))
Jeff Layton5b095e92014-10-23 08:01:02 -04003854 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 }
3857 return NULL;
3858}
3859
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02003860struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003861find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003862{
3863 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003864 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003865
Jeff Layton5b095e92014-10-23 08:01:02 -04003866 rcu_read_lock();
3867 fp = find_file_locked(fh, hashval);
3868 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04003869 return fp;
3870}
3871
3872static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003873find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003874{
3875 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003876 unsigned int hashval = file_hashval(fh);
3877
3878 rcu_read_lock();
3879 fp = find_file_locked(fh, hashval);
3880 rcu_read_unlock();
3881 if (fp)
3882 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04003883
3884 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04003885 fp = find_file_locked(fh, hashval);
3886 if (likely(fp == NULL)) {
3887 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04003888 fp = new;
3889 }
3890 spin_unlock(&state_lock);
3891
3892 return fp;
3893}
3894
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003895/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 * Called to check deny when READ with all zero stateid or
3897 * WRITE with all zero or all one stateid
3898 */
Al Virob37ad282006-10-19 23:28:59 -07003899static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3901{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003903 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904
Trond Myklebustca943212014-07-23 16:17:39 -04003905 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003906 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003907 return ret;
3908 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003909 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003910 if (fp->fi_share_deny & deny_type)
3911 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003912 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003913 put_nfs4_file(fp);
3914 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915}
3916
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003917static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003919 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04003920 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3921 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003922
Trond Myklebust11b91642014-07-29 21:34:08 -04003923 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003924
Jeff Layton02e12152014-07-16 10:31:57 -04003925 /*
3926 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003927 * already holding inode->i_lock.
3928 *
Jeff Laytondff13992014-07-08 14:02:49 -04003929 * If the dl_time != 0, then we know that it has already been
3930 * queued for a lease break. Don't queue it again.
3931 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003932 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003933 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003934 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003935 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003936 }
Jeff Layton02e12152014-07-16 10:31:57 -04003937 spin_unlock(&state_lock);
3938}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003940static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3941 struct rpc_task *task)
3942{
3943 struct nfs4_delegation *dp = cb_to_delegation(cb);
3944
Andrew Elblea4579742015-08-31 12:06:41 -04003945 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3946 return 1;
3947
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003948 switch (task->tk_status) {
3949 case 0:
3950 return 1;
3951 case -EBADHANDLE:
3952 case -NFS4ERR_BAD_STATEID:
3953 /*
3954 * Race: client probably got cb_recall before open reply
3955 * granting delegation.
3956 */
3957 if (dp->dl_retries--) {
3958 rpc_delay(task, 2 * HZ);
3959 return 0;
3960 }
3961 /*FALLTHRU*/
3962 default:
3963 return -1;
3964 }
3965}
3966
3967static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3968{
3969 struct nfs4_delegation *dp = cb_to_delegation(cb);
3970
3971 nfs4_put_stid(&dp->dl_stid);
3972}
3973
Julia Lawallc4cb8972015-11-21 22:57:39 +01003974static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003975 .prepare = nfsd4_cb_recall_prepare,
3976 .done = nfsd4_cb_recall_done,
3977 .release = nfsd4_cb_recall_release,
3978};
3979
Jeff Layton02e12152014-07-16 10:31:57 -04003980static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3981{
3982 /*
3983 * We're assuming the state code never drops its reference
3984 * without first removing the lease. Since we're in this lease
J. Bruce Fields4a269efb2018-04-13 17:34:35 -04003985 * callback (and since the lease code is serialized by the
3986 * i_lock) we know the server hasn't removed the lease yet, and
3987 * we know it's safe to take a reference.
Jeff Layton02e12152014-07-16 10:31:57 -04003988 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03003989 refcount_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02003990 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003991}
3992
Jeff Layton1c8c6012013-06-21 08:58:15 -04003993/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003994static bool
3995nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003996{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003997 bool ret = false;
J. Bruce Fields653e5142018-02-15 22:08:45 -05003998 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
3999 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004000
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004001 /*
4002 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004003 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004004 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004005 */
4006 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007
Jeff Layton02e12152014-07-16 10:31:57 -04004008 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004009 fp->fi_had_conflict = true;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004010 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04004011 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004012 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013}
4014
Jeff Laytonc45198e2014-09-01 07:12:07 -04004015static int
Jeff Layton7448cc32015-01-16 15:05:57 -05004016nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4017 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018{
4019 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04004020 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 else
4022 return -EAGAIN;
4023}
4024
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004025static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04004026 .lm_break = nfsd_break_deleg_cb,
4027 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028};
4029
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004030static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4031{
4032 if (nfsd4_has_session(cstate))
4033 return nfs_ok;
4034 if (seqid == so->so_seqid - 1)
4035 return nfserr_replay_me;
4036 if (seqid == so->so_seqid)
4037 return nfs_ok;
4038 return nfserr_bad_seqid;
4039}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040
Jeff Layton4b24ca72014-06-30 11:48:44 -04004041static __be32 lookup_clientid(clientid_t *clid,
4042 struct nfsd4_compound_state *cstate,
4043 struct nfsd_net *nn)
4044{
4045 struct nfs4_client *found;
4046
4047 if (cstate->clp) {
4048 found = cstate->clp;
4049 if (!same_clid(&found->cl_clientid, clid))
4050 return nfserr_stale_clientid;
4051 return nfs_ok;
4052 }
4053
4054 if (STALE_CLIENTID(clid, nn))
4055 return nfserr_stale_clientid;
4056
4057 /*
4058 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4059 * cached already then we know this is for is for v4.0 and "sessions"
4060 * will be false.
4061 */
4062 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004063 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004064 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004065 if (!found) {
4066 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004067 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04004068 }
4069 atomic_inc(&found->cl_refcount);
4070 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004071
4072 /* Cache the nfs4_client in cstate! */
4073 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004074 return nfs_ok;
4075}
4076
Al Virob37ad282006-10-19 23:28:59 -07004077__be32
Andy Adamson66689582009-04-03 08:28:45 +03004078nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004079 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 clientid_t *clientid = &open->op_clientid;
4082 struct nfs4_client *clp = NULL;
4083 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004084 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004085 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004087 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004089 /*
4090 * In case we need it later, after we've already created the
4091 * file and don't want to risk a further failure:
4092 */
4093 open->op_file = nfsd4_alloc_file();
4094 if (open->op_file == NULL)
4095 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096
Trond Myklebust2d91e892014-06-30 11:48:46 -04004097 status = lookup_clientid(clientid, cstate, nn);
4098 if (status)
4099 return status;
4100 clp = cstate->clp;
4101
Trond Myklebustd4f04892014-07-29 21:34:36 -04004102 strhashval = ownerstr_hashval(&open->op_owner);
4103 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004104 open->op_openowner = oo;
4105 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004106 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004108 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004109 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004110 release_openowner(oo);
4111 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004112 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004113 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004114 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4115 if (status)
4116 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004117 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004118new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04004119 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004120 if (oo == NULL)
4121 return nfserr_jukebox;
4122 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004123alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04004124 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004125 if (!open->op_stp)
4126 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004127
4128 if (nfsd4_has_session(cstate) &&
4129 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4130 open->op_odstate = alloc_clnt_odstate(clp);
4131 if (!open->op_odstate)
4132 return nfserr_jukebox;
4133 }
4134
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004135 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136}
4137
Al Virob37ad282006-10-19 23:28:59 -07004138static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07004139nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4140{
4141 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4142 return nfserr_openmode;
4143 else
4144 return nfs_ok;
4145}
4146
Daniel Mackc47d8322011-05-16 16:38:14 +02004147static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04004148{
J. Bruce Fields24a01112010-05-18 20:01:35 -04004149 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4150}
4151
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004152static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004153{
4154 struct nfs4_stid *ret;
4155
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004156 ret = find_stateid_by_type(cl, s,
4157 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004158 if (!ret)
4159 return NULL;
4160 return delegstateid(ret);
4161}
4162
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004163static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4164{
4165 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4166 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4167}
4168
Al Virob37ad282006-10-19 23:28:59 -07004169static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04004170nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07004171 struct nfs4_delegation **dp)
4172{
4173 int flags;
Al Virob37ad282006-10-19 23:28:59 -07004174 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004175 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07004176
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004177 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4178 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07004179 goto out;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004180 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4181 nfs4_put_stid(&deleg->dl_stid);
4182 if (cl->cl_minorversion)
4183 status = nfserr_deleg_revoked;
4184 goto out;
4185 }
J. Bruce Fields24a01112010-05-18 20:01:35 -04004186 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004187 status = nfs4_check_delegmode(deleg, flags);
4188 if (status) {
4189 nfs4_put_stid(&deleg->dl_stid);
4190 goto out;
4191 }
4192 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004193out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004194 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004195 return nfs_ok;
4196 if (status)
4197 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004198 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004199 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07004200}
4201
J. Bruce Fields21fb4012010-07-28 12:21:23 -04004202static inline int nfs4_access_to_access(u32 nfs4_access)
4203{
4204 int flags = 0;
4205
4206 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4207 flags |= NFSD_MAY_READ;
4208 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4209 flags |= NFSD_MAY_WRITE;
4210 return flags;
4211}
4212
Al Virob37ad282006-10-19 23:28:59 -07004213static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4215 struct nfsd4_open *open)
4216{
4217 struct iattr iattr = {
4218 .ia_valid = ATTR_SIZE,
4219 .ia_size = 0,
4220 };
4221 if (!open->op_truncate)
4222 return 0;
4223 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08004224 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
4226}
4227
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004228static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004229 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4230 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004231{
Trond Myklebustde186432014-07-10 14:07:26 -04004232 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004233 __be32 status;
4234 int oflag = nfs4_access_to_omode(open->op_share_access);
4235 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004236 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004237
Trond Myklebustde186432014-07-10 14:07:26 -04004238 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004239
4240 /*
4241 * Are we trying to set a deny mode that would conflict with
4242 * current access?
4243 */
4244 status = nfs4_file_check_deny(fp, open->op_share_deny);
4245 if (status != nfs_ok) {
4246 spin_unlock(&fp->fi_lock);
4247 goto out;
4248 }
4249
4250 /* set access to the file */
4251 status = nfs4_file_get_access(fp, open->op_share_access);
4252 if (status != nfs_ok) {
4253 spin_unlock(&fp->fi_lock);
4254 goto out;
4255 }
4256
4257 /* Set access bits in stateid */
4258 old_access_bmap = stp->st_access_bmap;
4259 set_access(open->op_share_access, stp);
4260
4261 /* Set new deny mask */
4262 old_deny_bmap = stp->st_deny_bmap;
4263 set_deny(open->op_share_deny, stp);
4264 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4265
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004266 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04004267 spin_unlock(&fp->fi_lock);
4268 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004269 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004270 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04004271 spin_lock(&fp->fi_lock);
4272 if (!fp->fi_fds[oflag]) {
4273 fp->fi_fds[oflag] = filp;
4274 filp = NULL;
4275 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004276 }
Trond Myklebustde186432014-07-10 14:07:26 -04004277 spin_unlock(&fp->fi_lock);
4278 if (filp)
4279 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004280
4281 status = nfsd4_truncate(rqstp, cur_fh, open);
4282 if (status)
4283 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004284out:
4285 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004286out_put_access:
4287 stp->st_access_bmap = old_access_bmap;
4288 nfs4_file_put_access(fp, open->op_share_access);
4289 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4290 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004291}
4292
Al Virob37ad282006-10-19 23:28:59 -07004293static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004294nfs4_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 -07004295{
Al Virob37ad282006-10-19 23:28:59 -07004296 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02004297 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004299 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004300 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004301
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004302 /* test and set deny mode */
4303 spin_lock(&fp->fi_lock);
4304 status = nfs4_file_check_deny(fp, open->op_share_deny);
4305 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004306 set_deny(open->op_share_deny, stp);
4307 fp->fi_share_deny |=
4308 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4309 }
4310 spin_unlock(&fp->fi_lock);
4311
4312 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004315 status = nfsd4_truncate(rqstp, cur_fh, open);
4316 if (status != nfs_ok)
4317 reset_union_bmap_deny(old_deny_bmap, stp);
4318 return status;
4319}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004321/* Should we give out recallable state?: */
4322static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4323{
4324 if (clp->cl_cb_state == NFSD4_CB_UP)
4325 return true;
4326 /*
4327 * In the sessions case, since we don't have to establish a
4328 * separate connection for callbacks, we assume it's OK
4329 * until we hear otherwise:
4330 */
4331 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4332}
4333
J. Bruce Fields653e5142018-02-15 22:08:45 -05004334static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4335 int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004336{
4337 struct file_lock *fl;
4338
4339 fl = locks_alloc_lock();
4340 if (!fl)
4341 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004342 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04004343 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004344 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4345 fl->fl_end = OFFSET_MAX;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004346 fl->fl_owner = (fl_owner_t)dp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004347 fl->fl_pid = current->tgid;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004348 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004349 return fl;
4350}
4351
Jeff Layton0b266932014-07-25 07:34:25 -04004352static struct nfs4_delegation *
4353nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004354 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004355{
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004356 int status = 0;
Jeff Layton0b266932014-07-25 07:34:25 -04004357 struct nfs4_delegation *dp;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004358 struct file *filp;
4359 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04004360
J. Bruce Fields353601e2018-02-16 14:29:42 -05004361 /*
4362 * The fi_had_conflict and nfs_get_existing_delegation checks
4363 * here are just optimizations; we'll need to recheck them at
4364 * the end:
4365 */
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004366 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004367 return ERR_PTR(-EAGAIN);
4368
J. Bruce Fields353601e2018-02-16 14:29:42 -05004369 filp = find_readable_file(fp);
4370 if (!filp) {
4371 /* We should always have a readable file here */
4372 WARN_ON_ONCE(1);
4373 return ERR_PTR(-EBADF);
4374 }
Andrew Elble34ed9872015-10-15 12:07:28 -04004375 spin_lock(&state_lock);
4376 spin_lock(&fp->fi_lock);
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004377 if (nfs4_delegation_exists(clp, fp))
4378 status = -EAGAIN;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004379 else if (!fp->fi_deleg_file) {
4380 fp->fi_deleg_file = filp;
4381 /* increment early to prevent fi_deleg_file from being
4382 * cleared */
4383 fp->fi_delegees = 1;
4384 filp = NULL;
4385 } else
4386 fp->fi_delegees++;
4387 spin_unlock(&fp->fi_lock);
4388 spin_unlock(&state_lock);
4389 if (filp)
4390 fput(filp);
4391 if (status)
4392 return ERR_PTR(status);
4393
4394 status = -ENOMEM;
4395 dp = alloc_init_deleg(clp, fp, fh, odstate);
4396 if (!dp)
4397 goto out_delegees;
4398
4399 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
4400 if (!fl)
4401 goto out_stid;
4402
4403 status = vfs_setlease(fp->fi_deleg_file, fl->fl_type, &fl, NULL);
4404 if (fl)
4405 locks_free_lock(fl);
4406 if (status)
4407 goto out_clnt_odstate;
4408
4409 spin_lock(&state_lock);
4410 spin_lock(&fp->fi_lock);
4411 if (fp->fi_had_conflict)
4412 status = -EAGAIN;
4413 else
4414 status = hash_delegation_locked(dp, fp);
Andrew Elble34ed9872015-10-15 12:07:28 -04004415 spin_unlock(&fp->fi_lock);
4416 spin_unlock(&state_lock);
4417
4418 if (status)
Andrew Elble692ad282018-04-18 17:04:37 -04004419 goto out_unlock;
4420
Jeff Layton0b266932014-07-25 07:34:25 -04004421 return dp;
Andrew Elble692ad282018-04-18 17:04:37 -04004422out_unlock:
4423 vfs_setlease(fp->fi_deleg_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004424out_clnt_odstate:
4425 put_clnt_odstate(dp->dl_clnt_odstate);
4426out_stid:
4427 nfs4_put_stid(&dp->dl_stid);
4428out_delegees:
4429 put_deleg_file(fp);
4430 return ERR_PTR(status);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004431}
4432
Benny Halevy4aa89132012-02-21 14:16:44 -08004433static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4434{
4435 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4436 if (status == -EAGAIN)
4437 open->op_why_no_deleg = WND4_CONTENTION;
4438 else {
4439 open->op_why_no_deleg = WND4_RESOURCE;
4440 switch (open->op_deleg_want) {
4441 case NFS4_SHARE_WANT_READ_DELEG:
4442 case NFS4_SHARE_WANT_WRITE_DELEG:
4443 case NFS4_SHARE_WANT_ANY_DELEG:
4444 break;
4445 case NFS4_SHARE_WANT_CANCEL:
4446 open->op_why_no_deleg = WND4_CANCELLED;
4447 break;
4448 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004449 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08004450 }
4451 }
4452}
4453
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454/*
4455 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04004456 *
4457 * Note we don't support write delegations, and won't until the vfs has
4458 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 */
4460static void
Jeff Layton4cf59222014-07-25 07:34:24 -04004461nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4462 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463{
4464 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04004465 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4466 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004467 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004468 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004470 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07004471 open->op_recall = 0;
4472 switch (open->op_claim_type) {
4473 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05004474 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07004475 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004476 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4477 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004478 break;
4479 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00004480 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004481 /*
4482 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004483 * had the chance to reclaim theirs, *and* until
4484 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004485 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004486 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004487 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004488 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004489 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04004490 /*
4491 * Also, if the file was opened for write or
4492 * create, there's a good chance the client's
4493 * about to write to it, resulting in an
4494 * immediate recall (since we don't support
4495 * write delegations):
4496 */
NeilBrown7b190fe2005-06-23 22:03:23 -07004497 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04004498 goto out_no_deleg;
4499 if (open->op_create == NFS4_OPEN_CREATE)
4500 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004501 break;
4502 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004503 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004504 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004505 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004506 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004507 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004509 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004511 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004512 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04004513 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04004514 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004515 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004516out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004517 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4518 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004519 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04004520 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004521 open->op_recall = 1;
4522 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004523
4524 /* 4.1 client asking for a delegation? */
4525 if (open->op_deleg_want)
4526 nfsd4_open_deleg_none_ext(open, status);
4527 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528}
4529
Benny Halevye27f49c2012-02-21 14:16:54 -08004530static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4531 struct nfs4_delegation *dp)
4532{
4533 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4534 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4535 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4536 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4537 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4538 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4539 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4540 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4541 }
4542 /* Otherwise the client must be confused wanting a delegation
4543 * it already has, therefore we don't return
4544 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4545 */
4546}
4547
Al Virob37ad282006-10-19 23:28:59 -07004548__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4550{
Andy Adamson66689582009-04-03 08:28:45 +03004551 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004552 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004554 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004555 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004556 __be32 status;
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004557 bool new_stp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 /*
4560 * Lookup file; if found, lookup stateid and check open request,
4561 * and check for delegations in the process of being recalled.
4562 * If not found, create the nfs4_file struct
4563 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004564 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004565 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004566 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004567 if (status)
4568 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004569 stp = nfsd4_find_and_lock_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004571 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004572 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004573 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004574 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 }
4576
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004577 if (!stp) {
4578 stp = init_open_stateid(fp, open);
4579 if (!open->op_stp)
4580 new_stp = true;
4581 }
4582
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 /*
4584 * OPEN the file, or upgrade an existing OPEN.
4585 * If truncate fails, the OPEN fails.
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004586 *
4587 * stp is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 */
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004589 if (!new_stp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004591 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04004592 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004593 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04004595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 } else {
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004597 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4598 if (status) {
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004599 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004600 release_open_stateid(stp);
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004601 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004602 goto out;
4603 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004604
4605 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4606 open->op_odstate);
4607 if (stp->st_clnt_odstate == open->op_odstate)
4608 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 }
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004610
Jeff Layton9767feb2015-10-01 09:05:50 -04004611 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004612 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
Benny Halevyd24433c2012-02-16 20:57:17 +02004614 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004615 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4616 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4617 open->op_why_no_deleg = WND4_NOT_WANTED;
4618 goto nodeleg;
4619 }
4620 }
4621
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 /*
4623 * Attempt to hand out a delegation. No error return, because the
4624 * OPEN succeeds even if we fail.
4625 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004626 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004627nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 status = nfs_ok;
4629
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004630 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004631 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004633 /* 4.1 client trying to upgrade/downgrade delegation? */
4634 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004635 open->op_deleg_want)
4636 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004637
NeilBrown13cd2182005-06-23 22:03:10 -07004638 if (fp)
4639 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004640 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08004641 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 /*
4643 * To finish the open response, we just need to set the rflags.
4644 */
4645 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Jeff Layton19e4c342016-09-16 16:28:27 -04004646 if (nfsd4_has_session(&resp->cstate))
4647 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
4648 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Jeff Layton19e4c342016-09-16 16:28:27 -04004650
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004651 if (dp)
4652 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004653 if (stp)
4654 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655
4656 return status;
4657}
4658
Jeff Layton58fb12e2014-07-29 21:34:27 -04004659void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04004660 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004661{
4662 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004663 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004664
Jeff Laytond3134b12014-07-29 21:34:32 -04004665 nfsd4_cstate_assign_replay(cstate, so);
4666 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004667 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004668 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04004669 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004670 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004671 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004672 if (open->op_odstate)
4673 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004674}
4675
Al Virob37ad282006-10-19 23:28:59 -07004676__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004677nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02004678 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679{
Christoph Hellwigeb698532017-05-08 20:58:35 +02004680 clientid_t *clid = &u->renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004682 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004683 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 dprintk("process_renew(%08x/%08x): starting\n",
4686 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004687 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004688 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004690 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004692 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004693 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 goto out;
4695 status = nfs_ok;
4696out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 return status;
4698}
4699
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004700void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004701nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004702{
Jeff Layton33dcc482012-04-10 11:08:48 -04004703 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004704 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004705 return;
4706
NeilBrowna76b4312005-06-23 22:04:01 -07004707 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004708 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004709 /*
4710 * If the server goes down again right now, an NFSv4
4711 * client will still be allowed to reclaim after it comes back up,
4712 * even if it hasn't yet had a chance to reclaim state this time.
4713 *
4714 */
Jeff Layton919b8042014-09-12 16:40:20 -04004715 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004716 /*
4717 * At this point, NFSv4 clients can still reclaim. But if the
4718 * server crashes, any that have not yet reclaimed will be out
4719 * of luck on the next boot.
4720 *
4721 * (NFSv4.1+ clients are considered to have reclaimed once they
4722 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4723 * have reclaimed after their first OPEN.)
4724 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004725 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004726 /*
4727 * At this point, and once lockd and/or any other containers
4728 * exit their grace period, further reclaims will fail and
4729 * regular locking can resume.
4730 */
NeilBrowna76b4312005-06-23 22:04:01 -07004731}
4732
J. Bruce Fields03f318c2018-06-08 12:28:47 -04004733/*
4734 * If we've waited a lease period but there are still clients trying to
4735 * reclaim, wait a little longer to give them a chance to finish.
4736 */
4737static bool clients_still_reclaiming(struct nfsd_net *nn)
4738{
4739 unsigned long now = get_seconds();
4740 unsigned long double_grace_period_end = nn->boot_time +
4741 2 * nn->nfsd4_lease;
4742
4743 if (!nn->somebody_reclaimed)
4744 return false;
4745 nn->somebody_reclaimed = false;
4746 /*
4747 * If we've given them *two* lease times to reclaim, and they're
4748 * still not done, give up:
4749 */
4750 if (time_after(now, double_grace_period_end))
4751 return false;
4752 return true;
4753}
4754
NeilBrownfd39ca92005-06-23 22:04:03 -07004755static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004756nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757{
4758 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004759 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004761 struct nfs4_ol_stateid *stp;
Jeff Layton7919d0a2016-09-16 16:28:25 -04004762 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004764 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004765 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 dprintk("NFSD: laundromat service - starting\n");
J. Bruce Fields03f318c2018-06-08 12:28:47 -04004768
4769 if (clients_still_reclaiming(nn)) {
4770 new_timeo = 0;
4771 goto out;
4772 }
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004773 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004774 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004775 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004776 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 clp = list_entry(pos, struct nfs4_client, cl_lru);
4778 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4779 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004780 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 break;
4782 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004783 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004784 dprintk("NFSD: client in use (clientid %08x)\n",
4785 clp->cl_clientid.cl_id);
4786 continue;
4787 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004788 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004789 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004790 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004791 list_for_each_safe(pos, next, &reaplist) {
4792 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 dprintk("NFSD: purging unused client (clientid %08x)\n",
4794 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004795 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 expire_client(clp);
4797 }
Benny Halevycdc975052014-05-30 09:09:30 -04004798 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004799 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4801 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004802 t = dp->dl_time - cutoff;
4803 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 break;
4805 }
Jeff Layton3fcbbd22015-08-24 12:41:48 -04004806 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04004807 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 }
Benny Halevycdc975052014-05-30 09:09:30 -04004809 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004810 while (!list_empty(&reaplist)) {
4811 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4812 dl_recall_lru);
4813 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004814 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 }
Jeff Layton217526e2014-07-30 08:27:11 -04004816
4817 spin_lock(&nn->client_lock);
4818 while (!list_empty(&nn->close_lru)) {
4819 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4820 oo_close_lru);
4821 if (time_after((unsigned long)oo->oo_time,
4822 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004823 t = oo->oo_time - cutoff;
4824 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 break;
4826 }
Jeff Layton217526e2014-07-30 08:27:11 -04004827 list_del_init(&oo->oo_close_lru);
4828 stp = oo->oo_last_closed_stid;
4829 oo->oo_last_closed_stid = NULL;
4830 spin_unlock(&nn->client_lock);
4831 nfs4_put_stid(&stp->st_stid);
4832 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 }
Jeff Layton217526e2014-07-30 08:27:11 -04004834 spin_unlock(&nn->client_lock);
4835
Jeff Layton7919d0a2016-09-16 16:28:25 -04004836 /*
4837 * It's possible for a client to try and acquire an already held lock
4838 * that is being held for a long time, and then lose interest in it.
4839 * So, we clean out any un-revisited request after a lease period
4840 * under the assumption that the client is no longer interested.
4841 *
4842 * RFC5661, sec. 9.6 states that the client must not rely on getting
4843 * notifications and must continue to poll for locks, even when the
4844 * server supports them. Thus this shouldn't lead to clients blocking
4845 * indefinitely once the lock does become free.
4846 */
4847 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04004848 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04004849 while (!list_empty(&nn->blocked_locks_lru)) {
4850 nbl = list_first_entry(&nn->blocked_locks_lru,
4851 struct nfsd4_blocked_lock, nbl_lru);
4852 if (time_after((unsigned long)nbl->nbl_time,
4853 (unsigned long)cutoff)) {
4854 t = nbl->nbl_time - cutoff;
4855 new_timeo = min(new_timeo, t);
4856 break;
4857 }
4858 list_move(&nbl->nbl_lru, &reaplist);
4859 list_del_init(&nbl->nbl_list);
4860 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04004861 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04004862
4863 while (!list_empty(&reaplist)) {
Naofumi Honda64ebe1242017-11-09 10:57:16 -05004864 nbl = list_first_entry(&reaplist,
Jeff Layton7919d0a2016-09-16 16:28:25 -04004865 struct nfsd4_blocked_lock, nbl_lru);
4866 list_del_init(&nbl->nbl_lru);
4867 posix_unblock_lock(&nbl->nbl_lock);
4868 free_blocked_lock(nbl);
4869 }
J. Bruce Fields03f318c2018-06-08 12:28:47 -04004870out:
Jeff Laytona832e7a2014-05-30 09:09:26 -04004871 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004872 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873}
4874
Harvey Harrisona254b242008-02-20 12:49:00 -08004875static struct workqueue_struct *laundry_wq;
4876static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004877
4878static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004879laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880{
4881 time_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08004882 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004883 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4884 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004886 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004888 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889}
4890
Jeff Layton8fcd4612015-07-30 06:57:46 -04004891static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004892{
Jeff Layton8fcd4612015-07-30 06:57:46 -04004893 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004894 return nfserr_bad_stateid;
4895 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896}
4897
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004899access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004901 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4902 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4903 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904}
4905
4906static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004907access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004909 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4910 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911}
4912
4913static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004914__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915{
Al Virob37ad282006-10-19 23:28:59 -07004916 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917
J. Bruce Fields02921912010-07-29 15:16:59 -04004918 /* For lock stateid's, we test the parent open, not the lock: */
4919 if (stp->st_openstp)
4920 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004921 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004923 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 goto out;
4925 status = nfs_ok;
4926out:
4927 return status;
4928}
4929
Al Virob37ad282006-10-19 23:28:59 -07004930static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004931check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004933 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004935 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004936 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 * conflicting state; so we must wait out the grace period. */
4938 return nfserr_grace;
4939 } else if (flags & WR_STATE)
4940 return nfs4_share_conflict(current_fh,
4941 NFS4_SHARE_DENY_WRITE);
4942 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4943 return nfs4_share_conflict(current_fh,
4944 NFS4_SHARE_DENY_READ);
4945}
4946
4947/*
4948 * Allow READ/WRITE during grace period on recovered state only for files
4949 * that are not able to provide mandatory locking.
4950 */
4951static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004952grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004954 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955}
4956
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004957static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004958{
Andy Adamson66689582009-04-03 08:28:45 +03004959 /*
4960 * When sessions are used the stateid generation number is ignored
4961 * when it is zero.
4962 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004963 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004964 return nfs_ok;
4965
4966 if (in->si_generation == ref->si_generation)
4967 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004968
J. Bruce Fields0836f582008-01-26 19:08:12 -05004969 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04004970 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004971 return nfserr_bad_stateid;
4972 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004973 * However, we could see a stateid from the past, even from a
4974 * non-buggy client. For example, if the client sends a lock
4975 * while some IO is outstanding, the lock may bump si_generation
4976 * while the IO is still in flight. The client could avoid that
4977 * situation by waiting for responses on all the IO requests,
4978 * but better performance may result in retrying IO that
4979 * receives an old_stateid error if requests are rarely
4980 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004981 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004982 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004983}
4984
Trond Myklebust03da3162017-11-03 08:00:16 -04004985static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
4986{
4987 __be32 ret;
4988
4989 spin_lock(&s->sc_lock);
4990 ret = nfsd4_verify_open_stid(s);
4991 if (ret == nfs_ok)
4992 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
4993 spin_unlock(&s->sc_lock);
4994 return ret;
4995}
4996
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004997static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4998{
4999 if (ols->st_stateowner->so_is_open_owner &&
5000 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5001 return nfserr_bad_stateid;
5002 return nfs_ok;
5003}
5004
Chuck Lever7df302f2012-05-29 13:56:37 -04005005static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04005006{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005007 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005008 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04005009
Andrew Elbleae254dac2017-11-09 13:41:10 -05005010 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5011 CLOSE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04005012 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04005013 /* Client debugging aid. */
5014 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
5015 char addr_str[INET6_ADDRSTRLEN];
5016 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
5017 sizeof(addr_str));
5018 pr_warn_ratelimited("NFSD: client %s testing state ID "
5019 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005020 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04005021 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005022 spin_lock(&cl->cl_lock);
5023 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005024 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005025 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005026 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04005027 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005028 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04005029 switch (s->sc_type) {
5030 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005031 status = nfs_ok;
5032 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005033 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005034 status = nfserr_deleg_revoked;
5035 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005036 case NFS4_OPEN_STID:
5037 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005038 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04005039 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005040 default:
5041 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005042 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04005043 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005044 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005045 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04005046 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005047out_unlock:
5048 spin_unlock(&cl->cl_lock);
5049 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04005050}
5051
Christoph Hellwigcd61c522014-08-14 08:44:57 +02005052__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005053nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5054 stateid_t *stateid, unsigned char typemask,
5055 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005056{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005057 __be32 status;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005058 bool return_revoked = false;
5059
5060 /*
5061 * only return revoked delegations if explicitly asked.
5062 * otherwise we report revoked or bad_stateid status.
5063 */
5064 if (typemask & NFS4_REVOKED_DELEG_STID)
5065 return_revoked = true;
5066 else if (typemask & NFS4_DELEG_STID)
5067 typemask |= NFS4_REVOKED_DELEG_STID;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005068
Andrew Elbleae254dac2017-11-09 13:41:10 -05005069 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5070 CLOSE_STATEID(stateid))
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005071 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005072 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005073 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005074 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005075 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005076 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005077 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005078 if (status)
5079 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005080 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005081 if (!*s)
5082 return nfserr_bad_stateid;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005083 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5084 nfs4_put_stid(*s);
5085 if (cstate->minorversion)
5086 return nfserr_deleg_revoked;
5087 return nfserr_bad_stateid;
5088 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005089 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005090}
5091
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005092static struct file *
5093nfs4_find_file(struct nfs4_stid *s, int flags)
5094{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005095 if (!s)
5096 return NULL;
5097
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005098 switch (s->sc_type) {
5099 case NFS4_DELEG_STID:
5100 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5101 return NULL;
5102 return get_file(s->sc_file->fi_deleg_file);
5103 case NFS4_OPEN_STID:
5104 case NFS4_LOCK_STID:
5105 if (flags & RD_STATE)
5106 return find_readable_file(s->sc_file);
5107 else
5108 return find_writeable_file(s->sc_file);
5109 break;
5110 }
5111
5112 return NULL;
5113}
5114
5115static __be32
5116nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
5117{
5118 __be32 status;
5119
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005120 status = nfsd4_check_openowner_confirmed(ols);
5121 if (status)
5122 return status;
5123 return nfs4_check_openmode(ols, flags);
5124}
5125
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005126static __be32
5127nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
5128 struct file **filpp, bool *tmp_file, int flags)
5129{
5130 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
5131 struct file *file;
5132 __be32 status;
5133
5134 file = nfs4_find_file(s, flags);
5135 if (file) {
5136 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5137 acc | NFSD_MAY_OWNER_OVERRIDE);
5138 if (status) {
5139 fput(file);
5140 return status;
5141 }
5142
5143 *filpp = file;
5144 } else {
5145 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
5146 if (status)
5147 return status;
5148
5149 if (tmp_file)
5150 *tmp_file = true;
5151 }
5152
5153 return 0;
5154}
5155
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005157 * Checks for stateid operations
5158 */
Al Virob37ad282006-10-19 23:28:59 -07005159__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005160nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01005161 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
5162 stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005164 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005165 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005166 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005167 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005168 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 if (filpp)
5171 *filpp = NULL;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005172 if (tmp_file)
5173 *tmp_file = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005175 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 return nfserr_grace;
5177
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005178 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5179 status = check_special_stateids(net, fhp, stateid, flags);
5180 goto done;
5181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005183 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04005184 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005185 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005186 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04005187 return status;
Trond Myklebust03da3162017-11-03 08:00:16 -04005188 status = nfsd4_stid_check_stateid_generation(stateid, s,
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005189 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04005190 if (status)
5191 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005192
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005193 switch (s->sc_type) {
5194 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005195 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005196 break;
5197 case NFS4_OPEN_STID:
5198 case NFS4_LOCK_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005199 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005200 break;
5201 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04005202 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005203 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04005205 if (status)
5206 goto out;
5207 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005208
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005209done:
5210 if (!status && filpp)
5211 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212out:
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005213 if (s)
5214 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 return status;
5216}
5217
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005218/*
Bryan Schumaker17456802011-07-13 10:50:48 -04005219 * Test if the stateid is valid
5220 */
5221__be32
5222nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005223 union nfsd4_op_u *u)
Bryan Schumaker17456802011-07-13 10:50:48 -04005224{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005225 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005226 struct nfsd4_test_stateid_id *stateid;
5227 struct nfs4_client *cl = cstate->session->se_client;
5228
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005229 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04005230 stateid->ts_id_status =
5231 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005232
Bryan Schumaker17456802011-07-13 10:50:48 -04005233 return nfs_ok;
5234}
5235
Chuck Lever42691392016-08-11 10:37:30 -04005236static __be32
5237nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5238{
5239 struct nfs4_ol_stateid *stp = openlockstateid(s);
5240 __be32 ret;
5241
Trond Myklebust659aefb2017-11-03 08:00:13 -04005242 ret = nfsd4_lock_ol_stateid(stp);
5243 if (ret)
5244 goto out_put_stid;
Chuck Lever42691392016-08-11 10:37:30 -04005245
5246 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5247 if (ret)
5248 goto out;
5249
5250 ret = nfserr_locks_held;
5251 if (check_for_locks(stp->st_stid.sc_file,
5252 lockowner(stp->st_stateowner)))
5253 goto out;
5254
5255 release_lock_stateid(stp);
5256 ret = nfs_ok;
5257
5258out:
5259 mutex_unlock(&stp->st_mutex);
Trond Myklebust659aefb2017-11-03 08:00:13 -04005260out_put_stid:
Chuck Lever42691392016-08-11 10:37:30 -04005261 nfs4_put_stid(s);
5262 return ret;
5263}
5264
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005265__be32
5266nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005267 union nfsd4_op_u *u)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005268{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005269 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005270 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005271 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005272 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005273 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005274 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005275
Jeff Layton1af71cc2014-07-29 21:34:14 -04005276 spin_lock(&cl->cl_lock);
5277 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005278 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005279 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005280 spin_lock(&s->sc_lock);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005281 switch (s->sc_type) {
5282 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005283 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005284 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005285 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005286 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5287 if (ret)
5288 break;
5289 ret = nfserr_locks_held;
5290 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005291 case NFS4_LOCK_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005292 spin_unlock(&s->sc_lock);
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03005293 refcount_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005294 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04005295 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005296 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005297 case NFS4_REVOKED_DELEG_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005298 spin_unlock(&s->sc_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005299 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005300 list_del_init(&dp->dl_recall_lru);
5301 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04005302 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005303 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005304 goto out;
5305 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005306 }
Trond Myklebust03da3162017-11-03 08:00:16 -04005307 spin_unlock(&s->sc_lock);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005308out_unlock:
5309 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005310out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005311 return ret;
5312}
5313
NeilBrown4c4cd222005-07-07 17:59:27 -07005314static inline int
5315setlkflg (int type)
5316{
5317 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5318 RD_STATE : WR_STATE;
5319}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005321static __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 -04005322{
5323 struct svc_fh *current_fh = &cstate->current_fh;
5324 struct nfs4_stateowner *sop = stp->st_stateowner;
5325 __be32 status;
5326
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005327 status = nfsd4_check_seqid(cstate, sop, seqid);
5328 if (status)
5329 return status;
Trond Myklebust9271d7e2017-11-03 08:00:15 -04005330 status = nfsd4_lock_ol_stateid(stp);
5331 if (status != nfs_ok)
5332 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005333 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04005334 if (status == nfs_ok)
5335 status = nfs4_check_fh(current_fh, &stp->st_stid);
5336 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005337 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005338 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005339}
5340
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341/*
5342 * Checks for sequence id mutating operations.
5343 */
Al Virob37ad282006-10-19 23:28:59 -07005344static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03005345nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04005346 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005347 struct nfs4_ol_stateid **stpp,
5348 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349{
J. Bruce Fields0836f582008-01-26 19:08:12 -05005350 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005351 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005352 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005354 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5355 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07005356
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005358 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005359 if (status)
5360 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005361 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005362 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005364 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005365 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005366 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04005367 else
5368 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005369 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005370}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05005371
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005372static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5373 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005374{
5375 __be32 status;
5376 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005377 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005379 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005380 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04005381 if (status)
5382 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005383 oo = openowner(stp->st_stateowner);
5384 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005385 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005386 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07005387 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005388 }
5389 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07005390 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391}
5392
Al Virob37ad282006-10-19 23:28:59 -07005393__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005394nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005395 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005397 struct nfsd4_open_confirm *oc = &u->open_confirm;
Al Virob37ad282006-10-19 23:28:59 -07005398 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005399 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005400 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005401 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402
Al Viroa6a9f182013-09-16 10:57:01 -04005403 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5404 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005406 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07005407 if (status)
5408 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005410 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005411 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005412 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005413 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005414 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005415 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005416 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005417 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005418 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04005419 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005420 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04005421 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04005422 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005423 mutex_unlock(&stp->st_mutex);
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005424 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005425 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07005426
Jeff Layton2a4317c2012-03-21 16:42:43 -04005427 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005428 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04005429put_stateid:
5430 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005432 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 return status;
5434}
5435
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005436static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005438 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005439 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04005440 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005441 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005442}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04005443
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005444static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5445{
5446 switch (to_access) {
5447 case NFS4_SHARE_ACCESS_READ:
5448 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5449 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5450 break;
5451 case NFS4_SHARE_ACCESS_WRITE:
5452 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5453 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5454 break;
5455 case NFS4_SHARE_ACCESS_BOTH:
5456 break;
5457 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005458 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 }
5460}
5461
Al Virob37ad282006-10-19 23:28:59 -07005462__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005463nfsd4_open_downgrade(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005464 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005466 struct nfsd4_open_downgrade *od = &u->open_downgrade;
Al Virob37ad282006-10-19 23:28:59 -07005467 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005468 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005469 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470
Al Viroa6a9f182013-09-16 10:57:01 -04005471 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5472 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04005474 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02005475 if (od->od_deleg_want)
5476 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5477 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005479 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005480 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005481 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005484 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005485 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005487 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04005489 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005490 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005492 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005494 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04005495 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04005496 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005498put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005499 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005500 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005502 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 return status;
5504}
5505
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005506static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5507{
Trond Myklebustacf92952014-06-30 11:48:37 -04005508 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04005509 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04005510 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005511
Jeff Layton2c41beb2014-07-29 21:34:41 -04005512 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04005513 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005514
Jeff Laytond83017f2014-07-29 21:34:42 -04005515 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04005516 if (unhashed)
5517 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04005518 spin_unlock(&clp->cl_lock);
5519 free_ol_stateid_reaplist(&reaplist);
5520 } else {
5521 spin_unlock(&clp->cl_lock);
5522 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04005523 if (unhashed)
5524 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04005525 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04005526}
5527
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528/*
5529 * nfs4_unlock_state() called after encode
5530 */
Al Virob37ad282006-10-19 23:28:59 -07005531__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005532nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005533 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005535 struct nfsd4_close *close = &u->close;
Al Virob37ad282006-10-19 23:28:59 -07005536 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005537 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005538 struct net *net = SVC_NET(rqstp);
5539 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540
Al Viroa6a9f182013-09-16 10:57:01 -04005541 dprintk("NFSD: nfsd4_close on file %pd\n",
5542 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005544 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5545 &close->cl_stateid,
5546 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005547 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005548 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005549 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005551
5552 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Laytonbd2deca2018-02-15 15:16:18 -05005553
5554 /*
5555 * Technically we don't _really_ have to increment or copy it, since
5556 * it should just be gone after this operation and we clobber the
5557 * copied value below, but we continue to do so here just to ensure
5558 * that racing ops see that there was a state change.
5559 */
Jeff Layton9767feb2015-10-01 09:05:50 -04005560 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005562 nfsd4_close_open_stateid(stp);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005563 mutex_unlock(&stp->st_mutex);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005564
Jeff Laytonbd2deca2018-02-15 15:16:18 -05005565 /* v4.1+ suggests that we send a special stateid in here, since the
5566 * clients should just ignore this anyway. Since this is not useful
5567 * for v4.0 clients either, we set it to the special close_stateid
5568 * universally.
5569 *
5570 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
5571 */
5572 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
Trond Myklebustfb500a72017-11-03 08:00:12 -04005573
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005574 /* put reference from nfs4_preprocess_seqid_op */
5575 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 return status;
5578}
5579
Al Virob37ad282006-10-19 23:28:59 -07005580__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005581nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005582 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005584 struct nfsd4_delegreturn *dr = &u->delegreturn;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005585 struct nfs4_delegation *dp;
5586 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005587 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07005588 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005589 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005591 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005592 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005594 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005595 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005596 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005597 dp = delegstateid(s);
Trond Myklebust03da3162017-11-03 08:00:16 -04005598 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005599 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04005600 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005601
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005602 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005603put_stateid:
5604 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605out:
5606 return status;
5607}
5608
Benny Halevy87df4de2008-12-15 19:42:03 +02005609static inline u64
5610end_offset(u64 start, u64 len)
5611{
5612 u64 end;
5613
5614 end = start + len;
5615 return end >= start ? end: NFS4_MAX_UINT64;
5616}
5617
5618/* last octet in a range */
5619static inline u64
5620last_byte_offset(u64 start, u64 len)
5621{
5622 u64 end;
5623
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005624 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02005625 end = start + len;
5626 return end > start ? end - 1: NFS4_MAX_UINT64;
5627}
5628
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629/*
5630 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5631 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5632 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5633 * locking, this prevents us from being completely protocol-compliant. The
5634 * real solution to this problem is to start using unsigned file offsets in
5635 * the VFS, but this is a very deep change!
5636 */
5637static inline void
5638nfs4_transform_lock_offset(struct file_lock *lock)
5639{
5640 if (lock->fl_start < 0)
5641 lock->fl_start = OFFSET_MAX;
5642 if (lock->fl_end < 0)
5643 lock->fl_end = OFFSET_MAX;
5644}
5645
Jeff Laytoncae80b32015-04-03 09:04:04 -04005646static fl_owner_t
5647nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005648{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005649 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5650
5651 nfs4_get_stateowner(&lo->lo_owner);
5652 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005653}
5654
Jeff Laytoncae80b32015-04-03 09:04:04 -04005655static void
5656nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005657{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005658 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005659
Jeff Laytoncae80b32015-04-03 09:04:04 -04005660 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04005661 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04005662}
5663
Jeff Layton76d348f2016-09-16 16:28:24 -04005664static void
5665nfsd4_lm_notify(struct file_lock *fl)
5666{
5667 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
5668 struct net *net = lo->lo_owner.so_client->net;
5669 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5670 struct nfsd4_blocked_lock *nbl = container_of(fl,
5671 struct nfsd4_blocked_lock, nbl_lock);
5672 bool queue = false;
5673
Jeff Layton7919d0a2016-09-16 16:28:25 -04005674 /* An empty list means that something else is going to be using it */
Jeff Layton0cc11a62016-10-20 09:34:31 -04005675 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005676 if (!list_empty(&nbl->nbl_list)) {
5677 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005678 list_del_init(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -04005679 queue = true;
5680 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04005681 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005682
5683 if (queue)
5684 nfsd4_run_cb(&nbl->nbl_cb);
5685}
5686
Alexey Dobriyan7b021962009-09-21 17:01:12 -07005687static const struct lock_manager_operations nfsd_posix_mng_ops = {
Jeff Layton76d348f2016-09-16 16:28:24 -04005688 .lm_notify = nfsd4_lm_notify,
Kinglong Meeaef95832014-08-22 10:18:44 -04005689 .lm_get_owner = nfsd4_fl_get_owner,
5690 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07005691};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692
5693static inline void
5694nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5695{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005696 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697
NeilBrownd5b90262006-04-10 22:55:22 -07005698 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005699 lo = (struct nfs4_lockowner *) fl->fl_owner;
5700 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5701 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005702 if (!deny->ld_owner.data)
5703 /* We just don't care that much */
5704 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005705 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5706 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07005707 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005708nevermind:
5709 deny->ld_owner.len = 0;
5710 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07005711 deny->ld_clientid.cl_boot = 0;
5712 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 }
5714 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02005715 deny->ld_length = NFS4_MAX_UINT64;
5716 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5718 deny->ld_type = NFS4_READ_LT;
5719 if (fl->fl_type != F_RDLCK)
5720 deny->ld_type = NFS4_WRITE_LT;
5721}
5722
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005723static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005724find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725{
Trond Myklebustd4f04892014-07-29 21:34:36 -04005726 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005727 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728
Trond Myklebust0a880a22014-07-30 08:27:10 -04005729 lockdep_assert_held(&clp->cl_lock);
5730
Trond Myklebustd4f04892014-07-29 21:34:36 -04005731 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5732 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005733 if (so->so_is_open_owner)
5734 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005735 if (same_owner_str(so, owner))
5736 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 }
5738 return NULL;
5739}
5740
Trond Myklebustc58c6612014-07-29 21:34:35 -04005741static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005742find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04005743{
5744 struct nfs4_lockowner *lo;
5745
Trond Myklebustd4f04892014-07-29 21:34:36 -04005746 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005747 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005748 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005749 return lo;
5750}
5751
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005752static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5753{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005754 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005755}
5756
Jeff Layton6b180f02014-07-29 21:34:26 -04005757static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5758{
5759 struct nfs4_lockowner *lo = lockowner(sop);
5760
5761 kmem_cache_free(lockowner_slab, lo);
5762}
5763
5764static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005765 .so_unhash = nfs4_unhash_lockowner,
5766 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005767};
5768
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769/*
5770 * Alloc a lock owner structure.
5771 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005772 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005774 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005776static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005777alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5778 struct nfs4_ol_stateid *open_stp,
5779 struct nfsd4_lock *lock)
5780{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005781 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005783 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5784 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04005786 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005787 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5788 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005789 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005790 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005791 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005792 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005793 if (ret == NULL) {
5794 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005795 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005796 ret = lo;
5797 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08005798 nfs4_free_stateowner(&lo->lo_owner);
5799
Trond Myklebustd4f04892014-07-29 21:34:36 -04005800 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04005801 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802}
5803
Trond Myklebustfd1fd682017-11-03 08:00:14 -04005804static struct nfs4_ol_stateid *
5805find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5806{
5807 struct nfs4_ol_stateid *lst;
5808 struct nfs4_client *clp = lo->lo_owner.so_client;
5809
5810 lockdep_assert_held(&clp->cl_lock);
5811
5812 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
5813 if (lst->st_stid.sc_type != NFS4_LOCK_STID)
5814 continue;
5815 if (lst->st_stid.sc_file == fp) {
5816 refcount_inc(&lst->st_stid.sc_count);
5817 return lst;
5818 }
5819 }
5820 return NULL;
5821}
5822
Trond Myklebustbeeca192017-11-03 08:00:14 -04005823static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04005824init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5825 struct nfs4_file *fp, struct inode *inode,
5826 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005828 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustbeeca192017-11-03 08:00:14 -04005829 struct nfs4_ol_stateid *retstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830
Trond Myklebustbeeca192017-11-03 08:00:14 -04005831 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05005832 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Trond Myklebustbeeca192017-11-03 08:00:14 -04005833retry:
5834 spin_lock(&clp->cl_lock);
5835 spin_lock(&fp->fi_lock);
5836 retstp = find_lock_stateid(lo, fp);
5837 if (retstp)
5838 goto out_unlock;
Jeff Layton356a95e2014-07-29 21:34:13 -04005839
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03005840 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005841 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005842 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07005843 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005844 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005845 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005847 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005848 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005849 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005850 list_add(&stp->st_perfile, &fp->fi_stateids);
Trond Myklebustbeeca192017-11-03 08:00:14 -04005851out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04005852 spin_unlock(&fp->fi_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04005853 spin_unlock(&clp->cl_lock);
5854 if (retstp) {
5855 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
5856 nfs4_put_stid(&retstp->st_stid);
5857 goto retry;
5858 }
5859 /* To keep mutex tracking happy */
5860 mutex_unlock(&stp->st_mutex);
5861 stp = retstp;
5862 }
5863 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864}
5865
Jeff Laytonc53530d2014-06-30 11:48:39 -04005866static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04005867find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5868 struct inode *inode, struct nfs4_ol_stateid *ost,
5869 bool *new)
5870{
5871 struct nfs4_stid *ns = NULL;
5872 struct nfs4_ol_stateid *lst;
5873 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5874 struct nfs4_client *clp = oo->oo_owner.so_client;
5875
Trond Myklebustbeeca192017-11-03 08:00:14 -04005876 *new = false;
Jeff Layton356a95e2014-07-29 21:34:13 -04005877 spin_lock(&clp->cl_lock);
5878 lst = find_lock_stateid(lo, fi);
Jeff Layton356a95e2014-07-29 21:34:13 -04005879 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04005880 if (lst != NULL) {
5881 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
5882 goto out;
5883 nfs4_put_stid(&lst->st_stid);
5884 }
5885 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
5886 if (ns == NULL)
5887 return NULL;
5888
5889 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
5890 if (lst == openlockstateid(ns))
5891 *new = true;
5892 else
Jeff Layton356a95e2014-07-29 21:34:13 -04005893 nfs4_put_stid(ns);
Trond Myklebustbeeca192017-11-03 08:00:14 -04005894out:
Jeff Layton356a95e2014-07-29 21:34:13 -04005895 return lst;
5896}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005897
NeilBrownfd39ca92005-06-23 22:04:03 -07005898static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899check_lock_length(u64 offset, u64 length)
5900{
Kinglong Meee7969312015-07-13 17:34:19 +08005901 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5902 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903}
5904
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005905static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005906{
Trond Myklebust11b91642014-07-29 21:34:08 -04005907 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005908
Jeff Layton7214e862014-07-10 14:07:33 -04005909 lockdep_assert_held(&fp->fi_lock);
5910
Jeff Layton82c5ff12012-05-11 09:45:13 -04005911 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005912 return;
Jeff Layton12659652014-07-10 14:07:28 -04005913 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005914 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005915}
5916
Jeff Layton356a95e2014-07-29 21:34:13 -04005917static __be32
5918lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5919 struct nfs4_ol_stateid *ost,
5920 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04005921 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005922{
Jeff Layton5db1c032014-07-29 21:34:28 -04005923 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005924 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005925 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5926 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00005927 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005928 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04005929 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005930 unsigned int strhashval;
5931
Kinglong Meec8623992015-07-13 17:35:37 +08005932 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005933 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005934 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005935 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5936 if (lo == NULL)
5937 return nfserr_jukebox;
5938 } else {
5939 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005940 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005941 if (!cstate->minorversion &&
5942 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005943 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005944 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005945
Jeff Laytondd257932016-08-11 10:37:39 -04005946 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5947 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005948 status = nfserr_jukebox;
5949 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005950 }
Jeff Laytondd257932016-08-11 10:37:39 -04005951
Jeff Layton5db1c032014-07-29 21:34:28 -04005952 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04005953 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04005954out:
5955 nfs4_put_stateowner(&lo->lo_owner);
5956 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005957}
5958
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959/*
5960 * LOCK operation
5961 */
Al Virob37ad282006-10-19 23:28:59 -07005962__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005963nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005964 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005966 struct nfsd4_lock *lock = &u->lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005967 struct nfs4_openowner *open_sop = NULL;
5968 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005969 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005970 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005971 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005972 struct file *filp = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04005973 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005974 struct file_lock *file_lock = NULL;
5975 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005976 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005977 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005978 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005979 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04005980 unsigned char fl_type;
5981 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005982 struct net *net = SVC_NET(rqstp);
5983 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984
5985 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5986 (long long) lock->lk_offset,
5987 (long long) lock->lk_length);
5988
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 if (check_lock_length(lock->lk_offset, lock->lk_length))
5990 return nfserr_inval;
5991
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005992 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005993 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005994 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5995 return status;
5996 }
5997
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005999 if (nfsd4_has_session(cstate))
6000 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006001 memcpy(&lock->lk_new_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04006002 &cstate->session->se_client->cl_clientid,
6003 sizeof(clientid_t));
6004
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006006 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006010 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 lock->lk_new_open_seqid,
6012 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006013 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07006014 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006016 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006017 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006018 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04006019 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006020 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006021 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006022 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04006023 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006024 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03006025 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006026 lock->lk_old_lock_seqid,
6027 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006028 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006029 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04006030 if (status)
6031 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006032 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006034 lkflg = setlkflg(lock->lk_type);
6035 status = nfs4_check_openmode(lock_stp, lkflg);
6036 if (status)
6037 goto out;
6038
NeilBrown0dd395d2005-07-07 17:59:15 -07006039 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006040 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006041 goto out;
6042 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006043 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006044 goto out;
6045
Trond Myklebust11b91642014-07-29 21:34:08 -04006046 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 case NFS4_READW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006049 if (nfsd4_has_session(cstate))
6050 fl_flags |= FL_SLEEP;
6051 /* Fallthrough */
6052 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006053 spin_lock(&fp->fi_lock);
6054 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006055 if (filp)
6056 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04006057 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006058 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006059 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060 case NFS4_WRITEW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006061 if (nfsd4_has_session(cstate))
6062 fl_flags |= FL_SLEEP;
6063 /* Fallthrough */
6064 case NFS4_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006065 spin_lock(&fp->fi_lock);
6066 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006067 if (filp)
6068 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04006069 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006070 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006071 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 default:
6073 status = nfserr_inval;
6074 goto out;
6075 }
Jeff Layton76d348f2016-09-16 16:28:24 -04006076
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006077 if (!filp) {
6078 status = nfserr_openmode;
6079 goto out;
6080 }
Kinglong Meeaef95832014-08-22 10:18:44 -04006081
Jeff Layton76d348f2016-09-16 16:28:24 -04006082 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6083 if (!nbl) {
6084 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6085 status = nfserr_jukebox;
6086 goto out;
6087 }
6088
6089 file_lock = &nbl->nbl_lock;
6090 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04006091 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04006092 file_lock->fl_pid = current->tgid;
6093 file_lock->fl_file = filp;
Jeff Layton76d348f2016-09-16 16:28:24 -04006094 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04006095 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6096 file_lock->fl_start = lock->lk_offset;
6097 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6098 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099
Jeff Layton21179d82012-08-21 08:03:32 -04006100 conflock = locks_alloc_lock();
6101 if (!conflock) {
6102 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6103 status = nfserr_jukebox;
6104 goto out;
6105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106
Jeff Layton76d348f2016-09-16 16:28:24 -04006107 if (fl_flags & FL_SLEEP) {
Jeff Layton7919d0a2016-09-16 16:28:25 -04006108 nbl->nbl_time = jiffies;
Jeff Layton0cc11a62016-10-20 09:34:31 -04006109 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006110 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006111 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006112 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006113 }
6114
Jeff Layton21179d82012-08-21 08:03:32 -04006115 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006116 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04006118 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07006119 status = 0;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04006120 if (lock->lk_reclaim)
6121 nn->somebody_reclaimed = true;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006122 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006123 case FILE_LOCK_DEFERRED:
6124 nbl = NULL;
6125 /* Fallthrough */
6126 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006127 status = nfserr_denied;
6128 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04006129 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006130 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006131 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006133 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04006134 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05006135 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04006136 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006137 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139out:
Jeff Layton76d348f2016-09-16 16:28:24 -04006140 if (nbl) {
6141 /* dequeue it if we queued it before */
6142 if (fl_flags & FL_SLEEP) {
Jeff Layton0cc11a62016-10-20 09:34:31 -04006143 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006144 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006145 list_del_init(&nbl->nbl_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006146 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006147 }
6148 free_blocked_lock(nbl);
6149 }
Trond Myklebustde186432014-07-10 14:07:26 -04006150 if (filp)
6151 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04006152 if (lock_stp) {
6153 /* Bump seqid manually if the 4.0 replay owner is openowner */
6154 if (cstate->replay_owner &&
6155 cstate->replay_owner != &lock_sop->lo_owner &&
6156 seqid_mutating_err(ntohl(status)))
6157 lock_sop->lo_owner.so_seqid++;
6158
6159 /*
6160 * If this is a new, never-before-used stateid, and we are
6161 * returning an error, then just go ahead and release it.
6162 */
J. Bruce Fields25020722018-01-17 16:25:59 -05006163 if (status && new)
Jeff Layton5db1c032014-07-29 21:34:28 -04006164 release_lock_stateid(lock_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006165
6166 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton5db1c032014-07-29 21:34:28 -04006167
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006168 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04006169 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006170 if (open_stp)
6171 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006172 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006173 if (conflock)
6174 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 return status;
6176}
6177
6178/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006179 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6180 * so we do a temporary open here just to get an open file to pass to
6181 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6182 * inode operation.)
6183 */
Al Viro04da6e92012-04-13 00:00:04 -04006184static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006185{
6186 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04006187 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
6188 if (!err) {
6189 err = nfserrno(vfs_test_lock(file, lock));
Christoph Hellwigfd891452015-04-28 15:41:16 +02006190 fput(file);
Al Viro04da6e92012-04-13 00:00:04 -04006191 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006192 return err;
6193}
6194
6195/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 * LOCKT operation
6197 */
Al Virob37ad282006-10-19 23:28:59 -07006198__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006199nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006200 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006202 struct nfsd4_lockt *lockt = &u->lockt;
Jeff Layton21179d82012-08-21 08:03:32 -04006203 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04006204 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006205 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006206 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006208 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209 return nfserr_grace;
6210
6211 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6212 return nfserr_inval;
6213
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006214 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04006215 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006216 if (status)
6217 goto out;
6218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219
J. Bruce Fields75c096f2011-08-15 18:39:32 -04006220 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222
Jeff Layton21179d82012-08-21 08:03:32 -04006223 file_lock = locks_alloc_lock();
6224 if (!file_lock) {
6225 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6226 status = nfserr_jukebox;
6227 goto out;
6228 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006229
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230 switch (lockt->lt_type) {
6231 case NFS4_READ_LT:
6232 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006233 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234 break;
6235 case NFS4_WRITE_LT:
6236 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006237 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 break;
6239 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04006240 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 status = nfserr_inval;
6242 goto out;
6243 }
6244
Kinglong Meec8623992015-07-13 17:35:37 +08006245 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006246 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04006247 file_lock->fl_owner = (fl_owner_t)lo;
6248 file_lock->fl_pid = current->tgid;
6249 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250
Jeff Layton21179d82012-08-21 08:03:32 -04006251 file_lock->fl_start = lockt->lt_offset;
6252 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253
Jeff Layton21179d82012-08-21 08:03:32 -04006254 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255
Jeff Layton21179d82012-08-21 08:03:32 -04006256 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04006257 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05006258 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04006259
Jeff Layton21179d82012-08-21 08:03:32 -04006260 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04006262 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 }
6264out:
Jeff Layton5db1c032014-07-29 21:34:28 -04006265 if (lo)
6266 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04006267 if (file_lock)
6268 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269 return status;
6270}
6271
Al Virob37ad282006-10-19 23:28:59 -07006272__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006273nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006274 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006276 struct nfsd4_locku *locku = &u->locku;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006277 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006279 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006280 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07006281 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006282 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6283
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6285 (long long) locku->lu_offset,
6286 (long long) locku->lu_length);
6287
6288 if (check_lock_length(locku->lu_offset, locku->lu_length))
6289 return nfserr_inval;
6290
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006291 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006292 &locku->lu_stateid, NFS4_LOCK_STID,
6293 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006294 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04006296 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006297 if (!filp) {
6298 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04006299 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006300 }
Jeff Layton21179d82012-08-21 08:03:32 -04006301 file_lock = locks_alloc_lock();
6302 if (!file_lock) {
6303 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6304 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04006305 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04006306 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006307
Jeff Layton21179d82012-08-21 08:03:32 -04006308 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04006309 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04006310 file_lock->fl_pid = current->tgid;
6311 file_lock->fl_file = filp;
6312 file_lock->fl_flags = FL_POSIX;
6313 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6314 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315
Jeff Layton21179d82012-08-21 08:03:32 -04006316 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6317 locku->lu_length);
6318 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319
Jeff Layton21179d82012-08-21 08:03:32 -04006320 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07006321 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05006322 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323 goto out_nfserr;
6324 }
Jeff Layton9767feb2015-10-01 09:05:50 -04006325 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Trond Myklebustde186432014-07-10 14:07:26 -04006326fput:
6327 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04006328put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006329 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04006330 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006332 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006333 if (file_lock)
6334 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335 return status;
6336
6337out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07006338 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04006339 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340}
6341
6342/*
6343 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006344 * true: locks held by lockowner
6345 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006347static bool
6348check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006350 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006351 int status = false;
6352 struct file *filp = find_any_file(fp);
6353 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006354 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006355
6356 if (!filp) {
6357 /* Any valid lock stateid should have some sort of access */
6358 WARN_ON_ONCE(1);
6359 return status;
6360 }
6361
Amir Goldstein64bed6c2018-07-13 17:22:24 +03006362 inode = locks_inode(filp);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006363 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006365 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05006366 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006367 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6368 if (fl->fl_owner == (fl_owner_t)lowner) {
6369 status = true;
6370 break;
6371 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08006372 }
Jeff Layton6109c852015-01-16 15:05:57 -05006373 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374 }
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006375 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376 return status;
6377}
6378
Al Virob37ad282006-10-19 23:28:59 -07006379__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08006380nfsd4_release_lockowner(struct svc_rqst *rqstp,
6381 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006382 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006384 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04006386 struct nfs4_stateowner *sop;
6387 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006388 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006390 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07006391 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006392 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006393 struct nfs4_client *clp;
Chuck Lever88584812016-07-13 16:40:14 -04006394 LIST_HEAD (reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395
6396 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6397 clid->cl_boot, clid->cl_id);
6398
Jeff Layton4b24ca72014-06-30 11:48:44 -04006399 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006400 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04006401 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006402
Trond Myklebustd4f04892014-07-29 21:34:36 -04006403 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04006404 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04006405 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04006406 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04006407 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04006408
6409 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006410 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04006411
Jeff Layton882e9d22014-07-29 21:34:37 -04006412 /* see if there are still any locks associated with it */
6413 lo = lockowner(sop);
6414 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6415 if (check_for_locks(stp->st_stid.sc_file, lo)) {
6416 status = nfserr_locks_held;
6417 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04006418 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04006419 }
Jeff Layton5adfd882014-07-29 21:34:31 -04006420 }
Jeff Layton882e9d22014-07-29 21:34:37 -04006421
Kinglong Meeb5971af2014-08-22 10:18:43 -04006422 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04006423 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04006424 }
Chuck Lever88584812016-07-13 16:40:14 -04006425 if (!lo) {
6426 spin_unlock(&clp->cl_lock);
6427 return status;
6428 }
6429
6430 unhash_lockowner_locked(lo);
6431 while (!list_empty(&lo->lo_owner.so_stateids)) {
6432 stp = list_first_entry(&lo->lo_owner.so_stateids,
6433 struct nfs4_ol_stateid,
6434 st_perstateowner);
6435 WARN_ON(!unhash_lock_stateid(stp));
6436 put_ol_stateid_locked(stp, &reaplist);
6437 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04006438 spin_unlock(&clp->cl_lock);
Chuck Lever88584812016-07-13 16:40:14 -04006439 free_ol_stateid_reaplist(&reaplist);
Jeff Layton68ef3bc2018-03-16 11:32:02 -04006440 remove_blocked_locks(lo);
Chuck Lever88584812016-07-13 16:40:14 -04006441 nfs4_put_stateowner(&lo->lo_owner);
6442
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443 return status;
6444}
6445
6446static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07006447alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448{
NeilBrowna55370a2005-06-23 22:03:52 -07006449 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450}
6451
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006452bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006453nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07006454{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006455 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07006456
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006457 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006458 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07006459}
6460
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461/*
6462 * failure => all reset bets are off, nfserr_no_grace...
6463 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05006464struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006465nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466{
6467 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006468 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469
NeilBrowna55370a2005-06-23 22:03:52 -07006470 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
6471 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05006472 if (crp) {
6473 strhashval = clientstr_hashval(name);
6474 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006475 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05006476 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006477 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006478 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006479 }
6480 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481}
6482
Jeff Layton2a4317c2012-03-21 16:42:43 -04006483void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006484nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05006485{
6486 list_del(&crp->cr_strhash);
6487 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006488 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05006489}
6490
6491void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006492nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493{
6494 struct nfs4_client_reclaim *crp = NULL;
6495 int i;
6496
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006498 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6499 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006501 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502 }
6503 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006504 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505}
6506
6507/*
6508 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04006509struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006510nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511{
6512 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513 struct nfs4_client_reclaim *crp = NULL;
6514
Jeff Layton278c9312012-11-12 15:00:52 -05006515 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516
Jeff Layton278c9312012-11-12 15:00:52 -05006517 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006518 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05006519 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520 return crp;
6521 }
6522 }
6523 return NULL;
6524}
6525
6526/*
6527* Called from OPEN. Look for clientid in reclaim list.
6528*/
Al Virob37ad282006-10-19 23:28:59 -07006529__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006530nfs4_check_open_reclaim(clientid_t *clid,
6531 struct nfsd4_compound_state *cstate,
6532 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006534 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04006535
6536 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006537 status = lookup_clientid(clid, cstate, nn);
6538 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04006539 return nfserr_reclaim_bad;
6540
Jeff Layton3b3e7b72014-09-12 16:40:22 -04006541 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6542 return nfserr_no_grace;
6543
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006544 if (nfsd4_client_record_check(cstate->clp))
6545 return nfserr_reclaim_bad;
6546
6547 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548}
6549
Bryan Schumaker65178db2011-11-01 13:35:21 -04006550#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04006551static inline void
6552put_client(struct nfs4_client *clp)
6553{
6554 atomic_dec(&clp->cl_refcount);
6555}
6556
Jeff Layton285abde2014-07-30 08:27:24 -04006557static struct nfs4_client *
6558nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6559{
6560 struct nfs4_client *clp;
6561 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6562 nfsd_net_id);
6563
6564 if (!nfsd_netns_ready(nn))
6565 return NULL;
6566
6567 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6568 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
6569 return clp;
6570 }
6571 return NULL;
6572}
6573
Jeff Layton7ec0e362014-07-30 08:27:17 -04006574u64
Jeff Layton285abde2014-07-30 08:27:24 -04006575nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04006576{
6577 struct nfs4_client *clp;
6578 u64 count = 0;
6579 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6580 nfsd_net_id);
6581 char buf[INET6_ADDRSTRLEN];
6582
6583 if (!nfsd_netns_ready(nn))
6584 return 0;
6585
6586 spin_lock(&nn->client_lock);
6587 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6588 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6589 pr_info("NFS Client: %s\n", buf);
6590 ++count;
6591 }
6592 spin_unlock(&nn->client_lock);
6593
6594 return count;
6595}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006596
Jeff Laytona0926d12014-07-30 08:27:18 -04006597u64
Jeff Layton285abde2014-07-30 08:27:24 -04006598nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04006599{
6600 u64 count = 0;
6601 struct nfs4_client *clp;
6602 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6603 nfsd_net_id);
6604
6605 if (!nfsd_netns_ready(nn))
6606 return count;
6607
6608 spin_lock(&nn->client_lock);
6609 clp = nfsd_find_client(addr, addr_size);
6610 if (clp) {
6611 if (mark_client_expired_locked(clp) == nfs_ok)
6612 ++count;
6613 else
6614 clp = NULL;
6615 }
6616 spin_unlock(&nn->client_lock);
6617
6618 if (clp)
6619 expire_client(clp);
6620
6621 return count;
6622}
6623
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006624u64
Jeff Layton285abde2014-07-30 08:27:24 -04006625nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006626{
6627 u64 count = 0;
6628 struct nfs4_client *clp, *next;
6629 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6630 nfsd_net_id);
6631 LIST_HEAD(reaplist);
6632
6633 if (!nfsd_netns_ready(nn))
6634 return count;
6635
6636 spin_lock(&nn->client_lock);
6637 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6638 if (mark_client_expired_locked(clp) == nfs_ok) {
6639 list_add(&clp->cl_lru, &reaplist);
6640 if (max != 0 && ++count >= max)
6641 break;
6642 }
6643 }
6644 spin_unlock(&nn->client_lock);
6645
6646 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6647 expire_client(clp);
6648
6649 return count;
6650}
6651
Bryan Schumaker184c1842012-11-29 11:40:44 -05006652static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6653 const char *type)
6654{
6655 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05006656 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05006657 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6658}
6659
Jeff Layton016200c2014-07-30 08:27:21 -04006660static void
6661nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6662 struct list_head *collect)
6663{
6664 struct nfs4_client *clp = lst->st_stid.sc_client;
6665 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6666 nfsd_net_id);
6667
6668 if (!collect)
6669 return;
6670
6671 lockdep_assert_held(&nn->client_lock);
6672 atomic_inc(&clp->cl_refcount);
6673 list_add(&lst->st_locks, collect);
6674}
6675
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006676static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04006677 struct list_head *collect,
Jeff Laytone8568732015-08-24 12:41:47 -04006678 bool (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05006679{
6680 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006681 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006682 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006683 u64 count = 0;
6684
Jeff Layton016200c2014-07-30 08:27:21 -04006685 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006686 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006687 list_for_each_entry_safe(stp, st_next,
6688 &oop->oo_owner.so_stateids, st_perstateowner) {
6689 list_for_each_entry_safe(lst, lst_next,
6690 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04006691 if (func) {
Jeff Laytone8568732015-08-24 12:41:47 -04006692 if (func(lst))
6693 nfsd_inject_add_lock_to_list(lst,
6694 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04006695 }
Jeff Layton016200c2014-07-30 08:27:21 -04006696 ++count;
6697 /*
6698 * Despite the fact that these functions deal
6699 * with 64-bit integers for "count", we must
6700 * ensure that it doesn't blow up the
6701 * clp->cl_refcount. Throw a warning if we
6702 * start to approach INT_MAX here.
6703 */
6704 WARN_ON_ONCE(count == (INT_MAX / 2));
6705 if (count == max)
6706 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006707 }
6708 }
6709 }
Jeff Layton016200c2014-07-30 08:27:21 -04006710out:
6711 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006712
6713 return count;
6714}
6715
Jeff Layton016200c2014-07-30 08:27:21 -04006716static u64
6717nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6718 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05006719{
Jeff Layton016200c2014-07-30 08:27:21 -04006720 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006721}
6722
Jeff Layton016200c2014-07-30 08:27:21 -04006723static u64
6724nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006725{
Jeff Layton016200c2014-07-30 08:27:21 -04006726 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006727 nfsd_print_count(clp, count, "locked files");
6728 return count;
6729}
6730
Jeff Layton016200c2014-07-30 08:27:21 -04006731u64
Jeff Layton285abde2014-07-30 08:27:24 -04006732nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04006733{
6734 struct nfs4_client *clp;
6735 u64 count = 0;
6736 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6737 nfsd_net_id);
6738
6739 if (!nfsd_netns_ready(nn))
6740 return 0;
6741
6742 spin_lock(&nn->client_lock);
6743 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6744 count += nfsd_print_client_locks(clp);
6745 spin_unlock(&nn->client_lock);
6746
6747 return count;
6748}
6749
6750static void
6751nfsd_reap_locks(struct list_head *reaplist)
6752{
6753 struct nfs4_client *clp;
6754 struct nfs4_ol_stateid *stp, *next;
6755
6756 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6757 list_del_init(&stp->st_locks);
6758 clp = stp->st_stid.sc_client;
6759 nfs4_put_stid(&stp->st_stid);
6760 put_client(clp);
6761 }
6762}
6763
6764u64
Jeff Layton285abde2014-07-30 08:27:24 -04006765nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04006766{
6767 unsigned int count = 0;
6768 struct nfs4_client *clp;
6769 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6770 nfsd_net_id);
6771 LIST_HEAD(reaplist);
6772
6773 if (!nfsd_netns_ready(nn))
6774 return count;
6775
6776 spin_lock(&nn->client_lock);
6777 clp = nfsd_find_client(addr, addr_size);
6778 if (clp)
6779 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6780 spin_unlock(&nn->client_lock);
6781 nfsd_reap_locks(&reaplist);
6782 return count;
6783}
6784
6785u64
Jeff Layton285abde2014-07-30 08:27:24 -04006786nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04006787{
6788 u64 count = 0;
6789 struct nfs4_client *clp;
6790 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6791 nfsd_net_id);
6792 LIST_HEAD(reaplist);
6793
6794 if (!nfsd_netns_ready(nn))
6795 return count;
6796
6797 spin_lock(&nn->client_lock);
6798 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6799 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6800 if (max != 0 && count >= max)
6801 break;
6802 }
6803 spin_unlock(&nn->client_lock);
6804 nfsd_reap_locks(&reaplist);
6805 return count;
6806}
6807
Jeff Layton82e05ef2014-07-30 08:27:22 -04006808static u64
6809nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6810 struct list_head *collect,
6811 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006812{
6813 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04006814 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6815 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006816 u64 count = 0;
6817
Jeff Layton82e05ef2014-07-30 08:27:22 -04006818 lockdep_assert_held(&nn->client_lock);
6819
6820 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006821 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04006822 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006823 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04006824 if (collect) {
6825 atomic_inc(&clp->cl_refcount);
6826 list_add(&oop->oo_perclient, collect);
6827 }
6828 }
6829 ++count;
6830 /*
6831 * Despite the fact that these functions deal with
6832 * 64-bit integers for "count", we must ensure that
6833 * it doesn't blow up the clp->cl_refcount. Throw a
6834 * warning if we start to approach INT_MAX here.
6835 */
6836 WARN_ON_ONCE(count == (INT_MAX / 2));
6837 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006838 break;
6839 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04006840 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006841
6842 return count;
6843}
6844
Jeff Layton82e05ef2014-07-30 08:27:22 -04006845static u64
6846nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006847{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006848 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6849
6850 nfsd_print_count(clp, count, "openowners");
6851 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006852}
6853
Jeff Layton82e05ef2014-07-30 08:27:22 -04006854static u64
6855nfsd_collect_client_openowners(struct nfs4_client *clp,
6856 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006857{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006858 return nfsd_foreach_client_openowner(clp, max, collect,
6859 unhash_openowner_locked);
6860}
6861
6862u64
Jeff Layton285abde2014-07-30 08:27:24 -04006863nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006864{
6865 struct nfs4_client *clp;
6866 u64 count = 0;
6867 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6868 nfsd_net_id);
6869
6870 if (!nfsd_netns_ready(nn))
6871 return 0;
6872
6873 spin_lock(&nn->client_lock);
6874 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6875 count += nfsd_print_client_openowners(clp);
6876 spin_unlock(&nn->client_lock);
6877
6878 return count;
6879}
6880
6881static void
6882nfsd_reap_openowners(struct list_head *reaplist)
6883{
6884 struct nfs4_client *clp;
6885 struct nfs4_openowner *oop, *next;
6886
6887 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6888 list_del_init(&oop->oo_perclient);
6889 clp = oop->oo_owner.so_client;
6890 release_openowner(oop);
6891 put_client(clp);
6892 }
6893}
6894
6895u64
Jeff Layton285abde2014-07-30 08:27:24 -04006896nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6897 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006898{
6899 unsigned int count = 0;
6900 struct nfs4_client *clp;
6901 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6902 nfsd_net_id);
6903 LIST_HEAD(reaplist);
6904
6905 if (!nfsd_netns_ready(nn))
6906 return count;
6907
6908 spin_lock(&nn->client_lock);
6909 clp = nfsd_find_client(addr, addr_size);
6910 if (clp)
6911 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6912 spin_unlock(&nn->client_lock);
6913 nfsd_reap_openowners(&reaplist);
6914 return count;
6915}
6916
6917u64
Jeff Layton285abde2014-07-30 08:27:24 -04006918nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006919{
6920 u64 count = 0;
6921 struct nfs4_client *clp;
6922 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6923 nfsd_net_id);
6924 LIST_HEAD(reaplist);
6925
6926 if (!nfsd_netns_ready(nn))
6927 return count;
6928
6929 spin_lock(&nn->client_lock);
6930 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6931 count += nfsd_collect_client_openowners(clp, &reaplist,
6932 max - count);
6933 if (max != 0 && count >= max)
6934 break;
6935 }
6936 spin_unlock(&nn->client_lock);
6937 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006938 return count;
6939}
6940
Bryan Schumaker269de302012-11-29 11:40:42 -05006941static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6942 struct list_head *victims)
6943{
6944 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006945 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6946 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006947 u64 count = 0;
6948
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006949 lockdep_assert_held(&nn->client_lock);
6950
6951 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006952 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006953 if (victims) {
6954 /*
6955 * It's not safe to mess with delegations that have a
6956 * non-zero dl_time. They might have already been broken
6957 * and could be processed by the laundromat outside of
6958 * the state_lock. Just leave them be.
6959 */
6960 if (dp->dl_time != 0)
6961 continue;
6962
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006963 atomic_inc(&clp->cl_refcount);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04006964 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04006965 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006966 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006967 ++count;
6968 /*
6969 * Despite the fact that these functions deal with
6970 * 64-bit integers for "count", we must ensure that
6971 * it doesn't blow up the clp->cl_refcount. Throw a
6972 * warning if we start to approach INT_MAX here.
6973 */
6974 WARN_ON_ONCE(count == (INT_MAX / 2));
6975 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006976 break;
6977 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006978 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006979 return count;
6980}
6981
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006982static u64
6983nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006984{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006985 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006986
6987 nfsd_print_count(clp, count, "delegations");
6988 return count;
6989}
6990
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006991u64
Jeff Layton285abde2014-07-30 08:27:24 -04006992nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006993{
6994 struct nfs4_client *clp;
6995 u64 count = 0;
6996 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6997 nfsd_net_id);
6998
6999 if (!nfsd_netns_ready(nn))
7000 return 0;
7001
7002 spin_lock(&nn->client_lock);
7003 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7004 count += nfsd_print_client_delegations(clp);
7005 spin_unlock(&nn->client_lock);
7006
7007 return count;
7008}
7009
7010static void
7011nfsd_forget_delegations(struct list_head *reaplist)
7012{
7013 struct nfs4_client *clp;
7014 struct nfs4_delegation *dp, *next;
7015
7016 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7017 list_del_init(&dp->dl_recall_lru);
7018 clp = dp->dl_stid.sc_client;
7019 revoke_delegation(dp);
7020 put_client(clp);
7021 }
7022}
7023
7024u64
Jeff Layton285abde2014-07-30 08:27:24 -04007025nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
7026 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007027{
7028 u64 count = 0;
7029 struct nfs4_client *clp;
7030 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7031 nfsd_net_id);
7032 LIST_HEAD(reaplist);
7033
7034 if (!nfsd_netns_ready(nn))
7035 return count;
7036
7037 spin_lock(&nn->client_lock);
7038 clp = nfsd_find_client(addr, addr_size);
7039 if (clp)
7040 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7041 spin_unlock(&nn->client_lock);
7042
7043 nfsd_forget_delegations(&reaplist);
7044 return count;
7045}
7046
7047u64
Jeff Layton285abde2014-07-30 08:27:24 -04007048nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007049{
7050 u64 count = 0;
7051 struct nfs4_client *clp;
7052 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7053 nfsd_net_id);
7054 LIST_HEAD(reaplist);
7055
7056 if (!nfsd_netns_ready(nn))
7057 return count;
7058
7059 spin_lock(&nn->client_lock);
7060 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7061 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7062 if (max != 0 && count >= max)
7063 break;
7064 }
7065 spin_unlock(&nn->client_lock);
7066 nfsd_forget_delegations(&reaplist);
7067 return count;
7068}
7069
7070static void
7071nfsd_recall_delegations(struct list_head *reaplist)
7072{
7073 struct nfs4_client *clp;
7074 struct nfs4_delegation *dp, *next;
7075
7076 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7077 list_del_init(&dp->dl_recall_lru);
7078 clp = dp->dl_stid.sc_client;
7079 /*
7080 * We skipped all entries that had a zero dl_time before,
7081 * so we can now reset the dl_time back to 0. If a delegation
7082 * break comes in now, then it won't make any difference since
7083 * we're recalling it either way.
7084 */
7085 spin_lock(&state_lock);
7086 dp->dl_time = 0;
7087 spin_unlock(&state_lock);
7088 nfsd_break_one_deleg(dp);
7089 put_client(clp);
7090 }
7091}
7092
7093u64
Jeff Layton285abde2014-07-30 08:27:24 -04007094nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007095 size_t addr_size)
7096{
7097 u64 count = 0;
7098 struct nfs4_client *clp;
7099 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7100 nfsd_net_id);
7101 LIST_HEAD(reaplist);
7102
7103 if (!nfsd_netns_ready(nn))
7104 return count;
7105
7106 spin_lock(&nn->client_lock);
7107 clp = nfsd_find_client(addr, addr_size);
7108 if (clp)
7109 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7110 spin_unlock(&nn->client_lock);
7111
7112 nfsd_recall_delegations(&reaplist);
7113 return count;
7114}
7115
7116u64
Jeff Layton285abde2014-07-30 08:27:24 -04007117nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007118{
7119 u64 count = 0;
7120 struct nfs4_client *clp, *next;
7121 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7122 nfsd_net_id);
7123 LIST_HEAD(reaplist);
7124
7125 if (!nfsd_netns_ready(nn))
7126 return count;
7127
7128 spin_lock(&nn->client_lock);
7129 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7130 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7131 if (max != 0 && ++count >= max)
7132 break;
7133 }
7134 spin_unlock(&nn->client_lock);
7135 nfsd_recall_delegations(&reaplist);
7136 return count;
7137}
Bryan Schumaker65178db2011-11-01 13:35:21 -04007138#endif /* CONFIG_NFSD_FAULT_INJECTION */
7139
Meelap Shahc2f1a552007-07-17 04:04:39 -07007140/*
7141 * Since the lifetime of a delegation isn't limited to that of an open, a
7142 * client may quite reasonably hang on to a delegation as long as it has
7143 * the inode cached. This becomes an obvious problem the first time a
7144 * client's inode cache approaches the size of the server's total memory.
7145 *
7146 * For now we avoid this problem by imposing a hard limit on the number
7147 * of delegations, which varies according to the server's memory size.
7148 */
7149static void
7150set_max_delegations(void)
7151{
7152 /*
7153 * Allow at most 4 delegations per megabyte of RAM. Quick
7154 * estimates suggest that in the worst case (where every delegation
7155 * is for a different inode), a delegation could take about 1.5K,
7156 * giving a worst case usage of about 6% of memory.
7157 */
7158 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7159}
7160
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007161static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007162{
7163 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7164 int i;
7165
Kees Cook6da2ec52018-06-12 13:55:00 -07007166 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7167 sizeof(struct list_head),
7168 GFP_KERNEL);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007169 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007170 goto err;
Kees Cook6da2ec52018-06-12 13:55:00 -07007171 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7172 sizeof(struct list_head),
7173 GFP_KERNEL);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007174 if (!nn->unconf_id_hashtbl)
7175 goto err_unconf_id;
Kees Cook6da2ec52018-06-12 13:55:00 -07007176 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7177 sizeof(struct list_head),
7178 GFP_KERNEL);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007179 if (!nn->sessionid_hashtbl)
7180 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007181
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007182 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007183 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007184 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007185 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007186 for (i = 0; i < SESSION_HASH_SIZE; i++)
7187 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007188 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007189 nn->unconf_name_tree = RB_ROOT;
Vasily Averin81833de2017-11-13 07:25:40 +03007190 nn->boot_time = get_seconds();
7191 nn->grace_ended = false;
7192 nn->nfsd4_manager.block_opens = true;
7193 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03007194 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03007195 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007196 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03007197 spin_lock_init(&nn->client_lock);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04007198 spin_lock_init(&nn->s2s_cp_lock);
7199 idr_init(&nn->s2s_cp_stateids);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007200
Jeff Layton0cc11a62016-10-20 09:34:31 -04007201 spin_lock_init(&nn->blocked_locks_lock);
7202 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7203
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007204 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007205 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007206
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007207 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007208
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007209err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03007210 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007211err_unconf_id:
7212 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007213err:
7214 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007215}
7216
7217static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007218nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007219{
7220 int i;
7221 struct nfs4_client *clp = NULL;
7222 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7223
7224 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7225 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7226 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7227 destroy_client(clp);
7228 }
7229 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007230
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007231 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7232
Kinglong Mee2b905632014-03-26 22:09:30 +08007233 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7234 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7235 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7236 destroy_client(clp);
7237 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007238 }
7239
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007240 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007241 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007242 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007243 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007244}
7245
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007246int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007247nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07007248{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04007249 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007250 int ret;
7251
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007252 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007253 if (ret)
7254 return ret;
Jeff Laytond4318ac2014-09-12 16:40:21 -04007255 locks_start_grace(net, &nn->nfsd4_manager);
7256 nfsd4_client_tracking_init(net);
Vasily Averin7e981a82017-11-06 16:46:04 +03007257 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %x)\n",
7258 nn->nfsd4_grace, net->ns.inum);
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03007259 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007260 return 0;
7261}
7262
7263/* initialization to perform when the nfsd service is started: */
7264
7265int
7266nfs4_state_start(void)
7267{
7268 int ret;
7269
Jeff Layton51a54562015-08-20 07:17:01 -04007270 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05007271 if (laundry_wq == NULL) {
7272 ret = -ENOMEM;
Chuck Levera26dd642018-08-16 12:06:09 -04007273 goto out;
Jeff Laytona6d6b782012-03-05 11:42:36 -05007274 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007275 ret = nfsd4_create_callback_queue();
7276 if (ret)
7277 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007278
Meelap Shahc2f1a552007-07-17 04:04:39 -07007279 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007280 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007281
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007282out_free_laundry:
7283 destroy_workqueue(laundry_wq);
Chuck Levera26dd642018-08-16 12:06:09 -04007284out:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007285 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007286}
7287
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007288void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007289nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007293 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007295 cancel_delayed_work_sync(&nn->laundromat_work);
7296 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05007297
Linus Torvalds1da177e2005-04-16 15:20:36 -07007298 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04007299 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007300 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007301 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007302 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007303 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007304 }
Benny Halevycdc975052014-05-30 09:09:30 -04007305 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306 list_for_each_safe(pos, next, &reaplist) {
7307 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04007308 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05007309 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310 }
7311
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007312 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007313 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007314}
7315
7316void
7317nfs4_state_shutdown(void)
7318{
NeilBrown5e8d5c22006-04-10 22:55:37 -07007319 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04007320 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007321}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007322
7323static void
7324get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7325{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007326 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
7327 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007328}
7329
7330static void
7331put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7332{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007333 if (cstate->minorversion) {
7334 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7335 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7336 }
7337}
7338
7339void
7340clear_current_stateid(struct nfsd4_compound_state *cstate)
7341{
7342 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007343}
7344
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007345/*
7346 * functions to set current state id
7347 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007348void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007349nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7350 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007351{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007352 put_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007353}
7354
7355void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007356nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7357 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007358{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007359 put_stateid(cstate, &u->open.op_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007360}
7361
7362void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007363nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7364 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007365{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007366 put_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007367}
7368
7369void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007370nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7371 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007372{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007373 put_stateid(cstate, &u->lock.lk_resp_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007374}
7375
7376/*
7377 * functions to consume current state id
7378 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007379
7380void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007381nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7382 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007383{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007384 get_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007385}
7386
7387void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007388nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7389 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007390{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007391 get_stateid(cstate, &u->delegreturn.dr_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007392}
7393
7394void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007395nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7396 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007397{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007398 get_stateid(cstate, &u->free_stateid.fr_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007399}
7400
7401void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007402nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7403 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007404{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007405 get_stateid(cstate, &u->setattr.sa_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007406}
7407
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007408void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007409nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7410 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007411{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007412 get_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007413}
7414
7415void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007416nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7417 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007418{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007419 get_stateid(cstate, &u->locku.lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007420}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007421
7422void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007423nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7424 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007425{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007426 get_stateid(cstate, &u->read.rd_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007427}
7428
7429void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007430nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7431 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007432{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007433 get_stateid(cstate, &u->write.wr_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007434}