blob: be6dcc4c2ab08fdf8ac06daac1c24ac49f8930b4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Chuck Lever7df302f2012-05-29 13:56:37 -040041#include <linux/ratelimit.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050042#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton59766872013-02-04 12:50:00 -050043#include <linux/sunrpc/addr.h>
Daniel Borkmann87545892014-12-10 16:33:11 +010044#include <linux/jhash.h>
J. Bruce Fields169319f2019-06-19 12:39:46 -040045#include <linux/string_helpers.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020046#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040047#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050048#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040049#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040051#include "netns.h"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020052#include "pnfs.h"
Jeff Laytonfd4f83f2019-08-18 14:18:52 -040053#include "filecache.h"
Chuck Leverdd5e3fb2020-04-05 11:42:29 -040054#include "trace.h"
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define NFSDDBG_FACILITY NFSDDBG_PROC
57
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050058#define all_ones {{~0,~0},~0}
59static const stateid_t one_stateid = {
60 .si_generation = ~0,
61 .si_opaque = all_ones,
62};
63static const stateid_t zero_stateid = {
64 /* all fields zero */
65};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010066static const stateid_t currentstateid = {
67 .si_generation = 1,
68};
Trond Myklebustfb500a72017-11-03 08:00:12 -040069static const stateid_t close_stateid = {
70 .si_generation = 0xffffffffU,
71};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050072
Andy Adamsonec6b5d72009-04-03 08:28:28 +030073static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070074
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050075#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
76#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010077#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Andrew Elbleae254dac2017-11-09 13:41:10 -050078#define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040081static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040082static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Scott Mayhew362063a2019-03-26 18:06:28 -040083void nfsd4_end_grace(struct nfsd_net *nn);
Olga Kornievskaia624322f2019-10-04 16:34:26 -040084static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
J. Bruce Fields8b671b82009-02-22 14:51:34 -080086/* Locking: */
87
J. Bruce Fields8b671b82009-02-22 14:51:34 -080088/*
89 * Currently used for the del_recall_lru and file hash table. In an
90 * effort to decrease the scope of the client_mutex, this spinlock may
91 * eventually cover more:
92 */
Benny Halevycdc975052014-05-30 09:09:30 -040093static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080094
Andrew Elble4f34bd02017-11-08 17:29:51 -050095enum nfsd4_st_mutex_lock_subclass {
96 OPEN_STATEID_MUTEX = 0,
97 LOCK_STATEID_MUTEX = 1,
98};
99
Jeff Laytonb401be222014-07-29 21:34:33 -0400100/*
101 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
102 * the refcount on the open stateid to drop.
103 */
104static DECLARE_WAIT_QUEUE_HEAD(close_wq);
105
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400106/*
107 * A waitqueue where a writer to clients/#/ctl destroying a client can
108 * wait for cl_rpc_users to drop to 0 and then for the client to be
109 * unhashed.
110 */
111static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
112
Jeff Layton9258a2d2018-03-16 09:47:22 -0400113static struct kmem_cache *client_slab;
Christoph Hellwigabf11352014-05-21 07:43:03 -0700114static struct kmem_cache *openowner_slab;
115static struct kmem_cache *lockowner_slab;
116static struct kmem_cache *file_slab;
117static struct kmem_cache *stateid_slab;
118static struct kmem_cache *deleg_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +0200119static struct kmem_cache *odstate_slab;
NeilBrowne60d4392005-06-23 22:03:01 -0700120
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400121static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800122
Julia Lawallc4cb8972015-11-21 22:57:39 +0100123static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
Jeff Layton76d348f2016-09-16 16:28:24 -0400124static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200125
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400126static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800127{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400128 return ses->se_flags & NFS4_SESSION_DEAD;
129}
130
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400131static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
132{
133 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400134 return nfserr_jukebox;
135 ses->se_flags |= NFS4_SESSION_DEAD;
136 return nfs_ok;
137}
138
J. Bruce Fields221a6872013-04-01 22:23:49 -0400139static bool is_client_expired(struct nfs4_client *clp)
140{
141 return clp->cl_time == 0;
142}
143
J. Bruce Fields221a6872013-04-01 22:23:49 -0400144static __be32 get_client_locked(struct nfs4_client *clp)
145{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400146 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
147
148 lockdep_assert_held(&nn->client_lock);
149
J. Bruce Fields221a6872013-04-01 22:23:49 -0400150 if (is_client_expired(clp))
151 return nfserr_expired;
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400152 atomic_inc(&clp->cl_rpc_users);
J. Bruce Fields221a6872013-04-01 22:23:49 -0400153 return nfs_ok;
154}
155
156/* must be called under the client_lock */
157static inline void
158renew_client_locked(struct nfs4_client *clp)
159{
160 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
161
162 if (is_client_expired(clp)) {
163 WARN_ON(1);
164 printk("%s: client (clientid %08x/%08x) already expired\n",
165 __func__,
166 clp->cl_clientid.cl_boot,
167 clp->cl_clientid.cl_id);
168 return;
169 }
170
J. Bruce Fields221a6872013-04-01 22:23:49 -0400171 list_move_tail(&clp->cl_lru, &nn->client_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +0100172 clp->cl_time = ktime_get_boottime_seconds();
J. Bruce Fields221a6872013-04-01 22:23:49 -0400173}
174
Fengguang Wuba138432013-04-16 22:14:15 -0400175static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400176{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400177 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
178
179 lockdep_assert_held(&nn->client_lock);
180
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400181 if (!atomic_dec_and_test(&clp->cl_rpc_users))
J. Bruce Fields221a6872013-04-01 22:23:49 -0400182 return;
183 if (!is_client_expired(clp))
184 renew_client_locked(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400185 else
186 wake_up_all(&expiry_wq);
J. Bruce Fields221a6872013-04-01 22:23:49 -0400187}
188
Jeff Layton4b24ca72014-06-30 11:48:44 -0400189static void put_client_renew(struct nfs4_client *clp)
190{
191 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
192
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400193 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
Jeff Laytond6c249b2014-07-08 14:02:50 -0400194 return;
195 if (!is_client_expired(clp))
196 renew_client_locked(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400197 else
198 wake_up_all(&expiry_wq);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400199 spin_unlock(&nn->client_lock);
200}
201
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400202static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
203{
204 __be32 status;
205
206 if (is_session_dead(ses))
207 return nfserr_badsession;
208 status = get_client_locked(ses->se_client);
209 if (status)
210 return status;
211 atomic_inc(&ses->se_ref);
212 return nfs_ok;
213}
214
215static void nfsd4_put_session_locked(struct nfsd4_session *ses)
216{
217 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400218 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
219
220 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400221
222 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
223 free_session(ses);
224 put_client_renew_locked(clp);
225}
226
227static void nfsd4_put_session(struct nfsd4_session *ses)
228{
229 struct nfs4_client *clp = ses->se_client;
230 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
231
232 spin_lock(&nn->client_lock);
233 nfsd4_put_session_locked(ses);
234 spin_unlock(&nn->client_lock);
235}
236
Jeff Layton76d348f2016-09-16 16:28:24 -0400237static struct nfsd4_blocked_lock *
238find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
239 struct nfsd_net *nn)
240{
241 struct nfsd4_blocked_lock *cur, *found = NULL;
242
Jeff Layton0cc11a62016-10-20 09:34:31 -0400243 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400244 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
245 if (fh_match(fh, &cur->nbl_fh)) {
246 list_del_init(&cur->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -0400247 list_del_init(&cur->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400248 found = cur;
249 break;
250 }
251 }
Jeff Layton0cc11a62016-10-20 09:34:31 -0400252 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400253 if (found)
NeilBrowncb03f942018-11-30 10:04:08 +1100254 locks_delete_block(&found->nbl_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400255 return found;
256}
257
258static struct nfsd4_blocked_lock *
259find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
260 struct nfsd_net *nn)
261{
262 struct nfsd4_blocked_lock *nbl;
263
264 nbl = find_blocked_lock(lo, fh, nn);
265 if (!nbl) {
266 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
267 if (nbl) {
Vasily Averine1e83992020-03-27 07:50:40 +0300268 INIT_LIST_HEAD(&nbl->nbl_list);
269 INIT_LIST_HEAD(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400270 fh_copy_shallow(&nbl->nbl_fh, fh);
271 locks_init_lock(&nbl->nbl_lock);
272 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
273 &nfsd4_cb_notify_lock_ops,
274 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
275 }
276 }
277 return nbl;
278}
279
280static void
281free_blocked_lock(struct nfsd4_blocked_lock *nbl)
282{
Jeff Layton6aaafc42019-04-22 12:34:23 -0400283 locks_delete_block(&nbl->nbl_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400284 locks_release_private(&nbl->nbl_lock);
285 kfree(nbl);
286}
287
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400288static void
289remove_blocked_locks(struct nfs4_lockowner *lo)
290{
291 struct nfs4_client *clp = lo->lo_owner.so_client;
292 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
293 struct nfsd4_blocked_lock *nbl;
294 LIST_HEAD(reaplist);
295
296 /* Dequeue all blocked locks */
297 spin_lock(&nn->blocked_locks_lock);
298 while (!list_empty(&lo->lo_blocked)) {
299 nbl = list_first_entry(&lo->lo_blocked,
300 struct nfsd4_blocked_lock,
301 nbl_list);
302 list_del_init(&nbl->nbl_list);
303 list_move(&nbl->nbl_lru, &reaplist);
304 }
305 spin_unlock(&nn->blocked_locks_lock);
306
307 /* Now free them */
308 while (!list_empty(&reaplist)) {
309 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
310 nbl_lru);
311 list_del_init(&nbl->nbl_lru);
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400312 free_blocked_lock(nbl);
313 }
314}
315
Jeff Laytonf4564582019-04-22 12:34:24 -0400316static void
317nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
318{
319 struct nfsd4_blocked_lock *nbl = container_of(cb,
320 struct nfsd4_blocked_lock, nbl_cb);
321 locks_delete_block(&nbl->nbl_lock);
322}
323
Jeff Layton76d348f2016-09-16 16:28:24 -0400324static int
325nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
326{
327 /*
328 * Since this is just an optimization, we don't try very hard if it
329 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
330 * just quit trying on anything else.
331 */
332 switch (task->tk_status) {
333 case -NFS4ERR_DELAY:
334 rpc_delay(task, 1 * HZ);
335 return 0;
336 default:
337 return 1;
338 }
339}
340
341static void
342nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
343{
344 struct nfsd4_blocked_lock *nbl = container_of(cb,
345 struct nfsd4_blocked_lock, nbl_cb);
346
347 free_blocked_lock(nbl);
348}
349
350static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
Jeff Laytonf4564582019-04-22 12:34:24 -0400351 .prepare = nfsd4_cb_notify_lock_prepare,
Jeff Layton76d348f2016-09-16 16:28:24 -0400352 .done = nfsd4_cb_notify_lock_done,
353 .release = nfsd4_cb_notify_lock_release,
354};
355
Kinglong Meeb5971af2014-08-22 10:18:43 -0400356static inline struct nfs4_stateowner *
357nfs4_get_stateowner(struct nfs4_stateowner *sop)
358{
359 atomic_inc(&sop->so_count);
360 return sop;
361}
362
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400363static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400364same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400365{
366 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400367 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400368}
369
370static struct nfs4_openowner *
371find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400372 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400373{
374 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400375
Trond Myklebustd4f04892014-07-29 21:34:36 -0400376 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400377
Trond Myklebustd4f04892014-07-29 21:34:36 -0400378 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
379 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400380 if (!so->so_is_open_owner)
381 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400382 if (same_owner_str(so, &open->op_owner))
383 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400384 }
385 return NULL;
386}
387
388static struct nfs4_openowner *
389find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400390 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400391{
392 struct nfs4_openowner *oo;
393
Trond Myklebustd4f04892014-07-29 21:34:36 -0400394 spin_lock(&clp->cl_lock);
395 oo = find_openstateowner_str_locked(hashval, open, clp);
396 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400397 return oo;
398}
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400static inline u32
401opaque_hashval(const void *ptr, int nbytes)
402{
403 unsigned char *cptr = (unsigned char *) ptr;
404
405 u32 x = 0;
406 while (nbytes--) {
407 x *= 37;
408 x += *cptr++;
409 }
410 return x;
411}
412
Jeff Layton5b095e92014-10-23 08:01:02 -0400413static void nfsd4_free_file_rcu(struct rcu_head *rcu)
J. Bruce Fields32513b42011-10-13 16:00:16 -0400414{
Jeff Layton5b095e92014-10-23 08:01:02 -0400415 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
416
417 kmem_cache_free(file_slab, fp);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400418}
419
Christoph Hellwige6ba76e2014-08-14 08:50:16 +0200420void
NeilBrown13cd2182005-06-23 22:03:10 -0700421put_nfs4_file(struct nfs4_file *fi)
422{
Jeff Layton02e12152014-07-16 10:31:57 -0400423 might_lock(&state_lock);
424
Elena Reshetova818a34e2017-10-20 12:53:30 +0300425 if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton5b095e92014-10-23 08:01:02 -0400426 hlist_del_rcu(&fi->fi_hash);
Benny Halevycdc975052014-05-30 09:09:30 -0400427 spin_unlock(&state_lock);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200428 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
Jeff Layton5b095e92014-10-23 08:01:02 -0400429 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
430 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800431 }
NeilBrown13cd2182005-06-23 22:03:10 -0700432}
433
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400434static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400435__nfs4_get_fd(struct nfs4_file *f, int oflag)
436{
437 if (f->fi_fds[oflag])
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400438 return nfsd_file_get(f->fi_fds[oflag]);
Trond Myklebustde186432014-07-10 14:07:26 -0400439 return NULL;
440}
441
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400442static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400443find_writeable_file_locked(struct nfs4_file *f)
444{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400445 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400446
447 lockdep_assert_held(&f->fi_lock);
448
449 ret = __nfs4_get_fd(f, O_WRONLY);
450 if (!ret)
451 ret = __nfs4_get_fd(f, O_RDWR);
452 return ret;
453}
454
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400455static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400456find_writeable_file(struct nfs4_file *f)
457{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400458 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400459
460 spin_lock(&f->fi_lock);
461 ret = find_writeable_file_locked(f);
462 spin_unlock(&f->fi_lock);
463
464 return ret;
465}
466
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400467static struct nfsd_file *
468find_readable_file_locked(struct nfs4_file *f)
Trond Myklebustde186432014-07-10 14:07:26 -0400469{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400470 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400471
472 lockdep_assert_held(&f->fi_lock);
473
474 ret = __nfs4_get_fd(f, O_RDONLY);
475 if (!ret)
476 ret = __nfs4_get_fd(f, O_RDWR);
477 return ret;
478}
479
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400480static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400481find_readable_file(struct nfs4_file *f)
482{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400483 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400484
485 spin_lock(&f->fi_lock);
486 ret = find_readable_file_locked(f);
487 spin_unlock(&f->fi_lock);
488
489 return ret;
490}
491
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400492struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400493find_any_file(struct nfs4_file *f)
494{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400495 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400496
Trond Myklebusta451b122020-03-01 18:21:38 -0500497 if (!f)
498 return NULL;
Trond Myklebustde186432014-07-10 14:07:26 -0400499 spin_lock(&f->fi_lock);
500 ret = __nfs4_get_fd(f, O_RDWR);
501 if (!ret) {
502 ret = __nfs4_get_fd(f, O_WRONLY);
503 if (!ret)
504 ret = __nfs4_get_fd(f, O_RDONLY);
505 }
506 spin_unlock(&f->fi_lock);
507 return ret;
508}
509
J. Bruce Fields9affa432020-07-15 13:31:36 -0400510static struct nfsd_file *find_deleg_file(struct nfs4_file *f)
511{
512 struct nfsd_file *ret = NULL;
513
514 spin_lock(&f->fi_lock);
515 if (f->fi_deleg_file)
516 ret = nfsd_file_get(f->fi_deleg_file);
517 spin_unlock(&f->fi_lock);
518 return ret;
519}
520
Trond Myklebust02a35082014-07-25 07:34:22 -0400521static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800522unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700523
524/*
525 * Open owner state (share locks)
526 */
527
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500528/* hash tables for lock and open owners */
529#define OWNER_HASH_BITS 8
530#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
531#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700532
Trond Myklebustd4f04892014-07-29 21:34:36 -0400533static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400534{
535 unsigned int ret;
536
537 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500538 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400539}
NeilBrownef0f3392006-04-10 22:55:41 -0700540
NeilBrownef0f3392006-04-10 22:55:41 -0700541/* hash table for nfs4_file */
542#define FILE_HASH_BITS 8
543#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800544
Trond Myklebustca943212014-07-23 16:17:39 -0400545static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400546{
Trond Myklebustca943212014-07-23 16:17:39 -0400547 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
548}
549
550static unsigned int file_hashval(struct knfsd_fh *fh)
551{
552 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
553}
554
Jeff Layton89876f82013-04-02 09:01:59 -0400555static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700556
Jeff Layton12659652014-07-10 14:07:28 -0400557static void
558__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400559{
Jeff Layton7214e862014-07-10 14:07:33 -0400560 lockdep_assert_held(&fp->fi_lock);
561
Jeff Layton12659652014-07-10 14:07:28 -0400562 if (access & NFS4_SHARE_ACCESS_WRITE)
563 atomic_inc(&fp->fi_access[O_WRONLY]);
564 if (access & NFS4_SHARE_ACCESS_READ)
565 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400566}
567
Jeff Layton12659652014-07-10 14:07:28 -0400568static __be32
569nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400570{
Jeff Layton7214e862014-07-10 14:07:33 -0400571 lockdep_assert_held(&fp->fi_lock);
572
Jeff Layton12659652014-07-10 14:07:28 -0400573 /* Does this access mode make sense? */
574 if (access & ~NFS4_SHARE_ACCESS_BOTH)
575 return nfserr_inval;
576
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400577 /* Does it conflict with a deny mode already set? */
578 if ((access & fp->fi_share_deny) != 0)
579 return nfserr_share_denied;
580
Jeff Layton12659652014-07-10 14:07:28 -0400581 __nfs4_file_get_access(fp, access);
582 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400583}
584
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400585static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
586{
587 /* Common case is that there is no deny mode. */
588 if (deny) {
589 /* Does this deny mode make sense? */
590 if (deny & ~NFS4_SHARE_DENY_BOTH)
591 return nfserr_inval;
592
593 if ((deny & NFS4_SHARE_DENY_READ) &&
594 atomic_read(&fp->fi_access[O_RDONLY]))
595 return nfserr_share_denied;
596
597 if ((deny & NFS4_SHARE_DENY_WRITE) &&
598 atomic_read(&fp->fi_access[O_WRONLY]))
599 return nfserr_share_denied;
600 }
601 return nfs_ok;
602}
603
J. Bruce Fields998db522010-08-07 09:21:41 -0400604static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400605{
Trond Myklebustde186432014-07-10 14:07:26 -0400606 might_lock(&fp->fi_lock);
607
608 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400609 struct nfsd_file *f1 = NULL;
610 struct nfsd_file *f2 = NULL;
Trond Myklebustde186432014-07-10 14:07:26 -0400611
Jeff Layton6d338b52014-07-10 14:07:29 -0400612 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400613 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400614 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400615 spin_unlock(&fp->fi_lock);
616 if (f1)
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400617 nfsd_file_put(f1);
Trond Myklebustde186432014-07-10 14:07:26 -0400618 if (f2)
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400619 nfsd_file_put(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400620 }
621}
622
Jeff Layton12659652014-07-10 14:07:28 -0400623static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400624{
Jeff Layton12659652014-07-10 14:07:28 -0400625 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
626
627 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400628 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400629 if (access & NFS4_SHARE_ACCESS_READ)
630 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400631}
632
Sachin Bhamare8287f002015-04-27 14:50:14 +0200633/*
634 * Allocate a new open/delegation state counter. This is needed for
635 * pNFS for proper return on close semantics.
636 *
637 * Note that we only allocate it for pNFS-enabled exports, otherwise
638 * all pointers to struct nfs4_clnt_odstate are always NULL.
639 */
640static struct nfs4_clnt_odstate *
641alloc_clnt_odstate(struct nfs4_client *clp)
642{
643 struct nfs4_clnt_odstate *co;
644
645 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
646 if (co) {
647 co->co_client = clp;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300648 refcount_set(&co->co_odcount, 1);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200649 }
650 return co;
651}
652
653static void
654hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
655{
656 struct nfs4_file *fp = co->co_file;
657
658 lockdep_assert_held(&fp->fi_lock);
659 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
660}
661
662static inline void
663get_clnt_odstate(struct nfs4_clnt_odstate *co)
664{
665 if (co)
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300666 refcount_inc(&co->co_odcount);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200667}
668
669static void
670put_clnt_odstate(struct nfs4_clnt_odstate *co)
671{
672 struct nfs4_file *fp;
673
674 if (!co)
675 return;
676
677 fp = co->co_file;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300678 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
Sachin Bhamare8287f002015-04-27 14:50:14 +0200679 list_del(&co->co_perfile);
680 spin_unlock(&fp->fi_lock);
681
682 nfsd4_return_all_file_layouts(co->co_client, fp);
683 kmem_cache_free(odstate_slab, co);
684 }
685}
686
687static struct nfs4_clnt_odstate *
688find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
689{
690 struct nfs4_clnt_odstate *co;
691 struct nfs4_client *cl;
692
693 if (!new)
694 return NULL;
695
696 cl = new->co_client;
697
698 spin_lock(&fp->fi_lock);
699 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
700 if (co->co_client == cl) {
701 get_clnt_odstate(co);
702 goto out;
703 }
704 }
705 co = new;
706 co->co_file = fp;
707 hash_clnt_odstate_locked(new);
708out:
709 spin_unlock(&fp->fi_lock);
710 return co;
711}
712
Kinglong Meed19fb702017-01-18 19:04:42 +0800713struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
714 void (*sc_free)(struct nfs4_stid *))
J. Bruce Fields996e0932011-10-17 11:14:48 -0400715{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500716 struct nfs4_stid *stid;
717 int new_id;
718
Trond Myklebustf8338832014-07-25 07:34:19 -0400719 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500720 if (!stid)
721 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400722
Jeff Layton4770d722014-07-29 21:34:10 -0400723 idr_preload(GFP_KERNEL);
724 spin_lock(&cl->cl_lock);
J. Bruce Fields78599c42019-04-22 15:26:23 -0400725 /* Reserving 0 for start of file in nfsdfs "states" file: */
726 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
Jeff Layton4770d722014-07-29 21:34:10 -0400727 spin_unlock(&cl->cl_lock);
728 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700729 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500730 goto out_free;
Kinglong Meed19fb702017-01-18 19:04:42 +0800731
732 stid->sc_free = sc_free;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500733 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500734 stid->sc_stateid.si_opaque.so_id = new_id;
735 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
736 /* Will be incremented before return to client: */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300737 refcount_set(&stid->sc_count, 1);
Jeff Layton9767feb2015-10-01 09:05:50 -0400738 spin_lock_init(&stid->sc_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400739 INIT_LIST_HEAD(&stid->sc_cp_list);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500740
J. Bruce Fields996e0932011-10-17 11:14:48 -0400741 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500742 * It shouldn't be a problem to reuse an opaque stateid value.
743 * I don't think it is for 4.1. But with 4.0 I worry that, for
744 * example, a stray write retransmission could be accepted by
745 * the server when it should have been rejected. Therefore,
746 * adopt a trick from the sctp code to attempt to maximize the
747 * amount of time until an id is reused, by ensuring they always
748 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400749 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500750 return stid;
751out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800752 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500753 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400754}
755
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400756/*
757 * Create a unique stateid_t to represent each COPY.
758 */
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400759static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
760 unsigned char sc_type)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400761{
762 int new_id;
763
Arnd Bergmann9cc76802019-10-31 15:53:13 +0100764 stid->stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400765 stid->stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
766 stid->sc_type = sc_type;
767
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400768 idr_preload(GFP_KERNEL);
769 spin_lock(&nn->s2s_cp_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400770 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
771 stid->stid.si_opaque.so_id = new_id;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400772 spin_unlock(&nn->s2s_cp_lock);
773 idr_preload_end();
774 if (new_id < 0)
775 return 0;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400776 return 1;
777}
778
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400779int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
780{
781 return nfs4_init_cp_state(nn, &copy->cp_stateid, NFS4_COPY_STID);
782}
783
784struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
785 struct nfs4_stid *p_stid)
786{
787 struct nfs4_cpntf_state *cps;
788
789 cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
790 if (!cps)
791 return NULL;
Arnd Bergmann20b7d862019-11-04 16:31:52 +0100792 cps->cpntf_time = ktime_get_boottime_seconds();
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400793 refcount_set(&cps->cp_stateid.sc_count, 1);
794 if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
795 goto out_free;
796 spin_lock(&nn->s2s_cp_lock);
797 list_add(&cps->cp_list, &p_stid->sc_cp_list);
798 spin_unlock(&nn->s2s_cp_lock);
799 return cps;
800out_free:
801 kfree(cps);
802 return NULL;
803}
804
805void nfs4_free_copy_state(struct nfsd4_copy *copy)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400806{
807 struct nfsd_net *nn;
808
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400809 WARN_ON_ONCE(copy->cp_stateid.sc_type != NFS4_COPY_STID);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400810 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
811 spin_lock(&nn->s2s_cp_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400812 idr_remove(&nn->s2s_cp_stateids,
813 copy->cp_stateid.stid.si_opaque.so_id);
814 spin_unlock(&nn->s2s_cp_lock);
815}
816
817static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
818{
819 struct nfs4_cpntf_state *cps;
820 struct nfsd_net *nn;
821
822 nn = net_generic(net, nfsd_net_id);
823 spin_lock(&nn->s2s_cp_lock);
824 while (!list_empty(&stid->sc_cp_list)) {
825 cps = list_first_entry(&stid->sc_cp_list,
826 struct nfs4_cpntf_state, cp_list);
827 _free_cpntf_state_locked(nn, cps);
828 }
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400829 spin_unlock(&nn->s2s_cp_lock);
830}
831
Jeff Laytonb49e0842014-07-29 21:34:11 -0400832static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400833{
Trond Myklebust60116952014-07-29 21:34:06 -0400834 struct nfs4_stid *stid;
Trond Myklebust60116952014-07-29 21:34:06 -0400835
Kinglong Meed19fb702017-01-18 19:04:42 +0800836 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
Trond Myklebust60116952014-07-29 21:34:06 -0400837 if (!stid)
838 return NULL;
839
Kinglong Meed19fb702017-01-18 19:04:42 +0800840 return openlockstateid(stid);
Trond Myklebust60116952014-07-29 21:34:06 -0400841}
842
843static void nfs4_free_deleg(struct nfs4_stid *stid)
844{
Trond Myklebust60116952014-07-29 21:34:06 -0400845 kmem_cache_free(deleg_slab, stid);
846 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400847}
848
NeilBrown6282cd52014-06-04 17:39:26 +1000849/*
850 * When we recall a delegation, we should be careful not to hand it
851 * out again straight away.
852 * To ensure this we keep a pair of bloom filters ('new' and 'old')
853 * in which the filehandles of recalled delegations are "stored".
854 * If a filehandle appear in either filter, a delegation is blocked.
855 * When a delegation is recalled, the filehandle is stored in the "new"
856 * filter.
857 * Every 30 seconds we swap the filters and clear the "new" one,
858 * unless both are empty of course.
859 *
860 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
861 * low 3 bytes as hash-table indices.
862 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400863 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000864 * is used to manage concurrent access. Testing does not need the lock
865 * except when swapping the two filters.
866 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400867static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000868static struct bloom_pair {
869 int entries, old_entries;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200870 time64_t swap_time;
NeilBrown6282cd52014-06-04 17:39:26 +1000871 int new; /* index into 'set' */
872 DECLARE_BITMAP(set[2], 256);
873} blocked_delegations;
874
875static int delegation_blocked(struct knfsd_fh *fh)
876{
877 u32 hash;
878 struct bloom_pair *bd = &blocked_delegations;
879
880 if (bd->entries == 0)
881 return 0;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200882 if (ktime_get_seconds() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400883 spin_lock(&blocked_delegations_lock);
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200884 if (ktime_get_seconds() - bd->swap_time > 30) {
NeilBrown6282cd52014-06-04 17:39:26 +1000885 bd->entries -= bd->old_entries;
886 bd->old_entries = bd->entries;
887 memset(bd->set[bd->new], 0,
888 sizeof(bd->set[0]));
889 bd->new = 1-bd->new;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200890 bd->swap_time = ktime_get_seconds();
NeilBrown6282cd52014-06-04 17:39:26 +1000891 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400892 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000893 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100894 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000895 if (test_bit(hash&255, bd->set[0]) &&
896 test_bit((hash>>8)&255, bd->set[0]) &&
897 test_bit((hash>>16)&255, bd->set[0]))
898 return 1;
899
900 if (test_bit(hash&255, bd->set[1]) &&
901 test_bit((hash>>8)&255, bd->set[1]) &&
902 test_bit((hash>>16)&255, bd->set[1]))
903 return 1;
904
905 return 0;
906}
907
908static void block_delegations(struct knfsd_fh *fh)
909{
910 u32 hash;
911 struct bloom_pair *bd = &blocked_delegations;
912
Daniel Borkmann87545892014-12-10 16:33:11 +0100913 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000914
Jeff Laytonf54fe962014-07-25 07:34:26 -0400915 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000916 __set_bit(hash&255, bd->set[bd->new]);
917 __set_bit((hash>>8)&255, bd->set[bd->new]);
918 __set_bit((hash>>16)&255, bd->set[bd->new]);
919 if (bd->entries == 0)
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200920 bd->swap_time = ktime_get_seconds();
NeilBrown6282cd52014-06-04 17:39:26 +1000921 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400922 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000923}
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925static struct nfs4_delegation *
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500926alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
927 struct svc_fh *current_fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +0200928 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
930 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400931 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400934 n = atomic_long_inc_return(&num_delegations);
935 if (n < 0 || n > max_delegations)
936 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000937 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400938 goto out_dec;
Kinglong Meed19fb702017-01-18 19:04:42 +0800939 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700940 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400941 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400942
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400943 /*
944 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400945 * meaning of seqid 0 isn't meaningful, really, but let's avoid
946 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400947 */
948 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700949 INIT_LIST_HEAD(&dp->dl_perfile);
950 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200952 dp->dl_clnt_odstate = odstate;
953 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400954 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200955 dp->dl_retries = 1;
956 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200957 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500958 get_nfs4_file(fp);
959 dp->dl_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400961out_dec:
962 atomic_long_dec(&num_delegations);
963 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
966void
Trond Myklebust60116952014-07-29 21:34:06 -0400967nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Trond Myklebust11b91642014-07-29 21:34:08 -0400969 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400970 struct nfs4_client *clp = s->sc_client;
971
Jeff Layton4770d722014-07-29 21:34:10 -0400972 might_lock(&clp->cl_lock);
973
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300974 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
Jeff Laytonb401be222014-07-29 21:34:33 -0400975 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400976 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400977 }
Trond Myklebust60116952014-07-29 21:34:06 -0400978 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400979 nfs4_free_cpntf_statelist(clp->net, s);
Jeff Layton4770d722014-07-29 21:34:10 -0400980 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400981 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400982 if (fp)
983 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Jeff Layton9767feb2015-10-01 09:05:50 -0400986void
987nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
988{
989 stateid_t *src = &stid->sc_stateid;
990
991 spin_lock(&stid->sc_lock);
992 if (unlikely(++src->si_generation == 0))
993 src->si_generation = 1;
994 memcpy(dst, src, sizeof(*dst));
995 spin_unlock(&stid->sc_lock);
996}
997
J. Bruce Fields353601e2018-02-16 14:29:42 -0500998static void put_deleg_file(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001000 struct nfsd_file *nf = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -05001001
1002 spin_lock(&fp->fi_lock);
1003 if (--fp->fi_delegees == 0)
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001004 swap(nf, fp->fi_deleg_file);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001005 spin_unlock(&fp->fi_lock);
1006
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001007 if (nf)
1008 nfsd_file_put(nf);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001009}
1010
1011static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1012{
J. Bruce Fieldscba7b3d2018-02-15 15:50:49 -05001013 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001014 struct nfsd_file *nf = fp->fi_deleg_file;
Jeff Layton417c6622014-07-21 09:34:57 -04001015
J. Bruce Fieldsb8232d32018-02-15 15:29:15 -05001016 WARN_ON_ONCE(!fp->fi_delegees);
1017
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001018 vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001019 put_deleg_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001022static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1023{
1024 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001025 nfs4_unlock_deleg_lease(dp);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001026 nfs4_put_stid(&dp->dl_stid);
1027}
1028
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001029void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001030{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001031 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001032}
1033
Andrew Elble34ed9872015-10-15 12:07:28 -04001034/**
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001035 * nfs4_delegation_exists - Discover if this delegation already exists
Andrew Elble34ed9872015-10-15 12:07:28 -04001036 * @clp: a pointer to the nfs4_client we're granting a delegation to
1037 * @fp: a pointer to the nfs4_file we're granting a delegation on
1038 *
1039 * Return:
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001040 * On success: true iff an existing delegation is found
Andrew Elble34ed9872015-10-15 12:07:28 -04001041 */
1042
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001043static bool
1044nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
Benny Halevy931ee562014-05-30 09:09:27 -04001045{
Andrew Elble34ed9872015-10-15 12:07:28 -04001046 struct nfs4_delegation *searchdp = NULL;
1047 struct nfs4_client *searchclp = NULL;
1048
Benny Halevycdc975052014-05-30 09:09:30 -04001049 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04001050 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001051
Andrew Elble34ed9872015-10-15 12:07:28 -04001052 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1053 searchclp = searchdp->dl_stid.sc_client;
1054 if (clp == searchclp) {
Fengguang Wu51d87bc2018-03-22 13:37:20 +08001055 return true;
Andrew Elble34ed9872015-10-15 12:07:28 -04001056 }
1057 }
Fengguang Wu51d87bc2018-03-22 13:37:20 +08001058 return false;
Andrew Elble34ed9872015-10-15 12:07:28 -04001059}
1060
1061/**
1062 * hash_delegation_locked - Add a delegation to the appropriate lists
1063 * @dp: a pointer to the nfs4_delegation we are adding.
1064 * @fp: a pointer to the nfs4_file we're granting a delegation on
1065 *
1066 * Return:
1067 * On success: NULL if the delegation was successfully hashed.
1068 *
1069 * On error: -EAGAIN if one was previously granted to this
1070 * nfs4_client for this nfs4_file. Delegation is not hashed.
1071 *
1072 */
1073
1074static int
1075hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1076{
Andrew Elble34ed9872015-10-15 12:07:28 -04001077 struct nfs4_client *clp = dp->dl_stid.sc_client;
1078
1079 lockdep_assert_held(&state_lock);
1080 lockdep_assert_held(&fp->fi_lock);
1081
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001082 if (nfs4_delegation_exists(clp, fp))
1083 return -EAGAIN;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001084 refcount_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -04001085 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -04001086 list_add(&dp->dl_perfile, &fp->fi_delegations);
Andrew Elble34ed9872015-10-15 12:07:28 -04001087 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1088 return 0;
Benny Halevy931ee562014-05-30 09:09:27 -04001089}
1090
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001091static bool
Jeff Layton42690672014-07-25 07:34:20 -04001092unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Trond Myklebust11b91642014-07-29 21:34:08 -04001094 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -04001095
Jeff Layton42690672014-07-25 07:34:20 -04001096 lockdep_assert_held(&state_lock);
1097
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001098 if (list_empty(&dp->dl_perfile))
1099 return false;
1100
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04001101 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -04001102 /* Ensure that deleg break won't try to requeue it */
1103 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -04001104 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001105 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -04001107 list_del_init(&dp->dl_perfile);
1108 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001109 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001110}
1111
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001112static void destroy_delegation(struct nfs4_delegation *dp)
1113{
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001114 bool unhashed;
1115
Jeff Layton42690672014-07-25 07:34:20 -04001116 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001117 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -04001118 spin_unlock(&state_lock);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001119 if (unhashed)
1120 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001121}
1122
1123static void revoke_delegation(struct nfs4_delegation *dp)
1124{
1125 struct nfs4_client *clp = dp->dl_stid.sc_client;
1126
Jeff Layton2d4a5322014-07-25 07:34:21 -04001127 WARN_ON(!list_empty(&dp->dl_recall_lru));
1128
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001129 if (clp->cl_minorversion) {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001130 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001131 refcount_inc(&dp->dl_stid.sc_count);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001132 spin_lock(&clp->cl_lock);
1133 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1134 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001135 }
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001136 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001137}
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139/*
1140 * SETCLIENTID state
1141 */
1142
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001143static unsigned int clientid_hashval(u32 id)
1144{
1145 return id & CLIENT_HASH_MASK;
1146}
1147
Scott Mayhew6b189102019-03-26 18:06:26 -04001148static unsigned int clientstr_hashval(struct xdr_netobj name)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001149{
Scott Mayhew6b189102019-03-26 18:06:26 -04001150 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001151}
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001154 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1155 * st_{access,deny}_bmap field of the stateid, in order to track not
1156 * only what share bits are currently in force, but also what
1157 * combinations of share bits previous opens have used. This allows us
1158 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1159 * return an error if the client attempt to downgrade to a combination
1160 * of share bits not explicable by closing some of its previous opens.
1161 *
1162 * XXX: This enforcement is actually incomplete, since we don't keep
1163 * track of access/deny bit combinations; so, e.g., we allow:
1164 *
1165 * OPEN allow read, deny write
1166 * OPEN allow both, deny none
1167 * DOWNGRADE allow read, deny none
1168 *
1169 * which we should reject.
1170 */
Jeff Layton5ae037e2012-05-11 09:45:11 -04001171static unsigned int
1172bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001173 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -04001174 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001175
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001176 for (i = 1; i < 4; i++) {
1177 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -04001178 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001179 }
Jeff Layton5ae037e2012-05-11 09:45:11 -04001180 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001181}
1182
Jeff Layton82c5ff12012-05-11 09:45:13 -04001183/* set share access for a given stateid */
1184static inline void
1185set_access(u32 access, struct nfs4_ol_stateid *stp)
1186{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001187 unsigned char mask = 1 << access;
1188
1189 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1190 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001191}
1192
1193/* clear share access for a given stateid */
1194static inline void
1195clear_access(u32 access, struct nfs4_ol_stateid *stp)
1196{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001197 unsigned char mask = 1 << access;
1198
1199 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1200 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001201}
1202
1203/* test whether a given stateid has access */
1204static inline bool
1205test_access(u32 access, struct nfs4_ol_stateid *stp)
1206{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001207 unsigned char mask = 1 << access;
1208
1209 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001210}
1211
Jeff Laytonce0fc432012-05-11 09:45:14 -04001212/* set share deny for a given stateid */
1213static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001214set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001215{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001216 unsigned char mask = 1 << deny;
1217
1218 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1219 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001220}
1221
1222/* clear share deny for a given stateid */
1223static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001224clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001225{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001226 unsigned char mask = 1 << deny;
1227
1228 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1229 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001230}
1231
1232/* test whether a given stateid is denying specific access */
1233static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -04001234test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001235{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001236 unsigned char mask = 1 << deny;
1237
1238 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001239}
1240
1241static int nfs4_access_to_omode(u32 access)
1242{
J. Bruce Fields8f34a432010-09-02 15:23:16 -04001243 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001244 case NFS4_SHARE_ACCESS_READ:
1245 return O_RDONLY;
1246 case NFS4_SHARE_ACCESS_WRITE:
1247 return O_WRONLY;
1248 case NFS4_SHARE_ACCESS_BOTH:
1249 return O_RDWR;
1250 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001251 WARN_ON_ONCE(1);
1252 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001253}
1254
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001255/*
1256 * A stateid that had a deny mode associated with it is being released
1257 * or downgraded. Recalculate the deny mode on the file.
1258 */
1259static void
1260recalculate_deny_mode(struct nfs4_file *fp)
1261{
1262 struct nfs4_ol_stateid *stp;
1263
1264 spin_lock(&fp->fi_lock);
1265 fp->fi_share_deny = 0;
1266 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1267 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1268 spin_unlock(&fp->fi_lock);
1269}
1270
1271static void
1272reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1273{
1274 int i;
1275 bool change = false;
1276
1277 for (i = 1; i < 4; i++) {
1278 if ((i & deny) != i) {
1279 change = true;
1280 clear_deny(i, stp);
1281 }
1282 }
1283
1284 /* Recalculate per-file deny mode if there was a change */
1285 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -04001286 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001287}
1288
Jeff Layton82c5ff12012-05-11 09:45:13 -04001289/* release all access and file references for a given stateid */
1290static void
1291release_all_access(struct nfs4_ol_stateid *stp)
1292{
1293 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -04001294 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001295
1296 if (fp && stp->st_deny_bmap != 0)
1297 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001298
1299 for (i = 1; i < 4; i++) {
1300 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -04001301 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001302 clear_access(i, stp);
1303 }
1304}
1305
Kinglong Meed50ffde2015-07-16 12:05:07 +08001306static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1307{
1308 kfree(sop->so_owner.data);
1309 sop->so_ops->so_free(sop);
1310}
1311
Jeff Layton6b180f02014-07-29 21:34:26 -04001312static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1313{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001314 struct nfs4_client *clp = sop->so_client;
1315
1316 might_lock(&clp->cl_lock);
1317
1318 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001319 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001320 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001321 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001322 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001323}
1324
Trond Myklebusta451b122020-03-01 18:21:38 -05001325static bool
1326nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1327{
1328 return list_empty(&stp->st_perfile);
1329}
1330
Jeff Laytone8568732015-08-24 12:41:47 -04001331static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001332{
Trond Myklebust11b91642014-07-29 21:34:08 -04001333 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001334
Jeff Layton1c755dc2014-07-29 21:34:12 -04001335 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1336
Jeff Laytone8568732015-08-24 12:41:47 -04001337 if (list_empty(&stp->st_perfile))
1338 return false;
1339
Trond Myklebust1d31a252014-07-10 14:07:25 -04001340 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001341 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001342 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001343 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001344 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001345}
1346
Trond Myklebust60116952014-07-29 21:34:06 -04001347static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001348{
Trond Myklebust60116952014-07-29 21:34:06 -04001349 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001350
Sachin Bhamare8287f002015-04-27 14:50:14 +02001351 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001352 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001353 if (stp->st_stateowner)
1354 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -04001355 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001356}
1357
Jeff Laytonb49e0842014-07-29 21:34:11 -04001358static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001359{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001360 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1361 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001362 struct nfsd_file *nf;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001363
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001364 nf = find_any_file(stp->st_stid.sc_file);
1365 if (nf) {
1366 get_file(nf->nf_file);
1367 filp_close(nf->nf_file, (fl_owner_t)lo);
1368 nfsd_file_put(nf);
1369 }
Jeff Laytonb49e0842014-07-29 21:34:11 -04001370 nfs4_free_ol_stateid(stid);
1371}
1372
Jeff Layton2c41beb2014-07-29 21:34:41 -04001373/*
1374 * Put the persistent reference to an already unhashed generic stateid, while
1375 * holding the cl_lock. If it's the last reference, then put it onto the
1376 * reaplist for later destruction.
1377 */
1378static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1379 struct list_head *reaplist)
1380{
1381 struct nfs4_stid *s = &stp->st_stid;
1382 struct nfs4_client *clp = s->sc_client;
1383
1384 lockdep_assert_held(&clp->cl_lock);
1385
1386 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1387
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001388 if (!refcount_dec_and_test(&s->sc_count)) {
Jeff Layton2c41beb2014-07-29 21:34:41 -04001389 wake_up_all(&close_wq);
1390 return;
1391 }
1392
1393 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1394 list_add(&stp->st_locks, reaplist);
1395}
1396
Jeff Laytone8568732015-08-24 12:41:47 -04001397static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001398{
Chuck Leverf46c4452016-10-29 18:19:03 -04001399 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001400
Trond Myklebusta451b122020-03-01 18:21:38 -05001401 if (!unhash_ol_stateid(stp))
1402 return false;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001403 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001404 nfs4_unhash_stid(&stp->st_stid);
Trond Myklebusta451b122020-03-01 18:21:38 -05001405 return true;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001406}
1407
Jeff Layton5adfd882014-07-29 21:34:31 -04001408static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001409{
Chuck Leverf46c4452016-10-29 18:19:03 -04001410 struct nfs4_client *clp = stp->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04001411 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001412
Chuck Leverf46c4452016-10-29 18:19:03 -04001413 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001414 unhashed = unhash_lock_stateid(stp);
Chuck Leverf46c4452016-10-29 18:19:03 -04001415 spin_unlock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001416 if (unhashed)
1417 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001418}
1419
Trond Myklebustc58c6612014-07-29 21:34:35 -04001420static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001421{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001422 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001423
Trond Myklebustd4f04892014-07-29 21:34:36 -04001424 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001425
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001426 list_del_init(&lo->lo_owner.so_strhash);
1427}
1428
Jeff Layton2c41beb2014-07-29 21:34:41 -04001429/*
1430 * Free a list of generic stateids that were collected earlier after being
1431 * fully unhashed.
1432 */
1433static void
1434free_ol_stateid_reaplist(struct list_head *reaplist)
1435{
1436 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001437 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001438
1439 might_sleep();
1440
1441 while (!list_empty(reaplist)) {
1442 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1443 st_locks);
1444 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001445 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001446 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001447 if (fp)
1448 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001449 }
1450}
1451
Jeff Laytond83017f2014-07-29 21:34:42 -04001452static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1453 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001454{
1455 struct nfs4_ol_stateid *stp;
1456
Jeff Laytone8568732015-08-24 12:41:47 -04001457 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1458
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001459 while (!list_empty(&open_stp->st_locks)) {
1460 stp = list_entry(open_stp->st_locks.next,
1461 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001462 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001463 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001464 }
1465}
1466
Jeff Laytone8568732015-08-24 12:41:47 -04001467static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001468 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001469{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001470 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1471
Trond Myklebusta451b122020-03-01 18:21:38 -05001472 if (!unhash_ol_stateid(stp))
1473 return false;
Jeff Laytond83017f2014-07-29 21:34:42 -04001474 release_open_stateid_locks(stp, reaplist);
Trond Myklebusta451b122020-03-01 18:21:38 -05001475 return true;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001476}
1477
1478static void release_open_stateid(struct nfs4_ol_stateid *stp)
1479{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001480 LIST_HEAD(reaplist);
1481
1482 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001483 if (unhash_open_stateid(stp, &reaplist))
1484 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001485 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1486 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001487}
1488
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001489static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001490{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001491 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001492
Trond Myklebustd4f04892014-07-29 21:34:36 -04001493 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001494
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001495 list_del_init(&oo->oo_owner.so_strhash);
1496 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001497}
1498
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001499static void release_last_closed_stateid(struct nfs4_openowner *oo)
1500{
Jeff Layton217526e2014-07-30 08:27:11 -04001501 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1502 nfsd_net_id);
1503 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001504
Jeff Layton217526e2014-07-30 08:27:11 -04001505 spin_lock(&nn->client_lock);
1506 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001507 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001508 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001509 oo->oo_last_closed_stid = NULL;
1510 }
Jeff Layton217526e2014-07-30 08:27:11 -04001511 spin_unlock(&nn->client_lock);
1512 if (s)
1513 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001514}
1515
Jeff Layton2c41beb2014-07-29 21:34:41 -04001516static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001517{
1518 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001519 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001520 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001521
Jeff Layton2c41beb2014-07-29 21:34:41 -04001522 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001523
Trond Myklebustd4f04892014-07-29 21:34:36 -04001524 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001525 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001526 while (!list_empty(&oo->oo_owner.so_stateids)) {
1527 stp = list_first_entry(&oo->oo_owner.so_stateids,
1528 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001529 if (unhash_open_stateid(stp, &reaplist))
1530 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001531 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001532 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001533 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001534 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001535 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001536}
1537
Marc Eshel5282fd72009-04-03 08:27:52 +03001538static inline int
1539hash_sessionid(struct nfs4_sessionid *sessionid)
1540{
1541 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1542
1543 return sid->sequence % SESSION_HASH_SIZE;
1544}
1545
Mark Salter135dd002015-04-06 09:46:00 -04001546#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001547static inline void
1548dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1549{
1550 u32 *ptr = (u32 *)(&sessionid->data[0]);
1551 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1552}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001553#else
1554static inline void
1555dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1556{
1557}
1558#endif
1559
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001560/*
1561 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1562 * won't be used for replay.
1563 */
1564void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1565{
1566 struct nfs4_stateowner *so = cstate->replay_owner;
1567
1568 if (nfserr == nfserr_replay_me)
1569 return;
1570
1571 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001572 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001573 return;
1574 }
1575 if (!so)
1576 return;
1577 if (so->so_is_open_owner)
1578 release_last_closed_stateid(openowner(so));
1579 so->so_seqid++;
1580 return;
1581}
Marc Eshel5282fd72009-04-03 08:27:52 +03001582
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001583static void
1584gen_sessionid(struct nfsd4_session *ses)
1585{
1586 struct nfs4_client *clp = ses->se_client;
1587 struct nfsd4_sessionid *sid;
1588
1589 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1590 sid->clientid = clp->cl_clientid;
1591 sid->sequence = current_sessionid++;
1592 sid->reserved = 0;
1593}
1594
1595/*
Andy Adamsona649637c72009-08-28 08:45:01 -04001596 * The protocol defines ca_maxresponssize_cached to include the size of
1597 * the rpc header, but all we need to cache is the data starting after
1598 * the end of the initial SEQUENCE operation--the rest we regenerate
1599 * each time. Therefore we can advertise a ca_maxresponssize_cached
1600 * value that is the number of bytes in our cache plus a few additional
1601 * bytes. In order to stay on the safe side, and not promise more than
1602 * we can cache, those additional bytes must be the minimum possible: 24
1603 * bytes of rpc header (xid through accept state, with AUTH_NULL
1604 * verifier), 12 for the compound header (with zero-length tag), and 44
1605 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001606 */
Andy Adamsona649637c72009-08-28 08:45:01 -04001607#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1608
Andy Adamson557ce262009-08-28 08:45:04 -04001609static void
1610free_session_slots(struct nfsd4_session *ses)
1611{
1612 int i;
1613
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001614 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1615 free_svc_cred(&ses->se_slots[i]->sl_cred);
Andy Adamson557ce262009-08-28 08:45:04 -04001616 kfree(ses->se_slots[i]);
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001617 }
Andy Adamson557ce262009-08-28 08:45:04 -04001618}
1619
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001620/*
1621 * We don't actually need to cache the rpc and session headers, so we
1622 * can allocate a little less for each slot:
1623 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001624static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001625{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001626 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001627
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001628 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1629 size = 0;
1630 else
1631 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1632 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001633}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001634
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001635/*
1636 * XXX: If we run out of reserved DRC memory we could (up to a point)
1637 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001638 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001639 */
NeilBrown2030ca52019-09-20 16:36:45 +10001640static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001641{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001642 u32 slotsize = slot_bytes(ca);
1643 u32 num = ca->maxreqs;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001644 unsigned long avail, total_avail;
NeilBrown2030ca52019-09-20 16:36:45 +10001645 unsigned int scale_factor;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001646
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001647 spin_lock(&nfsd_drc_lock);
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001648 if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1649 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1650 else
1651 /* We have handed out more space than we chose in
1652 * set_max_drc() to allow. That isn't really a
1653 * problem as long as that doesn't make us think we
1654 * have lots more due to integer overflow.
1655 */
1656 total_avail = 0;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001657 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001658 /*
NeilBrown2030ca52019-09-20 16:36:45 +10001659 * Never use more than a fraction of the remaining memory,
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001660 * unless it's the only way to give this client a slot.
NeilBrown2030ca52019-09-20 16:36:45 +10001661 * The chosen fraction is either 1/8 or 1/number of threads,
1662 * whichever is smaller. This ensures there are adequate
1663 * slots to support multiple clients per thread.
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001664 * Give the client one slot even if that would require
1665 * over-allocation--it is better than failure.
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001666 */
NeilBrown2030ca52019-09-20 16:36:45 +10001667 scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1668
1669 avail = clamp_t(unsigned long, avail, slotsize,
1670 total_avail/scale_factor);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001671 num = min_t(int, num, avail / slotsize);
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001672 num = max_t(int, num, 1);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001673 nfsd_drc_mem_used += num * slotsize;
1674 spin_unlock(&nfsd_drc_lock);
1675
1676 return num;
1677}
1678
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001679static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001680{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001681 int slotsize = slot_bytes(ca);
1682
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001683 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001684 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001685 spin_unlock(&nfsd_drc_lock);
1686}
1687
Kinglong Mee60810e52014-01-01 00:35:47 +08001688static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1689 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001690{
Kinglong Mee60810e52014-01-01 00:35:47 +08001691 int numslots = fattrs->maxreqs;
1692 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001693 struct nfsd4_session *new;
1694 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001695
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001696 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001697 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1698 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001699
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001700 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001701 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001702 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001703 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001704 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001705 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001706 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001707 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001708 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001709
1710 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1711 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1712
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001713 return new;
1714out_free:
1715 while (i--)
1716 kfree(new->se_slots[i]);
1717 kfree(new);
1718 return NULL;
1719}
1720
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001721static void free_conn(struct nfsd4_conn *c)
1722{
1723 svc_xprt_put(c->cn_xprt);
1724 kfree(c);
1725}
1726
1727static void nfsd4_conn_lost(struct svc_xpt_user *u)
1728{
1729 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1730 struct nfs4_client *clp = c->cn_session->se_client;
1731
1732 spin_lock(&clp->cl_lock);
1733 if (!list_empty(&c->cn_persession)) {
1734 list_del(&c->cn_persession);
1735 free_conn(c);
1736 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001737 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001738 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001739}
1740
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001741static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001742{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001743 struct nfsd4_conn *conn;
1744
1745 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1746 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001747 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001748 svc_xprt_get(rqstp->rq_xprt);
1749 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001750 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001751 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1752 return conn;
1753}
1754
J. Bruce Fields328ead22010-09-29 16:11:06 -04001755static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1756{
1757 conn->cn_session = ses;
1758 list_add(&conn->cn_persession, &ses->se_conns);
1759}
1760
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001761static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1762{
1763 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001764
1765 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001766 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001767 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001768}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001769
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001770static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001771{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001772 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001773 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001774}
1775
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001776static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001777{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001778 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001779
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001780 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001781 ret = nfsd4_register_conn(conn);
1782 if (ret)
1783 /* oops; xprt is already down: */
1784 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001785 /* We may have gained or lost a callback channel: */
1786 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001787}
1788
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001789static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001790{
1791 u32 dir = NFS4_CDFC4_FORE;
1792
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001793 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001794 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001795 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001796}
1797
1798/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001799static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001800{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001801 struct nfs4_client *clp = s->se_client;
1802 struct nfsd4_conn *c;
1803
1804 spin_lock(&clp->cl_lock);
1805 while (!list_empty(&s->se_conns)) {
1806 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1807 list_del_init(&c->cn_persession);
1808 spin_unlock(&clp->cl_lock);
1809
1810 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1811 free_conn(c);
1812
1813 spin_lock(&clp->cl_lock);
1814 }
1815 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001816}
1817
J. Bruce Fields1377b692012-09-11 21:42:40 -04001818static void __free_session(struct nfsd4_session *ses)
1819{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001820 free_session_slots(ses);
1821 kfree(ses);
1822}
1823
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001824static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001825{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001826 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001827 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001828 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001829}
Andy Adamson557ce262009-08-28 08:45:04 -04001830
Fengguang Wu135ae822012-11-10 07:20:25 -05001831static 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 -04001832{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001833 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001834 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001835
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001836 new->se_client = clp;
1837 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001838
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001839 INIT_LIST_HEAD(&new->se_conns);
1840
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001841 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001842 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001843 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001844 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001845 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001846 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001847 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001848 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001849 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001850 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001851
Chuck Leverb0d2e422014-08-22 15:10:59 -04001852 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001853 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001854 /*
1855 * This is a little silly; with sessions there's no real
1856 * use for the callback address. Use the peer address
1857 * as a reasonable default for now, but consider fixing
1858 * the rpc client not to require an address in the
1859 * future:
1860 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001861 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1862 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001863 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001864}
1865
Benny Halevy9089f1b2010-05-12 00:12:26 +03001866/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001867static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001868__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001869{
1870 struct nfsd4_session *elem;
1871 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001872 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001873
Trond Myklebust0a880a22014-07-30 08:27:10 -04001874 lockdep_assert_held(&nn->client_lock);
1875
Marc Eshel5282fd72009-04-03 08:27:52 +03001876 dump_sessionid(__func__, sessionid);
1877 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001878 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001879 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001880 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1881 NFS4_MAX_SESSIONID_LEN)) {
1882 return elem;
1883 }
1884 }
1885
1886 dprintk("%s: session not found\n", __func__);
1887 return NULL;
1888}
1889
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001890static struct nfsd4_session *
1891find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1892 __be32 *ret)
1893{
1894 struct nfsd4_session *session;
1895 __be32 status = nfserr_badsession;
1896
1897 session = __find_in_sessionid_hashtbl(sessionid, net);
1898 if (!session)
1899 goto out;
1900 status = nfsd4_get_session_locked(session);
1901 if (status)
1902 session = NULL;
1903out:
1904 *ret = status;
1905 return session;
1906}
1907
Benny Halevy9089f1b2010-05-12 00:12:26 +03001908/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001909static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001910unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001911{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001912 struct nfs4_client *clp = ses->se_client;
1913 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1914
1915 lockdep_assert_held(&nn->client_lock);
1916
Andy Adamson7116ed62009-04-03 08:27:43 +03001917 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001918 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001919 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001920 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001921}
1922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1924static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001925STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001927 /*
1928 * We're assuming the clid was not given out from a boot
1929 * precisely 2^32 (about 136 years) before this one. That seems
1930 * a safe assumption:
1931 */
1932 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return 0;
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04001934 trace_nfsd_clid_stale(clid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 return 1;
1936}
1937
1938/*
1939 * XXX Should we use a slab cache ?
1940 * This type of memory management is somewhat inefficient, but we use it
1941 * anyway since SETCLIENTID is not a common operation.
1942 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001943static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944{
1945 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001946 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Jeff Layton9258a2d2018-03-16 09:47:22 -04001948 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001949 if (clp == NULL)
1950 return NULL;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04001951 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001952 if (clp->cl_name.data == NULL)
1953 goto err_no_name;
Kees Cook6da2ec52018-06-12 13:55:00 -07001954 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1955 sizeof(struct list_head),
1956 GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001957 if (!clp->cl_ownerstr_hashtbl)
1958 goto err_no_hashtbl;
1959 for (i = 0; i < OWNER_HASH_SIZE; i++)
1960 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
Trond Myklebust5694c932014-04-18 14:43:56 -04001961 INIT_LIST_HEAD(&clp->cl_sessions);
1962 idr_init(&clp->cl_stateids);
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04001963 atomic_set(&clp->cl_rpc_users, 0);
Trond Myklebust5694c932014-04-18 14:43:56 -04001964 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1965 INIT_LIST_HEAD(&clp->cl_idhash);
1966 INIT_LIST_HEAD(&clp->cl_openowners);
1967 INIT_LIST_HEAD(&clp->cl_delegations);
1968 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04001969 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001970#ifdef CONFIG_NFSD_PNFS
1971 INIT_LIST_HEAD(&clp->cl_lo_states);
1972#endif
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001973 INIT_LIST_HEAD(&clp->async_copies);
1974 spin_lock_init(&clp->async_lock);
Trond Myklebust5694c932014-04-18 14:43:56 -04001975 spin_lock_init(&clp->cl_lock);
1976 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001978err_no_hashtbl:
1979 kfree(clp->cl_name.data);
1980err_no_name:
Jeff Layton9258a2d2018-03-16 09:47:22 -04001981 kmem_cache_free(client_slab, clp);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001982 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
1984
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001985static void __free_client(struct kref *k)
1986{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04001987 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
1988 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001989
1990 free_svc_cred(&clp->cl_cred);
1991 kfree(clp->cl_ownerstr_hashtbl);
1992 kfree(clp->cl_name.data);
J. Bruce Fields79123442019-06-05 12:42:05 -04001993 kfree(clp->cl_nii_domain.data);
1994 kfree(clp->cl_nii_name.data);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001995 idr_destroy(&clp->cl_stateids);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001996 kmem_cache_free(client_slab, clp);
1997}
1998
YueHaibing297e57a2019-07-08 15:29:33 +08001999static void drop_client(struct nfs4_client *clp)
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002000{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002001 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002002}
2003
Trond Myklebust4dd86e152014-04-18 14:43:58 -04002004static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005free_client(struct nfs4_client *clp)
2006{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002007 while (!list_empty(&clp->cl_sessions)) {
2008 struct nfsd4_session *ses;
2009 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2010 se_perclnt);
2011 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002012 WARN_ON_ONCE(atomic_read(&ses->se_ref));
2013 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002014 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04002015 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002016 if (clp->cl_nfsd_dentry) {
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002017 nfsd_client_rmdir(clp->cl_nfsd_dentry);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002018 clp->cl_nfsd_dentry = NULL;
2019 wake_up_all(&expiry_wq);
2020 }
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002021 drop_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022}
2023
Benny Halevy84d38ac2010-05-12 00:13:16 +03002024/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04002025static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03002026unhash_client_locked(struct nfs4_client *clp)
2027{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002028 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002029 struct nfsd4_session *ses;
2030
Trond Myklebust0a880a22014-07-30 08:27:10 -04002031 lockdep_assert_held(&nn->client_lock);
2032
Trond Myklebust4beb3452014-07-30 08:27:02 -04002033 /* Mark the client as expired! */
2034 clp->cl_time = 0;
2035 /* Make it invisible */
2036 if (!list_empty(&clp->cl_idhash)) {
2037 list_del_init(&clp->cl_idhash);
2038 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2039 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2040 else
2041 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2042 }
2043 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04002044 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002045 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2046 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04002047 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03002048}
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04002051unhash_client(struct nfs4_client *clp)
2052{
2053 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2054
2055 spin_lock(&nn->client_lock);
2056 unhash_client_locked(clp);
2057 spin_unlock(&nn->client_lock);
2058}
2059
Jeff Layton97403d92014-07-30 08:27:12 -04002060static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2061{
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04002062 if (atomic_read(&clp->cl_rpc_users))
Jeff Layton97403d92014-07-30 08:27:12 -04002063 return nfserr_jukebox;
2064 unhash_client_locked(clp);
2065 return nfs_ok;
2066}
2067
Trond Myklebust4beb3452014-07-30 08:27:02 -04002068static void
2069__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
Jeff Layton68ef3bc2018-03-16 11:32:02 -04002071 int i;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002072 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 struct list_head reaplist;
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04002077 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07002078 while (!list_empty(&clp->cl_delegations)) {
2079 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04002080 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04002081 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 }
Benny Halevycdc975052014-05-30 09:09:30 -04002083 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 while (!list_empty(&reaplist)) {
2085 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04002086 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05002087 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04002089 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05002090 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04002091 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04002092 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02002093 }
NeilBrownea1da632005-06-23 22:04:17 -07002094 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002095 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04002096 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002097 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
Jeff Layton68ef3bc2018-03-16 11:32:02 -04002099 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2100 struct nfs4_stateowner *so, *tmp;
2101
2102 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2103 so_strhash) {
2104 /* Should be no openowners at this point */
2105 WARN_ON_ONCE(so->so_is_open_owner);
2106 remove_blocked_locks(lockowner(so));
2107 }
2108 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002109 nfsd4_return_all_client_layouts(clp);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002110 nfsd4_shutdown_copy(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04002111 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002112 if (clp->cl_cb_conn.cb_xprt)
2113 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002114 free_client(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002115 wake_up_all(&expiry_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116}
2117
Trond Myklebust4beb3452014-07-30 08:27:02 -04002118static void
2119destroy_client(struct nfs4_client *clp)
2120{
2121 unhash_client(clp);
2122 __destroy_client(clp);
2123}
2124
Scott Mayhew362063a2019-03-26 18:06:28 -04002125static void inc_reclaim_complete(struct nfs4_client *clp)
2126{
2127 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2128
2129 if (!nn->track_reclaim_completes)
2130 return;
2131 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2132 return;
2133 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2134 nn->reclaim_str_hashtbl_size) {
2135 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2136 clp->net->ns.inum);
2137 nfsd4_end_grace(nn);
2138 }
2139}
2140
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002141static void expire_client(struct nfs4_client *clp)
2142{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002143 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002144 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04002145 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002146}
2147
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002148static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2149{
2150 memcpy(target->cl_verifier.data, source->data,
2151 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152}
2153
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002154static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2155{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2157 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2158}
2159
J. Bruce Fields50043852015-11-20 15:58:37 -05002160static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2161{
NeilBrown2f10fdc2017-03-23 16:57:36 +08002162 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2163 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2164 GFP_KERNEL);
Chuck Lever9abdda52018-08-16 12:05:59 -04002165 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2166 if ((source->cr_principal && !target->cr_principal) ||
2167 (source->cr_raw_principal && !target->cr_raw_principal) ||
2168 (source->cr_targ_princ && !target->cr_targ_princ))
NeilBrown2f10fdc2017-03-23 16:57:36 +08002169 return -ENOMEM;
J. Bruce Fields50043852015-11-20 15:58:37 -05002170
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04002171 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 target->cr_uid = source->cr_uid;
2173 target->cr_gid = source->cr_gid;
2174 target->cr_group_info = source->cr_group_info;
2175 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04002176 target->cr_gss_mech = source->cr_gss_mech;
2177 if (source->cr_gss_mech)
2178 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002179 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002182static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002183compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2184{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002185 if (o1->len < o2->len)
2186 return -1;
2187 if (o1->len > o2->len)
2188 return 1;
2189 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002190}
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002193same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2194{
2195 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196}
2197
2198static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002199same_clid(clientid_t *cl1, clientid_t *cl2)
2200{
2201 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202}
2203
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002204static bool groups_equal(struct group_info *g1, struct group_info *g2)
2205{
2206 int i;
2207
2208 if (g1->ngroups != g2->ngroups)
2209 return false;
2210 for (i=0; i<g1->ngroups; i++)
Alexey Dobriyan81243ea2016-10-07 17:03:12 -07002211 if (!gid_eq(g1->gid[i], g2->gid[i]))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002212 return false;
2213 return true;
2214}
2215
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002216/*
2217 * RFC 3530 language requires clid_inuse be returned when the
2218 * "principal" associated with a requests differs from that previously
2219 * used. We use uid, gid's, and gss principal string as our best
2220 * approximation. We also don't want to allow non-gss use of a client
2221 * established using gss: in theory cr_principal should catch that
2222 * change, but in practice cr_principal can be null even in the gss case
2223 * since gssd doesn't always pass down a principal string.
2224 */
2225static bool is_gss_cred(struct svc_cred *cr)
2226{
2227 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2228 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2229}
2230
2231
Vivek Trivedi5559b502012-07-24 21:18:20 +05302232static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002233same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2234{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002235 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002236 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2237 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002238 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2239 return false;
Chuck Lever9abdda52018-08-16 12:05:59 -04002240 /* XXX: check that cr_targ_princ fields match ? */
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002241 if (cr1->cr_principal == cr2->cr_principal)
2242 return true;
2243 if (!cr1->cr_principal || !cr2->cr_principal)
2244 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05302245 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246}
2247
J. Bruce Fields57266a62013-04-13 14:27:29 -04002248static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2249{
2250 struct svc_cred *cr = &rqstp->rq_cred;
2251 u32 service;
2252
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04002253 if (!cr->cr_gss_mech)
2254 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002255 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2256 return service == RPC_GSS_SVC_INTEGRITY ||
2257 service == RPC_GSS_SVC_PRIVACY;
2258}
2259
Andrew Elblededeb132016-06-15 12:52:08 -04002260bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
J. Bruce Fields57266a62013-04-13 14:27:29 -04002261{
2262 struct svc_cred *cr = &rqstp->rq_cred;
2263
2264 if (!cl->cl_mach_cred)
2265 return true;
2266 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2267 return false;
2268 if (!svc_rqst_integrity_protected(rqstp))
2269 return false;
J. Bruce Fields414ca012015-11-20 10:48:02 -05002270 if (cl->cl_cred.cr_raw_principal)
2271 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2272 cr->cr_raw_principal);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002273 if (!cr->cr_principal)
2274 return false;
2275 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2276}
2277
Jeff Layton294ac322014-07-30 08:27:15 -04002278static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05002279{
Chuck Leverab4684d2012-03-02 17:13:50 -05002280 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Jeff Laytonf4199922014-06-17 07:44:11 -04002282 /*
2283 * This is opaque to client, so no need to byte-swap. Use
2284 * __force to keep sparse happy
2285 */
Arnd Bergmann9104ae42019-11-04 16:45:30 +01002286 verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08002287 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05002288 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289}
2290
Jeff Layton294ac322014-07-30 08:27:15 -04002291static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2292{
Arnd Bergmann9cc76802019-10-31 15:53:13 +01002293 clp->cl_clientid.cl_boot = (u32)nn->boot_time;
Jeff Layton294ac322014-07-30 08:27:15 -04002294 clp->cl_clientid.cl_id = nn->clientid_counter++;
2295 gen_confirm(clp, nn);
2296}
2297
Jeff Layton4770d722014-07-29 21:34:10 -04002298static struct nfs4_stid *
2299find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04002300{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002301 struct nfs4_stid *ret;
2302
2303 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2304 if (!ret || !ret->sc_type)
2305 return NULL;
2306 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04002307}
2308
Jeff Layton4770d722014-07-29 21:34:10 -04002309static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04002310find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002311{
2312 struct nfs4_stid *s;
2313
Jeff Layton4770d722014-07-29 21:34:10 -04002314 spin_lock(&cl->cl_lock);
2315 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002316 if (s != NULL) {
2317 if (typemask & s->sc_type)
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03002318 refcount_inc(&s->sc_count);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002319 else
2320 s = NULL;
2321 }
Jeff Layton4770d722014-07-29 21:34:10 -04002322 spin_unlock(&cl->cl_lock);
2323 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04002324}
2325
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002326static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2327{
2328 struct nfsdfs_client *nc;
2329 nc = get_nfsdfs_client(inode);
2330 if (!nc)
2331 return NULL;
2332 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2333}
2334
J. Bruce Fields169319f2019-06-19 12:39:46 -04002335static void seq_quote_mem(struct seq_file *m, char *data, int len)
2336{
2337 seq_printf(m, "\"");
2338 seq_escape_mem_ascii(m, data, len);
2339 seq_printf(m, "\"");
2340}
2341
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002342static int client_info_show(struct seq_file *m, void *v)
2343{
2344 struct inode *inode = m->private;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002345 struct nfs4_client *clp;
2346 u64 clid;
2347
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002348 clp = get_nfsdfs_clp(inode);
2349 if (!clp)
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002350 return -ENXIO;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002351 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2352 seq_printf(m, "clientid: 0x%llx\n", clid);
J. Bruce Fields169319f2019-06-19 12:39:46 -04002353 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2354 seq_printf(m, "name: ");
2355 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2356 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
J. Bruce Fields79123442019-06-05 12:42:05 -04002357 if (clp->cl_nii_domain.data) {
2358 seq_printf(m, "Implementation domain: ");
2359 seq_quote_mem(m, clp->cl_nii_domain.data,
2360 clp->cl_nii_domain.len);
2361 seq_printf(m, "\nImplementation name: ");
2362 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
Arnd Bergmanne29f4702019-10-31 14:52:43 +01002363 seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
J. Bruce Fields79123442019-06-05 12:42:05 -04002364 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2365 }
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002366 drop_client(clp);
2367
2368 return 0;
2369}
2370
2371static int client_info_open(struct inode *inode, struct file *file)
2372{
2373 return single_open(file, client_info_show, inode);
2374}
2375
2376static const struct file_operations client_info_fops = {
2377 .open = client_info_open,
2378 .read = seq_read,
2379 .llseek = seq_lseek,
2380 .release = single_release,
2381};
2382
J. Bruce Fields78599c42019-04-22 15:26:23 -04002383static void *states_start(struct seq_file *s, loff_t *pos)
2384 __acquires(&clp->cl_lock)
2385{
2386 struct nfs4_client *clp = s->private;
2387 unsigned long id = *pos;
2388 void *ret;
2389
2390 spin_lock(&clp->cl_lock);
2391 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2392 *pos = id;
2393 return ret;
2394}
2395
2396static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2397{
2398 struct nfs4_client *clp = s->private;
2399 unsigned long id = *pos;
2400 void *ret;
2401
2402 id = *pos;
2403 id++;
2404 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2405 *pos = id;
2406 return ret;
2407}
2408
2409static void states_stop(struct seq_file *s, void *v)
2410 __releases(&clp->cl_lock)
2411{
2412 struct nfs4_client *clp = s->private;
2413
2414 spin_unlock(&clp->cl_lock);
2415}
2416
Achilles Gaikwad580da462020-04-20 18:20:31 +05302417static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
2418{
2419 seq_printf(s, "filename: \"%pD2\"", f->nf_file);
2420}
2421
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002422static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002423{
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002424 struct inode *inode = f->nf_inode;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002425
2426 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2427 MAJOR(inode->i_sb->s_dev),
2428 MINOR(inode->i_sb->s_dev),
2429 inode->i_ino);
2430}
2431
2432static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2433{
2434 seq_printf(s, "owner: ");
2435 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2436}
2437
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002438static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
2439{
J. Bruce Fieldsee590d22020-04-13 22:03:06 -04002440 seq_printf(s, "0x%.8x", stid->si_generation);
2441 seq_printf(s, "%12phN", &stid->si_opaque);
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002442}
2443
J. Bruce Fields78599c42019-04-22 15:26:23 -04002444static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2445{
2446 struct nfs4_ol_stateid *ols;
2447 struct nfs4_file *nf;
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002448 struct nfsd_file *file;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002449 struct nfs4_stateowner *oo;
2450 unsigned int access, deny;
2451
2452 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2453 return 0; /* XXX: or SEQ_SKIP? */
2454 ols = openlockstateid(st);
2455 oo = ols->st_stateowner;
2456 nf = st->sc_file;
2457 file = find_any_file(nf);
J. Bruce Fields9affa432020-07-15 13:31:36 -04002458 if (!file)
2459 return 0;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002460
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002461 seq_printf(s, "- ");
2462 nfs4_show_stateid(s, &st->sc_stateid);
2463 seq_printf(s, ": { type: open, ");
J. Bruce Fields78599c42019-04-22 15:26:23 -04002464
2465 access = bmap_to_share_mode(ols->st_access_bmap);
2466 deny = bmap_to_share_mode(ols->st_deny_bmap);
2467
J. Bruce Fieldsc4b77ed2019-10-03 12:33:08 -04002468 seq_printf(s, "access: %s%s, ",
J. Bruce Fields78599c42019-04-22 15:26:23 -04002469 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2470 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
J. Bruce Fieldsc4b77ed2019-10-03 12:33:08 -04002471 seq_printf(s, "deny: %s%s, ",
J. Bruce Fields78599c42019-04-22 15:26:23 -04002472 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2473 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2474
2475 nfs4_show_superblock(s, file);
2476 seq_printf(s, ", ");
Achilles Gaikwad580da462020-04-20 18:20:31 +05302477 nfs4_show_fname(s, file);
2478 seq_printf(s, ", ");
J. Bruce Fields78599c42019-04-22 15:26:23 -04002479 nfs4_show_owner(s, oo);
2480 seq_printf(s, " }\n");
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002481 nfsd_file_put(file);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002482
2483 return 0;
2484}
2485
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002486static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2487{
2488 struct nfs4_ol_stateid *ols;
2489 struct nfs4_file *nf;
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002490 struct nfsd_file *file;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002491 struct nfs4_stateowner *oo;
2492
2493 ols = openlockstateid(st);
2494 oo = ols->st_stateowner;
2495 nf = st->sc_file;
2496 file = find_any_file(nf);
J. Bruce Fields9affa432020-07-15 13:31:36 -04002497 if (!file)
2498 return 0;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002499
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002500 seq_printf(s, "- ");
2501 nfs4_show_stateid(s, &st->sc_stateid);
2502 seq_printf(s, ": { type: lock, ");
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002503
2504 /*
2505 * Note: a lock stateid isn't really the same thing as a lock,
2506 * it's the locking state held by one owner on a file, and there
2507 * may be multiple (or no) lock ranges associated with it.
2508 * (Same for the matter is true of open stateids.)
2509 */
2510
2511 nfs4_show_superblock(s, file);
2512 /* XXX: open stateid? */
2513 seq_printf(s, ", ");
Achilles Gaikwad580da462020-04-20 18:20:31 +05302514 nfs4_show_fname(s, file);
2515 seq_printf(s, ", ");
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002516 nfs4_show_owner(s, oo);
2517 seq_printf(s, " }\n");
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002518 nfsd_file_put(file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002519
2520 return 0;
2521}
2522
2523static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2524{
2525 struct nfs4_delegation *ds;
2526 struct nfs4_file *nf;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04002527 struct nfsd_file *file;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002528
2529 ds = delegstateid(st);
2530 nf = st->sc_file;
J. Bruce Fields9affa432020-07-15 13:31:36 -04002531 file = find_deleg_file(nf);
2532 if (!file)
2533 return 0;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002534
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002535 seq_printf(s, "- ");
2536 nfs4_show_stateid(s, &st->sc_stateid);
2537 seq_printf(s, ": { type: deleg, ");
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002538
2539 /* Kinda dead code as long as we only support read delegs: */
2540 seq_printf(s, "access: %s, ",
2541 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2542
2543 /* XXX: lease time, whether it's being recalled. */
2544
2545 nfs4_show_superblock(s, file);
Achilles Gaikwad580da462020-04-20 18:20:31 +05302546 seq_printf(s, ", ");
2547 nfs4_show_fname(s, file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002548 seq_printf(s, " }\n");
J. Bruce Fields9affa432020-07-15 13:31:36 -04002549 nfsd_file_put(file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002550
2551 return 0;
2552}
2553
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002554static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2555{
2556 struct nfs4_layout_stateid *ls;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04002557 struct nfsd_file *file;
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002558
2559 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2560 file = ls->ls_file;
2561
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002562 seq_printf(s, "- ");
2563 nfs4_show_stateid(s, &st->sc_stateid);
2564 seq_printf(s, ": { type: layout, ");
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002565
2566 /* XXX: What else would be useful? */
2567
2568 nfs4_show_superblock(s, file);
Achilles Gaikwad580da462020-04-20 18:20:31 +05302569 seq_printf(s, ", ");
2570 nfs4_show_fname(s, file);
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002571 seq_printf(s, " }\n");
2572
2573 return 0;
2574}
2575
J. Bruce Fields78599c42019-04-22 15:26:23 -04002576static int states_show(struct seq_file *s, void *v)
2577{
2578 struct nfs4_stid *st = v;
2579
2580 switch (st->sc_type) {
2581 case NFS4_OPEN_STID:
2582 return nfs4_show_open(s, st);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002583 case NFS4_LOCK_STID:
2584 return nfs4_show_lock(s, st);
2585 case NFS4_DELEG_STID:
2586 return nfs4_show_deleg(s, st);
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002587 case NFS4_LAYOUT_STID:
2588 return nfs4_show_layout(s, st);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002589 default:
2590 return 0; /* XXX: or SEQ_SKIP? */
2591 }
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002592 /* XXX: copy stateids? */
J. Bruce Fields78599c42019-04-22 15:26:23 -04002593}
2594
2595static struct seq_operations states_seq_ops = {
2596 .start = states_start,
2597 .next = states_next,
2598 .stop = states_stop,
2599 .show = states_show
2600};
2601
2602static int client_states_open(struct inode *inode, struct file *file)
2603{
J. Bruce Fields78599c42019-04-22 15:26:23 -04002604 struct seq_file *s;
2605 struct nfs4_client *clp;
2606 int ret;
2607
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002608 clp = get_nfsdfs_clp(inode);
2609 if (!clp)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002610 return -ENXIO;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002611
2612 ret = seq_open(file, &states_seq_ops);
2613 if (ret)
2614 return ret;
2615 s = file->private_data;
2616 s->private = clp;
2617 return 0;
2618}
2619
2620static int client_opens_release(struct inode *inode, struct file *file)
2621{
2622 struct seq_file *m = file->private_data;
2623 struct nfs4_client *clp = m->private;
2624
2625 /* XXX: alternatively, we could get/drop in seq start/stop */
2626 drop_client(clp);
2627 return 0;
2628}
2629
2630static const struct file_operations client_states_fops = {
2631 .open = client_states_open,
2632 .read = seq_read,
2633 .llseek = seq_lseek,
2634 .release = client_opens_release,
2635};
2636
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002637/*
2638 * Normally we refuse to destroy clients that are in use, but here the
2639 * administrator is telling us to just do it. We also want to wait
2640 * so the caller has a guarantee that the client's locks are gone by
2641 * the time the write returns:
2642 */
YueHaibing297e57a2019-07-08 15:29:33 +08002643static void force_expire_client(struct nfs4_client *clp)
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002644{
2645 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2646 bool already_expired;
2647
2648 spin_lock(&clp->cl_lock);
2649 clp->cl_time = 0;
2650 spin_unlock(&clp->cl_lock);
2651
2652 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2653 spin_lock(&nn->client_lock);
2654 already_expired = list_empty(&clp->cl_lru);
2655 if (!already_expired)
2656 unhash_client_locked(clp);
2657 spin_unlock(&nn->client_lock);
2658
2659 if (!already_expired)
2660 expire_client(clp);
2661 else
2662 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2663}
2664
2665static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2666 size_t size, loff_t *pos)
2667{
2668 char *data;
2669 struct nfs4_client *clp;
2670
2671 data = simple_transaction_get(file, buf, size);
2672 if (IS_ERR(data))
2673 return PTR_ERR(data);
2674 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2675 return -EINVAL;
2676 clp = get_nfsdfs_clp(file_inode(file));
2677 if (!clp)
2678 return -ENXIO;
2679 force_expire_client(clp);
2680 drop_client(clp);
2681 return 7;
2682}
2683
2684static const struct file_operations client_ctl_fops = {
2685 .write = client_ctl_write,
2686 .release = simple_transaction_release,
2687};
2688
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002689static const struct tree_descr client_files[] = {
2690 [0] = {"info", &client_info_fops, S_IRUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002691 [1] = {"states", &client_states_fops, S_IRUSR},
Petr Vorel6cbfad52020-03-13 13:39:57 +01002692 [2] = {"ctl", &client_ctl_fops, S_IWUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002693 [3] = {""},
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002694};
2695
Jeff Layton2216d442012-11-12 15:00:57 -05002696static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002697 struct svc_rqst *rqstp, nfs4_verifier *verf)
2698{
2699 struct nfs4_client *clp;
2700 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002701 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002702 struct net *net = SVC_NET(rqstp);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002703 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002704
2705 clp = alloc_client(name);
2706 if (clp == NULL)
2707 return NULL;
2708
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002709 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2710 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002711 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002712 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002713 }
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002714 gen_clid(clp, nn);
2715 kref_init(&clp->cl_nfsdfs.cl_ref);
Christoph Hellwig0162ac22014-09-24 12:19:19 +02002716 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01002717 clp->cl_time = ktime_get_boottime_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002718 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002719 copy_verf(clp, verf);
J. Bruce Fields3bade242019-05-14 21:38:11 -04002720 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04002721 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002722 clp->net = net;
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002723 clp->cl_nfsd_dentry = nfsd_client_mkdir(nn, &clp->cl_nfsdfs,
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002724 clp->cl_clientid.cl_id - nn->clientid_base,
2725 client_files);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002726 if (!clp->cl_nfsd_dentry) {
2727 free_client(clp);
2728 return NULL;
2729 }
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002730 return clp;
2731}
2732
NeilBrownfd39ca92005-06-23 22:04:03 -07002733static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002734add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2735{
2736 struct rb_node **new = &(root->rb_node), *parent = NULL;
2737 struct nfs4_client *clp;
2738
2739 while (*new) {
2740 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2741 parent = *new;
2742
2743 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2744 new = &((*new)->rb_left);
2745 else
2746 new = &((*new)->rb_right);
2747 }
2748
2749 rb_link_node(&new_clp->cl_namenode, parent, new);
2750 rb_insert_color(&new_clp->cl_namenode, root);
2751}
2752
2753static struct nfs4_client *
2754find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2755{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002756 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002757 struct rb_node *node = root->rb_node;
2758 struct nfs4_client *clp;
2759
2760 while (node) {
2761 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2762 cmp = compare_blob(&clp->cl_name, name);
2763 if (cmp > 0)
2764 node = node->rb_left;
2765 else if (cmp < 0)
2766 node = node->rb_right;
2767 else
2768 return clp;
2769 }
2770 return NULL;
2771}
2772
2773static void
2774add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775{
2776 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002777 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
Trond Myklebust0a880a22014-07-30 08:27:10 -04002779 lockdep_assert_held(&nn->client_lock);
2780
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002781 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002782 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002784 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002785 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786}
2787
NeilBrownfd39ca92005-06-23 22:04:03 -07002788static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789move_to_confirmed(struct nfs4_client *clp)
2790{
2791 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002792 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
Trond Myklebust0a880a22014-07-30 08:27:10 -04002794 lockdep_assert_held(&nn->client_lock);
2795
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002797 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002798 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002799 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002800 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002801 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802}
2803
2804static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002805find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806{
2807 struct nfs4_client *clp;
2808 unsigned int idhashval = clientid_hashval(clid->cl_id);
2809
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002810 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002811 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002812 if ((bool)clp->cl_minorversion != sessions)
2813 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002814 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 }
2818 return NULL;
2819}
2820
2821static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002822find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2823{
2824 struct list_head *tbl = nn->conf_id_hashtbl;
2825
Trond Myklebust0a880a22014-07-30 08:27:10 -04002826 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002827 return find_client_in_id_table(tbl, clid, sessions);
2828}
2829
2830static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002831find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002833 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
Trond Myklebust0a880a22014-07-30 08:27:10 -04002835 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002836 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837}
2838
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002839static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002840{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002841 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002842}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002843
NeilBrown28ce6052005-06-23 22:03:56 -07002844static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002845find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002846{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002847 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002848 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002849}
2850
2851static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002852find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002853{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002854 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002855 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002856}
2857
NeilBrownfd39ca92005-06-23 22:04:03 -07002858static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002859gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002861 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002862 struct sockaddr *sa = svc_addr(rqstp);
2863 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002864 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Jeff Layton7077ecb2009-08-14 12:57:58 -04002866 /* Currently, we only support tcp and tcp6 for the callback channel */
2867 if (se->se_callback_netid_len == 3 &&
2868 !memcmp(se->se_callback_netid_val, "tcp", 3))
2869 expected_family = AF_INET;
2870 else if (se->se_callback_netid_len == 4 &&
2871 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2872 expected_family = AF_INET6;
2873 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 goto out_err;
2875
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002876 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002877 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002878 (struct sockaddr *)&conn->cb_addr,
2879 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002880
J. Bruce Fields07263f12010-05-31 19:09:40 -04002881 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002883
J. Bruce Fields07263f12010-05-31 19:09:40 -04002884 if (conn->cb_addr.ss_family == AF_INET6)
2885 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002886
J. Bruce Fields07263f12010-05-31 19:09:40 -04002887 conn->cb_prog = se->se_callback_prog;
2888 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002889 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Chuck Lever1eace0d2020-04-05 14:15:29 -04002890 trace_nfsd_cb_args(clp, conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 return;
2892out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002893 conn->cb_addr.ss_family = AF_UNSPEC;
2894 conn->cb_addrlen = 0;
Chuck Lever1eace0d2020-04-05 14:15:29 -04002895 trace_nfsd_cb_nodelegs(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 return;
2897}
2898
Andy Adamson074fe892009-04-03 08:28:15 +03002899/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002900 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002901 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002902static void
Andy Adamson074fe892009-04-03 08:28:15 +03002903nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2904{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002905 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002906 struct nfsd4_slot *slot = resp->cstate.slot;
2907 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002908
Andy Adamson557ce262009-08-28 08:45:04 -04002909 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002910
J. Bruce Fields085def32017-10-18 16:17:18 -04002911 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamson557ce262009-08-28 08:45:04 -04002912 slot->sl_opcnt = resp->opcnt;
2913 slot->sl_status = resp->cstate.status;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04002914 free_svc_cred(&slot->sl_cred);
2915 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
Andy Adamsonbf864a32009-04-03 08:28:35 +03002916
J. Bruce Fields085def32017-10-18 16:17:18 -04002917 if (!nfsd4_cache_this(resp)) {
2918 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002919 return;
2920 }
J. Bruce Fields085def32017-10-18 16:17:18 -04002921 slot->sl_flags |= NFSD4_SLOT_CACHED;
2922
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002923 base = resp->cstate.data_offset;
2924 slot->sl_datalen = buf->len - base;
2925 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Dan Carpenterd3f03402015-11-21 13:28:50 +03002926 WARN(1, "%s: sessions DRC could not cache compound\n",
2927 __func__);
Andy Adamson557ce262009-08-28 08:45:04 -04002928 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002929}
2930
2931/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002932 * Encode the replay sequence operation from the slot values.
2933 * If cachethis is FALSE encode the uncached rep error on the next
2934 * operation which sets resp->p and increments resp->opcnt for
2935 * nfs4svc_encode_compoundres.
2936 *
Andy Adamson074fe892009-04-03 08:28:15 +03002937 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002938static __be32
2939nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2940 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002941{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002942 struct nfsd4_op *op;
2943 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002944
Andy Adamsonabfabf82009-07-23 19:02:18 -04002945 /* Encode the replayed sequence operation */
2946 op = &args->ops[resp->opcnt - 1];
2947 nfsd4_encode_operation(resp, op);
2948
J. Bruce Fields085def32017-10-18 16:17:18 -04002949 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2950 return op->status;
2951 if (args->opcnt == 1) {
2952 /*
2953 * The original operation wasn't a solo sequence--we
2954 * always cache those--so this retry must not match the
2955 * original:
2956 */
2957 op->status = nfserr_seq_false_retry;
2958 } else {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002959 op = &args->ops[resp->opcnt++];
2960 op->status = nfserr_retry_uncached_rep;
2961 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002962 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002963 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002964}
2965
2966/*
Andy Adamson557ce262009-08-28 08:45:04 -04002967 * The sequence operation is not cached because we can use the slot and
2968 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002969 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002970static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002971nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2972 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002973{
Andy Adamson557ce262009-08-28 08:45:04 -04002974 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002975 struct xdr_stream *xdr = &resp->xdr;
2976 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002977 __be32 status;
2978
Andy Adamson557ce262009-08-28 08:45:04 -04002979 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002980
Andy Adamsonabfabf82009-07-23 19:02:18 -04002981 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002982 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002983 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002984
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002985 p = xdr_reserve_space(xdr, slot->sl_datalen);
2986 if (!p) {
2987 WARN_ON_ONCE(1);
2988 return nfserr_serverfault;
2989 }
2990 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2991 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002992
Andy Adamson557ce262009-08-28 08:45:04 -04002993 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002994 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002995}
2996
Andy Adamson0733d212009-04-03 08:28:01 +03002997/*
2998 * Set the exchange_id flags returned by the server.
2999 */
3000static void
3001nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
3002{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003003#ifdef CONFIG_NFSD_PNFS
3004 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
3005#else
Andy Adamson0733d212009-04-03 08:28:01 +03003006 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003007#endif
Andy Adamson0733d212009-04-03 08:28:01 +03003008
3009 /* Referrals are supported, Migration is not. */
3010 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
3011
3012 /* set the wire flags to return to client. */
3013 clid->flags = new->cl_exchange_flags;
3014}
3015
J. Bruce Fields4eaea132015-10-15 16:11:01 -04003016static bool client_has_openowners(struct nfs4_client *clp)
3017{
3018 struct nfs4_openowner *oo;
3019
3020 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
3021 if (!list_empty(&oo->oo_owner.so_stateids))
3022 return true;
3023 }
3024 return false;
3025}
3026
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003027static bool client_has_state(struct nfs4_client *clp)
3028{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04003029 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08003030#ifdef CONFIG_NFSD_PNFS
3031 || !list_empty(&clp->cl_lo_states)
3032#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04003033 || !list_empty(&clp->cl_delegations)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04003034 || !list_empty(&clp->cl_sessions)
3035 || !list_empty(&clp->async_copies);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003036}
3037
J. Bruce Fields79123442019-06-05 12:42:05 -04003038static __be32 copy_impl_id(struct nfs4_client *clp,
3039 struct nfsd4_exchange_id *exid)
3040{
3041 if (!exid->nii_domain.data)
3042 return 0;
3043 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
3044 if (!clp->cl_nii_domain.data)
3045 return nfserr_jukebox;
3046 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
3047 if (!clp->cl_nii_name.data)
3048 return nfserr_jukebox;
Arnd Bergmanne29f4702019-10-31 14:52:43 +01003049 clp->cl_nii_time = exid->nii_time;
J. Bruce Fields79123442019-06-05 12:42:05 -04003050 return 0;
3051}
3052
Al Virob37ad282006-10-19 23:28:59 -07003053__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003054nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3055 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003056{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003057 struct nfsd4_exchange_id *exid = &u->exchange_id;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003058 struct nfs4_client *conf, *new;
3059 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003060 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04003061 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03003062 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04003063 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04003064 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003065 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03003066
Jeff Layton363168b2009-08-14 12:57:56 -04003067 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03003068 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Chuck Lever523ec6e2020-11-02 15:19:12 -05003069 "ip_addr=%s flags %x, spa_how %u\n",
Andy Adamson0733d212009-04-03 08:28:01 +03003070 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04003071 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03003072
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003073 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03003074 return nfserr_inval;
3075
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003076 new = create_client(exid->clname, rqstp, &verf);
3077 if (new == NULL)
3078 return nfserr_jukebox;
J. Bruce Fields79123442019-06-05 12:42:05 -04003079 status = copy_impl_id(new, exid);
3080 if (status)
3081 goto out_nolock;
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003082
Andy Adamson0733d212009-04-03 08:28:01 +03003083 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003084 case SP4_MACH_CRED:
Andrew Elbleed941642016-06-15 12:52:09 -04003085 exid->spo_must_enforce[0] = 0;
3086 exid->spo_must_enforce[1] = (
3087 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3088 1 << (OP_EXCHANGE_ID - 32) |
3089 1 << (OP_CREATE_SESSION - 32) |
3090 1 << (OP_DESTROY_SESSION - 32) |
3091 1 << (OP_DESTROY_CLIENTID - 32));
3092
3093 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3094 1 << (OP_OPEN_DOWNGRADE) |
3095 1 << (OP_LOCKU) |
3096 1 << (OP_DELEGRETURN));
3097
3098 exid->spo_must_allow[1] &= (
3099 1 << (OP_TEST_STATEID - 32) |
3100 1 << (OP_FREE_STATEID - 32));
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003101 if (!svc_rqst_integrity_protected(rqstp)) {
3102 status = nfserr_inval;
3103 goto out_nolock;
3104 }
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05003105 /*
3106 * Sometimes userspace doesn't give us a principal.
3107 * Which is a bug, really. Anyway, we can't enforce
3108 * MACH_CRED in that case, better to give up now:
3109 */
J. Bruce Fields414ca012015-11-20 10:48:02 -05003110 if (!new->cl_cred.cr_principal &&
3111 !new->cl_cred.cr_raw_principal) {
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05003112 status = nfserr_serverfault;
3113 goto out_nolock;
3114 }
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003115 new->cl_mach_cred = true;
Andy Adamson0733d212009-04-03 08:28:01 +03003116 case SP4_NONE:
3117 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003118 default: /* checked by xdr code */
3119 WARN_ON_ONCE(1);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003120 fallthrough;
Andy Adamson0733d212009-04-03 08:28:01 +03003121 case SP4_SSV:
Kinglong Mee8edf4b02016-02-26 22:36:42 +08003122 status = nfserr_encr_alg_unsupp;
3123 goto out_nolock;
Andy Adamson0733d212009-04-03 08:28:01 +03003124 }
3125
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003126 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003127 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003128 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03003129 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04003130 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3131 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3132
J. Bruce Fields136e6582012-05-12 20:37:23 -04003133 if (update) {
3134 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003135 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03003136 goto out;
3137 }
Andrew Elblededeb132016-06-15 12:52:08 -04003138 if (!nfsd4_mach_creds_match(conf, rqstp)) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003139 status = nfserr_wrong_cred;
3140 goto out;
3141 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003142 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03003143 status = nfserr_perm;
3144 goto out;
3145 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003146 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03003147 status = nfserr_not_same;
3148 goto out;
3149 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003150 /* case 6 */
3151 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003152 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03003153 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003154 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003155 if (client_has_state(conf)) {
3156 status = nfserr_clid_inuse;
3157 goto out;
3158 }
Andy Adamson0733d212009-04-03 08:28:01 +03003159 goto out_new;
3160 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003161 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04003162 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003163 goto out_copy;
3164 }
3165 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003166 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003167 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003168 }
3169
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003170 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003171 status = nfserr_noent;
3172 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03003173 }
3174
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003175 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003176 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003177 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03003178
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003179 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03003180out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04003181 if (conf) {
3182 status = mark_client_expired_locked(conf);
3183 if (status)
3184 goto out;
3185 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04003186 new->cl_minorversion = cstate->minorversion;
Andrew Elbleed941642016-06-15 12:52:09 -04003187 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3188 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
Andy Adamson0733d212009-04-03 08:28:01 +03003189
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003190 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003191 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03003192out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003193 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3194 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03003195
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003196 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3197 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03003198
3199 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003200 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03003201 status = nfs_ok;
3202
3203out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003204 spin_unlock(&nn->client_lock);
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003205out_nolock:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003206 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003207 expire_client(new);
3208 if (unconf)
3209 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03003210 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003211}
3212
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003213static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04003214check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003215{
Andy Adamson88e588d2009-07-23 19:02:15 -04003216 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3217 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003218
3219 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04003220 if (slot_inuse) {
3221 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003222 return nfserr_jukebox;
3223 else
3224 return nfserr_seq_misordered;
3225 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05003226 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04003227 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03003228 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04003229 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003230 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003231 return nfserr_seq_misordered;
3232}
3233
Andy Adamson49557cc2009-07-23 19:02:16 -04003234/*
3235 * Cache the create session result into the create session single DRC
3236 * slot cache by saving the xdr structure. sl_seqid has been set.
3237 * Do this for solo or embedded create session operations.
3238 */
3239static void
3240nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003241 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04003242{
3243 slot->sl_status = nfserr;
3244 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3245}
3246
3247static __be32
3248nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3249 struct nfsd4_clid_slot *slot)
3250{
3251 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3252 return slot->sl_status;
3253}
3254
Mi Jinlong1b74c252011-07-14 14:50:17 +08003255#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3256 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3257 1 + /* MIN tag is length with zero, only length */ \
3258 3 + /* version, opcount, opcode */ \
3259 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3260 /* seqid, slotID, slotID, cache */ \
3261 4 ) * sizeof(__be32))
3262
3263#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3264 2 + /* verifier: AUTH_NULL, length 0 */\
3265 1 + /* status */ \
3266 1 + /* MIN tag is length with zero, only length */ \
3267 3 + /* opcount, opcode, opstatus*/ \
3268 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3269 /* seqid, slotID, slotID, slotID, status */ \
3270 5 ) * sizeof(__be32))
3271
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003272static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08003273{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003274 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3275
J. Bruce Fields373cd402013-04-08 15:42:12 -04003276 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3277 return nfserr_toosmall;
3278 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3279 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003280 ca->headerpadsz = 0;
3281 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3282 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3283 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3284 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3285 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3286 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3287 /*
3288 * Note decreasing slot size below client's request may make it
3289 * difficult for client to function correctly, whereas
3290 * decreasing the number of slots will (just?) affect
3291 * performance. When short on memory we therefore prefer to
3292 * decrease number of slots instead of their size. Clients that
3293 * request larger slots than they need will get poor results:
NeilBrown7f49fd5d2019-09-20 16:33:16 +10003294 * Note that we always allow at least one slot, because our
3295 * accounting is soft and provides no guarantees either way.
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003296 */
NeilBrown2030ca52019-09-20 16:36:45 +10003297 ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003298
J. Bruce Fields373cd402013-04-08 15:42:12 -04003299 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08003300}
3301
Chuck Lever45006322016-03-01 13:06:02 -05003302/*
3303 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3304 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3305 */
3306#define RPC_MAX_HEADER_WITH_AUTH_SYS \
3307 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3308
3309#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3310 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3311
Kinglong Mee8a891632013-12-23 18:11:02 +08003312#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003313 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003314#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003315 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3316 sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003317
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003318static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3319{
3320 ca->headerpadsz = 0;
3321
Kinglong Mee8a891632013-12-23 18:11:02 +08003322 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003323 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08003324 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003325 return nfserr_toosmall;
3326 ca->maxresp_cached = 0;
3327 if (ca->maxops < 2)
3328 return nfserr_toosmall;
3329
3330 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003331}
3332
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003333static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3334{
3335 switch (cbs->flavor) {
3336 case RPC_AUTH_NULL:
3337 case RPC_AUTH_UNIX:
3338 return nfs_ok;
3339 default:
3340 /*
3341 * GSS case: the spec doesn't allow us to return this
3342 * error. But it also doesn't allow us not to support
3343 * GSS.
3344 * I'd rather this fail hard than return some error the
3345 * client might think it can already handle:
3346 */
3347 return nfserr_encr_alg_unsupp;
3348 }
3349}
3350
Andy Adamson069b6ad2009-04-03 08:27:58 +03003351__be32
3352nfsd4_create_session(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003353 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003354{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003355 struct nfsd4_create_session *cr_ses = &u->create_session;
Jeff Layton363168b2009-08-14 12:57:56 -04003356 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003357 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003358 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003359 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003360 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003361 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003362 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003363 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003364
Mi Jinlonga62573d2011-03-23 17:57:07 +08003365 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3366 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003367 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3368 if (status)
3369 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003370 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04003371 if (status)
3372 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003373 status = check_backchannel_attrs(&cr_ses->back_channel);
3374 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08003375 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003376 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08003377 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003378 if (!new)
3379 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003380 conn = alloc_conn_from_crses(rqstp, cr_ses);
3381 if (!conn)
3382 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08003383
Jeff Laytond20c11d2014-07-30 08:27:07 -04003384 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003385 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003386 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003387 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003388
3389 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003390 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003391 if (!nfsd4_mach_creds_match(conf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003392 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003393 cs_slot = &conf->cl_cs_slot;
3394 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08003395 if (status) {
3396 if (status == nfserr_replay_cache)
3397 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003398 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003399 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003400 } else if (unconf) {
3401 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04003402 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003403 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003404 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003405 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04003406 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003407 if (!nfsd4_mach_creds_match(unconf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003408 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003409 cs_slot = &unconf->cl_cs_slot;
3410 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03003411 if (status) {
3412 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003413 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003414 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003415 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003416 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003417 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04003418 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003419 if (status) {
3420 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003421 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003422 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003423 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04003424 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003425 conf = unconf;
3426 } else {
3427 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003428 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003429 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003430 status = nfs_ok;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003431 /* Persistent sessions are not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003432 cr_ses->flags &= ~SESSION4_PERSIST;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003433 /* Upshifting from TCP to RDMA is not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003434 cr_ses->flags &= ~SESSION4_RDMA;
3435
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003436 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003437 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003438
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003439 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003440 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04003441 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04003442 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003443
Jeff Laytond20c11d2014-07-30 08:27:07 -04003444 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04003445 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003446 spin_unlock(&nn->client_lock);
3447 /* init connection and backchannel */
3448 nfsd4_init_conn(rqstp, conn, new);
3449 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003450 if (old)
3451 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003452 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003453out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003454 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003455 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003456 if (old)
3457 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003458out_free_session:
3459 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003460out_release_drc_mem:
3461 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04003462 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003463}
3464
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003465static __be32 nfsd4_map_bcts_dir(u32 *dir)
3466{
3467 switch (*dir) {
3468 case NFS4_CDFC4_FORE:
3469 case NFS4_CDFC4_BACK:
3470 return nfs_ok;
3471 case NFS4_CDFC4_FORE_OR_BOTH:
3472 case NFS4_CDFC4_BACK_OR_BOTH:
3473 *dir = NFS4_CDFC4_BOTH;
3474 return nfs_ok;
zhengbinfc5fc5d2019-12-19 17:29:20 +08003475 }
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003476 return nfserr_inval;
3477}
3478
Christoph Hellwigeb698532017-05-08 20:58:35 +02003479__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3480 struct nfsd4_compound_state *cstate,
3481 union nfsd4_op_u *u)
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003482{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003483 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003484 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003485 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003486 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003487
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003488 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3489 if (status)
3490 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003491 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003492 session->se_cb_prog = bc->bc_cb_program;
3493 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003494 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003495
3496 nfsd4_probe_callback(session->se_client);
3497
3498 return nfs_ok;
3499}
3500
J. Bruce Fieldsc2d715a2020-04-27 17:59:01 -04003501static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3502{
3503 struct nfsd4_conn *c;
3504
3505 list_for_each_entry(c, &s->se_conns, cn_persession) {
3506 if (c->cn_xprt == xpt) {
3507 return c;
3508 }
3509 }
3510 return NULL;
3511}
3512
3513static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst,
3514 struct nfsd4_session *session, u32 req)
3515{
3516 struct nfs4_client *clp = session->se_client;
3517 struct svc_xprt *xpt = rqst->rq_xprt;
3518 struct nfsd4_conn *c;
3519 __be32 status;
3520
3521 /* Following the last paragraph of RFC 5661 Section 18.34.3: */
3522 spin_lock(&clp->cl_lock);
3523 c = __nfsd4_find_conn(xpt, session);
3524 if (!c)
3525 status = nfserr_noent;
3526 else if (req == c->cn_flags)
3527 status = nfs_ok;
3528 else if (req == NFS4_CDFC4_FORE_OR_BOTH &&
3529 c->cn_flags != NFS4_CDFC4_BACK)
3530 status = nfs_ok;
3531 else if (req == NFS4_CDFC4_BACK_OR_BOTH &&
3532 c->cn_flags != NFS4_CDFC4_FORE)
3533 status = nfs_ok;
3534 else
3535 status = nfserr_inval;
3536 spin_unlock(&clp->cl_lock);
3537 return status;
3538}
3539
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003540__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3541 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003542 union nfsd4_op_u *u)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003543{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003544 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003545 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003546 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003547 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003548 struct net *net = SVC_NET(rqstp);
3549 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003550
3551 if (!nfsd4_last_compound_op(rqstp))
3552 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003553 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003554 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003555 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003556 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003557 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003558 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003559 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003560 goto out;
J. Bruce Fieldsc2d715a2020-04-27 17:59:01 -04003561 status = nfsd4_match_existing_connection(rqstp, session, bcts->dir);
3562 if (status == nfs_ok || status == nfserr_inval)
3563 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003564 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003565 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003566 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003567 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003568 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003569 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003570 goto out;
3571 nfsd4_init_conn(rqstp, conn, session);
3572 status = nfs_ok;
3573out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003574 nfsd4_put_session(session);
3575out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003576 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003577}
3578
J. Bruce Fields665d5072018-04-11 16:53:36 -04003579static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003580{
J. Bruce Fields665d5072018-04-11 16:53:36 -04003581 if (!cstate->session)
Fengguang Wu51d87bc2018-03-22 13:37:20 +08003582 return false;
J. Bruce Fields665d5072018-04-11 16:53:36 -04003583 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003584}
3585
Andy Adamson069b6ad2009-04-03 08:27:58 +03003586__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003587nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3588 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003589{
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003590 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003591 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003592 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003593 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003594 struct net *net = SVC_NET(r);
3595 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003596
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003597 status = nfserr_not_only_op;
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003598 if (nfsd4_compound_in_session(cstate, sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04003599 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003600 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003601 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04003602 }
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003603 dump_sessionid(__func__, sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003604 spin_lock(&nn->client_lock);
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003605 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003606 if (!ses)
3607 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003608 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003609 if (!nfsd4_mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003610 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003611 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003612 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003613 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003614 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003615 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003616
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05003617 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04003618
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003619 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003620 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003621out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003622 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003623out_client_lock:
3624 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003625out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03003626 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003627}
3628
J. Bruce Fields57266a62013-04-13 14:27:29 -04003629static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003630{
3631 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003632 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003633 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003634 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003635
3636 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003637 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003638 if (c)
3639 goto out_free;
3640 status = nfserr_conn_not_bound_to_session;
3641 if (clp->cl_mach_cred)
3642 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003643 __nfsd4_hash_conn(new, ses);
3644 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003645 ret = nfsd4_register_conn(new);
3646 if (ret)
3647 /* oops; xprt is already down: */
3648 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003649 return nfs_ok;
3650out_free:
3651 spin_unlock(&clp->cl_lock);
3652 free_conn(new);
3653 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003654}
3655
Mi Jinlong868b89c2011-04-27 09:09:58 +08003656static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3657{
3658 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3659
3660 return args->opcnt > session->se_fchannel.maxops;
3661}
3662
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003663static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3664 struct nfsd4_session *session)
3665{
3666 struct xdr_buf *xb = &rqstp->rq_arg;
3667
3668 return xb->len > session->se_fchannel.maxreq_sz;
3669}
3670
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003671static bool replay_matches_cache(struct svc_rqst *rqstp,
3672 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3673{
3674 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3675
3676 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3677 (bool)seq->cachethis)
3678 return false;
3679 /*
Scott Mayhew6e73e922019-10-09 15:11:37 -04003680 * If there's an error then the reply can have fewer ops than
3681 * the call.
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003682 */
Scott Mayhew6e73e922019-10-09 15:11:37 -04003683 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3684 return false;
3685 /*
3686 * But if we cached a reply with *more* ops than the call you're
3687 * sending us now, then this new call is clearly not really a
3688 * replay of the old one:
3689 */
3690 if (slot->sl_opcnt > argp->opcnt)
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003691 return false;
3692 /* This is the only check explicitly called by spec: */
3693 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3694 return false;
3695 /*
3696 * There may be more comparisons we could actually do, but the
3697 * spec doesn't require us to catch every case where the calls
3698 * don't match (that would require caching the call as well as
3699 * the reply), so we don't bother.
3700 */
3701 return true;
3702}
3703
Andy Adamson069b6ad2009-04-03 08:27:58 +03003704__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003705nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3706 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003707{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003708 struct nfsd4_sequence *seq = &u->sequence;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003709 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003710 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003711 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003712 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003713 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003714 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003715 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003716 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003717 struct net *net = SVC_NET(rqstp);
3718 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003719
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003720 if (resp->opcnt != 1)
3721 return nfserr_sequence_pos;
3722
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003723 /*
3724 * Will be either used or freed by nfsd4_sequence_check_conn
3725 * below.
3726 */
3727 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3728 if (!conn)
3729 return nfserr_jukebox;
3730
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003731 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003732 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003733 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003734 goto out_no_session;
3735 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003736
Mi Jinlong868b89c2011-04-27 09:09:58 +08003737 status = nfserr_too_many_ops;
3738 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003739 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003740
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003741 status = nfserr_req_too_big;
3742 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003743 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003744
Benny Halevyb85d4c02009-04-03 08:28:08 +03003745 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003746 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003747 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003748
Andy Adamson557ce262009-08-28 08:45:04 -04003749 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003750 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3751
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003752 /* We do not negotiate the number of slots yet, so set the
3753 * maxslots to the session maxreqs which is used to encode
3754 * sr_highest_slotid and the sr_target_slot id to maxslots */
3755 seq->maxslots = session->se_fchannel.maxreqs;
3756
J. Bruce Fields73e79482012-02-13 16:39:00 -05003757 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3758 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003759 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003760 status = nfserr_seq_misordered;
3761 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003762 goto out_put_session;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003763 status = nfserr_seq_false_retry;
3764 if (!replay_matches_cache(rqstp, seq, slot))
3765 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003766 cstate->slot = slot;
3767 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003768 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003769 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003770 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003771 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003772 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003773 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003774 }
3775 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003776 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003777
J. Bruce Fields57266a62013-04-13 14:27:29 -04003778 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003779 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003780 if (status)
3781 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003782
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003783 buflen = (seq->cachethis) ?
3784 session->se_fchannel.maxresp_cached :
3785 session->se_fchannel.maxresp_sz;
3786 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3787 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003788 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003789 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003790 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003791
3792 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003793 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003794 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003795 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003796 if (seq->cachethis)
3797 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003798 else
3799 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003800
3801 cstate->slot = slot;
3802 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003803 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003804
Benny Halevyb85d4c02009-04-03 08:28:08 +03003805out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003806 switch (clp->cl_cb_state) {
3807 case NFSD4_CB_DOWN:
3808 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3809 break;
3810 case NFSD4_CB_FAULT:
3811 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3812 break;
3813 default:
3814 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003815 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003816 if (!list_empty(&clp->cl_revoked))
3817 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003818out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08003819 if (conn)
3820 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003821 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003822 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003823out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003824 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003825 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003826}
3827
Trond Myklebustb6076642014-06-30 11:48:35 -04003828void
3829nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3830{
3831 struct nfsd4_compound_state *cs = &resp->cstate;
3832
3833 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04003834 if (cs->status != nfserr_replay_cache) {
3835 nfsd4_store_cache_entry(resp);
3836 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3837 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003838 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04003839 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003840 } else if (cs->clp)
3841 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04003842}
3843
Mi Jinlong345c2842011-10-20 17:51:39 +08003844__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003845nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3846 struct nfsd4_compound_state *cstate,
3847 union nfsd4_op_u *u)
Mi Jinlong345c2842011-10-20 17:51:39 +08003848{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003849 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003850 struct nfs4_client *conf, *unconf;
3851 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003852 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003853 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08003854
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003855 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003856 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003857 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003858 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08003859
3860 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04003861 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08003862 status = nfserr_clientid_busy;
3863 goto out;
3864 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04003865 status = mark_client_expired_locked(conf);
3866 if (status)
3867 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003868 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08003869 } else if (unconf)
3870 clp = unconf;
3871 else {
3872 status = nfserr_stale_clientid;
3873 goto out;
3874 }
Andrew Elblededeb132016-06-15 12:52:08 -04003875 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003876 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003877 status = nfserr_wrong_cred;
3878 goto out;
3879 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003880 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003881out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003882 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003883 if (clp)
3884 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003885 return status;
3886}
3887
Andy Adamson069b6ad2009-04-03 08:27:58 +03003888__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003889nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3890 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003891{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003892 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003893 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003894
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003895 if (rc->rca_one_fs) {
3896 if (!cstate->current_fh.fh_dentry)
3897 return nfserr_nofilehandle;
3898 /*
3899 * We don't take advantage of the rca_one_fs case.
3900 * That's OK, it's optional, we can safely ignore it.
3901 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02003902 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003903 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003904
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003905 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04003906 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3907 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003908 goto out;
3909
3910 status = nfserr_stale_clientid;
3911 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003912 /*
3913 * The following error isn't really legal.
3914 * But we only get here if the client just explicitly
3915 * destroyed the client. Surely it no longer cares what
3916 * error it gets back on an operation for the dead
3917 * client.
3918 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003919 goto out;
3920
3921 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04003922 nfsd4_client_record_create(cstate->session->se_client);
Scott Mayhew362063a2019-03-26 18:06:28 -04003923 inc_reclaim_complete(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003924out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003925 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003926}
3927
3928__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003929nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003930 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003932 struct nfsd4_setclientid *setclid = &u->setclientid;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003933 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003935 struct nfs4_client *conf, *new;
3936 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003937 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003938 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3939
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003940 new = create_client(clname, rqstp, &clverifier);
3941 if (new == NULL)
3942 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003943 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003944 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003945 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04003946 if (conf && client_has_state(conf)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003947 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003949 if (clp_used_exchangeid(conf))
3950 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003951 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04003952 trace_nfsd_clid_inuse_err(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 goto out;
3954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003956 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003957 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003958 unhash_client_locked(unconf);
Chuck Lever45f56da2020-04-05 14:40:25 -04003959 /* We need to handle only case 1: probable callback update */
Kinglong Mee41eb1672015-07-13 17:29:41 +08003960 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003962 gen_confirm(new, nn);
Chuck Lever45f56da2020-04-05 14:40:25 -04003963 }
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003964 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003965 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003966 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3968 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3969 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003970 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 status = nfs_ok;
3972out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003973 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003974 if (new)
3975 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003976 if (unconf)
3977 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 return status;
3979}
3980
3981
Al Virob37ad282006-10-19 23:28:59 -07003982__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003983nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003984 struct nfsd4_compound_state *cstate,
3985 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003987 struct nfsd4_setclientid_confirm *setclientid_confirm =
3988 &u->setclientid_confirm;
NeilBrown21ab45a2005-06-23 22:04:14 -07003989 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003990 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3992 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003993 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003994 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003996 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003998
Jeff Laytond20c11d2014-07-30 08:27:07 -04003999 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004000 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004001 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05004002 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04004003 * We try hard to give out unique clientid's, so if we get an
4004 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04004005 * the client may be buggy; this should never happen.
4006 *
4007 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05004008 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04004009 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04004010 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
4011 goto out;
4012 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
4013 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04004014 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04004015 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04004016 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
4017 /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 status = nfs_ok;
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04004019 } else /* case 4: client hasn't noticed we rebooted yet? */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04004020 status = nfserr_stale_clientid;
4021 goto out;
4022 }
4023 status = nfs_ok;
4024 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04004025 old = unconf;
4026 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04004027 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04004028 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04004029 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4030 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04004031 status = nfserr_clid_inuse;
4032 if (client_has_state(old)
4033 && !same_creds(&unconf->cl_cred,
4034 &old->cl_cred))
4035 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04004036 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04004037 if (status) {
4038 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04004039 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04004040 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004041 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04004042 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04004043 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07004044 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04004045 get_client_locked(conf);
4046 spin_unlock(&nn->client_lock);
4047 nfsd4_probe_callback(conf);
4048 spin_lock(&nn->client_lock);
4049 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04004051 spin_unlock(&nn->client_lock);
4052 if (old)
4053 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 return status;
4055}
4056
J. Bruce Fields32513b42011-10-13 16:00:16 -04004057static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058{
J. Bruce Fields32513b42011-10-13 16:00:16 -04004059 return kmem_cache_alloc(file_slab, GFP_KERNEL);
4060}
4061
4062/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04004063static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
4064 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04004065{
Trond Myklebust950e0112014-06-30 11:48:31 -04004066 lockdep_assert_held(&state_lock);
4067
Elena Reshetova818a34e2017-10-20 12:53:30 +03004068 refcount_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004069 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04004070 INIT_LIST_HEAD(&fp->fi_stateids);
4071 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004072 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04004073 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04004074 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004075 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004076 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004077 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4078 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004079#ifdef CONFIG_NFSD_PNFS
4080 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02004081 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004082#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04004083 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084}
4085
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004086void
NeilBrowne60d4392005-06-23 22:03:01 -07004087nfsd4_free_slabs(void)
4088{
Jeff Layton9258a2d2018-03-16 09:47:22 -04004089 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004090 kmem_cache_destroy(openowner_slab);
4091 kmem_cache_destroy(lockowner_slab);
4092 kmem_cache_destroy(file_slab);
4093 kmem_cache_destroy(stateid_slab);
4094 kmem_cache_destroy(deleg_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04004095 kmem_cache_destroy(odstate_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07004096}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097
Bryan Schumaker72083392011-11-01 15:24:59 -04004098int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099nfsd4_init_slabs(void)
4100{
Jeff Layton9258a2d2018-03-16 09:47:22 -04004101 client_slab = kmem_cache_create("nfsd4_clients",
4102 sizeof(struct nfs4_client), 0, 0, NULL);
4103 if (client_slab == NULL)
4104 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004105 openowner_slab = kmem_cache_create("nfsd4_openowners",
4106 sizeof(struct nfs4_openowner), 0, 0, NULL);
4107 if (openowner_slab == NULL)
Jeff Layton9258a2d2018-03-16 09:47:22 -04004108 goto out_free_client_slab;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004109 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08004110 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004111 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004112 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07004113 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09004114 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07004115 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004116 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07004117 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004118 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07004119 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004120 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07004121 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09004122 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07004123 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004124 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004125 odstate_slab = kmem_cache_create("nfsd4_odstate",
4126 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4127 if (odstate_slab == NULL)
4128 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07004130
Sachin Bhamare8287f002015-04-27 14:50:14 +02004131out_free_deleg_slab:
4132 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004133out_free_stateid_slab:
4134 kmem_cache_destroy(stateid_slab);
4135out_free_file_slab:
4136 kmem_cache_destroy(file_slab);
4137out_free_lockowner_slab:
4138 kmem_cache_destroy(lockowner_slab);
4139out_free_openowner_slab:
4140 kmem_cache_destroy(openowner_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04004141out_free_client_slab:
4142 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004143out:
NeilBrowne60d4392005-06-23 22:03:01 -07004144 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145}
4146
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004147static void init_nfs4_replay(struct nfs4_replay *rp)
4148{
4149 rp->rp_status = nfserr_serverfault;
4150 rp->rp_buflen = 0;
4151 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04004152 mutex_init(&rp->rp_mutex);
4153}
4154
4155static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4156 struct nfs4_stateowner *so)
4157{
4158 if (!nfsd4_has_session(cstate)) {
4159 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004160 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004161 }
4162}
4163
4164void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4165{
4166 struct nfs4_stateowner *so = cstate->replay_owner;
4167
4168 if (so != NULL) {
4169 cstate->replay_owner = NULL;
4170 mutex_unlock(&so->so_replay.rp_mutex);
4171 nfs4_put_stateowner(so);
4172 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004173}
4174
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004175static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176{
4177 struct nfs4_stateowner *sop;
4178
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004179 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004180 if (!sop)
4181 return NULL;
4182
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04004183 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004184 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004185 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004186 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004188
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004189 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004190 sop->so_client = clp;
4191 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04004192 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004193 return sop;
4194}
4195
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004196static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004197{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004198 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004199
Trond Myklebustd4f04892014-07-29 21:34:36 -04004200 list_add(&oo->oo_owner.so_strhash,
4201 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004202 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203}
4204
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004205static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4206{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004207 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004208}
4209
Jeff Layton6b180f02014-07-29 21:34:26 -04004210static void nfs4_free_openowner(struct nfs4_stateowner *so)
4211{
4212 struct nfs4_openowner *oo = openowner(so);
4213
4214 kmem_cache_free(openowner_slab, oo);
4215}
4216
4217static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004218 .so_unhash = nfs4_unhash_openowner,
4219 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004220};
4221
Andrew Elble7fc05642015-11-05 20:42:43 -05004222static struct nfs4_ol_stateid *
4223nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4224{
4225 struct nfs4_ol_stateid *local, *ret = NULL;
4226 struct nfs4_openowner *oo = open->op_openowner;
4227
4228 lockdep_assert_held(&fp->fi_lock);
4229
4230 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4231 /* ignore lock owners */
4232 if (local->st_stateowner->so_is_open_owner == 0)
4233 continue;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004234 if (local->st_stateowner != &oo->oo_owner)
4235 continue;
4236 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
Andrew Elble7fc05642015-11-05 20:42:43 -05004237 ret = local;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004238 refcount_inc(&ret->st_stid.sc_count);
Andrew Elble7fc05642015-11-05 20:42:43 -05004239 break;
4240 }
4241 }
4242 return ret;
4243}
4244
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004245static __be32
4246nfsd4_verify_open_stid(struct nfs4_stid *s)
4247{
4248 __be32 ret = nfs_ok;
4249
4250 switch (s->sc_type) {
4251 default:
4252 break;
Trond Myklebust4f176412018-01-12 17:42:30 -05004253 case 0:
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004254 case NFS4_CLOSED_STID:
4255 case NFS4_CLOSED_DELEG_STID:
4256 ret = nfserr_bad_stateid;
4257 break;
4258 case NFS4_REVOKED_DELEG_STID:
4259 ret = nfserr_deleg_revoked;
4260 }
4261 return ret;
4262}
4263
4264/* Lock the stateid st_mutex, and deal with races with CLOSE */
4265static __be32
4266nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4267{
4268 __be32 ret;
4269
Andrew Elble4f34bd02017-11-08 17:29:51 -05004270 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004271 ret = nfsd4_verify_open_stid(&stp->st_stid);
4272 if (ret != nfs_ok)
4273 mutex_unlock(&stp->st_mutex);
4274 return ret;
4275}
4276
4277static struct nfs4_ol_stateid *
4278nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4279{
4280 struct nfs4_ol_stateid *stp;
4281 for (;;) {
4282 spin_lock(&fp->fi_lock);
4283 stp = nfsd4_find_existing_open(fp, open);
4284 spin_unlock(&fp->fi_lock);
4285 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4286 break;
4287 nfs4_put_stid(&stp->st_stid);
4288 }
4289 return stp;
4290}
4291
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004292static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04004293alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004294 struct nfsd4_compound_state *cstate)
4295{
Trond Myklebust13d6f662014-06-30 11:48:45 -04004296 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004297 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004299 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4300 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04004302 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004303 oo->oo_owner.so_is_open_owner = 1;
4304 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004305 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004306 if (nfsd4_has_session(cstate))
4307 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004308 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004309 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004310 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04004311 spin_lock(&clp->cl_lock);
4312 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004313 if (ret == NULL) {
4314 hash_openowner(oo, clp, strhashval);
4315 ret = oo;
4316 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08004317 nfs4_free_stateowner(&oo->oo_owner);
4318
Trond Myklebustd4f04892014-07-29 21:34:36 -04004319 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04004320 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321}
4322
Andrew Elble7fc05642015-11-05 20:42:43 -05004323static struct nfs4_ol_stateid *
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004324init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
Andrew Elble7fc05642015-11-05 20:42:43 -05004325{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326
Andrew Elble7fc05642015-11-05 20:42:43 -05004327 struct nfs4_openowner *oo = open->op_openowner;
4328 struct nfs4_ol_stateid *retstp = NULL;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004329 struct nfs4_ol_stateid *stp;
Andrew Elble7fc05642015-11-05 20:42:43 -05004330
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004331 stp = open->op_stp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004332 /* We are moving these outside of the spinlocks to avoid the warnings */
4333 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05004334 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004335
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004336retry:
Andrew Elble7fc05642015-11-05 20:42:43 -05004337 spin_lock(&oo->oo_owner.so_client->cl_lock);
4338 spin_lock(&fp->fi_lock);
4339
4340 retstp = nfsd4_find_existing_open(fp, open);
4341 if (retstp)
4342 goto out_unlock;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004343
4344 open->op_stp = NULL;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004345 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004346 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004347 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004348 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07004349 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004350 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 stp->st_access_bmap = 0;
4352 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07004353 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04004354 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004355 list_add(&stp->st_perfile, &fp->fi_stateids);
Andrew Elble7fc05642015-11-05 20:42:43 -05004356
4357out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04004358 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004359 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004360 if (retstp) {
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004361 /* Handle races with CLOSE */
4362 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4363 nfs4_put_stid(&retstp->st_stid);
4364 goto retry;
4365 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004366 /* To keep mutex tracking happy */
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004367 mutex_unlock(&stp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004368 stp = retstp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004369 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004370 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371}
4372
Jeff Laytond3134b12014-07-29 21:34:32 -04004373/*
4374 * In the 4.0 case we need to keep the owners around a little while to handle
4375 * CLOSE replay. We still do need to release any file access that is held by
4376 * them before returning however.
4377 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378static void
Jeff Laytond3134b12014-07-29 21:34:32 -04004379move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380{
Jeff Layton217526e2014-07-30 08:27:11 -04004381 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04004382 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4383 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4384 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004385
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004386 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
Jeff Laytonb401be222014-07-29 21:34:33 -04004388 /*
4389 * We know that we hold one reference via nfsd4_close, and another
4390 * "persistent" reference for the client. If the refcount is higher
4391 * than 2, then there are still calls in progress that are using this
4392 * stateid. We can't put the sc_file reference until they are finished.
4393 * Wait for the refcount to drop to 2. Since it has been unhashed,
4394 * there should be no danger of the refcount going back up again at
4395 * this point.
4396 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004397 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
Jeff Laytonb401be222014-07-29 21:34:33 -04004398
Jeff Laytond3134b12014-07-29 21:34:32 -04004399 release_all_access(s);
4400 if (s->st_stid.sc_file) {
4401 put_nfs4_file(s->st_stid.sc_file);
4402 s->st_stid.sc_file = NULL;
4403 }
Jeff Layton217526e2014-07-30 08:27:11 -04004404
4405 spin_lock(&nn->client_lock);
4406 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004407 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004408 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01004409 oo->oo_time = ktime_get_boottime_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04004410 spin_unlock(&nn->client_lock);
4411 if (last)
4412 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413}
4414
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415/* search file_hashtbl[] for file */
4416static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04004417find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 struct nfs4_file *fp;
4420
Madhuparna Bhowmik36a80492020-02-13 20:33:31 +05304421 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4422 lockdep_is_held(&state_lock)) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02004423 if (fh_match(&fp->fi_fhandle, fh)) {
Elena Reshetova818a34e2017-10-20 12:53:30 +03004424 if (refcount_inc_not_zero(&fp->fi_ref))
Jeff Layton5b095e92014-10-23 08:01:02 -04004425 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07004426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 }
4428 return NULL;
4429}
4430
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02004431struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04004432find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004433{
4434 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004435 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04004436
Jeff Layton5b095e92014-10-23 08:01:02 -04004437 rcu_read_lock();
4438 fp = find_file_locked(fh, hashval);
4439 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04004440 return fp;
4441}
4442
4443static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004444find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004445{
4446 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004447 unsigned int hashval = file_hashval(fh);
4448
4449 rcu_read_lock();
4450 fp = find_file_locked(fh, hashval);
4451 rcu_read_unlock();
4452 if (fp)
4453 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04004454
4455 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04004456 fp = find_file_locked(fh, hashval);
4457 if (likely(fp == NULL)) {
4458 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04004459 fp = new;
4460 }
4461 spin_unlock(&state_lock);
4462
4463 return fp;
4464}
4465
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04004466/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 * Called to check deny when READ with all zero stateid or
4468 * WRITE with all zero or all one stateid
4469 */
Al Virob37ad282006-10-19 23:28:59 -07004470static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4472{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004474 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
Trond Myklebustca943212014-07-23 16:17:39 -04004476 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07004477 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004478 return ret;
4479 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04004480 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004481 if (fp->fi_share_deny & deny_type)
4482 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04004483 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07004484 put_nfs4_file(fp);
4485 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486}
4487
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004488static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004490 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04004491 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4492 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004493
Trond Myklebust11b91642014-07-29 21:34:08 -04004494 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04004495
Jeff Layton02e12152014-07-16 10:31:57 -04004496 /*
4497 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04004498 * already holding inode->i_lock.
4499 *
Jeff Laytondff13992014-07-08 14:02:49 -04004500 * If the dl_time != 0, then we know that it has already been
4501 * queued for a lease break. Don't queue it again.
4502 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04004503 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04004504 if (dp->dl_time == 0) {
Arnd Bergmann20b7d862019-11-04 16:31:52 +01004505 dp->dl_time = ktime_get_boottime_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04004506 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04004507 }
Jeff Layton02e12152014-07-16 10:31:57 -04004508 spin_unlock(&state_lock);
4509}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004511static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4512 struct rpc_task *task)
4513{
4514 struct nfs4_delegation *dp = cb_to_delegation(cb);
4515
J. Bruce Fields12ed22f2020-07-31 16:20:05 -04004516 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID ||
4517 dp->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID)
Andrew Elblea4579742015-08-31 12:06:41 -04004518 return 1;
4519
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004520 switch (task->tk_status) {
4521 case 0:
4522 return 1;
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004523 case -NFS4ERR_DELAY:
4524 rpc_delay(task, 2 * HZ);
4525 return 0;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004526 case -EBADHANDLE:
4527 case -NFS4ERR_BAD_STATEID:
4528 /*
4529 * Race: client probably got cb_recall before open reply
4530 * granting delegation.
4531 */
4532 if (dp->dl_retries--) {
4533 rpc_delay(task, 2 * HZ);
4534 return 0;
4535 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004536 fallthrough;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004537 default:
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004538 return 1;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004539 }
4540}
4541
4542static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4543{
4544 struct nfs4_delegation *dp = cb_to_delegation(cb);
4545
4546 nfs4_put_stid(&dp->dl_stid);
4547}
4548
Julia Lawallc4cb8972015-11-21 22:57:39 +01004549static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004550 .prepare = nfsd4_cb_recall_prepare,
4551 .done = nfsd4_cb_recall_done,
4552 .release = nfsd4_cb_recall_release,
4553};
4554
Jeff Layton02e12152014-07-16 10:31:57 -04004555static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4556{
4557 /*
4558 * We're assuming the state code never drops its reference
4559 * without first removing the lease. Since we're in this lease
J. Bruce Fields4a269efb2018-04-13 17:34:35 -04004560 * callback (and since the lease code is serialized by the
4561 * i_lock) we know the server hasn't removed the lease yet, and
4562 * we know it's safe to take a reference.
Jeff Layton02e12152014-07-16 10:31:57 -04004563 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004564 refcount_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02004565 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004566}
4567
Jeff Layton1c8c6012013-06-21 08:58:15 -04004568/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004569static bool
4570nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004571{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004572 bool ret = false;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004573 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4574 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004575
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04004576 trace_nfsd_deleg_break(&dp->dl_stid.sc_stateid);
4577
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004578 /*
4579 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004580 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004581 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004582 */
4583 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584
Jeff Layton02e12152014-07-16 10:31:57 -04004585 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004586 fp->fi_had_conflict = true;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004587 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04004588 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004589 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590}
4591
J. Bruce Fields28df3d12017-07-28 16:35:15 -04004592static bool nfsd_breaker_owns_lease(struct file_lock *fl)
4593{
4594 struct nfs4_delegation *dl = fl->fl_owner;
4595 struct svc_rqst *rqst;
4596 struct nfs4_client *clp;
4597
4598 if (!i_am_nfsd())
4599 return NULL;
4600 rqst = kthread_data(current);
J. Bruce Fields13956162020-09-25 10:12:39 -04004601 /* Note rq_prog == NFS_ACL_PROGRAM is also possible: */
4602 if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4)
J. Bruce Fields34b09af2020-08-05 15:10:11 -04004603 return NULL;
J. Bruce Fields28df3d12017-07-28 16:35:15 -04004604 clp = *(rqst->rq_lease_breaker);
4605 return dl->dl_stid.sc_client == clp;
4606}
4607
Jeff Laytonc45198e2014-09-01 07:12:07 -04004608static int
Jeff Layton7448cc32015-01-16 15:05:57 -05004609nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4610 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611{
4612 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04004613 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 else
4615 return -EAGAIN;
4616}
4617
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004618static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields28df3d12017-07-28 16:35:15 -04004619 .lm_breaker_owns_lease = nfsd_breaker_owns_lease,
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04004620 .lm_break = nfsd_break_deleg_cb,
4621 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622};
4623
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004624static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4625{
4626 if (nfsd4_has_session(cstate))
4627 return nfs_ok;
4628 if (seqid == so->so_seqid - 1)
4629 return nfserr_replay_me;
4630 if (seqid == so->so_seqid)
4631 return nfs_ok;
4632 return nfserr_bad_seqid;
4633}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634
Jeff Layton4b24ca72014-06-30 11:48:44 -04004635static __be32 lookup_clientid(clientid_t *clid,
4636 struct nfsd4_compound_state *cstate,
Olga Kornievskaiab7342202019-09-06 15:17:21 -04004637 struct nfsd_net *nn,
4638 bool sessions)
Jeff Layton4b24ca72014-06-30 11:48:44 -04004639{
4640 struct nfs4_client *found;
4641
4642 if (cstate->clp) {
4643 found = cstate->clp;
4644 if (!same_clid(&found->cl_clientid, clid))
4645 return nfserr_stale_clientid;
4646 return nfs_ok;
4647 }
4648
4649 if (STALE_CLIENTID(clid, nn))
4650 return nfserr_stale_clientid;
4651
4652 /*
4653 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4654 * cached already then we know this is for is for v4.0 and "sessions"
4655 * will be false.
4656 */
4657 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004658 spin_lock(&nn->client_lock);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04004659 found = find_confirmed_client(clid, sessions, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004660 if (!found) {
4661 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004662 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04004663 }
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04004664 atomic_inc(&found->cl_rpc_users);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004665 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004666
4667 /* Cache the nfs4_client in cstate! */
4668 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004669 return nfs_ok;
4670}
4671
Al Virob37ad282006-10-19 23:28:59 -07004672__be32
Andy Adamson66689582009-04-03 08:28:45 +03004673nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004674 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 clientid_t *clientid = &open->op_clientid;
4677 struct nfs4_client *clp = NULL;
4678 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004679 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004680 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004682 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004684 /*
4685 * In case we need it later, after we've already created the
4686 * file and don't want to risk a further failure:
4687 */
4688 open->op_file = nfsd4_alloc_file();
4689 if (open->op_file == NULL)
4690 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691
Olga Kornievskaiab7342202019-09-06 15:17:21 -04004692 status = lookup_clientid(clientid, cstate, nn, false);
Trond Myklebust2d91e892014-06-30 11:48:46 -04004693 if (status)
4694 return status;
4695 clp = cstate->clp;
4696
Trond Myklebustd4f04892014-07-29 21:34:36 -04004697 strhashval = ownerstr_hashval(&open->op_owner);
4698 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004699 open->op_openowner = oo;
4700 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004701 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004703 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004704 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004705 release_openowner(oo);
4706 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004707 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004708 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004709 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4710 if (status)
4711 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004712 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004713new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04004714 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004715 if (oo == NULL)
4716 return nfserr_jukebox;
4717 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004718alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04004719 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004720 if (!open->op_stp)
4721 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004722
4723 if (nfsd4_has_session(cstate) &&
4724 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4725 open->op_odstate = alloc_clnt_odstate(clp);
4726 if (!open->op_odstate)
4727 return nfserr_jukebox;
4728 }
4729
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004730 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731}
4732
Al Virob37ad282006-10-19 23:28:59 -07004733static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07004734nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4735{
4736 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4737 return nfserr_openmode;
4738 else
4739 return nfs_ok;
4740}
4741
Daniel Mackc47d8322011-05-16 16:38:14 +02004742static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04004743{
J. Bruce Fields24a01112010-05-18 20:01:35 -04004744 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4745}
4746
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004747static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004748{
4749 struct nfs4_stid *ret;
4750
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004751 ret = find_stateid_by_type(cl, s,
4752 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004753 if (!ret)
4754 return NULL;
4755 return delegstateid(ret);
4756}
4757
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004758static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4759{
4760 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4761 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4762}
4763
Al Virob37ad282006-10-19 23:28:59 -07004764static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04004765nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07004766 struct nfs4_delegation **dp)
4767{
4768 int flags;
Al Virob37ad282006-10-19 23:28:59 -07004769 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004770 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07004771
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004772 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4773 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07004774 goto out;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004775 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4776 nfs4_put_stid(&deleg->dl_stid);
4777 if (cl->cl_minorversion)
4778 status = nfserr_deleg_revoked;
4779 goto out;
4780 }
J. Bruce Fields24a01112010-05-18 20:01:35 -04004781 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004782 status = nfs4_check_delegmode(deleg, flags);
4783 if (status) {
4784 nfs4_put_stid(&deleg->dl_stid);
4785 goto out;
4786 }
4787 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004788out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004789 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004790 return nfs_ok;
4791 if (status)
4792 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004793 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004794 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07004795}
4796
J. Bruce Fields21fb4012010-07-28 12:21:23 -04004797static inline int nfs4_access_to_access(u32 nfs4_access)
4798{
4799 int flags = 0;
4800
4801 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4802 flags |= NFSD_MAY_READ;
4803 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4804 flags |= NFSD_MAY_WRITE;
4805 return flags;
4806}
4807
Al Virob37ad282006-10-19 23:28:59 -07004808static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4810 struct nfsd4_open *open)
4811{
4812 struct iattr iattr = {
4813 .ia_valid = ATTR_SIZE,
4814 .ia_size = 0,
4815 };
4816 if (!open->op_truncate)
4817 return 0;
4818 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08004819 return nfserr_inval;
Arnd Bergmann2a1aa482019-11-03 17:50:18 +01004820 return nfsd_setattr(rqstp, fh, &iattr, 0, (time64_t)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821}
4822
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004823static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004824 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4825 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004826{
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004827 struct nfsd_file *nf = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004828 __be32 status;
4829 int oflag = nfs4_access_to_omode(open->op_share_access);
4830 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004831 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004832
Trond Myklebustde186432014-07-10 14:07:26 -04004833 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004834
4835 /*
4836 * Are we trying to set a deny mode that would conflict with
4837 * current access?
4838 */
4839 status = nfs4_file_check_deny(fp, open->op_share_deny);
4840 if (status != nfs_ok) {
4841 spin_unlock(&fp->fi_lock);
4842 goto out;
4843 }
4844
4845 /* set access to the file */
4846 status = nfs4_file_get_access(fp, open->op_share_access);
4847 if (status != nfs_ok) {
4848 spin_unlock(&fp->fi_lock);
4849 goto out;
4850 }
4851
4852 /* Set access bits in stateid */
4853 old_access_bmap = stp->st_access_bmap;
4854 set_access(open->op_share_access, stp);
4855
4856 /* Set new deny mask */
4857 old_deny_bmap = stp->st_deny_bmap;
4858 set_deny(open->op_share_deny, stp);
4859 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4860
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004861 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04004862 spin_unlock(&fp->fi_lock);
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004863 status = nfsd_file_acquire(rqstp, cur_fh, access, &nf);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004864 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004865 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04004866 spin_lock(&fp->fi_lock);
4867 if (!fp->fi_fds[oflag]) {
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004868 fp->fi_fds[oflag] = nf;
4869 nf = NULL;
Trond Myklebustde186432014-07-10 14:07:26 -04004870 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004871 }
Trond Myklebustde186432014-07-10 14:07:26 -04004872 spin_unlock(&fp->fi_lock);
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004873 if (nf)
4874 nfsd_file_put(nf);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004875
4876 status = nfsd4_truncate(rqstp, cur_fh, open);
4877 if (status)
4878 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004879out:
4880 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004881out_put_access:
4882 stp->st_access_bmap = old_access_bmap;
4883 nfs4_file_put_access(fp, open->op_share_access);
4884 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4885 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004886}
4887
Al Virob37ad282006-10-19 23:28:59 -07004888static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004889nfs4_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 -07004890{
Al Virob37ad282006-10-19 23:28:59 -07004891 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02004892 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004894 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004895 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004896
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004897 /* test and set deny mode */
4898 spin_lock(&fp->fi_lock);
4899 status = nfs4_file_check_deny(fp, open->op_share_deny);
4900 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004901 set_deny(open->op_share_deny, stp);
4902 fp->fi_share_deny |=
4903 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4904 }
4905 spin_unlock(&fp->fi_lock);
4906
4907 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004910 status = nfsd4_truncate(rqstp, cur_fh, open);
4911 if (status != nfs_ok)
4912 reset_union_bmap_deny(old_deny_bmap, stp);
4913 return status;
4914}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004916/* Should we give out recallable state?: */
4917static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4918{
4919 if (clp->cl_cb_state == NFSD4_CB_UP)
4920 return true;
4921 /*
4922 * In the sessions case, since we don't have to establish a
4923 * separate connection for callbacks, we assume it's OK
4924 * until we hear otherwise:
4925 */
4926 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4927}
4928
J. Bruce Fields653e5142018-02-15 22:08:45 -05004929static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4930 int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004931{
4932 struct file_lock *fl;
4933
4934 fl = locks_alloc_lock();
4935 if (!fl)
4936 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004937 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04004938 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004939 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4940 fl->fl_end = OFFSET_MAX;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004941 fl->fl_owner = (fl_owner_t)dp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004942 fl->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004943 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004944 return fl;
4945}
4946
J. Bruce Fields94415b02020-07-07 09:28:05 -04004947static int nfsd4_check_conflicting_opens(struct nfs4_client *clp,
4948 struct nfs4_file *fp)
4949{
4950 struct nfs4_clnt_odstate *co;
4951 struct file *f = fp->fi_deleg_file->nf_file;
4952 struct inode *ino = locks_inode(f);
4953 int writes = atomic_read(&ino->i_writecount);
4954
4955 if (fp->fi_fds[O_WRONLY])
4956 writes--;
4957 if (fp->fi_fds[O_RDWR])
4958 writes--;
J. Bruce Fields94415b02020-07-07 09:28:05 -04004959 if (writes > 0)
4960 return -EAGAIN;
4961 spin_lock(&fp->fi_lock);
4962 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
4963 if (co->co_client != clp) {
4964 spin_unlock(&fp->fi_lock);
4965 return -EAGAIN;
4966 }
4967 }
4968 spin_unlock(&fp->fi_lock);
4969 return 0;
4970}
4971
Jeff Layton0b266932014-07-25 07:34:25 -04004972static struct nfs4_delegation *
4973nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004974 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004975{
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004976 int status = 0;
Jeff Layton0b266932014-07-25 07:34:25 -04004977 struct nfs4_delegation *dp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004978 struct nfsd_file *nf;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004979 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04004980
J. Bruce Fields353601e2018-02-16 14:29:42 -05004981 /*
4982 * The fi_had_conflict and nfs_get_existing_delegation checks
4983 * here are just optimizations; we'll need to recheck them at
4984 * the end:
4985 */
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004986 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004987 return ERR_PTR(-EAGAIN);
4988
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004989 nf = find_readable_file(fp);
4990 if (!nf) {
J. Bruce Fields94415b02020-07-07 09:28:05 -04004991 /*
4992 * We probably could attempt another open and get a read
4993 * delegation, but for now, don't bother until the
4994 * client actually sends us one.
4995 */
4996 return ERR_PTR(-EAGAIN);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004997 }
Andrew Elble34ed9872015-10-15 12:07:28 -04004998 spin_lock(&state_lock);
4999 spin_lock(&fp->fi_lock);
J. Bruce Fields68b18f52018-02-19 11:38:33 -05005000 if (nfs4_delegation_exists(clp, fp))
5001 status = -EAGAIN;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005002 else if (!fp->fi_deleg_file) {
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005003 fp->fi_deleg_file = nf;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005004 /* increment early to prevent fi_deleg_file from being
5005 * cleared */
5006 fp->fi_delegees = 1;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005007 nf = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005008 } else
5009 fp->fi_delegees++;
5010 spin_unlock(&fp->fi_lock);
5011 spin_unlock(&state_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005012 if (nf)
5013 nfsd_file_put(nf);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005014 if (status)
5015 return ERR_PTR(status);
5016
5017 status = -ENOMEM;
5018 dp = alloc_init_deleg(clp, fp, fh, odstate);
5019 if (!dp)
5020 goto out_delegees;
5021
5022 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
5023 if (!fl)
Andrew Elblebd8d7252018-10-05 09:32:08 -04005024 goto out_clnt_odstate;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005025
J. Bruce Fields94415b02020-07-07 09:28:05 -04005026 status = nfsd4_check_conflicting_opens(clp, fp);
5027 if (status) {
5028 locks_free_lock(fl);
5029 goto out_clnt_odstate;
5030 }
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005031 status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005032 if (fl)
5033 locks_free_lock(fl);
5034 if (status)
5035 goto out_clnt_odstate;
J. Bruce Fields94415b02020-07-07 09:28:05 -04005036 status = nfsd4_check_conflicting_opens(clp, fp);
5037 if (status)
5038 goto out_clnt_odstate;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005039
5040 spin_lock(&state_lock);
5041 spin_lock(&fp->fi_lock);
5042 if (fp->fi_had_conflict)
5043 status = -EAGAIN;
5044 else
5045 status = hash_delegation_locked(dp, fp);
Andrew Elble34ed9872015-10-15 12:07:28 -04005046 spin_unlock(&fp->fi_lock);
5047 spin_unlock(&state_lock);
5048
5049 if (status)
Andrew Elble692ad282018-04-18 17:04:37 -04005050 goto out_unlock;
5051
Jeff Layton0b266932014-07-25 07:34:25 -04005052 return dp;
Andrew Elble692ad282018-04-18 17:04:37 -04005053out_unlock:
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005054 vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005055out_clnt_odstate:
5056 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005057 nfs4_put_stid(&dp->dl_stid);
5058out_delegees:
5059 put_deleg_file(fp);
5060 return ERR_PTR(status);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05005061}
5062
Benny Halevy4aa89132012-02-21 14:16:44 -08005063static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
5064{
5065 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5066 if (status == -EAGAIN)
5067 open->op_why_no_deleg = WND4_CONTENTION;
5068 else {
5069 open->op_why_no_deleg = WND4_RESOURCE;
5070 switch (open->op_deleg_want) {
5071 case NFS4_SHARE_WANT_READ_DELEG:
5072 case NFS4_SHARE_WANT_WRITE_DELEG:
5073 case NFS4_SHARE_WANT_ANY_DELEG:
5074 break;
5075 case NFS4_SHARE_WANT_CANCEL:
5076 open->op_why_no_deleg = WND4_CANCELLED;
5077 break;
5078 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005079 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08005080 }
5081 }
5082}
5083
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084/*
5085 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04005086 *
5087 * Note we don't support write delegations, and won't until the vfs has
5088 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 */
5090static void
Jeff Layton4cf59222014-07-25 07:34:24 -04005091nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
5092 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093{
5094 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04005095 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
5096 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05005097 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04005098 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005100 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07005101 open->op_recall = 0;
5102 switch (open->op_claim_type) {
5103 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05005104 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07005105 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04005106 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
5107 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005108 break;
5109 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00005110 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005111 /*
5112 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005113 * had the chance to reclaim theirs, *and* until
5114 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005115 */
Jeff Layton4cf59222014-07-25 07:34:24 -04005116 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04005117 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04005118 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04005119 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005120 break;
5121 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005122 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005123 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02005124 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04005125 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005126 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04005128 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129
Hou Tao3caf9172020-08-28 15:02:55 +08005130 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
J. Bruce Fields99c41512013-05-21 16:21:25 -04005131 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04005132 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005133 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005134out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005135 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
5136 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04005137 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04005138 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04005139 open->op_recall = 1;
5140 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04005141
5142 /* 4.1 client asking for a delegation? */
5143 if (open->op_deleg_want)
5144 nfsd4_open_deleg_none_ext(open, status);
5145 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146}
5147
Benny Halevye27f49c2012-02-21 14:16:54 -08005148static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5149 struct nfs4_delegation *dp)
5150{
5151 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5152 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5153 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5154 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5155 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5156 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5157 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5158 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5159 }
5160 /* Otherwise the client must be confused wanting a delegation
5161 * it already has, therefore we don't return
5162 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5163 */
5164}
5165
Al Virob37ad282006-10-19 23:28:59 -07005166__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5168{
Andy Adamson66689582009-04-03 08:28:45 +03005169 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005170 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005172 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07005173 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005174 __be32 status;
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005175 bool new_stp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 /*
5178 * Lookup file; if found, lookup stateid and check open request,
5179 * and check for delegations in the process of being recalled.
5180 * If not found, create the nfs4_file struct
5181 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005182 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04005183 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04005184 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07005185 if (status)
5186 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005187 stp = nfsd4_find_and_lock_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04005189 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07005190 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04005191 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07005192 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 }
5194
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005195 if (!stp) {
5196 stp = init_open_stateid(fp, open);
5197 if (!open->op_stp)
5198 new_stp = true;
5199 }
5200
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 /*
5202 * OPEN the file, or upgrade an existing OPEN.
5203 * If truncate fails, the OPEN fails.
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005204 *
5205 * stp is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 */
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005207 if (!new_stp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005209 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005210 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005211 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 } else {
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005215 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
5216 if (status) {
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005217 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005218 release_open_stateid(stp);
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005219 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005220 goto out;
5221 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02005222
5223 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5224 open->op_odstate);
5225 if (stp->st_clnt_odstate == open->op_odstate)
5226 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 }
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005228
Jeff Layton9767feb2015-10-01 09:05:50 -04005229 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005230 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231
Benny Halevyd24433c2012-02-16 20:57:17 +02005232 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02005233 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5234 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5235 open->op_why_no_deleg = WND4_NOT_WANTED;
5236 goto nodeleg;
5237 }
5238 }
5239
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 /*
5241 * Attempt to hand out a delegation. No error return, because the
5242 * OPEN succeeds even if we fail.
5243 */
Jeff Layton4cf59222014-07-25 07:34:24 -04005244 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005245nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 status = nfs_ok;
Hou Tao3caf9172020-08-28 15:02:55 +08005247 trace_nfsd_open(&stp->st_stid.sc_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248out:
Benny Halevyd24433c2012-02-16 20:57:17 +02005249 /* 4.1 client trying to upgrade/downgrade delegation? */
5250 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08005251 open->op_deleg_want)
5252 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005253
NeilBrown13cd2182005-06-23 22:03:10 -07005254 if (fp)
5255 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07005256 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08005257 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 /*
5259 * To finish the open response, we just need to set the rflags.
5260 */
5261 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Jeff Layton19e4c342016-09-16 16:28:27 -04005262 if (nfsd4_has_session(&resp->cstate))
5263 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5264 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Jeff Layton19e4c342016-09-16 16:28:27 -04005266
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04005267 if (dp)
5268 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04005269 if (stp)
5270 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271
5272 return status;
5273}
5274
Jeff Layton58fb12e2014-07-29 21:34:27 -04005275void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04005276 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005277{
5278 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04005279 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005280
Jeff Laytond3134b12014-07-29 21:34:32 -04005281 nfsd4_cstate_assign_replay(cstate, so);
5282 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005283 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04005284 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04005285 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04005286 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04005287 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02005288 if (open->op_odstate)
5289 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005290}
5291
Al Virob37ad282006-10-19 23:28:59 -07005292__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005293nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005294 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005296 clientid_t *clid = &u->renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07005298 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005299 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005301 trace_nfsd_clid_renew(clid);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005302 status = lookup_clientid(clid, cstate, nn, false);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005303 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005305 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07005307 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04005308 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 goto out;
5310 status = nfs_ok;
5311out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 return status;
5313}
5314
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04005315void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005316nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07005317{
Jeff Layton33dcc482012-04-10 11:08:48 -04005318 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005319 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04005320 return;
5321
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005322 trace_nfsd_grace_complete(nn);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005323 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04005324 /*
5325 * If the server goes down again right now, an NFSv4
5326 * client will still be allowed to reclaim after it comes back up,
5327 * even if it hasn't yet had a chance to reclaim state this time.
5328 *
5329 */
Jeff Layton919b8042014-09-12 16:40:20 -04005330 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005331 /*
5332 * At this point, NFSv4 clients can still reclaim. But if the
5333 * server crashes, any that have not yet reclaimed will be out
5334 * of luck on the next boot.
5335 *
5336 * (NFSv4.1+ clients are considered to have reclaimed once they
5337 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5338 * have reclaimed after their first OPEN.)
5339 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005340 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005341 /*
5342 * At this point, and once lockd and/or any other containers
5343 * exit their grace period, further reclaims will fail and
5344 * regular locking can resume.
5345 */
NeilBrowna76b4312005-06-23 22:04:01 -07005346}
5347
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005348/*
5349 * If we've waited a lease period but there are still clients trying to
5350 * reclaim, wait a little longer to give them a chance to finish.
5351 */
5352static bool clients_still_reclaiming(struct nfsd_net *nn)
5353{
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005354 time64_t double_grace_period_end = nn->boot_time +
5355 2 * nn->nfsd4_lease;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005356
Scott Mayhew362063a2019-03-26 18:06:28 -04005357 if (nn->track_reclaim_completes &&
5358 atomic_read(&nn->nr_reclaim_complete) ==
5359 nn->reclaim_str_hashtbl_size)
5360 return false;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005361 if (!nn->somebody_reclaimed)
5362 return false;
5363 nn->somebody_reclaimed = false;
5364 /*
5365 * If we've given them *two* lease times to reclaim, and they're
5366 * still not done, give up:
5367 */
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005368 if (ktime_get_boottime_seconds() > double_grace_period_end)
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005369 return false;
5370 return true;
5371}
5372
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005373static time64_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005374nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375{
5376 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005377 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04005379 struct nfs4_ol_stateid *stp;
Jeff Layton7919d0a2016-09-16 16:28:25 -04005380 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 struct list_head *pos, *next, reaplist;
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005382 time64_t cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease;
5383 time64_t t, new_timeo = nn->nfsd4_lease;
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005384 struct nfs4_cpntf_state *cps;
5385 copy_stateid_t *cps_t;
5386 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005388 if (clients_still_reclaiming(nn)) {
5389 new_timeo = 0;
5390 goto out;
5391 }
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005392 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03005393 INIT_LIST_HEAD(&reaplist);
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005394
5395 spin_lock(&nn->s2s_cp_lock);
5396 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
5397 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
5398 if (cps->cp_stateid.sc_type == NFS4_COPYNOTIFY_STID &&
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005399 cps->cpntf_time > cutoff)
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005400 _free_cpntf_state_locked(nn, cps);
5401 }
5402 spin_unlock(&nn->s2s_cp_lock);
5403
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005404 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005405 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 clp = list_entry(pos, struct nfs4_client, cl_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005407 if (clp->cl_time > cutoff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04005409 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 break;
5411 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04005412 if (mark_client_expired_locked(clp)) {
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005413 trace_nfsd_clid_expired(&clp->cl_clientid);
Benny Halevyd7682982010-05-12 00:13:54 +03005414 continue;
5415 }
Trond Myklebust4864af92014-07-30 08:27:03 -04005416 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03005417 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005418 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03005419 list_for_each_safe(pos, next, &reaplist) {
5420 clp = list_entry(pos, struct nfs4_client, cl_lru);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005421 trace_nfsd_clid_purged(&clp->cl_clientid);
Trond Myklebust4864af92014-07-30 08:27:03 -04005422 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 expire_client(clp);
5424 }
Benny Halevycdc975052014-05-30 09:09:30 -04005425 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005426 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005428 if (dp->dl_time > cutoff) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04005429 t = dp->dl_time - cutoff;
5430 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 break;
5432 }
Jeff Layton3fcbbd22015-08-24 12:41:48 -04005433 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04005434 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 }
Benny Halevycdc975052014-05-30 09:09:30 -04005436 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005437 while (!list_empty(&reaplist)) {
5438 dp = list_first_entry(&reaplist, struct nfs4_delegation,
5439 dl_recall_lru);
5440 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005441 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 }
Jeff Layton217526e2014-07-30 08:27:11 -04005443
5444 spin_lock(&nn->client_lock);
5445 while (!list_empty(&nn->close_lru)) {
5446 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
5447 oo_close_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005448 if (oo->oo_time > cutoff) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04005449 t = oo->oo_time - cutoff;
5450 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 break;
5452 }
Jeff Layton217526e2014-07-30 08:27:11 -04005453 list_del_init(&oo->oo_close_lru);
5454 stp = oo->oo_last_closed_stid;
5455 oo->oo_last_closed_stid = NULL;
5456 spin_unlock(&nn->client_lock);
5457 nfs4_put_stid(&stp->st_stid);
5458 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 }
Jeff Layton217526e2014-07-30 08:27:11 -04005460 spin_unlock(&nn->client_lock);
5461
Jeff Layton7919d0a2016-09-16 16:28:25 -04005462 /*
5463 * It's possible for a client to try and acquire an already held lock
5464 * that is being held for a long time, and then lose interest in it.
5465 * So, we clean out any un-revisited request after a lease period
5466 * under the assumption that the client is no longer interested.
5467 *
5468 * RFC5661, sec. 9.6 states that the client must not rely on getting
5469 * notifications and must continue to poll for locks, even when the
5470 * server supports them. Thus this shouldn't lead to clients blocking
5471 * indefinitely once the lock does become free.
5472 */
5473 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04005474 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005475 while (!list_empty(&nn->blocked_locks_lru)) {
5476 nbl = list_first_entry(&nn->blocked_locks_lru,
5477 struct nfsd4_blocked_lock, nbl_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005478 if (nbl->nbl_time > cutoff) {
Jeff Layton7919d0a2016-09-16 16:28:25 -04005479 t = nbl->nbl_time - cutoff;
5480 new_timeo = min(new_timeo, t);
5481 break;
5482 }
5483 list_move(&nbl->nbl_lru, &reaplist);
5484 list_del_init(&nbl->nbl_list);
5485 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04005486 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005487
5488 while (!list_empty(&reaplist)) {
Naofumi Honda64ebe1242017-11-09 10:57:16 -05005489 nbl = list_first_entry(&reaplist,
Jeff Layton7919d0a2016-09-16 16:28:25 -04005490 struct nfsd4_blocked_lock, nbl_lru);
5491 list_del_init(&nbl->nbl_lru);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005492 free_blocked_lock(nbl);
5493 }
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005494out:
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005495 new_timeo = max_t(time64_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04005496 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497}
5498
Harvey Harrisona254b242008-02-20 12:49:00 -08005499static struct workqueue_struct *laundry_wq;
5500static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08005501
5502static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005503laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504{
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005505 time64_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08005506 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005507 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
5508 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005510 t = nfs4_laundromat(nn);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005511 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512}
5513
Jeff Layton8fcd4612015-07-30 06:57:46 -04005514static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07005515{
Jeff Layton8fcd4612015-07-30 06:57:46 -04005516 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005517 return nfserr_bad_stateid;
5518 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519}
5520
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04005522access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005524 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
5525 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
5526 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527}
5528
5529static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04005530access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005532 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
5533 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534}
5535
5536static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005537__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538{
Al Virob37ad282006-10-19 23:28:59 -07005539 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540
J. Bruce Fields02921912010-07-29 15:16:59 -04005541 /* For lock stateid's, we test the parent open, not the lock: */
5542 if (stp->st_openstp)
5543 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005544 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005546 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 goto out;
5548 status = nfs_ok;
5549out:
5550 return status;
5551}
5552
Al Virob37ad282006-10-19 23:28:59 -07005553static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005554check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005556 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005558 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005559 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 * conflicting state; so we must wait out the grace period. */
5561 return nfserr_grace;
5562 } else if (flags & WR_STATE)
5563 return nfs4_share_conflict(current_fh,
5564 NFS4_SHARE_DENY_WRITE);
5565 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
5566 return nfs4_share_conflict(current_fh,
5567 NFS4_SHARE_DENY_READ);
5568}
5569
5570/*
5571 * Allow READ/WRITE during grace period on recovered state only for files
5572 * that are not able to provide mandatory locking.
5573 */
5574static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005575grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005577 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578}
5579
J. Bruce Fields57b7b432012-04-25 17:58:50 -04005580static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05005581{
Andy Adamson66689582009-04-03 08:28:45 +03005582 /*
5583 * When sessions are used the stateid generation number is ignored
5584 * when it is zero.
5585 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04005586 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04005587 return nfs_ok;
5588
5589 if (in->si_generation == ref->si_generation)
5590 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03005591
J. Bruce Fields0836f582008-01-26 19:08:12 -05005592 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04005593 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05005594 return nfserr_bad_stateid;
5595 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04005596 * However, we could see a stateid from the past, even from a
5597 * non-buggy client. For example, if the client sends a lock
5598 * while some IO is outstanding, the lock may bump si_generation
5599 * while the IO is still in flight. The client could avoid that
5600 * situation by waiting for responses on all the IO requests,
5601 * but better performance may result in retrying IO that
5602 * receives an old_stateid error if requests are rarely
5603 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05005604 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04005605 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05005606}
5607
Trond Myklebust03da3162017-11-03 08:00:16 -04005608static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
5609{
5610 __be32 ret;
5611
5612 spin_lock(&s->sc_lock);
5613 ret = nfsd4_verify_open_stid(s);
5614 if (ret == nfs_ok)
5615 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
5616 spin_unlock(&s->sc_lock);
5617 return ret;
5618}
5619
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005620static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
5621{
5622 if (ols->st_stateowner->so_is_open_owner &&
5623 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5624 return nfserr_bad_stateid;
5625 return nfs_ok;
5626}
5627
Chuck Lever7df302f2012-05-29 13:56:37 -04005628static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04005629{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005630 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005631 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04005632
Andrew Elbleae254dac2017-11-09 13:41:10 -05005633 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5634 CLOSE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04005635 return status;
J. Bruce Fields663e36f2020-03-19 10:18:49 -04005636 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04005637 return status;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005638 spin_lock(&cl->cl_lock);
5639 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005640 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005641 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005642 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04005643 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005644 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04005645 switch (s->sc_type) {
5646 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005647 status = nfs_ok;
5648 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005649 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005650 status = nfserr_deleg_revoked;
5651 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005652 case NFS4_OPEN_STID:
5653 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005654 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04005655 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005656 default:
5657 printk("unknown stateid type %x\n", s->sc_type);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05005658 fallthrough;
J. Bruce Fields23340032013-04-09 17:42:28 -04005659 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005660 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005661 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04005662 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005663out_unlock:
5664 spin_unlock(&cl->cl_lock);
5665 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04005666}
5667
Christoph Hellwigcd61c522014-08-14 08:44:57 +02005668__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005669nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5670 stateid_t *stateid, unsigned char typemask,
5671 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005672{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005673 __be32 status;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005674 bool return_revoked = false;
5675
5676 /*
5677 * only return revoked delegations if explicitly asked.
5678 * otherwise we report revoked or bad_stateid status.
5679 */
5680 if (typemask & NFS4_REVOKED_DELEG_STID)
5681 return_revoked = true;
5682 else if (typemask & NFS4_DELEG_STID)
5683 typemask |= NFS4_REVOKED_DELEG_STID;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005684
Andrew Elbleae254dac2017-11-09 13:41:10 -05005685 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5686 CLOSE_STATEID(stateid))
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005687 return nfserr_bad_stateid;
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005688 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn,
5689 false);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005690 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005691 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005692 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005693 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005694 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005695 if (status)
5696 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005697 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005698 if (!*s)
5699 return nfserr_bad_stateid;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005700 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5701 nfs4_put_stid(*s);
5702 if (cstate->minorversion)
5703 return nfserr_deleg_revoked;
5704 return nfserr_bad_stateid;
5705 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005706 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005707}
5708
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005709static struct nfsd_file *
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005710nfs4_find_file(struct nfs4_stid *s, int flags)
5711{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005712 if (!s)
5713 return NULL;
5714
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005715 switch (s->sc_type) {
5716 case NFS4_DELEG_STID:
5717 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5718 return NULL;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005719 return nfsd_file_get(s->sc_file->fi_deleg_file);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005720 case NFS4_OPEN_STID:
5721 case NFS4_LOCK_STID:
5722 if (flags & RD_STATE)
5723 return find_readable_file(s->sc_file);
5724 else
5725 return find_writeable_file(s->sc_file);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005726 }
5727
5728 return NULL;
5729}
5730
5731static __be32
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005732nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005733{
5734 __be32 status;
5735
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005736 status = nfsd4_check_openowner_confirmed(ols);
5737 if (status)
5738 return status;
5739 return nfs4_check_openmode(ols, flags);
5740}
5741
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005742static __be32
5743nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
Jeff Layton5c4583b2019-08-18 14:18:54 -04005744 struct nfsd_file **nfp, int flags)
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005745{
5746 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005747 struct nfsd_file *nf;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005748 __be32 status;
5749
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005750 nf = nfs4_find_file(s, flags);
5751 if (nf) {
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005752 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5753 acc | NFSD_MAY_OWNER_OVERRIDE);
Jeff Layton5c4583b2019-08-18 14:18:54 -04005754 if (status) {
5755 nfsd_file_put(nf);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005756 goto out;
Jeff Layton5c4583b2019-08-18 14:18:54 -04005757 }
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005758 } else {
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005759 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005760 if (status)
5761 return status;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005762 }
Jeff Layton5c4583b2019-08-18 14:18:54 -04005763 *nfp = nf;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005764out:
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005765 return status;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005766}
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005767static void
5768_free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5769{
5770 WARN_ON_ONCE(cps->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID);
5771 if (!refcount_dec_and_test(&cps->cp_stateid.sc_count))
5772 return;
5773 list_del(&cps->cp_list);
5774 idr_remove(&nn->s2s_cp_stateids,
5775 cps->cp_stateid.stid.si_opaque.so_id);
5776 kfree(cps);
5777}
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005778/*
5779 * A READ from an inter server to server COPY will have a
5780 * copy stateid. Look up the copy notify stateid from the
5781 * idr structure and take a reference on it.
5782 */
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005783__be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5784 struct nfs4_client *clp,
5785 struct nfs4_cpntf_state **cps)
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005786{
5787 copy_stateid_t *cps_t;
5788 struct nfs4_cpntf_state *state = NULL;
5789
5790 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
5791 return nfserr_bad_stateid;
5792 spin_lock(&nn->s2s_cp_lock);
5793 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
5794 if (cps_t) {
5795 state = container_of(cps_t, struct nfs4_cpntf_state,
5796 cp_stateid);
Dan Carpenter5277a792019-12-04 10:59:36 +03005797 if (state->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID) {
5798 state = NULL;
5799 goto unlock;
5800 }
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005801 if (!clp)
5802 refcount_inc(&state->cp_stateid.sc_count);
5803 else
5804 _free_cpntf_state_locked(nn, state);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005805 }
Dan Carpenter5277a792019-12-04 10:59:36 +03005806unlock:
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005807 spin_unlock(&nn->s2s_cp_lock);
5808 if (!state)
5809 return nfserr_bad_stateid;
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005810 if (!clp && state)
5811 *cps = state;
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005812 return 0;
5813}
5814
5815static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5816 struct nfs4_stid **stid)
5817{
5818 __be32 status;
5819 struct nfs4_cpntf_state *cps = NULL;
5820 struct nfsd4_compound_state cstate;
5821
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005822 status = manage_cpntf_state(nn, st, NULL, &cps);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005823 if (status)
5824 return status;
5825
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005826 cps->cpntf_time = ktime_get_boottime_seconds();
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005827 memset(&cstate, 0, sizeof(cstate));
5828 status = lookup_clientid(&cps->cp_p_clid, &cstate, nn, true);
5829 if (status)
5830 goto out;
5831 status = nfsd4_lookup_stateid(&cstate, &cps->cp_p_stateid,
5832 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
5833 stid, nn);
5834 put_client_renew(cstate.clp);
5835out:
5836 nfs4_put_cpntf_state(nn, cps);
5837 return status;
5838}
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005839
5840void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5841{
5842 spin_lock(&nn->s2s_cp_lock);
5843 _free_cpntf_state_locked(nn, cps);
5844 spin_unlock(&nn->s2s_cp_lock);
5845}
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005846
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005848 * Checks for stateid operations
5849 */
Al Virob37ad282006-10-19 23:28:59 -07005850__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005851nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01005852 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005853 stateid_t *stateid, int flags, struct nfsd_file **nfp,
5854 struct nfs4_stid **cstid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005856 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005857 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005858 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005859 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005860 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861
Jeff Layton5c4583b2019-08-18 14:18:54 -04005862 if (nfp)
5863 *nfp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005865 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 return nfserr_grace;
5867
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005868 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5869 status = check_special_stateids(net, fhp, stateid, flags);
5870 goto done;
5871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005873 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04005874 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005875 &s, nn);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005876 if (status == nfserr_bad_stateid)
5877 status = find_cpntf_state(nn, stateid, &s);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005878 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04005879 return status;
Trond Myklebust03da3162017-11-03 08:00:16 -04005880 status = nfsd4_stid_check_stateid_generation(stateid, s,
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005881 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04005882 if (status)
5883 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005884
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005885 switch (s->sc_type) {
5886 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005887 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005888 break;
5889 case NFS4_OPEN_STID:
5890 case NFS4_LOCK_STID:
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005891 status = nfs4_check_olstateid(openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005892 break;
5893 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04005894 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005895 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04005897 if (status)
5898 goto out;
5899 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005900
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005901done:
Jeff Layton5c4583b2019-08-18 14:18:54 -04005902 if (status == nfs_ok && nfp)
5903 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904out:
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005905 if (s) {
5906 if (!status && cstid)
5907 *cstid = s;
5908 else
5909 nfs4_put_stid(s);
5910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 return status;
5912}
5913
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005914/*
Bryan Schumaker17456802011-07-13 10:50:48 -04005915 * Test if the stateid is valid
5916 */
5917__be32
5918nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005919 union nfsd4_op_u *u)
Bryan Schumaker17456802011-07-13 10:50:48 -04005920{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005921 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005922 struct nfsd4_test_stateid_id *stateid;
5923 struct nfs4_client *cl = cstate->session->se_client;
5924
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005925 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04005926 stateid->ts_id_status =
5927 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005928
Bryan Schumaker17456802011-07-13 10:50:48 -04005929 return nfs_ok;
5930}
5931
Chuck Lever42691392016-08-11 10:37:30 -04005932static __be32
5933nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5934{
5935 struct nfs4_ol_stateid *stp = openlockstateid(s);
5936 __be32 ret;
5937
Trond Myklebust659aefb2017-11-03 08:00:13 -04005938 ret = nfsd4_lock_ol_stateid(stp);
5939 if (ret)
5940 goto out_put_stid;
Chuck Lever42691392016-08-11 10:37:30 -04005941
5942 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5943 if (ret)
5944 goto out;
5945
5946 ret = nfserr_locks_held;
5947 if (check_for_locks(stp->st_stid.sc_file,
5948 lockowner(stp->st_stateowner)))
5949 goto out;
5950
5951 release_lock_stateid(stp);
5952 ret = nfs_ok;
5953
5954out:
5955 mutex_unlock(&stp->st_mutex);
Trond Myklebust659aefb2017-11-03 08:00:13 -04005956out_put_stid:
Chuck Lever42691392016-08-11 10:37:30 -04005957 nfs4_put_stid(s);
5958 return ret;
5959}
5960
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005961__be32
5962nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005963 union nfsd4_op_u *u)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005964{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005965 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005966 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005967 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005968 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005969 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005970 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005971
Jeff Layton1af71cc2014-07-29 21:34:14 -04005972 spin_lock(&cl->cl_lock);
5973 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005974 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005975 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005976 spin_lock(&s->sc_lock);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005977 switch (s->sc_type) {
5978 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005979 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005980 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005981 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005982 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5983 if (ret)
5984 break;
5985 ret = nfserr_locks_held;
5986 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005987 case NFS4_LOCK_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005988 spin_unlock(&s->sc_lock);
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03005989 refcount_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005990 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04005991 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005992 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005993 case NFS4_REVOKED_DELEG_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005994 spin_unlock(&s->sc_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005995 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005996 list_del_init(&dp->dl_recall_lru);
5997 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04005998 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005999 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04006000 goto out;
6001 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006002 }
Trond Myklebust03da3162017-11-03 08:00:16 -04006003 spin_unlock(&s->sc_lock);
Jeff Layton1af71cc2014-07-29 21:34:14 -04006004out_unlock:
6005 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006006out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006007 return ret;
6008}
6009
NeilBrown4c4cd222005-07-07 17:59:27 -07006010static inline int
6011setlkflg (int type)
6012{
6013 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
6014 RD_STATE : WR_STATE;
6015}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006017static __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 -04006018{
6019 struct svc_fh *current_fh = &cstate->current_fh;
6020 struct nfs4_stateowner *sop = stp->st_stateowner;
6021 __be32 status;
6022
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006023 status = nfsd4_check_seqid(cstate, sop, seqid);
6024 if (status)
6025 return status;
Trond Myklebust9271d7e2017-11-03 08:00:15 -04006026 status = nfsd4_lock_ol_stateid(stp);
6027 if (status != nfs_ok)
6028 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006029 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04006030 if (status == nfs_ok)
6031 status = nfs4_check_fh(current_fh, &stp->st_stid);
6032 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006033 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04006034 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006035}
6036
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037/*
6038 * Checks for sequence id mutating operations.
6039 */
Al Virob37ad282006-10-19 23:28:59 -07006040static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03006041nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04006042 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006043 struct nfs4_ol_stateid **stpp,
6044 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045{
J. Bruce Fields0836f582008-01-26 19:08:12 -05006046 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006047 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006048 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04006050 trace_nfsd_preprocess(seqid, stateid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07006051
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006053 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006054 if (status)
6055 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006056 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04006057 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006059 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04006060 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006061 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04006062 else
6063 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006064 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006065}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05006066
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006067static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6068 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006069{
6070 __be32 status;
6071 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006072 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006074 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006075 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04006076 if (status)
6077 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006078 oo = openowner(stp->st_stateowner);
6079 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006080 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006081 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07006082 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006083 }
6084 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07006085 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086}
6087
Al Virob37ad282006-10-19 23:28:59 -07006088__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006089nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006090 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006092 struct nfsd4_open_confirm *oc = &u->open_confirm;
Al Virob37ad282006-10-19 23:28:59 -07006093 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006094 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006095 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006096 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097
Al Viroa6a9f182013-09-16 10:57:01 -04006098 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
6099 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006101 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07006102 if (status)
6103 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006105 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006106 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006107 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006108 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006109 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006110 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006111 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04006112 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006113 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04006114 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04006115 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04006116 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04006117 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006118 mutex_unlock(&stp->st_mutex);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04006119 trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
Jeff Layton2a4317c2012-03-21 16:42:43 -04006120 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006121 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04006122put_stateid:
6123 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006125 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126 return status;
6127}
6128
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006129static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130{
Jeff Layton82c5ff12012-05-11 09:45:13 -04006131 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006132 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04006133 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006134 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006135}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04006136
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006137static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
6138{
6139 switch (to_access) {
6140 case NFS4_SHARE_ACCESS_READ:
6141 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
6142 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6143 break;
6144 case NFS4_SHARE_ACCESS_WRITE:
6145 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
6146 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6147 break;
6148 case NFS4_SHARE_ACCESS_BOTH:
6149 break;
6150 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006151 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152 }
6153}
6154
Al Virob37ad282006-10-19 23:28:59 -07006155__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006156nfsd4_open_downgrade(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006157 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006159 struct nfsd4_open_downgrade *od = &u->open_downgrade;
Al Virob37ad282006-10-19 23:28:59 -07006160 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006161 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006162 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163
Al Viroa6a9f182013-09-16 10:57:01 -04006164 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
6165 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04006167 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02006168 if (od->od_deleg_want)
6169 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
6170 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006172 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006173 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006174 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04006177 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04006178 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006180 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04006182 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04006183 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006185 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006187 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04006188 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04006189 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006191put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006192 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006193 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006195 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 return status;
6197}
6198
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006199static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
6200{
Trond Myklebustacf92952014-06-30 11:48:37 -04006201 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04006202 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04006203 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04006204
Jeff Layton2c41beb2014-07-29 21:34:41 -04006205 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04006206 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04006207
Jeff Laytond83017f2014-07-29 21:34:42 -04006208 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04006209 if (unhashed)
6210 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04006211 spin_unlock(&clp->cl_lock);
6212 free_ol_stateid_reaplist(&reaplist);
6213 } else {
6214 spin_unlock(&clp->cl_lock);
6215 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04006216 if (unhashed)
6217 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04006218 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04006219}
6220
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221/*
6222 * nfs4_unlock_state() called after encode
6223 */
Al Virob37ad282006-10-19 23:28:59 -07006224__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006225nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006226 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006228 struct nfsd4_close *close = &u->close;
Al Virob37ad282006-10-19 23:28:59 -07006229 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006230 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006231 struct net *net = SVC_NET(rqstp);
6232 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233
Al Viroa6a9f182013-09-16 10:57:01 -04006234 dprintk("NFSD: nfsd4_close on file %pd\n",
6235 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006237 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
6238 &close->cl_stateid,
6239 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006240 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006241 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006242 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04006244
6245 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Laytonbd2deca2018-02-15 15:16:18 -05006246
6247 /*
6248 * Technically we don't _really_ have to increment or copy it, since
6249 * it should just be gone after this operation and we clobber the
6250 * copied value below, but we continue to do so here just to ensure
6251 * that racing ops see that there was a state change.
6252 */
Jeff Layton9767feb2015-10-01 09:05:50 -04006253 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006255 nfsd4_close_open_stateid(stp);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04006256 mutex_unlock(&stp->st_mutex);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04006257
Jeff Laytonbd2deca2018-02-15 15:16:18 -05006258 /* v4.1+ suggests that we send a special stateid in here, since the
6259 * clients should just ignore this anyway. Since this is not useful
6260 * for v4.0 clients either, we set it to the special close_stateid
6261 * universally.
6262 *
6263 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
6264 */
6265 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
Trond Myklebustfb500a72017-11-03 08:00:12 -04006266
Trond Myklebust8a0b5892014-07-29 21:34:20 -04006267 /* put reference from nfs4_preprocess_seqid_op */
6268 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270 return status;
6271}
6272
Al Virob37ad282006-10-19 23:28:59 -07006273__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006274nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006275 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006277 struct nfsd4_delegreturn *dr = &u->delegreturn;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006278 struct nfs4_delegation *dp;
6279 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006280 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07006281 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006282 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006284 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006285 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006287 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006288 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006289 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006290 dp = delegstateid(s);
Trond Myklebust03da3162017-11-03 08:00:16 -04006291 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006292 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04006293 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006294
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006295 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04006296put_stateid:
6297 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298out:
6299 return status;
6300}
6301
Benny Halevy87df4de2008-12-15 19:42:03 +02006302static inline u64
6303end_offset(u64 start, u64 len)
6304{
6305 u64 end;
6306
6307 end = start + len;
6308 return end >= start ? end: NFS4_MAX_UINT64;
6309}
6310
6311/* last octet in a range */
6312static inline u64
6313last_byte_offset(u64 start, u64 len)
6314{
6315 u64 end;
6316
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006317 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02006318 end = start + len;
6319 return end > start ? end - 1: NFS4_MAX_UINT64;
6320}
6321
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322/*
6323 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6324 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6325 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6326 * locking, this prevents us from being completely protocol-compliant. The
6327 * real solution to this problem is to start using unsigned file offsets in
6328 * the VFS, but this is a very deep change!
6329 */
6330static inline void
6331nfs4_transform_lock_offset(struct file_lock *lock)
6332{
6333 if (lock->fl_start < 0)
6334 lock->fl_start = OFFSET_MAX;
6335 if (lock->fl_end < 0)
6336 lock->fl_end = OFFSET_MAX;
6337}
6338
Jeff Laytoncae80b32015-04-03 09:04:04 -04006339static fl_owner_t
6340nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006341{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006342 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6343
6344 nfs4_get_stateowner(&lo->lo_owner);
6345 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006346}
6347
Jeff Laytoncae80b32015-04-03 09:04:04 -04006348static void
6349nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006350{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006351 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006352
Jeff Laytoncae80b32015-04-03 09:04:04 -04006353 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04006354 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04006355}
6356
Jeff Layton76d348f2016-09-16 16:28:24 -04006357static void
6358nfsd4_lm_notify(struct file_lock *fl)
6359{
6360 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
6361 struct net *net = lo->lo_owner.so_client->net;
6362 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6363 struct nfsd4_blocked_lock *nbl = container_of(fl,
6364 struct nfsd4_blocked_lock, nbl_lock);
6365 bool queue = false;
6366
Jeff Layton7919d0a2016-09-16 16:28:25 -04006367 /* An empty list means that something else is going to be using it */
Jeff Layton0cc11a62016-10-20 09:34:31 -04006368 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006369 if (!list_empty(&nbl->nbl_list)) {
6370 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006371 list_del_init(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -04006372 queue = true;
6373 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04006374 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006375
6376 if (queue)
6377 nfsd4_run_cb(&nbl->nbl_cb);
6378}
6379
Alexey Dobriyan7b021962009-09-21 17:01:12 -07006380static const struct lock_manager_operations nfsd_posix_mng_ops = {
Jeff Layton76d348f2016-09-16 16:28:24 -04006381 .lm_notify = nfsd4_lm_notify,
Kinglong Meeaef95832014-08-22 10:18:44 -04006382 .lm_get_owner = nfsd4_fl_get_owner,
6383 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07006384};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385
6386static inline void
6387nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
6388{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006389 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390
NeilBrownd5b90262006-04-10 22:55:22 -07006391 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006392 lo = (struct nfs4_lockowner *) fl->fl_owner;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04006393 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
6394 GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006395 if (!deny->ld_owner.data)
6396 /* We just don't care that much */
6397 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006398 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07006399 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006400nevermind:
6401 deny->ld_owner.len = 0;
6402 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07006403 deny->ld_clientid.cl_boot = 0;
6404 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405 }
6406 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02006407 deny->ld_length = NFS4_MAX_UINT64;
6408 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 deny->ld_length = fl->fl_end - fl->fl_start + 1;
6410 deny->ld_type = NFS4_READ_LT;
6411 if (fl->fl_type != F_RDLCK)
6412 deny->ld_type = NFS4_WRITE_LT;
6413}
6414
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006415static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006416find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417{
Trond Myklebustd4f04892014-07-29 21:34:36 -04006418 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006419 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420
Trond Myklebust0a880a22014-07-30 08:27:10 -04006421 lockdep_assert_held(&clp->cl_lock);
6422
Trond Myklebustd4f04892014-07-29 21:34:36 -04006423 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
6424 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006425 if (so->so_is_open_owner)
6426 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006427 if (same_owner_str(so, owner))
6428 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429 }
6430 return NULL;
6431}
6432
Trond Myklebustc58c6612014-07-29 21:34:35 -04006433static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006434find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04006435{
6436 struct nfs4_lockowner *lo;
6437
Trond Myklebustd4f04892014-07-29 21:34:36 -04006438 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006439 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04006440 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006441 return lo;
6442}
6443
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006444static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
6445{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006446 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006447}
6448
Jeff Layton6b180f02014-07-29 21:34:26 -04006449static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
6450{
6451 struct nfs4_lockowner *lo = lockowner(sop);
6452
6453 kmem_cache_free(lockowner_slab, lo);
6454}
6455
6456static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006457 .so_unhash = nfs4_unhash_lockowner,
6458 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04006459};
6460
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461/*
6462 * Alloc a lock owner structure.
6463 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006464 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006466 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006468static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04006469alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
6470 struct nfs4_ol_stateid *open_stp,
6471 struct nfsd4_lock *lock)
6472{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006473 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006475 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
6476 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006478 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006479 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
6480 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04006481 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04006482 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006483 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006484 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006485 if (ret == NULL) {
6486 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04006487 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006488 ret = lo;
6489 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08006490 nfs4_free_stateowner(&lo->lo_owner);
6491
Trond Myklebustd4f04892014-07-29 21:34:36 -04006492 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04006493 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494}
6495
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006496static struct nfs4_ol_stateid *
Trond Myklebusta451b122020-03-01 18:21:38 -05006497find_lock_stateid(const struct nfs4_lockowner *lo,
6498 const struct nfs4_ol_stateid *ost)
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006499{
6500 struct nfs4_ol_stateid *lst;
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006501
Trond Myklebusta451b122020-03-01 18:21:38 -05006502 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006503
Trond Myklebusta451b122020-03-01 18:21:38 -05006504 /* If ost is not hashed, ost->st_locks will not be valid */
6505 if (!nfs4_ol_stateid_unhashed(ost))
6506 list_for_each_entry(lst, &ost->st_locks, st_locks) {
6507 if (lst->st_stateowner == &lo->lo_owner) {
6508 refcount_inc(&lst->st_stid.sc_count);
6509 return lst;
6510 }
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006511 }
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006512 return NULL;
6513}
6514
Trond Myklebustbeeca192017-11-03 08:00:14 -04006515static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006516init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
6517 struct nfs4_file *fp, struct inode *inode,
6518 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006519{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04006520 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustbeeca192017-11-03 08:00:14 -04006521 struct nfs4_ol_stateid *retstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522
Trond Myklebustbeeca192017-11-03 08:00:14 -04006523 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05006524 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006525retry:
6526 spin_lock(&clp->cl_lock);
Trond Myklebusta451b122020-03-01 18:21:38 -05006527 if (nfs4_ol_stateid_unhashed(open_stp))
6528 goto out_close;
6529 retstp = find_lock_stateid(lo, open_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006530 if (retstp)
Trond Myklebusta451b122020-03-01 18:21:38 -05006531 goto out_found;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03006532 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05006533 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006534 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07006535 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04006536 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006537 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07006539 stp->st_openstp = open_stp;
Trond Myklebusta451b122020-03-01 18:21:38 -05006540 spin_lock(&fp->fi_lock);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006541 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04006542 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04006543 list_add(&stp->st_perfile, &fp->fi_stateids);
6544 spin_unlock(&fp->fi_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006545 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006546 return stp;
Trond Myklebusta451b122020-03-01 18:21:38 -05006547out_found:
6548 spin_unlock(&clp->cl_lock);
6549 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
6550 nfs4_put_stid(&retstp->st_stid);
6551 goto retry;
6552 }
6553 /* To keep mutex tracking happy */
6554 mutex_unlock(&stp->st_mutex);
6555 return retstp;
6556out_close:
6557 spin_unlock(&clp->cl_lock);
6558 mutex_unlock(&stp->st_mutex);
6559 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560}
6561
Jeff Laytonc53530d2014-06-30 11:48:39 -04006562static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006563find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
6564 struct inode *inode, struct nfs4_ol_stateid *ost,
6565 bool *new)
6566{
6567 struct nfs4_stid *ns = NULL;
6568 struct nfs4_ol_stateid *lst;
6569 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6570 struct nfs4_client *clp = oo->oo_owner.so_client;
6571
Trond Myklebustbeeca192017-11-03 08:00:14 -04006572 *new = false;
Jeff Layton356a95e2014-07-29 21:34:13 -04006573 spin_lock(&clp->cl_lock);
Trond Myklebusta451b122020-03-01 18:21:38 -05006574 lst = find_lock_stateid(lo, ost);
Jeff Layton356a95e2014-07-29 21:34:13 -04006575 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006576 if (lst != NULL) {
6577 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
6578 goto out;
6579 nfs4_put_stid(&lst->st_stid);
6580 }
6581 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
6582 if (ns == NULL)
6583 return NULL;
6584
6585 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
6586 if (lst == openlockstateid(ns))
6587 *new = true;
6588 else
Jeff Layton356a95e2014-07-29 21:34:13 -04006589 nfs4_put_stid(ns);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006590out:
Jeff Layton356a95e2014-07-29 21:34:13 -04006591 return lst;
6592}
Jeff Laytonc53530d2014-06-30 11:48:39 -04006593
NeilBrownfd39ca92005-06-23 22:04:03 -07006594static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595check_lock_length(u64 offset, u64 length)
6596{
Kinglong Meee7969312015-07-13 17:34:19 +08006597 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
6598 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599}
6600
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006601static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006602{
Trond Myklebust11b91642014-07-29 21:34:08 -04006603 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006604
Jeff Layton7214e862014-07-10 14:07:33 -04006605 lockdep_assert_held(&fp->fi_lock);
6606
Jeff Layton82c5ff12012-05-11 09:45:13 -04006607 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05006608 return;
Jeff Layton12659652014-07-10 14:07:28 -04006609 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006610 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006611}
6612
Jeff Layton356a95e2014-07-29 21:34:13 -04006613static __be32
6614lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
6615 struct nfs4_ol_stateid *ost,
6616 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04006617 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006618{
Jeff Layton5db1c032014-07-29 21:34:28 -04006619 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04006620 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006621 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6622 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00006623 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006624 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04006625 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006626 unsigned int strhashval;
6627
Kinglong Meec8623992015-07-13 17:35:37 +08006628 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006629 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006630 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006631 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
6632 if (lo == NULL)
6633 return nfserr_jukebox;
6634 } else {
6635 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04006636 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04006637 if (!cstate->minorversion &&
6638 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04006639 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006640 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04006641
Jeff Laytondd257932016-08-11 10:37:39 -04006642 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
6643 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04006644 status = nfserr_jukebox;
6645 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006646 }
Jeff Laytondd257932016-08-11 10:37:39 -04006647
Jeff Layton5db1c032014-07-29 21:34:28 -04006648 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04006649 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04006650out:
6651 nfs4_put_stateowner(&lo->lo_owner);
6652 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006653}
6654
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655/*
6656 * LOCK operation
6657 */
Al Virob37ad282006-10-19 23:28:59 -07006658__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006659nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006660 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006661{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006662 struct nfsd4_lock *lock = &u->lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006663 struct nfs4_openowner *open_sop = NULL;
6664 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006665 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006666 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04006667 struct nfs4_file *fp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006668 struct nfsd_file *nf = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006669 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006670 struct file_lock *file_lock = NULL;
6671 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006672 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006673 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07006674 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04006675 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04006676 unsigned char fl_type;
6677 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006678 struct net *net = SVC_NET(rqstp);
6679 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680
6681 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
6682 (long long) lock->lk_offset,
6683 (long long) lock->lk_length);
6684
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 if (check_lock_length(lock->lk_offset, lock->lk_length))
6686 return nfserr_inval;
6687
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006688 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02006689 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08006690 dprintk("NFSD: nfsd4_lock: permission denied!\n");
6691 return status;
6692 }
6693
Linus Torvalds1da177e2005-04-16 15:20:36 -07006694 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04006695 if (nfsd4_has_session(cstate))
6696 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006697 memcpy(&lock->lk_new_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04006698 &cstate->session->se_client->cl_clientid,
6699 sizeof(clientid_t));
6700
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006702 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006706 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707 lock->lk_new_open_seqid,
6708 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006709 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07006710 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006712 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006713 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006714 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04006715 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006716 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006717 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006718 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04006719 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006720 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03006721 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006722 lock->lk_old_lock_seqid,
6723 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006724 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006725 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04006726 if (status)
6727 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006728 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006730 lkflg = setlkflg(lock->lk_type);
6731 status = nfs4_check_openmode(lock_stp, lkflg);
6732 if (status)
6733 goto out;
6734
NeilBrown0dd395d2005-07-07 17:59:15 -07006735 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006736 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006737 goto out;
6738 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006739 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006740 goto out;
6741
Trond Myklebust11b91642014-07-29 21:34:08 -04006742 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744 case NFS4_READW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006745 if (nfsd4_has_session(cstate))
6746 fl_flags |= FL_SLEEP;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006747 fallthrough;
Jeff Layton76d348f2016-09-16 16:28:24 -04006748 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006749 spin_lock(&fp->fi_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006750 nf = find_readable_file_locked(fp);
6751 if (nf)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006752 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04006753 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006754 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006755 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 case NFS4_WRITEW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006757 if (nfsd4_has_session(cstate))
6758 fl_flags |= FL_SLEEP;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006759 fallthrough;
Jeff Layton76d348f2016-09-16 16:28:24 -04006760 case NFS4_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006761 spin_lock(&fp->fi_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006762 nf = find_writeable_file_locked(fp);
6763 if (nf)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006764 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04006765 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006766 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006767 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 default:
6769 status = nfserr_inval;
6770 goto out;
6771 }
Jeff Layton76d348f2016-09-16 16:28:24 -04006772
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006773 if (!nf) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006774 status = nfserr_openmode;
6775 goto out;
6776 }
Kinglong Meeaef95832014-08-22 10:18:44 -04006777
Jeff Layton76d348f2016-09-16 16:28:24 -04006778 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6779 if (!nbl) {
6780 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6781 status = nfserr_jukebox;
6782 goto out;
6783 }
6784
6785 file_lock = &nbl->nbl_lock;
6786 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04006787 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04006788 file_lock->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006789 file_lock->fl_file = nf->nf_file;
Jeff Layton76d348f2016-09-16 16:28:24 -04006790 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04006791 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6792 file_lock->fl_start = lock->lk_offset;
6793 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6794 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795
Jeff Layton21179d82012-08-21 08:03:32 -04006796 conflock = locks_alloc_lock();
6797 if (!conflock) {
6798 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6799 status = nfserr_jukebox;
6800 goto out;
6801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802
Jeff Layton76d348f2016-09-16 16:28:24 -04006803 if (fl_flags & FL_SLEEP) {
Arnd Bergmann20b7d862019-11-04 16:31:52 +01006804 nbl->nbl_time = ktime_get_boottime_seconds();
Jeff Layton0cc11a62016-10-20 09:34:31 -04006805 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006806 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006807 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006808 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006809 }
6810
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006811 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006812 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04006814 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07006815 status = 0;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04006816 if (lock->lk_reclaim)
6817 nn->somebody_reclaimed = true;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006818 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006819 case FILE_LOCK_DEFERRED:
6820 nbl = NULL;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006821 fallthrough;
Jeff Layton76d348f2016-09-16 16:28:24 -04006822 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006823 status = nfserr_denied;
6824 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04006825 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006826 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006827 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006829 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04006830 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05006831 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04006832 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006833 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835out:
Jeff Layton76d348f2016-09-16 16:28:24 -04006836 if (nbl) {
6837 /* dequeue it if we queued it before */
6838 if (fl_flags & FL_SLEEP) {
Jeff Layton0cc11a62016-10-20 09:34:31 -04006839 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006840 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006841 list_del_init(&nbl->nbl_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006842 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006843 }
6844 free_blocked_lock(nbl);
6845 }
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006846 if (nf)
6847 nfsd_file_put(nf);
Jeff Layton5db1c032014-07-29 21:34:28 -04006848 if (lock_stp) {
6849 /* Bump seqid manually if the 4.0 replay owner is openowner */
6850 if (cstate->replay_owner &&
6851 cstate->replay_owner != &lock_sop->lo_owner &&
6852 seqid_mutating_err(ntohl(status)))
6853 lock_sop->lo_owner.so_seqid++;
6854
6855 /*
6856 * If this is a new, never-before-used stateid, and we are
6857 * returning an error, then just go ahead and release it.
6858 */
J. Bruce Fields25020722018-01-17 16:25:59 -05006859 if (status && new)
Jeff Layton5db1c032014-07-29 21:34:28 -04006860 release_lock_stateid(lock_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006861
6862 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton5db1c032014-07-29 21:34:28 -04006863
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006864 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04006865 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006866 if (open_stp)
6867 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006868 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006869 if (conflock)
6870 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871 return status;
6872}
6873
6874/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006875 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6876 * so we do a temporary open here just to get an open file to pass to
6877 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6878 * inode operation.)
6879 */
Al Viro04da6e92012-04-13 00:00:04 -04006880static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006881{
Jeff Layton6b556ca2019-08-18 14:18:55 -04006882 struct nfsd_file *nf;
6883 __be32 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
Al Viro04da6e92012-04-13 00:00:04 -04006884 if (!err) {
Jeff Layton6b556ca2019-08-18 14:18:55 -04006885 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
6886 nfsd_file_put(nf);
Al Viro04da6e92012-04-13 00:00:04 -04006887 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006888 return err;
6889}
6890
6891/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892 * LOCKT operation
6893 */
Al Virob37ad282006-10-19 23:28:59 -07006894__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006895nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006896 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006898 struct nfsd4_lockt *lockt = &u->lockt;
Jeff Layton21179d82012-08-21 08:03:32 -04006899 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04006900 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006901 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006902 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006904 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 return nfserr_grace;
6906
6907 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6908 return nfserr_inval;
6909
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006910 if (!nfsd4_has_session(cstate)) {
Olga Kornievskaiab7342202019-09-06 15:17:21 -04006911 status = lookup_clientid(&lockt->lt_clientid, cstate, nn,
6912 false);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006913 if (status)
6914 goto out;
6915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916
J. Bruce Fields75c096f2011-08-15 18:39:32 -04006917 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919
Jeff Layton21179d82012-08-21 08:03:32 -04006920 file_lock = locks_alloc_lock();
6921 if (!file_lock) {
6922 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6923 status = nfserr_jukebox;
6924 goto out;
6925 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006926
Linus Torvalds1da177e2005-04-16 15:20:36 -07006927 switch (lockt->lt_type) {
6928 case NFS4_READ_LT:
6929 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006930 file_lock->fl_type = F_RDLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006931 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932 case NFS4_WRITE_LT:
6933 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006934 file_lock->fl_type = F_WRLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006935 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04006937 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938 status = nfserr_inval;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006939 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940 }
6941
Kinglong Meec8623992015-07-13 17:35:37 +08006942 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006943 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04006944 file_lock->fl_owner = (fl_owner_t)lo;
6945 file_lock->fl_pid = current->tgid;
6946 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947
Jeff Layton21179d82012-08-21 08:03:32 -04006948 file_lock->fl_start = lockt->lt_offset;
6949 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950
Jeff Layton21179d82012-08-21 08:03:32 -04006951 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952
Jeff Layton21179d82012-08-21 08:03:32 -04006953 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04006954 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05006955 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04006956
Jeff Layton21179d82012-08-21 08:03:32 -04006957 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04006959 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960 }
6961out:
Jeff Layton5db1c032014-07-29 21:34:28 -04006962 if (lo)
6963 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04006964 if (file_lock)
6965 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966 return status;
6967}
6968
Al Virob37ad282006-10-19 23:28:59 -07006969__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006970nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006971 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006973 struct nfsd4_locku *locku = &u->locku;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006974 struct nfs4_ol_stateid *stp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006975 struct nfsd_file *nf = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006976 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006977 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07006978 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006979 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6980
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6982 (long long) locku->lu_offset,
6983 (long long) locku->lu_length);
6984
6985 if (check_lock_length(locku->lu_offset, locku->lu_length))
6986 return nfserr_inval;
6987
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006988 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006989 &locku->lu_stateid, NFS4_LOCK_STID,
6990 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006991 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992 goto out;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006993 nf = find_any_file(stp->st_stid.sc_file);
6994 if (!nf) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006995 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04006996 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006997 }
Jeff Layton21179d82012-08-21 08:03:32 -04006998 file_lock = locks_alloc_lock();
6999 if (!file_lock) {
7000 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7001 status = nfserr_jukebox;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007002 goto put_file;
Jeff Layton21179d82012-08-21 08:03:32 -04007003 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08007004
Jeff Layton21179d82012-08-21 08:03:32 -04007005 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04007006 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04007007 file_lock->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007008 file_lock->fl_file = nf->nf_file;
Jeff Layton21179d82012-08-21 08:03:32 -04007009 file_lock->fl_flags = FL_POSIX;
7010 file_lock->fl_lmops = &nfsd_posix_mng_ops;
7011 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012
Jeff Layton21179d82012-08-21 08:03:32 -04007013 file_lock->fl_end = last_byte_offset(locku->lu_offset,
7014 locku->lu_length);
7015 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007017 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07007018 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05007019 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007020 goto out_nfserr;
7021 }
Jeff Layton9767feb2015-10-01 09:05:50 -04007022 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007023put_file:
7024 nfsd_file_put(nf);
Trond Myklebust858cc572014-07-29 21:34:16 -04007025put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04007026 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04007027 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04007029 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04007030 if (file_lock)
7031 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032 return status;
7033
7034out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07007035 status = nfserrno(err);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007036 goto put_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037}
7038
7039/*
7040 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007041 * true: locks held by lockowner
7042 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007044static bool
7045check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007046{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007047 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007048 int status = false;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007049 struct nfsd_file *nf = find_any_file(fp);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007050 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007051 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007052
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007053 if (!nf) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007054 /* Any valid lock stateid should have some sort of access */
7055 WARN_ON_ONCE(1);
7056 return status;
7057 }
7058
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007059 inode = locks_inode(nf->nf_file);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007060 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007062 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05007063 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007064 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
7065 if (fl->fl_owner == (fl_owner_t)lowner) {
7066 status = true;
7067 break;
7068 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08007069 }
Jeff Layton6109c852015-01-16 15:05:57 -05007070 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071 }
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007072 nfsd_file_put(nf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073 return status;
7074}
7075
Al Virob37ad282006-10-19 23:28:59 -07007076__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08007077nfsd4_release_lockowner(struct svc_rqst *rqstp,
7078 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02007079 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080{
Christoph Hellwigeb698532017-05-08 20:58:35 +02007081 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04007083 struct nfs4_stateowner *sop;
7084 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04007085 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007086 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04007087 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07007088 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03007089 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04007090 struct nfs4_client *clp;
Chuck Lever88584812016-07-13 16:40:14 -04007091 LIST_HEAD (reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092
7093 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
7094 clid->cl_boot, clid->cl_id);
7095
Olga Kornievskaiab7342202019-09-06 15:17:21 -04007096 status = lookup_clientid(clid, cstate, nn, false);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05007097 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04007098 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05007099
Trond Myklebustd4f04892014-07-29 21:34:36 -04007100 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04007101 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04007102 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04007103 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04007104 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04007105
7106 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05007107 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04007108
Jeff Layton882e9d22014-07-29 21:34:37 -04007109 /* see if there are still any locks associated with it */
7110 lo = lockowner(sop);
7111 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
7112 if (check_for_locks(stp->st_stid.sc_file, lo)) {
7113 status = nfserr_locks_held;
7114 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04007115 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04007116 }
Jeff Layton5adfd882014-07-29 21:34:31 -04007117 }
Jeff Layton882e9d22014-07-29 21:34:37 -04007118
Kinglong Meeb5971af2014-08-22 10:18:43 -04007119 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04007120 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04007121 }
Chuck Lever88584812016-07-13 16:40:14 -04007122 if (!lo) {
7123 spin_unlock(&clp->cl_lock);
7124 return status;
7125 }
7126
7127 unhash_lockowner_locked(lo);
7128 while (!list_empty(&lo->lo_owner.so_stateids)) {
7129 stp = list_first_entry(&lo->lo_owner.so_stateids,
7130 struct nfs4_ol_stateid,
7131 st_perstateowner);
7132 WARN_ON(!unhash_lock_stateid(stp));
7133 put_ol_stateid_locked(stp, &reaplist);
7134 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04007135 spin_unlock(&clp->cl_lock);
Chuck Lever88584812016-07-13 16:40:14 -04007136 free_ol_stateid_reaplist(&reaplist);
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007137 remove_blocked_locks(lo);
Chuck Lever88584812016-07-13 16:40:14 -04007138 nfs4_put_stateowner(&lo->lo_owner);
7139
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140 return status;
7141}
7142
7143static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07007144alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145{
NeilBrowna55370a2005-06-23 22:03:52 -07007146 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147}
7148
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007149bool
Scott Mayhew6b189102019-03-26 18:06:26 -04007150nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07007151{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007152 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07007153
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007154 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007155 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07007156}
7157
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158/*
7159 * failure => all reset bets are off, nfserr_no_grace...
Scott Mayhew6b189102019-03-26 18:06:26 -04007160 *
7161 * The caller is responsible for freeing name.data if NULL is returned (it
7162 * will be freed in nfs4_remove_reclaim_record in the normal case).
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05007164struct nfs4_client_reclaim *
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007165nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
7166 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167{
7168 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05007169 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04007171 trace_nfsd_clid_reclaim(nn, name.len, name.data);
NeilBrowna55370a2005-06-23 22:03:52 -07007172 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05007173 if (crp) {
7174 strhashval = clientstr_hashval(name);
7175 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007176 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Scott Mayhew6b189102019-03-26 18:06:26 -04007177 crp->cr_name.data = name.data;
7178 crp->cr_name.len = name.len;
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007179 crp->cr_princhash.data = princhash.data;
7180 crp->cr_princhash.len = princhash.len;
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007181 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007182 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05007183 }
7184 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185}
7186
Jeff Layton2a4317c2012-03-21 16:42:43 -04007187void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007188nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05007189{
7190 list_del(&crp->cr_strhash);
Scott Mayhew6b189102019-03-26 18:06:26 -04007191 kfree(crp->cr_name.data);
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007192 kfree(crp->cr_princhash.data);
Jeff Laytonce30e532012-11-12 15:00:53 -05007193 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007194 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05007195}
7196
7197void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007198nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199{
7200 struct nfs4_client_reclaim *crp = NULL;
7201 int i;
7202
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007204 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
7205 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007207 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208 }
7209 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05007210 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211}
7212
7213/*
7214 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04007215struct nfs4_client_reclaim *
Scott Mayhew6b189102019-03-26 18:06:26 -04007216nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217{
7218 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219 struct nfs4_client_reclaim *crp = NULL;
7220
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04007221 trace_nfsd_clid_find(nn, name.len, name.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007222
Scott Mayhew6b189102019-03-26 18:06:26 -04007223 strhashval = clientstr_hashval(name);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007224 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Scott Mayhew6b189102019-03-26 18:06:26 -04007225 if (compare_blob(&crp->cr_name, &name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226 return crp;
7227 }
7228 }
7229 return NULL;
7230}
7231
7232/*
7233* Called from OPEN. Look for clientid in reclaim list.
7234*/
Al Virob37ad282006-10-19 23:28:59 -07007235__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04007236nfs4_check_open_reclaim(clientid_t *clid,
7237 struct nfsd4_compound_state *cstate,
7238 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04007240 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04007241
7242 /* find clientid in conf_id_hashtbl */
Olga Kornievskaiab7342202019-09-06 15:17:21 -04007243 status = lookup_clientid(clid, cstate, nn, false);
Trond Myklebust0fe492d2014-06-30 11:48:47 -04007244 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04007245 return nfserr_reclaim_bad;
7246
Jeff Layton3b3e7b72014-09-12 16:40:22 -04007247 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
7248 return nfserr_no_grace;
7249
Trond Myklebust0fe492d2014-06-30 11:48:47 -04007250 if (nfsd4_client_record_check(cstate->clp))
7251 return nfserr_reclaim_bad;
7252
7253 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007254}
7255
Meelap Shahc2f1a552007-07-17 04:04:39 -07007256/*
7257 * Since the lifetime of a delegation isn't limited to that of an open, a
7258 * client may quite reasonably hang on to a delegation as long as it has
7259 * the inode cached. This becomes an obvious problem the first time a
7260 * client's inode cache approaches the size of the server's total memory.
7261 *
7262 * For now we avoid this problem by imposing a hard limit on the number
7263 * of delegations, which varies according to the server's memory size.
7264 */
7265static void
7266set_max_delegations(void)
7267{
7268 /*
7269 * Allow at most 4 delegations per megabyte of RAM. Quick
7270 * estimates suggest that in the worst case (where every delegation
7271 * is for a different inode), a delegation could take about 1.5K,
7272 * giving a worst case usage of about 6% of memory.
7273 */
7274 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7275}
7276
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007277static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007278{
7279 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7280 int i;
7281
Kees Cook6da2ec52018-06-12 13:55:00 -07007282 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7283 sizeof(struct list_head),
7284 GFP_KERNEL);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007285 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007286 goto err;
Kees Cook6da2ec52018-06-12 13:55:00 -07007287 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7288 sizeof(struct list_head),
7289 GFP_KERNEL);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007290 if (!nn->unconf_id_hashtbl)
7291 goto err_unconf_id;
Kees Cook6da2ec52018-06-12 13:55:00 -07007292 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7293 sizeof(struct list_head),
7294 GFP_KERNEL);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007295 if (!nn->sessionid_hashtbl)
7296 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007297
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007298 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007299 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007300 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007301 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007302 for (i = 0; i < SESSION_HASH_SIZE; i++)
7303 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007304 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007305 nn->unconf_name_tree = RB_ROOT;
Arnd Bergmann9cc76802019-10-31 15:53:13 +01007306 nn->boot_time = ktime_get_real_seconds();
Vasily Averin81833de2017-11-13 07:25:40 +03007307 nn->grace_ended = false;
7308 nn->nfsd4_manager.block_opens = true;
7309 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03007310 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03007311 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007312 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03007313 spin_lock_init(&nn->client_lock);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04007314 spin_lock_init(&nn->s2s_cp_lock);
7315 idr_init(&nn->s2s_cp_stateids);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007316
Jeff Layton0cc11a62016-10-20 09:34:31 -04007317 spin_lock_init(&nn->blocked_locks_lock);
7318 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7319
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007320 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007321 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007322
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007323 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007324
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007325err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03007326 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007327err_unconf_id:
7328 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007329err:
7330 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007331}
7332
7333static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007334nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007335{
7336 int i;
7337 struct nfs4_client *clp = NULL;
7338 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7339
7340 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7341 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7342 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7343 destroy_client(clp);
7344 }
7345 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007346
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007347 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7348
Kinglong Mee2b905632014-03-26 22:09:30 +08007349 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7350 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7351 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7352 destroy_client(clp);
7353 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007354 }
7355
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007356 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007357 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007358 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007359 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007360}
7361
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007362int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007363nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07007364{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04007365 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007366 int ret;
7367
J. Bruce Fields681370f2020-06-23 16:00:33 -04007368 ret = get_nfsdfs(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007369 if (ret)
7370 return ret;
J. Bruce Fields681370f2020-06-23 16:00:33 -04007371 ret = nfs4_state_create_net(net);
7372 if (ret) {
7373 mntput(nn->nfsd_mnt);
7374 return ret;
7375 }
Jeff Laytond4318ac2014-09-12 16:40:21 -04007376 locks_start_grace(net, &nn->nfsd4_manager);
7377 nfsd4_client_tracking_init(net);
Scott Mayhew362063a2019-03-26 18:06:28 -04007378 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7379 goto skip_grace;
Arnd Bergmann20b7d862019-11-04 16:31:52 +01007380 printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
Vasily Averin7e981a82017-11-06 16:46:04 +03007381 nn->nfsd4_grace, net->ns.inum);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04007382 trace_nfsd_grace_start(nn);
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03007383 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007384 return 0;
Scott Mayhew362063a2019-03-26 18:06:28 -04007385
7386skip_grace:
7387 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7388 net->ns.inum);
7389 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7390 nfsd4_end_grace(nn);
7391 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007392}
7393
7394/* initialization to perform when the nfsd service is started: */
7395
7396int
7397nfs4_state_start(void)
7398{
7399 int ret;
7400
Jeff Layton51a54562015-08-20 07:17:01 -04007401 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05007402 if (laundry_wq == NULL) {
7403 ret = -ENOMEM;
Chuck Levera26dd642018-08-16 12:06:09 -04007404 goto out;
Jeff Laytona6d6b782012-03-05 11:42:36 -05007405 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007406 ret = nfsd4_create_callback_queue();
7407 if (ret)
7408 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007409
Meelap Shahc2f1a552007-07-17 04:04:39 -07007410 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007411 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007412
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007413out_free_laundry:
7414 destroy_workqueue(laundry_wq);
Chuck Levera26dd642018-08-16 12:06:09 -04007415out:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007416 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007417}
7418
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007419void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007420nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007422 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007424 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007426 cancel_delayed_work_sync(&nn->laundromat_work);
7427 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05007428
Linus Torvalds1da177e2005-04-16 15:20:36 -07007429 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04007430 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007431 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007432 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007433 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007434 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007435 }
Benny Halevycdc975052014-05-30 09:09:30 -04007436 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007437 list_for_each_safe(pos, next, &reaplist) {
7438 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04007439 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05007440 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441 }
7442
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007443 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007444 nfs4_state_destroy_net(net);
J. Bruce Fields681370f2020-06-23 16:00:33 -04007445 mntput(nn->nfsd_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446}
7447
7448void
7449nfs4_state_shutdown(void)
7450{
NeilBrown5e8d5c22006-04-10 22:55:37 -07007451 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04007452 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007454
7455static void
7456get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7457{
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007458 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
7459 CURRENT_STATEID(stateid))
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007460 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007461}
7462
7463static void
7464put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7465{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007466 if (cstate->minorversion) {
7467 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007468 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007469 }
7470}
7471
7472void
7473clear_current_stateid(struct nfsd4_compound_state *cstate)
7474{
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007475 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007476}
7477
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007478/*
7479 * functions to set current state id
7480 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007481void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007482nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7483 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007484{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007485 put_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007486}
7487
7488void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007489nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7490 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007491{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007492 put_stateid(cstate, &u->open.op_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007493}
7494
7495void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007496nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7497 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007498{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007499 put_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007500}
7501
7502void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007503nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7504 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007505{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007506 put_stateid(cstate, &u->lock.lk_resp_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007507}
7508
7509/*
7510 * functions to consume current state id
7511 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007512
7513void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007514nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7515 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007516{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007517 get_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007518}
7519
7520void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007521nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7522 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007523{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007524 get_stateid(cstate, &u->delegreturn.dr_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007525}
7526
7527void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007528nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7529 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007530{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007531 get_stateid(cstate, &u->free_stateid.fr_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007532}
7533
7534void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007535nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7536 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007537{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007538 get_stateid(cstate, &u->setattr.sa_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007539}
7540
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007541void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007542nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7543 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007544{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007545 get_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007546}
7547
7548void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007549nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7550 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007551{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007552 get_stateid(cstate, &u->locku.lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007553}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007554
7555void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007556nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7557 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007558{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007559 get_stateid(cstate, &u->read.rd_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007560}
7561
7562void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007563nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7564 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007565{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007566 get_stateid(cstate, &u->write.wr_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007567}