blob: dfe4b596c2e57589f573216a7b3678381add1ef3 [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);
Scott Mayhew362063a2019-03-26 18:06:28 -040080void nfsd4_end_grace(struct nfsd_net *nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
J. Bruce Fields8b671b82009-02-22 14:51:34 -080082/* Locking: */
83
J. Bruce Fields8b671b82009-02-22 14:51:34 -080084/*
85 * Currently used for the del_recall_lru and file hash table. In an
86 * effort to decrease the scope of the client_mutex, this spinlock may
87 * eventually cover more:
88 */
Benny Halevycdc975052014-05-30 09:09:30 -040089static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080090
Andrew Elble4f34bd02017-11-08 17:29:51 -050091enum nfsd4_st_mutex_lock_subclass {
92 OPEN_STATEID_MUTEX = 0,
93 LOCK_STATEID_MUTEX = 1,
94};
95
Jeff Laytonb401be222014-07-29 21:34:33 -040096/*
97 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
98 * the refcount on the open stateid to drop.
99 */
100static DECLARE_WAIT_QUEUE_HEAD(close_wq);
101
Jeff Layton9258a2d2018-03-16 09:47:22 -0400102static struct kmem_cache *client_slab;
Christoph Hellwigabf11352014-05-21 07:43:03 -0700103static struct kmem_cache *openowner_slab;
104static struct kmem_cache *lockowner_slab;
105static struct kmem_cache *file_slab;
106static struct kmem_cache *stateid_slab;
107static struct kmem_cache *deleg_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +0200108static struct kmem_cache *odstate_slab;
NeilBrowne60d4392005-06-23 22:03:01 -0700109
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400110static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800111
Julia Lawallc4cb8972015-11-21 22:57:39 +0100112static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
Jeff Layton76d348f2016-09-16 16:28:24 -0400113static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200114
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400115static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800116{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400117 return ses->se_flags & NFS4_SESSION_DEAD;
118}
119
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400120static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
121{
122 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400123 return nfserr_jukebox;
124 ses->se_flags |= NFS4_SESSION_DEAD;
125 return nfs_ok;
126}
127
J. Bruce Fields221a6872013-04-01 22:23:49 -0400128static bool is_client_expired(struct nfs4_client *clp)
129{
130 return clp->cl_time == 0;
131}
132
J. Bruce Fields221a6872013-04-01 22:23:49 -0400133static __be32 get_client_locked(struct nfs4_client *clp)
134{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400135 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
136
137 lockdep_assert_held(&nn->client_lock);
138
J. Bruce Fields221a6872013-04-01 22:23:49 -0400139 if (is_client_expired(clp))
140 return nfserr_expired;
141 atomic_inc(&clp->cl_refcount);
142 return nfs_ok;
143}
144
145/* must be called under the client_lock */
146static inline void
147renew_client_locked(struct nfs4_client *clp)
148{
149 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
150
151 if (is_client_expired(clp)) {
152 WARN_ON(1);
153 printk("%s: client (clientid %08x/%08x) already expired\n",
154 __func__,
155 clp->cl_clientid.cl_boot,
156 clp->cl_clientid.cl_id);
157 return;
158 }
159
160 dprintk("renewing client (clientid %08x/%08x)\n",
161 clp->cl_clientid.cl_boot,
162 clp->cl_clientid.cl_id);
163 list_move_tail(&clp->cl_lru, &nn->client_lru);
164 clp->cl_time = get_seconds();
165}
166
Fengguang Wuba138432013-04-16 22:14:15 -0400167static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400168{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400169 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
170
171 lockdep_assert_held(&nn->client_lock);
172
J. Bruce Fields221a6872013-04-01 22:23:49 -0400173 if (!atomic_dec_and_test(&clp->cl_refcount))
174 return;
175 if (!is_client_expired(clp))
176 renew_client_locked(clp);
177}
178
Jeff Layton4b24ca72014-06-30 11:48:44 -0400179static void put_client_renew(struct nfs4_client *clp)
180{
181 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
182
Jeff Laytond6c249b2014-07-08 14:02:50 -0400183 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
184 return;
185 if (!is_client_expired(clp))
186 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400187 spin_unlock(&nn->client_lock);
188}
189
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400190static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
191{
192 __be32 status;
193
194 if (is_session_dead(ses))
195 return nfserr_badsession;
196 status = get_client_locked(ses->se_client);
197 if (status)
198 return status;
199 atomic_inc(&ses->se_ref);
200 return nfs_ok;
201}
202
203static void nfsd4_put_session_locked(struct nfsd4_session *ses)
204{
205 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400206 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
207
208 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400209
210 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
211 free_session(ses);
212 put_client_renew_locked(clp);
213}
214
215static void nfsd4_put_session(struct nfsd4_session *ses)
216{
217 struct nfs4_client *clp = ses->se_client;
218 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
219
220 spin_lock(&nn->client_lock);
221 nfsd4_put_session_locked(ses);
222 spin_unlock(&nn->client_lock);
223}
224
Jeff Layton76d348f2016-09-16 16:28:24 -0400225static struct nfsd4_blocked_lock *
226find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
227 struct nfsd_net *nn)
228{
229 struct nfsd4_blocked_lock *cur, *found = NULL;
230
Jeff Layton0cc11a62016-10-20 09:34:31 -0400231 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400232 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
233 if (fh_match(fh, &cur->nbl_fh)) {
234 list_del_init(&cur->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -0400235 list_del_init(&cur->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400236 found = cur;
237 break;
238 }
239 }
Jeff Layton0cc11a62016-10-20 09:34:31 -0400240 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400241 if (found)
NeilBrowncb03f942018-11-30 10:04:08 +1100242 locks_delete_block(&found->nbl_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400243 return found;
244}
245
246static struct nfsd4_blocked_lock *
247find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
248 struct nfsd_net *nn)
249{
250 struct nfsd4_blocked_lock *nbl;
251
252 nbl = find_blocked_lock(lo, fh, nn);
253 if (!nbl) {
254 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
255 if (nbl) {
256 fh_copy_shallow(&nbl->nbl_fh, fh);
257 locks_init_lock(&nbl->nbl_lock);
258 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
259 &nfsd4_cb_notify_lock_ops,
260 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
261 }
262 }
263 return nbl;
264}
265
266static void
267free_blocked_lock(struct nfsd4_blocked_lock *nbl)
268{
Jeff Layton6aaafc42019-04-22 12:34:23 -0400269 locks_delete_block(&nbl->nbl_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400270 locks_release_private(&nbl->nbl_lock);
271 kfree(nbl);
272}
273
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400274static void
275remove_blocked_locks(struct nfs4_lockowner *lo)
276{
277 struct nfs4_client *clp = lo->lo_owner.so_client;
278 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
279 struct nfsd4_blocked_lock *nbl;
280 LIST_HEAD(reaplist);
281
282 /* Dequeue all blocked locks */
283 spin_lock(&nn->blocked_locks_lock);
284 while (!list_empty(&lo->lo_blocked)) {
285 nbl = list_first_entry(&lo->lo_blocked,
286 struct nfsd4_blocked_lock,
287 nbl_list);
288 list_del_init(&nbl->nbl_list);
289 list_move(&nbl->nbl_lru, &reaplist);
290 }
291 spin_unlock(&nn->blocked_locks_lock);
292
293 /* Now free them */
294 while (!list_empty(&reaplist)) {
295 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
296 nbl_lru);
297 list_del_init(&nbl->nbl_lru);
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400298 free_blocked_lock(nbl);
299 }
300}
301
Jeff Laytonf4564582019-04-22 12:34:24 -0400302static void
303nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
304{
305 struct nfsd4_blocked_lock *nbl = container_of(cb,
306 struct nfsd4_blocked_lock, nbl_cb);
307 locks_delete_block(&nbl->nbl_lock);
308}
309
Jeff Layton76d348f2016-09-16 16:28:24 -0400310static int
311nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
312{
313 /*
314 * Since this is just an optimization, we don't try very hard if it
315 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
316 * just quit trying on anything else.
317 */
318 switch (task->tk_status) {
319 case -NFS4ERR_DELAY:
320 rpc_delay(task, 1 * HZ);
321 return 0;
322 default:
323 return 1;
324 }
325}
326
327static void
328nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
329{
330 struct nfsd4_blocked_lock *nbl = container_of(cb,
331 struct nfsd4_blocked_lock, nbl_cb);
332
333 free_blocked_lock(nbl);
334}
335
336static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
Jeff Laytonf4564582019-04-22 12:34:24 -0400337 .prepare = nfsd4_cb_notify_lock_prepare,
Jeff Layton76d348f2016-09-16 16:28:24 -0400338 .done = nfsd4_cb_notify_lock_done,
339 .release = nfsd4_cb_notify_lock_release,
340};
341
Kinglong Meeb5971af2014-08-22 10:18:43 -0400342static inline struct nfs4_stateowner *
343nfs4_get_stateowner(struct nfs4_stateowner *sop)
344{
345 atomic_inc(&sop->so_count);
346 return sop;
347}
348
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400349static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400350same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400351{
352 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400353 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400354}
355
356static struct nfs4_openowner *
357find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400358 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400359{
360 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400361
Trond Myklebustd4f04892014-07-29 21:34:36 -0400362 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400363
Trond Myklebustd4f04892014-07-29 21:34:36 -0400364 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
365 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400366 if (!so->so_is_open_owner)
367 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400368 if (same_owner_str(so, &open->op_owner))
369 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400370 }
371 return NULL;
372}
373
374static struct nfs4_openowner *
375find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400376 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400377{
378 struct nfs4_openowner *oo;
379
Trond Myklebustd4f04892014-07-29 21:34:36 -0400380 spin_lock(&clp->cl_lock);
381 oo = find_openstateowner_str_locked(hashval, open, clp);
382 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400383 return oo;
384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386static inline u32
387opaque_hashval(const void *ptr, int nbytes)
388{
389 unsigned char *cptr = (unsigned char *) ptr;
390
391 u32 x = 0;
392 while (nbytes--) {
393 x *= 37;
394 x += *cptr++;
395 }
396 return x;
397}
398
Jeff Layton5b095e92014-10-23 08:01:02 -0400399static void nfsd4_free_file_rcu(struct rcu_head *rcu)
J. Bruce Fields32513b42011-10-13 16:00:16 -0400400{
Jeff Layton5b095e92014-10-23 08:01:02 -0400401 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
402
403 kmem_cache_free(file_slab, fp);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400404}
405
Christoph Hellwige6ba76e2014-08-14 08:50:16 +0200406void
NeilBrown13cd2182005-06-23 22:03:10 -0700407put_nfs4_file(struct nfs4_file *fi)
408{
Jeff Layton02e12152014-07-16 10:31:57 -0400409 might_lock(&state_lock);
410
Elena Reshetova818a34e2017-10-20 12:53:30 +0300411 if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton5b095e92014-10-23 08:01:02 -0400412 hlist_del_rcu(&fi->fi_hash);
Benny Halevycdc975052014-05-30 09:09:30 -0400413 spin_unlock(&state_lock);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200414 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
Jeff Layton5b095e92014-10-23 08:01:02 -0400415 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
416 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800417 }
NeilBrown13cd2182005-06-23 22:03:10 -0700418}
419
Trond Myklebustde186432014-07-10 14:07:26 -0400420static struct file *
421__nfs4_get_fd(struct nfs4_file *f, int oflag)
422{
423 if (f->fi_fds[oflag])
424 return get_file(f->fi_fds[oflag]);
425 return NULL;
426}
427
428static struct file *
429find_writeable_file_locked(struct nfs4_file *f)
430{
431 struct file *ret;
432
433 lockdep_assert_held(&f->fi_lock);
434
435 ret = __nfs4_get_fd(f, O_WRONLY);
436 if (!ret)
437 ret = __nfs4_get_fd(f, O_RDWR);
438 return ret;
439}
440
441static struct file *
442find_writeable_file(struct nfs4_file *f)
443{
444 struct file *ret;
445
446 spin_lock(&f->fi_lock);
447 ret = find_writeable_file_locked(f);
448 spin_unlock(&f->fi_lock);
449
450 return ret;
451}
452
453static struct file *find_readable_file_locked(struct nfs4_file *f)
454{
455 struct file *ret;
456
457 lockdep_assert_held(&f->fi_lock);
458
459 ret = __nfs4_get_fd(f, O_RDONLY);
460 if (!ret)
461 ret = __nfs4_get_fd(f, O_RDWR);
462 return ret;
463}
464
465static struct file *
466find_readable_file(struct nfs4_file *f)
467{
468 struct file *ret;
469
470 spin_lock(&f->fi_lock);
471 ret = find_readable_file_locked(f);
472 spin_unlock(&f->fi_lock);
473
474 return ret;
475}
476
Christoph Hellwig4d227fc2014-08-17 07:40:00 -0500477struct file *
Trond Myklebustde186432014-07-10 14:07:26 -0400478find_any_file(struct nfs4_file *f)
479{
480 struct file *ret;
481
482 spin_lock(&f->fi_lock);
483 ret = __nfs4_get_fd(f, O_RDWR);
484 if (!ret) {
485 ret = __nfs4_get_fd(f, O_WRONLY);
486 if (!ret)
487 ret = __nfs4_get_fd(f, O_RDONLY);
488 }
489 spin_unlock(&f->fi_lock);
490 return ret;
491}
492
Trond Myklebust02a35082014-07-25 07:34:22 -0400493static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800494unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700495
496/*
497 * Open owner state (share locks)
498 */
499
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500500/* hash tables for lock and open owners */
501#define OWNER_HASH_BITS 8
502#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
503#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700504
Trond Myklebustd4f04892014-07-29 21:34:36 -0400505static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400506{
507 unsigned int ret;
508
509 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500510 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400511}
NeilBrownef0f3392006-04-10 22:55:41 -0700512
NeilBrownef0f3392006-04-10 22:55:41 -0700513/* hash table for nfs4_file */
514#define FILE_HASH_BITS 8
515#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800516
Trond Myklebustca943212014-07-23 16:17:39 -0400517static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400518{
Trond Myklebustca943212014-07-23 16:17:39 -0400519 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
520}
521
522static unsigned int file_hashval(struct knfsd_fh *fh)
523{
524 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
525}
526
Jeff Layton89876f82013-04-02 09:01:59 -0400527static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700528
Jeff Layton12659652014-07-10 14:07:28 -0400529static void
530__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400531{
Jeff Layton7214e862014-07-10 14:07:33 -0400532 lockdep_assert_held(&fp->fi_lock);
533
Jeff Layton12659652014-07-10 14:07:28 -0400534 if (access & NFS4_SHARE_ACCESS_WRITE)
535 atomic_inc(&fp->fi_access[O_WRONLY]);
536 if (access & NFS4_SHARE_ACCESS_READ)
537 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400538}
539
Jeff Layton12659652014-07-10 14:07:28 -0400540static __be32
541nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400542{
Jeff Layton7214e862014-07-10 14:07:33 -0400543 lockdep_assert_held(&fp->fi_lock);
544
Jeff Layton12659652014-07-10 14:07:28 -0400545 /* Does this access mode make sense? */
546 if (access & ~NFS4_SHARE_ACCESS_BOTH)
547 return nfserr_inval;
548
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400549 /* Does it conflict with a deny mode already set? */
550 if ((access & fp->fi_share_deny) != 0)
551 return nfserr_share_denied;
552
Jeff Layton12659652014-07-10 14:07:28 -0400553 __nfs4_file_get_access(fp, access);
554 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400555}
556
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400557static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
558{
559 /* Common case is that there is no deny mode. */
560 if (deny) {
561 /* Does this deny mode make sense? */
562 if (deny & ~NFS4_SHARE_DENY_BOTH)
563 return nfserr_inval;
564
565 if ((deny & NFS4_SHARE_DENY_READ) &&
566 atomic_read(&fp->fi_access[O_RDONLY]))
567 return nfserr_share_denied;
568
569 if ((deny & NFS4_SHARE_DENY_WRITE) &&
570 atomic_read(&fp->fi_access[O_WRONLY]))
571 return nfserr_share_denied;
572 }
573 return nfs_ok;
574}
575
J. Bruce Fields998db522010-08-07 09:21:41 -0400576static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400577{
Trond Myklebustde186432014-07-10 14:07:26 -0400578 might_lock(&fp->fi_lock);
579
580 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
581 struct file *f1 = NULL;
582 struct file *f2 = NULL;
583
Jeff Layton6d338b52014-07-10 14:07:29 -0400584 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400585 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400586 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400587 spin_unlock(&fp->fi_lock);
588 if (f1)
589 fput(f1);
590 if (f2)
591 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400592 }
593}
594
Jeff Layton12659652014-07-10 14:07:28 -0400595static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400596{
Jeff Layton12659652014-07-10 14:07:28 -0400597 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
598
599 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400600 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400601 if (access & NFS4_SHARE_ACCESS_READ)
602 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400603}
604
Sachin Bhamare8287f002015-04-27 14:50:14 +0200605/*
606 * Allocate a new open/delegation state counter. This is needed for
607 * pNFS for proper return on close semantics.
608 *
609 * Note that we only allocate it for pNFS-enabled exports, otherwise
610 * all pointers to struct nfs4_clnt_odstate are always NULL.
611 */
612static struct nfs4_clnt_odstate *
613alloc_clnt_odstate(struct nfs4_client *clp)
614{
615 struct nfs4_clnt_odstate *co;
616
617 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
618 if (co) {
619 co->co_client = clp;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300620 refcount_set(&co->co_odcount, 1);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200621 }
622 return co;
623}
624
625static void
626hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
627{
628 struct nfs4_file *fp = co->co_file;
629
630 lockdep_assert_held(&fp->fi_lock);
631 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
632}
633
634static inline void
635get_clnt_odstate(struct nfs4_clnt_odstate *co)
636{
637 if (co)
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300638 refcount_inc(&co->co_odcount);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200639}
640
641static void
642put_clnt_odstate(struct nfs4_clnt_odstate *co)
643{
644 struct nfs4_file *fp;
645
646 if (!co)
647 return;
648
649 fp = co->co_file;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300650 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
Sachin Bhamare8287f002015-04-27 14:50:14 +0200651 list_del(&co->co_perfile);
652 spin_unlock(&fp->fi_lock);
653
654 nfsd4_return_all_file_layouts(co->co_client, fp);
655 kmem_cache_free(odstate_slab, co);
656 }
657}
658
659static struct nfs4_clnt_odstate *
660find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
661{
662 struct nfs4_clnt_odstate *co;
663 struct nfs4_client *cl;
664
665 if (!new)
666 return NULL;
667
668 cl = new->co_client;
669
670 spin_lock(&fp->fi_lock);
671 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
672 if (co->co_client == cl) {
673 get_clnt_odstate(co);
674 goto out;
675 }
676 }
677 co = new;
678 co->co_file = fp;
679 hash_clnt_odstate_locked(new);
680out:
681 spin_unlock(&fp->fi_lock);
682 return co;
683}
684
Kinglong Meed19fb702017-01-18 19:04:42 +0800685struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
686 void (*sc_free)(struct nfs4_stid *))
J. Bruce Fields996e0932011-10-17 11:14:48 -0400687{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500688 struct nfs4_stid *stid;
689 int new_id;
690
Trond Myklebustf8338832014-07-25 07:34:19 -0400691 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500692 if (!stid)
693 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400694
Jeff Layton4770d722014-07-29 21:34:10 -0400695 idr_preload(GFP_KERNEL);
696 spin_lock(&cl->cl_lock);
697 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
698 spin_unlock(&cl->cl_lock);
699 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700700 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500701 goto out_free;
Kinglong Meed19fb702017-01-18 19:04:42 +0800702
703 stid->sc_free = sc_free;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500704 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500705 stid->sc_stateid.si_opaque.so_id = new_id;
706 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
707 /* Will be incremented before return to client: */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300708 refcount_set(&stid->sc_count, 1);
Jeff Layton9767feb2015-10-01 09:05:50 -0400709 spin_lock_init(&stid->sc_lock);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500710
J. Bruce Fields996e0932011-10-17 11:14:48 -0400711 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500712 * It shouldn't be a problem to reuse an opaque stateid value.
713 * I don't think it is for 4.1. But with 4.0 I worry that, for
714 * example, a stray write retransmission could be accepted by
715 * the server when it should have been rejected. Therefore,
716 * adopt a trick from the sctp code to attempt to maximize the
717 * amount of time until an id is reused, by ensuring they always
718 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400719 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500720 return stid;
721out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800722 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500723 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400724}
725
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400726/*
727 * Create a unique stateid_t to represent each COPY.
728 */
729int nfs4_init_cp_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
730{
731 int new_id;
732
733 idr_preload(GFP_KERNEL);
734 spin_lock(&nn->s2s_cp_lock);
735 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, copy, 0, 0, GFP_NOWAIT);
736 spin_unlock(&nn->s2s_cp_lock);
737 idr_preload_end();
738 if (new_id < 0)
739 return 0;
740 copy->cp_stateid.si_opaque.so_id = new_id;
741 copy->cp_stateid.si_opaque.so_clid.cl_boot = nn->boot_time;
742 copy->cp_stateid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
743 return 1;
744}
745
746void nfs4_free_cp_state(struct nfsd4_copy *copy)
747{
748 struct nfsd_net *nn;
749
750 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
751 spin_lock(&nn->s2s_cp_lock);
752 idr_remove(&nn->s2s_cp_stateids, copy->cp_stateid.si_opaque.so_id);
753 spin_unlock(&nn->s2s_cp_lock);
754}
755
Jeff Laytonb49e0842014-07-29 21:34:11 -0400756static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400757{
Trond Myklebust60116952014-07-29 21:34:06 -0400758 struct nfs4_stid *stid;
Trond Myklebust60116952014-07-29 21:34:06 -0400759
Kinglong Meed19fb702017-01-18 19:04:42 +0800760 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
Trond Myklebust60116952014-07-29 21:34:06 -0400761 if (!stid)
762 return NULL;
763
Kinglong Meed19fb702017-01-18 19:04:42 +0800764 return openlockstateid(stid);
Trond Myklebust60116952014-07-29 21:34:06 -0400765}
766
767static void nfs4_free_deleg(struct nfs4_stid *stid)
768{
Trond Myklebust60116952014-07-29 21:34:06 -0400769 kmem_cache_free(deleg_slab, stid);
770 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400771}
772
NeilBrown6282cd52014-06-04 17:39:26 +1000773/*
774 * When we recall a delegation, we should be careful not to hand it
775 * out again straight away.
776 * To ensure this we keep a pair of bloom filters ('new' and 'old')
777 * in which the filehandles of recalled delegations are "stored".
778 * If a filehandle appear in either filter, a delegation is blocked.
779 * When a delegation is recalled, the filehandle is stored in the "new"
780 * filter.
781 * Every 30 seconds we swap the filters and clear the "new" one,
782 * unless both are empty of course.
783 *
784 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
785 * low 3 bytes as hash-table indices.
786 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400787 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000788 * is used to manage concurrent access. Testing does not need the lock
789 * except when swapping the two filters.
790 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400791static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000792static struct bloom_pair {
793 int entries, old_entries;
794 time_t swap_time;
795 int new; /* index into 'set' */
796 DECLARE_BITMAP(set[2], 256);
797} blocked_delegations;
798
799static int delegation_blocked(struct knfsd_fh *fh)
800{
801 u32 hash;
802 struct bloom_pair *bd = &blocked_delegations;
803
804 if (bd->entries == 0)
805 return 0;
806 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400807 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000808 if (seconds_since_boot() - bd->swap_time > 30) {
809 bd->entries -= bd->old_entries;
810 bd->old_entries = bd->entries;
811 memset(bd->set[bd->new], 0,
812 sizeof(bd->set[0]));
813 bd->new = 1-bd->new;
814 bd->swap_time = seconds_since_boot();
815 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400816 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000817 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100818 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000819 if (test_bit(hash&255, bd->set[0]) &&
820 test_bit((hash>>8)&255, bd->set[0]) &&
821 test_bit((hash>>16)&255, bd->set[0]))
822 return 1;
823
824 if (test_bit(hash&255, bd->set[1]) &&
825 test_bit((hash>>8)&255, bd->set[1]) &&
826 test_bit((hash>>16)&255, bd->set[1]))
827 return 1;
828
829 return 0;
830}
831
832static void block_delegations(struct knfsd_fh *fh)
833{
834 u32 hash;
835 struct bloom_pair *bd = &blocked_delegations;
836
Daniel Borkmann87545892014-12-10 16:33:11 +0100837 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000838
Jeff Laytonf54fe962014-07-25 07:34:26 -0400839 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000840 __set_bit(hash&255, bd->set[bd->new]);
841 __set_bit((hash>>8)&255, bd->set[bd->new]);
842 __set_bit((hash>>16)&255, bd->set[bd->new]);
843 if (bd->entries == 0)
844 bd->swap_time = seconds_since_boot();
845 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400846 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000847}
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849static struct nfs4_delegation *
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500850alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
851 struct svc_fh *current_fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +0200852 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
854 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400855 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400858 n = atomic_long_inc_return(&num_delegations);
859 if (n < 0 || n > max_delegations)
860 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000861 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400862 goto out_dec;
Kinglong Meed19fb702017-01-18 19:04:42 +0800863 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700864 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400865 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400866
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400867 /*
868 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400869 * meaning of seqid 0 isn't meaningful, really, but let's avoid
870 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400871 */
872 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700873 INIT_LIST_HEAD(&dp->dl_perfile);
874 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200876 dp->dl_clnt_odstate = odstate;
877 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400878 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200879 dp->dl_retries = 1;
880 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200881 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500882 get_nfs4_file(fp);
883 dp->dl_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400885out_dec:
886 atomic_long_dec(&num_delegations);
887 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
890void
Trond Myklebust60116952014-07-29 21:34:06 -0400891nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Trond Myklebust11b91642014-07-29 21:34:08 -0400893 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400894 struct nfs4_client *clp = s->sc_client;
895
Jeff Layton4770d722014-07-29 21:34:10 -0400896 might_lock(&clp->cl_lock);
897
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300898 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
Jeff Laytonb401be222014-07-29 21:34:33 -0400899 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400900 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400901 }
Trond Myklebust60116952014-07-29 21:34:06 -0400902 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400903 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400904 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400905 if (fp)
906 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
Jeff Layton9767feb2015-10-01 09:05:50 -0400909void
910nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
911{
912 stateid_t *src = &stid->sc_stateid;
913
914 spin_lock(&stid->sc_lock);
915 if (unlikely(++src->si_generation == 0))
916 src->si_generation = 1;
917 memcpy(dst, src, sizeof(*dst));
918 spin_unlock(&stid->sc_lock);
919}
920
J. Bruce Fields353601e2018-02-16 14:29:42 -0500921static void put_deleg_file(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Jeff Layton6bcc0342014-08-09 10:22:40 -0400923 struct file *filp = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -0500924
925 spin_lock(&fp->fi_lock);
926 if (--fp->fi_delegees == 0)
927 swap(filp, fp->fi_deleg_file);
928 spin_unlock(&fp->fi_lock);
929
930 if (filp)
931 fput(filp);
932}
933
934static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
935{
J. Bruce Fieldscba7b3d2018-02-15 15:50:49 -0500936 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fields353601e2018-02-16 14:29:42 -0500937 struct file *filp = fp->fi_deleg_file;
Jeff Layton417c6622014-07-21 09:34:57 -0400938
J. Bruce Fieldsb8232d32018-02-15 15:29:15 -0500939 WARN_ON_ONCE(!fp->fi_delegees);
940
J. Bruce Fields353601e2018-02-16 14:29:42 -0500941 vfs_setlease(filp, F_UNLCK, NULL, (void **)&dp);
942 put_deleg_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
J. Bruce Fields0af6e692018-02-21 15:11:03 -0500945static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
946{
947 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -0500948 nfs4_unlock_deleg_lease(dp);
J. Bruce Fields0af6e692018-02-21 15:11:03 -0500949 nfs4_put_stid(&dp->dl_stid);
950}
951
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200952void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400953{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500954 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400955}
956
Andrew Elble34ed9872015-10-15 12:07:28 -0400957/**
J. Bruce Fields68b18f52018-02-19 11:38:33 -0500958 * nfs4_delegation_exists - Discover if this delegation already exists
Andrew Elble34ed9872015-10-15 12:07:28 -0400959 * @clp: a pointer to the nfs4_client we're granting a delegation to
960 * @fp: a pointer to the nfs4_file we're granting a delegation on
961 *
962 * Return:
J. Bruce Fields68b18f52018-02-19 11:38:33 -0500963 * On success: true iff an existing delegation is found
Andrew Elble34ed9872015-10-15 12:07:28 -0400964 */
965
J. Bruce Fields68b18f52018-02-19 11:38:33 -0500966static bool
967nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
Benny Halevy931ee562014-05-30 09:09:27 -0400968{
Andrew Elble34ed9872015-10-15 12:07:28 -0400969 struct nfs4_delegation *searchdp = NULL;
970 struct nfs4_client *searchclp = NULL;
971
Benny Halevycdc975052014-05-30 09:09:30 -0400972 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400973 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400974
Andrew Elble34ed9872015-10-15 12:07:28 -0400975 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
976 searchclp = searchdp->dl_stid.sc_client;
977 if (clp == searchclp) {
Fengguang Wu51d87bc2018-03-22 13:37:20 +0800978 return true;
Andrew Elble34ed9872015-10-15 12:07:28 -0400979 }
980 }
Fengguang Wu51d87bc2018-03-22 13:37:20 +0800981 return false;
Andrew Elble34ed9872015-10-15 12:07:28 -0400982}
983
984/**
985 * hash_delegation_locked - Add a delegation to the appropriate lists
986 * @dp: a pointer to the nfs4_delegation we are adding.
987 * @fp: a pointer to the nfs4_file we're granting a delegation on
988 *
989 * Return:
990 * On success: NULL if the delegation was successfully hashed.
991 *
992 * On error: -EAGAIN if one was previously granted to this
993 * nfs4_client for this nfs4_file. Delegation is not hashed.
994 *
995 */
996
997static int
998hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
999{
Andrew Elble34ed9872015-10-15 12:07:28 -04001000 struct nfs4_client *clp = dp->dl_stid.sc_client;
1001
1002 lockdep_assert_held(&state_lock);
1003 lockdep_assert_held(&fp->fi_lock);
1004
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001005 if (nfs4_delegation_exists(clp, fp))
1006 return -EAGAIN;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001007 refcount_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -04001008 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -04001009 list_add(&dp->dl_perfile, &fp->fi_delegations);
Andrew Elble34ed9872015-10-15 12:07:28 -04001010 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1011 return 0;
Benny Halevy931ee562014-05-30 09:09:27 -04001012}
1013
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001014static bool
Jeff Layton42690672014-07-25 07:34:20 -04001015unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Trond Myklebust11b91642014-07-29 21:34:08 -04001017 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -04001018
Jeff Layton42690672014-07-25 07:34:20 -04001019 lockdep_assert_held(&state_lock);
1020
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001021 if (list_empty(&dp->dl_perfile))
1022 return false;
1023
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04001024 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -04001025 /* Ensure that deleg break won't try to requeue it */
1026 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -04001027 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001028 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -04001030 list_del_init(&dp->dl_perfile);
1031 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001032 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001033}
1034
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001035static void destroy_delegation(struct nfs4_delegation *dp)
1036{
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001037 bool unhashed;
1038
Jeff Layton42690672014-07-25 07:34:20 -04001039 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001040 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -04001041 spin_unlock(&state_lock);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001042 if (unhashed)
1043 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001044}
1045
1046static void revoke_delegation(struct nfs4_delegation *dp)
1047{
1048 struct nfs4_client *clp = dp->dl_stid.sc_client;
1049
Jeff Layton2d4a5322014-07-25 07:34:21 -04001050 WARN_ON(!list_empty(&dp->dl_recall_lru));
1051
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001052 if (clp->cl_minorversion) {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001053 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001054 refcount_inc(&dp->dl_stid.sc_count);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001055 spin_lock(&clp->cl_lock);
1056 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1057 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001058 }
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001059 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001060}
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062/*
1063 * SETCLIENTID state
1064 */
1065
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001066static unsigned int clientid_hashval(u32 id)
1067{
1068 return id & CLIENT_HASH_MASK;
1069}
1070
Scott Mayhew6b189102019-03-26 18:06:26 -04001071static unsigned int clientstr_hashval(struct xdr_netobj name)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001072{
Scott Mayhew6b189102019-03-26 18:06:26 -04001073 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001074}
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001077 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1078 * st_{access,deny}_bmap field of the stateid, in order to track not
1079 * only what share bits are currently in force, but also what
1080 * combinations of share bits previous opens have used. This allows us
1081 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1082 * return an error if the client attempt to downgrade to a combination
1083 * of share bits not explicable by closing some of its previous opens.
1084 *
1085 * XXX: This enforcement is actually incomplete, since we don't keep
1086 * track of access/deny bit combinations; so, e.g., we allow:
1087 *
1088 * OPEN allow read, deny write
1089 * OPEN allow both, deny none
1090 * DOWNGRADE allow read, deny none
1091 *
1092 * which we should reject.
1093 */
Jeff Layton5ae037e2012-05-11 09:45:11 -04001094static unsigned int
1095bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001096 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -04001097 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001098
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001099 for (i = 1; i < 4; i++) {
1100 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -04001101 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001102 }
Jeff Layton5ae037e2012-05-11 09:45:11 -04001103 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001104}
1105
Jeff Layton82c5ff12012-05-11 09:45:13 -04001106/* set share access for a given stateid */
1107static inline void
1108set_access(u32 access, struct nfs4_ol_stateid *stp)
1109{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001110 unsigned char mask = 1 << access;
1111
1112 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1113 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001114}
1115
1116/* clear share access for a given stateid */
1117static inline void
1118clear_access(u32 access, struct nfs4_ol_stateid *stp)
1119{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001120 unsigned char mask = 1 << access;
1121
1122 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1123 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001124}
1125
1126/* test whether a given stateid has access */
1127static inline bool
1128test_access(u32 access, struct nfs4_ol_stateid *stp)
1129{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001130 unsigned char mask = 1 << access;
1131
1132 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001133}
1134
Jeff Laytonce0fc432012-05-11 09:45:14 -04001135/* set share deny for a given stateid */
1136static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001137set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001138{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001139 unsigned char mask = 1 << deny;
1140
1141 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1142 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001143}
1144
1145/* clear share deny for a given stateid */
1146static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001147clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001148{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001149 unsigned char mask = 1 << deny;
1150
1151 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1152 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001153}
1154
1155/* test whether a given stateid is denying specific access */
1156static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -04001157test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001158{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001159 unsigned char mask = 1 << deny;
1160
1161 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001162}
1163
1164static int nfs4_access_to_omode(u32 access)
1165{
J. Bruce Fields8f34a432010-09-02 15:23:16 -04001166 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001167 case NFS4_SHARE_ACCESS_READ:
1168 return O_RDONLY;
1169 case NFS4_SHARE_ACCESS_WRITE:
1170 return O_WRONLY;
1171 case NFS4_SHARE_ACCESS_BOTH:
1172 return O_RDWR;
1173 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001174 WARN_ON_ONCE(1);
1175 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001176}
1177
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001178/*
1179 * A stateid that had a deny mode associated with it is being released
1180 * or downgraded. Recalculate the deny mode on the file.
1181 */
1182static void
1183recalculate_deny_mode(struct nfs4_file *fp)
1184{
1185 struct nfs4_ol_stateid *stp;
1186
1187 spin_lock(&fp->fi_lock);
1188 fp->fi_share_deny = 0;
1189 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1190 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1191 spin_unlock(&fp->fi_lock);
1192}
1193
1194static void
1195reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1196{
1197 int i;
1198 bool change = false;
1199
1200 for (i = 1; i < 4; i++) {
1201 if ((i & deny) != i) {
1202 change = true;
1203 clear_deny(i, stp);
1204 }
1205 }
1206
1207 /* Recalculate per-file deny mode if there was a change */
1208 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -04001209 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001210}
1211
Jeff Layton82c5ff12012-05-11 09:45:13 -04001212/* release all access and file references for a given stateid */
1213static void
1214release_all_access(struct nfs4_ol_stateid *stp)
1215{
1216 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -04001217 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001218
1219 if (fp && stp->st_deny_bmap != 0)
1220 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001221
1222 for (i = 1; i < 4; i++) {
1223 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -04001224 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001225 clear_access(i, stp);
1226 }
1227}
1228
Kinglong Meed50ffde2015-07-16 12:05:07 +08001229static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1230{
1231 kfree(sop->so_owner.data);
1232 sop->so_ops->so_free(sop);
1233}
1234
Jeff Layton6b180f02014-07-29 21:34:26 -04001235static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1236{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001237 struct nfs4_client *clp = sop->so_client;
1238
1239 might_lock(&clp->cl_lock);
1240
1241 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001242 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001243 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001244 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001245 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001246}
1247
Jeff Laytone8568732015-08-24 12:41:47 -04001248static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001249{
Trond Myklebust11b91642014-07-29 21:34:08 -04001250 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001251
Jeff Layton1c755dc2014-07-29 21:34:12 -04001252 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1253
Jeff Laytone8568732015-08-24 12:41:47 -04001254 if (list_empty(&stp->st_perfile))
1255 return false;
1256
Trond Myklebust1d31a252014-07-10 14:07:25 -04001257 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001258 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001259 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001260 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001261 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001262}
1263
Trond Myklebust60116952014-07-29 21:34:06 -04001264static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001265{
Trond Myklebust60116952014-07-29 21:34:06 -04001266 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001267
Sachin Bhamare8287f002015-04-27 14:50:14 +02001268 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001269 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001270 if (stp->st_stateowner)
1271 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -04001272 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001273}
1274
Jeff Laytonb49e0842014-07-29 21:34:11 -04001275static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001276{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001277 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1278 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001279 struct file *file;
1280
Jeff Laytonb49e0842014-07-29 21:34:11 -04001281 file = find_any_file(stp->st_stid.sc_file);
1282 if (file)
1283 filp_close(file, (fl_owner_t)lo);
1284 nfs4_free_ol_stateid(stid);
1285}
1286
Jeff Layton2c41beb2014-07-29 21:34:41 -04001287/*
1288 * Put the persistent reference to an already unhashed generic stateid, while
1289 * holding the cl_lock. If it's the last reference, then put it onto the
1290 * reaplist for later destruction.
1291 */
1292static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1293 struct list_head *reaplist)
1294{
1295 struct nfs4_stid *s = &stp->st_stid;
1296 struct nfs4_client *clp = s->sc_client;
1297
1298 lockdep_assert_held(&clp->cl_lock);
1299
1300 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1301
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001302 if (!refcount_dec_and_test(&s->sc_count)) {
Jeff Layton2c41beb2014-07-29 21:34:41 -04001303 wake_up_all(&close_wq);
1304 return;
1305 }
1306
1307 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1308 list_add(&stp->st_locks, reaplist);
1309}
1310
Jeff Laytone8568732015-08-24 12:41:47 -04001311static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001312{
Chuck Leverf46c4452016-10-29 18:19:03 -04001313 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001314
1315 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001316 nfs4_unhash_stid(&stp->st_stid);
Jeff Laytone8568732015-08-24 12:41:47 -04001317 return unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001318}
1319
Jeff Layton5adfd882014-07-29 21:34:31 -04001320static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001321{
Chuck Leverf46c4452016-10-29 18:19:03 -04001322 struct nfs4_client *clp = stp->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04001323 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001324
Chuck Leverf46c4452016-10-29 18:19:03 -04001325 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001326 unhashed = unhash_lock_stateid(stp);
Chuck Leverf46c4452016-10-29 18:19:03 -04001327 spin_unlock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001328 if (unhashed)
1329 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001330}
1331
Trond Myklebustc58c6612014-07-29 21:34:35 -04001332static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001333{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001334 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001335
Trond Myklebustd4f04892014-07-29 21:34:36 -04001336 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001337
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001338 list_del_init(&lo->lo_owner.so_strhash);
1339}
1340
Jeff Layton2c41beb2014-07-29 21:34:41 -04001341/*
1342 * Free a list of generic stateids that were collected earlier after being
1343 * fully unhashed.
1344 */
1345static void
1346free_ol_stateid_reaplist(struct list_head *reaplist)
1347{
1348 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001349 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001350
1351 might_sleep();
1352
1353 while (!list_empty(reaplist)) {
1354 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1355 st_locks);
1356 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001357 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001358 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001359 if (fp)
1360 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001361 }
1362}
1363
Jeff Laytond83017f2014-07-29 21:34:42 -04001364static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1365 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001366{
1367 struct nfs4_ol_stateid *stp;
1368
Jeff Laytone8568732015-08-24 12:41:47 -04001369 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1370
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001371 while (!list_empty(&open_stp->st_locks)) {
1372 stp = list_entry(open_stp->st_locks.next,
1373 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001374 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001375 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001376 }
1377}
1378
Jeff Laytone8568732015-08-24 12:41:47 -04001379static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001380 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001381{
Jeff Laytone8568732015-08-24 12:41:47 -04001382 bool unhashed;
1383
Jeff Layton2c41beb2014-07-29 21:34:41 -04001384 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1385
Jeff Laytone8568732015-08-24 12:41:47 -04001386 unhashed = unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001387 release_open_stateid_locks(stp, reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04001388 return unhashed;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001389}
1390
1391static void release_open_stateid(struct nfs4_ol_stateid *stp)
1392{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001393 LIST_HEAD(reaplist);
1394
1395 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001396 if (unhash_open_stateid(stp, &reaplist))
1397 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001398 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1399 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001400}
1401
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001402static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001403{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001404 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001405
Trond Myklebustd4f04892014-07-29 21:34:36 -04001406 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001407
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001408 list_del_init(&oo->oo_owner.so_strhash);
1409 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001410}
1411
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001412static void release_last_closed_stateid(struct nfs4_openowner *oo)
1413{
Jeff Layton217526e2014-07-30 08:27:11 -04001414 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1415 nfsd_net_id);
1416 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001417
Jeff Layton217526e2014-07-30 08:27:11 -04001418 spin_lock(&nn->client_lock);
1419 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001420 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001421 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001422 oo->oo_last_closed_stid = NULL;
1423 }
Jeff Layton217526e2014-07-30 08:27:11 -04001424 spin_unlock(&nn->client_lock);
1425 if (s)
1426 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001427}
1428
Jeff Layton2c41beb2014-07-29 21:34:41 -04001429static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001430{
1431 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001432 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001433 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001434
Jeff Layton2c41beb2014-07-29 21:34:41 -04001435 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001436
Trond Myklebustd4f04892014-07-29 21:34:36 -04001437 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001438 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001439 while (!list_empty(&oo->oo_owner.so_stateids)) {
1440 stp = list_first_entry(&oo->oo_owner.so_stateids,
1441 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001442 if (unhash_open_stateid(stp, &reaplist))
1443 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001444 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001445 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001446 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001447 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001448 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001449}
1450
Marc Eshel5282fd72009-04-03 08:27:52 +03001451static inline int
1452hash_sessionid(struct nfs4_sessionid *sessionid)
1453{
1454 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1455
1456 return sid->sequence % SESSION_HASH_SIZE;
1457}
1458
Mark Salter135dd002015-04-06 09:46:00 -04001459#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001460static inline void
1461dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1462{
1463 u32 *ptr = (u32 *)(&sessionid->data[0]);
1464 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1465}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001466#else
1467static inline void
1468dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1469{
1470}
1471#endif
1472
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001473/*
1474 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1475 * won't be used for replay.
1476 */
1477void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1478{
1479 struct nfs4_stateowner *so = cstate->replay_owner;
1480
1481 if (nfserr == nfserr_replay_me)
1482 return;
1483
1484 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001485 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001486 return;
1487 }
1488 if (!so)
1489 return;
1490 if (so->so_is_open_owner)
1491 release_last_closed_stateid(openowner(so));
1492 so->so_seqid++;
1493 return;
1494}
Marc Eshel5282fd72009-04-03 08:27:52 +03001495
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001496static void
1497gen_sessionid(struct nfsd4_session *ses)
1498{
1499 struct nfs4_client *clp = ses->se_client;
1500 struct nfsd4_sessionid *sid;
1501
1502 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1503 sid->clientid = clp->cl_clientid;
1504 sid->sequence = current_sessionid++;
1505 sid->reserved = 0;
1506}
1507
1508/*
Andy Adamsona649637c72009-08-28 08:45:01 -04001509 * The protocol defines ca_maxresponssize_cached to include the size of
1510 * the rpc header, but all we need to cache is the data starting after
1511 * the end of the initial SEQUENCE operation--the rest we regenerate
1512 * each time. Therefore we can advertise a ca_maxresponssize_cached
1513 * value that is the number of bytes in our cache plus a few additional
1514 * bytes. In order to stay on the safe side, and not promise more than
1515 * we can cache, those additional bytes must be the minimum possible: 24
1516 * bytes of rpc header (xid through accept state, with AUTH_NULL
1517 * verifier), 12 for the compound header (with zero-length tag), and 44
1518 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001519 */
Andy Adamsona649637c72009-08-28 08:45:01 -04001520#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1521
Andy Adamson557ce262009-08-28 08:45:04 -04001522static void
1523free_session_slots(struct nfsd4_session *ses)
1524{
1525 int i;
1526
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001527 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1528 free_svc_cred(&ses->se_slots[i]->sl_cred);
Andy Adamson557ce262009-08-28 08:45:04 -04001529 kfree(ses->se_slots[i]);
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001530 }
Andy Adamson557ce262009-08-28 08:45:04 -04001531}
1532
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001533/*
1534 * We don't actually need to cache the rpc and session headers, so we
1535 * can allocate a little less for each slot:
1536 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001537static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001538{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001539 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001540
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001541 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1542 size = 0;
1543 else
1544 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1545 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001546}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001547
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001548/*
1549 * XXX: If we run out of reserved DRC memory we could (up to a point)
1550 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001551 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001552 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001553static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001554{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001555 u32 slotsize = slot_bytes(ca);
1556 u32 num = ca->maxreqs;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001557 unsigned long avail, total_avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001558
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001559 spin_lock(&nfsd_drc_lock);
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001560 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1561 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001562 /*
1563 * Never use more than a third of the remaining memory,
1564 * unless it's the only way to give this client a slot:
1565 */
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001566 avail = clamp_t(int, avail, slotsize, total_avail/3);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001567 num = min_t(int, num, avail / slotsize);
1568 nfsd_drc_mem_used += num * slotsize;
1569 spin_unlock(&nfsd_drc_lock);
1570
1571 return num;
1572}
1573
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001574static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001575{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001576 int slotsize = slot_bytes(ca);
1577
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001578 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001579 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001580 spin_unlock(&nfsd_drc_lock);
1581}
1582
Kinglong Mee60810e52014-01-01 00:35:47 +08001583static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1584 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001585{
Kinglong Mee60810e52014-01-01 00:35:47 +08001586 int numslots = fattrs->maxreqs;
1587 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001588 struct nfsd4_session *new;
1589 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001590
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001591 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001592 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1593 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001594
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001595 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001596 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001597 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001598 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001599 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001600 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001601 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001602 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001603 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001604
1605 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1606 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1607
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001608 return new;
1609out_free:
1610 while (i--)
1611 kfree(new->se_slots[i]);
1612 kfree(new);
1613 return NULL;
1614}
1615
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001616static void free_conn(struct nfsd4_conn *c)
1617{
1618 svc_xprt_put(c->cn_xprt);
1619 kfree(c);
1620}
1621
1622static void nfsd4_conn_lost(struct svc_xpt_user *u)
1623{
1624 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1625 struct nfs4_client *clp = c->cn_session->se_client;
1626
1627 spin_lock(&clp->cl_lock);
1628 if (!list_empty(&c->cn_persession)) {
1629 list_del(&c->cn_persession);
1630 free_conn(c);
1631 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001632 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001633 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001634}
1635
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001636static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001637{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001638 struct nfsd4_conn *conn;
1639
1640 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1641 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001642 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001643 svc_xprt_get(rqstp->rq_xprt);
1644 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001645 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001646 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1647 return conn;
1648}
1649
J. Bruce Fields328ead22010-09-29 16:11:06 -04001650static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1651{
1652 conn->cn_session = ses;
1653 list_add(&conn->cn_persession, &ses->se_conns);
1654}
1655
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001656static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1657{
1658 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001659
1660 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001661 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001662 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001663}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001664
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001665static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001666{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001667 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001668 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001669}
1670
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001671static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001672{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001673 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001674
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001675 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001676 ret = nfsd4_register_conn(conn);
1677 if (ret)
1678 /* oops; xprt is already down: */
1679 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001680 /* We may have gained or lost a callback channel: */
1681 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001682}
1683
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001684static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001685{
1686 u32 dir = NFS4_CDFC4_FORE;
1687
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001688 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001689 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001690 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001691}
1692
1693/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001694static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001695{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001696 struct nfs4_client *clp = s->se_client;
1697 struct nfsd4_conn *c;
1698
1699 spin_lock(&clp->cl_lock);
1700 while (!list_empty(&s->se_conns)) {
1701 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1702 list_del_init(&c->cn_persession);
1703 spin_unlock(&clp->cl_lock);
1704
1705 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1706 free_conn(c);
1707
1708 spin_lock(&clp->cl_lock);
1709 }
1710 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001711}
1712
J. Bruce Fields1377b692012-09-11 21:42:40 -04001713static void __free_session(struct nfsd4_session *ses)
1714{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001715 free_session_slots(ses);
1716 kfree(ses);
1717}
1718
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001719static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001720{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001721 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001722 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001723 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001724}
Andy Adamson557ce262009-08-28 08:45:04 -04001725
Fengguang Wu135ae822012-11-10 07:20:25 -05001726static 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 -04001727{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001728 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001729 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001730
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001731 new->se_client = clp;
1732 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001733
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001734 INIT_LIST_HEAD(&new->se_conns);
1735
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001736 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001737 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001738 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001739 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001740 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001741 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001742 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001743 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001744 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001745 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001746
Chuck Leverb0d2e422014-08-22 15:10:59 -04001747 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001748 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001749 /*
1750 * This is a little silly; with sessions there's no real
1751 * use for the callback address. Use the peer address
1752 * as a reasonable default for now, but consider fixing
1753 * the rpc client not to require an address in the
1754 * future:
1755 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001756 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1757 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001758 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001759}
1760
Benny Halevy9089f1b2010-05-12 00:12:26 +03001761/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001762static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001763__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001764{
1765 struct nfsd4_session *elem;
1766 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001767 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001768
Trond Myklebust0a880a22014-07-30 08:27:10 -04001769 lockdep_assert_held(&nn->client_lock);
1770
Marc Eshel5282fd72009-04-03 08:27:52 +03001771 dump_sessionid(__func__, sessionid);
1772 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001773 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001774 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001775 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1776 NFS4_MAX_SESSIONID_LEN)) {
1777 return elem;
1778 }
1779 }
1780
1781 dprintk("%s: session not found\n", __func__);
1782 return NULL;
1783}
1784
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001785static struct nfsd4_session *
1786find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1787 __be32 *ret)
1788{
1789 struct nfsd4_session *session;
1790 __be32 status = nfserr_badsession;
1791
1792 session = __find_in_sessionid_hashtbl(sessionid, net);
1793 if (!session)
1794 goto out;
1795 status = nfsd4_get_session_locked(session);
1796 if (status)
1797 session = NULL;
1798out:
1799 *ret = status;
1800 return session;
1801}
1802
Benny Halevy9089f1b2010-05-12 00:12:26 +03001803/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001804static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001805unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001806{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001807 struct nfs4_client *clp = ses->se_client;
1808 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1809
1810 lockdep_assert_held(&nn->client_lock);
1811
Andy Adamson7116ed62009-04-03 08:27:43 +03001812 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001813 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001814 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001815 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001816}
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1819static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001820STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001822 /*
1823 * We're assuming the clid was not given out from a boot
1824 * precisely 2^32 (about 136 years) before this one. That seems
1825 * a safe assumption:
1826 */
1827 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001829 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001830 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return 1;
1832}
1833
1834/*
1835 * XXX Should we use a slab cache ?
1836 * This type of memory management is somewhat inefficient, but we use it
1837 * anyway since SETCLIENTID is not a common operation.
1838 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001839static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
1841 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001842 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Jeff Layton9258a2d2018-03-16 09:47:22 -04001844 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001845 if (clp == NULL)
1846 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001847 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001848 if (clp->cl_name.data == NULL)
1849 goto err_no_name;
Kees Cook6da2ec52018-06-12 13:55:00 -07001850 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1851 sizeof(struct list_head),
1852 GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001853 if (!clp->cl_ownerstr_hashtbl)
1854 goto err_no_hashtbl;
1855 for (i = 0; i < OWNER_HASH_SIZE; i++)
1856 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001857 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001858 INIT_LIST_HEAD(&clp->cl_sessions);
1859 idr_init(&clp->cl_stateids);
1860 atomic_set(&clp->cl_refcount, 0);
1861 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1862 INIT_LIST_HEAD(&clp->cl_idhash);
1863 INIT_LIST_HEAD(&clp->cl_openowners);
1864 INIT_LIST_HEAD(&clp->cl_delegations);
1865 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04001866 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001867#ifdef CONFIG_NFSD_PNFS
1868 INIT_LIST_HEAD(&clp->cl_lo_states);
1869#endif
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001870 INIT_LIST_HEAD(&clp->async_copies);
1871 spin_lock_init(&clp->async_lock);
Trond Myklebust5694c932014-04-18 14:43:56 -04001872 spin_lock_init(&clp->cl_lock);
1873 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001875err_no_hashtbl:
1876 kfree(clp->cl_name.data);
1877err_no_name:
Jeff Layton9258a2d2018-03-16 09:47:22 -04001878 kmem_cache_free(client_slab, clp);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001879 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001882static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883free_client(struct nfs4_client *clp)
1884{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001885 while (!list_empty(&clp->cl_sessions)) {
1886 struct nfsd4_session *ses;
1887 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1888 se_perclnt);
1889 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001890 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1891 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001892 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001893 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001894 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001895 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001897 idr_destroy(&clp->cl_stateids);
Jeff Layton9258a2d2018-03-16 09:47:22 -04001898 kmem_cache_free(client_slab, clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
1900
Benny Halevy84d38ac2010-05-12 00:13:16 +03001901/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001902static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001903unhash_client_locked(struct nfs4_client *clp)
1904{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001905 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001906 struct nfsd4_session *ses;
1907
Trond Myklebust0a880a22014-07-30 08:27:10 -04001908 lockdep_assert_held(&nn->client_lock);
1909
Trond Myklebust4beb3452014-07-30 08:27:02 -04001910 /* Mark the client as expired! */
1911 clp->cl_time = 0;
1912 /* Make it invisible */
1913 if (!list_empty(&clp->cl_idhash)) {
1914 list_del_init(&clp->cl_idhash);
1915 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1916 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1917 else
1918 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1919 }
1920 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001921 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001922 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1923 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001924 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001925}
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001928unhash_client(struct nfs4_client *clp)
1929{
1930 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1931
1932 spin_lock(&nn->client_lock);
1933 unhash_client_locked(clp);
1934 spin_unlock(&nn->client_lock);
1935}
1936
Jeff Layton97403d92014-07-30 08:27:12 -04001937static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1938{
1939 if (atomic_read(&clp->cl_refcount))
1940 return nfserr_jukebox;
1941 unhash_client_locked(clp);
1942 return nfs_ok;
1943}
1944
Trond Myklebust4beb3452014-07-30 08:27:02 -04001945static void
1946__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
Jeff Layton68ef3bc2018-03-16 11:32:02 -04001948 int i;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001949 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 struct list_head reaplist;
1952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04001954 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001955 while (!list_empty(&clp->cl_delegations)) {
1956 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001957 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04001958 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 }
Benny Halevycdc975052014-05-30 09:09:30 -04001960 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 while (!list_empty(&reaplist)) {
1962 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001963 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001964 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001966 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05001967 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001968 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001969 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001970 }
NeilBrownea1da632005-06-23 22:04:17 -07001971 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001972 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04001973 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001974 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 }
Jeff Layton68ef3bc2018-03-16 11:32:02 -04001976 for (i = 0; i < OWNER_HASH_SIZE; i++) {
1977 struct nfs4_stateowner *so, *tmp;
1978
1979 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
1980 so_strhash) {
1981 /* Should be no openowners at this point */
1982 WARN_ON_ONCE(so->so_is_open_owner);
1983 remove_blocked_locks(lockowner(so));
1984 }
1985 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001986 nfsd4_return_all_client_layouts(clp);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001987 nfsd4_shutdown_copy(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001988 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001989 if (clp->cl_cb_conn.cb_xprt)
1990 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001991 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992}
1993
Trond Myklebust4beb3452014-07-30 08:27:02 -04001994static void
1995destroy_client(struct nfs4_client *clp)
1996{
1997 unhash_client(clp);
1998 __destroy_client(clp);
1999}
2000
Scott Mayhew362063a2019-03-26 18:06:28 -04002001static void inc_reclaim_complete(struct nfs4_client *clp)
2002{
2003 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2004
2005 if (!nn->track_reclaim_completes)
2006 return;
2007 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2008 return;
2009 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2010 nn->reclaim_str_hashtbl_size) {
2011 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2012 clp->net->ns.inum);
2013 nfsd4_end_grace(nn);
2014 }
2015}
2016
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002017static void expire_client(struct nfs4_client *clp)
2018{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002019 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002020 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04002021 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002022}
2023
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002024static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2025{
2026 memcpy(target->cl_verifier.data, source->data,
2027 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
2029
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002030static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2031{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2033 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2034}
2035
J. Bruce Fields50043852015-11-20 15:58:37 -05002036static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2037{
NeilBrown2f10fdc2017-03-23 16:57:36 +08002038 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2039 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2040 GFP_KERNEL);
Chuck Lever9abdda52018-08-16 12:05:59 -04002041 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2042 if ((source->cr_principal && !target->cr_principal) ||
2043 (source->cr_raw_principal && !target->cr_raw_principal) ||
2044 (source->cr_targ_princ && !target->cr_targ_princ))
NeilBrown2f10fdc2017-03-23 16:57:36 +08002045 return -ENOMEM;
J. Bruce Fields50043852015-11-20 15:58:37 -05002046
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04002047 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 target->cr_uid = source->cr_uid;
2049 target->cr_gid = source->cr_gid;
2050 target->cr_group_info = source->cr_group_info;
2051 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04002052 target->cr_gss_mech = source->cr_gss_mech;
2053 if (source->cr_gss_mech)
2054 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002055 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056}
2057
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002058static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002059compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2060{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002061 if (o1->len < o2->len)
2062 return -1;
2063 if (o1->len > o2->len)
2064 return 1;
2065 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002066}
2067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002069same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2070{
2071 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
2074static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002075same_clid(clientid_t *cl1, clientid_t *cl2)
2076{
2077 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002080static bool groups_equal(struct group_info *g1, struct group_info *g2)
2081{
2082 int i;
2083
2084 if (g1->ngroups != g2->ngroups)
2085 return false;
2086 for (i=0; i<g1->ngroups; i++)
Alexey Dobriyan81243ea2016-10-07 17:03:12 -07002087 if (!gid_eq(g1->gid[i], g2->gid[i]))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002088 return false;
2089 return true;
2090}
2091
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002092/*
2093 * RFC 3530 language requires clid_inuse be returned when the
2094 * "principal" associated with a requests differs from that previously
2095 * used. We use uid, gid's, and gss principal string as our best
2096 * approximation. We also don't want to allow non-gss use of a client
2097 * established using gss: in theory cr_principal should catch that
2098 * change, but in practice cr_principal can be null even in the gss case
2099 * since gssd doesn't always pass down a principal string.
2100 */
2101static bool is_gss_cred(struct svc_cred *cr)
2102{
2103 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2104 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2105}
2106
2107
Vivek Trivedi5559b502012-07-24 21:18:20 +05302108static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002109same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2110{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002111 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002112 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2113 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002114 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2115 return false;
Chuck Lever9abdda52018-08-16 12:05:59 -04002116 /* XXX: check that cr_targ_princ fields match ? */
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002117 if (cr1->cr_principal == cr2->cr_principal)
2118 return true;
2119 if (!cr1->cr_principal || !cr2->cr_principal)
2120 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05302121 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
2123
J. Bruce Fields57266a62013-04-13 14:27:29 -04002124static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2125{
2126 struct svc_cred *cr = &rqstp->rq_cred;
2127 u32 service;
2128
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04002129 if (!cr->cr_gss_mech)
2130 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002131 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2132 return service == RPC_GSS_SVC_INTEGRITY ||
2133 service == RPC_GSS_SVC_PRIVACY;
2134}
2135
Andrew Elblededeb132016-06-15 12:52:08 -04002136bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
J. Bruce Fields57266a62013-04-13 14:27:29 -04002137{
2138 struct svc_cred *cr = &rqstp->rq_cred;
2139
2140 if (!cl->cl_mach_cred)
2141 return true;
2142 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2143 return false;
2144 if (!svc_rqst_integrity_protected(rqstp))
2145 return false;
J. Bruce Fields414ca012015-11-20 10:48:02 -05002146 if (cl->cl_cred.cr_raw_principal)
2147 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2148 cr->cr_raw_principal);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002149 if (!cr->cr_principal)
2150 return false;
2151 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2152}
2153
Jeff Layton294ac322014-07-30 08:27:15 -04002154static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05002155{
Chuck Leverab4684d2012-03-02 17:13:50 -05002156 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Jeff Laytonf4199922014-06-17 07:44:11 -04002158 /*
2159 * This is opaque to client, so no need to byte-swap. Use
2160 * __force to keep sparse happy
2161 */
2162 verf[0] = (__force __be32)get_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08002163 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05002164 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
2166
Jeff Layton294ac322014-07-30 08:27:15 -04002167static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2168{
2169 clp->cl_clientid.cl_boot = nn->boot_time;
2170 clp->cl_clientid.cl_id = nn->clientid_counter++;
2171 gen_confirm(clp, nn);
2172}
2173
Jeff Layton4770d722014-07-29 21:34:10 -04002174static struct nfs4_stid *
2175find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04002176{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002177 struct nfs4_stid *ret;
2178
2179 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2180 if (!ret || !ret->sc_type)
2181 return NULL;
2182 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04002183}
2184
Jeff Layton4770d722014-07-29 21:34:10 -04002185static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04002186find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002187{
2188 struct nfs4_stid *s;
2189
Jeff Layton4770d722014-07-29 21:34:10 -04002190 spin_lock(&cl->cl_lock);
2191 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002192 if (s != NULL) {
2193 if (typemask & s->sc_type)
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03002194 refcount_inc(&s->sc_count);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002195 else
2196 s = NULL;
2197 }
Jeff Layton4770d722014-07-29 21:34:10 -04002198 spin_unlock(&cl->cl_lock);
2199 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04002200}
2201
Jeff Layton2216d442012-11-12 15:00:57 -05002202static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002203 struct svc_rqst *rqstp, nfs4_verifier *verf)
2204{
2205 struct nfs4_client *clp;
2206 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002207 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002208 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002209
2210 clp = alloc_client(name);
2211 if (clp == NULL)
2212 return NULL;
2213
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002214 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2215 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002216 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002217 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002218 }
Christoph Hellwig0162ac22014-09-24 12:19:19 +02002219 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Benny Halevy07cd4902010-05-12 00:13:41 +03002220 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002221 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002222 copy_verf(clp, verf);
2223 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04002224 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002225 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002226 return clp;
2227}
2228
NeilBrownfd39ca92005-06-23 22:04:03 -07002229static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002230add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2231{
2232 struct rb_node **new = &(root->rb_node), *parent = NULL;
2233 struct nfs4_client *clp;
2234
2235 while (*new) {
2236 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2237 parent = *new;
2238
2239 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2240 new = &((*new)->rb_left);
2241 else
2242 new = &((*new)->rb_right);
2243 }
2244
2245 rb_link_node(&new_clp->cl_namenode, parent, new);
2246 rb_insert_color(&new_clp->cl_namenode, root);
2247}
2248
2249static struct nfs4_client *
2250find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2251{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002252 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002253 struct rb_node *node = root->rb_node;
2254 struct nfs4_client *clp;
2255
2256 while (node) {
2257 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2258 cmp = compare_blob(&clp->cl_name, name);
2259 if (cmp > 0)
2260 node = node->rb_left;
2261 else if (cmp < 0)
2262 node = node->rb_right;
2263 else
2264 return clp;
2265 }
2266 return NULL;
2267}
2268
2269static void
2270add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
2272 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002273 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Trond Myklebust0a880a22014-07-30 08:27:10 -04002275 lockdep_assert_held(&nn->client_lock);
2276
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002277 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002278 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002280 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002281 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282}
2283
NeilBrownfd39ca92005-06-23 22:04:03 -07002284static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285move_to_confirmed(struct nfs4_client *clp)
2286{
2287 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002288 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Trond Myklebust0a880a22014-07-30 08:27:10 -04002290 lockdep_assert_held(&nn->client_lock);
2291
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002293 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002294 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002295 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002296 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002297 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298}
2299
2300static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002301find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
2303 struct nfs4_client *clp;
2304 unsigned int idhashval = clientid_hashval(clid->cl_id);
2305
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002306 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002307 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002308 if ((bool)clp->cl_minorversion != sessions)
2309 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002310 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 }
2314 return NULL;
2315}
2316
2317static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002318find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2319{
2320 struct list_head *tbl = nn->conf_id_hashtbl;
2321
Trond Myklebust0a880a22014-07-30 08:27:10 -04002322 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002323 return find_client_in_id_table(tbl, clid, sessions);
2324}
2325
2326static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002327find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002329 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Trond Myklebust0a880a22014-07-30 08:27:10 -04002331 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002332 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333}
2334
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002335static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002336{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002337 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002338}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002339
NeilBrown28ce6052005-06-23 22:03:56 -07002340static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002341find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002342{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002343 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002344 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002345}
2346
2347static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002348find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002349{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002350 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002351 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002352}
2353
NeilBrownfd39ca92005-06-23 22:04:03 -07002354static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002355gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002357 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002358 struct sockaddr *sa = svc_addr(rqstp);
2359 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002360 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Jeff Layton7077ecb2009-08-14 12:57:58 -04002362 /* Currently, we only support tcp and tcp6 for the callback channel */
2363 if (se->se_callback_netid_len == 3 &&
2364 !memcmp(se->se_callback_netid_val, "tcp", 3))
2365 expected_family = AF_INET;
2366 else if (se->se_callback_netid_len == 4 &&
2367 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2368 expected_family = AF_INET6;
2369 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 goto out_err;
2371
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002372 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002373 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002374 (struct sockaddr *)&conn->cb_addr,
2375 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002376
J. Bruce Fields07263f12010-05-31 19:09:40 -04002377 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002379
J. Bruce Fields07263f12010-05-31 19:09:40 -04002380 if (conn->cb_addr.ss_family == AF_INET6)
2381 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002382
J. Bruce Fields07263f12010-05-31 19:09:40 -04002383 conn->cb_prog = se->se_callback_prog;
2384 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002385 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 return;
2387out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002388 conn->cb_addr.ss_family = AF_UNSPEC;
2389 conn->cb_addrlen = 0;
Rasmus Villemoes4ab495b2017-02-24 01:15:55 +01002390 dprintk("NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 "will not receive delegations\n",
2392 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 return;
2395}
2396
Andy Adamson074fe892009-04-03 08:28:15 +03002397/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002398 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002399 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002400static void
Andy Adamson074fe892009-04-03 08:28:15 +03002401nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2402{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002403 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002404 struct nfsd4_slot *slot = resp->cstate.slot;
2405 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002406
Andy Adamson557ce262009-08-28 08:45:04 -04002407 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002408
J. Bruce Fields085def32017-10-18 16:17:18 -04002409 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamson557ce262009-08-28 08:45:04 -04002410 slot->sl_opcnt = resp->opcnt;
2411 slot->sl_status = resp->cstate.status;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04002412 free_svc_cred(&slot->sl_cred);
2413 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
Andy Adamsonbf864a32009-04-03 08:28:35 +03002414
J. Bruce Fields085def32017-10-18 16:17:18 -04002415 if (!nfsd4_cache_this(resp)) {
2416 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002417 return;
2418 }
J. Bruce Fields085def32017-10-18 16:17:18 -04002419 slot->sl_flags |= NFSD4_SLOT_CACHED;
2420
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002421 base = resp->cstate.data_offset;
2422 slot->sl_datalen = buf->len - base;
2423 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Dan Carpenterd3f03402015-11-21 13:28:50 +03002424 WARN(1, "%s: sessions DRC could not cache compound\n",
2425 __func__);
Andy Adamson557ce262009-08-28 08:45:04 -04002426 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002427}
2428
2429/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002430 * Encode the replay sequence operation from the slot values.
2431 * If cachethis is FALSE encode the uncached rep error on the next
2432 * operation which sets resp->p and increments resp->opcnt for
2433 * nfs4svc_encode_compoundres.
2434 *
Andy Adamson074fe892009-04-03 08:28:15 +03002435 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002436static __be32
2437nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2438 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002439{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002440 struct nfsd4_op *op;
2441 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002442
Andy Adamsonabfabf82009-07-23 19:02:18 -04002443 /* Encode the replayed sequence operation */
2444 op = &args->ops[resp->opcnt - 1];
2445 nfsd4_encode_operation(resp, op);
2446
J. Bruce Fields085def32017-10-18 16:17:18 -04002447 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2448 return op->status;
2449 if (args->opcnt == 1) {
2450 /*
2451 * The original operation wasn't a solo sequence--we
2452 * always cache those--so this retry must not match the
2453 * original:
2454 */
2455 op->status = nfserr_seq_false_retry;
2456 } else {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002457 op = &args->ops[resp->opcnt++];
2458 op->status = nfserr_retry_uncached_rep;
2459 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002460 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002461 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002462}
2463
2464/*
Andy Adamson557ce262009-08-28 08:45:04 -04002465 * The sequence operation is not cached because we can use the slot and
2466 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002467 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002468static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002469nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2470 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002471{
Andy Adamson557ce262009-08-28 08:45:04 -04002472 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002473 struct xdr_stream *xdr = &resp->xdr;
2474 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002475 __be32 status;
2476
Andy Adamson557ce262009-08-28 08:45:04 -04002477 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002478
Andy Adamsonabfabf82009-07-23 19:02:18 -04002479 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002480 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002481 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002482
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002483 p = xdr_reserve_space(xdr, slot->sl_datalen);
2484 if (!p) {
2485 WARN_ON_ONCE(1);
2486 return nfserr_serverfault;
2487 }
2488 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2489 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002490
Andy Adamson557ce262009-08-28 08:45:04 -04002491 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002492 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002493}
2494
Andy Adamson0733d212009-04-03 08:28:01 +03002495/*
2496 * Set the exchange_id flags returned by the server.
2497 */
2498static void
2499nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2500{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002501#ifdef CONFIG_NFSD_PNFS
2502 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2503#else
Andy Adamson0733d212009-04-03 08:28:01 +03002504 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002505#endif
Andy Adamson0733d212009-04-03 08:28:01 +03002506
2507 /* Referrals are supported, Migration is not. */
2508 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2509
2510 /* set the wire flags to return to client. */
2511 clid->flags = new->cl_exchange_flags;
2512}
2513
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002514static bool client_has_openowners(struct nfs4_client *clp)
2515{
2516 struct nfs4_openowner *oo;
2517
2518 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2519 if (!list_empty(&oo->oo_owner.so_stateids))
2520 return true;
2521 }
2522 return false;
2523}
2524
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002525static bool client_has_state(struct nfs4_client *clp)
2526{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002527 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08002528#ifdef CONFIG_NFSD_PNFS
2529 || !list_empty(&clp->cl_lo_states)
2530#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002531 || !list_empty(&clp->cl_delegations)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002532 || !list_empty(&clp->cl_sessions)
2533 || !list_empty(&clp->async_copies);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002534}
2535
Al Virob37ad282006-10-19 23:28:59 -07002536__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02002537nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2538 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03002539{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002540 struct nfsd4_exchange_id *exid = &u->exchange_id;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002541 struct nfs4_client *conf, *new;
2542 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002543 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002544 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002545 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002546 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002547 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002548 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002549
Jeff Layton363168b2009-08-14 12:57:56 -04002550 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002551 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002552 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002553 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002554 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002555
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002556 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002557 return nfserr_inval;
2558
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002559 new = create_client(exid->clname, rqstp, &verf);
2560 if (new == NULL)
2561 return nfserr_jukebox;
2562
Andy Adamson0733d212009-04-03 08:28:01 +03002563 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002564 case SP4_MACH_CRED:
Andrew Elbleed941642016-06-15 12:52:09 -04002565 exid->spo_must_enforce[0] = 0;
2566 exid->spo_must_enforce[1] = (
2567 1 << (OP_BIND_CONN_TO_SESSION - 32) |
2568 1 << (OP_EXCHANGE_ID - 32) |
2569 1 << (OP_CREATE_SESSION - 32) |
2570 1 << (OP_DESTROY_SESSION - 32) |
2571 1 << (OP_DESTROY_CLIENTID - 32));
2572
2573 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
2574 1 << (OP_OPEN_DOWNGRADE) |
2575 1 << (OP_LOCKU) |
2576 1 << (OP_DELEGRETURN));
2577
2578 exid->spo_must_allow[1] &= (
2579 1 << (OP_TEST_STATEID - 32) |
2580 1 << (OP_FREE_STATEID - 32));
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002581 if (!svc_rqst_integrity_protected(rqstp)) {
2582 status = nfserr_inval;
2583 goto out_nolock;
2584 }
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002585 /*
2586 * Sometimes userspace doesn't give us a principal.
2587 * Which is a bug, really. Anyway, we can't enforce
2588 * MACH_CRED in that case, better to give up now:
2589 */
J. Bruce Fields414ca012015-11-20 10:48:02 -05002590 if (!new->cl_cred.cr_principal &&
2591 !new->cl_cred.cr_raw_principal) {
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002592 status = nfserr_serverfault;
2593 goto out_nolock;
2594 }
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002595 new->cl_mach_cred = true;
Andy Adamson0733d212009-04-03 08:28:01 +03002596 case SP4_NONE:
2597 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002598 default: /* checked by xdr code */
2599 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002600 case SP4_SSV:
Kinglong Mee8edf4b02016-02-26 22:36:42 +08002601 status = nfserr_encr_alg_unsupp;
2602 goto out_nolock;
Andy Adamson0733d212009-04-03 08:28:01 +03002603 }
2604
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002605 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002606 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002607 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002608 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002609 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2610 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2611
J. Bruce Fields136e6582012-05-12 20:37:23 -04002612 if (update) {
2613 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002614 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002615 goto out;
2616 }
Andrew Elblededeb132016-06-15 12:52:08 -04002617 if (!nfsd4_mach_creds_match(conf, rqstp)) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002618 status = nfserr_wrong_cred;
2619 goto out;
2620 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002621 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002622 status = nfserr_perm;
2623 goto out;
2624 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002625 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002626 status = nfserr_not_same;
2627 goto out;
2628 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002629 /* case 6 */
2630 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002631 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002632 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002633 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002634 if (client_has_state(conf)) {
2635 status = nfserr_clid_inuse;
2636 goto out;
2637 }
Andy Adamson0733d212009-04-03 08:28:01 +03002638 goto out_new;
2639 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002640 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002641 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002642 goto out_copy;
2643 }
2644 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002645 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002646 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002647 }
2648
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002649 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002650 status = nfserr_noent;
2651 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002652 }
2653
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002654 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002655 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002656 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002657
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002658 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002659out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002660 if (conf) {
2661 status = mark_client_expired_locked(conf);
2662 if (status)
2663 goto out;
2664 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002665 new->cl_minorversion = cstate->minorversion;
Andrew Elbleed941642016-06-15 12:52:09 -04002666 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
2667 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
Andy Adamson0733d212009-04-03 08:28:01 +03002668
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002669 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002670 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002671 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002672out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002673 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2674 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002675
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002676 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2677 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002678
2679 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002680 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002681 status = nfs_ok;
2682
2683out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002684 spin_unlock(&nn->client_lock);
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002685out_nolock:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002686 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002687 expire_client(new);
2688 if (unconf)
2689 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002690 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002691}
2692
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002693static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002694check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002695{
Andy Adamson88e588d2009-07-23 19:02:15 -04002696 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2697 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002698
2699 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002700 if (slot_inuse) {
2701 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002702 return nfserr_jukebox;
2703 else
2704 return nfserr_seq_misordered;
2705 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002706 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002707 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002708 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002709 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002710 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002711 return nfserr_seq_misordered;
2712}
2713
Andy Adamson49557cc2009-07-23 19:02:16 -04002714/*
2715 * Cache the create session result into the create session single DRC
2716 * slot cache by saving the xdr structure. sl_seqid has been set.
2717 * Do this for solo or embedded create session operations.
2718 */
2719static void
2720nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002721 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002722{
2723 slot->sl_status = nfserr;
2724 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2725}
2726
2727static __be32
2728nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2729 struct nfsd4_clid_slot *slot)
2730{
2731 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2732 return slot->sl_status;
2733}
2734
Mi Jinlong1b74c252011-07-14 14:50:17 +08002735#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2736 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2737 1 + /* MIN tag is length with zero, only length */ \
2738 3 + /* version, opcount, opcode */ \
2739 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2740 /* seqid, slotID, slotID, cache */ \
2741 4 ) * sizeof(__be32))
2742
2743#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2744 2 + /* verifier: AUTH_NULL, length 0 */\
2745 1 + /* status */ \
2746 1 + /* MIN tag is length with zero, only length */ \
2747 3 + /* opcount, opcode, opstatus*/ \
2748 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2749 /* seqid, slotID, slotID, slotID, status */ \
2750 5 ) * sizeof(__be32))
2751
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002752static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002753{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002754 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2755
J. Bruce Fields373cd402013-04-08 15:42:12 -04002756 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2757 return nfserr_toosmall;
2758 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2759 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002760 ca->headerpadsz = 0;
2761 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2762 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2763 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2764 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2765 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2766 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2767 /*
2768 * Note decreasing slot size below client's request may make it
2769 * difficult for client to function correctly, whereas
2770 * decreasing the number of slots will (just?) affect
2771 * performance. When short on memory we therefore prefer to
2772 * decrease number of slots instead of their size. Clients that
2773 * request larger slots than they need will get poor results:
2774 */
2775 ca->maxreqs = nfsd4_get_drc_mem(ca);
2776 if (!ca->maxreqs)
2777 return nfserr_jukebox;
2778
J. Bruce Fields373cd402013-04-08 15:42:12 -04002779 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002780}
2781
Chuck Lever45006322016-03-01 13:06:02 -05002782/*
2783 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
2784 * These are based on similar macros in linux/sunrpc/msg_prot.h .
2785 */
2786#define RPC_MAX_HEADER_WITH_AUTH_SYS \
2787 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
2788
2789#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
2790 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
2791
Kinglong Mee8a891632013-12-23 18:11:02 +08002792#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05002793 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08002794#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05002795 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
2796 sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08002797
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002798static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2799{
2800 ca->headerpadsz = 0;
2801
Kinglong Mee8a891632013-12-23 18:11:02 +08002802 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002803 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002804 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002805 return nfserr_toosmall;
2806 ca->maxresp_cached = 0;
2807 if (ca->maxops < 2)
2808 return nfserr_toosmall;
2809
2810 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002811}
2812
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002813static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2814{
2815 switch (cbs->flavor) {
2816 case RPC_AUTH_NULL:
2817 case RPC_AUTH_UNIX:
2818 return nfs_ok;
2819 default:
2820 /*
2821 * GSS case: the spec doesn't allow us to return this
2822 * error. But it also doesn't allow us not to support
2823 * GSS.
2824 * I'd rather this fail hard than return some error the
2825 * client might think it can already handle:
2826 */
2827 return nfserr_encr_alg_unsupp;
2828 }
2829}
2830
Andy Adamson069b6ad2009-04-03 08:27:58 +03002831__be32
2832nfsd4_create_session(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02002833 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03002834{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002835 struct nfsd4_create_session *cr_ses = &u->create_session;
Jeff Layton363168b2009-08-14 12:57:56 -04002836 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002837 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002838 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04002839 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002840 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002841 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002842 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002843 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002844
Mi Jinlonga62573d2011-03-23 17:57:07 +08002845 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2846 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002847 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2848 if (status)
2849 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002850 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002851 if (status)
2852 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002853 status = check_backchannel_attrs(&cr_ses->back_channel);
2854 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002855 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002856 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002857 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002858 if (!new)
2859 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002860 conn = alloc_conn_from_crses(rqstp, cr_ses);
2861 if (!conn)
2862 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002863
Jeff Laytond20c11d2014-07-30 08:27:07 -04002864 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002865 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002866 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002867 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002868
2869 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002870 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002871 if (!nfsd4_mach_creds_match(conf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002872 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002873 cs_slot = &conf->cl_cs_slot;
2874 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08002875 if (status) {
2876 if (status == nfserr_replay_cache)
2877 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002878 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002879 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002880 } else if (unconf) {
2881 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002882 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002883 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002884 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002885 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002886 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002887 if (!nfsd4_mach_creds_match(unconf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002888 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002889 cs_slot = &unconf->cl_cs_slot;
2890 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002891 if (status) {
2892 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002893 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002894 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002895 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002896 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002897 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002898 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002899 if (status) {
2900 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002901 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002902 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002903 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002904 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002905 conf = unconf;
2906 } else {
2907 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002908 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002909 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002910 status = nfs_ok;
Chuck Lever4ce85c82016-03-01 13:05:27 -05002911 /* Persistent sessions are not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002912 cr_ses->flags &= ~SESSION4_PERSIST;
Chuck Lever4ce85c82016-03-01 13:05:27 -05002913 /* Upshifting from TCP to RDMA is not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002914 cr_ses->flags &= ~SESSION4_RDMA;
2915
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002916 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002917 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002918
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04002919 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002920 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002921 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002922 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002923
Jeff Laytond20c11d2014-07-30 08:27:07 -04002924 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002925 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002926 spin_unlock(&nn->client_lock);
2927 /* init connection and backchannel */
2928 nfsd4_init_conn(rqstp, conn, new);
2929 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002930 if (old)
2931 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002932 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002933out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002934 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002935 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002936 if (old)
2937 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002938out_free_session:
2939 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002940out_release_drc_mem:
2941 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002942 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002943}
2944
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002945static __be32 nfsd4_map_bcts_dir(u32 *dir)
2946{
2947 switch (*dir) {
2948 case NFS4_CDFC4_FORE:
2949 case NFS4_CDFC4_BACK:
2950 return nfs_ok;
2951 case NFS4_CDFC4_FORE_OR_BOTH:
2952 case NFS4_CDFC4_BACK_OR_BOTH:
2953 *dir = NFS4_CDFC4_BOTH;
2954 return nfs_ok;
2955 };
2956 return nfserr_inval;
2957}
2958
Christoph Hellwigeb698532017-05-08 20:58:35 +02002959__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
2960 struct nfsd4_compound_state *cstate,
2961 union nfsd4_op_u *u)
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002962{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002963 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002964 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002965 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002966 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002967
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002968 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2969 if (status)
2970 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002971 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002972 session->se_cb_prog = bc->bc_cb_program;
2973 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002974 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002975
2976 nfsd4_probe_callback(session->se_client);
2977
2978 return nfs_ok;
2979}
2980
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002981__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2982 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02002983 union nfsd4_op_u *u)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002984{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002985 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002986 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002987 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002988 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002989 struct net *net = SVC_NET(rqstp);
2990 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002991
2992 if (!nfsd4_last_compound_op(rqstp))
2993 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002994 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002995 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002996 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002997 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002998 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002999 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003000 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003001 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003002 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003003 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003004 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003005 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003006 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003007 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003008 goto out;
3009 nfsd4_init_conn(rqstp, conn, session);
3010 status = nfs_ok;
3011out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003012 nfsd4_put_session(session);
3013out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003014 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003015}
3016
J. Bruce Fields665d5072018-04-11 16:53:36 -04003017static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003018{
J. Bruce Fields665d5072018-04-11 16:53:36 -04003019 if (!cstate->session)
Fengguang Wu51d87bc2018-03-22 13:37:20 +08003020 return false;
J. Bruce Fields665d5072018-04-11 16:53:36 -04003021 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003022}
3023
Andy Adamson069b6ad2009-04-03 08:27:58 +03003024__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003025nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3026 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003027{
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003028 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003029 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003030 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003031 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003032 struct net *net = SVC_NET(r);
3033 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003034
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003035 status = nfserr_not_only_op;
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003036 if (nfsd4_compound_in_session(cstate, sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04003037 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003038 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003039 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04003040 }
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003041 dump_sessionid(__func__, sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003042 spin_lock(&nn->client_lock);
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003043 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003044 if (!ses)
3045 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003046 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003047 if (!nfsd4_mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003048 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003049 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003050 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003051 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003052 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003053 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003054
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05003055 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04003056
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003057 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003058 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003059out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003060 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003061out_client_lock:
3062 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003063out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03003064 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003065}
3066
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003067static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003068{
3069 struct nfsd4_conn *c;
3070
3071 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003072 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04003073 return c;
3074 }
3075 }
3076 return NULL;
3077}
3078
J. Bruce Fields57266a62013-04-13 14:27:29 -04003079static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003080{
3081 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003082 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003083 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003084 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003085
3086 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003087 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003088 if (c)
3089 goto out_free;
3090 status = nfserr_conn_not_bound_to_session;
3091 if (clp->cl_mach_cred)
3092 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003093 __nfsd4_hash_conn(new, ses);
3094 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003095 ret = nfsd4_register_conn(new);
3096 if (ret)
3097 /* oops; xprt is already down: */
3098 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003099 return nfs_ok;
3100out_free:
3101 spin_unlock(&clp->cl_lock);
3102 free_conn(new);
3103 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003104}
3105
Mi Jinlong868b89c2011-04-27 09:09:58 +08003106static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3107{
3108 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3109
3110 return args->opcnt > session->se_fchannel.maxops;
3111}
3112
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003113static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3114 struct nfsd4_session *session)
3115{
3116 struct xdr_buf *xb = &rqstp->rq_arg;
3117
3118 return xb->len > session->se_fchannel.maxreq_sz;
3119}
3120
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003121static bool replay_matches_cache(struct svc_rqst *rqstp,
3122 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3123{
3124 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3125
3126 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3127 (bool)seq->cachethis)
3128 return false;
3129 /*
3130 * If there's an error than the reply can have fewer ops than
3131 * the call. But if we cached a reply with *more* ops than the
3132 * call you're sending us now, then this new call is clearly not
3133 * really a replay of the old one:
3134 */
3135 if (slot->sl_opcnt < argp->opcnt)
3136 return false;
3137 /* This is the only check explicitly called by spec: */
3138 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3139 return false;
3140 /*
3141 * There may be more comparisons we could actually do, but the
3142 * spec doesn't require us to catch every case where the calls
3143 * don't match (that would require caching the call as well as
3144 * the reply), so we don't bother.
3145 */
3146 return true;
3147}
3148
Andy Adamson069b6ad2009-04-03 08:27:58 +03003149__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003150nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3151 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003152{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003153 struct nfsd4_sequence *seq = &u->sequence;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003154 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003155 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003156 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003157 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003158 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003159 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003160 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003161 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003162 struct net *net = SVC_NET(rqstp);
3163 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003164
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003165 if (resp->opcnt != 1)
3166 return nfserr_sequence_pos;
3167
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003168 /*
3169 * Will be either used or freed by nfsd4_sequence_check_conn
3170 * below.
3171 */
3172 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3173 if (!conn)
3174 return nfserr_jukebox;
3175
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003176 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003177 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003178 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003179 goto out_no_session;
3180 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003181
Mi Jinlong868b89c2011-04-27 09:09:58 +08003182 status = nfserr_too_many_ops;
3183 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003184 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003185
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003186 status = nfserr_req_too_big;
3187 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003188 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003189
Benny Halevyb85d4c02009-04-03 08:28:08 +03003190 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003191 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003192 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003193
Andy Adamson557ce262009-08-28 08:45:04 -04003194 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003195 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3196
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003197 /* We do not negotiate the number of slots yet, so set the
3198 * maxslots to the session maxreqs which is used to encode
3199 * sr_highest_slotid and the sr_target_slot id to maxslots */
3200 seq->maxslots = session->se_fchannel.maxreqs;
3201
J. Bruce Fields73e79482012-02-13 16:39:00 -05003202 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3203 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003204 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003205 status = nfserr_seq_misordered;
3206 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003207 goto out_put_session;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003208 status = nfserr_seq_false_retry;
3209 if (!replay_matches_cache(rqstp, seq, slot))
3210 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003211 cstate->slot = slot;
3212 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003213 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003214 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003215 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003216 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003217 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003218 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003219 }
3220 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003221 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003222
J. Bruce Fields57266a62013-04-13 14:27:29 -04003223 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003224 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003225 if (status)
3226 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003227
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003228 buflen = (seq->cachethis) ?
3229 session->se_fchannel.maxresp_cached :
3230 session->se_fchannel.maxresp_sz;
3231 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3232 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003233 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003234 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003235 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003236
3237 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003238 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003239 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003240 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003241 if (seq->cachethis)
3242 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003243 else
3244 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003245
3246 cstate->slot = slot;
3247 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003248 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003249
Benny Halevyb85d4c02009-04-03 08:28:08 +03003250out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003251 switch (clp->cl_cb_state) {
3252 case NFSD4_CB_DOWN:
3253 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3254 break;
3255 case NFSD4_CB_FAULT:
3256 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3257 break;
3258 default:
3259 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003260 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003261 if (!list_empty(&clp->cl_revoked))
3262 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003263out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08003264 if (conn)
3265 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003266 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003267 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003268out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003269 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003270 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003271}
3272
Trond Myklebustb6076642014-06-30 11:48:35 -04003273void
3274nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3275{
3276 struct nfsd4_compound_state *cs = &resp->cstate;
3277
3278 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04003279 if (cs->status != nfserr_replay_cache) {
3280 nfsd4_store_cache_entry(resp);
3281 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3282 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003283 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04003284 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003285 } else if (cs->clp)
3286 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04003287}
3288
Mi Jinlong345c2842011-10-20 17:51:39 +08003289__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003290nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3291 struct nfsd4_compound_state *cstate,
3292 union nfsd4_op_u *u)
Mi Jinlong345c2842011-10-20 17:51:39 +08003293{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003294 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003295 struct nfs4_client *conf, *unconf;
3296 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003297 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003298 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08003299
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003300 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003301 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003302 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003303 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08003304
3305 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04003306 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08003307 status = nfserr_clientid_busy;
3308 goto out;
3309 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04003310 status = mark_client_expired_locked(conf);
3311 if (status)
3312 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003313 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08003314 } else if (unconf)
3315 clp = unconf;
3316 else {
3317 status = nfserr_stale_clientid;
3318 goto out;
3319 }
Andrew Elblededeb132016-06-15 12:52:08 -04003320 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003321 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003322 status = nfserr_wrong_cred;
3323 goto out;
3324 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003325 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003326out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003327 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003328 if (clp)
3329 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003330 return status;
3331}
3332
Andy Adamson069b6ad2009-04-03 08:27:58 +03003333__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003334nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3335 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003336{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003337 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003338 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003339
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003340 if (rc->rca_one_fs) {
3341 if (!cstate->current_fh.fh_dentry)
3342 return nfserr_nofilehandle;
3343 /*
3344 * We don't take advantage of the rca_one_fs case.
3345 * That's OK, it's optional, we can safely ignore it.
3346 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02003347 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003348 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003349
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003350 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04003351 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3352 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003353 goto out;
3354
3355 status = nfserr_stale_clientid;
3356 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003357 /*
3358 * The following error isn't really legal.
3359 * But we only get here if the client just explicitly
3360 * destroyed the client. Surely it no longer cares what
3361 * error it gets back on an operation for the dead
3362 * client.
3363 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003364 goto out;
3365
3366 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04003367 nfsd4_client_record_create(cstate->session->se_client);
Scott Mayhew362063a2019-03-26 18:06:28 -04003368 inc_reclaim_complete(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003369out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003370 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003371}
3372
3373__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003374nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003375 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003377 struct nfsd4_setclientid *setclid = &u->setclientid;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003378 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003380 struct nfs4_client *conf, *new;
3381 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003382 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003383 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3384
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003385 new = create_client(clname, rqstp, &clverifier);
3386 if (new == NULL)
3387 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003388 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003389 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003390 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04003391 if (conf && client_has_state(conf)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003392 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003394 if (clp_used_exchangeid(conf))
3395 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003396 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04003397 char addr_str[INET6_ADDRSTRLEN];
3398 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3399 sizeof(addr_str));
3400 dprintk("NFSD: setclientid: string in use by client "
3401 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 goto out;
3403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003405 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003406 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003407 unhash_client_locked(unconf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003408 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003409 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003411 gen_confirm(new, nn);
3412 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003413 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003414 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003415 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003416 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3418 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3419 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003420 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 status = nfs_ok;
3422out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003423 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003424 if (new)
3425 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003426 if (unconf)
3427 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 return status;
3429}
3430
3431
Al Virob37ad282006-10-19 23:28:59 -07003432__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003433nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003434 struct nfsd4_compound_state *cstate,
3435 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003437 struct nfsd4_setclientid_confirm *setclientid_confirm =
3438 &u->setclientid_confirm;
NeilBrown21ab45a2005-06-23 22:04:14 -07003439 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003440 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3442 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003443 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003444 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003446 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003448
Jeff Laytond20c11d2014-07-30 08:27:07 -04003449 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003450 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003451 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003452 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003453 * We try hard to give out unique clientid's, so if we get an
3454 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003455 * the client may be buggy; this should never happen.
3456 *
3457 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003458 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003459 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04003460 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3461 goto out;
3462 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3463 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003464 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003465 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003466 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3467 /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 status = nfs_ok;
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003469 } else /* case 4: client hasn't noticed we rebooted yet? */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003470 status = nfserr_stale_clientid;
3471 goto out;
3472 }
3473 status = nfs_ok;
3474 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003475 old = unconf;
3476 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003477 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003478 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003479 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3480 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04003481 status = nfserr_clid_inuse;
3482 if (client_has_state(old)
3483 && !same_creds(&unconf->cl_cred,
3484 &old->cl_cred))
3485 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003486 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003487 if (status) {
3488 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003489 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003490 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003491 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003492 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003493 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003494 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003495 get_client_locked(conf);
3496 spin_unlock(&nn->client_lock);
3497 nfsd4_probe_callback(conf);
3498 spin_lock(&nn->client_lock);
3499 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003501 spin_unlock(&nn->client_lock);
3502 if (old)
3503 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 return status;
3505}
3506
J. Bruce Fields32513b42011-10-13 16:00:16 -04003507static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003509 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3510}
3511
3512/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003513static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3514 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003515{
Trond Myklebust950e0112014-06-30 11:48:31 -04003516 lockdep_assert_held(&state_lock);
3517
Elena Reshetova818a34e2017-10-20 12:53:30 +03003518 refcount_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003519 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003520 INIT_LIST_HEAD(&fp->fi_stateids);
3521 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02003522 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003523 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003524 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003525 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003526 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003527 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3528 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003529#ifdef CONFIG_NFSD_PNFS
3530 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02003531 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003532#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04003533 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534}
3535
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003536void
NeilBrowne60d4392005-06-23 22:03:01 -07003537nfsd4_free_slabs(void)
3538{
Jeff Layton9258a2d2018-03-16 09:47:22 -04003539 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003540 kmem_cache_destroy(openowner_slab);
3541 kmem_cache_destroy(lockowner_slab);
3542 kmem_cache_destroy(file_slab);
3543 kmem_cache_destroy(stateid_slab);
3544 kmem_cache_destroy(deleg_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04003545 kmem_cache_destroy(odstate_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003546}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547
Bryan Schumaker72083392011-11-01 15:24:59 -04003548int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549nfsd4_init_slabs(void)
3550{
Jeff Layton9258a2d2018-03-16 09:47:22 -04003551 client_slab = kmem_cache_create("nfsd4_clients",
3552 sizeof(struct nfs4_client), 0, 0, NULL);
3553 if (client_slab == NULL)
3554 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003555 openowner_slab = kmem_cache_create("nfsd4_openowners",
3556 sizeof(struct nfs4_openowner), 0, 0, NULL);
3557 if (openowner_slab == NULL)
Jeff Layton9258a2d2018-03-16 09:47:22 -04003558 goto out_free_client_slab;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003559 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003560 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003561 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003562 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003563 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003564 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003565 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003566 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003567 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003568 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003569 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003570 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003571 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003572 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003573 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003574 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003575 odstate_slab = kmem_cache_create("nfsd4_odstate",
3576 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3577 if (odstate_slab == NULL)
3578 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003580
Sachin Bhamare8287f002015-04-27 14:50:14 +02003581out_free_deleg_slab:
3582 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003583out_free_stateid_slab:
3584 kmem_cache_destroy(stateid_slab);
3585out_free_file_slab:
3586 kmem_cache_destroy(file_slab);
3587out_free_lockowner_slab:
3588 kmem_cache_destroy(lockowner_slab);
3589out_free_openowner_slab:
3590 kmem_cache_destroy(openowner_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04003591out_free_client_slab:
3592 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003593out:
NeilBrowne60d4392005-06-23 22:03:01 -07003594 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3595 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596}
3597
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003598static void init_nfs4_replay(struct nfs4_replay *rp)
3599{
3600 rp->rp_status = nfserr_serverfault;
3601 rp->rp_buflen = 0;
3602 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003603 mutex_init(&rp->rp_mutex);
3604}
3605
3606static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3607 struct nfs4_stateowner *so)
3608{
3609 if (!nfsd4_has_session(cstate)) {
3610 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003611 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04003612 }
3613}
3614
3615void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3616{
3617 struct nfs4_stateowner *so = cstate->replay_owner;
3618
3619 if (so != NULL) {
3620 cstate->replay_owner = NULL;
3621 mutex_unlock(&so->so_replay.rp_mutex);
3622 nfs4_put_stateowner(so);
3623 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003624}
3625
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003626static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627{
3628 struct nfs4_stateowner *sop;
3629
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003630 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003631 if (!sop)
3632 return NULL;
3633
3634 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3635 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003636 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003637 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003639 sop->so_owner.len = owner->len;
3640
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003641 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003642 sop->so_client = clp;
3643 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003644 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003645 return sop;
3646}
3647
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003648static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003649{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003650 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003651
Trond Myklebustd4f04892014-07-29 21:34:36 -04003652 list_add(&oo->oo_owner.so_strhash,
3653 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003654 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655}
3656
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003657static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3658{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003659 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003660}
3661
Jeff Layton6b180f02014-07-29 21:34:26 -04003662static void nfs4_free_openowner(struct nfs4_stateowner *so)
3663{
3664 struct nfs4_openowner *oo = openowner(so);
3665
3666 kmem_cache_free(openowner_slab, oo);
3667}
3668
3669static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003670 .so_unhash = nfs4_unhash_openowner,
3671 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003672};
3673
Andrew Elble7fc05642015-11-05 20:42:43 -05003674static struct nfs4_ol_stateid *
3675nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3676{
3677 struct nfs4_ol_stateid *local, *ret = NULL;
3678 struct nfs4_openowner *oo = open->op_openowner;
3679
3680 lockdep_assert_held(&fp->fi_lock);
3681
3682 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3683 /* ignore lock owners */
3684 if (local->st_stateowner->so_is_open_owner == 0)
3685 continue;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003686 if (local->st_stateowner != &oo->oo_owner)
3687 continue;
3688 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
Andrew Elble7fc05642015-11-05 20:42:43 -05003689 ret = local;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03003690 refcount_inc(&ret->st_stid.sc_count);
Andrew Elble7fc05642015-11-05 20:42:43 -05003691 break;
3692 }
3693 }
3694 return ret;
3695}
3696
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003697static __be32
3698nfsd4_verify_open_stid(struct nfs4_stid *s)
3699{
3700 __be32 ret = nfs_ok;
3701
3702 switch (s->sc_type) {
3703 default:
3704 break;
Trond Myklebust4f176412018-01-12 17:42:30 -05003705 case 0:
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003706 case NFS4_CLOSED_STID:
3707 case NFS4_CLOSED_DELEG_STID:
3708 ret = nfserr_bad_stateid;
3709 break;
3710 case NFS4_REVOKED_DELEG_STID:
3711 ret = nfserr_deleg_revoked;
3712 }
3713 return ret;
3714}
3715
3716/* Lock the stateid st_mutex, and deal with races with CLOSE */
3717static __be32
3718nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
3719{
3720 __be32 ret;
3721
Andrew Elble4f34bd02017-11-08 17:29:51 -05003722 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003723 ret = nfsd4_verify_open_stid(&stp->st_stid);
3724 if (ret != nfs_ok)
3725 mutex_unlock(&stp->st_mutex);
3726 return ret;
3727}
3728
3729static struct nfs4_ol_stateid *
3730nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3731{
3732 struct nfs4_ol_stateid *stp;
3733 for (;;) {
3734 spin_lock(&fp->fi_lock);
3735 stp = nfsd4_find_existing_open(fp, open);
3736 spin_unlock(&fp->fi_lock);
3737 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
3738 break;
3739 nfs4_put_stid(&stp->st_stid);
3740 }
3741 return stp;
3742}
3743
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003744static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003745alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003746 struct nfsd4_compound_state *cstate)
3747{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003748 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003749 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003751 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3752 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003754 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003755 oo->oo_owner.so_is_open_owner = 1;
3756 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003757 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003758 if (nfsd4_has_session(cstate))
3759 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003760 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003761 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003762 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003763 spin_lock(&clp->cl_lock);
3764 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003765 if (ret == NULL) {
3766 hash_openowner(oo, clp, strhashval);
3767 ret = oo;
3768 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08003769 nfs4_free_stateowner(&oo->oo_owner);
3770
Trond Myklebustd4f04892014-07-29 21:34:36 -04003771 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04003772 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773}
3774
Andrew Elble7fc05642015-11-05 20:42:43 -05003775static struct nfs4_ol_stateid *
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003776init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
Andrew Elble7fc05642015-11-05 20:42:43 -05003777{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778
Andrew Elble7fc05642015-11-05 20:42:43 -05003779 struct nfs4_openowner *oo = open->op_openowner;
3780 struct nfs4_ol_stateid *retstp = NULL;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003781 struct nfs4_ol_stateid *stp;
Andrew Elble7fc05642015-11-05 20:42:43 -05003782
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003783 stp = open->op_stp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003784 /* We are moving these outside of the spinlocks to avoid the warnings */
3785 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05003786 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003787
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003788retry:
Andrew Elble7fc05642015-11-05 20:42:43 -05003789 spin_lock(&oo->oo_owner.so_client->cl_lock);
3790 spin_lock(&fp->fi_lock);
3791
3792 retstp = nfsd4_find_existing_open(fp, open);
3793 if (retstp)
3794 goto out_unlock;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003795
3796 open->op_stp = NULL;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03003797 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003798 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003799 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003800 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07003801 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003802 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 stp->st_access_bmap = 0;
3804 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003805 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003806 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003807 list_add(&stp->st_perfile, &fp->fi_stateids);
Andrew Elble7fc05642015-11-05 20:42:43 -05003808
3809out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04003810 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003811 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003812 if (retstp) {
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003813 /* Handle races with CLOSE */
3814 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
3815 nfs4_put_stid(&retstp->st_stid);
3816 goto retry;
3817 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003818 /* To keep mutex tracking happy */
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003819 mutex_unlock(&stp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003820 stp = retstp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003821 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003822 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823}
3824
Jeff Laytond3134b12014-07-29 21:34:32 -04003825/*
3826 * In the 4.0 case we need to keep the owners around a little while to handle
3827 * CLOSE replay. We still do need to release any file access that is held by
3828 * them before returning however.
3829 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003831move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832{
Jeff Layton217526e2014-07-30 08:27:11 -04003833 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003834 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3835 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3836 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003837
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003838 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839
Jeff Laytonb401be222014-07-29 21:34:33 -04003840 /*
3841 * We know that we hold one reference via nfsd4_close, and another
3842 * "persistent" reference for the client. If the refcount is higher
3843 * than 2, then there are still calls in progress that are using this
3844 * stateid. We can't put the sc_file reference until they are finished.
3845 * Wait for the refcount to drop to 2. Since it has been unhashed,
3846 * there should be no danger of the refcount going back up again at
3847 * this point.
3848 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03003849 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
Jeff Laytonb401be222014-07-29 21:34:33 -04003850
Jeff Laytond3134b12014-07-29 21:34:32 -04003851 release_all_access(s);
3852 if (s->st_stid.sc_file) {
3853 put_nfs4_file(s->st_stid.sc_file);
3854 s->st_stid.sc_file = NULL;
3855 }
Jeff Layton217526e2014-07-30 08:27:11 -04003856
3857 spin_lock(&nn->client_lock);
3858 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003859 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003860 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003861 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003862 spin_unlock(&nn->client_lock);
3863 if (last)
3864 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865}
3866
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867/* search file_hashtbl[] for file */
3868static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04003869find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 struct nfs4_file *fp;
3872
Jeff Layton5b095e92014-10-23 08:01:02 -04003873 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02003874 if (fh_match(&fp->fi_fhandle, fh)) {
Elena Reshetova818a34e2017-10-20 12:53:30 +03003875 if (refcount_inc_not_zero(&fp->fi_ref))
Jeff Layton5b095e92014-10-23 08:01:02 -04003876 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 }
3879 return NULL;
3880}
3881
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02003882struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003883find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003884{
3885 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003886 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003887
Jeff Layton5b095e92014-10-23 08:01:02 -04003888 rcu_read_lock();
3889 fp = find_file_locked(fh, hashval);
3890 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04003891 return fp;
3892}
3893
3894static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003895find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003896{
3897 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003898 unsigned int hashval = file_hashval(fh);
3899
3900 rcu_read_lock();
3901 fp = find_file_locked(fh, hashval);
3902 rcu_read_unlock();
3903 if (fp)
3904 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04003905
3906 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04003907 fp = find_file_locked(fh, hashval);
3908 if (likely(fp == NULL)) {
3909 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04003910 fp = new;
3911 }
3912 spin_unlock(&state_lock);
3913
3914 return fp;
3915}
3916
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003917/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 * Called to check deny when READ with all zero stateid or
3919 * WRITE with all zero or all one stateid
3920 */
Al Virob37ad282006-10-19 23:28:59 -07003921static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3923{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003925 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926
Trond Myklebustca943212014-07-23 16:17:39 -04003927 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003928 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003929 return ret;
3930 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003931 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003932 if (fp->fi_share_deny & deny_type)
3933 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003934 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003935 put_nfs4_file(fp);
3936 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937}
3938
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003939static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003941 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04003942 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3943 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003944
Trond Myklebust11b91642014-07-29 21:34:08 -04003945 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003946
Jeff Layton02e12152014-07-16 10:31:57 -04003947 /*
3948 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003949 * already holding inode->i_lock.
3950 *
Jeff Laytondff13992014-07-08 14:02:49 -04003951 * If the dl_time != 0, then we know that it has already been
3952 * queued for a lease break. Don't queue it again.
3953 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003954 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003955 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003956 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003957 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003958 }
Jeff Layton02e12152014-07-16 10:31:57 -04003959 spin_unlock(&state_lock);
3960}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003962static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3963 struct rpc_task *task)
3964{
3965 struct nfs4_delegation *dp = cb_to_delegation(cb);
3966
Andrew Elblea4579742015-08-31 12:06:41 -04003967 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3968 return 1;
3969
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003970 switch (task->tk_status) {
3971 case 0:
3972 return 1;
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04003973 case -NFS4ERR_DELAY:
3974 rpc_delay(task, 2 * HZ);
3975 return 0;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003976 case -EBADHANDLE:
3977 case -NFS4ERR_BAD_STATEID:
3978 /*
3979 * Race: client probably got cb_recall before open reply
3980 * granting delegation.
3981 */
3982 if (dp->dl_retries--) {
3983 rpc_delay(task, 2 * HZ);
3984 return 0;
3985 }
3986 /*FALLTHRU*/
3987 default:
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04003988 return 1;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003989 }
3990}
3991
3992static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3993{
3994 struct nfs4_delegation *dp = cb_to_delegation(cb);
3995
3996 nfs4_put_stid(&dp->dl_stid);
3997}
3998
Julia Lawallc4cb8972015-11-21 22:57:39 +01003999static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004000 .prepare = nfsd4_cb_recall_prepare,
4001 .done = nfsd4_cb_recall_done,
4002 .release = nfsd4_cb_recall_release,
4003};
4004
Jeff Layton02e12152014-07-16 10:31:57 -04004005static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4006{
4007 /*
4008 * We're assuming the state code never drops its reference
4009 * without first removing the lease. Since we're in this lease
J. Bruce Fields4a269efb2018-04-13 17:34:35 -04004010 * callback (and since the lease code is serialized by the
4011 * i_lock) we know the server hasn't removed the lease yet, and
4012 * we know it's safe to take a reference.
Jeff Layton02e12152014-07-16 10:31:57 -04004013 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004014 refcount_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02004015 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004016}
4017
Jeff Layton1c8c6012013-06-21 08:58:15 -04004018/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004019static bool
4020nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004021{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004022 bool ret = false;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004023 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4024 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004025
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004026 /*
4027 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004028 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004029 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004030 */
4031 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032
Jeff Layton02e12152014-07-16 10:31:57 -04004033 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004034 fp->fi_had_conflict = true;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004035 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04004036 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004037 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038}
4039
Jeff Laytonc45198e2014-09-01 07:12:07 -04004040static int
Jeff Layton7448cc32015-01-16 15:05:57 -05004041nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4042 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043{
4044 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04004045 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 else
4047 return -EAGAIN;
4048}
4049
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004050static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04004051 .lm_break = nfsd_break_deleg_cb,
4052 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053};
4054
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004055static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4056{
4057 if (nfsd4_has_session(cstate))
4058 return nfs_ok;
4059 if (seqid == so->so_seqid - 1)
4060 return nfserr_replay_me;
4061 if (seqid == so->so_seqid)
4062 return nfs_ok;
4063 return nfserr_bad_seqid;
4064}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065
Jeff Layton4b24ca72014-06-30 11:48:44 -04004066static __be32 lookup_clientid(clientid_t *clid,
4067 struct nfsd4_compound_state *cstate,
4068 struct nfsd_net *nn)
4069{
4070 struct nfs4_client *found;
4071
4072 if (cstate->clp) {
4073 found = cstate->clp;
4074 if (!same_clid(&found->cl_clientid, clid))
4075 return nfserr_stale_clientid;
4076 return nfs_ok;
4077 }
4078
4079 if (STALE_CLIENTID(clid, nn))
4080 return nfserr_stale_clientid;
4081
4082 /*
4083 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4084 * cached already then we know this is for is for v4.0 and "sessions"
4085 * will be false.
4086 */
4087 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004088 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004089 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004090 if (!found) {
4091 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004092 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04004093 }
4094 atomic_inc(&found->cl_refcount);
4095 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004096
4097 /* Cache the nfs4_client in cstate! */
4098 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004099 return nfs_ok;
4100}
4101
Al Virob37ad282006-10-19 23:28:59 -07004102__be32
Andy Adamson66689582009-04-03 08:28:45 +03004103nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004104 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 clientid_t *clientid = &open->op_clientid;
4107 struct nfs4_client *clp = NULL;
4108 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004109 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004110 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004112 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004114 /*
4115 * In case we need it later, after we've already created the
4116 * file and don't want to risk a further failure:
4117 */
4118 open->op_file = nfsd4_alloc_file();
4119 if (open->op_file == NULL)
4120 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
Trond Myklebust2d91e892014-06-30 11:48:46 -04004122 status = lookup_clientid(clientid, cstate, nn);
4123 if (status)
4124 return status;
4125 clp = cstate->clp;
4126
Trond Myklebustd4f04892014-07-29 21:34:36 -04004127 strhashval = ownerstr_hashval(&open->op_owner);
4128 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004129 open->op_openowner = oo;
4130 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004131 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004133 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004134 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004135 release_openowner(oo);
4136 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004137 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004138 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004139 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4140 if (status)
4141 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004142 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004143new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04004144 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004145 if (oo == NULL)
4146 return nfserr_jukebox;
4147 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004148alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04004149 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004150 if (!open->op_stp)
4151 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004152
4153 if (nfsd4_has_session(cstate) &&
4154 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4155 open->op_odstate = alloc_clnt_odstate(clp);
4156 if (!open->op_odstate)
4157 return nfserr_jukebox;
4158 }
4159
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004160 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161}
4162
Al Virob37ad282006-10-19 23:28:59 -07004163static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07004164nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4165{
4166 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4167 return nfserr_openmode;
4168 else
4169 return nfs_ok;
4170}
4171
Daniel Mackc47d8322011-05-16 16:38:14 +02004172static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04004173{
J. Bruce Fields24a01112010-05-18 20:01:35 -04004174 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4175}
4176
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004177static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004178{
4179 struct nfs4_stid *ret;
4180
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004181 ret = find_stateid_by_type(cl, s,
4182 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004183 if (!ret)
4184 return NULL;
4185 return delegstateid(ret);
4186}
4187
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004188static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4189{
4190 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4191 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4192}
4193
Al Virob37ad282006-10-19 23:28:59 -07004194static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04004195nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07004196 struct nfs4_delegation **dp)
4197{
4198 int flags;
Al Virob37ad282006-10-19 23:28:59 -07004199 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004200 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07004201
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004202 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4203 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07004204 goto out;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004205 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4206 nfs4_put_stid(&deleg->dl_stid);
4207 if (cl->cl_minorversion)
4208 status = nfserr_deleg_revoked;
4209 goto out;
4210 }
J. Bruce Fields24a01112010-05-18 20:01:35 -04004211 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004212 status = nfs4_check_delegmode(deleg, flags);
4213 if (status) {
4214 nfs4_put_stid(&deleg->dl_stid);
4215 goto out;
4216 }
4217 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004218out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004219 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004220 return nfs_ok;
4221 if (status)
4222 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004223 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004224 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07004225}
4226
J. Bruce Fields21fb4012010-07-28 12:21:23 -04004227static inline int nfs4_access_to_access(u32 nfs4_access)
4228{
4229 int flags = 0;
4230
4231 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4232 flags |= NFSD_MAY_READ;
4233 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4234 flags |= NFSD_MAY_WRITE;
4235 return flags;
4236}
4237
Al Virob37ad282006-10-19 23:28:59 -07004238static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4240 struct nfsd4_open *open)
4241{
4242 struct iattr iattr = {
4243 .ia_valid = ATTR_SIZE,
4244 .ia_size = 0,
4245 };
4246 if (!open->op_truncate)
4247 return 0;
4248 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08004249 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
4251}
4252
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004253static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004254 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4255 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004256{
Trond Myklebustde186432014-07-10 14:07:26 -04004257 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004258 __be32 status;
4259 int oflag = nfs4_access_to_omode(open->op_share_access);
4260 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004261 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004262
Trond Myklebustde186432014-07-10 14:07:26 -04004263 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004264
4265 /*
4266 * Are we trying to set a deny mode that would conflict with
4267 * current access?
4268 */
4269 status = nfs4_file_check_deny(fp, open->op_share_deny);
4270 if (status != nfs_ok) {
4271 spin_unlock(&fp->fi_lock);
4272 goto out;
4273 }
4274
4275 /* set access to the file */
4276 status = nfs4_file_get_access(fp, open->op_share_access);
4277 if (status != nfs_ok) {
4278 spin_unlock(&fp->fi_lock);
4279 goto out;
4280 }
4281
4282 /* Set access bits in stateid */
4283 old_access_bmap = stp->st_access_bmap;
4284 set_access(open->op_share_access, stp);
4285
4286 /* Set new deny mask */
4287 old_deny_bmap = stp->st_deny_bmap;
4288 set_deny(open->op_share_deny, stp);
4289 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4290
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004291 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04004292 spin_unlock(&fp->fi_lock);
4293 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004294 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004295 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04004296 spin_lock(&fp->fi_lock);
4297 if (!fp->fi_fds[oflag]) {
4298 fp->fi_fds[oflag] = filp;
4299 filp = NULL;
4300 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004301 }
Trond Myklebustde186432014-07-10 14:07:26 -04004302 spin_unlock(&fp->fi_lock);
4303 if (filp)
4304 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004305
4306 status = nfsd4_truncate(rqstp, cur_fh, open);
4307 if (status)
4308 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004309out:
4310 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004311out_put_access:
4312 stp->st_access_bmap = old_access_bmap;
4313 nfs4_file_put_access(fp, open->op_share_access);
4314 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4315 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004316}
4317
Al Virob37ad282006-10-19 23:28:59 -07004318static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004319nfs4_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 -07004320{
Al Virob37ad282006-10-19 23:28:59 -07004321 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02004322 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004324 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004325 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004326
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004327 /* test and set deny mode */
4328 spin_lock(&fp->fi_lock);
4329 status = nfs4_file_check_deny(fp, open->op_share_deny);
4330 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004331 set_deny(open->op_share_deny, stp);
4332 fp->fi_share_deny |=
4333 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4334 }
4335 spin_unlock(&fp->fi_lock);
4336
4337 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004340 status = nfsd4_truncate(rqstp, cur_fh, open);
4341 if (status != nfs_ok)
4342 reset_union_bmap_deny(old_deny_bmap, stp);
4343 return status;
4344}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004346/* Should we give out recallable state?: */
4347static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4348{
4349 if (clp->cl_cb_state == NFSD4_CB_UP)
4350 return true;
4351 /*
4352 * In the sessions case, since we don't have to establish a
4353 * separate connection for callbacks, we assume it's OK
4354 * until we hear otherwise:
4355 */
4356 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4357}
4358
J. Bruce Fields653e5142018-02-15 22:08:45 -05004359static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4360 int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004361{
4362 struct file_lock *fl;
4363
4364 fl = locks_alloc_lock();
4365 if (!fl)
4366 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004367 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04004368 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004369 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4370 fl->fl_end = OFFSET_MAX;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004371 fl->fl_owner = (fl_owner_t)dp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004372 fl->fl_pid = current->tgid;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004373 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004374 return fl;
4375}
4376
Jeff Layton0b266932014-07-25 07:34:25 -04004377static struct nfs4_delegation *
4378nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004379 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004380{
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004381 int status = 0;
Jeff Layton0b266932014-07-25 07:34:25 -04004382 struct nfs4_delegation *dp;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004383 struct file *filp;
4384 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04004385
J. Bruce Fields353601e2018-02-16 14:29:42 -05004386 /*
4387 * The fi_had_conflict and nfs_get_existing_delegation checks
4388 * here are just optimizations; we'll need to recheck them at
4389 * the end:
4390 */
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004391 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004392 return ERR_PTR(-EAGAIN);
4393
J. Bruce Fields353601e2018-02-16 14:29:42 -05004394 filp = find_readable_file(fp);
4395 if (!filp) {
4396 /* We should always have a readable file here */
4397 WARN_ON_ONCE(1);
4398 return ERR_PTR(-EBADF);
4399 }
Andrew Elble34ed9872015-10-15 12:07:28 -04004400 spin_lock(&state_lock);
4401 spin_lock(&fp->fi_lock);
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004402 if (nfs4_delegation_exists(clp, fp))
4403 status = -EAGAIN;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004404 else if (!fp->fi_deleg_file) {
4405 fp->fi_deleg_file = filp;
4406 /* increment early to prevent fi_deleg_file from being
4407 * cleared */
4408 fp->fi_delegees = 1;
4409 filp = NULL;
4410 } else
4411 fp->fi_delegees++;
4412 spin_unlock(&fp->fi_lock);
4413 spin_unlock(&state_lock);
4414 if (filp)
4415 fput(filp);
4416 if (status)
4417 return ERR_PTR(status);
4418
4419 status = -ENOMEM;
4420 dp = alloc_init_deleg(clp, fp, fh, odstate);
4421 if (!dp)
4422 goto out_delegees;
4423
4424 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
4425 if (!fl)
Andrew Elblebd8d7252018-10-05 09:32:08 -04004426 goto out_clnt_odstate;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004427
4428 status = vfs_setlease(fp->fi_deleg_file, fl->fl_type, &fl, NULL);
4429 if (fl)
4430 locks_free_lock(fl);
4431 if (status)
4432 goto out_clnt_odstate;
4433
4434 spin_lock(&state_lock);
4435 spin_lock(&fp->fi_lock);
4436 if (fp->fi_had_conflict)
4437 status = -EAGAIN;
4438 else
4439 status = hash_delegation_locked(dp, fp);
Andrew Elble34ed9872015-10-15 12:07:28 -04004440 spin_unlock(&fp->fi_lock);
4441 spin_unlock(&state_lock);
4442
4443 if (status)
Andrew Elble692ad282018-04-18 17:04:37 -04004444 goto out_unlock;
4445
Jeff Layton0b266932014-07-25 07:34:25 -04004446 return dp;
Andrew Elble692ad282018-04-18 17:04:37 -04004447out_unlock:
4448 vfs_setlease(fp->fi_deleg_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004449out_clnt_odstate:
4450 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004451 nfs4_put_stid(&dp->dl_stid);
4452out_delegees:
4453 put_deleg_file(fp);
4454 return ERR_PTR(status);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004455}
4456
Benny Halevy4aa89132012-02-21 14:16:44 -08004457static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4458{
4459 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4460 if (status == -EAGAIN)
4461 open->op_why_no_deleg = WND4_CONTENTION;
4462 else {
4463 open->op_why_no_deleg = WND4_RESOURCE;
4464 switch (open->op_deleg_want) {
4465 case NFS4_SHARE_WANT_READ_DELEG:
4466 case NFS4_SHARE_WANT_WRITE_DELEG:
4467 case NFS4_SHARE_WANT_ANY_DELEG:
4468 break;
4469 case NFS4_SHARE_WANT_CANCEL:
4470 open->op_why_no_deleg = WND4_CANCELLED;
4471 break;
4472 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004473 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08004474 }
4475 }
4476}
4477
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478/*
4479 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04004480 *
4481 * Note we don't support write delegations, and won't until the vfs has
4482 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 */
4484static void
Jeff Layton4cf59222014-07-25 07:34:24 -04004485nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4486 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487{
4488 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04004489 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4490 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004491 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004492 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004494 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07004495 open->op_recall = 0;
4496 switch (open->op_claim_type) {
4497 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05004498 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07004499 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004500 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4501 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004502 break;
4503 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00004504 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004505 /*
4506 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004507 * had the chance to reclaim theirs, *and* until
4508 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004509 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004510 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004511 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004512 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004513 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04004514 /*
4515 * Also, if the file was opened for write or
4516 * create, there's a good chance the client's
4517 * about to write to it, resulting in an
4518 * immediate recall (since we don't support
4519 * write delegations):
4520 */
NeilBrown7b190fe2005-06-23 22:03:23 -07004521 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04004522 goto out_no_deleg;
4523 if (open->op_create == NFS4_OPEN_CREATE)
4524 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004525 break;
4526 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004527 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004528 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004529 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004530 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004531 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004533 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004535 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004536 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04004537 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04004538 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004539 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004540out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004541 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4542 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004543 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04004544 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004545 open->op_recall = 1;
4546 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004547
4548 /* 4.1 client asking for a delegation? */
4549 if (open->op_deleg_want)
4550 nfsd4_open_deleg_none_ext(open, status);
4551 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552}
4553
Benny Halevye27f49c2012-02-21 14:16:54 -08004554static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4555 struct nfs4_delegation *dp)
4556{
4557 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4558 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4559 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4560 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4561 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4562 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4563 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4564 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4565 }
4566 /* Otherwise the client must be confused wanting a delegation
4567 * it already has, therefore we don't return
4568 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4569 */
4570}
4571
Al Virob37ad282006-10-19 23:28:59 -07004572__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4574{
Andy Adamson66689582009-04-03 08:28:45 +03004575 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004576 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004578 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004579 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004580 __be32 status;
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004581 bool new_stp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 /*
4584 * Lookup file; if found, lookup stateid and check open request,
4585 * and check for delegations in the process of being recalled.
4586 * If not found, create the nfs4_file struct
4587 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004588 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004589 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004590 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004591 if (status)
4592 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004593 stp = nfsd4_find_and_lock_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004595 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004596 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004597 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004598 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 }
4600
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004601 if (!stp) {
4602 stp = init_open_stateid(fp, open);
4603 if (!open->op_stp)
4604 new_stp = true;
4605 }
4606
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 /*
4608 * OPEN the file, or upgrade an existing OPEN.
4609 * If truncate fails, the OPEN fails.
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004610 *
4611 * stp is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 */
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004613 if (!new_stp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004615 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04004616 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004617 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04004619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 } else {
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004621 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4622 if (status) {
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004623 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004624 release_open_stateid(stp);
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004625 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004626 goto out;
4627 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004628
4629 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4630 open->op_odstate);
4631 if (stp->st_clnt_odstate == open->op_odstate)
4632 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 }
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004634
Jeff Layton9767feb2015-10-01 09:05:50 -04004635 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004636 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637
Benny Halevyd24433c2012-02-16 20:57:17 +02004638 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004639 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4640 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4641 open->op_why_no_deleg = WND4_NOT_WANTED;
4642 goto nodeleg;
4643 }
4644 }
4645
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 /*
4647 * Attempt to hand out a delegation. No error return, because the
4648 * OPEN succeeds even if we fail.
4649 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004650 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004651nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 status = nfs_ok;
4653
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004654 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004655 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004657 /* 4.1 client trying to upgrade/downgrade delegation? */
4658 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004659 open->op_deleg_want)
4660 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004661
NeilBrown13cd2182005-06-23 22:03:10 -07004662 if (fp)
4663 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004664 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08004665 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 /*
4667 * To finish the open response, we just need to set the rflags.
4668 */
4669 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Jeff Layton19e4c342016-09-16 16:28:27 -04004670 if (nfsd4_has_session(&resp->cstate))
4671 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
4672 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Jeff Layton19e4c342016-09-16 16:28:27 -04004674
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004675 if (dp)
4676 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004677 if (stp)
4678 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679
4680 return status;
4681}
4682
Jeff Layton58fb12e2014-07-29 21:34:27 -04004683void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04004684 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004685{
4686 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004687 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004688
Jeff Laytond3134b12014-07-29 21:34:32 -04004689 nfsd4_cstate_assign_replay(cstate, so);
4690 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004691 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004692 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04004693 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004694 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004695 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004696 if (open->op_odstate)
4697 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004698}
4699
Al Virob37ad282006-10-19 23:28:59 -07004700__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004701nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02004702 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703{
Christoph Hellwigeb698532017-05-08 20:58:35 +02004704 clientid_t *clid = &u->renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004706 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004707 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 dprintk("process_renew(%08x/%08x): starting\n",
4710 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004711 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004712 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004714 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004716 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004717 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 goto out;
4719 status = nfs_ok;
4720out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 return status;
4722}
4723
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004724void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004725nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004726{
Jeff Layton33dcc482012-04-10 11:08:48 -04004727 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004728 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004729 return;
4730
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004731 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004732 /*
4733 * If the server goes down again right now, an NFSv4
4734 * client will still be allowed to reclaim after it comes back up,
4735 * even if it hasn't yet had a chance to reclaim state this time.
4736 *
4737 */
Jeff Layton919b8042014-09-12 16:40:20 -04004738 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004739 /*
4740 * At this point, NFSv4 clients can still reclaim. But if the
4741 * server crashes, any that have not yet reclaimed will be out
4742 * of luck on the next boot.
4743 *
4744 * (NFSv4.1+ clients are considered to have reclaimed once they
4745 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4746 * have reclaimed after their first OPEN.)
4747 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004748 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004749 /*
4750 * At this point, and once lockd and/or any other containers
4751 * exit their grace period, further reclaims will fail and
4752 * regular locking can resume.
4753 */
NeilBrowna76b4312005-06-23 22:04:01 -07004754}
4755
J. Bruce Fields03f318c2018-06-08 12:28:47 -04004756/*
4757 * If we've waited a lease period but there are still clients trying to
4758 * reclaim, wait a little longer to give them a chance to finish.
4759 */
4760static bool clients_still_reclaiming(struct nfsd_net *nn)
4761{
4762 unsigned long now = get_seconds();
4763 unsigned long double_grace_period_end = nn->boot_time +
4764 2 * nn->nfsd4_lease;
4765
Scott Mayhew362063a2019-03-26 18:06:28 -04004766 if (nn->track_reclaim_completes &&
4767 atomic_read(&nn->nr_reclaim_complete) ==
4768 nn->reclaim_str_hashtbl_size)
4769 return false;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04004770 if (!nn->somebody_reclaimed)
4771 return false;
4772 nn->somebody_reclaimed = false;
4773 /*
4774 * If we've given them *two* lease times to reclaim, and they're
4775 * still not done, give up:
4776 */
4777 if (time_after(now, double_grace_period_end))
4778 return false;
4779 return true;
4780}
4781
NeilBrownfd39ca92005-06-23 22:04:03 -07004782static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004783nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784{
4785 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004786 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004788 struct nfs4_ol_stateid *stp;
Jeff Layton7919d0a2016-09-16 16:28:25 -04004789 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004791 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004792 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 dprintk("NFSD: laundromat service - starting\n");
J. Bruce Fields03f318c2018-06-08 12:28:47 -04004795
4796 if (clients_still_reclaiming(nn)) {
4797 new_timeo = 0;
4798 goto out;
4799 }
Scott Mayhew362063a2019-03-26 18:06:28 -04004800 dprintk("NFSD: end of grace period\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004801 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004802 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004803 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004804 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 clp = list_entry(pos, struct nfs4_client, cl_lru);
4806 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4807 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004808 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 break;
4810 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004811 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004812 dprintk("NFSD: client in use (clientid %08x)\n",
4813 clp->cl_clientid.cl_id);
4814 continue;
4815 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004816 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004817 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004818 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004819 list_for_each_safe(pos, next, &reaplist) {
4820 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 dprintk("NFSD: purging unused client (clientid %08x)\n",
4822 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004823 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 expire_client(clp);
4825 }
Benny Halevycdc975052014-05-30 09:09:30 -04004826 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004827 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4829 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004830 t = dp->dl_time - cutoff;
4831 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 break;
4833 }
Jeff Layton3fcbbd22015-08-24 12:41:48 -04004834 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04004835 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 }
Benny Halevycdc975052014-05-30 09:09:30 -04004837 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004838 while (!list_empty(&reaplist)) {
4839 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4840 dl_recall_lru);
4841 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004842 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 }
Jeff Layton217526e2014-07-30 08:27:11 -04004844
4845 spin_lock(&nn->client_lock);
4846 while (!list_empty(&nn->close_lru)) {
4847 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4848 oo_close_lru);
4849 if (time_after((unsigned long)oo->oo_time,
4850 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004851 t = oo->oo_time - cutoff;
4852 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 break;
4854 }
Jeff Layton217526e2014-07-30 08:27:11 -04004855 list_del_init(&oo->oo_close_lru);
4856 stp = oo->oo_last_closed_stid;
4857 oo->oo_last_closed_stid = NULL;
4858 spin_unlock(&nn->client_lock);
4859 nfs4_put_stid(&stp->st_stid);
4860 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 }
Jeff Layton217526e2014-07-30 08:27:11 -04004862 spin_unlock(&nn->client_lock);
4863
Jeff Layton7919d0a2016-09-16 16:28:25 -04004864 /*
4865 * It's possible for a client to try and acquire an already held lock
4866 * that is being held for a long time, and then lose interest in it.
4867 * So, we clean out any un-revisited request after a lease period
4868 * under the assumption that the client is no longer interested.
4869 *
4870 * RFC5661, sec. 9.6 states that the client must not rely on getting
4871 * notifications and must continue to poll for locks, even when the
4872 * server supports them. Thus this shouldn't lead to clients blocking
4873 * indefinitely once the lock does become free.
4874 */
4875 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04004876 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04004877 while (!list_empty(&nn->blocked_locks_lru)) {
4878 nbl = list_first_entry(&nn->blocked_locks_lru,
4879 struct nfsd4_blocked_lock, nbl_lru);
4880 if (time_after((unsigned long)nbl->nbl_time,
4881 (unsigned long)cutoff)) {
4882 t = nbl->nbl_time - cutoff;
4883 new_timeo = min(new_timeo, t);
4884 break;
4885 }
4886 list_move(&nbl->nbl_lru, &reaplist);
4887 list_del_init(&nbl->nbl_list);
4888 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04004889 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04004890
4891 while (!list_empty(&reaplist)) {
Naofumi Honda64ebe1242017-11-09 10:57:16 -05004892 nbl = list_first_entry(&reaplist,
Jeff Layton7919d0a2016-09-16 16:28:25 -04004893 struct nfsd4_blocked_lock, nbl_lru);
4894 list_del_init(&nbl->nbl_lru);
Jeff Layton7919d0a2016-09-16 16:28:25 -04004895 free_blocked_lock(nbl);
4896 }
J. Bruce Fields03f318c2018-06-08 12:28:47 -04004897out:
Jeff Laytona832e7a2014-05-30 09:09:26 -04004898 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004899 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900}
4901
Harvey Harrisona254b242008-02-20 12:49:00 -08004902static struct workqueue_struct *laundry_wq;
4903static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004904
4905static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004906laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907{
4908 time_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08004909 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004910 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4911 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004913 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004915 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916}
4917
Jeff Layton8fcd4612015-07-30 06:57:46 -04004918static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004919{
Jeff Layton8fcd4612015-07-30 06:57:46 -04004920 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004921 return nfserr_bad_stateid;
4922 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923}
4924
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004926access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004928 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4929 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4930 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931}
4932
4933static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004934access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004936 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4937 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938}
4939
4940static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004941__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942{
Al Virob37ad282006-10-19 23:28:59 -07004943 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944
J. Bruce Fields02921912010-07-29 15:16:59 -04004945 /* For lock stateid's, we test the parent open, not the lock: */
4946 if (stp->st_openstp)
4947 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004948 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004950 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 goto out;
4952 status = nfs_ok;
4953out:
4954 return status;
4955}
4956
Al Virob37ad282006-10-19 23:28:59 -07004957static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004958check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004960 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004962 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004963 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 * conflicting state; so we must wait out the grace period. */
4965 return nfserr_grace;
4966 } else if (flags & WR_STATE)
4967 return nfs4_share_conflict(current_fh,
4968 NFS4_SHARE_DENY_WRITE);
4969 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4970 return nfs4_share_conflict(current_fh,
4971 NFS4_SHARE_DENY_READ);
4972}
4973
4974/*
4975 * Allow READ/WRITE during grace period on recovered state only for files
4976 * that are not able to provide mandatory locking.
4977 */
4978static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004979grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004981 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982}
4983
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004984static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004985{
Andy Adamson66689582009-04-03 08:28:45 +03004986 /*
4987 * When sessions are used the stateid generation number is ignored
4988 * when it is zero.
4989 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004990 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004991 return nfs_ok;
4992
4993 if (in->si_generation == ref->si_generation)
4994 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004995
J. Bruce Fields0836f582008-01-26 19:08:12 -05004996 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04004997 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004998 return nfserr_bad_stateid;
4999 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04005000 * However, we could see a stateid from the past, even from a
5001 * non-buggy client. For example, if the client sends a lock
5002 * while some IO is outstanding, the lock may bump si_generation
5003 * while the IO is still in flight. The client could avoid that
5004 * situation by waiting for responses on all the IO requests,
5005 * but better performance may result in retrying IO that
5006 * receives an old_stateid error if requests are rarely
5007 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05005008 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04005009 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05005010}
5011
Trond Myklebust03da3162017-11-03 08:00:16 -04005012static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
5013{
5014 __be32 ret;
5015
5016 spin_lock(&s->sc_lock);
5017 ret = nfsd4_verify_open_stid(s);
5018 if (ret == nfs_ok)
5019 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
5020 spin_unlock(&s->sc_lock);
5021 return ret;
5022}
5023
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005024static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
5025{
5026 if (ols->st_stateowner->so_is_open_owner &&
5027 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5028 return nfserr_bad_stateid;
5029 return nfs_ok;
5030}
5031
Chuck Lever7df302f2012-05-29 13:56:37 -04005032static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04005033{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005034 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005035 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04005036
Andrew Elbleae254dac2017-11-09 13:41:10 -05005037 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5038 CLOSE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04005039 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04005040 /* Client debugging aid. */
5041 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
5042 char addr_str[INET6_ADDRSTRLEN];
5043 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
5044 sizeof(addr_str));
5045 pr_warn_ratelimited("NFSD: client %s testing state ID "
5046 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005047 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04005048 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005049 spin_lock(&cl->cl_lock);
5050 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005051 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005052 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005053 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04005054 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005055 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04005056 switch (s->sc_type) {
5057 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005058 status = nfs_ok;
5059 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005060 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005061 status = nfserr_deleg_revoked;
5062 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005063 case NFS4_OPEN_STID:
5064 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005065 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04005066 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005067 default:
5068 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005069 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04005070 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005071 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005072 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04005073 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005074out_unlock:
5075 spin_unlock(&cl->cl_lock);
5076 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04005077}
5078
Christoph Hellwigcd61c522014-08-14 08:44:57 +02005079__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005080nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5081 stateid_t *stateid, unsigned char typemask,
5082 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005083{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005084 __be32 status;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005085 bool return_revoked = false;
5086
5087 /*
5088 * only return revoked delegations if explicitly asked.
5089 * otherwise we report revoked or bad_stateid status.
5090 */
5091 if (typemask & NFS4_REVOKED_DELEG_STID)
5092 return_revoked = true;
5093 else if (typemask & NFS4_DELEG_STID)
5094 typemask |= NFS4_REVOKED_DELEG_STID;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005095
Andrew Elbleae254dac2017-11-09 13:41:10 -05005096 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5097 CLOSE_STATEID(stateid))
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005098 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005099 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005100 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005101 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005102 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005103 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005104 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005105 if (status)
5106 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005107 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005108 if (!*s)
5109 return nfserr_bad_stateid;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005110 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5111 nfs4_put_stid(*s);
5112 if (cstate->minorversion)
5113 return nfserr_deleg_revoked;
5114 return nfserr_bad_stateid;
5115 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005116 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005117}
5118
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005119static struct file *
5120nfs4_find_file(struct nfs4_stid *s, int flags)
5121{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005122 if (!s)
5123 return NULL;
5124
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005125 switch (s->sc_type) {
5126 case NFS4_DELEG_STID:
5127 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5128 return NULL;
5129 return get_file(s->sc_file->fi_deleg_file);
5130 case NFS4_OPEN_STID:
5131 case NFS4_LOCK_STID:
5132 if (flags & RD_STATE)
5133 return find_readable_file(s->sc_file);
5134 else
5135 return find_writeable_file(s->sc_file);
5136 break;
5137 }
5138
5139 return NULL;
5140}
5141
5142static __be32
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005143nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005144{
5145 __be32 status;
5146
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005147 status = nfsd4_check_openowner_confirmed(ols);
5148 if (status)
5149 return status;
5150 return nfs4_check_openmode(ols, flags);
5151}
5152
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005153static __be32
5154nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
5155 struct file **filpp, bool *tmp_file, int flags)
5156{
5157 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
5158 struct file *file;
5159 __be32 status;
5160
5161 file = nfs4_find_file(s, flags);
5162 if (file) {
5163 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5164 acc | NFSD_MAY_OWNER_OVERRIDE);
5165 if (status) {
5166 fput(file);
5167 return status;
5168 }
5169
5170 *filpp = file;
5171 } else {
5172 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
5173 if (status)
5174 return status;
5175
5176 if (tmp_file)
5177 *tmp_file = true;
5178 }
5179
5180 return 0;
5181}
5182
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005184 * Checks for stateid operations
5185 */
Al Virob37ad282006-10-19 23:28:59 -07005186__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005187nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01005188 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
5189 stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005191 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005192 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005193 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005194 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005195 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 if (filpp)
5198 *filpp = NULL;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005199 if (tmp_file)
5200 *tmp_file = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005202 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 return nfserr_grace;
5204
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005205 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5206 status = check_special_stateids(net, fhp, stateid, flags);
5207 goto done;
5208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005210 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04005211 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005212 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005213 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04005214 return status;
Trond Myklebust03da3162017-11-03 08:00:16 -04005215 status = nfsd4_stid_check_stateid_generation(stateid, s,
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005216 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04005217 if (status)
5218 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005219
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005220 switch (s->sc_type) {
5221 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005222 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005223 break;
5224 case NFS4_OPEN_STID:
5225 case NFS4_LOCK_STID:
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005226 status = nfs4_check_olstateid(openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005227 break;
5228 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04005229 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005230 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04005232 if (status)
5233 goto out;
5234 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005235
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005236done:
5237 if (!status && filpp)
5238 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239out:
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005240 if (s)
5241 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 return status;
5243}
5244
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005245/*
Bryan Schumaker17456802011-07-13 10:50:48 -04005246 * Test if the stateid is valid
5247 */
5248__be32
5249nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005250 union nfsd4_op_u *u)
Bryan Schumaker17456802011-07-13 10:50:48 -04005251{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005252 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005253 struct nfsd4_test_stateid_id *stateid;
5254 struct nfs4_client *cl = cstate->session->se_client;
5255
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005256 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04005257 stateid->ts_id_status =
5258 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005259
Bryan Schumaker17456802011-07-13 10:50:48 -04005260 return nfs_ok;
5261}
5262
Chuck Lever42691392016-08-11 10:37:30 -04005263static __be32
5264nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5265{
5266 struct nfs4_ol_stateid *stp = openlockstateid(s);
5267 __be32 ret;
5268
Trond Myklebust659aefb2017-11-03 08:00:13 -04005269 ret = nfsd4_lock_ol_stateid(stp);
5270 if (ret)
5271 goto out_put_stid;
Chuck Lever42691392016-08-11 10:37:30 -04005272
5273 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5274 if (ret)
5275 goto out;
5276
5277 ret = nfserr_locks_held;
5278 if (check_for_locks(stp->st_stid.sc_file,
5279 lockowner(stp->st_stateowner)))
5280 goto out;
5281
5282 release_lock_stateid(stp);
5283 ret = nfs_ok;
5284
5285out:
5286 mutex_unlock(&stp->st_mutex);
Trond Myklebust659aefb2017-11-03 08:00:13 -04005287out_put_stid:
Chuck Lever42691392016-08-11 10:37:30 -04005288 nfs4_put_stid(s);
5289 return ret;
5290}
5291
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005292__be32
5293nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005294 union nfsd4_op_u *u)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005295{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005296 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005297 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005298 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005299 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005300 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005301 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005302
Jeff Layton1af71cc2014-07-29 21:34:14 -04005303 spin_lock(&cl->cl_lock);
5304 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005305 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005306 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005307 spin_lock(&s->sc_lock);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005308 switch (s->sc_type) {
5309 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005310 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005311 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005312 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005313 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5314 if (ret)
5315 break;
5316 ret = nfserr_locks_held;
5317 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005318 case NFS4_LOCK_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005319 spin_unlock(&s->sc_lock);
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03005320 refcount_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005321 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04005322 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005323 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005324 case NFS4_REVOKED_DELEG_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005325 spin_unlock(&s->sc_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005326 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005327 list_del_init(&dp->dl_recall_lru);
5328 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04005329 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005330 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005331 goto out;
5332 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005333 }
Trond Myklebust03da3162017-11-03 08:00:16 -04005334 spin_unlock(&s->sc_lock);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005335out_unlock:
5336 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005337out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005338 return ret;
5339}
5340
NeilBrown4c4cd222005-07-07 17:59:27 -07005341static inline int
5342setlkflg (int type)
5343{
5344 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5345 RD_STATE : WR_STATE;
5346}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005348static __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 -04005349{
5350 struct svc_fh *current_fh = &cstate->current_fh;
5351 struct nfs4_stateowner *sop = stp->st_stateowner;
5352 __be32 status;
5353
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005354 status = nfsd4_check_seqid(cstate, sop, seqid);
5355 if (status)
5356 return status;
Trond Myklebust9271d7e2017-11-03 08:00:15 -04005357 status = nfsd4_lock_ol_stateid(stp);
5358 if (status != nfs_ok)
5359 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005360 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04005361 if (status == nfs_ok)
5362 status = nfs4_check_fh(current_fh, &stp->st_stid);
5363 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005364 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005365 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005366}
5367
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368/*
5369 * Checks for sequence id mutating operations.
5370 */
Al Virob37ad282006-10-19 23:28:59 -07005371static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03005372nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04005373 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005374 struct nfs4_ol_stateid **stpp,
5375 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376{
J. Bruce Fields0836f582008-01-26 19:08:12 -05005377 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005378 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005379 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005381 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5382 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07005383
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005385 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005386 if (status)
5387 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005388 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005389 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005391 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005392 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005393 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04005394 else
5395 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005396 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005397}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05005398
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005399static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5400 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005401{
5402 __be32 status;
5403 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005404 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005406 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005407 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04005408 if (status)
5409 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005410 oo = openowner(stp->st_stateowner);
5411 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005412 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005413 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07005414 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005415 }
5416 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07005417 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418}
5419
Al Virob37ad282006-10-19 23:28:59 -07005420__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005421nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005422 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005424 struct nfsd4_open_confirm *oc = &u->open_confirm;
Al Virob37ad282006-10-19 23:28:59 -07005425 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005426 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005427 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005428 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429
Al Viroa6a9f182013-09-16 10:57:01 -04005430 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5431 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005433 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07005434 if (status)
5435 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005437 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005438 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005439 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005440 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005441 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005442 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005443 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005444 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005445 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04005446 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005447 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04005448 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04005449 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005450 mutex_unlock(&stp->st_mutex);
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005451 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005452 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07005453
Jeff Layton2a4317c2012-03-21 16:42:43 -04005454 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005455 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04005456put_stateid:
5457 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005459 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 return status;
5461}
5462
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005463static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005465 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005466 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04005467 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005468 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005469}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04005470
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005471static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5472{
5473 switch (to_access) {
5474 case NFS4_SHARE_ACCESS_READ:
5475 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5476 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5477 break;
5478 case NFS4_SHARE_ACCESS_WRITE:
5479 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5480 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5481 break;
5482 case NFS4_SHARE_ACCESS_BOTH:
5483 break;
5484 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005485 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 }
5487}
5488
Al Virob37ad282006-10-19 23:28:59 -07005489__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005490nfsd4_open_downgrade(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005491 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005493 struct nfsd4_open_downgrade *od = &u->open_downgrade;
Al Virob37ad282006-10-19 23:28:59 -07005494 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005495 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005496 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497
Al Viroa6a9f182013-09-16 10:57:01 -04005498 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5499 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04005501 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02005502 if (od->od_deleg_want)
5503 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5504 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005506 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005507 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005508 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005511 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005512 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005514 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04005516 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005517 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005519 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005521 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04005522 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04005523 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005525put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005526 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005527 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005529 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 return status;
5531}
5532
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005533static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5534{
Trond Myklebustacf92952014-06-30 11:48:37 -04005535 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04005536 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04005537 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005538
Jeff Layton2c41beb2014-07-29 21:34:41 -04005539 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04005540 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005541
Jeff Laytond83017f2014-07-29 21:34:42 -04005542 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04005543 if (unhashed)
5544 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04005545 spin_unlock(&clp->cl_lock);
5546 free_ol_stateid_reaplist(&reaplist);
5547 } else {
5548 spin_unlock(&clp->cl_lock);
5549 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04005550 if (unhashed)
5551 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04005552 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04005553}
5554
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555/*
5556 * nfs4_unlock_state() called after encode
5557 */
Al Virob37ad282006-10-19 23:28:59 -07005558__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005559nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005560 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005562 struct nfsd4_close *close = &u->close;
Al Virob37ad282006-10-19 23:28:59 -07005563 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005564 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005565 struct net *net = SVC_NET(rqstp);
5566 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567
Al Viroa6a9f182013-09-16 10:57:01 -04005568 dprintk("NFSD: nfsd4_close on file %pd\n",
5569 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005571 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5572 &close->cl_stateid,
5573 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005574 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005575 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005576 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005578
5579 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Laytonbd2deca2018-02-15 15:16:18 -05005580
5581 /*
5582 * Technically we don't _really_ have to increment or copy it, since
5583 * it should just be gone after this operation and we clobber the
5584 * copied value below, but we continue to do so here just to ensure
5585 * that racing ops see that there was a state change.
5586 */
Jeff Layton9767feb2015-10-01 09:05:50 -04005587 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005589 nfsd4_close_open_stateid(stp);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005590 mutex_unlock(&stp->st_mutex);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005591
Jeff Laytonbd2deca2018-02-15 15:16:18 -05005592 /* v4.1+ suggests that we send a special stateid in here, since the
5593 * clients should just ignore this anyway. Since this is not useful
5594 * for v4.0 clients either, we set it to the special close_stateid
5595 * universally.
5596 *
5597 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
5598 */
5599 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
Trond Myklebustfb500a72017-11-03 08:00:12 -04005600
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005601 /* put reference from nfs4_preprocess_seqid_op */
5602 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 return status;
5605}
5606
Al Virob37ad282006-10-19 23:28:59 -07005607__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005608nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005609 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005611 struct nfsd4_delegreturn *dr = &u->delegreturn;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005612 struct nfs4_delegation *dp;
5613 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005614 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07005615 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005616 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005618 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005619 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005621 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005622 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005623 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005624 dp = delegstateid(s);
Trond Myklebust03da3162017-11-03 08:00:16 -04005625 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005626 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04005627 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005628
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005629 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005630put_stateid:
5631 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632out:
5633 return status;
5634}
5635
Benny Halevy87df4de2008-12-15 19:42:03 +02005636static inline u64
5637end_offset(u64 start, u64 len)
5638{
5639 u64 end;
5640
5641 end = start + len;
5642 return end >= start ? end: NFS4_MAX_UINT64;
5643}
5644
5645/* last octet in a range */
5646static inline u64
5647last_byte_offset(u64 start, u64 len)
5648{
5649 u64 end;
5650
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005651 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02005652 end = start + len;
5653 return end > start ? end - 1: NFS4_MAX_UINT64;
5654}
5655
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656/*
5657 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5658 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5659 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5660 * locking, this prevents us from being completely protocol-compliant. The
5661 * real solution to this problem is to start using unsigned file offsets in
5662 * the VFS, but this is a very deep change!
5663 */
5664static inline void
5665nfs4_transform_lock_offset(struct file_lock *lock)
5666{
5667 if (lock->fl_start < 0)
5668 lock->fl_start = OFFSET_MAX;
5669 if (lock->fl_end < 0)
5670 lock->fl_end = OFFSET_MAX;
5671}
5672
Jeff Laytoncae80b32015-04-03 09:04:04 -04005673static fl_owner_t
5674nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005675{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005676 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5677
5678 nfs4_get_stateowner(&lo->lo_owner);
5679 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005680}
5681
Jeff Laytoncae80b32015-04-03 09:04:04 -04005682static void
5683nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005684{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005685 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005686
Jeff Laytoncae80b32015-04-03 09:04:04 -04005687 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04005688 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04005689}
5690
Jeff Layton76d348f2016-09-16 16:28:24 -04005691static void
5692nfsd4_lm_notify(struct file_lock *fl)
5693{
5694 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
5695 struct net *net = lo->lo_owner.so_client->net;
5696 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5697 struct nfsd4_blocked_lock *nbl = container_of(fl,
5698 struct nfsd4_blocked_lock, nbl_lock);
5699 bool queue = false;
5700
Jeff Layton7919d0a2016-09-16 16:28:25 -04005701 /* An empty list means that something else is going to be using it */
Jeff Layton0cc11a62016-10-20 09:34:31 -04005702 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005703 if (!list_empty(&nbl->nbl_list)) {
5704 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005705 list_del_init(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -04005706 queue = true;
5707 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04005708 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005709
5710 if (queue)
5711 nfsd4_run_cb(&nbl->nbl_cb);
5712}
5713
Alexey Dobriyan7b021962009-09-21 17:01:12 -07005714static const struct lock_manager_operations nfsd_posix_mng_ops = {
Jeff Layton76d348f2016-09-16 16:28:24 -04005715 .lm_notify = nfsd4_lm_notify,
Kinglong Meeaef95832014-08-22 10:18:44 -04005716 .lm_get_owner = nfsd4_fl_get_owner,
5717 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07005718};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719
5720static inline void
5721nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5722{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005723 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724
NeilBrownd5b90262006-04-10 22:55:22 -07005725 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005726 lo = (struct nfs4_lockowner *) fl->fl_owner;
5727 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5728 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005729 if (!deny->ld_owner.data)
5730 /* We just don't care that much */
5731 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005732 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5733 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07005734 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005735nevermind:
5736 deny->ld_owner.len = 0;
5737 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07005738 deny->ld_clientid.cl_boot = 0;
5739 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 }
5741 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02005742 deny->ld_length = NFS4_MAX_UINT64;
5743 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5745 deny->ld_type = NFS4_READ_LT;
5746 if (fl->fl_type != F_RDLCK)
5747 deny->ld_type = NFS4_WRITE_LT;
5748}
5749
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005750static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005751find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752{
Trond Myklebustd4f04892014-07-29 21:34:36 -04005753 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005754 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755
Trond Myklebust0a880a22014-07-30 08:27:10 -04005756 lockdep_assert_held(&clp->cl_lock);
5757
Trond Myklebustd4f04892014-07-29 21:34:36 -04005758 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5759 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005760 if (so->so_is_open_owner)
5761 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005762 if (same_owner_str(so, owner))
5763 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 }
5765 return NULL;
5766}
5767
Trond Myklebustc58c6612014-07-29 21:34:35 -04005768static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005769find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04005770{
5771 struct nfs4_lockowner *lo;
5772
Trond Myklebustd4f04892014-07-29 21:34:36 -04005773 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005774 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005775 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005776 return lo;
5777}
5778
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005779static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5780{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005781 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005782}
5783
Jeff Layton6b180f02014-07-29 21:34:26 -04005784static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5785{
5786 struct nfs4_lockowner *lo = lockowner(sop);
5787
5788 kmem_cache_free(lockowner_slab, lo);
5789}
5790
5791static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005792 .so_unhash = nfs4_unhash_lockowner,
5793 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005794};
5795
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796/*
5797 * Alloc a lock owner structure.
5798 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005799 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005801 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005803static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005804alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5805 struct nfs4_ol_stateid *open_stp,
5806 struct nfsd4_lock *lock)
5807{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005808 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005810 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5811 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04005813 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005814 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5815 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005816 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005817 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005818 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005819 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005820 if (ret == NULL) {
5821 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005822 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005823 ret = lo;
5824 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08005825 nfs4_free_stateowner(&lo->lo_owner);
5826
Trond Myklebustd4f04892014-07-29 21:34:36 -04005827 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04005828 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829}
5830
Trond Myklebustfd1fd682017-11-03 08:00:14 -04005831static struct nfs4_ol_stateid *
5832find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5833{
5834 struct nfs4_ol_stateid *lst;
5835 struct nfs4_client *clp = lo->lo_owner.so_client;
5836
5837 lockdep_assert_held(&clp->cl_lock);
5838
5839 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
5840 if (lst->st_stid.sc_type != NFS4_LOCK_STID)
5841 continue;
5842 if (lst->st_stid.sc_file == fp) {
5843 refcount_inc(&lst->st_stid.sc_count);
5844 return lst;
5845 }
5846 }
5847 return NULL;
5848}
5849
Trond Myklebustbeeca192017-11-03 08:00:14 -04005850static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04005851init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5852 struct nfs4_file *fp, struct inode *inode,
5853 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005855 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustbeeca192017-11-03 08:00:14 -04005856 struct nfs4_ol_stateid *retstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857
Trond Myklebustbeeca192017-11-03 08:00:14 -04005858 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05005859 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Trond Myklebustbeeca192017-11-03 08:00:14 -04005860retry:
5861 spin_lock(&clp->cl_lock);
5862 spin_lock(&fp->fi_lock);
5863 retstp = find_lock_stateid(lo, fp);
5864 if (retstp)
5865 goto out_unlock;
Jeff Layton356a95e2014-07-29 21:34:13 -04005866
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03005867 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005868 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005869 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07005870 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005871 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005872 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005874 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005875 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005876 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005877 list_add(&stp->st_perfile, &fp->fi_stateids);
Trond Myklebustbeeca192017-11-03 08:00:14 -04005878out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04005879 spin_unlock(&fp->fi_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04005880 spin_unlock(&clp->cl_lock);
5881 if (retstp) {
5882 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
5883 nfs4_put_stid(&retstp->st_stid);
5884 goto retry;
5885 }
5886 /* To keep mutex tracking happy */
5887 mutex_unlock(&stp->st_mutex);
5888 stp = retstp;
5889 }
5890 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891}
5892
Jeff Laytonc53530d2014-06-30 11:48:39 -04005893static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04005894find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5895 struct inode *inode, struct nfs4_ol_stateid *ost,
5896 bool *new)
5897{
5898 struct nfs4_stid *ns = NULL;
5899 struct nfs4_ol_stateid *lst;
5900 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5901 struct nfs4_client *clp = oo->oo_owner.so_client;
5902
Trond Myklebustbeeca192017-11-03 08:00:14 -04005903 *new = false;
Jeff Layton356a95e2014-07-29 21:34:13 -04005904 spin_lock(&clp->cl_lock);
5905 lst = find_lock_stateid(lo, fi);
Jeff Layton356a95e2014-07-29 21:34:13 -04005906 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04005907 if (lst != NULL) {
5908 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
5909 goto out;
5910 nfs4_put_stid(&lst->st_stid);
5911 }
5912 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
5913 if (ns == NULL)
5914 return NULL;
5915
5916 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
5917 if (lst == openlockstateid(ns))
5918 *new = true;
5919 else
Jeff Layton356a95e2014-07-29 21:34:13 -04005920 nfs4_put_stid(ns);
Trond Myklebustbeeca192017-11-03 08:00:14 -04005921out:
Jeff Layton356a95e2014-07-29 21:34:13 -04005922 return lst;
5923}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005924
NeilBrownfd39ca92005-06-23 22:04:03 -07005925static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926check_lock_length(u64 offset, u64 length)
5927{
Kinglong Meee7969312015-07-13 17:34:19 +08005928 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5929 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930}
5931
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005932static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005933{
Trond Myklebust11b91642014-07-29 21:34:08 -04005934 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005935
Jeff Layton7214e862014-07-10 14:07:33 -04005936 lockdep_assert_held(&fp->fi_lock);
5937
Jeff Layton82c5ff12012-05-11 09:45:13 -04005938 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005939 return;
Jeff Layton12659652014-07-10 14:07:28 -04005940 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005941 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005942}
5943
Jeff Layton356a95e2014-07-29 21:34:13 -04005944static __be32
5945lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5946 struct nfs4_ol_stateid *ost,
5947 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04005948 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005949{
Jeff Layton5db1c032014-07-29 21:34:28 -04005950 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005951 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005952 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5953 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00005954 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005955 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04005956 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005957 unsigned int strhashval;
5958
Kinglong Meec8623992015-07-13 17:35:37 +08005959 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005960 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005961 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005962 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5963 if (lo == NULL)
5964 return nfserr_jukebox;
5965 } else {
5966 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005967 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005968 if (!cstate->minorversion &&
5969 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005970 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005971 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005972
Jeff Laytondd257932016-08-11 10:37:39 -04005973 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5974 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005975 status = nfserr_jukebox;
5976 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005977 }
Jeff Laytondd257932016-08-11 10:37:39 -04005978
Jeff Layton5db1c032014-07-29 21:34:28 -04005979 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04005980 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04005981out:
5982 nfs4_put_stateowner(&lo->lo_owner);
5983 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005984}
5985
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986/*
5987 * LOCK operation
5988 */
Al Virob37ad282006-10-19 23:28:59 -07005989__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005990nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005991 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005993 struct nfsd4_lock *lock = &u->lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005994 struct nfs4_openowner *open_sop = NULL;
5995 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005996 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005997 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005998 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005999 struct file *filp = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006000 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006001 struct file_lock *file_lock = NULL;
6002 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006003 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006004 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07006005 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04006006 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04006007 unsigned char fl_type;
6008 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006009 struct net *net = SVC_NET(rqstp);
6010 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011
6012 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
6013 (long long) lock->lk_offset,
6014 (long long) lock->lk_length);
6015
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 if (check_lock_length(lock->lk_offset, lock->lk_length))
6017 return nfserr_inval;
6018
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006019 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02006020 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08006021 dprintk("NFSD: nfsd4_lock: permission denied!\n");
6022 return status;
6023 }
6024
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04006026 if (nfsd4_has_session(cstate))
6027 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006028 memcpy(&lock->lk_new_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04006029 &cstate->session->se_client->cl_clientid,
6030 sizeof(clientid_t));
6031
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006033 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006037 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 lock->lk_new_open_seqid,
6039 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006040 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07006041 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006043 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006044 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006045 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04006046 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006047 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006048 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006049 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04006050 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006051 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03006052 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006053 lock->lk_old_lock_seqid,
6054 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006055 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006056 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04006057 if (status)
6058 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006059 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006061 lkflg = setlkflg(lock->lk_type);
6062 status = nfs4_check_openmode(lock_stp, lkflg);
6063 if (status)
6064 goto out;
6065
NeilBrown0dd395d2005-07-07 17:59:15 -07006066 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006067 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006068 goto out;
6069 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006070 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006071 goto out;
6072
Trond Myklebust11b91642014-07-29 21:34:08 -04006073 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 case NFS4_READW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006076 if (nfsd4_has_session(cstate))
6077 fl_flags |= FL_SLEEP;
6078 /* Fallthrough */
6079 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006080 spin_lock(&fp->fi_lock);
6081 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006082 if (filp)
6083 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04006084 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006085 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006086 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 case NFS4_WRITEW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006088 if (nfsd4_has_session(cstate))
6089 fl_flags |= FL_SLEEP;
6090 /* Fallthrough */
6091 case NFS4_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006092 spin_lock(&fp->fi_lock);
6093 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006094 if (filp)
6095 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04006096 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006097 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006098 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 default:
6100 status = nfserr_inval;
6101 goto out;
6102 }
Jeff Layton76d348f2016-09-16 16:28:24 -04006103
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006104 if (!filp) {
6105 status = nfserr_openmode;
6106 goto out;
6107 }
Kinglong Meeaef95832014-08-22 10:18:44 -04006108
Jeff Layton76d348f2016-09-16 16:28:24 -04006109 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6110 if (!nbl) {
6111 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6112 status = nfserr_jukebox;
6113 goto out;
6114 }
6115
6116 file_lock = &nbl->nbl_lock;
6117 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04006118 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04006119 file_lock->fl_pid = current->tgid;
6120 file_lock->fl_file = filp;
Jeff Layton76d348f2016-09-16 16:28:24 -04006121 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04006122 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6123 file_lock->fl_start = lock->lk_offset;
6124 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6125 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126
Jeff Layton21179d82012-08-21 08:03:32 -04006127 conflock = locks_alloc_lock();
6128 if (!conflock) {
6129 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6130 status = nfserr_jukebox;
6131 goto out;
6132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133
Jeff Layton76d348f2016-09-16 16:28:24 -04006134 if (fl_flags & FL_SLEEP) {
Jeff Layton7919d0a2016-09-16 16:28:25 -04006135 nbl->nbl_time = jiffies;
Jeff Layton0cc11a62016-10-20 09:34:31 -04006136 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006137 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006138 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006139 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006140 }
6141
Jeff Layton21179d82012-08-21 08:03:32 -04006142 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006143 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04006145 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07006146 status = 0;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04006147 if (lock->lk_reclaim)
6148 nn->somebody_reclaimed = true;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006149 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006150 case FILE_LOCK_DEFERRED:
6151 nbl = NULL;
6152 /* Fallthrough */
6153 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006154 status = nfserr_denied;
6155 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04006156 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006157 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006158 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006160 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04006161 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05006162 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04006163 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006164 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166out:
Jeff Layton76d348f2016-09-16 16:28:24 -04006167 if (nbl) {
6168 /* dequeue it if we queued it before */
6169 if (fl_flags & FL_SLEEP) {
Jeff Layton0cc11a62016-10-20 09:34:31 -04006170 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006171 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006172 list_del_init(&nbl->nbl_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006173 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006174 }
6175 free_blocked_lock(nbl);
6176 }
Trond Myklebustde186432014-07-10 14:07:26 -04006177 if (filp)
6178 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04006179 if (lock_stp) {
6180 /* Bump seqid manually if the 4.0 replay owner is openowner */
6181 if (cstate->replay_owner &&
6182 cstate->replay_owner != &lock_sop->lo_owner &&
6183 seqid_mutating_err(ntohl(status)))
6184 lock_sop->lo_owner.so_seqid++;
6185
6186 /*
6187 * If this is a new, never-before-used stateid, and we are
6188 * returning an error, then just go ahead and release it.
6189 */
J. Bruce Fields25020722018-01-17 16:25:59 -05006190 if (status && new)
Jeff Layton5db1c032014-07-29 21:34:28 -04006191 release_lock_stateid(lock_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006192
6193 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton5db1c032014-07-29 21:34:28 -04006194
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006195 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04006196 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006197 if (open_stp)
6198 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006199 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006200 if (conflock)
6201 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202 return status;
6203}
6204
6205/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006206 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6207 * so we do a temporary open here just to get an open file to pass to
6208 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6209 * inode operation.)
6210 */
Al Viro04da6e92012-04-13 00:00:04 -04006211static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006212{
6213 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04006214 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
6215 if (!err) {
6216 err = nfserrno(vfs_test_lock(file, lock));
Christoph Hellwigfd891452015-04-28 15:41:16 +02006217 fput(file);
Al Viro04da6e92012-04-13 00:00:04 -04006218 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006219 return err;
6220}
6221
6222/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223 * LOCKT operation
6224 */
Al Virob37ad282006-10-19 23:28:59 -07006225__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006226nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006227 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006229 struct nfsd4_lockt *lockt = &u->lockt;
Jeff Layton21179d82012-08-21 08:03:32 -04006230 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04006231 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006232 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006233 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006235 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236 return nfserr_grace;
6237
6238 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6239 return nfserr_inval;
6240
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006241 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04006242 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006243 if (status)
6244 goto out;
6245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246
J. Bruce Fields75c096f2011-08-15 18:39:32 -04006247 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249
Jeff Layton21179d82012-08-21 08:03:32 -04006250 file_lock = locks_alloc_lock();
6251 if (!file_lock) {
6252 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6253 status = nfserr_jukebox;
6254 goto out;
6255 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006256
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 switch (lockt->lt_type) {
6258 case NFS4_READ_LT:
6259 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006260 file_lock->fl_type = F_RDLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006261 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 case NFS4_WRITE_LT:
6263 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006264 file_lock->fl_type = F_WRLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006265 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04006267 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268 status = nfserr_inval;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006269 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270 }
6271
Kinglong Meec8623992015-07-13 17:35:37 +08006272 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006273 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04006274 file_lock->fl_owner = (fl_owner_t)lo;
6275 file_lock->fl_pid = current->tgid;
6276 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277
Jeff Layton21179d82012-08-21 08:03:32 -04006278 file_lock->fl_start = lockt->lt_offset;
6279 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280
Jeff Layton21179d82012-08-21 08:03:32 -04006281 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282
Jeff Layton21179d82012-08-21 08:03:32 -04006283 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04006284 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05006285 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04006286
Jeff Layton21179d82012-08-21 08:03:32 -04006287 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04006289 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290 }
6291out:
Jeff Layton5db1c032014-07-29 21:34:28 -04006292 if (lo)
6293 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04006294 if (file_lock)
6295 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 return status;
6297}
6298
Al Virob37ad282006-10-19 23:28:59 -07006299__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006300nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006301 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006303 struct nfsd4_locku *locku = &u->locku;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006304 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006306 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006307 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07006308 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006309 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6310
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6312 (long long) locku->lu_offset,
6313 (long long) locku->lu_length);
6314
6315 if (check_lock_length(locku->lu_offset, locku->lu_length))
6316 return nfserr_inval;
6317
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006318 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006319 &locku->lu_stateid, NFS4_LOCK_STID,
6320 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006321 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04006323 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006324 if (!filp) {
6325 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04006326 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006327 }
Jeff Layton21179d82012-08-21 08:03:32 -04006328 file_lock = locks_alloc_lock();
6329 if (!file_lock) {
6330 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6331 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04006332 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04006333 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006334
Jeff Layton21179d82012-08-21 08:03:32 -04006335 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04006336 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04006337 file_lock->fl_pid = current->tgid;
6338 file_lock->fl_file = filp;
6339 file_lock->fl_flags = FL_POSIX;
6340 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6341 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342
Jeff Layton21179d82012-08-21 08:03:32 -04006343 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6344 locku->lu_length);
6345 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346
Jeff Layton21179d82012-08-21 08:03:32 -04006347 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07006348 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05006349 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 goto out_nfserr;
6351 }
Jeff Layton9767feb2015-10-01 09:05:50 -04006352 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Trond Myklebustde186432014-07-10 14:07:26 -04006353fput:
6354 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04006355put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006356 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04006357 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006359 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006360 if (file_lock)
6361 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 return status;
6363
6364out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07006365 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04006366 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367}
6368
6369/*
6370 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006371 * true: locks held by lockowner
6372 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006374static bool
6375check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006377 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006378 int status = false;
6379 struct file *filp = find_any_file(fp);
6380 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006381 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006382
6383 if (!filp) {
6384 /* Any valid lock stateid should have some sort of access */
6385 WARN_ON_ONCE(1);
6386 return status;
6387 }
6388
Amir Goldstein64bed6c2018-07-13 17:22:24 +03006389 inode = locks_inode(filp);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006390 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006392 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05006393 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006394 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6395 if (fl->fl_owner == (fl_owner_t)lowner) {
6396 status = true;
6397 break;
6398 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08006399 }
Jeff Layton6109c852015-01-16 15:05:57 -05006400 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401 }
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006402 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403 return status;
6404}
6405
Al Virob37ad282006-10-19 23:28:59 -07006406__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08006407nfsd4_release_lockowner(struct svc_rqst *rqstp,
6408 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006409 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006411 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04006413 struct nfs4_stateowner *sop;
6414 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006415 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006417 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07006418 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006419 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006420 struct nfs4_client *clp;
Chuck Lever88584812016-07-13 16:40:14 -04006421 LIST_HEAD (reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422
6423 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6424 clid->cl_boot, clid->cl_id);
6425
Jeff Layton4b24ca72014-06-30 11:48:44 -04006426 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006427 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04006428 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006429
Trond Myklebustd4f04892014-07-29 21:34:36 -04006430 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04006431 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04006432 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04006433 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04006434 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04006435
6436 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006437 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04006438
Jeff Layton882e9d22014-07-29 21:34:37 -04006439 /* see if there are still any locks associated with it */
6440 lo = lockowner(sop);
6441 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6442 if (check_for_locks(stp->st_stid.sc_file, lo)) {
6443 status = nfserr_locks_held;
6444 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04006445 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04006446 }
Jeff Layton5adfd882014-07-29 21:34:31 -04006447 }
Jeff Layton882e9d22014-07-29 21:34:37 -04006448
Kinglong Meeb5971af2014-08-22 10:18:43 -04006449 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04006450 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04006451 }
Chuck Lever88584812016-07-13 16:40:14 -04006452 if (!lo) {
6453 spin_unlock(&clp->cl_lock);
6454 return status;
6455 }
6456
6457 unhash_lockowner_locked(lo);
6458 while (!list_empty(&lo->lo_owner.so_stateids)) {
6459 stp = list_first_entry(&lo->lo_owner.so_stateids,
6460 struct nfs4_ol_stateid,
6461 st_perstateowner);
6462 WARN_ON(!unhash_lock_stateid(stp));
6463 put_ol_stateid_locked(stp, &reaplist);
6464 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04006465 spin_unlock(&clp->cl_lock);
Chuck Lever88584812016-07-13 16:40:14 -04006466 free_ol_stateid_reaplist(&reaplist);
Jeff Layton68ef3bc2018-03-16 11:32:02 -04006467 remove_blocked_locks(lo);
Chuck Lever88584812016-07-13 16:40:14 -04006468 nfs4_put_stateowner(&lo->lo_owner);
6469
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470 return status;
6471}
6472
6473static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07006474alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475{
NeilBrowna55370a2005-06-23 22:03:52 -07006476 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477}
6478
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006479bool
Scott Mayhew6b189102019-03-26 18:06:26 -04006480nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07006481{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006482 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07006483
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006484 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006485 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07006486}
6487
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488/*
6489 * failure => all reset bets are off, nfserr_no_grace...
Scott Mayhew6b189102019-03-26 18:06:26 -04006490 *
6491 * The caller is responsible for freeing name.data if NULL is returned (it
6492 * will be freed in nfs4_remove_reclaim_record in the normal case).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05006494struct nfs4_client_reclaim *
Scott Mayhew6b189102019-03-26 18:06:26 -04006495nfs4_client_to_reclaim(struct xdr_netobj name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496{
6497 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006498 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499
Scott Mayhew6b189102019-03-26 18:06:26 -04006500 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", name.len, name.data);
NeilBrowna55370a2005-06-23 22:03:52 -07006501 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05006502 if (crp) {
6503 strhashval = clientstr_hashval(name);
6504 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006505 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Scott Mayhew6b189102019-03-26 18:06:26 -04006506 crp->cr_name.data = name.data;
6507 crp->cr_name.len = name.len;
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006508 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006509 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006510 }
6511 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512}
6513
Jeff Layton2a4317c2012-03-21 16:42:43 -04006514void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006515nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05006516{
6517 list_del(&crp->cr_strhash);
Scott Mayhew6b189102019-03-26 18:06:26 -04006518 kfree(crp->cr_name.data);
Jeff Laytonce30e532012-11-12 15:00:53 -05006519 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006520 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05006521}
6522
6523void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006524nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525{
6526 struct nfs4_client_reclaim *crp = NULL;
6527 int i;
6528
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006530 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6531 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006533 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534 }
6535 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006536 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537}
6538
6539/*
6540 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04006541struct nfs4_client_reclaim *
Scott Mayhew6b189102019-03-26 18:06:26 -04006542nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543{
6544 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545 struct nfs4_client_reclaim *crp = NULL;
6546
Scott Mayhew6b189102019-03-26 18:06:26 -04006547 dprintk("NFSD: nfs4_find_reclaim_client for name %.*s\n", name.len, name.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548
Scott Mayhew6b189102019-03-26 18:06:26 -04006549 strhashval = clientstr_hashval(name);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006550 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Scott Mayhew6b189102019-03-26 18:06:26 -04006551 if (compare_blob(&crp->cr_name, &name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552 return crp;
6553 }
6554 }
6555 return NULL;
6556}
6557
6558/*
6559* Called from OPEN. Look for clientid in reclaim list.
6560*/
Al Virob37ad282006-10-19 23:28:59 -07006561__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006562nfs4_check_open_reclaim(clientid_t *clid,
6563 struct nfsd4_compound_state *cstate,
6564 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006566 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04006567
6568 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006569 status = lookup_clientid(clid, cstate, nn);
6570 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04006571 return nfserr_reclaim_bad;
6572
Jeff Layton3b3e7b72014-09-12 16:40:22 -04006573 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6574 return nfserr_no_grace;
6575
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006576 if (nfsd4_client_record_check(cstate->clp))
6577 return nfserr_reclaim_bad;
6578
6579 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580}
6581
Bryan Schumaker65178db2011-11-01 13:35:21 -04006582#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04006583static inline void
6584put_client(struct nfs4_client *clp)
6585{
6586 atomic_dec(&clp->cl_refcount);
6587}
6588
Jeff Layton285abde2014-07-30 08:27:24 -04006589static struct nfs4_client *
6590nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6591{
6592 struct nfs4_client *clp;
6593 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6594 nfsd_net_id);
6595
6596 if (!nfsd_netns_ready(nn))
6597 return NULL;
6598
6599 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6600 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
6601 return clp;
6602 }
6603 return NULL;
6604}
6605
Jeff Layton7ec0e362014-07-30 08:27:17 -04006606u64
Jeff Layton285abde2014-07-30 08:27:24 -04006607nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04006608{
6609 struct nfs4_client *clp;
6610 u64 count = 0;
6611 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6612 nfsd_net_id);
6613 char buf[INET6_ADDRSTRLEN];
6614
6615 if (!nfsd_netns_ready(nn))
6616 return 0;
6617
6618 spin_lock(&nn->client_lock);
6619 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6620 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6621 pr_info("NFS Client: %s\n", buf);
6622 ++count;
6623 }
6624 spin_unlock(&nn->client_lock);
6625
6626 return count;
6627}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006628
Jeff Laytona0926d12014-07-30 08:27:18 -04006629u64
Jeff Layton285abde2014-07-30 08:27:24 -04006630nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04006631{
6632 u64 count = 0;
6633 struct nfs4_client *clp;
6634 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6635 nfsd_net_id);
6636
6637 if (!nfsd_netns_ready(nn))
6638 return count;
6639
6640 spin_lock(&nn->client_lock);
6641 clp = nfsd_find_client(addr, addr_size);
6642 if (clp) {
6643 if (mark_client_expired_locked(clp) == nfs_ok)
6644 ++count;
6645 else
6646 clp = NULL;
6647 }
6648 spin_unlock(&nn->client_lock);
6649
6650 if (clp)
6651 expire_client(clp);
6652
6653 return count;
6654}
6655
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006656u64
Jeff Layton285abde2014-07-30 08:27:24 -04006657nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006658{
6659 u64 count = 0;
6660 struct nfs4_client *clp, *next;
6661 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6662 nfsd_net_id);
6663 LIST_HEAD(reaplist);
6664
6665 if (!nfsd_netns_ready(nn))
6666 return count;
6667
6668 spin_lock(&nn->client_lock);
6669 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6670 if (mark_client_expired_locked(clp) == nfs_ok) {
6671 list_add(&clp->cl_lru, &reaplist);
6672 if (max != 0 && ++count >= max)
6673 break;
6674 }
6675 }
6676 spin_unlock(&nn->client_lock);
6677
6678 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6679 expire_client(clp);
6680
6681 return count;
6682}
6683
Bryan Schumaker184c1842012-11-29 11:40:44 -05006684static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6685 const char *type)
6686{
6687 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05006688 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05006689 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6690}
6691
Jeff Layton016200c2014-07-30 08:27:21 -04006692static void
6693nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6694 struct list_head *collect)
6695{
6696 struct nfs4_client *clp = lst->st_stid.sc_client;
6697 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6698 nfsd_net_id);
6699
6700 if (!collect)
6701 return;
6702
6703 lockdep_assert_held(&nn->client_lock);
6704 atomic_inc(&clp->cl_refcount);
6705 list_add(&lst->st_locks, collect);
6706}
6707
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006708static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04006709 struct list_head *collect,
Jeff Laytone8568732015-08-24 12:41:47 -04006710 bool (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05006711{
6712 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006713 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006714 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006715 u64 count = 0;
6716
Jeff Layton016200c2014-07-30 08:27:21 -04006717 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006718 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006719 list_for_each_entry_safe(stp, st_next,
6720 &oop->oo_owner.so_stateids, st_perstateowner) {
6721 list_for_each_entry_safe(lst, lst_next,
6722 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04006723 if (func) {
Jeff Laytone8568732015-08-24 12:41:47 -04006724 if (func(lst))
6725 nfsd_inject_add_lock_to_list(lst,
6726 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04006727 }
Jeff Layton016200c2014-07-30 08:27:21 -04006728 ++count;
6729 /*
6730 * Despite the fact that these functions deal
6731 * with 64-bit integers for "count", we must
6732 * ensure that it doesn't blow up the
6733 * clp->cl_refcount. Throw a warning if we
6734 * start to approach INT_MAX here.
6735 */
6736 WARN_ON_ONCE(count == (INT_MAX / 2));
6737 if (count == max)
6738 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006739 }
6740 }
6741 }
Jeff Layton016200c2014-07-30 08:27:21 -04006742out:
6743 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006744
6745 return count;
6746}
6747
Jeff Layton016200c2014-07-30 08:27:21 -04006748static u64
6749nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6750 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05006751{
Jeff Layton016200c2014-07-30 08:27:21 -04006752 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006753}
6754
Jeff Layton016200c2014-07-30 08:27:21 -04006755static u64
6756nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006757{
Jeff Layton016200c2014-07-30 08:27:21 -04006758 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006759 nfsd_print_count(clp, count, "locked files");
6760 return count;
6761}
6762
Jeff Layton016200c2014-07-30 08:27:21 -04006763u64
Jeff Layton285abde2014-07-30 08:27:24 -04006764nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04006765{
6766 struct nfs4_client *clp;
6767 u64 count = 0;
6768 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6769 nfsd_net_id);
6770
6771 if (!nfsd_netns_ready(nn))
6772 return 0;
6773
6774 spin_lock(&nn->client_lock);
6775 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6776 count += nfsd_print_client_locks(clp);
6777 spin_unlock(&nn->client_lock);
6778
6779 return count;
6780}
6781
6782static void
6783nfsd_reap_locks(struct list_head *reaplist)
6784{
6785 struct nfs4_client *clp;
6786 struct nfs4_ol_stateid *stp, *next;
6787
6788 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6789 list_del_init(&stp->st_locks);
6790 clp = stp->st_stid.sc_client;
6791 nfs4_put_stid(&stp->st_stid);
6792 put_client(clp);
6793 }
6794}
6795
6796u64
Jeff Layton285abde2014-07-30 08:27:24 -04006797nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04006798{
6799 unsigned int count = 0;
6800 struct nfs4_client *clp;
6801 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6802 nfsd_net_id);
6803 LIST_HEAD(reaplist);
6804
6805 if (!nfsd_netns_ready(nn))
6806 return count;
6807
6808 spin_lock(&nn->client_lock);
6809 clp = nfsd_find_client(addr, addr_size);
6810 if (clp)
6811 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6812 spin_unlock(&nn->client_lock);
6813 nfsd_reap_locks(&reaplist);
6814 return count;
6815}
6816
6817u64
Jeff Layton285abde2014-07-30 08:27:24 -04006818nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04006819{
6820 u64 count = 0;
6821 struct nfs4_client *clp;
6822 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6823 nfsd_net_id);
6824 LIST_HEAD(reaplist);
6825
6826 if (!nfsd_netns_ready(nn))
6827 return count;
6828
6829 spin_lock(&nn->client_lock);
6830 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6831 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6832 if (max != 0 && count >= max)
6833 break;
6834 }
6835 spin_unlock(&nn->client_lock);
6836 nfsd_reap_locks(&reaplist);
6837 return count;
6838}
6839
Jeff Layton82e05ef2014-07-30 08:27:22 -04006840static u64
6841nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6842 struct list_head *collect,
6843 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006844{
6845 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04006846 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6847 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006848 u64 count = 0;
6849
Jeff Layton82e05ef2014-07-30 08:27:22 -04006850 lockdep_assert_held(&nn->client_lock);
6851
6852 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006853 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04006854 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006855 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04006856 if (collect) {
6857 atomic_inc(&clp->cl_refcount);
6858 list_add(&oop->oo_perclient, collect);
6859 }
6860 }
6861 ++count;
6862 /*
6863 * Despite the fact that these functions deal with
6864 * 64-bit integers for "count", we must ensure that
6865 * it doesn't blow up the clp->cl_refcount. Throw a
6866 * warning if we start to approach INT_MAX here.
6867 */
6868 WARN_ON_ONCE(count == (INT_MAX / 2));
6869 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006870 break;
6871 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04006872 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006873
6874 return count;
6875}
6876
Jeff Layton82e05ef2014-07-30 08:27:22 -04006877static u64
6878nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006879{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006880 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6881
6882 nfsd_print_count(clp, count, "openowners");
6883 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006884}
6885
Jeff Layton82e05ef2014-07-30 08:27:22 -04006886static u64
6887nfsd_collect_client_openowners(struct nfs4_client *clp,
6888 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006889{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006890 return nfsd_foreach_client_openowner(clp, max, collect,
6891 unhash_openowner_locked);
6892}
6893
6894u64
Jeff Layton285abde2014-07-30 08:27:24 -04006895nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006896{
6897 struct nfs4_client *clp;
6898 u64 count = 0;
6899 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6900 nfsd_net_id);
6901
6902 if (!nfsd_netns_ready(nn))
6903 return 0;
6904
6905 spin_lock(&nn->client_lock);
6906 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6907 count += nfsd_print_client_openowners(clp);
6908 spin_unlock(&nn->client_lock);
6909
6910 return count;
6911}
6912
6913static void
6914nfsd_reap_openowners(struct list_head *reaplist)
6915{
6916 struct nfs4_client *clp;
6917 struct nfs4_openowner *oop, *next;
6918
6919 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6920 list_del_init(&oop->oo_perclient);
6921 clp = oop->oo_owner.so_client;
6922 release_openowner(oop);
6923 put_client(clp);
6924 }
6925}
6926
6927u64
Jeff Layton285abde2014-07-30 08:27:24 -04006928nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6929 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006930{
6931 unsigned int count = 0;
6932 struct nfs4_client *clp;
6933 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6934 nfsd_net_id);
6935 LIST_HEAD(reaplist);
6936
6937 if (!nfsd_netns_ready(nn))
6938 return count;
6939
6940 spin_lock(&nn->client_lock);
6941 clp = nfsd_find_client(addr, addr_size);
6942 if (clp)
6943 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6944 spin_unlock(&nn->client_lock);
6945 nfsd_reap_openowners(&reaplist);
6946 return count;
6947}
6948
6949u64
Jeff Layton285abde2014-07-30 08:27:24 -04006950nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006951{
6952 u64 count = 0;
6953 struct nfs4_client *clp;
6954 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6955 nfsd_net_id);
6956 LIST_HEAD(reaplist);
6957
6958 if (!nfsd_netns_ready(nn))
6959 return count;
6960
6961 spin_lock(&nn->client_lock);
6962 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6963 count += nfsd_collect_client_openowners(clp, &reaplist,
6964 max - count);
6965 if (max != 0 && count >= max)
6966 break;
6967 }
6968 spin_unlock(&nn->client_lock);
6969 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006970 return count;
6971}
6972
Bryan Schumaker269de302012-11-29 11:40:42 -05006973static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6974 struct list_head *victims)
6975{
6976 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006977 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6978 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006979 u64 count = 0;
6980
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006981 lockdep_assert_held(&nn->client_lock);
6982
6983 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006984 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006985 if (victims) {
6986 /*
6987 * It's not safe to mess with delegations that have a
6988 * non-zero dl_time. They might have already been broken
6989 * and could be processed by the laundromat outside of
6990 * the state_lock. Just leave them be.
6991 */
6992 if (dp->dl_time != 0)
6993 continue;
6994
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006995 atomic_inc(&clp->cl_refcount);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04006996 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04006997 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006998 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006999 ++count;
7000 /*
7001 * Despite the fact that these functions deal with
7002 * 64-bit integers for "count", we must ensure that
7003 * it doesn't blow up the clp->cl_refcount. Throw a
7004 * warning if we start to approach INT_MAX here.
7005 */
7006 WARN_ON_ONCE(count == (INT_MAX / 2));
7007 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05007008 break;
7009 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007010 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05007011 return count;
7012}
7013
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007014static u64
7015nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05007016{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007017 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05007018
7019 nfsd_print_count(clp, count, "delegations");
7020 return count;
7021}
7022
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007023u64
Jeff Layton285abde2014-07-30 08:27:24 -04007024nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007025{
7026 struct nfs4_client *clp;
7027 u64 count = 0;
7028 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7029 nfsd_net_id);
7030
7031 if (!nfsd_netns_ready(nn))
7032 return 0;
7033
7034 spin_lock(&nn->client_lock);
7035 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7036 count += nfsd_print_client_delegations(clp);
7037 spin_unlock(&nn->client_lock);
7038
7039 return count;
7040}
7041
7042static void
7043nfsd_forget_delegations(struct list_head *reaplist)
7044{
7045 struct nfs4_client *clp;
7046 struct nfs4_delegation *dp, *next;
7047
7048 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7049 list_del_init(&dp->dl_recall_lru);
7050 clp = dp->dl_stid.sc_client;
7051 revoke_delegation(dp);
7052 put_client(clp);
7053 }
7054}
7055
7056u64
Jeff Layton285abde2014-07-30 08:27:24 -04007057nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
7058 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007059{
7060 u64 count = 0;
7061 struct nfs4_client *clp;
7062 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7063 nfsd_net_id);
7064 LIST_HEAD(reaplist);
7065
7066 if (!nfsd_netns_ready(nn))
7067 return count;
7068
7069 spin_lock(&nn->client_lock);
7070 clp = nfsd_find_client(addr, addr_size);
7071 if (clp)
7072 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7073 spin_unlock(&nn->client_lock);
7074
7075 nfsd_forget_delegations(&reaplist);
7076 return count;
7077}
7078
7079u64
Jeff Layton285abde2014-07-30 08:27:24 -04007080nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007081{
7082 u64 count = 0;
7083 struct nfs4_client *clp;
7084 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7085 nfsd_net_id);
7086 LIST_HEAD(reaplist);
7087
7088 if (!nfsd_netns_ready(nn))
7089 return count;
7090
7091 spin_lock(&nn->client_lock);
7092 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7093 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7094 if (max != 0 && count >= max)
7095 break;
7096 }
7097 spin_unlock(&nn->client_lock);
7098 nfsd_forget_delegations(&reaplist);
7099 return count;
7100}
7101
7102static void
7103nfsd_recall_delegations(struct list_head *reaplist)
7104{
7105 struct nfs4_client *clp;
7106 struct nfs4_delegation *dp, *next;
7107
7108 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7109 list_del_init(&dp->dl_recall_lru);
7110 clp = dp->dl_stid.sc_client;
7111 /*
7112 * We skipped all entries that had a zero dl_time before,
7113 * so we can now reset the dl_time back to 0. If a delegation
7114 * break comes in now, then it won't make any difference since
7115 * we're recalling it either way.
7116 */
7117 spin_lock(&state_lock);
7118 dp->dl_time = 0;
7119 spin_unlock(&state_lock);
7120 nfsd_break_one_deleg(dp);
7121 put_client(clp);
7122 }
7123}
7124
7125u64
Jeff Layton285abde2014-07-30 08:27:24 -04007126nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007127 size_t addr_size)
7128{
7129 u64 count = 0;
7130 struct nfs4_client *clp;
7131 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7132 nfsd_net_id);
7133 LIST_HEAD(reaplist);
7134
7135 if (!nfsd_netns_ready(nn))
7136 return count;
7137
7138 spin_lock(&nn->client_lock);
7139 clp = nfsd_find_client(addr, addr_size);
7140 if (clp)
7141 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7142 spin_unlock(&nn->client_lock);
7143
7144 nfsd_recall_delegations(&reaplist);
7145 return count;
7146}
7147
7148u64
Jeff Layton285abde2014-07-30 08:27:24 -04007149nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007150{
7151 u64 count = 0;
7152 struct nfs4_client *clp, *next;
7153 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7154 nfsd_net_id);
7155 LIST_HEAD(reaplist);
7156
7157 if (!nfsd_netns_ready(nn))
7158 return count;
7159
7160 spin_lock(&nn->client_lock);
7161 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7162 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7163 if (max != 0 && ++count >= max)
7164 break;
7165 }
7166 spin_unlock(&nn->client_lock);
7167 nfsd_recall_delegations(&reaplist);
7168 return count;
7169}
Bryan Schumaker65178db2011-11-01 13:35:21 -04007170#endif /* CONFIG_NFSD_FAULT_INJECTION */
7171
Meelap Shahc2f1a552007-07-17 04:04:39 -07007172/*
7173 * Since the lifetime of a delegation isn't limited to that of an open, a
7174 * client may quite reasonably hang on to a delegation as long as it has
7175 * the inode cached. This becomes an obvious problem the first time a
7176 * client's inode cache approaches the size of the server's total memory.
7177 *
7178 * For now we avoid this problem by imposing a hard limit on the number
7179 * of delegations, which varies according to the server's memory size.
7180 */
7181static void
7182set_max_delegations(void)
7183{
7184 /*
7185 * Allow at most 4 delegations per megabyte of RAM. Quick
7186 * estimates suggest that in the worst case (where every delegation
7187 * is for a different inode), a delegation could take about 1.5K,
7188 * giving a worst case usage of about 6% of memory.
7189 */
7190 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7191}
7192
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007193static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007194{
7195 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7196 int i;
7197
Kees Cook6da2ec52018-06-12 13:55:00 -07007198 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7199 sizeof(struct list_head),
7200 GFP_KERNEL);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007201 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007202 goto err;
Kees Cook6da2ec52018-06-12 13:55:00 -07007203 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7204 sizeof(struct list_head),
7205 GFP_KERNEL);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007206 if (!nn->unconf_id_hashtbl)
7207 goto err_unconf_id;
Kees Cook6da2ec52018-06-12 13:55:00 -07007208 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7209 sizeof(struct list_head),
7210 GFP_KERNEL);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007211 if (!nn->sessionid_hashtbl)
7212 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007213
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007214 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007215 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007216 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007217 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007218 for (i = 0; i < SESSION_HASH_SIZE; i++)
7219 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007220 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007221 nn->unconf_name_tree = RB_ROOT;
Vasily Averin81833de2017-11-13 07:25:40 +03007222 nn->boot_time = get_seconds();
7223 nn->grace_ended = false;
7224 nn->nfsd4_manager.block_opens = true;
7225 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03007226 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03007227 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007228 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03007229 spin_lock_init(&nn->client_lock);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04007230 spin_lock_init(&nn->s2s_cp_lock);
7231 idr_init(&nn->s2s_cp_stateids);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007232
Jeff Layton0cc11a62016-10-20 09:34:31 -04007233 spin_lock_init(&nn->blocked_locks_lock);
7234 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7235
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007236 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007237 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007238
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007239 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007240
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007241err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03007242 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007243err_unconf_id:
7244 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007245err:
7246 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007247}
7248
7249static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007250nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007251{
7252 int i;
7253 struct nfs4_client *clp = NULL;
7254 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7255
7256 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7257 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7258 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7259 destroy_client(clp);
7260 }
7261 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007262
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007263 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7264
Kinglong Mee2b905632014-03-26 22:09:30 +08007265 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7266 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7267 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7268 destroy_client(clp);
7269 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007270 }
7271
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007272 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007273 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007274 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007275 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007276}
7277
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007278int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007279nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07007280{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04007281 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007282 int ret;
7283
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007284 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007285 if (ret)
7286 return ret;
Jeff Laytond4318ac2014-09-12 16:40:21 -04007287 locks_start_grace(net, &nn->nfsd4_manager);
7288 nfsd4_client_tracking_init(net);
Scott Mayhew362063a2019-03-26 18:06:28 -04007289 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7290 goto skip_grace;
Vasily Averin7e981a82017-11-06 16:46:04 +03007291 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %x)\n",
7292 nn->nfsd4_grace, net->ns.inum);
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03007293 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007294 return 0;
Scott Mayhew362063a2019-03-26 18:06:28 -04007295
7296skip_grace:
7297 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7298 net->ns.inum);
7299 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7300 nfsd4_end_grace(nn);
7301 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007302}
7303
7304/* initialization to perform when the nfsd service is started: */
7305
7306int
7307nfs4_state_start(void)
7308{
7309 int ret;
7310
Jeff Layton51a54562015-08-20 07:17:01 -04007311 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05007312 if (laundry_wq == NULL) {
7313 ret = -ENOMEM;
Chuck Levera26dd642018-08-16 12:06:09 -04007314 goto out;
Jeff Laytona6d6b782012-03-05 11:42:36 -05007315 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007316 ret = nfsd4_create_callback_queue();
7317 if (ret)
7318 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007319
Meelap Shahc2f1a552007-07-17 04:04:39 -07007320 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007321 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007322
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007323out_free_laundry:
7324 destroy_workqueue(laundry_wq);
Chuck Levera26dd642018-08-16 12:06:09 -04007325out:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007326 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327}
7328
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007329void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007330nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007331{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007332 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007334 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007335
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007336 cancel_delayed_work_sync(&nn->laundromat_work);
7337 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05007338
Linus Torvalds1da177e2005-04-16 15:20:36 -07007339 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04007340 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007341 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007343 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007344 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007345 }
Benny Halevycdc975052014-05-30 09:09:30 -04007346 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347 list_for_each_safe(pos, next, &reaplist) {
7348 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04007349 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05007350 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007351 }
7352
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007353 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007354 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355}
7356
7357void
7358nfs4_state_shutdown(void)
7359{
NeilBrown5e8d5c22006-04-10 22:55:37 -07007360 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04007361 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007363
7364static void
7365get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7366{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007367 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
7368 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007369}
7370
7371static void
7372put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7373{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007374 if (cstate->minorversion) {
7375 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7376 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7377 }
7378}
7379
7380void
7381clear_current_stateid(struct nfsd4_compound_state *cstate)
7382{
7383 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007384}
7385
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007386/*
7387 * functions to set current state id
7388 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007389void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007390nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7391 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007392{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007393 put_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007394}
7395
7396void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007397nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7398 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007399{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007400 put_stateid(cstate, &u->open.op_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007401}
7402
7403void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007404nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7405 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007406{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007407 put_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007408}
7409
7410void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007411nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7412 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007413{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007414 put_stateid(cstate, &u->lock.lk_resp_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007415}
7416
7417/*
7418 * functions to consume current state id
7419 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007420
7421void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007422nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7423 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007424{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007425 get_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007426}
7427
7428void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007429nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7430 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007431{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007432 get_stateid(cstate, &u->delegreturn.dr_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007433}
7434
7435void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007436nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7437 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007438{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007439 get_stateid(cstate, &u->free_stateid.fr_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007440}
7441
7442void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007443nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7444 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007445{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007446 get_stateid(cstate, &u->setattr.sa_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007447}
7448
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007449void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007450nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7451 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007452{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007453 get_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007454}
7455
7456void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007457nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7458 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007459{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007460 get_stateid(cstate, &u->locku.lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007461}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007462
7463void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007464nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7465 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007466{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007467 get_stateid(cstate, &u->read.rd_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007468}
7469
7470void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007471nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7472 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007473{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007474 get_stateid(cstate, &u->write.wr_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007475}