blob: fdcebfce5fdb76a60b3c7e2ac37f5e1794463d5d [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 Halevycdc97502014-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 Halevycdc97502014-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
Jeff Laytonb49e0842014-07-29 21:34:11 -0400716static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400717{
Trond Myklebust60116952014-07-29 21:34:06 -0400718 struct nfs4_stid *stid;
Trond Myklebust60116952014-07-29 21:34:06 -0400719
Kinglong Meed19fb702017-01-18 19:04:42 +0800720 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
Trond Myklebust60116952014-07-29 21:34:06 -0400721 if (!stid)
722 return NULL;
723
Kinglong Meed19fb702017-01-18 19:04:42 +0800724 return openlockstateid(stid);
Trond Myklebust60116952014-07-29 21:34:06 -0400725}
726
727static void nfs4_free_deleg(struct nfs4_stid *stid)
728{
Trond Myklebust60116952014-07-29 21:34:06 -0400729 kmem_cache_free(deleg_slab, stid);
730 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400731}
732
NeilBrown6282cd52014-06-04 17:39:26 +1000733/*
734 * When we recall a delegation, we should be careful not to hand it
735 * out again straight away.
736 * To ensure this we keep a pair of bloom filters ('new' and 'old')
737 * in which the filehandles of recalled delegations are "stored".
738 * If a filehandle appear in either filter, a delegation is blocked.
739 * When a delegation is recalled, the filehandle is stored in the "new"
740 * filter.
741 * Every 30 seconds we swap the filters and clear the "new" one,
742 * unless both are empty of course.
743 *
744 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
745 * low 3 bytes as hash-table indices.
746 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400747 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000748 * is used to manage concurrent access. Testing does not need the lock
749 * except when swapping the two filters.
750 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400751static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000752static struct bloom_pair {
753 int entries, old_entries;
754 time_t swap_time;
755 int new; /* index into 'set' */
756 DECLARE_BITMAP(set[2], 256);
757} blocked_delegations;
758
759static int delegation_blocked(struct knfsd_fh *fh)
760{
761 u32 hash;
762 struct bloom_pair *bd = &blocked_delegations;
763
764 if (bd->entries == 0)
765 return 0;
766 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400767 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000768 if (seconds_since_boot() - bd->swap_time > 30) {
769 bd->entries -= bd->old_entries;
770 bd->old_entries = bd->entries;
771 memset(bd->set[bd->new], 0,
772 sizeof(bd->set[0]));
773 bd->new = 1-bd->new;
774 bd->swap_time = seconds_since_boot();
775 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400776 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000777 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100778 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000779 if (test_bit(hash&255, bd->set[0]) &&
780 test_bit((hash>>8)&255, bd->set[0]) &&
781 test_bit((hash>>16)&255, bd->set[0]))
782 return 1;
783
784 if (test_bit(hash&255, bd->set[1]) &&
785 test_bit((hash>>8)&255, bd->set[1]) &&
786 test_bit((hash>>16)&255, bd->set[1]))
787 return 1;
788
789 return 0;
790}
791
792static void block_delegations(struct knfsd_fh *fh)
793{
794 u32 hash;
795 struct bloom_pair *bd = &blocked_delegations;
796
Daniel Borkmann87545892014-12-10 16:33:11 +0100797 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000798
Jeff Laytonf54fe962014-07-25 07:34:26 -0400799 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000800 __set_bit(hash&255, bd->set[bd->new]);
801 __set_bit((hash>>8)&255, bd->set[bd->new]);
802 __set_bit((hash>>16)&255, bd->set[bd->new]);
803 if (bd->entries == 0)
804 bd->swap_time = seconds_since_boot();
805 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400806 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000807}
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809static struct nfs4_delegation *
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500810alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
811 struct svc_fh *current_fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +0200812 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400815 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400818 n = atomic_long_inc_return(&num_delegations);
819 if (n < 0 || n > max_delegations)
820 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000821 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400822 goto out_dec;
Kinglong Meed19fb702017-01-18 19:04:42 +0800823 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700824 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400825 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400826
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400827 /*
828 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400829 * meaning of seqid 0 isn't meaningful, really, but let's avoid
830 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400831 */
832 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700833 INIT_LIST_HEAD(&dp->dl_perfile);
834 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200836 dp->dl_clnt_odstate = odstate;
837 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400838 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200839 dp->dl_retries = 1;
840 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200841 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500842 get_nfs4_file(fp);
843 dp->dl_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400845out_dec:
846 atomic_long_dec(&num_delegations);
847 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
850void
Trond Myklebust60116952014-07-29 21:34:06 -0400851nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Trond Myklebust11b91642014-07-29 21:34:08 -0400853 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400854 struct nfs4_client *clp = s->sc_client;
855
Jeff Layton4770d722014-07-29 21:34:10 -0400856 might_lock(&clp->cl_lock);
857
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300858 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
Jeff Laytonb401be222014-07-29 21:34:33 -0400859 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400860 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400861 }
Trond Myklebust60116952014-07-29 21:34:06 -0400862 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400863 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400864 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400865 if (fp)
866 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
Jeff Layton9767feb2015-10-01 09:05:50 -0400869void
870nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
871{
872 stateid_t *src = &stid->sc_stateid;
873
874 spin_lock(&stid->sc_lock);
875 if (unlikely(++src->si_generation == 0))
876 src->si_generation = 1;
877 memcpy(dst, src, sizeof(*dst));
878 spin_unlock(&stid->sc_lock);
879}
880
J. Bruce Fieldscba7b3d2018-02-15 15:50:49 -0500881static void nfs4_put_deleg_lease(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Jeff Layton6bcc0342014-08-09 10:22:40 -0400883 struct file *filp = NULL;
J. Bruce Fieldscba7b3d2018-02-15 15:50:49 -0500884 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton417c6622014-07-21 09:34:57 -0400885
J. Bruce Fieldsb8232d32018-02-15 15:29:15 -0500886 WARN_ON_ONCE(!fp->fi_delegees);
887
Jeff Layton6bcc0342014-08-09 10:22:40 -0400888 spin_lock(&fp->fi_lock);
J. Bruce Fieldsb8232d32018-02-15 15:29:15 -0500889 if (--fp->fi_delegees == 0) {
Jeff Layton6bcc0342014-08-09 10:22:40 -0400890 swap(filp, fp->fi_deleg_file);
J. Bruce Fieldsb8232d32018-02-15 15:29:15 -0500891 }
Jeff Layton6bcc0342014-08-09 10:22:40 -0400892 spin_unlock(&fp->fi_lock);
893
894 if (filp) {
J. Bruce Fields653e5142018-02-15 22:08:45 -0500895 vfs_setlease(filp, F_UNLCK, NULL, (void **)&dp);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400896 fput(filp);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
J. Bruce Fields0af6e692018-02-21 15:11:03 -0500900static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
901{
902 put_clnt_odstate(dp->dl_clnt_odstate);
903 nfs4_put_deleg_lease(dp);
904 nfs4_put_stid(&dp->dl_stid);
905}
906
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200907void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400908{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500909 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400910}
911
Andrew Elble34ed9872015-10-15 12:07:28 -0400912/**
J. Bruce Fields68b18f52018-02-19 11:38:33 -0500913 * nfs4_delegation_exists - Discover if this delegation already exists
Andrew Elble34ed9872015-10-15 12:07:28 -0400914 * @clp: a pointer to the nfs4_client we're granting a delegation to
915 * @fp: a pointer to the nfs4_file we're granting a delegation on
916 *
917 * Return:
J. Bruce Fields68b18f52018-02-19 11:38:33 -0500918 * On success: true iff an existing delegation is found
Andrew Elble34ed9872015-10-15 12:07:28 -0400919 */
920
J. Bruce Fields68b18f52018-02-19 11:38:33 -0500921static bool
922nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
Benny Halevy931ee562014-05-30 09:09:27 -0400923{
Andrew Elble34ed9872015-10-15 12:07:28 -0400924 struct nfs4_delegation *searchdp = NULL;
925 struct nfs4_client *searchclp = NULL;
926
Benny Halevycdc97502014-05-30 09:09:30 -0400927 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400928 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400929
Andrew Elble34ed9872015-10-15 12:07:28 -0400930 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
931 searchclp = searchdp->dl_stid.sc_client;
932 if (clp == searchclp) {
933 return -EAGAIN;
934 }
935 }
936 return 0;
937}
938
939/**
940 * hash_delegation_locked - Add a delegation to the appropriate lists
941 * @dp: a pointer to the nfs4_delegation we are adding.
942 * @fp: a pointer to the nfs4_file we're granting a delegation on
943 *
944 * Return:
945 * On success: NULL if the delegation was successfully hashed.
946 *
947 * On error: -EAGAIN if one was previously granted to this
948 * nfs4_client for this nfs4_file. Delegation is not hashed.
949 *
950 */
951
952static int
953hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
954{
Andrew Elble34ed9872015-10-15 12:07:28 -0400955 struct nfs4_client *clp = dp->dl_stid.sc_client;
956
957 lockdep_assert_held(&state_lock);
958 lockdep_assert_held(&fp->fi_lock);
959
J. Bruce Fields68b18f52018-02-19 11:38:33 -0500960 if (nfs4_delegation_exists(clp, fp))
961 return -EAGAIN;
Andrew Elble34ed9872015-10-15 12:07:28 -0400962 ++fp->fi_delegees;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300963 refcount_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400964 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400965 list_add(&dp->dl_perfile, &fp->fi_delegations);
Andrew Elble34ed9872015-10-15 12:07:28 -0400966 list_add(&dp->dl_perclnt, &clp->cl_delegations);
967 return 0;
Benny Halevy931ee562014-05-30 09:09:27 -0400968}
969
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400970static bool
Jeff Layton42690672014-07-25 07:34:20 -0400971unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Trond Myklebust11b91642014-07-29 21:34:08 -0400973 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400974
Jeff Layton42690672014-07-25 07:34:20 -0400975 lockdep_assert_held(&state_lock);
976
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400977 if (list_empty(&dp->dl_perfile))
978 return false;
979
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400980 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400981 /* Ensure that deleg break won't try to requeue it */
982 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400983 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400984 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400986 list_del_init(&dp->dl_perfile);
987 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400988 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400989}
990
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400991static void destroy_delegation(struct nfs4_delegation *dp)
992{
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400993 bool unhashed;
994
Jeff Layton42690672014-07-25 07:34:20 -0400995 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400996 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -0400997 spin_unlock(&state_lock);
J. Bruce Fields0af6e692018-02-21 15:11:03 -0500998 if (unhashed)
999 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001000}
1001
1002static void revoke_delegation(struct nfs4_delegation *dp)
1003{
1004 struct nfs4_client *clp = dp->dl_stid.sc_client;
1005
Jeff Layton2d4a5322014-07-25 07:34:21 -04001006 WARN_ON(!list_empty(&dp->dl_recall_lru));
1007
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001008 if (clp->cl_minorversion) {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001009 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001010 refcount_inc(&dp->dl_stid.sc_count);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001011 spin_lock(&clp->cl_lock);
1012 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1013 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001014 }
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001015 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001016}
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018/*
1019 * SETCLIENTID state
1020 */
1021
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001022static unsigned int clientid_hashval(u32 id)
1023{
1024 return id & CLIENT_HASH_MASK;
1025}
1026
1027static unsigned int clientstr_hashval(const char *name)
1028{
1029 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
1030}
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001033 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1034 * st_{access,deny}_bmap field of the stateid, in order to track not
1035 * only what share bits are currently in force, but also what
1036 * combinations of share bits previous opens have used. This allows us
1037 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1038 * return an error if the client attempt to downgrade to a combination
1039 * of share bits not explicable by closing some of its previous opens.
1040 *
1041 * XXX: This enforcement is actually incomplete, since we don't keep
1042 * track of access/deny bit combinations; so, e.g., we allow:
1043 *
1044 * OPEN allow read, deny write
1045 * OPEN allow both, deny none
1046 * DOWNGRADE allow read, deny none
1047 *
1048 * which we should reject.
1049 */
Jeff Layton5ae037e2012-05-11 09:45:11 -04001050static unsigned int
1051bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001052 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -04001053 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001054
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001055 for (i = 1; i < 4; i++) {
1056 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -04001057 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001058 }
Jeff Layton5ae037e2012-05-11 09:45:11 -04001059 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001060}
1061
Jeff Layton82c5ff12012-05-11 09:45:13 -04001062/* set share access for a given stateid */
1063static inline void
1064set_access(u32 access, struct nfs4_ol_stateid *stp)
1065{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001066 unsigned char mask = 1 << access;
1067
1068 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1069 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001070}
1071
1072/* clear share access for a given stateid */
1073static inline void
1074clear_access(u32 access, struct nfs4_ol_stateid *stp)
1075{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001076 unsigned char mask = 1 << access;
1077
1078 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1079 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001080}
1081
1082/* test whether a given stateid has access */
1083static inline bool
1084test_access(u32 access, struct nfs4_ol_stateid *stp)
1085{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001086 unsigned char mask = 1 << access;
1087
1088 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001089}
1090
Jeff Laytonce0fc432012-05-11 09:45:14 -04001091/* set share deny for a given stateid */
1092static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001093set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001094{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001095 unsigned char mask = 1 << deny;
1096
1097 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1098 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001099}
1100
1101/* clear share deny for a given stateid */
1102static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001103clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001104{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001105 unsigned char mask = 1 << deny;
1106
1107 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1108 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001109}
1110
1111/* test whether a given stateid is denying specific access */
1112static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -04001113test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001114{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001115 unsigned char mask = 1 << deny;
1116
1117 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001118}
1119
1120static int nfs4_access_to_omode(u32 access)
1121{
J. Bruce Fields8f34a432010-09-02 15:23:16 -04001122 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001123 case NFS4_SHARE_ACCESS_READ:
1124 return O_RDONLY;
1125 case NFS4_SHARE_ACCESS_WRITE:
1126 return O_WRONLY;
1127 case NFS4_SHARE_ACCESS_BOTH:
1128 return O_RDWR;
1129 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001130 WARN_ON_ONCE(1);
1131 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001132}
1133
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001134/*
1135 * A stateid that had a deny mode associated with it is being released
1136 * or downgraded. Recalculate the deny mode on the file.
1137 */
1138static void
1139recalculate_deny_mode(struct nfs4_file *fp)
1140{
1141 struct nfs4_ol_stateid *stp;
1142
1143 spin_lock(&fp->fi_lock);
1144 fp->fi_share_deny = 0;
1145 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1146 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1147 spin_unlock(&fp->fi_lock);
1148}
1149
1150static void
1151reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1152{
1153 int i;
1154 bool change = false;
1155
1156 for (i = 1; i < 4; i++) {
1157 if ((i & deny) != i) {
1158 change = true;
1159 clear_deny(i, stp);
1160 }
1161 }
1162
1163 /* Recalculate per-file deny mode if there was a change */
1164 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -04001165 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001166}
1167
Jeff Layton82c5ff12012-05-11 09:45:13 -04001168/* release all access and file references for a given stateid */
1169static void
1170release_all_access(struct nfs4_ol_stateid *stp)
1171{
1172 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -04001173 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001174
1175 if (fp && stp->st_deny_bmap != 0)
1176 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001177
1178 for (i = 1; i < 4; i++) {
1179 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -04001180 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001181 clear_access(i, stp);
1182 }
1183}
1184
Kinglong Meed50ffde2015-07-16 12:05:07 +08001185static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1186{
1187 kfree(sop->so_owner.data);
1188 sop->so_ops->so_free(sop);
1189}
1190
Jeff Layton6b180f02014-07-29 21:34:26 -04001191static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1192{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001193 struct nfs4_client *clp = sop->so_client;
1194
1195 might_lock(&clp->cl_lock);
1196
1197 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001198 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001199 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001200 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001201 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001202}
1203
Jeff Laytone8568732015-08-24 12:41:47 -04001204static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001205{
Trond Myklebust11b91642014-07-29 21:34:08 -04001206 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001207
Jeff Layton1c755dc2014-07-29 21:34:12 -04001208 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1209
Jeff Laytone8568732015-08-24 12:41:47 -04001210 if (list_empty(&stp->st_perfile))
1211 return false;
1212
Trond Myklebust1d31a252014-07-10 14:07:25 -04001213 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001214 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001215 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001216 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001217 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001218}
1219
Trond Myklebust60116952014-07-29 21:34:06 -04001220static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001221{
Trond Myklebust60116952014-07-29 21:34:06 -04001222 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001223
Sachin Bhamare8287f002015-04-27 14:50:14 +02001224 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001225 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001226 if (stp->st_stateowner)
1227 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -04001228 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001229}
1230
Jeff Laytonb49e0842014-07-29 21:34:11 -04001231static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001232{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001233 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1234 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001235 struct file *file;
1236
Jeff Laytonb49e0842014-07-29 21:34:11 -04001237 file = find_any_file(stp->st_stid.sc_file);
1238 if (file)
1239 filp_close(file, (fl_owner_t)lo);
1240 nfs4_free_ol_stateid(stid);
1241}
1242
Jeff Layton2c41beb2014-07-29 21:34:41 -04001243/*
1244 * Put the persistent reference to an already unhashed generic stateid, while
1245 * holding the cl_lock. If it's the last reference, then put it onto the
1246 * reaplist for later destruction.
1247 */
1248static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1249 struct list_head *reaplist)
1250{
1251 struct nfs4_stid *s = &stp->st_stid;
1252 struct nfs4_client *clp = s->sc_client;
1253
1254 lockdep_assert_held(&clp->cl_lock);
1255
1256 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1257
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001258 if (!refcount_dec_and_test(&s->sc_count)) {
Jeff Layton2c41beb2014-07-29 21:34:41 -04001259 wake_up_all(&close_wq);
1260 return;
1261 }
1262
1263 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1264 list_add(&stp->st_locks, reaplist);
1265}
1266
Jeff Laytone8568732015-08-24 12:41:47 -04001267static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001268{
Chuck Leverf46c4452016-10-29 18:19:03 -04001269 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001270
1271 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001272 nfs4_unhash_stid(&stp->st_stid);
Jeff Laytone8568732015-08-24 12:41:47 -04001273 return unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001274}
1275
Jeff Layton5adfd882014-07-29 21:34:31 -04001276static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001277{
Chuck Leverf46c4452016-10-29 18:19:03 -04001278 struct nfs4_client *clp = stp->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04001279 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001280
Chuck Leverf46c4452016-10-29 18:19:03 -04001281 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001282 unhashed = unhash_lock_stateid(stp);
Chuck Leverf46c4452016-10-29 18:19:03 -04001283 spin_unlock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001284 if (unhashed)
1285 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001286}
1287
Trond Myklebustc58c6612014-07-29 21:34:35 -04001288static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001289{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001290 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001291
Trond Myklebustd4f04892014-07-29 21:34:36 -04001292 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001293
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001294 list_del_init(&lo->lo_owner.so_strhash);
1295}
1296
Jeff Layton2c41beb2014-07-29 21:34:41 -04001297/*
1298 * Free a list of generic stateids that were collected earlier after being
1299 * fully unhashed.
1300 */
1301static void
1302free_ol_stateid_reaplist(struct list_head *reaplist)
1303{
1304 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001305 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001306
1307 might_sleep();
1308
1309 while (!list_empty(reaplist)) {
1310 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1311 st_locks);
1312 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001313 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001314 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001315 if (fp)
1316 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001317 }
1318}
1319
Jeff Laytond83017f2014-07-29 21:34:42 -04001320static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1321 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001322{
1323 struct nfs4_ol_stateid *stp;
1324
Jeff Laytone8568732015-08-24 12:41:47 -04001325 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1326
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001327 while (!list_empty(&open_stp->st_locks)) {
1328 stp = list_entry(open_stp->st_locks.next,
1329 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001330 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001331 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001332 }
1333}
1334
Jeff Laytone8568732015-08-24 12:41:47 -04001335static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001336 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001337{
Jeff Laytone8568732015-08-24 12:41:47 -04001338 bool unhashed;
1339
Jeff Layton2c41beb2014-07-29 21:34:41 -04001340 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1341
Jeff Laytone8568732015-08-24 12:41:47 -04001342 unhashed = unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001343 release_open_stateid_locks(stp, reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04001344 return unhashed;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001345}
1346
1347static void release_open_stateid(struct nfs4_ol_stateid *stp)
1348{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001349 LIST_HEAD(reaplist);
1350
1351 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001352 if (unhash_open_stateid(stp, &reaplist))
1353 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001354 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1355 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001356}
1357
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001358static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001359{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001360 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001361
Trond Myklebustd4f04892014-07-29 21:34:36 -04001362 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001363
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001364 list_del_init(&oo->oo_owner.so_strhash);
1365 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001366}
1367
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001368static void release_last_closed_stateid(struct nfs4_openowner *oo)
1369{
Jeff Layton217526e2014-07-30 08:27:11 -04001370 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1371 nfsd_net_id);
1372 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001373
Jeff Layton217526e2014-07-30 08:27:11 -04001374 spin_lock(&nn->client_lock);
1375 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001376 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001377 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001378 oo->oo_last_closed_stid = NULL;
1379 }
Jeff Layton217526e2014-07-30 08:27:11 -04001380 spin_unlock(&nn->client_lock);
1381 if (s)
1382 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001383}
1384
Jeff Layton2c41beb2014-07-29 21:34:41 -04001385static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001386{
1387 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001388 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001389 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001390
Jeff Layton2c41beb2014-07-29 21:34:41 -04001391 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001392
Trond Myklebustd4f04892014-07-29 21:34:36 -04001393 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001394 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001395 while (!list_empty(&oo->oo_owner.so_stateids)) {
1396 stp = list_first_entry(&oo->oo_owner.so_stateids,
1397 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001398 if (unhash_open_stateid(stp, &reaplist))
1399 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001400 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001401 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001402 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001403 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001404 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001405}
1406
Marc Eshel5282fd72009-04-03 08:27:52 +03001407static inline int
1408hash_sessionid(struct nfs4_sessionid *sessionid)
1409{
1410 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1411
1412 return sid->sequence % SESSION_HASH_SIZE;
1413}
1414
Mark Salter135dd002015-04-06 09:46:00 -04001415#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001416static inline void
1417dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1418{
1419 u32 *ptr = (u32 *)(&sessionid->data[0]);
1420 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1421}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001422#else
1423static inline void
1424dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1425{
1426}
1427#endif
1428
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001429/*
1430 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1431 * won't be used for replay.
1432 */
1433void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1434{
1435 struct nfs4_stateowner *so = cstate->replay_owner;
1436
1437 if (nfserr == nfserr_replay_me)
1438 return;
1439
1440 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001441 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001442 return;
1443 }
1444 if (!so)
1445 return;
1446 if (so->so_is_open_owner)
1447 release_last_closed_stateid(openowner(so));
1448 so->so_seqid++;
1449 return;
1450}
Marc Eshel5282fd72009-04-03 08:27:52 +03001451
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001452static void
1453gen_sessionid(struct nfsd4_session *ses)
1454{
1455 struct nfs4_client *clp = ses->se_client;
1456 struct nfsd4_sessionid *sid;
1457
1458 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1459 sid->clientid = clp->cl_clientid;
1460 sid->sequence = current_sessionid++;
1461 sid->reserved = 0;
1462}
1463
1464/*
Andy Adamsona649637c72009-08-28 08:45:01 -04001465 * The protocol defines ca_maxresponssize_cached to include the size of
1466 * the rpc header, but all we need to cache is the data starting after
1467 * the end of the initial SEQUENCE operation--the rest we regenerate
1468 * each time. Therefore we can advertise a ca_maxresponssize_cached
1469 * value that is the number of bytes in our cache plus a few additional
1470 * bytes. In order to stay on the safe side, and not promise more than
1471 * we can cache, those additional bytes must be the minimum possible: 24
1472 * bytes of rpc header (xid through accept state, with AUTH_NULL
1473 * verifier), 12 for the compound header (with zero-length tag), and 44
1474 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001475 */
Andy Adamsona649637c72009-08-28 08:45:01 -04001476#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1477
Andy Adamson557ce262009-08-28 08:45:04 -04001478static void
1479free_session_slots(struct nfsd4_session *ses)
1480{
1481 int i;
1482
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001483 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1484 free_svc_cred(&ses->se_slots[i]->sl_cred);
Andy Adamson557ce262009-08-28 08:45:04 -04001485 kfree(ses->se_slots[i]);
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001486 }
Andy Adamson557ce262009-08-28 08:45:04 -04001487}
1488
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001489/*
1490 * We don't actually need to cache the rpc and session headers, so we
1491 * can allocate a little less for each slot:
1492 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001493static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001494{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001495 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001496
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001497 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1498 size = 0;
1499 else
1500 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1501 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001502}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001503
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001504/*
1505 * XXX: If we run out of reserved DRC memory we could (up to a point)
1506 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001507 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001508 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001509static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001510{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001511 u32 slotsize = slot_bytes(ca);
1512 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001513 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001514
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001515 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001516 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1517 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001518 /*
1519 * Never use more than a third of the remaining memory,
1520 * unless it's the only way to give this client a slot:
1521 */
1522 avail = clamp_t(int, avail, slotsize, avail/3);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001523 num = min_t(int, num, avail / slotsize);
1524 nfsd_drc_mem_used += num * slotsize;
1525 spin_unlock(&nfsd_drc_lock);
1526
1527 return num;
1528}
1529
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001530static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001531{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001532 int slotsize = slot_bytes(ca);
1533
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001534 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001535 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001536 spin_unlock(&nfsd_drc_lock);
1537}
1538
Kinglong Mee60810e52014-01-01 00:35:47 +08001539static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1540 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001541{
Kinglong Mee60810e52014-01-01 00:35:47 +08001542 int numslots = fattrs->maxreqs;
1543 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001544 struct nfsd4_session *new;
1545 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001546
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001547 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001548 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1549 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001550
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001551 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001552 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001553 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001554 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001555 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001556 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001557 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001558 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001559 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001560
1561 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1562 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1563
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001564 return new;
1565out_free:
1566 while (i--)
1567 kfree(new->se_slots[i]);
1568 kfree(new);
1569 return NULL;
1570}
1571
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001572static void free_conn(struct nfsd4_conn *c)
1573{
1574 svc_xprt_put(c->cn_xprt);
1575 kfree(c);
1576}
1577
1578static void nfsd4_conn_lost(struct svc_xpt_user *u)
1579{
1580 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1581 struct nfs4_client *clp = c->cn_session->se_client;
1582
1583 spin_lock(&clp->cl_lock);
1584 if (!list_empty(&c->cn_persession)) {
1585 list_del(&c->cn_persession);
1586 free_conn(c);
1587 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001588 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001589 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001590}
1591
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001592static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001593{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001594 struct nfsd4_conn *conn;
1595
1596 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1597 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001598 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001599 svc_xprt_get(rqstp->rq_xprt);
1600 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001601 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001602 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1603 return conn;
1604}
1605
J. Bruce Fields328ead22010-09-29 16:11:06 -04001606static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1607{
1608 conn->cn_session = ses;
1609 list_add(&conn->cn_persession, &ses->se_conns);
1610}
1611
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001612static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1613{
1614 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001615
1616 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001617 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001618 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001619}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001620
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001621static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001622{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001623 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001624 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001625}
1626
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001627static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001628{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001629 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001630
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001631 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001632 ret = nfsd4_register_conn(conn);
1633 if (ret)
1634 /* oops; xprt is already down: */
1635 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001636 /* We may have gained or lost a callback channel: */
1637 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001638}
1639
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001640static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001641{
1642 u32 dir = NFS4_CDFC4_FORE;
1643
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001644 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001645 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001646 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001647}
1648
1649/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001650static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001651{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001652 struct nfs4_client *clp = s->se_client;
1653 struct nfsd4_conn *c;
1654
1655 spin_lock(&clp->cl_lock);
1656 while (!list_empty(&s->se_conns)) {
1657 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1658 list_del_init(&c->cn_persession);
1659 spin_unlock(&clp->cl_lock);
1660
1661 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1662 free_conn(c);
1663
1664 spin_lock(&clp->cl_lock);
1665 }
1666 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001667}
1668
J. Bruce Fields1377b692012-09-11 21:42:40 -04001669static void __free_session(struct nfsd4_session *ses)
1670{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001671 free_session_slots(ses);
1672 kfree(ses);
1673}
1674
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001675static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001676{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001677 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001678 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001679 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001680}
Andy Adamson557ce262009-08-28 08:45:04 -04001681
Fengguang Wu135ae822012-11-10 07:20:25 -05001682static 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 -04001683{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001684 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001685 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001686
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001687 new->se_client = clp;
1688 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001689
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001690 INIT_LIST_HEAD(&new->se_conns);
1691
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001692 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001693 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001694 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001695 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001696 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001697 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001698 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001699 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001700 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001701 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001702
Chuck Leverb0d2e422014-08-22 15:10:59 -04001703 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001704 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001705 /*
1706 * This is a little silly; with sessions there's no real
1707 * use for the callback address. Use the peer address
1708 * as a reasonable default for now, but consider fixing
1709 * the rpc client not to require an address in the
1710 * future:
1711 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001712 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1713 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001714 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001715}
1716
Benny Halevy9089f1b2010-05-12 00:12:26 +03001717/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001718static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001719__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001720{
1721 struct nfsd4_session *elem;
1722 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001723 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001724
Trond Myklebust0a880a22014-07-30 08:27:10 -04001725 lockdep_assert_held(&nn->client_lock);
1726
Marc Eshel5282fd72009-04-03 08:27:52 +03001727 dump_sessionid(__func__, sessionid);
1728 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001729 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001730 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001731 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1732 NFS4_MAX_SESSIONID_LEN)) {
1733 return elem;
1734 }
1735 }
1736
1737 dprintk("%s: session not found\n", __func__);
1738 return NULL;
1739}
1740
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001741static struct nfsd4_session *
1742find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1743 __be32 *ret)
1744{
1745 struct nfsd4_session *session;
1746 __be32 status = nfserr_badsession;
1747
1748 session = __find_in_sessionid_hashtbl(sessionid, net);
1749 if (!session)
1750 goto out;
1751 status = nfsd4_get_session_locked(session);
1752 if (status)
1753 session = NULL;
1754out:
1755 *ret = status;
1756 return session;
1757}
1758
Benny Halevy9089f1b2010-05-12 00:12:26 +03001759/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001760static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001761unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001762{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001763 struct nfs4_client *clp = ses->se_client;
1764 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1765
1766 lockdep_assert_held(&nn->client_lock);
1767
Andy Adamson7116ed62009-04-03 08:27:43 +03001768 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001769 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001770 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001771 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001772}
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1775static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001776STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001778 /*
1779 * We're assuming the clid was not given out from a boot
1780 * precisely 2^32 (about 136 years) before this one. That seems
1781 * a safe assumption:
1782 */
1783 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001785 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001786 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 return 1;
1788}
1789
1790/*
1791 * XXX Should we use a slab cache ?
1792 * This type of memory management is somewhat inefficient, but we use it
1793 * anyway since SETCLIENTID is not a common operation.
1794 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001795static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
1797 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001798 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Jeff Layton9258a2d2018-03-16 09:47:22 -04001800 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001801 if (clp == NULL)
1802 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001803 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001804 if (clp->cl_name.data == NULL)
1805 goto err_no_name;
1806 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1807 OWNER_HASH_SIZE, GFP_KERNEL);
1808 if (!clp->cl_ownerstr_hashtbl)
1809 goto err_no_hashtbl;
1810 for (i = 0; i < OWNER_HASH_SIZE; i++)
1811 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001812 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001813 INIT_LIST_HEAD(&clp->cl_sessions);
1814 idr_init(&clp->cl_stateids);
1815 atomic_set(&clp->cl_refcount, 0);
1816 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1817 INIT_LIST_HEAD(&clp->cl_idhash);
1818 INIT_LIST_HEAD(&clp->cl_openowners);
1819 INIT_LIST_HEAD(&clp->cl_delegations);
1820 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04001821 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001822#ifdef CONFIG_NFSD_PNFS
1823 INIT_LIST_HEAD(&clp->cl_lo_states);
1824#endif
Trond Myklebust5694c932014-04-18 14:43:56 -04001825 spin_lock_init(&clp->cl_lock);
1826 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001828err_no_hashtbl:
1829 kfree(clp->cl_name.data);
1830err_no_name:
Jeff Layton9258a2d2018-03-16 09:47:22 -04001831 kmem_cache_free(client_slab, clp);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001832 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001835static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836free_client(struct nfs4_client *clp)
1837{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001838 while (!list_empty(&clp->cl_sessions)) {
1839 struct nfsd4_session *ses;
1840 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1841 se_perclnt);
1842 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001843 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1844 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001845 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001846 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001847 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001848 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001850 idr_destroy(&clp->cl_stateids);
Jeff Layton9258a2d2018-03-16 09:47:22 -04001851 kmem_cache_free(client_slab, clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852}
1853
Benny Halevy84d38ac2010-05-12 00:13:16 +03001854/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001855static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001856unhash_client_locked(struct nfs4_client *clp)
1857{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001858 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001859 struct nfsd4_session *ses;
1860
Trond Myklebust0a880a22014-07-30 08:27:10 -04001861 lockdep_assert_held(&nn->client_lock);
1862
Trond Myklebust4beb3452014-07-30 08:27:02 -04001863 /* Mark the client as expired! */
1864 clp->cl_time = 0;
1865 /* Make it invisible */
1866 if (!list_empty(&clp->cl_idhash)) {
1867 list_del_init(&clp->cl_idhash);
1868 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1869 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1870 else
1871 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1872 }
1873 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001874 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001875 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1876 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001877 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001878}
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001881unhash_client(struct nfs4_client *clp)
1882{
1883 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1884
1885 spin_lock(&nn->client_lock);
1886 unhash_client_locked(clp);
1887 spin_unlock(&nn->client_lock);
1888}
1889
Jeff Layton97403d92014-07-30 08:27:12 -04001890static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1891{
1892 if (atomic_read(&clp->cl_refcount))
1893 return nfserr_jukebox;
1894 unhash_client_locked(clp);
1895 return nfs_ok;
1896}
1897
Trond Myklebust4beb3452014-07-30 08:27:02 -04001898static void
1899__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
Jeff Layton68ef3bc2018-03-16 11:32:02 -04001901 int i;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001902 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 struct list_head reaplist;
1905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001907 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001908 while (!list_empty(&clp->cl_delegations)) {
1909 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001910 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04001911 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
Benny Halevycdc97502014-05-30 09:09:30 -04001913 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 while (!list_empty(&reaplist)) {
1915 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001916 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001917 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001919 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05001920 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001921 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001922 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001923 }
NeilBrownea1da632005-06-23 22:04:17 -07001924 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001925 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04001926 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001927 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 }
Jeff Layton68ef3bc2018-03-16 11:32:02 -04001929 for (i = 0; i < OWNER_HASH_SIZE; i++) {
1930 struct nfs4_stateowner *so, *tmp;
1931
1932 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
1933 so_strhash) {
1934 /* Should be no openowners at this point */
1935 WARN_ON_ONCE(so->so_is_open_owner);
1936 remove_blocked_locks(lockowner(so));
1937 }
1938 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001939 nfsd4_return_all_client_layouts(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001940 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001941 if (clp->cl_cb_conn.cb_xprt)
1942 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001943 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944}
1945
Trond Myklebust4beb3452014-07-30 08:27:02 -04001946static void
1947destroy_client(struct nfs4_client *clp)
1948{
1949 unhash_client(clp);
1950 __destroy_client(clp);
1951}
1952
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001953static void expire_client(struct nfs4_client *clp)
1954{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001955 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001956 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001957 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001958}
1959
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001960static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1961{
1962 memcpy(target->cl_verifier.data, source->data,
1963 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
1965
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001966static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1967{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1969 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1970}
1971
J. Bruce Fields50043852015-11-20 15:58:37 -05001972static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1973{
NeilBrown2f10fdc2017-03-23 16:57:36 +08001974 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
1975 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
1976 GFP_KERNEL);
1977 if ((source->cr_principal && ! target->cr_principal) ||
1978 (source->cr_raw_principal && ! target->cr_raw_principal))
1979 return -ENOMEM;
J. Bruce Fields50043852015-11-20 15:58:37 -05001980
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001981 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 target->cr_uid = source->cr_uid;
1983 target->cr_gid = source->cr_gid;
1984 target->cr_group_info = source->cr_group_info;
1985 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001986 target->cr_gss_mech = source->cr_gss_mech;
1987 if (source->cr_gss_mech)
1988 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001989 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990}
1991
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001992static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001993compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1994{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001995 if (o1->len < o2->len)
1996 return -1;
1997 if (o1->len > o2->len)
1998 return 1;
1999 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002000}
2001
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002002static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002003{
NeilBrowna55370a2005-06-23 22:03:52 -07002004 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005}
2006
2007static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002008same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2009{
2010 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
2012
2013static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002014same_clid(clientid_t *cl1, clientid_t *cl2)
2015{
2016 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002019static bool groups_equal(struct group_info *g1, struct group_info *g2)
2020{
2021 int i;
2022
2023 if (g1->ngroups != g2->ngroups)
2024 return false;
2025 for (i=0; i<g1->ngroups; i++)
Alexey Dobriyan81243ea2016-10-07 17:03:12 -07002026 if (!gid_eq(g1->gid[i], g2->gid[i]))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002027 return false;
2028 return true;
2029}
2030
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002031/*
2032 * RFC 3530 language requires clid_inuse be returned when the
2033 * "principal" associated with a requests differs from that previously
2034 * used. We use uid, gid's, and gss principal string as our best
2035 * approximation. We also don't want to allow non-gss use of a client
2036 * established using gss: in theory cr_principal should catch that
2037 * change, but in practice cr_principal can be null even in the gss case
2038 * since gssd doesn't always pass down a principal string.
2039 */
2040static bool is_gss_cred(struct svc_cred *cr)
2041{
2042 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2043 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2044}
2045
2046
Vivek Trivedi5559b502012-07-24 21:18:20 +05302047static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002048same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2049{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002050 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002051 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2052 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002053 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2054 return false;
2055 if (cr1->cr_principal == cr2->cr_principal)
2056 return true;
2057 if (!cr1->cr_principal || !cr2->cr_principal)
2058 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05302059 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060}
2061
J. Bruce Fields57266a62013-04-13 14:27:29 -04002062static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2063{
2064 struct svc_cred *cr = &rqstp->rq_cred;
2065 u32 service;
2066
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04002067 if (!cr->cr_gss_mech)
2068 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002069 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2070 return service == RPC_GSS_SVC_INTEGRITY ||
2071 service == RPC_GSS_SVC_PRIVACY;
2072}
2073
Andrew Elblededeb132016-06-15 12:52:08 -04002074bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
J. Bruce Fields57266a62013-04-13 14:27:29 -04002075{
2076 struct svc_cred *cr = &rqstp->rq_cred;
2077
2078 if (!cl->cl_mach_cred)
2079 return true;
2080 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2081 return false;
2082 if (!svc_rqst_integrity_protected(rqstp))
2083 return false;
J. Bruce Fields414ca012015-11-20 10:48:02 -05002084 if (cl->cl_cred.cr_raw_principal)
2085 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2086 cr->cr_raw_principal);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002087 if (!cr->cr_principal)
2088 return false;
2089 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2090}
2091
Jeff Layton294ac322014-07-30 08:27:15 -04002092static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05002093{
Chuck Leverab4684d2012-03-02 17:13:50 -05002094 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Jeff Laytonf4199922014-06-17 07:44:11 -04002096 /*
2097 * This is opaque to client, so no need to byte-swap. Use
2098 * __force to keep sparse happy
2099 */
2100 verf[0] = (__force __be32)get_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08002101 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05002102 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103}
2104
Jeff Layton294ac322014-07-30 08:27:15 -04002105static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2106{
2107 clp->cl_clientid.cl_boot = nn->boot_time;
2108 clp->cl_clientid.cl_id = nn->clientid_counter++;
2109 gen_confirm(clp, nn);
2110}
2111
Jeff Layton4770d722014-07-29 21:34:10 -04002112static struct nfs4_stid *
2113find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04002114{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002115 struct nfs4_stid *ret;
2116
2117 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2118 if (!ret || !ret->sc_type)
2119 return NULL;
2120 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04002121}
2122
Jeff Layton4770d722014-07-29 21:34:10 -04002123static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04002124find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002125{
2126 struct nfs4_stid *s;
2127
Jeff Layton4770d722014-07-29 21:34:10 -04002128 spin_lock(&cl->cl_lock);
2129 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002130 if (s != NULL) {
2131 if (typemask & s->sc_type)
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03002132 refcount_inc(&s->sc_count);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002133 else
2134 s = NULL;
2135 }
Jeff Layton4770d722014-07-29 21:34:10 -04002136 spin_unlock(&cl->cl_lock);
2137 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04002138}
2139
Jeff Layton2216d442012-11-12 15:00:57 -05002140static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002141 struct svc_rqst *rqstp, nfs4_verifier *verf)
2142{
2143 struct nfs4_client *clp;
2144 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002145 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002146 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002147
2148 clp = alloc_client(name);
2149 if (clp == NULL)
2150 return NULL;
2151
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002152 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2153 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002154 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002155 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002156 }
Christoph Hellwig0162ac22014-09-24 12:19:19 +02002157 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Benny Halevy07cd4902010-05-12 00:13:41 +03002158 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002159 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002160 copy_verf(clp, verf);
2161 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04002162 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002163 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002164 return clp;
2165}
2166
NeilBrownfd39ca92005-06-23 22:04:03 -07002167static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002168add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2169{
2170 struct rb_node **new = &(root->rb_node), *parent = NULL;
2171 struct nfs4_client *clp;
2172
2173 while (*new) {
2174 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2175 parent = *new;
2176
2177 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2178 new = &((*new)->rb_left);
2179 else
2180 new = &((*new)->rb_right);
2181 }
2182
2183 rb_link_node(&new_clp->cl_namenode, parent, new);
2184 rb_insert_color(&new_clp->cl_namenode, root);
2185}
2186
2187static struct nfs4_client *
2188find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2189{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002190 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002191 struct rb_node *node = root->rb_node;
2192 struct nfs4_client *clp;
2193
2194 while (node) {
2195 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2196 cmp = compare_blob(&clp->cl_name, name);
2197 if (cmp > 0)
2198 node = node->rb_left;
2199 else if (cmp < 0)
2200 node = node->rb_right;
2201 else
2202 return clp;
2203 }
2204 return NULL;
2205}
2206
2207static void
2208add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
2210 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002211 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Trond Myklebust0a880a22014-07-30 08:27:10 -04002213 lockdep_assert_held(&nn->client_lock);
2214
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002215 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002216 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002218 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002219 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220}
2221
NeilBrownfd39ca92005-06-23 22:04:03 -07002222static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223move_to_confirmed(struct nfs4_client *clp)
2224{
2225 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002226 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Trond Myklebust0a880a22014-07-30 08:27:10 -04002228 lockdep_assert_held(&nn->client_lock);
2229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002231 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002232 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002233 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002234 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002235 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236}
2237
2238static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002239find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240{
2241 struct nfs4_client *clp;
2242 unsigned int idhashval = clientid_hashval(clid->cl_id);
2243
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002244 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002245 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002246 if ((bool)clp->cl_minorversion != sessions)
2247 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002248 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 }
2252 return NULL;
2253}
2254
2255static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002256find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2257{
2258 struct list_head *tbl = nn->conf_id_hashtbl;
2259
Trond Myklebust0a880a22014-07-30 08:27:10 -04002260 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002261 return find_client_in_id_table(tbl, clid, sessions);
2262}
2263
2264static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002265find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002267 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Trond Myklebust0a880a22014-07-30 08:27:10 -04002269 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002270 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271}
2272
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002273static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002274{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002275 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002276}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002277
NeilBrown28ce6052005-06-23 22:03:56 -07002278static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002279find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002280{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002281 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002282 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002283}
2284
2285static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002286find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002287{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002288 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002289 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002290}
2291
NeilBrownfd39ca92005-06-23 22:04:03 -07002292static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002293gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002295 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002296 struct sockaddr *sa = svc_addr(rqstp);
2297 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002298 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
Jeff Layton7077ecb2009-08-14 12:57:58 -04002300 /* Currently, we only support tcp and tcp6 for the callback channel */
2301 if (se->se_callback_netid_len == 3 &&
2302 !memcmp(se->se_callback_netid_val, "tcp", 3))
2303 expected_family = AF_INET;
2304 else if (se->se_callback_netid_len == 4 &&
2305 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2306 expected_family = AF_INET6;
2307 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 goto out_err;
2309
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002310 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002311 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002312 (struct sockaddr *)&conn->cb_addr,
2313 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002314
J. Bruce Fields07263f12010-05-31 19:09:40 -04002315 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002317
J. Bruce Fields07263f12010-05-31 19:09:40 -04002318 if (conn->cb_addr.ss_family == AF_INET6)
2319 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002320
J. Bruce Fields07263f12010-05-31 19:09:40 -04002321 conn->cb_prog = se->se_callback_prog;
2322 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002323 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 return;
2325out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002326 conn->cb_addr.ss_family = AF_UNSPEC;
2327 conn->cb_addrlen = 0;
Rasmus Villemoes4ab495b2017-02-24 01:15:55 +01002328 dprintk("NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 "will not receive delegations\n",
2330 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2331
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return;
2333}
2334
Andy Adamson074fe892009-04-03 08:28:15 +03002335/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002336 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002337 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002338static void
Andy Adamson074fe892009-04-03 08:28:15 +03002339nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2340{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002341 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002342 struct nfsd4_slot *slot = resp->cstate.slot;
2343 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002344
Andy Adamson557ce262009-08-28 08:45:04 -04002345 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002346
J. Bruce Fields085def32017-10-18 16:17:18 -04002347 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamson557ce262009-08-28 08:45:04 -04002348 slot->sl_opcnt = resp->opcnt;
2349 slot->sl_status = resp->cstate.status;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04002350 free_svc_cred(&slot->sl_cred);
2351 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
Andy Adamsonbf864a32009-04-03 08:28:35 +03002352
J. Bruce Fields085def32017-10-18 16:17:18 -04002353 if (!nfsd4_cache_this(resp)) {
2354 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002355 return;
2356 }
J. Bruce Fields085def32017-10-18 16:17:18 -04002357 slot->sl_flags |= NFSD4_SLOT_CACHED;
2358
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002359 base = resp->cstate.data_offset;
2360 slot->sl_datalen = buf->len - base;
2361 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Dan Carpenterd3f03402015-11-21 13:28:50 +03002362 WARN(1, "%s: sessions DRC could not cache compound\n",
2363 __func__);
Andy Adamson557ce262009-08-28 08:45:04 -04002364 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002365}
2366
2367/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002368 * Encode the replay sequence operation from the slot values.
2369 * If cachethis is FALSE encode the uncached rep error on the next
2370 * operation which sets resp->p and increments resp->opcnt for
2371 * nfs4svc_encode_compoundres.
2372 *
Andy Adamson074fe892009-04-03 08:28:15 +03002373 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002374static __be32
2375nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2376 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002377{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002378 struct nfsd4_op *op;
2379 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002380
Andy Adamsonabfabf82009-07-23 19:02:18 -04002381 /* Encode the replayed sequence operation */
2382 op = &args->ops[resp->opcnt - 1];
2383 nfsd4_encode_operation(resp, op);
2384
J. Bruce Fields085def32017-10-18 16:17:18 -04002385 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2386 return op->status;
2387 if (args->opcnt == 1) {
2388 /*
2389 * The original operation wasn't a solo sequence--we
2390 * always cache those--so this retry must not match the
2391 * original:
2392 */
2393 op->status = nfserr_seq_false_retry;
2394 } else {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002395 op = &args->ops[resp->opcnt++];
2396 op->status = nfserr_retry_uncached_rep;
2397 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002398 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002399 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002400}
2401
2402/*
Andy Adamson557ce262009-08-28 08:45:04 -04002403 * The sequence operation is not cached because we can use the slot and
2404 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002405 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002406static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002407nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2408 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002409{
Andy Adamson557ce262009-08-28 08:45:04 -04002410 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002411 struct xdr_stream *xdr = &resp->xdr;
2412 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002413 __be32 status;
2414
Andy Adamson557ce262009-08-28 08:45:04 -04002415 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002416
Andy Adamsonabfabf82009-07-23 19:02:18 -04002417 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002418 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002419 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002420
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002421 p = xdr_reserve_space(xdr, slot->sl_datalen);
2422 if (!p) {
2423 WARN_ON_ONCE(1);
2424 return nfserr_serverfault;
2425 }
2426 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2427 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002428
Andy Adamson557ce262009-08-28 08:45:04 -04002429 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002430 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002431}
2432
Andy Adamson0733d212009-04-03 08:28:01 +03002433/*
2434 * Set the exchange_id flags returned by the server.
2435 */
2436static void
2437nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2438{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002439#ifdef CONFIG_NFSD_PNFS
2440 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2441#else
Andy Adamson0733d212009-04-03 08:28:01 +03002442 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002443#endif
Andy Adamson0733d212009-04-03 08:28:01 +03002444
2445 /* Referrals are supported, Migration is not. */
2446 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2447
2448 /* set the wire flags to return to client. */
2449 clid->flags = new->cl_exchange_flags;
2450}
2451
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002452static bool client_has_openowners(struct nfs4_client *clp)
2453{
2454 struct nfs4_openowner *oo;
2455
2456 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2457 if (!list_empty(&oo->oo_owner.so_stateids))
2458 return true;
2459 }
2460 return false;
2461}
2462
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002463static bool client_has_state(struct nfs4_client *clp)
2464{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002465 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08002466#ifdef CONFIG_NFSD_PNFS
2467 || !list_empty(&clp->cl_lo_states)
2468#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002469 || !list_empty(&clp->cl_delegations)
2470 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002471}
2472
Al Virob37ad282006-10-19 23:28:59 -07002473__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02002474nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2475 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03002476{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002477 struct nfsd4_exchange_id *exid = &u->exchange_id;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002478 struct nfs4_client *conf, *new;
2479 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002480 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002481 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002482 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002483 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002484 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002485 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002486
Jeff Layton363168b2009-08-14 12:57:56 -04002487 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002488 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002489 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002490 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002491 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002492
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002493 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002494 return nfserr_inval;
2495
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002496 new = create_client(exid->clname, rqstp, &verf);
2497 if (new == NULL)
2498 return nfserr_jukebox;
2499
Andy Adamson0733d212009-04-03 08:28:01 +03002500 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002501 case SP4_MACH_CRED:
Andrew Elbleed941642016-06-15 12:52:09 -04002502 exid->spo_must_enforce[0] = 0;
2503 exid->spo_must_enforce[1] = (
2504 1 << (OP_BIND_CONN_TO_SESSION - 32) |
2505 1 << (OP_EXCHANGE_ID - 32) |
2506 1 << (OP_CREATE_SESSION - 32) |
2507 1 << (OP_DESTROY_SESSION - 32) |
2508 1 << (OP_DESTROY_CLIENTID - 32));
2509
2510 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
2511 1 << (OP_OPEN_DOWNGRADE) |
2512 1 << (OP_LOCKU) |
2513 1 << (OP_DELEGRETURN));
2514
2515 exid->spo_must_allow[1] &= (
2516 1 << (OP_TEST_STATEID - 32) |
2517 1 << (OP_FREE_STATEID - 32));
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002518 if (!svc_rqst_integrity_protected(rqstp)) {
2519 status = nfserr_inval;
2520 goto out_nolock;
2521 }
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002522 /*
2523 * Sometimes userspace doesn't give us a principal.
2524 * Which is a bug, really. Anyway, we can't enforce
2525 * MACH_CRED in that case, better to give up now:
2526 */
J. Bruce Fields414ca012015-11-20 10:48:02 -05002527 if (!new->cl_cred.cr_principal &&
2528 !new->cl_cred.cr_raw_principal) {
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002529 status = nfserr_serverfault;
2530 goto out_nolock;
2531 }
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002532 new->cl_mach_cred = true;
Andy Adamson0733d212009-04-03 08:28:01 +03002533 case SP4_NONE:
2534 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002535 default: /* checked by xdr code */
2536 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002537 case SP4_SSV:
Kinglong Mee8edf4b02016-02-26 22:36:42 +08002538 status = nfserr_encr_alg_unsupp;
2539 goto out_nolock;
Andy Adamson0733d212009-04-03 08:28:01 +03002540 }
2541
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002542 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002543 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002544 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002545 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002546 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2547 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2548
J. Bruce Fields136e6582012-05-12 20:37:23 -04002549 if (update) {
2550 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002551 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002552 goto out;
2553 }
Andrew Elblededeb132016-06-15 12:52:08 -04002554 if (!nfsd4_mach_creds_match(conf, rqstp)) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002555 status = nfserr_wrong_cred;
2556 goto out;
2557 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002558 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002559 status = nfserr_perm;
2560 goto out;
2561 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002562 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002563 status = nfserr_not_same;
2564 goto out;
2565 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002566 /* case 6 */
2567 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002568 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002569 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002570 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002571 if (client_has_state(conf)) {
2572 status = nfserr_clid_inuse;
2573 goto out;
2574 }
Andy Adamson0733d212009-04-03 08:28:01 +03002575 goto out_new;
2576 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002577 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002578 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002579 goto out_copy;
2580 }
2581 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002582 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002583 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002584 }
2585
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002586 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002587 status = nfserr_noent;
2588 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002589 }
2590
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002591 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002592 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002593 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002594
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002595 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002596out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002597 if (conf) {
2598 status = mark_client_expired_locked(conf);
2599 if (status)
2600 goto out;
2601 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002602 new->cl_minorversion = cstate->minorversion;
Andrew Elbleed941642016-06-15 12:52:09 -04002603 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
2604 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
Andy Adamson0733d212009-04-03 08:28:01 +03002605
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002606 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002607 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002608 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002609out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002610 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2611 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002612
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002613 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2614 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002615
2616 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002617 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002618 status = nfs_ok;
2619
2620out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002621 spin_unlock(&nn->client_lock);
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002622out_nolock:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002623 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002624 expire_client(new);
2625 if (unconf)
2626 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002627 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002628}
2629
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002630static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002631check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002632{
Andy Adamson88e588d2009-07-23 19:02:15 -04002633 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2634 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002635
2636 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002637 if (slot_inuse) {
2638 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002639 return nfserr_jukebox;
2640 else
2641 return nfserr_seq_misordered;
2642 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002643 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002644 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002645 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002646 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002647 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002648 return nfserr_seq_misordered;
2649}
2650
Andy Adamson49557cc2009-07-23 19:02:16 -04002651/*
2652 * Cache the create session result into the create session single DRC
2653 * slot cache by saving the xdr structure. sl_seqid has been set.
2654 * Do this for solo or embedded create session operations.
2655 */
2656static void
2657nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002658 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002659{
2660 slot->sl_status = nfserr;
2661 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2662}
2663
2664static __be32
2665nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2666 struct nfsd4_clid_slot *slot)
2667{
2668 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2669 return slot->sl_status;
2670}
2671
Mi Jinlong1b74c252011-07-14 14:50:17 +08002672#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2673 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2674 1 + /* MIN tag is length with zero, only length */ \
2675 3 + /* version, opcount, opcode */ \
2676 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2677 /* seqid, slotID, slotID, cache */ \
2678 4 ) * sizeof(__be32))
2679
2680#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2681 2 + /* verifier: AUTH_NULL, length 0 */\
2682 1 + /* status */ \
2683 1 + /* MIN tag is length with zero, only length */ \
2684 3 + /* opcount, opcode, opstatus*/ \
2685 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2686 /* seqid, slotID, slotID, slotID, status */ \
2687 5 ) * sizeof(__be32))
2688
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002689static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002690{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002691 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2692
J. Bruce Fields373cd402013-04-08 15:42:12 -04002693 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2694 return nfserr_toosmall;
2695 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2696 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002697 ca->headerpadsz = 0;
2698 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2699 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2700 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2701 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2702 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2703 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2704 /*
2705 * Note decreasing slot size below client's request may make it
2706 * difficult for client to function correctly, whereas
2707 * decreasing the number of slots will (just?) affect
2708 * performance. When short on memory we therefore prefer to
2709 * decrease number of slots instead of their size. Clients that
2710 * request larger slots than they need will get poor results:
2711 */
2712 ca->maxreqs = nfsd4_get_drc_mem(ca);
2713 if (!ca->maxreqs)
2714 return nfserr_jukebox;
2715
J. Bruce Fields373cd402013-04-08 15:42:12 -04002716 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002717}
2718
Chuck Lever45006322016-03-01 13:06:02 -05002719/*
2720 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
2721 * These are based on similar macros in linux/sunrpc/msg_prot.h .
2722 */
2723#define RPC_MAX_HEADER_WITH_AUTH_SYS \
2724 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
2725
2726#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
2727 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
2728
Kinglong Mee8a891632013-12-23 18:11:02 +08002729#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05002730 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08002731#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05002732 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
2733 sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08002734
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002735static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2736{
2737 ca->headerpadsz = 0;
2738
Kinglong Mee8a891632013-12-23 18:11:02 +08002739 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002740 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002741 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002742 return nfserr_toosmall;
2743 ca->maxresp_cached = 0;
2744 if (ca->maxops < 2)
2745 return nfserr_toosmall;
2746
2747 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002748}
2749
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002750static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2751{
2752 switch (cbs->flavor) {
2753 case RPC_AUTH_NULL:
2754 case RPC_AUTH_UNIX:
2755 return nfs_ok;
2756 default:
2757 /*
2758 * GSS case: the spec doesn't allow us to return this
2759 * error. But it also doesn't allow us not to support
2760 * GSS.
2761 * I'd rather this fail hard than return some error the
2762 * client might think it can already handle:
2763 */
2764 return nfserr_encr_alg_unsupp;
2765 }
2766}
2767
Andy Adamson069b6ad2009-04-03 08:27:58 +03002768__be32
2769nfsd4_create_session(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02002770 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03002771{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002772 struct nfsd4_create_session *cr_ses = &u->create_session;
Jeff Layton363168b2009-08-14 12:57:56 -04002773 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002774 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002775 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002776 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002777 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002778 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002779 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002780 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002781
Mi Jinlonga62573d2011-03-23 17:57:07 +08002782 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2783 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002784 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2785 if (status)
2786 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002787 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002788 if (status)
2789 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002790 status = check_backchannel_attrs(&cr_ses->back_channel);
2791 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002792 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002793 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002794 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002795 if (!new)
2796 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002797 conn = alloc_conn_from_crses(rqstp, cr_ses);
2798 if (!conn)
2799 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002800
Jeff Laytond20c11d2014-07-30 08:27:07 -04002801 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002802 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002803 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002804 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002805
2806 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002807 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002808 if (!nfsd4_mach_creds_match(conf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002809 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002810 cs_slot = &conf->cl_cs_slot;
2811 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08002812 if (status) {
2813 if (status == nfserr_replay_cache)
2814 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002815 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002816 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002817 } else if (unconf) {
2818 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002819 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002820 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002821 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002822 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002823 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002824 if (!nfsd4_mach_creds_match(unconf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002825 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002826 cs_slot = &unconf->cl_cs_slot;
2827 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002828 if (status) {
2829 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002830 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002831 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002832 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002833 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002834 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002835 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002836 if (status) {
2837 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002838 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002839 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002840 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002841 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002842 conf = unconf;
2843 } else {
2844 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002845 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002846 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002847 status = nfs_ok;
Chuck Lever4ce85c82016-03-01 13:05:27 -05002848 /* Persistent sessions are not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002849 cr_ses->flags &= ~SESSION4_PERSIST;
Chuck Lever4ce85c82016-03-01 13:05:27 -05002850 /* Upshifting from TCP to RDMA is not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002851 cr_ses->flags &= ~SESSION4_RDMA;
2852
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002853 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002854 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002855
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002856 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002857 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002858 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002859 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002860
Jeff Laytond20c11d2014-07-30 08:27:07 -04002861 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002862 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002863 spin_unlock(&nn->client_lock);
2864 /* init connection and backchannel */
2865 nfsd4_init_conn(rqstp, conn, new);
2866 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002867 if (old)
2868 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002869 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002870out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002871 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002872 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002873 if (old)
2874 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002875out_free_session:
2876 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002877out_release_drc_mem:
2878 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002879 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002880}
2881
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002882static __be32 nfsd4_map_bcts_dir(u32 *dir)
2883{
2884 switch (*dir) {
2885 case NFS4_CDFC4_FORE:
2886 case NFS4_CDFC4_BACK:
2887 return nfs_ok;
2888 case NFS4_CDFC4_FORE_OR_BOTH:
2889 case NFS4_CDFC4_BACK_OR_BOTH:
2890 *dir = NFS4_CDFC4_BOTH;
2891 return nfs_ok;
2892 };
2893 return nfserr_inval;
2894}
2895
Christoph Hellwigeb698532017-05-08 20:58:35 +02002896__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
2897 struct nfsd4_compound_state *cstate,
2898 union nfsd4_op_u *u)
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002899{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002900 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002901 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002902 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002903 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002904
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002905 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2906 if (status)
2907 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002908 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002909 session->se_cb_prog = bc->bc_cb_program;
2910 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002911 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002912
2913 nfsd4_probe_callback(session->se_client);
2914
2915 return nfs_ok;
2916}
2917
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002918__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2919 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02002920 union nfsd4_op_u *u)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002921{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002922 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002923 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002924 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002925 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002926 struct net *net = SVC_NET(rqstp);
2927 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002928
2929 if (!nfsd4_last_compound_op(rqstp))
2930 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002931 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002932 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002933 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002934 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002935 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002936 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002937 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002938 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002939 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002940 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002941 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002942 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002943 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002944 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002945 goto out;
2946 nfsd4_init_conn(rqstp, conn, session);
2947 status = nfs_ok;
2948out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002949 nfsd4_put_session(session);
2950out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002951 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002952}
2953
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002954static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2955{
2956 if (!session)
2957 return 0;
2958 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2959}
2960
Andy Adamson069b6ad2009-04-03 08:27:58 +03002961__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02002962nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
2963 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03002964{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002965 struct nfsd4_destroy_session *sessionid = &u->destroy_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002966 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002967 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002968 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002969 struct net *net = SVC_NET(r);
2970 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002971
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002972 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002973 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002974 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002975 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002976 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002977 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002978 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002979 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002980 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002981 if (!ses)
2982 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002983 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002984 if (!nfsd4_mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002985 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002986 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002987 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002988 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002989 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002990 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002991
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002992 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002993
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002994 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002995 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002996out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002997 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002998out_client_lock:
2999 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003000out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03003001 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003002}
3003
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003004static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003005{
3006 struct nfsd4_conn *c;
3007
3008 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003009 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04003010 return c;
3011 }
3012 }
3013 return NULL;
3014}
3015
J. Bruce Fields57266a62013-04-13 14:27:29 -04003016static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003017{
3018 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003019 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003020 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003021 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003022
3023 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003024 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003025 if (c)
3026 goto out_free;
3027 status = nfserr_conn_not_bound_to_session;
3028 if (clp->cl_mach_cred)
3029 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003030 __nfsd4_hash_conn(new, ses);
3031 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003032 ret = nfsd4_register_conn(new);
3033 if (ret)
3034 /* oops; xprt is already down: */
3035 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003036 return nfs_ok;
3037out_free:
3038 spin_unlock(&clp->cl_lock);
3039 free_conn(new);
3040 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003041}
3042
Mi Jinlong868b89c2011-04-27 09:09:58 +08003043static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3044{
3045 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3046
3047 return args->opcnt > session->se_fchannel.maxops;
3048}
3049
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003050static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3051 struct nfsd4_session *session)
3052{
3053 struct xdr_buf *xb = &rqstp->rq_arg;
3054
3055 return xb->len > session->se_fchannel.maxreq_sz;
3056}
3057
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003058static bool replay_matches_cache(struct svc_rqst *rqstp,
3059 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3060{
3061 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3062
3063 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3064 (bool)seq->cachethis)
3065 return false;
3066 /*
3067 * If there's an error than the reply can have fewer ops than
3068 * the call. But if we cached a reply with *more* ops than the
3069 * call you're sending us now, then this new call is clearly not
3070 * really a replay of the old one:
3071 */
3072 if (slot->sl_opcnt < argp->opcnt)
3073 return false;
3074 /* This is the only check explicitly called by spec: */
3075 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3076 return false;
3077 /*
3078 * There may be more comparisons we could actually do, but the
3079 * spec doesn't require us to catch every case where the calls
3080 * don't match (that would require caching the call as well as
3081 * the reply), so we don't bother.
3082 */
3083 return true;
3084}
3085
Andy Adamson069b6ad2009-04-03 08:27:58 +03003086__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003087nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3088 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003089{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003090 struct nfsd4_sequence *seq = &u->sequence;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003091 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003092 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003093 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003094 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003095 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003096 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003097 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003098 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003099 struct net *net = SVC_NET(rqstp);
3100 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003101
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003102 if (resp->opcnt != 1)
3103 return nfserr_sequence_pos;
3104
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003105 /*
3106 * Will be either used or freed by nfsd4_sequence_check_conn
3107 * below.
3108 */
3109 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3110 if (!conn)
3111 return nfserr_jukebox;
3112
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003113 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003114 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003115 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003116 goto out_no_session;
3117 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003118
Mi Jinlong868b89c2011-04-27 09:09:58 +08003119 status = nfserr_too_many_ops;
3120 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003121 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003122
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003123 status = nfserr_req_too_big;
3124 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003125 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003126
Benny Halevyb85d4c02009-04-03 08:28:08 +03003127 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003128 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003129 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003130
Andy Adamson557ce262009-08-28 08:45:04 -04003131 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003132 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3133
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003134 /* We do not negotiate the number of slots yet, so set the
3135 * maxslots to the session maxreqs which is used to encode
3136 * sr_highest_slotid and the sr_target_slot id to maxslots */
3137 seq->maxslots = session->se_fchannel.maxreqs;
3138
J. Bruce Fields73e79482012-02-13 16:39:00 -05003139 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3140 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003141 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003142 status = nfserr_seq_misordered;
3143 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003144 goto out_put_session;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003145 status = nfserr_seq_false_retry;
3146 if (!replay_matches_cache(rqstp, seq, slot))
3147 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003148 cstate->slot = slot;
3149 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003150 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003151 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003152 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003153 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003154 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003155 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003156 }
3157 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003158 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003159
J. Bruce Fields57266a62013-04-13 14:27:29 -04003160 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003161 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003162 if (status)
3163 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003164
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003165 buflen = (seq->cachethis) ?
3166 session->se_fchannel.maxresp_cached :
3167 session->se_fchannel.maxresp_sz;
3168 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3169 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003170 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003171 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003172 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003173
3174 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003175 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003176 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003177 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003178 if (seq->cachethis)
3179 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003180 else
3181 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003182
3183 cstate->slot = slot;
3184 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003185 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003186
Benny Halevyb85d4c02009-04-03 08:28:08 +03003187out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003188 switch (clp->cl_cb_state) {
3189 case NFSD4_CB_DOWN:
3190 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3191 break;
3192 case NFSD4_CB_FAULT:
3193 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3194 break;
3195 default:
3196 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003197 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003198 if (!list_empty(&clp->cl_revoked))
3199 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003200out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08003201 if (conn)
3202 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003203 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003204 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003205out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003206 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003207 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003208}
3209
Trond Myklebustb6076642014-06-30 11:48:35 -04003210void
3211nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3212{
3213 struct nfsd4_compound_state *cs = &resp->cstate;
3214
3215 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04003216 if (cs->status != nfserr_replay_cache) {
3217 nfsd4_store_cache_entry(resp);
3218 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3219 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003220 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04003221 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003222 } else if (cs->clp)
3223 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04003224}
3225
Mi Jinlong345c2842011-10-20 17:51:39 +08003226__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003227nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3228 struct nfsd4_compound_state *cstate,
3229 union nfsd4_op_u *u)
Mi Jinlong345c2842011-10-20 17:51:39 +08003230{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003231 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003232 struct nfs4_client *conf, *unconf;
3233 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003234 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003235 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08003236
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003237 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003238 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003239 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003240 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08003241
3242 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04003243 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08003244 status = nfserr_clientid_busy;
3245 goto out;
3246 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04003247 status = mark_client_expired_locked(conf);
3248 if (status)
3249 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003250 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08003251 } else if (unconf)
3252 clp = unconf;
3253 else {
3254 status = nfserr_stale_clientid;
3255 goto out;
3256 }
Andrew Elblededeb132016-06-15 12:52:08 -04003257 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003258 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003259 status = nfserr_wrong_cred;
3260 goto out;
3261 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003262 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003263out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003264 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003265 if (clp)
3266 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003267 return status;
3268}
3269
Andy Adamson069b6ad2009-04-03 08:27:58 +03003270__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003271nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3272 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003273{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003274 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003275 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003276
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003277 if (rc->rca_one_fs) {
3278 if (!cstate->current_fh.fh_dentry)
3279 return nfserr_nofilehandle;
3280 /*
3281 * We don't take advantage of the rca_one_fs case.
3282 * That's OK, it's optional, we can safely ignore it.
3283 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02003284 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003285 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003286
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003287 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04003288 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3289 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003290 goto out;
3291
3292 status = nfserr_stale_clientid;
3293 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003294 /*
3295 * The following error isn't really legal.
3296 * But we only get here if the client just explicitly
3297 * destroyed the client. Surely it no longer cares what
3298 * error it gets back on an operation for the dead
3299 * client.
3300 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003301 goto out;
3302
3303 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04003304 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003305out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003306 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003307}
3308
3309__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003310nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003311 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003313 struct nfsd4_setclientid *setclid = &u->setclientid;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003314 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003316 struct nfs4_client *conf, *new;
3317 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003318 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003319 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3320
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003321 new = create_client(clname, rqstp, &clverifier);
3322 if (new == NULL)
3323 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003324 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003325 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003326 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04003327 if (conf && client_has_state(conf)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003328 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003330 if (clp_used_exchangeid(conf))
3331 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003332 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04003333 char addr_str[INET6_ADDRSTRLEN];
3334 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3335 sizeof(addr_str));
3336 dprintk("NFSD: setclientid: string in use by client "
3337 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 goto out;
3339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003341 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003342 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003343 unhash_client_locked(unconf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003344 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003345 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003347 gen_confirm(new, nn);
3348 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003349 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003350 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003351 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003352 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3354 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3355 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003356 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 status = nfs_ok;
3358out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003359 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003360 if (new)
3361 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003362 if (unconf)
3363 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 return status;
3365}
3366
3367
Al Virob37ad282006-10-19 23:28:59 -07003368__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003369nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003370 struct nfsd4_compound_state *cstate,
3371 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003373 struct nfsd4_setclientid_confirm *setclientid_confirm =
3374 &u->setclientid_confirm;
NeilBrown21ab45a2005-06-23 22:04:14 -07003375 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003376 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3378 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003379 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003380 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003382 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003384
Jeff Laytond20c11d2014-07-30 08:27:07 -04003385 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003386 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003387 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003388 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003389 * We try hard to give out unique clientid's, so if we get an
3390 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003391 * the client may be buggy; this should never happen.
3392 *
3393 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003394 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003395 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04003396 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3397 goto out;
3398 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3399 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003400 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003401 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003402 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3403 /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 status = nfs_ok;
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003405 } else /* case 4: client hasn't noticed we rebooted yet? */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003406 status = nfserr_stale_clientid;
3407 goto out;
3408 }
3409 status = nfs_ok;
3410 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003411 old = unconf;
3412 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003413 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003414 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003415 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3416 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04003417 status = nfserr_clid_inuse;
3418 if (client_has_state(old)
3419 && !same_creds(&unconf->cl_cred,
3420 &old->cl_cred))
3421 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003422 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003423 if (status) {
3424 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003425 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003426 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003427 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003428 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003429 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003430 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003431 get_client_locked(conf);
3432 spin_unlock(&nn->client_lock);
3433 nfsd4_probe_callback(conf);
3434 spin_lock(&nn->client_lock);
3435 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003437 spin_unlock(&nn->client_lock);
3438 if (old)
3439 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 return status;
3441}
3442
J. Bruce Fields32513b42011-10-13 16:00:16 -04003443static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003445 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3446}
3447
3448/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003449static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3450 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003451{
Trond Myklebust950e0112014-06-30 11:48:31 -04003452 lockdep_assert_held(&state_lock);
3453
Elena Reshetova818a34e2017-10-20 12:53:30 +03003454 refcount_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003455 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003456 INIT_LIST_HEAD(&fp->fi_stateids);
3457 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02003458 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003459 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003460 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003461 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003462 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003463 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3464 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003465#ifdef CONFIG_NFSD_PNFS
3466 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02003467 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003468#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04003469 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470}
3471
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003472void
NeilBrowne60d4392005-06-23 22:03:01 -07003473nfsd4_free_slabs(void)
3474{
Jeff Layton9258a2d2018-03-16 09:47:22 -04003475 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003476 kmem_cache_destroy(openowner_slab);
3477 kmem_cache_destroy(lockowner_slab);
3478 kmem_cache_destroy(file_slab);
3479 kmem_cache_destroy(stateid_slab);
3480 kmem_cache_destroy(deleg_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04003481 kmem_cache_destroy(odstate_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003482}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
Bryan Schumaker72083392011-11-01 15:24:59 -04003484int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485nfsd4_init_slabs(void)
3486{
Jeff Layton9258a2d2018-03-16 09:47:22 -04003487 client_slab = kmem_cache_create("nfsd4_clients",
3488 sizeof(struct nfs4_client), 0, 0, NULL);
3489 if (client_slab == NULL)
3490 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003491 openowner_slab = kmem_cache_create("nfsd4_openowners",
3492 sizeof(struct nfs4_openowner), 0, 0, NULL);
3493 if (openowner_slab == NULL)
Jeff Layton9258a2d2018-03-16 09:47:22 -04003494 goto out_free_client_slab;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003495 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003496 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003497 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003498 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003499 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003500 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003501 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003502 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003503 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003504 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003505 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003506 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003507 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003508 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003509 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003510 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003511 odstate_slab = kmem_cache_create("nfsd4_odstate",
3512 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3513 if (odstate_slab == NULL)
3514 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003516
Sachin Bhamare8287f002015-04-27 14:50:14 +02003517out_free_deleg_slab:
3518 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003519out_free_stateid_slab:
3520 kmem_cache_destroy(stateid_slab);
3521out_free_file_slab:
3522 kmem_cache_destroy(file_slab);
3523out_free_lockowner_slab:
3524 kmem_cache_destroy(lockowner_slab);
3525out_free_openowner_slab:
3526 kmem_cache_destroy(openowner_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04003527out_free_client_slab:
3528 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003529out:
NeilBrowne60d4392005-06-23 22:03:01 -07003530 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3531 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532}
3533
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003534static void init_nfs4_replay(struct nfs4_replay *rp)
3535{
3536 rp->rp_status = nfserr_serverfault;
3537 rp->rp_buflen = 0;
3538 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003539 mutex_init(&rp->rp_mutex);
3540}
3541
3542static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3543 struct nfs4_stateowner *so)
3544{
3545 if (!nfsd4_has_session(cstate)) {
3546 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003547 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04003548 }
3549}
3550
3551void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3552{
3553 struct nfs4_stateowner *so = cstate->replay_owner;
3554
3555 if (so != NULL) {
3556 cstate->replay_owner = NULL;
3557 mutex_unlock(&so->so_replay.rp_mutex);
3558 nfs4_put_stateowner(so);
3559 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003560}
3561
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003562static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563{
3564 struct nfs4_stateowner *sop;
3565
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003566 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003567 if (!sop)
3568 return NULL;
3569
3570 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3571 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003572 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003573 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003575 sop->so_owner.len = owner->len;
3576
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003577 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003578 sop->so_client = clp;
3579 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003580 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003581 return sop;
3582}
3583
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003584static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003585{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003586 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003587
Trond Myklebustd4f04892014-07-29 21:34:36 -04003588 list_add(&oo->oo_owner.so_strhash,
3589 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003590 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591}
3592
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003593static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3594{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003595 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003596}
3597
Jeff Layton6b180f02014-07-29 21:34:26 -04003598static void nfs4_free_openowner(struct nfs4_stateowner *so)
3599{
3600 struct nfs4_openowner *oo = openowner(so);
3601
3602 kmem_cache_free(openowner_slab, oo);
3603}
3604
3605static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003606 .so_unhash = nfs4_unhash_openowner,
3607 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003608};
3609
Andrew Elble7fc05642015-11-05 20:42:43 -05003610static struct nfs4_ol_stateid *
3611nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3612{
3613 struct nfs4_ol_stateid *local, *ret = NULL;
3614 struct nfs4_openowner *oo = open->op_openowner;
3615
3616 lockdep_assert_held(&fp->fi_lock);
3617
3618 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3619 /* ignore lock owners */
3620 if (local->st_stateowner->so_is_open_owner == 0)
3621 continue;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003622 if (local->st_stateowner != &oo->oo_owner)
3623 continue;
3624 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
Andrew Elble7fc05642015-11-05 20:42:43 -05003625 ret = local;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03003626 refcount_inc(&ret->st_stid.sc_count);
Andrew Elble7fc05642015-11-05 20:42:43 -05003627 break;
3628 }
3629 }
3630 return ret;
3631}
3632
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003633static __be32
3634nfsd4_verify_open_stid(struct nfs4_stid *s)
3635{
3636 __be32 ret = nfs_ok;
3637
3638 switch (s->sc_type) {
3639 default:
3640 break;
Trond Myklebust4f176412018-01-12 17:42:30 -05003641 case 0:
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003642 case NFS4_CLOSED_STID:
3643 case NFS4_CLOSED_DELEG_STID:
3644 ret = nfserr_bad_stateid;
3645 break;
3646 case NFS4_REVOKED_DELEG_STID:
3647 ret = nfserr_deleg_revoked;
3648 }
3649 return ret;
3650}
3651
3652/* Lock the stateid st_mutex, and deal with races with CLOSE */
3653static __be32
3654nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
3655{
3656 __be32 ret;
3657
Andrew Elble4f34bd02017-11-08 17:29:51 -05003658 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003659 ret = nfsd4_verify_open_stid(&stp->st_stid);
3660 if (ret != nfs_ok)
3661 mutex_unlock(&stp->st_mutex);
3662 return ret;
3663}
3664
3665static struct nfs4_ol_stateid *
3666nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3667{
3668 struct nfs4_ol_stateid *stp;
3669 for (;;) {
3670 spin_lock(&fp->fi_lock);
3671 stp = nfsd4_find_existing_open(fp, open);
3672 spin_unlock(&fp->fi_lock);
3673 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
3674 break;
3675 nfs4_put_stid(&stp->st_stid);
3676 }
3677 return stp;
3678}
3679
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003680static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003681alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003682 struct nfsd4_compound_state *cstate)
3683{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003684 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003685 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003687 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3688 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003690 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003691 oo->oo_owner.so_is_open_owner = 1;
3692 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003693 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003694 if (nfsd4_has_session(cstate))
3695 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003696 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003697 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003698 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003699 spin_lock(&clp->cl_lock);
3700 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003701 if (ret == NULL) {
3702 hash_openowner(oo, clp, strhashval);
3703 ret = oo;
3704 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08003705 nfs4_free_stateowner(&oo->oo_owner);
3706
Trond Myklebustd4f04892014-07-29 21:34:36 -04003707 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04003708 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709}
3710
Andrew Elble7fc05642015-11-05 20:42:43 -05003711static struct nfs4_ol_stateid *
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003712init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
Andrew Elble7fc05642015-11-05 20:42:43 -05003713{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714
Andrew Elble7fc05642015-11-05 20:42:43 -05003715 struct nfs4_openowner *oo = open->op_openowner;
3716 struct nfs4_ol_stateid *retstp = NULL;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003717 struct nfs4_ol_stateid *stp;
Andrew Elble7fc05642015-11-05 20:42:43 -05003718
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003719 stp = open->op_stp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003720 /* We are moving these outside of the spinlocks to avoid the warnings */
3721 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05003722 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003723
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003724retry:
Andrew Elble7fc05642015-11-05 20:42:43 -05003725 spin_lock(&oo->oo_owner.so_client->cl_lock);
3726 spin_lock(&fp->fi_lock);
3727
3728 retstp = nfsd4_find_existing_open(fp, open);
3729 if (retstp)
3730 goto out_unlock;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003731
3732 open->op_stp = NULL;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03003733 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003734 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003735 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003736 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07003737 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003738 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 stp->st_access_bmap = 0;
3740 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003741 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003742 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003743 list_add(&stp->st_perfile, &fp->fi_stateids);
Andrew Elble7fc05642015-11-05 20:42:43 -05003744
3745out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04003746 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003747 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003748 if (retstp) {
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003749 /* Handle races with CLOSE */
3750 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
3751 nfs4_put_stid(&retstp->st_stid);
3752 goto retry;
3753 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003754 /* To keep mutex tracking happy */
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003755 mutex_unlock(&stp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003756 stp = retstp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003757 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003758 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759}
3760
Jeff Laytond3134b12014-07-29 21:34:32 -04003761/*
3762 * In the 4.0 case we need to keep the owners around a little while to handle
3763 * CLOSE replay. We still do need to release any file access that is held by
3764 * them before returning however.
3765 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003767move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768{
Jeff Layton217526e2014-07-30 08:27:11 -04003769 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003770 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3771 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3772 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003773
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003774 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
Jeff Laytonb401be222014-07-29 21:34:33 -04003776 /*
3777 * We know that we hold one reference via nfsd4_close, and another
3778 * "persistent" reference for the client. If the refcount is higher
3779 * than 2, then there are still calls in progress that are using this
3780 * stateid. We can't put the sc_file reference until they are finished.
3781 * Wait for the refcount to drop to 2. Since it has been unhashed,
3782 * there should be no danger of the refcount going back up again at
3783 * this point.
3784 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03003785 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
Jeff Laytonb401be222014-07-29 21:34:33 -04003786
Jeff Laytond3134b12014-07-29 21:34:32 -04003787 release_all_access(s);
3788 if (s->st_stid.sc_file) {
3789 put_nfs4_file(s->st_stid.sc_file);
3790 s->st_stid.sc_file = NULL;
3791 }
Jeff Layton217526e2014-07-30 08:27:11 -04003792
3793 spin_lock(&nn->client_lock);
3794 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003795 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003796 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003797 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003798 spin_unlock(&nn->client_lock);
3799 if (last)
3800 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801}
3802
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803/* search file_hashtbl[] for file */
3804static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04003805find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 struct nfs4_file *fp;
3808
Jeff Layton5b095e92014-10-23 08:01:02 -04003809 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02003810 if (fh_match(&fp->fi_fhandle, fh)) {
Elena Reshetova818a34e2017-10-20 12:53:30 +03003811 if (refcount_inc_not_zero(&fp->fi_ref))
Jeff Layton5b095e92014-10-23 08:01:02 -04003812 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 }
3815 return NULL;
3816}
3817
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02003818struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003819find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003820{
3821 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003822 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003823
Jeff Layton5b095e92014-10-23 08:01:02 -04003824 rcu_read_lock();
3825 fp = find_file_locked(fh, hashval);
3826 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04003827 return fp;
3828}
3829
3830static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003831find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003832{
3833 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003834 unsigned int hashval = file_hashval(fh);
3835
3836 rcu_read_lock();
3837 fp = find_file_locked(fh, hashval);
3838 rcu_read_unlock();
3839 if (fp)
3840 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04003841
3842 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04003843 fp = find_file_locked(fh, hashval);
3844 if (likely(fp == NULL)) {
3845 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04003846 fp = new;
3847 }
3848 spin_unlock(&state_lock);
3849
3850 return fp;
3851}
3852
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003853/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 * Called to check deny when READ with all zero stateid or
3855 * WRITE with all zero or all one stateid
3856 */
Al Virob37ad282006-10-19 23:28:59 -07003857static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3859{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003861 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862
Trond Myklebustca943212014-07-23 16:17:39 -04003863 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003864 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003865 return ret;
3866 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003867 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003868 if (fp->fi_share_deny & deny_type)
3869 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003870 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003871 put_nfs4_file(fp);
3872 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873}
3874
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003875static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003877 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04003878 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3879 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003880
Trond Myklebust11b91642014-07-29 21:34:08 -04003881 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003882
Jeff Layton02e12152014-07-16 10:31:57 -04003883 /*
3884 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003885 * already holding inode->i_lock.
3886 *
Jeff Laytondff13992014-07-08 14:02:49 -04003887 * If the dl_time != 0, then we know that it has already been
3888 * queued for a lease break. Don't queue it again.
3889 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003890 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003891 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003892 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003893 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003894 }
Jeff Layton02e12152014-07-16 10:31:57 -04003895 spin_unlock(&state_lock);
3896}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003898static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3899 struct rpc_task *task)
3900{
3901 struct nfs4_delegation *dp = cb_to_delegation(cb);
3902
Andrew Elblea4579742015-08-31 12:06:41 -04003903 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3904 return 1;
3905
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003906 switch (task->tk_status) {
3907 case 0:
3908 return 1;
3909 case -EBADHANDLE:
3910 case -NFS4ERR_BAD_STATEID:
3911 /*
3912 * Race: client probably got cb_recall before open reply
3913 * granting delegation.
3914 */
3915 if (dp->dl_retries--) {
3916 rpc_delay(task, 2 * HZ);
3917 return 0;
3918 }
3919 /*FALLTHRU*/
3920 default:
3921 return -1;
3922 }
3923}
3924
3925static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3926{
3927 struct nfs4_delegation *dp = cb_to_delegation(cb);
3928
3929 nfs4_put_stid(&dp->dl_stid);
3930}
3931
Julia Lawallc4cb8972015-11-21 22:57:39 +01003932static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003933 .prepare = nfsd4_cb_recall_prepare,
3934 .done = nfsd4_cb_recall_done,
3935 .release = nfsd4_cb_recall_release,
3936};
3937
Jeff Layton02e12152014-07-16 10:31:57 -04003938static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3939{
3940 /*
3941 * We're assuming the state code never drops its reference
3942 * without first removing the lease. Since we're in this lease
3943 * callback (and since the lease code is serialized by the kernel
3944 * lock) we know the server hasn't removed the lease yet, we know
3945 * it's safe to take a reference.
3946 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03003947 refcount_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02003948 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003949}
3950
Jeff Layton1c8c6012013-06-21 08:58:15 -04003951/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003952static bool
3953nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003954{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003955 bool ret = false;
J. Bruce Fields653e5142018-02-15 22:08:45 -05003956 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
3957 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003958
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003959 if (fp->fi_had_conflict) {
3960 WARN(1, "duplicate break on %p\n", fp);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003961 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003962 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003963 /*
3964 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003965 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003966 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003967 */
3968 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969
Jeff Layton02e12152014-07-16 10:31:57 -04003970 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003971 fp->fi_had_conflict = true;
3972 /*
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003973 * If there are no delegations on the list, then return true
3974 * so that the lease code will go ahead and delete it.
Jeff Layton417c6622014-07-21 09:34:57 -04003975 */
3976 if (list_empty(&fp->fi_delegations))
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003977 ret = true;
Jeff Layton417c6622014-07-21 09:34:57 -04003978 else
3979 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3980 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003981 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003982 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983}
3984
Jeff Laytonc45198e2014-09-01 07:12:07 -04003985static int
Jeff Layton7448cc32015-01-16 15:05:57 -05003986nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3987 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988{
3989 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04003990 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 else
3992 return -EAGAIN;
3993}
3994
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003995static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003996 .lm_break = nfsd_break_deleg_cb,
3997 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998};
3999
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004000static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4001{
4002 if (nfsd4_has_session(cstate))
4003 return nfs_ok;
4004 if (seqid == so->so_seqid - 1)
4005 return nfserr_replay_me;
4006 if (seqid == so->so_seqid)
4007 return nfs_ok;
4008 return nfserr_bad_seqid;
4009}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010
Jeff Layton4b24ca72014-06-30 11:48:44 -04004011static __be32 lookup_clientid(clientid_t *clid,
4012 struct nfsd4_compound_state *cstate,
4013 struct nfsd_net *nn)
4014{
4015 struct nfs4_client *found;
4016
4017 if (cstate->clp) {
4018 found = cstate->clp;
4019 if (!same_clid(&found->cl_clientid, clid))
4020 return nfserr_stale_clientid;
4021 return nfs_ok;
4022 }
4023
4024 if (STALE_CLIENTID(clid, nn))
4025 return nfserr_stale_clientid;
4026
4027 /*
4028 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4029 * cached already then we know this is for is for v4.0 and "sessions"
4030 * will be false.
4031 */
4032 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004033 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004034 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004035 if (!found) {
4036 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004037 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04004038 }
4039 atomic_inc(&found->cl_refcount);
4040 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004041
4042 /* Cache the nfs4_client in cstate! */
4043 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004044 return nfs_ok;
4045}
4046
Al Virob37ad282006-10-19 23:28:59 -07004047__be32
Andy Adamson66689582009-04-03 08:28:45 +03004048nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004049 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 clientid_t *clientid = &open->op_clientid;
4052 struct nfs4_client *clp = NULL;
4053 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004054 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004055 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004057 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004059 /*
4060 * In case we need it later, after we've already created the
4061 * file and don't want to risk a further failure:
4062 */
4063 open->op_file = nfsd4_alloc_file();
4064 if (open->op_file == NULL)
4065 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066
Trond Myklebust2d91e892014-06-30 11:48:46 -04004067 status = lookup_clientid(clientid, cstate, nn);
4068 if (status)
4069 return status;
4070 clp = cstate->clp;
4071
Trond Myklebustd4f04892014-07-29 21:34:36 -04004072 strhashval = ownerstr_hashval(&open->op_owner);
4073 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004074 open->op_openowner = oo;
4075 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004076 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004078 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004079 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004080 release_openowner(oo);
4081 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004082 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004083 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004084 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4085 if (status)
4086 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004087 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004088new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04004089 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004090 if (oo == NULL)
4091 return nfserr_jukebox;
4092 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004093alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04004094 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004095 if (!open->op_stp)
4096 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004097
4098 if (nfsd4_has_session(cstate) &&
4099 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4100 open->op_odstate = alloc_clnt_odstate(clp);
4101 if (!open->op_odstate)
4102 return nfserr_jukebox;
4103 }
4104
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004105 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106}
4107
Al Virob37ad282006-10-19 23:28:59 -07004108static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07004109nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4110{
4111 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4112 return nfserr_openmode;
4113 else
4114 return nfs_ok;
4115}
4116
Daniel Mackc47d8322011-05-16 16:38:14 +02004117static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04004118{
J. Bruce Fields24a01112010-05-18 20:01:35 -04004119 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4120}
4121
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004122static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004123{
4124 struct nfs4_stid *ret;
4125
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004126 ret = find_stateid_by_type(cl, s,
4127 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004128 if (!ret)
4129 return NULL;
4130 return delegstateid(ret);
4131}
4132
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004133static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4134{
4135 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4136 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4137}
4138
Al Virob37ad282006-10-19 23:28:59 -07004139static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04004140nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07004141 struct nfs4_delegation **dp)
4142{
4143 int flags;
Al Virob37ad282006-10-19 23:28:59 -07004144 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004145 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07004146
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004147 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4148 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07004149 goto out;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004150 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4151 nfs4_put_stid(&deleg->dl_stid);
4152 if (cl->cl_minorversion)
4153 status = nfserr_deleg_revoked;
4154 goto out;
4155 }
J. Bruce Fields24a01112010-05-18 20:01:35 -04004156 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004157 status = nfs4_check_delegmode(deleg, flags);
4158 if (status) {
4159 nfs4_put_stid(&deleg->dl_stid);
4160 goto out;
4161 }
4162 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004163out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004164 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004165 return nfs_ok;
4166 if (status)
4167 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004168 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004169 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07004170}
4171
J. Bruce Fields21fb4012010-07-28 12:21:23 -04004172static inline int nfs4_access_to_access(u32 nfs4_access)
4173{
4174 int flags = 0;
4175
4176 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4177 flags |= NFSD_MAY_READ;
4178 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4179 flags |= NFSD_MAY_WRITE;
4180 return flags;
4181}
4182
Al Virob37ad282006-10-19 23:28:59 -07004183static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4185 struct nfsd4_open *open)
4186{
4187 struct iattr iattr = {
4188 .ia_valid = ATTR_SIZE,
4189 .ia_size = 0,
4190 };
4191 if (!open->op_truncate)
4192 return 0;
4193 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08004194 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
4196}
4197
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004198static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004199 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4200 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004201{
Trond Myklebustde186432014-07-10 14:07:26 -04004202 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004203 __be32 status;
4204 int oflag = nfs4_access_to_omode(open->op_share_access);
4205 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004206 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004207
Trond Myklebustde186432014-07-10 14:07:26 -04004208 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004209
4210 /*
4211 * Are we trying to set a deny mode that would conflict with
4212 * current access?
4213 */
4214 status = nfs4_file_check_deny(fp, open->op_share_deny);
4215 if (status != nfs_ok) {
4216 spin_unlock(&fp->fi_lock);
4217 goto out;
4218 }
4219
4220 /* set access to the file */
4221 status = nfs4_file_get_access(fp, open->op_share_access);
4222 if (status != nfs_ok) {
4223 spin_unlock(&fp->fi_lock);
4224 goto out;
4225 }
4226
4227 /* Set access bits in stateid */
4228 old_access_bmap = stp->st_access_bmap;
4229 set_access(open->op_share_access, stp);
4230
4231 /* Set new deny mask */
4232 old_deny_bmap = stp->st_deny_bmap;
4233 set_deny(open->op_share_deny, stp);
4234 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4235
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004236 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04004237 spin_unlock(&fp->fi_lock);
4238 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004239 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004240 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04004241 spin_lock(&fp->fi_lock);
4242 if (!fp->fi_fds[oflag]) {
4243 fp->fi_fds[oflag] = filp;
4244 filp = NULL;
4245 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004246 }
Trond Myklebustde186432014-07-10 14:07:26 -04004247 spin_unlock(&fp->fi_lock);
4248 if (filp)
4249 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004250
4251 status = nfsd4_truncate(rqstp, cur_fh, open);
4252 if (status)
4253 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004254out:
4255 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004256out_put_access:
4257 stp->st_access_bmap = old_access_bmap;
4258 nfs4_file_put_access(fp, open->op_share_access);
4259 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4260 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004261}
4262
Al Virob37ad282006-10-19 23:28:59 -07004263static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004264nfs4_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 -07004265{
Al Virob37ad282006-10-19 23:28:59 -07004266 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02004267 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004269 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004270 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004271
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004272 /* test and set deny mode */
4273 spin_lock(&fp->fi_lock);
4274 status = nfs4_file_check_deny(fp, open->op_share_deny);
4275 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004276 set_deny(open->op_share_deny, stp);
4277 fp->fi_share_deny |=
4278 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4279 }
4280 spin_unlock(&fp->fi_lock);
4281
4282 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004285 status = nfsd4_truncate(rqstp, cur_fh, open);
4286 if (status != nfs_ok)
4287 reset_union_bmap_deny(old_deny_bmap, stp);
4288 return status;
4289}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004291/* Should we give out recallable state?: */
4292static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4293{
4294 if (clp->cl_cb_state == NFSD4_CB_UP)
4295 return true;
4296 /*
4297 * In the sessions case, since we don't have to establish a
4298 * separate connection for callbacks, we assume it's OK
4299 * until we hear otherwise:
4300 */
4301 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4302}
4303
J. Bruce Fields653e5142018-02-15 22:08:45 -05004304static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4305 int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004306{
4307 struct file_lock *fl;
4308
4309 fl = locks_alloc_lock();
4310 if (!fl)
4311 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004312 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04004313 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004314 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4315 fl->fl_end = OFFSET_MAX;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004316 fl->fl_owner = (fl_owner_t)dp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004317 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004318 return fl;
4319}
4320
Andrew Elble34ed9872015-10-15 12:07:28 -04004321/**
4322 * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer
4323 * @dp: a pointer to the nfs4_delegation we're adding.
4324 *
4325 * Return:
4326 * On success: Return code will be 0 on success.
4327 *
4328 * On error: -EAGAIN if there was an existing delegation.
4329 * nonzero if there is an error in other cases.
4330 *
4331 */
4332
J. Bruce Fields99c41512013-05-21 16:21:25 -04004333static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004334{
Trond Myklebust11b91642014-07-29 21:34:08 -04004335 struct nfs4_file *fp = dp->dl_stid.sc_file;
Kinglong Meeefde6b42015-07-13 17:33:24 +08004336 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04004337 struct file *filp;
4338 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004339
J. Bruce Fields653e5142018-02-15 22:08:45 -05004340 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004341 if (!fl)
4342 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04004343 filp = find_readable_file(fp);
4344 if (!filp) {
4345 /* We should always have a readable file here */
4346 WARN_ON_ONCE(1);
Kinglong Meeaf9dbaf2015-07-13 17:30:51 +08004347 locks_free_lock(fl);
Jeff Layton417c6622014-07-21 09:34:57 -04004348 return -EBADF;
4349 }
4350 fl->fl_file = filp;
Jeff Laytone6f5c782014-08-22 10:40:25 -04004351 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04004352 if (fl)
Jeff Layton417c6622014-07-21 09:34:57 -04004353 locks_free_lock(fl);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04004354 if (status)
Jeff Layton417c6622014-07-21 09:34:57 -04004355 goto out_fput;
Benny Halevycdc97502014-05-30 09:09:30 -04004356 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004357 spin_lock(&fp->fi_lock);
4358 /* Did the lease get broken before we took the lock? */
4359 status = -EAGAIN;
4360 if (fp->fi_had_conflict)
4361 goto out_unlock;
4362 /* Race breaker */
Jeff Layton0c637be2014-08-22 12:05:43 -04004363 if (fp->fi_deleg_file) {
Andrew Elble34ed9872015-10-15 12:07:28 -04004364 status = hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04004365 goto out_unlock;
4366 }
Jeff Layton417c6622014-07-21 09:34:57 -04004367 fp->fi_deleg_file = filp;
Andrew Elble34ed9872015-10-15 12:07:28 -04004368 fp->fi_delegees = 0;
4369 status = hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04004370 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04004371 spin_unlock(&state_lock);
Andrew Elble34ed9872015-10-15 12:07:28 -04004372 if (status) {
4373 /* Should never happen, this is a new fi_deleg_file */
4374 WARN_ON_ONCE(1);
4375 goto out_fput;
4376 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004377 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04004378out_unlock:
4379 spin_unlock(&fp->fi_lock);
4380 spin_unlock(&state_lock);
4381out_fput:
4382 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05004383 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004384}
4385
Jeff Layton0b266932014-07-25 07:34:25 -04004386static struct nfs4_delegation *
4387nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004388 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004389{
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004390 int status = 0;
Jeff Layton0b266932014-07-25 07:34:25 -04004391 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04004392
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004393 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004394 return ERR_PTR(-EAGAIN);
4395
Andrew Elble34ed9872015-10-15 12:07:28 -04004396 spin_lock(&state_lock);
4397 spin_lock(&fp->fi_lock);
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004398 if (nfs4_delegation_exists(clp, fp))
4399 status = -EAGAIN;
Andrew Elble34ed9872015-10-15 12:07:28 -04004400 spin_unlock(&fp->fi_lock);
4401 spin_unlock(&state_lock);
4402
4403 if (status)
4404 return ERR_PTR(status);
4405
J. Bruce Fields86d29b12018-02-21 15:27:28 -05004406 dp = alloc_init_deleg(clp, fp, fh, odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004407 if (!dp)
4408 return ERR_PTR(-ENOMEM);
4409
Benny Halevycdc97502014-05-30 09:09:30 -04004410 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004411 spin_lock(&fp->fi_lock);
Jeff Layton0c637be2014-08-22 12:05:43 -04004412 if (!fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04004413 spin_unlock(&fp->fi_lock);
4414 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004415 status = nfs4_setlease(dp);
4416 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04004417 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004418 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04004419 status = -EAGAIN;
4420 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004421 }
Andrew Elble34ed9872015-10-15 12:07:28 -04004422 status = hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04004423out_unlock:
4424 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04004425 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004426out:
4427 if (status) {
Sachin Bhamare8287f002015-04-27 14:50:14 +02004428 put_clnt_odstate(dp->dl_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04004429 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04004430 return ERR_PTR(status);
4431 }
4432 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004433}
4434
Benny Halevy4aa89132012-02-21 14:16:44 -08004435static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4436{
4437 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4438 if (status == -EAGAIN)
4439 open->op_why_no_deleg = WND4_CONTENTION;
4440 else {
4441 open->op_why_no_deleg = WND4_RESOURCE;
4442 switch (open->op_deleg_want) {
4443 case NFS4_SHARE_WANT_READ_DELEG:
4444 case NFS4_SHARE_WANT_WRITE_DELEG:
4445 case NFS4_SHARE_WANT_ANY_DELEG:
4446 break;
4447 case NFS4_SHARE_WANT_CANCEL:
4448 open->op_why_no_deleg = WND4_CANCELLED;
4449 break;
4450 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004451 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08004452 }
4453 }
4454}
4455
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456/*
4457 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04004458 *
4459 * Note we don't support write delegations, and won't until the vfs has
4460 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 */
4462static void
Jeff Layton4cf59222014-07-25 07:34:24 -04004463nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4464 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465{
4466 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04004467 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4468 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004469 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004470 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004472 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07004473 open->op_recall = 0;
4474 switch (open->op_claim_type) {
4475 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05004476 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07004477 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004478 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4479 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004480 break;
4481 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00004482 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004483 /*
4484 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004485 * had the chance to reclaim theirs, *and* until
4486 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004487 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004488 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004489 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004490 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004491 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04004492 /*
4493 * Also, if the file was opened for write or
4494 * create, there's a good chance the client's
4495 * about to write to it, resulting in an
4496 * immediate recall (since we don't support
4497 * write delegations):
4498 */
NeilBrown7b190fe2005-06-23 22:03:23 -07004499 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04004500 goto out_no_deleg;
4501 if (open->op_create == NFS4_OPEN_CREATE)
4502 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004503 break;
4504 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004505 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004506 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004507 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004508 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004509 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004511 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004513 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004514 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04004515 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04004516 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004517 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004518out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004519 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4520 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004521 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04004522 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004523 open->op_recall = 1;
4524 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004525
4526 /* 4.1 client asking for a delegation? */
4527 if (open->op_deleg_want)
4528 nfsd4_open_deleg_none_ext(open, status);
4529 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530}
4531
Benny Halevye27f49c2012-02-21 14:16:54 -08004532static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4533 struct nfs4_delegation *dp)
4534{
4535 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4536 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4537 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4538 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4539 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4540 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4541 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4542 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4543 }
4544 /* Otherwise the client must be confused wanting a delegation
4545 * it already has, therefore we don't return
4546 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4547 */
4548}
4549
Al Virob37ad282006-10-19 23:28:59 -07004550__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4552{
Andy Adamson66689582009-04-03 08:28:45 +03004553 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004554 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004556 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004557 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004558 __be32 status;
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004559 bool new_stp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 /*
4562 * Lookup file; if found, lookup stateid and check open request,
4563 * and check for delegations in the process of being recalled.
4564 * If not found, create the nfs4_file struct
4565 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004566 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004567 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004568 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004569 if (status)
4570 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004571 stp = nfsd4_find_and_lock_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004573 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004574 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004575 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004576 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 }
4578
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004579 if (!stp) {
4580 stp = init_open_stateid(fp, open);
4581 if (!open->op_stp)
4582 new_stp = true;
4583 }
4584
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 /*
4586 * OPEN the file, or upgrade an existing OPEN.
4587 * If truncate fails, the OPEN fails.
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004588 *
4589 * stp is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 */
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004591 if (!new_stp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004593 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04004594 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004595 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04004597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 } else {
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004599 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4600 if (status) {
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004601 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004602 release_open_stateid(stp);
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004603 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004604 goto out;
4605 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004606
4607 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4608 open->op_odstate);
4609 if (stp->st_clnt_odstate == open->op_odstate)
4610 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 }
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004612
Jeff Layton9767feb2015-10-01 09:05:50 -04004613 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004614 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615
Benny Halevyd24433c2012-02-16 20:57:17 +02004616 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004617 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4618 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4619 open->op_why_no_deleg = WND4_NOT_WANTED;
4620 goto nodeleg;
4621 }
4622 }
4623
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 /*
4625 * Attempt to hand out a delegation. No error return, because the
4626 * OPEN succeeds even if we fail.
4627 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004628 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004629nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 status = nfs_ok;
4631
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004632 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004633 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004635 /* 4.1 client trying to upgrade/downgrade delegation? */
4636 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004637 open->op_deleg_want)
4638 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004639
NeilBrown13cd2182005-06-23 22:03:10 -07004640 if (fp)
4641 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004642 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08004643 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 /*
4645 * To finish the open response, we just need to set the rflags.
4646 */
4647 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Jeff Layton19e4c342016-09-16 16:28:27 -04004648 if (nfsd4_has_session(&resp->cstate))
4649 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
4650 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Jeff Layton19e4c342016-09-16 16:28:27 -04004652
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004653 if (dp)
4654 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004655 if (stp)
4656 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657
4658 return status;
4659}
4660
Jeff Layton58fb12e2014-07-29 21:34:27 -04004661void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04004662 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004663{
4664 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004665 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004666
Jeff Laytond3134b12014-07-29 21:34:32 -04004667 nfsd4_cstate_assign_replay(cstate, so);
4668 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004669 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004670 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04004671 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004672 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004673 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004674 if (open->op_odstate)
4675 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004676}
4677
Al Virob37ad282006-10-19 23:28:59 -07004678__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004679nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02004680 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681{
Christoph Hellwigeb698532017-05-08 20:58:35 +02004682 clientid_t *clid = &u->renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004684 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004685 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 dprintk("process_renew(%08x/%08x): starting\n",
4688 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004689 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004690 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004692 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004694 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004695 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 goto out;
4697 status = nfs_ok;
4698out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 return status;
4700}
4701
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004702void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004703nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004704{
Jeff Layton33dcc482012-04-10 11:08:48 -04004705 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004706 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004707 return;
4708
NeilBrowna76b4312005-06-23 22:04:01 -07004709 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004710 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004711 /*
4712 * If the server goes down again right now, an NFSv4
4713 * client will still be allowed to reclaim after it comes back up,
4714 * even if it hasn't yet had a chance to reclaim state this time.
4715 *
4716 */
Jeff Layton919b8042014-09-12 16:40:20 -04004717 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004718 /*
4719 * At this point, NFSv4 clients can still reclaim. But if the
4720 * server crashes, any that have not yet reclaimed will be out
4721 * of luck on the next boot.
4722 *
4723 * (NFSv4.1+ clients are considered to have reclaimed once they
4724 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4725 * have reclaimed after their first OPEN.)
4726 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004727 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004728 /*
4729 * At this point, and once lockd and/or any other containers
4730 * exit their grace period, further reclaims will fail and
4731 * regular locking can resume.
4732 */
NeilBrowna76b4312005-06-23 22:04:01 -07004733}
4734
NeilBrownfd39ca92005-06-23 22:04:03 -07004735static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004736nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737{
4738 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004739 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004741 struct nfs4_ol_stateid *stp;
Jeff Layton7919d0a2016-09-16 16:28:25 -04004742 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004744 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004745 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004748 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004749 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004750 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004751 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 clp = list_entry(pos, struct nfs4_client, cl_lru);
4753 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4754 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004755 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 break;
4757 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004758 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004759 dprintk("NFSD: client in use (clientid %08x)\n",
4760 clp->cl_clientid.cl_id);
4761 continue;
4762 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004763 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004764 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004765 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004766 list_for_each_safe(pos, next, &reaplist) {
4767 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 dprintk("NFSD: purging unused client (clientid %08x)\n",
4769 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004770 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 expire_client(clp);
4772 }
Benny Halevycdc97502014-05-30 09:09:30 -04004773 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004774 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4776 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004777 t = dp->dl_time - cutoff;
4778 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 break;
4780 }
Jeff Layton3fcbbd22015-08-24 12:41:48 -04004781 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04004782 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 }
Benny Halevycdc97502014-05-30 09:09:30 -04004784 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004785 while (!list_empty(&reaplist)) {
4786 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4787 dl_recall_lru);
4788 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004789 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 }
Jeff Layton217526e2014-07-30 08:27:11 -04004791
4792 spin_lock(&nn->client_lock);
4793 while (!list_empty(&nn->close_lru)) {
4794 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4795 oo_close_lru);
4796 if (time_after((unsigned long)oo->oo_time,
4797 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004798 t = oo->oo_time - cutoff;
4799 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 break;
4801 }
Jeff Layton217526e2014-07-30 08:27:11 -04004802 list_del_init(&oo->oo_close_lru);
4803 stp = oo->oo_last_closed_stid;
4804 oo->oo_last_closed_stid = NULL;
4805 spin_unlock(&nn->client_lock);
4806 nfs4_put_stid(&stp->st_stid);
4807 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 }
Jeff Layton217526e2014-07-30 08:27:11 -04004809 spin_unlock(&nn->client_lock);
4810
Jeff Layton7919d0a2016-09-16 16:28:25 -04004811 /*
4812 * It's possible for a client to try and acquire an already held lock
4813 * that is being held for a long time, and then lose interest in it.
4814 * So, we clean out any un-revisited request after a lease period
4815 * under the assumption that the client is no longer interested.
4816 *
4817 * RFC5661, sec. 9.6 states that the client must not rely on getting
4818 * notifications and must continue to poll for locks, even when the
4819 * server supports them. Thus this shouldn't lead to clients blocking
4820 * indefinitely once the lock does become free.
4821 */
4822 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04004823 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04004824 while (!list_empty(&nn->blocked_locks_lru)) {
4825 nbl = list_first_entry(&nn->blocked_locks_lru,
4826 struct nfsd4_blocked_lock, nbl_lru);
4827 if (time_after((unsigned long)nbl->nbl_time,
4828 (unsigned long)cutoff)) {
4829 t = nbl->nbl_time - cutoff;
4830 new_timeo = min(new_timeo, t);
4831 break;
4832 }
4833 list_move(&nbl->nbl_lru, &reaplist);
4834 list_del_init(&nbl->nbl_list);
4835 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04004836 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04004837
4838 while (!list_empty(&reaplist)) {
Naofumi Honda64ebe1242017-11-09 10:57:16 -05004839 nbl = list_first_entry(&reaplist,
Jeff Layton7919d0a2016-09-16 16:28:25 -04004840 struct nfsd4_blocked_lock, nbl_lru);
4841 list_del_init(&nbl->nbl_lru);
4842 posix_unblock_lock(&nbl->nbl_lock);
4843 free_blocked_lock(nbl);
4844 }
4845
Jeff Laytona832e7a2014-05-30 09:09:26 -04004846 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004847 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848}
4849
Harvey Harrisona254b242008-02-20 12:49:00 -08004850static struct workqueue_struct *laundry_wq;
4851static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004852
4853static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004854laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855{
4856 time_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08004857 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004858 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4859 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004861 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004863 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864}
4865
Jeff Layton8fcd4612015-07-30 06:57:46 -04004866static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004867{
Jeff Layton8fcd4612015-07-30 06:57:46 -04004868 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004869 return nfserr_bad_stateid;
4870 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871}
4872
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004874access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004876 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4877 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4878 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879}
4880
4881static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004882access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004884 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4885 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886}
4887
4888static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004889__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890{
Al Virob37ad282006-10-19 23:28:59 -07004891 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892
J. Bruce Fields02921912010-07-29 15:16:59 -04004893 /* For lock stateid's, we test the parent open, not the lock: */
4894 if (stp->st_openstp)
4895 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004896 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004898 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 goto out;
4900 status = nfs_ok;
4901out:
4902 return status;
4903}
4904
Al Virob37ad282006-10-19 23:28:59 -07004905static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004906check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004908 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004910 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004911 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 * conflicting state; so we must wait out the grace period. */
4913 return nfserr_grace;
4914 } else if (flags & WR_STATE)
4915 return nfs4_share_conflict(current_fh,
4916 NFS4_SHARE_DENY_WRITE);
4917 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4918 return nfs4_share_conflict(current_fh,
4919 NFS4_SHARE_DENY_READ);
4920}
4921
4922/*
4923 * Allow READ/WRITE during grace period on recovered state only for files
4924 * that are not able to provide mandatory locking.
4925 */
4926static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004927grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004929 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930}
4931
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004932static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004933{
Andy Adamson66689582009-04-03 08:28:45 +03004934 /*
4935 * When sessions are used the stateid generation number is ignored
4936 * when it is zero.
4937 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004938 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004939 return nfs_ok;
4940
4941 if (in->si_generation == ref->si_generation)
4942 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004943
J. Bruce Fields0836f582008-01-26 19:08:12 -05004944 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04004945 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004946 return nfserr_bad_stateid;
4947 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004948 * However, we could see a stateid from the past, even from a
4949 * non-buggy client. For example, if the client sends a lock
4950 * while some IO is outstanding, the lock may bump si_generation
4951 * while the IO is still in flight. The client could avoid that
4952 * situation by waiting for responses on all the IO requests,
4953 * but better performance may result in retrying IO that
4954 * receives an old_stateid error if requests are rarely
4955 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004956 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004957 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004958}
4959
Trond Myklebust03da3162017-11-03 08:00:16 -04004960static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
4961{
4962 __be32 ret;
4963
4964 spin_lock(&s->sc_lock);
4965 ret = nfsd4_verify_open_stid(s);
4966 if (ret == nfs_ok)
4967 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
4968 spin_unlock(&s->sc_lock);
4969 return ret;
4970}
4971
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004972static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4973{
4974 if (ols->st_stateowner->so_is_open_owner &&
4975 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4976 return nfserr_bad_stateid;
4977 return nfs_ok;
4978}
4979
Chuck Lever7df302f2012-05-29 13:56:37 -04004980static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004981{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004982 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004983 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004984
Andrew Elbleae254dac2017-11-09 13:41:10 -05004985 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
4986 CLOSE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004987 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004988 /* Client debugging aid. */
4989 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4990 char addr_str[INET6_ADDRSTRLEN];
4991 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4992 sizeof(addr_str));
4993 pr_warn_ratelimited("NFSD: client %s testing state ID "
4994 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004995 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004996 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004997 spin_lock(&cl->cl_lock);
4998 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004999 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005000 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005001 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04005002 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005003 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04005004 switch (s->sc_type) {
5005 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005006 status = nfs_ok;
5007 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005008 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005009 status = nfserr_deleg_revoked;
5010 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005011 case NFS4_OPEN_STID:
5012 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005013 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04005014 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005015 default:
5016 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005017 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04005018 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005019 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005020 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04005021 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005022out_unlock:
5023 spin_unlock(&cl->cl_lock);
5024 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04005025}
5026
Christoph Hellwigcd61c522014-08-14 08:44:57 +02005027__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005028nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5029 stateid_t *stateid, unsigned char typemask,
5030 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005031{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005032 __be32 status;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005033 bool return_revoked = false;
5034
5035 /*
5036 * only return revoked delegations if explicitly asked.
5037 * otherwise we report revoked or bad_stateid status.
5038 */
5039 if (typemask & NFS4_REVOKED_DELEG_STID)
5040 return_revoked = true;
5041 else if (typemask & NFS4_DELEG_STID)
5042 typemask |= NFS4_REVOKED_DELEG_STID;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005043
Andrew Elbleae254dac2017-11-09 13:41:10 -05005044 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5045 CLOSE_STATEID(stateid))
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005046 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005047 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005048 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005049 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005050 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005051 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005052 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005053 if (status)
5054 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005055 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005056 if (!*s)
5057 return nfserr_bad_stateid;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005058 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5059 nfs4_put_stid(*s);
5060 if (cstate->minorversion)
5061 return nfserr_deleg_revoked;
5062 return nfserr_bad_stateid;
5063 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005064 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005065}
5066
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005067static struct file *
5068nfs4_find_file(struct nfs4_stid *s, int flags)
5069{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005070 if (!s)
5071 return NULL;
5072
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005073 switch (s->sc_type) {
5074 case NFS4_DELEG_STID:
5075 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5076 return NULL;
5077 return get_file(s->sc_file->fi_deleg_file);
5078 case NFS4_OPEN_STID:
5079 case NFS4_LOCK_STID:
5080 if (flags & RD_STATE)
5081 return find_readable_file(s->sc_file);
5082 else
5083 return find_writeable_file(s->sc_file);
5084 break;
5085 }
5086
5087 return NULL;
5088}
5089
5090static __be32
5091nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
5092{
5093 __be32 status;
5094
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005095 status = nfsd4_check_openowner_confirmed(ols);
5096 if (status)
5097 return status;
5098 return nfs4_check_openmode(ols, flags);
5099}
5100
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005101static __be32
5102nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
5103 struct file **filpp, bool *tmp_file, int flags)
5104{
5105 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
5106 struct file *file;
5107 __be32 status;
5108
5109 file = nfs4_find_file(s, flags);
5110 if (file) {
5111 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5112 acc | NFSD_MAY_OWNER_OVERRIDE);
5113 if (status) {
5114 fput(file);
5115 return status;
5116 }
5117
5118 *filpp = file;
5119 } else {
5120 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
5121 if (status)
5122 return status;
5123
5124 if (tmp_file)
5125 *tmp_file = true;
5126 }
5127
5128 return 0;
5129}
5130
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005132 * Checks for stateid operations
5133 */
Al Virob37ad282006-10-19 23:28:59 -07005134__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005135nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01005136 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
5137 stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005139 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005140 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005141 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005142 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005143 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 if (filpp)
5146 *filpp = NULL;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005147 if (tmp_file)
5148 *tmp_file = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005150 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 return nfserr_grace;
5152
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005153 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5154 status = check_special_stateids(net, fhp, stateid, flags);
5155 goto done;
5156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005158 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04005159 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005160 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005161 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04005162 return status;
Trond Myklebust03da3162017-11-03 08:00:16 -04005163 status = nfsd4_stid_check_stateid_generation(stateid, s,
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005164 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04005165 if (status)
5166 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005167
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005168 switch (s->sc_type) {
5169 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005170 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005171 break;
5172 case NFS4_OPEN_STID:
5173 case NFS4_LOCK_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005174 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005175 break;
5176 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04005177 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005178 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04005180 if (status)
5181 goto out;
5182 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005183
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005184done:
5185 if (!status && filpp)
5186 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187out:
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005188 if (s)
5189 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 return status;
5191}
5192
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005193/*
Bryan Schumaker17456802011-07-13 10:50:48 -04005194 * Test if the stateid is valid
5195 */
5196__be32
5197nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005198 union nfsd4_op_u *u)
Bryan Schumaker17456802011-07-13 10:50:48 -04005199{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005200 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005201 struct nfsd4_test_stateid_id *stateid;
5202 struct nfs4_client *cl = cstate->session->se_client;
5203
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005204 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04005205 stateid->ts_id_status =
5206 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005207
Bryan Schumaker17456802011-07-13 10:50:48 -04005208 return nfs_ok;
5209}
5210
Chuck Lever42691392016-08-11 10:37:30 -04005211static __be32
5212nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5213{
5214 struct nfs4_ol_stateid *stp = openlockstateid(s);
5215 __be32 ret;
5216
Trond Myklebust659aefb2017-11-03 08:00:13 -04005217 ret = nfsd4_lock_ol_stateid(stp);
5218 if (ret)
5219 goto out_put_stid;
Chuck Lever42691392016-08-11 10:37:30 -04005220
5221 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5222 if (ret)
5223 goto out;
5224
5225 ret = nfserr_locks_held;
5226 if (check_for_locks(stp->st_stid.sc_file,
5227 lockowner(stp->st_stateowner)))
5228 goto out;
5229
5230 release_lock_stateid(stp);
5231 ret = nfs_ok;
5232
5233out:
5234 mutex_unlock(&stp->st_mutex);
Trond Myklebust659aefb2017-11-03 08:00:13 -04005235out_put_stid:
Chuck Lever42691392016-08-11 10:37:30 -04005236 nfs4_put_stid(s);
5237 return ret;
5238}
5239
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005240__be32
5241nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005242 union nfsd4_op_u *u)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005243{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005244 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005245 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005246 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005247 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005248 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005249 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005250
Jeff Layton1af71cc2014-07-29 21:34:14 -04005251 spin_lock(&cl->cl_lock);
5252 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005253 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005254 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005255 spin_lock(&s->sc_lock);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005256 switch (s->sc_type) {
5257 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005258 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005259 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005260 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005261 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5262 if (ret)
5263 break;
5264 ret = nfserr_locks_held;
5265 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005266 case NFS4_LOCK_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005267 spin_unlock(&s->sc_lock);
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03005268 refcount_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005269 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04005270 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005271 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005272 case NFS4_REVOKED_DELEG_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005273 spin_unlock(&s->sc_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005274 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005275 list_del_init(&dp->dl_recall_lru);
5276 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04005277 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005278 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005279 goto out;
5280 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005281 }
Trond Myklebust03da3162017-11-03 08:00:16 -04005282 spin_unlock(&s->sc_lock);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005283out_unlock:
5284 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005285out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005286 return ret;
5287}
5288
NeilBrown4c4cd222005-07-07 17:59:27 -07005289static inline int
5290setlkflg (int type)
5291{
5292 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5293 RD_STATE : WR_STATE;
5294}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005296static __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 -04005297{
5298 struct svc_fh *current_fh = &cstate->current_fh;
5299 struct nfs4_stateowner *sop = stp->st_stateowner;
5300 __be32 status;
5301
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005302 status = nfsd4_check_seqid(cstate, sop, seqid);
5303 if (status)
5304 return status;
Trond Myklebust9271d7e2017-11-03 08:00:15 -04005305 status = nfsd4_lock_ol_stateid(stp);
5306 if (status != nfs_ok)
5307 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005308 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04005309 if (status == nfs_ok)
5310 status = nfs4_check_fh(current_fh, &stp->st_stid);
5311 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005312 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005313 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005314}
5315
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316/*
5317 * Checks for sequence id mutating operations.
5318 */
Al Virob37ad282006-10-19 23:28:59 -07005319static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03005320nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04005321 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005322 struct nfs4_ol_stateid **stpp,
5323 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324{
J. Bruce Fields0836f582008-01-26 19:08:12 -05005325 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005326 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005327 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005329 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5330 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07005331
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005333 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005334 if (status)
5335 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005336 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005337 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005339 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005340 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005341 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04005342 else
5343 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005344 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005345}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05005346
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005347static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5348 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005349{
5350 __be32 status;
5351 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005352 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005354 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005355 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04005356 if (status)
5357 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005358 oo = openowner(stp->st_stateowner);
5359 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005360 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005361 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07005362 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005363 }
5364 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07005365 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366}
5367
Al Virob37ad282006-10-19 23:28:59 -07005368__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005369nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005370 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005372 struct nfsd4_open_confirm *oc = &u->open_confirm;
Al Virob37ad282006-10-19 23:28:59 -07005373 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005374 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005375 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005376 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377
Al Viroa6a9f182013-09-16 10:57:01 -04005378 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5379 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005381 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07005382 if (status)
5383 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005385 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005386 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005387 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005388 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005389 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005390 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005391 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005392 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005393 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04005394 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005395 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04005396 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04005397 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005398 mutex_unlock(&stp->st_mutex);
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005399 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005400 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07005401
Jeff Layton2a4317c2012-03-21 16:42:43 -04005402 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005403 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04005404put_stateid:
5405 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005407 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 return status;
5409}
5410
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005411static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005413 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005414 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04005415 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005416 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005417}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04005418
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005419static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5420{
5421 switch (to_access) {
5422 case NFS4_SHARE_ACCESS_READ:
5423 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5424 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5425 break;
5426 case NFS4_SHARE_ACCESS_WRITE:
5427 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5428 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5429 break;
5430 case NFS4_SHARE_ACCESS_BOTH:
5431 break;
5432 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005433 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 }
5435}
5436
Al Virob37ad282006-10-19 23:28:59 -07005437__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005438nfsd4_open_downgrade(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005439 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005441 struct nfsd4_open_downgrade *od = &u->open_downgrade;
Al Virob37ad282006-10-19 23:28:59 -07005442 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005443 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005444 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445
Al Viroa6a9f182013-09-16 10:57:01 -04005446 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5447 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04005449 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02005450 if (od->od_deleg_want)
5451 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5452 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005454 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005455 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005456 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005459 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005460 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005462 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04005464 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005465 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005467 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005469 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04005470 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04005471 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005473put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005474 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005475 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005477 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 return status;
5479}
5480
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005481static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5482{
Trond Myklebustacf92952014-06-30 11:48:37 -04005483 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04005484 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04005485 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005486
Jeff Layton2c41beb2014-07-29 21:34:41 -04005487 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04005488 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005489
Jeff Laytond83017f2014-07-29 21:34:42 -04005490 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04005491 if (unhashed)
5492 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04005493 spin_unlock(&clp->cl_lock);
5494 free_ol_stateid_reaplist(&reaplist);
5495 } else {
5496 spin_unlock(&clp->cl_lock);
5497 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04005498 if (unhashed)
5499 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04005500 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04005501}
5502
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503/*
5504 * nfs4_unlock_state() called after encode
5505 */
Al Virob37ad282006-10-19 23:28:59 -07005506__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005507nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005508 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005510 struct nfsd4_close *close = &u->close;
Al Virob37ad282006-10-19 23:28:59 -07005511 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005512 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005513 struct net *net = SVC_NET(rqstp);
5514 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515
Al Viroa6a9f182013-09-16 10:57:01 -04005516 dprintk("NFSD: nfsd4_close on file %pd\n",
5517 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005519 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5520 &close->cl_stateid,
5521 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005522 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005523 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005524 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005526
5527 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Laytonbd2deca2018-02-15 15:16:18 -05005528
5529 /*
5530 * Technically we don't _really_ have to increment or copy it, since
5531 * it should just be gone after this operation and we clobber the
5532 * copied value below, but we continue to do so here just to ensure
5533 * that racing ops see that there was a state change.
5534 */
Jeff Layton9767feb2015-10-01 09:05:50 -04005535 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005537 nfsd4_close_open_stateid(stp);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005538 mutex_unlock(&stp->st_mutex);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005539
Jeff Laytonbd2deca2018-02-15 15:16:18 -05005540 /* v4.1+ suggests that we send a special stateid in here, since the
5541 * clients should just ignore this anyway. Since this is not useful
5542 * for v4.0 clients either, we set it to the special close_stateid
5543 * universally.
5544 *
5545 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
5546 */
5547 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
Trond Myklebustfb500a72017-11-03 08:00:12 -04005548
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005549 /* put reference from nfs4_preprocess_seqid_op */
5550 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 return status;
5553}
5554
Al Virob37ad282006-10-19 23:28:59 -07005555__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005556nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005557 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005559 struct nfsd4_delegreturn *dr = &u->delegreturn;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005560 struct nfs4_delegation *dp;
5561 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005562 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07005563 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005564 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005566 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005567 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005569 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005570 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005571 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005572 dp = delegstateid(s);
Trond Myklebust03da3162017-11-03 08:00:16 -04005573 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005574 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04005575 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005576
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005577 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005578put_stateid:
5579 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580out:
5581 return status;
5582}
5583
Benny Halevy87df4de2008-12-15 19:42:03 +02005584static inline u64
5585end_offset(u64 start, u64 len)
5586{
5587 u64 end;
5588
5589 end = start + len;
5590 return end >= start ? end: NFS4_MAX_UINT64;
5591}
5592
5593/* last octet in a range */
5594static inline u64
5595last_byte_offset(u64 start, u64 len)
5596{
5597 u64 end;
5598
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005599 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02005600 end = start + len;
5601 return end > start ? end - 1: NFS4_MAX_UINT64;
5602}
5603
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604/*
5605 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5606 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5607 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5608 * locking, this prevents us from being completely protocol-compliant. The
5609 * real solution to this problem is to start using unsigned file offsets in
5610 * the VFS, but this is a very deep change!
5611 */
5612static inline void
5613nfs4_transform_lock_offset(struct file_lock *lock)
5614{
5615 if (lock->fl_start < 0)
5616 lock->fl_start = OFFSET_MAX;
5617 if (lock->fl_end < 0)
5618 lock->fl_end = OFFSET_MAX;
5619}
5620
Jeff Laytoncae80b32015-04-03 09:04:04 -04005621static fl_owner_t
5622nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005623{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005624 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5625
5626 nfs4_get_stateowner(&lo->lo_owner);
5627 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005628}
5629
Jeff Laytoncae80b32015-04-03 09:04:04 -04005630static void
5631nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005632{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005633 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005634
Jeff Laytoncae80b32015-04-03 09:04:04 -04005635 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04005636 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04005637}
5638
Jeff Layton76d348f2016-09-16 16:28:24 -04005639static void
5640nfsd4_lm_notify(struct file_lock *fl)
5641{
5642 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
5643 struct net *net = lo->lo_owner.so_client->net;
5644 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5645 struct nfsd4_blocked_lock *nbl = container_of(fl,
5646 struct nfsd4_blocked_lock, nbl_lock);
5647 bool queue = false;
5648
Jeff Layton7919d0a2016-09-16 16:28:25 -04005649 /* An empty list means that something else is going to be using it */
Jeff Layton0cc11a62016-10-20 09:34:31 -04005650 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005651 if (!list_empty(&nbl->nbl_list)) {
5652 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005653 list_del_init(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -04005654 queue = true;
5655 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04005656 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005657
5658 if (queue)
5659 nfsd4_run_cb(&nbl->nbl_cb);
5660}
5661
Alexey Dobriyan7b021962009-09-21 17:01:12 -07005662static const struct lock_manager_operations nfsd_posix_mng_ops = {
Jeff Layton76d348f2016-09-16 16:28:24 -04005663 .lm_notify = nfsd4_lm_notify,
Kinglong Meeaef95832014-08-22 10:18:44 -04005664 .lm_get_owner = nfsd4_fl_get_owner,
5665 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07005666};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667
5668static inline void
5669nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5670{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005671 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672
NeilBrownd5b90262006-04-10 22:55:22 -07005673 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005674 lo = (struct nfs4_lockowner *) fl->fl_owner;
5675 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5676 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005677 if (!deny->ld_owner.data)
5678 /* We just don't care that much */
5679 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005680 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5681 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07005682 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005683nevermind:
5684 deny->ld_owner.len = 0;
5685 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07005686 deny->ld_clientid.cl_boot = 0;
5687 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 }
5689 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02005690 deny->ld_length = NFS4_MAX_UINT64;
5691 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5693 deny->ld_type = NFS4_READ_LT;
5694 if (fl->fl_type != F_RDLCK)
5695 deny->ld_type = NFS4_WRITE_LT;
5696}
5697
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005698static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005699find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700{
Trond Myklebustd4f04892014-07-29 21:34:36 -04005701 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005702 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703
Trond Myklebust0a880a22014-07-30 08:27:10 -04005704 lockdep_assert_held(&clp->cl_lock);
5705
Trond Myklebustd4f04892014-07-29 21:34:36 -04005706 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5707 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005708 if (so->so_is_open_owner)
5709 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005710 if (same_owner_str(so, owner))
5711 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 }
5713 return NULL;
5714}
5715
Trond Myklebustc58c6612014-07-29 21:34:35 -04005716static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005717find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04005718{
5719 struct nfs4_lockowner *lo;
5720
Trond Myklebustd4f04892014-07-29 21:34:36 -04005721 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005722 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005723 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005724 return lo;
5725}
5726
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005727static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5728{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005729 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005730}
5731
Jeff Layton6b180f02014-07-29 21:34:26 -04005732static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5733{
5734 struct nfs4_lockowner *lo = lockowner(sop);
5735
5736 kmem_cache_free(lockowner_slab, lo);
5737}
5738
5739static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005740 .so_unhash = nfs4_unhash_lockowner,
5741 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005742};
5743
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744/*
5745 * Alloc a lock owner structure.
5746 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005747 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005749 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005751static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005752alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5753 struct nfs4_ol_stateid *open_stp,
5754 struct nfsd4_lock *lock)
5755{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005756 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005758 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5759 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04005761 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005762 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5763 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005764 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005765 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005766 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005767 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005768 if (ret == NULL) {
5769 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005770 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005771 ret = lo;
5772 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08005773 nfs4_free_stateowner(&lo->lo_owner);
5774
Trond Myklebustd4f04892014-07-29 21:34:36 -04005775 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04005776 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777}
5778
Trond Myklebustfd1fd682017-11-03 08:00:14 -04005779static struct nfs4_ol_stateid *
5780find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5781{
5782 struct nfs4_ol_stateid *lst;
5783 struct nfs4_client *clp = lo->lo_owner.so_client;
5784
5785 lockdep_assert_held(&clp->cl_lock);
5786
5787 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
5788 if (lst->st_stid.sc_type != NFS4_LOCK_STID)
5789 continue;
5790 if (lst->st_stid.sc_file == fp) {
5791 refcount_inc(&lst->st_stid.sc_count);
5792 return lst;
5793 }
5794 }
5795 return NULL;
5796}
5797
Trond Myklebustbeeca192017-11-03 08:00:14 -04005798static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04005799init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5800 struct nfs4_file *fp, struct inode *inode,
5801 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005803 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustbeeca192017-11-03 08:00:14 -04005804 struct nfs4_ol_stateid *retstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805
Trond Myklebustbeeca192017-11-03 08:00:14 -04005806 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05005807 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Trond Myklebustbeeca192017-11-03 08:00:14 -04005808retry:
5809 spin_lock(&clp->cl_lock);
5810 spin_lock(&fp->fi_lock);
5811 retstp = find_lock_stateid(lo, fp);
5812 if (retstp)
5813 goto out_unlock;
Jeff Layton356a95e2014-07-29 21:34:13 -04005814
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03005815 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005816 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005817 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07005818 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005819 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005820 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005822 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005823 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005824 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005825 list_add(&stp->st_perfile, &fp->fi_stateids);
Trond Myklebustbeeca192017-11-03 08:00:14 -04005826out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04005827 spin_unlock(&fp->fi_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04005828 spin_unlock(&clp->cl_lock);
5829 if (retstp) {
5830 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
5831 nfs4_put_stid(&retstp->st_stid);
5832 goto retry;
5833 }
5834 /* To keep mutex tracking happy */
5835 mutex_unlock(&stp->st_mutex);
5836 stp = retstp;
5837 }
5838 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839}
5840
Jeff Laytonc53530d2014-06-30 11:48:39 -04005841static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04005842find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5843 struct inode *inode, struct nfs4_ol_stateid *ost,
5844 bool *new)
5845{
5846 struct nfs4_stid *ns = NULL;
5847 struct nfs4_ol_stateid *lst;
5848 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5849 struct nfs4_client *clp = oo->oo_owner.so_client;
5850
Trond Myklebustbeeca192017-11-03 08:00:14 -04005851 *new = false;
Jeff Layton356a95e2014-07-29 21:34:13 -04005852 spin_lock(&clp->cl_lock);
5853 lst = find_lock_stateid(lo, fi);
Jeff Layton356a95e2014-07-29 21:34:13 -04005854 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04005855 if (lst != NULL) {
5856 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
5857 goto out;
5858 nfs4_put_stid(&lst->st_stid);
5859 }
5860 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
5861 if (ns == NULL)
5862 return NULL;
5863
5864 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
5865 if (lst == openlockstateid(ns))
5866 *new = true;
5867 else
Jeff Layton356a95e2014-07-29 21:34:13 -04005868 nfs4_put_stid(ns);
Trond Myklebustbeeca192017-11-03 08:00:14 -04005869out:
Jeff Layton356a95e2014-07-29 21:34:13 -04005870 return lst;
5871}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005872
NeilBrownfd39ca92005-06-23 22:04:03 -07005873static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874check_lock_length(u64 offset, u64 length)
5875{
Kinglong Meee7969312015-07-13 17:34:19 +08005876 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5877 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878}
5879
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005880static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005881{
Trond Myklebust11b91642014-07-29 21:34:08 -04005882 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005883
Jeff Layton7214e862014-07-10 14:07:33 -04005884 lockdep_assert_held(&fp->fi_lock);
5885
Jeff Layton82c5ff12012-05-11 09:45:13 -04005886 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005887 return;
Jeff Layton12659652014-07-10 14:07:28 -04005888 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005889 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005890}
5891
Jeff Layton356a95e2014-07-29 21:34:13 -04005892static __be32
5893lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5894 struct nfs4_ol_stateid *ost,
5895 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04005896 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005897{
Jeff Layton5db1c032014-07-29 21:34:28 -04005898 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005899 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005900 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5901 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00005902 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005903 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04005904 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005905 unsigned int strhashval;
5906
Kinglong Meec8623992015-07-13 17:35:37 +08005907 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005908 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005909 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005910 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5911 if (lo == NULL)
5912 return nfserr_jukebox;
5913 } else {
5914 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005915 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005916 if (!cstate->minorversion &&
5917 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005918 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005919 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005920
Jeff Laytondd257932016-08-11 10:37:39 -04005921 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5922 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005923 status = nfserr_jukebox;
5924 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005925 }
Jeff Laytondd257932016-08-11 10:37:39 -04005926
Jeff Layton5db1c032014-07-29 21:34:28 -04005927 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04005928 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04005929out:
5930 nfs4_put_stateowner(&lo->lo_owner);
5931 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005932}
5933
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934/*
5935 * LOCK operation
5936 */
Al Virob37ad282006-10-19 23:28:59 -07005937__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005938nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005939 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005941 struct nfsd4_lock *lock = &u->lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005942 struct nfs4_openowner *open_sop = NULL;
5943 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005944 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005945 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005946 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005947 struct file *filp = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04005948 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005949 struct file_lock *file_lock = NULL;
5950 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005951 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005952 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005953 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005954 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04005955 unsigned char fl_type;
5956 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005957 struct net *net = SVC_NET(rqstp);
5958 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959
5960 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5961 (long long) lock->lk_offset,
5962 (long long) lock->lk_length);
5963
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 if (check_lock_length(lock->lk_offset, lock->lk_length))
5965 return nfserr_inval;
5966
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005967 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005968 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005969 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5970 return status;
5971 }
5972
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005974 if (nfsd4_has_session(cstate))
5975 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005976 memcpy(&lock->lk_new_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04005977 &cstate->session->se_client->cl_clientid,
5978 sizeof(clientid_t));
5979
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005981 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005985 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 lock->lk_new_open_seqid,
5987 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005988 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005989 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005991 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005992 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005993 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005994 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005995 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005996 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005997 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005998 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005999 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03006000 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006001 lock->lk_old_lock_seqid,
6002 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006003 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006004 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04006005 if (status)
6006 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006007 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006009 lkflg = setlkflg(lock->lk_type);
6010 status = nfs4_check_openmode(lock_stp, lkflg);
6011 if (status)
6012 goto out;
6013
NeilBrown0dd395d2005-07-07 17:59:15 -07006014 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006015 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006016 goto out;
6017 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006018 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006019 goto out;
6020
Trond Myklebust11b91642014-07-29 21:34:08 -04006021 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 case NFS4_READW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006024 if (nfsd4_has_session(cstate))
6025 fl_flags |= FL_SLEEP;
6026 /* Fallthrough */
6027 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006028 spin_lock(&fp->fi_lock);
6029 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006030 if (filp)
6031 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04006032 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006033 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006034 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035 case NFS4_WRITEW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006036 if (nfsd4_has_session(cstate))
6037 fl_flags |= FL_SLEEP;
6038 /* Fallthrough */
6039 case NFS4_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006040 spin_lock(&fp->fi_lock);
6041 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006042 if (filp)
6043 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04006044 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006045 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006046 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 default:
6048 status = nfserr_inval;
6049 goto out;
6050 }
Jeff Layton76d348f2016-09-16 16:28:24 -04006051
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006052 if (!filp) {
6053 status = nfserr_openmode;
6054 goto out;
6055 }
Kinglong Meeaef95832014-08-22 10:18:44 -04006056
Jeff Layton76d348f2016-09-16 16:28:24 -04006057 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6058 if (!nbl) {
6059 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6060 status = nfserr_jukebox;
6061 goto out;
6062 }
6063
6064 file_lock = &nbl->nbl_lock;
6065 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04006066 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04006067 file_lock->fl_pid = current->tgid;
6068 file_lock->fl_file = filp;
Jeff Layton76d348f2016-09-16 16:28:24 -04006069 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04006070 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6071 file_lock->fl_start = lock->lk_offset;
6072 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6073 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074
Jeff Layton21179d82012-08-21 08:03:32 -04006075 conflock = locks_alloc_lock();
6076 if (!conflock) {
6077 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6078 status = nfserr_jukebox;
6079 goto out;
6080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081
Jeff Layton76d348f2016-09-16 16:28:24 -04006082 if (fl_flags & FL_SLEEP) {
Jeff Layton7919d0a2016-09-16 16:28:25 -04006083 nbl->nbl_time = jiffies;
Jeff Layton0cc11a62016-10-20 09:34:31 -04006084 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006085 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006086 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006087 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006088 }
6089
Jeff Layton21179d82012-08-21 08:03:32 -04006090 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006091 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04006093 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07006094 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006095 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006096 case FILE_LOCK_DEFERRED:
6097 nbl = NULL;
6098 /* Fallthrough */
6099 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006100 status = nfserr_denied;
6101 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04006102 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006103 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006104 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006106 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04006107 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05006108 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04006109 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006110 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112out:
Jeff Layton76d348f2016-09-16 16:28:24 -04006113 if (nbl) {
6114 /* dequeue it if we queued it before */
6115 if (fl_flags & FL_SLEEP) {
Jeff Layton0cc11a62016-10-20 09:34:31 -04006116 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006117 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006118 list_del_init(&nbl->nbl_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006119 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006120 }
6121 free_blocked_lock(nbl);
6122 }
Trond Myklebustde186432014-07-10 14:07:26 -04006123 if (filp)
6124 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04006125 if (lock_stp) {
6126 /* Bump seqid manually if the 4.0 replay owner is openowner */
6127 if (cstate->replay_owner &&
6128 cstate->replay_owner != &lock_sop->lo_owner &&
6129 seqid_mutating_err(ntohl(status)))
6130 lock_sop->lo_owner.so_seqid++;
6131
6132 /*
6133 * If this is a new, never-before-used stateid, and we are
6134 * returning an error, then just go ahead and release it.
6135 */
J. Bruce Fields25020722018-01-17 16:25:59 -05006136 if (status && new)
Jeff Layton5db1c032014-07-29 21:34:28 -04006137 release_lock_stateid(lock_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006138
6139 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton5db1c032014-07-29 21:34:28 -04006140
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006141 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04006142 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006143 if (open_stp)
6144 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006145 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006146 if (conflock)
6147 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148 return status;
6149}
6150
6151/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006152 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6153 * so we do a temporary open here just to get an open file to pass to
6154 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6155 * inode operation.)
6156 */
Al Viro04da6e92012-04-13 00:00:04 -04006157static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006158{
6159 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04006160 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
6161 if (!err) {
6162 err = nfserrno(vfs_test_lock(file, lock));
Christoph Hellwigfd891452015-04-28 15:41:16 +02006163 fput(file);
Al Viro04da6e92012-04-13 00:00:04 -04006164 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006165 return err;
6166}
6167
6168/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 * LOCKT operation
6170 */
Al Virob37ad282006-10-19 23:28:59 -07006171__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006172nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006173 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006175 struct nfsd4_lockt *lockt = &u->lockt;
Jeff Layton21179d82012-08-21 08:03:32 -04006176 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04006177 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006178 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006179 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006181 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 return nfserr_grace;
6183
6184 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6185 return nfserr_inval;
6186
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006187 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04006188 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006189 if (status)
6190 goto out;
6191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192
J. Bruce Fields75c096f2011-08-15 18:39:32 -04006193 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195
Jeff Layton21179d82012-08-21 08:03:32 -04006196 file_lock = locks_alloc_lock();
6197 if (!file_lock) {
6198 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6199 status = nfserr_jukebox;
6200 goto out;
6201 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006202
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203 switch (lockt->lt_type) {
6204 case NFS4_READ_LT:
6205 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006206 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207 break;
6208 case NFS4_WRITE_LT:
6209 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006210 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 break;
6212 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04006213 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214 status = nfserr_inval;
6215 goto out;
6216 }
6217
Kinglong Meec8623992015-07-13 17:35:37 +08006218 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006219 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04006220 file_lock->fl_owner = (fl_owner_t)lo;
6221 file_lock->fl_pid = current->tgid;
6222 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223
Jeff Layton21179d82012-08-21 08:03:32 -04006224 file_lock->fl_start = lockt->lt_offset;
6225 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226
Jeff Layton21179d82012-08-21 08:03:32 -04006227 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228
Jeff Layton21179d82012-08-21 08:03:32 -04006229 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04006230 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05006231 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04006232
Jeff Layton21179d82012-08-21 08:03:32 -04006233 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04006235 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236 }
6237out:
Jeff Layton5db1c032014-07-29 21:34:28 -04006238 if (lo)
6239 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04006240 if (file_lock)
6241 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 return status;
6243}
6244
Al Virob37ad282006-10-19 23:28:59 -07006245__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006246nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006247 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006249 struct nfsd4_locku *locku = &u->locku;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006250 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006252 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006253 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07006254 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006255 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6256
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6258 (long long) locku->lu_offset,
6259 (long long) locku->lu_length);
6260
6261 if (check_lock_length(locku->lu_offset, locku->lu_length))
6262 return nfserr_inval;
6263
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006264 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006265 &locku->lu_stateid, NFS4_LOCK_STID,
6266 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006267 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04006269 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006270 if (!filp) {
6271 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04006272 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006273 }
Jeff Layton21179d82012-08-21 08:03:32 -04006274 file_lock = locks_alloc_lock();
6275 if (!file_lock) {
6276 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6277 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04006278 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04006279 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006280
Jeff Layton21179d82012-08-21 08:03:32 -04006281 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04006282 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04006283 file_lock->fl_pid = current->tgid;
6284 file_lock->fl_file = filp;
6285 file_lock->fl_flags = FL_POSIX;
6286 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6287 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288
Jeff Layton21179d82012-08-21 08:03:32 -04006289 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6290 locku->lu_length);
6291 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292
Jeff Layton21179d82012-08-21 08:03:32 -04006293 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07006294 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05006295 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 goto out_nfserr;
6297 }
Jeff Layton9767feb2015-10-01 09:05:50 -04006298 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Trond Myklebustde186432014-07-10 14:07:26 -04006299fput:
6300 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04006301put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006302 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04006303 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006305 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006306 if (file_lock)
6307 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 return status;
6309
6310out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07006311 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04006312 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313}
6314
6315/*
6316 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006317 * true: locks held by lockowner
6318 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006320static bool
6321check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006323 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006324 int status = false;
6325 struct file *filp = find_any_file(fp);
6326 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006327 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006328
6329 if (!filp) {
6330 /* Any valid lock stateid should have some sort of access */
6331 WARN_ON_ONCE(1);
6332 return status;
6333 }
6334
6335 inode = file_inode(filp);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006336 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006338 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05006339 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006340 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6341 if (fl->fl_owner == (fl_owner_t)lowner) {
6342 status = true;
6343 break;
6344 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08006345 }
Jeff Layton6109c852015-01-16 15:05:57 -05006346 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347 }
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006348 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 return status;
6350}
6351
Al Virob37ad282006-10-19 23:28:59 -07006352__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08006353nfsd4_release_lockowner(struct svc_rqst *rqstp,
6354 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006355 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006357 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04006359 struct nfs4_stateowner *sop;
6360 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006361 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006363 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07006364 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006365 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006366 struct nfs4_client *clp;
Chuck Lever88584812016-07-13 16:40:14 -04006367 LIST_HEAD (reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368
6369 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6370 clid->cl_boot, clid->cl_id);
6371
Jeff Layton4b24ca72014-06-30 11:48:44 -04006372 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006373 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04006374 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006375
Trond Myklebustd4f04892014-07-29 21:34:36 -04006376 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04006377 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04006378 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04006379 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04006380 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04006381
6382 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006383 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04006384
Jeff Layton882e9d22014-07-29 21:34:37 -04006385 /* see if there are still any locks associated with it */
6386 lo = lockowner(sop);
6387 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6388 if (check_for_locks(stp->st_stid.sc_file, lo)) {
6389 status = nfserr_locks_held;
6390 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04006391 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04006392 }
Jeff Layton5adfd882014-07-29 21:34:31 -04006393 }
Jeff Layton882e9d22014-07-29 21:34:37 -04006394
Kinglong Meeb5971af2014-08-22 10:18:43 -04006395 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04006396 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04006397 }
Chuck Lever88584812016-07-13 16:40:14 -04006398 if (!lo) {
6399 spin_unlock(&clp->cl_lock);
6400 return status;
6401 }
6402
6403 unhash_lockowner_locked(lo);
6404 while (!list_empty(&lo->lo_owner.so_stateids)) {
6405 stp = list_first_entry(&lo->lo_owner.so_stateids,
6406 struct nfs4_ol_stateid,
6407 st_perstateowner);
6408 WARN_ON(!unhash_lock_stateid(stp));
6409 put_ol_stateid_locked(stp, &reaplist);
6410 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04006411 spin_unlock(&clp->cl_lock);
Chuck Lever88584812016-07-13 16:40:14 -04006412 free_ol_stateid_reaplist(&reaplist);
Jeff Layton68ef3bc2018-03-16 11:32:02 -04006413 remove_blocked_locks(lo);
Chuck Lever88584812016-07-13 16:40:14 -04006414 nfs4_put_stateowner(&lo->lo_owner);
6415
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 return status;
6417}
6418
6419static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07006420alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421{
NeilBrowna55370a2005-06-23 22:03:52 -07006422 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423}
6424
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006425bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006426nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07006427{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006428 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07006429
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006430 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006431 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07006432}
6433
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434/*
6435 * failure => all reset bets are off, nfserr_no_grace...
6436 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05006437struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006438nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439{
6440 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006441 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442
NeilBrowna55370a2005-06-23 22:03:52 -07006443 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
6444 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05006445 if (crp) {
6446 strhashval = clientstr_hashval(name);
6447 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006448 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05006449 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006450 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006451 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006452 }
6453 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454}
6455
Jeff Layton2a4317c2012-03-21 16:42:43 -04006456void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006457nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05006458{
6459 list_del(&crp->cr_strhash);
6460 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006461 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05006462}
6463
6464void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006465nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466{
6467 struct nfs4_client_reclaim *crp = NULL;
6468 int i;
6469
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006471 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6472 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006474 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475 }
6476 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006477 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478}
6479
6480/*
6481 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04006482struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006483nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484{
6485 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486 struct nfs4_client_reclaim *crp = NULL;
6487
Jeff Layton278c9312012-11-12 15:00:52 -05006488 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489
Jeff Layton278c9312012-11-12 15:00:52 -05006490 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006491 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05006492 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493 return crp;
6494 }
6495 }
6496 return NULL;
6497}
6498
6499/*
6500* Called from OPEN. Look for clientid in reclaim list.
6501*/
Al Virob37ad282006-10-19 23:28:59 -07006502__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006503nfs4_check_open_reclaim(clientid_t *clid,
6504 struct nfsd4_compound_state *cstate,
6505 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006507 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04006508
6509 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006510 status = lookup_clientid(clid, cstate, nn);
6511 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04006512 return nfserr_reclaim_bad;
6513
Jeff Layton3b3e7b72014-09-12 16:40:22 -04006514 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6515 return nfserr_no_grace;
6516
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006517 if (nfsd4_client_record_check(cstate->clp))
6518 return nfserr_reclaim_bad;
6519
6520 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521}
6522
Bryan Schumaker65178db2011-11-01 13:35:21 -04006523#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04006524static inline void
6525put_client(struct nfs4_client *clp)
6526{
6527 atomic_dec(&clp->cl_refcount);
6528}
6529
Jeff Layton285abde2014-07-30 08:27:24 -04006530static struct nfs4_client *
6531nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6532{
6533 struct nfs4_client *clp;
6534 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6535 nfsd_net_id);
6536
6537 if (!nfsd_netns_ready(nn))
6538 return NULL;
6539
6540 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6541 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
6542 return clp;
6543 }
6544 return NULL;
6545}
6546
Jeff Layton7ec0e362014-07-30 08:27:17 -04006547u64
Jeff Layton285abde2014-07-30 08:27:24 -04006548nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04006549{
6550 struct nfs4_client *clp;
6551 u64 count = 0;
6552 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6553 nfsd_net_id);
6554 char buf[INET6_ADDRSTRLEN];
6555
6556 if (!nfsd_netns_ready(nn))
6557 return 0;
6558
6559 spin_lock(&nn->client_lock);
6560 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6561 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6562 pr_info("NFS Client: %s\n", buf);
6563 ++count;
6564 }
6565 spin_unlock(&nn->client_lock);
6566
6567 return count;
6568}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006569
Jeff Laytona0926d12014-07-30 08:27:18 -04006570u64
Jeff Layton285abde2014-07-30 08:27:24 -04006571nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04006572{
6573 u64 count = 0;
6574 struct nfs4_client *clp;
6575 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6576 nfsd_net_id);
6577
6578 if (!nfsd_netns_ready(nn))
6579 return count;
6580
6581 spin_lock(&nn->client_lock);
6582 clp = nfsd_find_client(addr, addr_size);
6583 if (clp) {
6584 if (mark_client_expired_locked(clp) == nfs_ok)
6585 ++count;
6586 else
6587 clp = NULL;
6588 }
6589 spin_unlock(&nn->client_lock);
6590
6591 if (clp)
6592 expire_client(clp);
6593
6594 return count;
6595}
6596
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006597u64
Jeff Layton285abde2014-07-30 08:27:24 -04006598nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006599{
6600 u64 count = 0;
6601 struct nfs4_client *clp, *next;
6602 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6603 nfsd_net_id);
6604 LIST_HEAD(reaplist);
6605
6606 if (!nfsd_netns_ready(nn))
6607 return count;
6608
6609 spin_lock(&nn->client_lock);
6610 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6611 if (mark_client_expired_locked(clp) == nfs_ok) {
6612 list_add(&clp->cl_lru, &reaplist);
6613 if (max != 0 && ++count >= max)
6614 break;
6615 }
6616 }
6617 spin_unlock(&nn->client_lock);
6618
6619 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6620 expire_client(clp);
6621
6622 return count;
6623}
6624
Bryan Schumaker184c1842012-11-29 11:40:44 -05006625static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6626 const char *type)
6627{
6628 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05006629 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05006630 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6631}
6632
Jeff Layton016200c2014-07-30 08:27:21 -04006633static void
6634nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6635 struct list_head *collect)
6636{
6637 struct nfs4_client *clp = lst->st_stid.sc_client;
6638 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6639 nfsd_net_id);
6640
6641 if (!collect)
6642 return;
6643
6644 lockdep_assert_held(&nn->client_lock);
6645 atomic_inc(&clp->cl_refcount);
6646 list_add(&lst->st_locks, collect);
6647}
6648
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006649static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04006650 struct list_head *collect,
Jeff Laytone8568732015-08-24 12:41:47 -04006651 bool (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05006652{
6653 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006654 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006655 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006656 u64 count = 0;
6657
Jeff Layton016200c2014-07-30 08:27:21 -04006658 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006659 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006660 list_for_each_entry_safe(stp, st_next,
6661 &oop->oo_owner.so_stateids, st_perstateowner) {
6662 list_for_each_entry_safe(lst, lst_next,
6663 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04006664 if (func) {
Jeff Laytone8568732015-08-24 12:41:47 -04006665 if (func(lst))
6666 nfsd_inject_add_lock_to_list(lst,
6667 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04006668 }
Jeff Layton016200c2014-07-30 08:27:21 -04006669 ++count;
6670 /*
6671 * Despite the fact that these functions deal
6672 * with 64-bit integers for "count", we must
6673 * ensure that it doesn't blow up the
6674 * clp->cl_refcount. Throw a warning if we
6675 * start to approach INT_MAX here.
6676 */
6677 WARN_ON_ONCE(count == (INT_MAX / 2));
6678 if (count == max)
6679 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006680 }
6681 }
6682 }
Jeff Layton016200c2014-07-30 08:27:21 -04006683out:
6684 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006685
6686 return count;
6687}
6688
Jeff Layton016200c2014-07-30 08:27:21 -04006689static u64
6690nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6691 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05006692{
Jeff Layton016200c2014-07-30 08:27:21 -04006693 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006694}
6695
Jeff Layton016200c2014-07-30 08:27:21 -04006696static u64
6697nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006698{
Jeff Layton016200c2014-07-30 08:27:21 -04006699 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006700 nfsd_print_count(clp, count, "locked files");
6701 return count;
6702}
6703
Jeff Layton016200c2014-07-30 08:27:21 -04006704u64
Jeff Layton285abde2014-07-30 08:27:24 -04006705nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04006706{
6707 struct nfs4_client *clp;
6708 u64 count = 0;
6709 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6710 nfsd_net_id);
6711
6712 if (!nfsd_netns_ready(nn))
6713 return 0;
6714
6715 spin_lock(&nn->client_lock);
6716 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6717 count += nfsd_print_client_locks(clp);
6718 spin_unlock(&nn->client_lock);
6719
6720 return count;
6721}
6722
6723static void
6724nfsd_reap_locks(struct list_head *reaplist)
6725{
6726 struct nfs4_client *clp;
6727 struct nfs4_ol_stateid *stp, *next;
6728
6729 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6730 list_del_init(&stp->st_locks);
6731 clp = stp->st_stid.sc_client;
6732 nfs4_put_stid(&stp->st_stid);
6733 put_client(clp);
6734 }
6735}
6736
6737u64
Jeff Layton285abde2014-07-30 08:27:24 -04006738nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04006739{
6740 unsigned int count = 0;
6741 struct nfs4_client *clp;
6742 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6743 nfsd_net_id);
6744 LIST_HEAD(reaplist);
6745
6746 if (!nfsd_netns_ready(nn))
6747 return count;
6748
6749 spin_lock(&nn->client_lock);
6750 clp = nfsd_find_client(addr, addr_size);
6751 if (clp)
6752 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6753 spin_unlock(&nn->client_lock);
6754 nfsd_reap_locks(&reaplist);
6755 return count;
6756}
6757
6758u64
Jeff Layton285abde2014-07-30 08:27:24 -04006759nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04006760{
6761 u64 count = 0;
6762 struct nfs4_client *clp;
6763 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6764 nfsd_net_id);
6765 LIST_HEAD(reaplist);
6766
6767 if (!nfsd_netns_ready(nn))
6768 return count;
6769
6770 spin_lock(&nn->client_lock);
6771 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6772 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6773 if (max != 0 && count >= max)
6774 break;
6775 }
6776 spin_unlock(&nn->client_lock);
6777 nfsd_reap_locks(&reaplist);
6778 return count;
6779}
6780
Jeff Layton82e05ef2014-07-30 08:27:22 -04006781static u64
6782nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6783 struct list_head *collect,
6784 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006785{
6786 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04006787 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6788 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006789 u64 count = 0;
6790
Jeff Layton82e05ef2014-07-30 08:27:22 -04006791 lockdep_assert_held(&nn->client_lock);
6792
6793 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006794 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04006795 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006796 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04006797 if (collect) {
6798 atomic_inc(&clp->cl_refcount);
6799 list_add(&oop->oo_perclient, collect);
6800 }
6801 }
6802 ++count;
6803 /*
6804 * Despite the fact that these functions deal with
6805 * 64-bit integers for "count", we must ensure that
6806 * it doesn't blow up the clp->cl_refcount. Throw a
6807 * warning if we start to approach INT_MAX here.
6808 */
6809 WARN_ON_ONCE(count == (INT_MAX / 2));
6810 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006811 break;
6812 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04006813 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006814
6815 return count;
6816}
6817
Jeff Layton82e05ef2014-07-30 08:27:22 -04006818static u64
6819nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006820{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006821 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6822
6823 nfsd_print_count(clp, count, "openowners");
6824 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006825}
6826
Jeff Layton82e05ef2014-07-30 08:27:22 -04006827static u64
6828nfsd_collect_client_openowners(struct nfs4_client *clp,
6829 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006830{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006831 return nfsd_foreach_client_openowner(clp, max, collect,
6832 unhash_openowner_locked);
6833}
6834
6835u64
Jeff Layton285abde2014-07-30 08:27:24 -04006836nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006837{
6838 struct nfs4_client *clp;
6839 u64 count = 0;
6840 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6841 nfsd_net_id);
6842
6843 if (!nfsd_netns_ready(nn))
6844 return 0;
6845
6846 spin_lock(&nn->client_lock);
6847 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6848 count += nfsd_print_client_openowners(clp);
6849 spin_unlock(&nn->client_lock);
6850
6851 return count;
6852}
6853
6854static void
6855nfsd_reap_openowners(struct list_head *reaplist)
6856{
6857 struct nfs4_client *clp;
6858 struct nfs4_openowner *oop, *next;
6859
6860 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6861 list_del_init(&oop->oo_perclient);
6862 clp = oop->oo_owner.so_client;
6863 release_openowner(oop);
6864 put_client(clp);
6865 }
6866}
6867
6868u64
Jeff Layton285abde2014-07-30 08:27:24 -04006869nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6870 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006871{
6872 unsigned int count = 0;
6873 struct nfs4_client *clp;
6874 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6875 nfsd_net_id);
6876 LIST_HEAD(reaplist);
6877
6878 if (!nfsd_netns_ready(nn))
6879 return count;
6880
6881 spin_lock(&nn->client_lock);
6882 clp = nfsd_find_client(addr, addr_size);
6883 if (clp)
6884 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6885 spin_unlock(&nn->client_lock);
6886 nfsd_reap_openowners(&reaplist);
6887 return count;
6888}
6889
6890u64
Jeff Layton285abde2014-07-30 08:27:24 -04006891nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006892{
6893 u64 count = 0;
6894 struct nfs4_client *clp;
6895 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6896 nfsd_net_id);
6897 LIST_HEAD(reaplist);
6898
6899 if (!nfsd_netns_ready(nn))
6900 return count;
6901
6902 spin_lock(&nn->client_lock);
6903 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6904 count += nfsd_collect_client_openowners(clp, &reaplist,
6905 max - count);
6906 if (max != 0 && count >= max)
6907 break;
6908 }
6909 spin_unlock(&nn->client_lock);
6910 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006911 return count;
6912}
6913
Bryan Schumaker269de302012-11-29 11:40:42 -05006914static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6915 struct list_head *victims)
6916{
6917 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006918 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6919 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006920 u64 count = 0;
6921
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006922 lockdep_assert_held(&nn->client_lock);
6923
6924 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006925 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006926 if (victims) {
6927 /*
6928 * It's not safe to mess with delegations that have a
6929 * non-zero dl_time. They might have already been broken
6930 * and could be processed by the laundromat outside of
6931 * the state_lock. Just leave them be.
6932 */
6933 if (dp->dl_time != 0)
6934 continue;
6935
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006936 atomic_inc(&clp->cl_refcount);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04006937 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04006938 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006939 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006940 ++count;
6941 /*
6942 * Despite the fact that these functions deal with
6943 * 64-bit integers for "count", we must ensure that
6944 * it doesn't blow up the clp->cl_refcount. Throw a
6945 * warning if we start to approach INT_MAX here.
6946 */
6947 WARN_ON_ONCE(count == (INT_MAX / 2));
6948 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006949 break;
6950 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006951 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006952 return count;
6953}
6954
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006955static u64
6956nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006957{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006958 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006959
6960 nfsd_print_count(clp, count, "delegations");
6961 return count;
6962}
6963
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006964u64
Jeff Layton285abde2014-07-30 08:27:24 -04006965nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006966{
6967 struct nfs4_client *clp;
6968 u64 count = 0;
6969 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6970 nfsd_net_id);
6971
6972 if (!nfsd_netns_ready(nn))
6973 return 0;
6974
6975 spin_lock(&nn->client_lock);
6976 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6977 count += nfsd_print_client_delegations(clp);
6978 spin_unlock(&nn->client_lock);
6979
6980 return count;
6981}
6982
6983static void
6984nfsd_forget_delegations(struct list_head *reaplist)
6985{
6986 struct nfs4_client *clp;
6987 struct nfs4_delegation *dp, *next;
6988
6989 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6990 list_del_init(&dp->dl_recall_lru);
6991 clp = dp->dl_stid.sc_client;
6992 revoke_delegation(dp);
6993 put_client(clp);
6994 }
6995}
6996
6997u64
Jeff Layton285abde2014-07-30 08:27:24 -04006998nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6999 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007000{
7001 u64 count = 0;
7002 struct nfs4_client *clp;
7003 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7004 nfsd_net_id);
7005 LIST_HEAD(reaplist);
7006
7007 if (!nfsd_netns_ready(nn))
7008 return count;
7009
7010 spin_lock(&nn->client_lock);
7011 clp = nfsd_find_client(addr, addr_size);
7012 if (clp)
7013 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7014 spin_unlock(&nn->client_lock);
7015
7016 nfsd_forget_delegations(&reaplist);
7017 return count;
7018}
7019
7020u64
Jeff Layton285abde2014-07-30 08:27:24 -04007021nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007022{
7023 u64 count = 0;
7024 struct nfs4_client *clp;
7025 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7026 nfsd_net_id);
7027 LIST_HEAD(reaplist);
7028
7029 if (!nfsd_netns_ready(nn))
7030 return count;
7031
7032 spin_lock(&nn->client_lock);
7033 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7034 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7035 if (max != 0 && count >= max)
7036 break;
7037 }
7038 spin_unlock(&nn->client_lock);
7039 nfsd_forget_delegations(&reaplist);
7040 return count;
7041}
7042
7043static void
7044nfsd_recall_delegations(struct list_head *reaplist)
7045{
7046 struct nfs4_client *clp;
7047 struct nfs4_delegation *dp, *next;
7048
7049 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7050 list_del_init(&dp->dl_recall_lru);
7051 clp = dp->dl_stid.sc_client;
7052 /*
7053 * We skipped all entries that had a zero dl_time before,
7054 * so we can now reset the dl_time back to 0. If a delegation
7055 * break comes in now, then it won't make any difference since
7056 * we're recalling it either way.
7057 */
7058 spin_lock(&state_lock);
7059 dp->dl_time = 0;
7060 spin_unlock(&state_lock);
7061 nfsd_break_one_deleg(dp);
7062 put_client(clp);
7063 }
7064}
7065
7066u64
Jeff Layton285abde2014-07-30 08:27:24 -04007067nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007068 size_t addr_size)
7069{
7070 u64 count = 0;
7071 struct nfs4_client *clp;
7072 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7073 nfsd_net_id);
7074 LIST_HEAD(reaplist);
7075
7076 if (!nfsd_netns_ready(nn))
7077 return count;
7078
7079 spin_lock(&nn->client_lock);
7080 clp = nfsd_find_client(addr, addr_size);
7081 if (clp)
7082 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7083 spin_unlock(&nn->client_lock);
7084
7085 nfsd_recall_delegations(&reaplist);
7086 return count;
7087}
7088
7089u64
Jeff Layton285abde2014-07-30 08:27:24 -04007090nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007091{
7092 u64 count = 0;
7093 struct nfs4_client *clp, *next;
7094 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7095 nfsd_net_id);
7096 LIST_HEAD(reaplist);
7097
7098 if (!nfsd_netns_ready(nn))
7099 return count;
7100
7101 spin_lock(&nn->client_lock);
7102 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7103 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7104 if (max != 0 && ++count >= max)
7105 break;
7106 }
7107 spin_unlock(&nn->client_lock);
7108 nfsd_recall_delegations(&reaplist);
7109 return count;
7110}
Bryan Schumaker65178db2011-11-01 13:35:21 -04007111#endif /* CONFIG_NFSD_FAULT_INJECTION */
7112
Meelap Shahc2f1a552007-07-17 04:04:39 -07007113/*
7114 * Since the lifetime of a delegation isn't limited to that of an open, a
7115 * client may quite reasonably hang on to a delegation as long as it has
7116 * the inode cached. This becomes an obvious problem the first time a
7117 * client's inode cache approaches the size of the server's total memory.
7118 *
7119 * For now we avoid this problem by imposing a hard limit on the number
7120 * of delegations, which varies according to the server's memory size.
7121 */
7122static void
7123set_max_delegations(void)
7124{
7125 /*
7126 * Allow at most 4 delegations per megabyte of RAM. Quick
7127 * estimates suggest that in the worst case (where every delegation
7128 * is for a different inode), a delegation could take about 1.5K,
7129 * giving a worst case usage of about 6% of memory.
7130 */
7131 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7132}
7133
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007134static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007135{
7136 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7137 int i;
7138
7139 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
7140 CLIENT_HASH_SIZE, GFP_KERNEL);
7141 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007142 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007143 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
7144 CLIENT_HASH_SIZE, GFP_KERNEL);
7145 if (!nn->unconf_id_hashtbl)
7146 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007147 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
7148 SESSION_HASH_SIZE, GFP_KERNEL);
7149 if (!nn->sessionid_hashtbl)
7150 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007151
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007152 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007153 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007154 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007155 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007156 for (i = 0; i < SESSION_HASH_SIZE; i++)
7157 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007158 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007159 nn->unconf_name_tree = RB_ROOT;
Vasily Averin81833de2017-11-13 07:25:40 +03007160 nn->boot_time = get_seconds();
7161 nn->grace_ended = false;
7162 nn->nfsd4_manager.block_opens = true;
7163 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03007164 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03007165 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007166 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03007167 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007168
Jeff Layton0cc11a62016-10-20 09:34:31 -04007169 spin_lock_init(&nn->blocked_locks_lock);
7170 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7171
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007172 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007173 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007174
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007175 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007176
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007177err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03007178 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007179err_unconf_id:
7180 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007181err:
7182 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007183}
7184
7185static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007186nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007187{
7188 int i;
7189 struct nfs4_client *clp = NULL;
7190 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7191
7192 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7193 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7194 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7195 destroy_client(clp);
7196 }
7197 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007198
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007199 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7200
Kinglong Mee2b905632014-03-26 22:09:30 +08007201 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7202 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7203 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7204 destroy_client(clp);
7205 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007206 }
7207
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007208 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007209 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007210 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007211 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007212}
7213
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007214int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007215nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07007216{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04007217 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007218 int ret;
7219
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007220 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007221 if (ret)
7222 return ret;
Jeff Laytond4318ac2014-09-12 16:40:21 -04007223 locks_start_grace(net, &nn->nfsd4_manager);
7224 nfsd4_client_tracking_init(net);
Vasily Averin7e981a82017-11-06 16:46:04 +03007225 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %x)\n",
7226 nn->nfsd4_grace, net->ns.inum);
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03007227 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007228 return 0;
7229}
7230
7231/* initialization to perform when the nfsd service is started: */
7232
7233int
7234nfs4_state_start(void)
7235{
7236 int ret;
7237
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007238 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007239 if (ret)
Kinglong Meef7d1ddb2017-02-05 09:57:07 +08007240 return ret;
7241
Jeff Layton51a54562015-08-20 07:17:01 -04007242 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05007243 if (laundry_wq == NULL) {
7244 ret = -ENOMEM;
Kinglong Meef7d1ddb2017-02-05 09:57:07 +08007245 goto out_cleanup_cred;
Jeff Laytona6d6b782012-03-05 11:42:36 -05007246 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007247 ret = nfsd4_create_callback_queue();
7248 if (ret)
7249 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007250
Meelap Shahc2f1a552007-07-17 04:04:39 -07007251 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007252 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007253
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007254out_free_laundry:
7255 destroy_workqueue(laundry_wq);
Kinglong Meef7d1ddb2017-02-05 09:57:07 +08007256out_cleanup_cred:
7257 cleanup_callback_cred();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007258 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007259}
7260
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007261void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007262nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007263{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007264 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007265 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007266 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007267
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007268 cancel_delayed_work_sync(&nn->laundromat_work);
7269 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05007270
Linus Torvalds1da177e2005-04-16 15:20:36 -07007271 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04007272 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007273 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007274 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007275 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007276 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007277 }
Benny Halevycdc97502014-05-30 09:09:30 -04007278 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279 list_for_each_safe(pos, next, &reaplist) {
7280 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04007281 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05007282 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283 }
7284
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007285 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007286 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287}
7288
7289void
7290nfs4_state_shutdown(void)
7291{
NeilBrown5e8d5c22006-04-10 22:55:37 -07007292 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04007293 nfsd4_destroy_callback_queue();
Kinglong Meef7d1ddb2017-02-05 09:57:07 +08007294 cleanup_callback_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007296
7297static void
7298get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7299{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007300 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
7301 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007302}
7303
7304static void
7305put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7306{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007307 if (cstate->minorversion) {
7308 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7309 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7310 }
7311}
7312
7313void
7314clear_current_stateid(struct nfsd4_compound_state *cstate)
7315{
7316 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007317}
7318
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007319/*
7320 * functions to set current state id
7321 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007322void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007323nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7324 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007325{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007326 put_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007327}
7328
7329void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007330nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7331 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007332{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007333 put_stateid(cstate, &u->open.op_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007334}
7335
7336void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007337nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7338 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007339{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007340 put_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007341}
7342
7343void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007344nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7345 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007346{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007347 put_stateid(cstate, &u->lock.lk_resp_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007348}
7349
7350/*
7351 * functions to consume current state id
7352 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007353
7354void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007355nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7356 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007357{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007358 get_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007359}
7360
7361void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007362nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7363 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007364{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007365 get_stateid(cstate, &u->delegreturn.dr_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007366}
7367
7368void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007369nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7370 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007371{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007372 get_stateid(cstate, &u->free_stateid.fr_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007373}
7374
7375void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007376nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7377 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007378{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007379 get_stateid(cstate, &u->setattr.sa_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007380}
7381
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007382void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007383nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7384 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007385{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007386 get_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007387}
7388
7389void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007390nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7391 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007392{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007393 get_stateid(cstate, &u->locku.lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007394}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007395
7396void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007397nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7398 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007399{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007400 get_stateid(cstate, &u->read.rd_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007401}
7402
7403void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007404nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7405 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007406{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007407 get_stateid(cstate, &u->write.wr_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007408}