blob: 32063733443d4ed93d39b43fb0e5f5ced1c0d5bf [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>
NeilBrown472d1552021-03-20 09:38:04 +110046#include <linux/fsnotify.h>
Dai Ngof4e44b32021-05-21 15:09:37 -040047#include <linux/nfs_ssc.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020048#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040049#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050050#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040051#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040053#include "netns.h"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020054#include "pnfs.h"
Jeff Laytonfd4f83f2019-08-18 14:18:52 -040055#include "filecache.h"
Chuck Leverdd5e3fb2020-04-05 11:42:29 -040056#include "trace.h"
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define NFSDDBG_FACILITY NFSDDBG_PROC
59
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050060#define all_ones {{~0,~0},~0}
61static const stateid_t one_stateid = {
62 .si_generation = ~0,
63 .si_opaque = all_ones,
64};
65static const stateid_t zero_stateid = {
66 /* all fields zero */
67};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010068static const stateid_t currentstateid = {
69 .si_generation = 1,
70};
Trond Myklebustfb500a72017-11-03 08:00:12 -040071static const stateid_t close_stateid = {
72 .si_generation = 0xffffffffU,
73};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050074
Andy Adamsonec6b5d72009-04-03 08:28:28 +030075static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070076
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050077#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
78#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010079#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Andrew Elbleae254dac2017-11-09 13:41:10 -050080#define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040083static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040084static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Scott Mayhew362063a2019-03-26 18:06:28 -040085void nfsd4_end_grace(struct nfsd_net *nn);
Olga Kornievskaia624322f2019-10-04 16:34:26 -040086static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
J. Bruce Fields8b671b82009-02-22 14:51:34 -080088/* Locking: */
89
J. Bruce Fields8b671b82009-02-22 14:51:34 -080090/*
91 * Currently used for the del_recall_lru and file hash table. In an
92 * effort to decrease the scope of the client_mutex, this spinlock may
93 * eventually cover more:
94 */
Benny Halevycdc975052014-05-30 09:09:30 -040095static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080096
Andrew Elble4f34bd02017-11-08 17:29:51 -050097enum nfsd4_st_mutex_lock_subclass {
98 OPEN_STATEID_MUTEX = 0,
99 LOCK_STATEID_MUTEX = 1,
100};
101
Jeff Laytonb401be222014-07-29 21:34:33 -0400102/*
103 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
104 * the refcount on the open stateid to drop.
105 */
106static DECLARE_WAIT_QUEUE_HEAD(close_wq);
107
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400108/*
109 * A waitqueue where a writer to clients/#/ctl destroying a client can
110 * wait for cl_rpc_users to drop to 0 and then for the client to be
111 * unhashed.
112 */
113static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
114
Jeff Layton9258a2d2018-03-16 09:47:22 -0400115static struct kmem_cache *client_slab;
Christoph Hellwigabf11352014-05-21 07:43:03 -0700116static struct kmem_cache *openowner_slab;
117static struct kmem_cache *lockowner_slab;
118static struct kmem_cache *file_slab;
119static struct kmem_cache *stateid_slab;
120static struct kmem_cache *deleg_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +0200121static struct kmem_cache *odstate_slab;
NeilBrowne60d4392005-06-23 22:03:01 -0700122
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400123static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800124
Julia Lawallc4cb8972015-11-21 22:57:39 +0100125static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
Jeff Layton76d348f2016-09-16 16:28:24 -0400126static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200127
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400128static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800129{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400130 return ses->se_flags & NFS4_SESSION_DEAD;
131}
132
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400133static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
134{
135 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400136 return nfserr_jukebox;
137 ses->se_flags |= NFS4_SESSION_DEAD;
138 return nfs_ok;
139}
140
J. Bruce Fields221a6872013-04-01 22:23:49 -0400141static bool is_client_expired(struct nfs4_client *clp)
142{
143 return clp->cl_time == 0;
144}
145
J. Bruce Fields221a6872013-04-01 22:23:49 -0400146static __be32 get_client_locked(struct nfs4_client *clp)
147{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400148 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
149
150 lockdep_assert_held(&nn->client_lock);
151
J. Bruce Fields221a6872013-04-01 22:23:49 -0400152 if (is_client_expired(clp))
153 return nfserr_expired;
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400154 atomic_inc(&clp->cl_rpc_users);
J. Bruce Fields221a6872013-04-01 22:23:49 -0400155 return nfs_ok;
156}
157
158/* must be called under the client_lock */
159static inline void
160renew_client_locked(struct nfs4_client *clp)
161{
162 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
163
164 if (is_client_expired(clp)) {
165 WARN_ON(1);
166 printk("%s: client (clientid %08x/%08x) already expired\n",
167 __func__,
168 clp->cl_clientid.cl_boot,
169 clp->cl_clientid.cl_id);
170 return;
171 }
172
J. Bruce Fields221a6872013-04-01 22:23:49 -0400173 list_move_tail(&clp->cl_lru, &nn->client_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +0100174 clp->cl_time = ktime_get_boottime_seconds();
J. Bruce Fields221a6872013-04-01 22:23:49 -0400175}
176
Fengguang Wuba138432013-04-16 22:14:15 -0400177static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400178{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400179 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
180
181 lockdep_assert_held(&nn->client_lock);
182
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400183 if (!atomic_dec_and_test(&clp->cl_rpc_users))
J. Bruce Fields221a6872013-04-01 22:23:49 -0400184 return;
185 if (!is_client_expired(clp))
186 renew_client_locked(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400187 else
188 wake_up_all(&expiry_wq);
J. Bruce Fields221a6872013-04-01 22:23:49 -0400189}
190
Jeff Layton4b24ca72014-06-30 11:48:44 -0400191static void put_client_renew(struct nfs4_client *clp)
192{
193 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
194
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400195 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
Jeff Laytond6c249b2014-07-08 14:02:50 -0400196 return;
197 if (!is_client_expired(clp))
198 renew_client_locked(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400199 else
200 wake_up_all(&expiry_wq);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400201 spin_unlock(&nn->client_lock);
202}
203
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400204static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
205{
206 __be32 status;
207
208 if (is_session_dead(ses))
209 return nfserr_badsession;
210 status = get_client_locked(ses->se_client);
211 if (status)
212 return status;
213 atomic_inc(&ses->se_ref);
214 return nfs_ok;
215}
216
217static void nfsd4_put_session_locked(struct nfsd4_session *ses)
218{
219 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400220 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
221
222 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400223
224 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
225 free_session(ses);
226 put_client_renew_locked(clp);
227}
228
229static void nfsd4_put_session(struct nfsd4_session *ses)
230{
231 struct nfs4_client *clp = ses->se_client;
232 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
233
234 spin_lock(&nn->client_lock);
235 nfsd4_put_session_locked(ses);
236 spin_unlock(&nn->client_lock);
237}
238
Jeff Layton76d348f2016-09-16 16:28:24 -0400239static struct nfsd4_blocked_lock *
240find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
241 struct nfsd_net *nn)
242{
243 struct nfsd4_blocked_lock *cur, *found = NULL;
244
Jeff Layton0cc11a62016-10-20 09:34:31 -0400245 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400246 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
247 if (fh_match(fh, &cur->nbl_fh)) {
248 list_del_init(&cur->nbl_list);
Vasily Averin47446d72021-12-17 09:49:39 +0300249 WARN_ON(list_empty(&cur->nbl_lru));
Jeff Layton7919d0a2016-09-16 16:28:25 -0400250 list_del_init(&cur->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400251 found = cur;
252 break;
253 }
254 }
Jeff Layton0cc11a62016-10-20 09:34:31 -0400255 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400256 if (found)
NeilBrowncb03f942018-11-30 10:04:08 +1100257 locks_delete_block(&found->nbl_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400258 return found;
259}
260
261static struct nfsd4_blocked_lock *
262find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
263 struct nfsd_net *nn)
264{
265 struct nfsd4_blocked_lock *nbl;
266
267 nbl = find_blocked_lock(lo, fh, nn);
268 if (!nbl) {
269 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
270 if (nbl) {
Vasily Averine1e83992020-03-27 07:50:40 +0300271 INIT_LIST_HEAD(&nbl->nbl_list);
272 INIT_LIST_HEAD(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400273 fh_copy_shallow(&nbl->nbl_fh, fh);
274 locks_init_lock(&nbl->nbl_lock);
Vasily Averin47446d72021-12-17 09:49:39 +0300275 kref_init(&nbl->nbl_kref);
Jeff Layton76d348f2016-09-16 16:28:24 -0400276 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
277 &nfsd4_cb_notify_lock_ops,
278 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
279 }
280 }
281 return nbl;
282}
283
284static void
Vasily Averin47446d72021-12-17 09:49:39 +0300285free_nbl(struct kref *kref)
286{
287 struct nfsd4_blocked_lock *nbl;
288
289 nbl = container_of(kref, struct nfsd4_blocked_lock, nbl_kref);
290 kfree(nbl);
291}
292
293static void
Jeff Layton76d348f2016-09-16 16:28:24 -0400294free_blocked_lock(struct nfsd4_blocked_lock *nbl)
295{
Jeff Layton6aaafc42019-04-22 12:34:23 -0400296 locks_delete_block(&nbl->nbl_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400297 locks_release_private(&nbl->nbl_lock);
Vasily Averin47446d72021-12-17 09:49:39 +0300298 kref_put(&nbl->nbl_kref, free_nbl);
Jeff Layton76d348f2016-09-16 16:28:24 -0400299}
300
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400301static void
302remove_blocked_locks(struct nfs4_lockowner *lo)
303{
304 struct nfs4_client *clp = lo->lo_owner.so_client;
305 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
306 struct nfsd4_blocked_lock *nbl;
307 LIST_HEAD(reaplist);
308
309 /* Dequeue all blocked locks */
310 spin_lock(&nn->blocked_locks_lock);
311 while (!list_empty(&lo->lo_blocked)) {
312 nbl = list_first_entry(&lo->lo_blocked,
313 struct nfsd4_blocked_lock,
314 nbl_list);
315 list_del_init(&nbl->nbl_list);
Vasily Averin47446d72021-12-17 09:49:39 +0300316 WARN_ON(list_empty(&nbl->nbl_lru));
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400317 list_move(&nbl->nbl_lru, &reaplist);
318 }
319 spin_unlock(&nn->blocked_locks_lock);
320
321 /* Now free them */
322 while (!list_empty(&reaplist)) {
323 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
324 nbl_lru);
325 list_del_init(&nbl->nbl_lru);
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400326 free_blocked_lock(nbl);
327 }
328}
329
Jeff Laytonf4564582019-04-22 12:34:24 -0400330static void
331nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
332{
333 struct nfsd4_blocked_lock *nbl = container_of(cb,
334 struct nfsd4_blocked_lock, nbl_cb);
335 locks_delete_block(&nbl->nbl_lock);
336}
337
Jeff Layton76d348f2016-09-16 16:28:24 -0400338static int
339nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
340{
341 /*
342 * Since this is just an optimization, we don't try very hard if it
343 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
344 * just quit trying on anything else.
345 */
346 switch (task->tk_status) {
347 case -NFS4ERR_DELAY:
348 rpc_delay(task, 1 * HZ);
349 return 0;
350 default:
351 return 1;
352 }
353}
354
355static void
356nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
357{
358 struct nfsd4_blocked_lock *nbl = container_of(cb,
359 struct nfsd4_blocked_lock, nbl_cb);
360
361 free_blocked_lock(nbl);
362}
363
364static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
Jeff Laytonf4564582019-04-22 12:34:24 -0400365 .prepare = nfsd4_cb_notify_lock_prepare,
Jeff Layton76d348f2016-09-16 16:28:24 -0400366 .done = nfsd4_cb_notify_lock_done,
367 .release = nfsd4_cb_notify_lock_release,
368};
369
J. Bruce Fieldsebd9d2c2021-04-16 14:00:17 -0400370/*
371 * We store the NONE, READ, WRITE, and BOTH bits separately in the
372 * st_{access,deny}_bmap field of the stateid, in order to track not
373 * only what share bits are currently in force, but also what
374 * combinations of share bits previous opens have used. This allows us
J. Bruce Fields3dcd1d82021-12-07 17:32:21 -0500375 * to enforce the recommendation in
376 * https://datatracker.ietf.org/doc/html/rfc7530#section-16.19.4 that
377 * the server return an error if the client attempt to downgrade to a
378 * combination of share bits not explicable by closing some of its
379 * previous opens.
J. Bruce Fieldsebd9d2c2021-04-16 14:00:17 -0400380 *
J. Bruce Fields3dcd1d82021-12-07 17:32:21 -0500381 * This enforcement is arguably incomplete, since we don't keep
J. Bruce Fieldsebd9d2c2021-04-16 14:00:17 -0400382 * track of access/deny bit combinations; so, e.g., we allow:
383 *
384 * OPEN allow read, deny write
385 * OPEN allow both, deny none
386 * DOWNGRADE allow read, deny none
387 *
388 * which we should reject.
J. Bruce Fields3dcd1d82021-12-07 17:32:21 -0500389 *
390 * But you could also argue that our current code is already overkill,
391 * since it only exists to return NFS4ERR_INVAL on incorrect client
392 * behavior.
J. Bruce Fieldsebd9d2c2021-04-16 14:00:17 -0400393 */
394static unsigned int
395bmap_to_share_mode(unsigned long bmap)
396{
397 int i;
398 unsigned int access = 0;
399
400 for (i = 1; i < 4; i++) {
401 if (test_bit(i, &bmap))
402 access |= i;
403 }
404 return access;
405}
406
407/* set share access for a given stateid */
408static inline void
409set_access(u32 access, struct nfs4_ol_stateid *stp)
410{
411 unsigned char mask = 1 << access;
412
413 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
414 stp->st_access_bmap |= mask;
415}
416
417/* clear share access for a given stateid */
418static inline void
419clear_access(u32 access, struct nfs4_ol_stateid *stp)
420{
421 unsigned char mask = 1 << access;
422
423 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
424 stp->st_access_bmap &= ~mask;
425}
426
427/* test whether a given stateid has access */
428static inline bool
429test_access(u32 access, struct nfs4_ol_stateid *stp)
430{
431 unsigned char mask = 1 << access;
432
433 return (bool)(stp->st_access_bmap & mask);
434}
435
436/* set share deny for a given stateid */
437static inline void
438set_deny(u32 deny, struct nfs4_ol_stateid *stp)
439{
440 unsigned char mask = 1 << deny;
441
442 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
443 stp->st_deny_bmap |= mask;
444}
445
446/* clear share deny for a given stateid */
447static inline void
448clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
449{
450 unsigned char mask = 1 << deny;
451
452 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
453 stp->st_deny_bmap &= ~mask;
454}
455
456/* test whether a given stateid is denying specific access */
457static inline bool
458test_deny(u32 deny, struct nfs4_ol_stateid *stp)
459{
460 unsigned char mask = 1 << deny;
461
462 return (bool)(stp->st_deny_bmap & mask);
463}
464
465static int nfs4_access_to_omode(u32 access)
466{
467 switch (access & NFS4_SHARE_ACCESS_BOTH) {
468 case NFS4_SHARE_ACCESS_READ:
469 return O_RDONLY;
470 case NFS4_SHARE_ACCESS_WRITE:
471 return O_WRONLY;
472 case NFS4_SHARE_ACCESS_BOTH:
473 return O_RDWR;
474 }
475 WARN_ON_ONCE(1);
476 return O_RDONLY;
477}
478
479static inline int
480access_permit_read(struct nfs4_ol_stateid *stp)
481{
482 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
483 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
484 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
485}
486
487static inline int
488access_permit_write(struct nfs4_ol_stateid *stp)
489{
490 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
491 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
492}
493
Kinglong Meeb5971af2014-08-22 10:18:43 -0400494static inline struct nfs4_stateowner *
495nfs4_get_stateowner(struct nfs4_stateowner *sop)
496{
497 atomic_inc(&sop->so_count);
498 return sop;
499}
500
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400501static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400502same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400503{
504 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400505 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400506}
507
508static struct nfs4_openowner *
509find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400510 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400511{
512 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400513
Trond Myklebustd4f04892014-07-29 21:34:36 -0400514 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400515
Trond Myklebustd4f04892014-07-29 21:34:36 -0400516 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
517 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400518 if (!so->so_is_open_owner)
519 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400520 if (same_owner_str(so, &open->op_owner))
521 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400522 }
523 return NULL;
524}
525
526static struct nfs4_openowner *
527find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400528 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400529{
530 struct nfs4_openowner *oo;
531
Trond Myklebustd4f04892014-07-29 21:34:36 -0400532 spin_lock(&clp->cl_lock);
533 oo = find_openstateowner_str_locked(hashval, open, clp);
534 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400535 return oo;
536}
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538static inline u32
539opaque_hashval(const void *ptr, int nbytes)
540{
541 unsigned char *cptr = (unsigned char *) ptr;
542
543 u32 x = 0;
544 while (nbytes--) {
545 x *= 37;
546 x += *cptr++;
547 }
548 return x;
549}
550
Jeff Layton5b095e92014-10-23 08:01:02 -0400551static void nfsd4_free_file_rcu(struct rcu_head *rcu)
J. Bruce Fields32513b42011-10-13 16:00:16 -0400552{
Jeff Layton5b095e92014-10-23 08:01:02 -0400553 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
554
555 kmem_cache_free(file_slab, fp);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400556}
557
Christoph Hellwige6ba76e2014-08-14 08:50:16 +0200558void
NeilBrown13cd2182005-06-23 22:03:10 -0700559put_nfs4_file(struct nfs4_file *fi)
560{
Jeff Layton02e12152014-07-16 10:31:57 -0400561 might_lock(&state_lock);
562
Elena Reshetova818a34e2017-10-20 12:53:30 +0300563 if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton5b095e92014-10-23 08:01:02 -0400564 hlist_del_rcu(&fi->fi_hash);
Benny Halevycdc975052014-05-30 09:09:30 -0400565 spin_unlock(&state_lock);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200566 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
Jeff Layton5b095e92014-10-23 08:01:02 -0400567 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
568 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800569 }
NeilBrown13cd2182005-06-23 22:03:10 -0700570}
571
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400572static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400573__nfs4_get_fd(struct nfs4_file *f, int oflag)
574{
575 if (f->fi_fds[oflag])
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400576 return nfsd_file_get(f->fi_fds[oflag]);
Trond Myklebustde186432014-07-10 14:07:26 -0400577 return NULL;
578}
579
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400580static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400581find_writeable_file_locked(struct nfs4_file *f)
582{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400583 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400584
585 lockdep_assert_held(&f->fi_lock);
586
587 ret = __nfs4_get_fd(f, O_WRONLY);
588 if (!ret)
589 ret = __nfs4_get_fd(f, O_RDWR);
590 return ret;
591}
592
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400593static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400594find_writeable_file(struct nfs4_file *f)
595{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400596 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400597
598 spin_lock(&f->fi_lock);
599 ret = find_writeable_file_locked(f);
600 spin_unlock(&f->fi_lock);
601
602 return ret;
603}
604
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400605static struct nfsd_file *
606find_readable_file_locked(struct nfs4_file *f)
Trond Myklebustde186432014-07-10 14:07:26 -0400607{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400608 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400609
610 lockdep_assert_held(&f->fi_lock);
611
612 ret = __nfs4_get_fd(f, O_RDONLY);
613 if (!ret)
614 ret = __nfs4_get_fd(f, O_RDWR);
615 return ret;
616}
617
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400618static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400619find_readable_file(struct nfs4_file *f)
620{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400621 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400622
623 spin_lock(&f->fi_lock);
624 ret = find_readable_file_locked(f);
625 spin_unlock(&f->fi_lock);
626
627 return ret;
628}
629
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400630struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400631find_any_file(struct nfs4_file *f)
632{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400633 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400634
Trond Myklebusta451b122020-03-01 18:21:38 -0500635 if (!f)
636 return NULL;
Trond Myklebustde186432014-07-10 14:07:26 -0400637 spin_lock(&f->fi_lock);
638 ret = __nfs4_get_fd(f, O_RDWR);
639 if (!ret) {
640 ret = __nfs4_get_fd(f, O_WRONLY);
641 if (!ret)
642 ret = __nfs4_get_fd(f, O_RDONLY);
643 }
644 spin_unlock(&f->fi_lock);
645 return ret;
646}
647
J. Bruce Fields9affa432020-07-15 13:31:36 -0400648static struct nfsd_file *find_deleg_file(struct nfs4_file *f)
649{
650 struct nfsd_file *ret = NULL;
651
652 spin_lock(&f->fi_lock);
653 if (f->fi_deleg_file)
654 ret = nfsd_file_get(f->fi_deleg_file);
655 spin_unlock(&f->fi_lock);
656 return ret;
657}
658
Trond Myklebust02a35082014-07-25 07:34:22 -0400659static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800660unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700661
662/*
663 * Open owner state (share locks)
664 */
665
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500666/* hash tables for lock and open owners */
667#define OWNER_HASH_BITS 8
668#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
669#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700670
Trond Myklebustd4f04892014-07-29 21:34:36 -0400671static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400672{
673 unsigned int ret;
674
675 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500676 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400677}
NeilBrownef0f3392006-04-10 22:55:41 -0700678
NeilBrownef0f3392006-04-10 22:55:41 -0700679/* hash table for nfs4_file */
680#define FILE_HASH_BITS 8
681#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800682
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -0400683static unsigned int file_hashval(struct svc_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400684{
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -0400685 struct inode *inode = d_inode(fh->fh_dentry);
Trond Myklebustca943212014-07-23 16:17:39 -0400686
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -0400687 /* XXX: why not (here & in file cache) use inode? */
688 return (unsigned int)hash_long(inode->i_ino, FILE_HASH_BITS);
Trond Myklebustca943212014-07-23 16:17:39 -0400689}
690
Jeff Layton89876f82013-04-02 09:01:59 -0400691static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700692
Jeff Layton12659652014-07-10 14:07:28 -0400693static void
694__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400695{
Jeff Layton7214e862014-07-10 14:07:33 -0400696 lockdep_assert_held(&fp->fi_lock);
697
Jeff Layton12659652014-07-10 14:07:28 -0400698 if (access & NFS4_SHARE_ACCESS_WRITE)
699 atomic_inc(&fp->fi_access[O_WRONLY]);
700 if (access & NFS4_SHARE_ACCESS_READ)
701 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400702}
703
Jeff Layton12659652014-07-10 14:07:28 -0400704static __be32
705nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400706{
Jeff Layton7214e862014-07-10 14:07:33 -0400707 lockdep_assert_held(&fp->fi_lock);
708
Jeff Layton12659652014-07-10 14:07:28 -0400709 /* Does this access mode make sense? */
710 if (access & ~NFS4_SHARE_ACCESS_BOTH)
711 return nfserr_inval;
712
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400713 /* Does it conflict with a deny mode already set? */
714 if ((access & fp->fi_share_deny) != 0)
715 return nfserr_share_denied;
716
Jeff Layton12659652014-07-10 14:07:28 -0400717 __nfs4_file_get_access(fp, access);
718 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400719}
720
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400721static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
722{
723 /* Common case is that there is no deny mode. */
724 if (deny) {
725 /* Does this deny mode make sense? */
726 if (deny & ~NFS4_SHARE_DENY_BOTH)
727 return nfserr_inval;
728
729 if ((deny & NFS4_SHARE_DENY_READ) &&
730 atomic_read(&fp->fi_access[O_RDONLY]))
731 return nfserr_share_denied;
732
733 if ((deny & NFS4_SHARE_DENY_WRITE) &&
734 atomic_read(&fp->fi_access[O_WRONLY]))
735 return nfserr_share_denied;
736 }
737 return nfs_ok;
738}
739
J. Bruce Fields998db522010-08-07 09:21:41 -0400740static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400741{
Trond Myklebustde186432014-07-10 14:07:26 -0400742 might_lock(&fp->fi_lock);
743
744 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400745 struct nfsd_file *f1 = NULL;
746 struct nfsd_file *f2 = NULL;
Trond Myklebustde186432014-07-10 14:07:26 -0400747
Jeff Layton6d338b52014-07-10 14:07:29 -0400748 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400749 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400750 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400751 spin_unlock(&fp->fi_lock);
752 if (f1)
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400753 nfsd_file_put(f1);
Trond Myklebustde186432014-07-10 14:07:26 -0400754 if (f2)
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400755 nfsd_file_put(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400756 }
757}
758
Jeff Layton12659652014-07-10 14:07:28 -0400759static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400760{
Jeff Layton12659652014-07-10 14:07:28 -0400761 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
762
763 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400764 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400765 if (access & NFS4_SHARE_ACCESS_READ)
766 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400767}
768
Sachin Bhamare8287f002015-04-27 14:50:14 +0200769/*
770 * Allocate a new open/delegation state counter. This is needed for
771 * pNFS for proper return on close semantics.
772 *
773 * Note that we only allocate it for pNFS-enabled exports, otherwise
774 * all pointers to struct nfs4_clnt_odstate are always NULL.
775 */
776static struct nfs4_clnt_odstate *
777alloc_clnt_odstate(struct nfs4_client *clp)
778{
779 struct nfs4_clnt_odstate *co;
780
781 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
782 if (co) {
783 co->co_client = clp;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300784 refcount_set(&co->co_odcount, 1);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200785 }
786 return co;
787}
788
789static void
790hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
791{
792 struct nfs4_file *fp = co->co_file;
793
794 lockdep_assert_held(&fp->fi_lock);
795 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
796}
797
798static inline void
799get_clnt_odstate(struct nfs4_clnt_odstate *co)
800{
801 if (co)
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300802 refcount_inc(&co->co_odcount);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200803}
804
805static void
806put_clnt_odstate(struct nfs4_clnt_odstate *co)
807{
808 struct nfs4_file *fp;
809
810 if (!co)
811 return;
812
813 fp = co->co_file;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300814 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
Sachin Bhamare8287f002015-04-27 14:50:14 +0200815 list_del(&co->co_perfile);
816 spin_unlock(&fp->fi_lock);
817
818 nfsd4_return_all_file_layouts(co->co_client, fp);
819 kmem_cache_free(odstate_slab, co);
820 }
821}
822
823static struct nfs4_clnt_odstate *
824find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
825{
826 struct nfs4_clnt_odstate *co;
827 struct nfs4_client *cl;
828
829 if (!new)
830 return NULL;
831
832 cl = new->co_client;
833
834 spin_lock(&fp->fi_lock);
835 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
836 if (co->co_client == cl) {
837 get_clnt_odstate(co);
838 goto out;
839 }
840 }
841 co = new;
842 co->co_file = fp;
843 hash_clnt_odstate_locked(new);
844out:
845 spin_unlock(&fp->fi_lock);
846 return co;
847}
848
Kinglong Meed19fb702017-01-18 19:04:42 +0800849struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
850 void (*sc_free)(struct nfs4_stid *))
J. Bruce Fields996e0932011-10-17 11:14:48 -0400851{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500852 struct nfs4_stid *stid;
853 int new_id;
854
Trond Myklebustf8338832014-07-25 07:34:19 -0400855 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500856 if (!stid)
857 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400858
Jeff Layton4770d722014-07-29 21:34:10 -0400859 idr_preload(GFP_KERNEL);
860 spin_lock(&cl->cl_lock);
J. Bruce Fields78599c42019-04-22 15:26:23 -0400861 /* Reserving 0 for start of file in nfsdfs "states" file: */
862 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
Jeff Layton4770d722014-07-29 21:34:10 -0400863 spin_unlock(&cl->cl_lock);
864 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700865 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500866 goto out_free;
Kinglong Meed19fb702017-01-18 19:04:42 +0800867
868 stid->sc_free = sc_free;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500869 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500870 stid->sc_stateid.si_opaque.so_id = new_id;
871 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
872 /* Will be incremented before return to client: */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300873 refcount_set(&stid->sc_count, 1);
Jeff Layton9767feb2015-10-01 09:05:50 -0400874 spin_lock_init(&stid->sc_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400875 INIT_LIST_HEAD(&stid->sc_cp_list);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500876
J. Bruce Fields996e0932011-10-17 11:14:48 -0400877 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500878 * It shouldn't be a problem to reuse an opaque stateid value.
879 * I don't think it is for 4.1. But with 4.0 I worry that, for
880 * example, a stray write retransmission could be accepted by
881 * the server when it should have been rejected. Therefore,
882 * adopt a trick from the sctp code to attempt to maximize the
883 * amount of time until an id is reused, by ensuring they always
884 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400885 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500886 return stid;
887out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800888 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500889 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400890}
891
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400892/*
893 * Create a unique stateid_t to represent each COPY.
894 */
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400895static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
896 unsigned char sc_type)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400897{
898 int new_id;
899
Arnd Bergmann9cc76802019-10-31 15:53:13 +0100900 stid->stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400901 stid->stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
902 stid->sc_type = sc_type;
903
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400904 idr_preload(GFP_KERNEL);
905 spin_lock(&nn->s2s_cp_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400906 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
907 stid->stid.si_opaque.so_id = new_id;
Dai Ngoca9364d2020-11-30 16:24:49 -0500908 stid->stid.si_generation = 1;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400909 spin_unlock(&nn->s2s_cp_lock);
910 idr_preload_end();
911 if (new_id < 0)
912 return 0;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400913 return 1;
914}
915
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400916int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
917{
918 return nfs4_init_cp_state(nn, &copy->cp_stateid, NFS4_COPY_STID);
919}
920
921struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
922 struct nfs4_stid *p_stid)
923{
924 struct nfs4_cpntf_state *cps;
925
926 cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
927 if (!cps)
928 return NULL;
Arnd Bergmann20b7d862019-11-04 16:31:52 +0100929 cps->cpntf_time = ktime_get_boottime_seconds();
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400930 refcount_set(&cps->cp_stateid.sc_count, 1);
931 if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
932 goto out_free;
933 spin_lock(&nn->s2s_cp_lock);
934 list_add(&cps->cp_list, &p_stid->sc_cp_list);
935 spin_unlock(&nn->s2s_cp_lock);
936 return cps;
937out_free:
938 kfree(cps);
939 return NULL;
940}
941
942void nfs4_free_copy_state(struct nfsd4_copy *copy)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400943{
944 struct nfsd_net *nn;
945
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400946 WARN_ON_ONCE(copy->cp_stateid.sc_type != NFS4_COPY_STID);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400947 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
948 spin_lock(&nn->s2s_cp_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400949 idr_remove(&nn->s2s_cp_stateids,
950 copy->cp_stateid.stid.si_opaque.so_id);
951 spin_unlock(&nn->s2s_cp_lock);
952}
953
954static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
955{
956 struct nfs4_cpntf_state *cps;
957 struct nfsd_net *nn;
958
959 nn = net_generic(net, nfsd_net_id);
960 spin_lock(&nn->s2s_cp_lock);
961 while (!list_empty(&stid->sc_cp_list)) {
962 cps = list_first_entry(&stid->sc_cp_list,
963 struct nfs4_cpntf_state, cp_list);
964 _free_cpntf_state_locked(nn, cps);
965 }
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400966 spin_unlock(&nn->s2s_cp_lock);
967}
968
Jeff Laytonb49e0842014-07-29 21:34:11 -0400969static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400970{
Trond Myklebust60116952014-07-29 21:34:06 -0400971 struct nfs4_stid *stid;
Trond Myklebust60116952014-07-29 21:34:06 -0400972
Kinglong Meed19fb702017-01-18 19:04:42 +0800973 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
Trond Myklebust60116952014-07-29 21:34:06 -0400974 if (!stid)
975 return NULL;
976
Kinglong Meed19fb702017-01-18 19:04:42 +0800977 return openlockstateid(stid);
Trond Myklebust60116952014-07-29 21:34:06 -0400978}
979
980static void nfs4_free_deleg(struct nfs4_stid *stid)
981{
Trond Myklebust60116952014-07-29 21:34:06 -0400982 kmem_cache_free(deleg_slab, stid);
983 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400984}
985
NeilBrown6282cd52014-06-04 17:39:26 +1000986/*
987 * When we recall a delegation, we should be careful not to hand it
988 * out again straight away.
989 * To ensure this we keep a pair of bloom filters ('new' and 'old')
990 * in which the filehandles of recalled delegations are "stored".
991 * If a filehandle appear in either filter, a delegation is blocked.
992 * When a delegation is recalled, the filehandle is stored in the "new"
993 * filter.
994 * Every 30 seconds we swap the filters and clear the "new" one,
995 * unless both are empty of course.
996 *
997 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
998 * low 3 bytes as hash-table indices.
999 *
Jeff Laytonf54fe962014-07-25 07:34:26 -04001000 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +10001001 * is used to manage concurrent access. Testing does not need the lock
1002 * except when swapping the two filters.
1003 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04001004static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +10001005static struct bloom_pair {
1006 int entries, old_entries;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +02001007 time64_t swap_time;
NeilBrown6282cd52014-06-04 17:39:26 +10001008 int new; /* index into 'set' */
1009 DECLARE_BITMAP(set[2], 256);
1010} blocked_delegations;
1011
1012static int delegation_blocked(struct knfsd_fh *fh)
1013{
1014 u32 hash;
1015 struct bloom_pair *bd = &blocked_delegations;
1016
1017 if (bd->entries == 0)
1018 return 0;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +02001019 if (ktime_get_seconds() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -04001020 spin_lock(&blocked_delegations_lock);
Arnd Bergmannb3f255e2017-10-20 11:25:34 +02001021 if (ktime_get_seconds() - bd->swap_time > 30) {
NeilBrown6282cd52014-06-04 17:39:26 +10001022 bd->entries -= bd->old_entries;
1023 bd->old_entries = bd->entries;
1024 memset(bd->set[bd->new], 0,
1025 sizeof(bd->set[0]));
1026 bd->new = 1-bd->new;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +02001027 bd->swap_time = ktime_get_seconds();
NeilBrown6282cd52014-06-04 17:39:26 +10001028 }
Jeff Laytonf54fe962014-07-25 07:34:26 -04001029 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +10001030 }
NeilBrownd8b26072021-09-02 11:16:32 +10001031 hash = jhash(&fh->fh_raw, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +10001032 if (test_bit(hash&255, bd->set[0]) &&
1033 test_bit((hash>>8)&255, bd->set[0]) &&
1034 test_bit((hash>>16)&255, bd->set[0]))
1035 return 1;
1036
1037 if (test_bit(hash&255, bd->set[1]) &&
1038 test_bit((hash>>8)&255, bd->set[1]) &&
1039 test_bit((hash>>16)&255, bd->set[1]))
1040 return 1;
1041
1042 return 0;
1043}
1044
1045static void block_delegations(struct knfsd_fh *fh)
1046{
1047 u32 hash;
1048 struct bloom_pair *bd = &blocked_delegations;
1049
NeilBrownd8b26072021-09-02 11:16:32 +10001050 hash = jhash(&fh->fh_raw, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +10001051
Jeff Laytonf54fe962014-07-25 07:34:26 -04001052 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +10001053 __set_bit(hash&255, bd->set[bd->new]);
1054 __set_bit((hash>>8)&255, bd->set[bd->new]);
1055 __set_bit((hash>>16)&255, bd->set[bd->new]);
1056 if (bd->entries == 0)
Arnd Bergmannb3f255e2017-10-20 11:25:34 +02001057 bd->swap_time = ktime_get_seconds();
NeilBrown6282cd52014-06-04 17:39:26 +10001058 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -04001059 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +10001060}
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062static struct nfs4_delegation *
J. Bruce Fields86d29b12018-02-21 15:27:28 -05001063alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
1064 struct svc_fh *current_fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02001065 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -04001068 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -04001071 n = atomic_long_inc_return(&num_delegations);
1072 if (n < 0 || n > max_delegations)
1073 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +10001074 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -04001075 goto out_dec;
Kinglong Meed19fb702017-01-18 19:04:42 +08001076 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
NeilBrown5b2d21c2005-06-23 22:03:04 -07001077 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -04001078 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -04001079
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04001080 /*
1081 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001082 * meaning of seqid 0 isn't meaningful, really, but let's avoid
1083 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04001084 */
1085 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -07001086 INIT_LIST_HEAD(&dp->dl_perfile);
1087 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +02001089 dp->dl_clnt_odstate = odstate;
1090 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -04001091 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02001092 dp->dl_retries = 1;
1093 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +02001094 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
J. Bruce Fields86d29b12018-02-21 15:27:28 -05001095 get_nfs4_file(fp);
1096 dp->dl_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -04001098out_dec:
1099 atomic_long_dec(&num_delegations);
1100 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102
1103void
Trond Myklebust60116952014-07-29 21:34:06 -04001104nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
Trond Myklebust11b91642014-07-29 21:34:08 -04001106 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -04001107 struct nfs4_client *clp = s->sc_client;
1108
Jeff Layton4770d722014-07-29 21:34:10 -04001109 might_lock(&clp->cl_lock);
1110
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001111 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
Jeff Laytonb401be222014-07-29 21:34:33 -04001112 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -04001113 return;
Jeff Laytonb401be222014-07-29 21:34:33 -04001114 }
Trond Myklebust60116952014-07-29 21:34:06 -04001115 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001116 nfs4_free_cpntf_statelist(clp->net, s);
Jeff Layton4770d722014-07-29 21:34:10 -04001117 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04001118 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -04001119 if (fp)
1120 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
Jeff Layton9767feb2015-10-01 09:05:50 -04001123void
1124nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
1125{
1126 stateid_t *src = &stid->sc_stateid;
1127
1128 spin_lock(&stid->sc_lock);
1129 if (unlikely(++src->si_generation == 0))
1130 src->si_generation = 1;
1131 memcpy(dst, src, sizeof(*dst));
1132 spin_unlock(&stid->sc_lock);
1133}
1134
J. Bruce Fields353601e2018-02-16 14:29:42 -05001135static void put_deleg_file(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001137 struct nfsd_file *nf = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -05001138
1139 spin_lock(&fp->fi_lock);
1140 if (--fp->fi_delegees == 0)
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001141 swap(nf, fp->fi_deleg_file);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001142 spin_unlock(&fp->fi_lock);
1143
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001144 if (nf)
1145 nfsd_file_put(nf);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001146}
1147
1148static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1149{
J. Bruce Fieldscba7b3d2018-02-15 15:50:49 -05001150 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001151 struct nfsd_file *nf = fp->fi_deleg_file;
Jeff Layton417c6622014-07-21 09:34:57 -04001152
J. Bruce Fieldsb8232d32018-02-15 15:29:15 -05001153 WARN_ON_ONCE(!fp->fi_delegees);
1154
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001155 vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001156 put_deleg_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001159static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1160{
1161 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001162 nfs4_unlock_deleg_lease(dp);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001163 nfs4_put_stid(&dp->dl_stid);
1164}
1165
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001166void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001167{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001168 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001169}
1170
Andrew Elble34ed9872015-10-15 12:07:28 -04001171/**
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001172 * nfs4_delegation_exists - Discover if this delegation already exists
Andrew Elble34ed9872015-10-15 12:07:28 -04001173 * @clp: a pointer to the nfs4_client we're granting a delegation to
1174 * @fp: a pointer to the nfs4_file we're granting a delegation on
1175 *
1176 * Return:
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001177 * On success: true iff an existing delegation is found
Andrew Elble34ed9872015-10-15 12:07:28 -04001178 */
1179
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001180static bool
1181nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
Benny Halevy931ee562014-05-30 09:09:27 -04001182{
Andrew Elble34ed9872015-10-15 12:07:28 -04001183 struct nfs4_delegation *searchdp = NULL;
1184 struct nfs4_client *searchclp = NULL;
1185
Benny Halevycdc975052014-05-30 09:09:30 -04001186 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04001187 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001188
Andrew Elble34ed9872015-10-15 12:07:28 -04001189 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1190 searchclp = searchdp->dl_stid.sc_client;
1191 if (clp == searchclp) {
Fengguang Wu51d87bc2018-03-22 13:37:20 +08001192 return true;
Andrew Elble34ed9872015-10-15 12:07:28 -04001193 }
1194 }
Fengguang Wu51d87bc2018-03-22 13:37:20 +08001195 return false;
Andrew Elble34ed9872015-10-15 12:07:28 -04001196}
1197
1198/**
1199 * hash_delegation_locked - Add a delegation to the appropriate lists
1200 * @dp: a pointer to the nfs4_delegation we are adding.
1201 * @fp: a pointer to the nfs4_file we're granting a delegation on
1202 *
1203 * Return:
1204 * On success: NULL if the delegation was successfully hashed.
1205 *
1206 * On error: -EAGAIN if one was previously granted to this
1207 * nfs4_client for this nfs4_file. Delegation is not hashed.
1208 *
1209 */
1210
1211static int
1212hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1213{
Andrew Elble34ed9872015-10-15 12:07:28 -04001214 struct nfs4_client *clp = dp->dl_stid.sc_client;
1215
1216 lockdep_assert_held(&state_lock);
1217 lockdep_assert_held(&fp->fi_lock);
1218
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001219 if (nfs4_delegation_exists(clp, fp))
1220 return -EAGAIN;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001221 refcount_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -04001222 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -04001223 list_add(&dp->dl_perfile, &fp->fi_delegations);
Andrew Elble34ed9872015-10-15 12:07:28 -04001224 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1225 return 0;
Benny Halevy931ee562014-05-30 09:09:27 -04001226}
1227
J. Bruce Fields548ec082021-11-29 15:08:00 -05001228static bool delegation_hashed(struct nfs4_delegation *dp)
1229{
1230 return !(list_empty(&dp->dl_perfile));
1231}
1232
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001233static bool
Jeff Layton42690672014-07-25 07:34:20 -04001234unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Trond Myklebust11b91642014-07-29 21:34:08 -04001236 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -04001237
Jeff Layton42690672014-07-25 07:34:20 -04001238 lockdep_assert_held(&state_lock);
1239
J. Bruce Fields548ec082021-11-29 15:08:00 -05001240 if (!delegation_hashed(dp))
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001241 return false;
1242
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04001243 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -04001244 /* Ensure that deleg break won't try to requeue it */
1245 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -04001246 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001247 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -04001249 list_del_init(&dp->dl_perfile);
1250 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001251 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001252}
1253
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001254static void destroy_delegation(struct nfs4_delegation *dp)
1255{
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001256 bool unhashed;
1257
Jeff Layton42690672014-07-25 07:34:20 -04001258 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001259 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -04001260 spin_unlock(&state_lock);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001261 if (unhashed)
1262 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001263}
1264
1265static void revoke_delegation(struct nfs4_delegation *dp)
1266{
1267 struct nfs4_client *clp = dp->dl_stid.sc_client;
1268
Jeff Layton2d4a5322014-07-25 07:34:21 -04001269 WARN_ON(!list_empty(&dp->dl_recall_lru));
1270
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001271 if (clp->cl_minorversion) {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001272 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001273 refcount_inc(&dp->dl_stid.sc_count);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001274 spin_lock(&clp->cl_lock);
1275 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1276 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001277 }
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001278 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001279}
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281/*
1282 * SETCLIENTID state
1283 */
1284
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001285static unsigned int clientid_hashval(u32 id)
1286{
1287 return id & CLIENT_HASH_MASK;
1288}
1289
Scott Mayhew6b189102019-03-26 18:06:26 -04001290static unsigned int clientstr_hashval(struct xdr_netobj name)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001291{
Scott Mayhew6b189102019-03-26 18:06:26 -04001292 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001293}
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295/*
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001296 * A stateid that had a deny mode associated with it is being released
1297 * or downgraded. Recalculate the deny mode on the file.
1298 */
1299static void
1300recalculate_deny_mode(struct nfs4_file *fp)
1301{
1302 struct nfs4_ol_stateid *stp;
1303
1304 spin_lock(&fp->fi_lock);
1305 fp->fi_share_deny = 0;
1306 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1307 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1308 spin_unlock(&fp->fi_lock);
1309}
1310
1311static void
1312reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1313{
1314 int i;
1315 bool change = false;
1316
1317 for (i = 1; i < 4; i++) {
1318 if ((i & deny) != i) {
1319 change = true;
1320 clear_deny(i, stp);
1321 }
1322 }
1323
1324 /* Recalculate per-file deny mode if there was a change */
1325 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -04001326 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001327}
1328
Jeff Layton82c5ff12012-05-11 09:45:13 -04001329/* release all access and file references for a given stateid */
1330static void
1331release_all_access(struct nfs4_ol_stateid *stp)
1332{
1333 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -04001334 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001335
1336 if (fp && stp->st_deny_bmap != 0)
1337 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001338
1339 for (i = 1; i < 4; i++) {
1340 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -04001341 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001342 clear_access(i, stp);
1343 }
1344}
1345
Kinglong Meed50ffde2015-07-16 12:05:07 +08001346static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1347{
1348 kfree(sop->so_owner.data);
1349 sop->so_ops->so_free(sop);
1350}
1351
Jeff Layton6b180f02014-07-29 21:34:26 -04001352static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1353{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001354 struct nfs4_client *clp = sop->so_client;
1355
1356 might_lock(&clp->cl_lock);
1357
1358 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001359 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001360 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001361 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001362 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001363}
1364
Trond Myklebusta451b122020-03-01 18:21:38 -05001365static bool
1366nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1367{
1368 return list_empty(&stp->st_perfile);
1369}
1370
Jeff Laytone8568732015-08-24 12:41:47 -04001371static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001372{
Trond Myklebust11b91642014-07-29 21:34:08 -04001373 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001374
Jeff Layton1c755dc2014-07-29 21:34:12 -04001375 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1376
Jeff Laytone8568732015-08-24 12:41:47 -04001377 if (list_empty(&stp->st_perfile))
1378 return false;
1379
Trond Myklebust1d31a252014-07-10 14:07:25 -04001380 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001381 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001382 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001383 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001384 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001385}
1386
Trond Myklebust60116952014-07-29 21:34:06 -04001387static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001388{
Trond Myklebust60116952014-07-29 21:34:06 -04001389 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001390
Sachin Bhamare8287f002015-04-27 14:50:14 +02001391 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001392 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001393 if (stp->st_stateowner)
1394 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -04001395 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001396}
1397
Jeff Laytonb49e0842014-07-29 21:34:11 -04001398static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001399{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001400 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1401 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001402 struct nfsd_file *nf;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001403
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001404 nf = find_any_file(stp->st_stid.sc_file);
1405 if (nf) {
1406 get_file(nf->nf_file);
1407 filp_close(nf->nf_file, (fl_owner_t)lo);
1408 nfsd_file_put(nf);
1409 }
Jeff Laytonb49e0842014-07-29 21:34:11 -04001410 nfs4_free_ol_stateid(stid);
1411}
1412
Jeff Layton2c41beb2014-07-29 21:34:41 -04001413/*
1414 * Put the persistent reference to an already unhashed generic stateid, while
1415 * holding the cl_lock. If it's the last reference, then put it onto the
1416 * reaplist for later destruction.
1417 */
1418static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1419 struct list_head *reaplist)
1420{
1421 struct nfs4_stid *s = &stp->st_stid;
1422 struct nfs4_client *clp = s->sc_client;
1423
1424 lockdep_assert_held(&clp->cl_lock);
1425
1426 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1427
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001428 if (!refcount_dec_and_test(&s->sc_count)) {
Jeff Layton2c41beb2014-07-29 21:34:41 -04001429 wake_up_all(&close_wq);
1430 return;
1431 }
1432
1433 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1434 list_add(&stp->st_locks, reaplist);
1435}
1436
Jeff Laytone8568732015-08-24 12:41:47 -04001437static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001438{
Chuck Leverf46c4452016-10-29 18:19:03 -04001439 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001440
Trond Myklebusta451b122020-03-01 18:21:38 -05001441 if (!unhash_ol_stateid(stp))
1442 return false;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001443 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001444 nfs4_unhash_stid(&stp->st_stid);
Trond Myklebusta451b122020-03-01 18:21:38 -05001445 return true;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001446}
1447
Jeff Layton5adfd882014-07-29 21:34:31 -04001448static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001449{
Chuck Leverf46c4452016-10-29 18:19:03 -04001450 struct nfs4_client *clp = stp->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04001451 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001452
Chuck Leverf46c4452016-10-29 18:19:03 -04001453 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001454 unhashed = unhash_lock_stateid(stp);
Chuck Leverf46c4452016-10-29 18:19:03 -04001455 spin_unlock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001456 if (unhashed)
1457 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001458}
1459
Trond Myklebustc58c6612014-07-29 21:34:35 -04001460static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001461{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001462 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001463
Trond Myklebustd4f04892014-07-29 21:34:36 -04001464 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001465
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001466 list_del_init(&lo->lo_owner.so_strhash);
1467}
1468
Jeff Layton2c41beb2014-07-29 21:34:41 -04001469/*
1470 * Free a list of generic stateids that were collected earlier after being
1471 * fully unhashed.
1472 */
1473static void
1474free_ol_stateid_reaplist(struct list_head *reaplist)
1475{
1476 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001477 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001478
1479 might_sleep();
1480
1481 while (!list_empty(reaplist)) {
1482 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1483 st_locks);
1484 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001485 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001486 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001487 if (fp)
1488 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001489 }
1490}
1491
Jeff Laytond83017f2014-07-29 21:34:42 -04001492static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1493 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001494{
1495 struct nfs4_ol_stateid *stp;
1496
Jeff Laytone8568732015-08-24 12:41:47 -04001497 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1498
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001499 while (!list_empty(&open_stp->st_locks)) {
1500 stp = list_entry(open_stp->st_locks.next,
1501 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001502 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001503 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001504 }
1505}
1506
Jeff Laytone8568732015-08-24 12:41:47 -04001507static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001508 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001509{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001510 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1511
Trond Myklebusta451b122020-03-01 18:21:38 -05001512 if (!unhash_ol_stateid(stp))
1513 return false;
Jeff Laytond83017f2014-07-29 21:34:42 -04001514 release_open_stateid_locks(stp, reaplist);
Trond Myklebusta451b122020-03-01 18:21:38 -05001515 return true;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001516}
1517
1518static void release_open_stateid(struct nfs4_ol_stateid *stp)
1519{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001520 LIST_HEAD(reaplist);
1521
1522 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001523 if (unhash_open_stateid(stp, &reaplist))
1524 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001525 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1526 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001527}
1528
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001529static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001530{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001531 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001532
Trond Myklebustd4f04892014-07-29 21:34:36 -04001533 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001534
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001535 list_del_init(&oo->oo_owner.so_strhash);
1536 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001537}
1538
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001539static void release_last_closed_stateid(struct nfs4_openowner *oo)
1540{
Jeff Layton217526e2014-07-30 08:27:11 -04001541 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1542 nfsd_net_id);
1543 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001544
Jeff Layton217526e2014-07-30 08:27:11 -04001545 spin_lock(&nn->client_lock);
1546 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001547 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001548 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001549 oo->oo_last_closed_stid = NULL;
1550 }
Jeff Layton217526e2014-07-30 08:27:11 -04001551 spin_unlock(&nn->client_lock);
1552 if (s)
1553 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001554}
1555
Jeff Layton2c41beb2014-07-29 21:34:41 -04001556static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001557{
1558 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001559 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001560 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001561
Jeff Layton2c41beb2014-07-29 21:34:41 -04001562 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001563
Trond Myklebustd4f04892014-07-29 21:34:36 -04001564 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001565 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001566 while (!list_empty(&oo->oo_owner.so_stateids)) {
1567 stp = list_first_entry(&oo->oo_owner.so_stateids,
1568 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001569 if (unhash_open_stateid(stp, &reaplist))
1570 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001571 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001572 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001573 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001574 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001575 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001576}
1577
Marc Eshel5282fd72009-04-03 08:27:52 +03001578static inline int
1579hash_sessionid(struct nfs4_sessionid *sessionid)
1580{
1581 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1582
1583 return sid->sequence % SESSION_HASH_SIZE;
1584}
1585
Mark Salter135dd002015-04-06 09:46:00 -04001586#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001587static inline void
1588dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1589{
1590 u32 *ptr = (u32 *)(&sessionid->data[0]);
1591 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1592}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001593#else
1594static inline void
1595dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1596{
1597}
1598#endif
1599
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001600/*
1601 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1602 * won't be used for replay.
1603 */
1604void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1605{
1606 struct nfs4_stateowner *so = cstate->replay_owner;
1607
1608 if (nfserr == nfserr_replay_me)
1609 return;
1610
1611 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001612 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001613 return;
1614 }
1615 if (!so)
1616 return;
1617 if (so->so_is_open_owner)
1618 release_last_closed_stateid(openowner(so));
1619 so->so_seqid++;
1620 return;
1621}
Marc Eshel5282fd72009-04-03 08:27:52 +03001622
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001623static void
1624gen_sessionid(struct nfsd4_session *ses)
1625{
1626 struct nfs4_client *clp = ses->se_client;
1627 struct nfsd4_sessionid *sid;
1628
1629 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1630 sid->clientid = clp->cl_clientid;
1631 sid->sequence = current_sessionid++;
1632 sid->reserved = 0;
1633}
1634
1635/*
Andy Adamsona649637c72009-08-28 08:45:01 -04001636 * The protocol defines ca_maxresponssize_cached to include the size of
1637 * the rpc header, but all we need to cache is the data starting after
1638 * the end of the initial SEQUENCE operation--the rest we regenerate
1639 * each time. Therefore we can advertise a ca_maxresponssize_cached
1640 * value that is the number of bytes in our cache plus a few additional
1641 * bytes. In order to stay on the safe side, and not promise more than
1642 * we can cache, those additional bytes must be the minimum possible: 24
1643 * bytes of rpc header (xid through accept state, with AUTH_NULL
1644 * verifier), 12 for the compound header (with zero-length tag), and 44
1645 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001646 */
Andy Adamsona649637c72009-08-28 08:45:01 -04001647#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1648
Andy Adamson557ce262009-08-28 08:45:04 -04001649static void
1650free_session_slots(struct nfsd4_session *ses)
1651{
1652 int i;
1653
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001654 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1655 free_svc_cred(&ses->se_slots[i]->sl_cred);
Andy Adamson557ce262009-08-28 08:45:04 -04001656 kfree(ses->se_slots[i]);
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001657 }
Andy Adamson557ce262009-08-28 08:45:04 -04001658}
1659
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001660/*
1661 * We don't actually need to cache the rpc and session headers, so we
1662 * can allocate a little less for each slot:
1663 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001664static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001665{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001666 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001667
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001668 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1669 size = 0;
1670 else
1671 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1672 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001673}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001674
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001675/*
1676 * XXX: If we run out of reserved DRC memory we could (up to a point)
1677 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001678 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001679 */
NeilBrown2030ca52019-09-20 16:36:45 +10001680static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001681{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001682 u32 slotsize = slot_bytes(ca);
1683 u32 num = ca->maxreqs;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001684 unsigned long avail, total_avail;
NeilBrown2030ca52019-09-20 16:36:45 +10001685 unsigned int scale_factor;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001686
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001687 spin_lock(&nfsd_drc_lock);
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001688 if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1689 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1690 else
1691 /* We have handed out more space than we chose in
1692 * set_max_drc() to allow. That isn't really a
1693 * problem as long as that doesn't make us think we
1694 * have lots more due to integer overflow.
1695 */
1696 total_avail = 0;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001697 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001698 /*
NeilBrown2030ca52019-09-20 16:36:45 +10001699 * Never use more than a fraction of the remaining memory,
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001700 * unless it's the only way to give this client a slot.
NeilBrown2030ca52019-09-20 16:36:45 +10001701 * The chosen fraction is either 1/8 or 1/number of threads,
1702 * whichever is smaller. This ensures there are adequate
1703 * slots to support multiple clients per thread.
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001704 * Give the client one slot even if that would require
1705 * over-allocation--it is better than failure.
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001706 */
NeilBrown2030ca52019-09-20 16:36:45 +10001707 scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1708
1709 avail = clamp_t(unsigned long, avail, slotsize,
1710 total_avail/scale_factor);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001711 num = min_t(int, num, avail / slotsize);
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001712 num = max_t(int, num, 1);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001713 nfsd_drc_mem_used += num * slotsize;
1714 spin_unlock(&nfsd_drc_lock);
1715
1716 return num;
1717}
1718
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001719static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001720{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001721 int slotsize = slot_bytes(ca);
1722
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001723 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001724 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001725 spin_unlock(&nfsd_drc_lock);
1726}
1727
Kinglong Mee60810e52014-01-01 00:35:47 +08001728static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1729 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001730{
Kinglong Mee60810e52014-01-01 00:35:47 +08001731 int numslots = fattrs->maxreqs;
1732 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001733 struct nfsd4_session *new;
1734 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001735
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001736 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001737 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1738 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001739
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001740 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001741 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001742 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001743 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001744 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001745 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001746 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001747 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001748 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001749
1750 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1751 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1752
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001753 return new;
1754out_free:
1755 while (i--)
1756 kfree(new->se_slots[i]);
1757 kfree(new);
1758 return NULL;
1759}
1760
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001761static void free_conn(struct nfsd4_conn *c)
1762{
1763 svc_xprt_put(c->cn_xprt);
1764 kfree(c);
1765}
1766
1767static void nfsd4_conn_lost(struct svc_xpt_user *u)
1768{
1769 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1770 struct nfs4_client *clp = c->cn_session->se_client;
1771
Chuck Lever806d65b2021-05-14 15:56:43 -04001772 trace_nfsd_cb_lost(clp);
1773
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001774 spin_lock(&clp->cl_lock);
1775 if (!list_empty(&c->cn_persession)) {
1776 list_del(&c->cn_persession);
1777 free_conn(c);
1778 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001779 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001780 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001781}
1782
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001783static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001784{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001785 struct nfsd4_conn *conn;
1786
1787 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1788 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001789 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001790 svc_xprt_get(rqstp->rq_xprt);
1791 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001792 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001793 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1794 return conn;
1795}
1796
J. Bruce Fields328ead22010-09-29 16:11:06 -04001797static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1798{
1799 conn->cn_session = ses;
1800 list_add(&conn->cn_persession, &ses->se_conns);
1801}
1802
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001803static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1804{
1805 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001806
1807 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001808 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001809 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001810}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001811
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001812static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001813{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001814 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001815 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001816}
1817
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001818static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001819{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001820 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001821
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001822 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001823 ret = nfsd4_register_conn(conn);
1824 if (ret)
1825 /* oops; xprt is already down: */
1826 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001827 /* We may have gained or lost a callback channel: */
1828 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001829}
1830
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001831static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001832{
1833 u32 dir = NFS4_CDFC4_FORE;
1834
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001835 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001836 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001837 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001838}
1839
1840/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001841static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001842{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001843 struct nfs4_client *clp = s->se_client;
1844 struct nfsd4_conn *c;
1845
1846 spin_lock(&clp->cl_lock);
1847 while (!list_empty(&s->se_conns)) {
1848 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1849 list_del_init(&c->cn_persession);
1850 spin_unlock(&clp->cl_lock);
1851
1852 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1853 free_conn(c);
1854
1855 spin_lock(&clp->cl_lock);
1856 }
1857 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001858}
1859
J. Bruce Fields1377b692012-09-11 21:42:40 -04001860static void __free_session(struct nfsd4_session *ses)
1861{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001862 free_session_slots(ses);
1863 kfree(ses);
1864}
1865
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001866static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001867{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001868 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001869 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001870 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001871}
Andy Adamson557ce262009-08-28 08:45:04 -04001872
Fengguang Wu135ae822012-11-10 07:20:25 -05001873static 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 -04001874{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001875 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001876 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001877
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001878 new->se_client = clp;
1879 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001880
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001881 INIT_LIST_HEAD(&new->se_conns);
1882
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001883 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001884 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001885 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001886 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001887 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001888 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001889 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001890 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001891 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001892 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001893
Chuck Leverb0d2e422014-08-22 15:10:59 -04001894 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001895 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001896 /*
1897 * This is a little silly; with sessions there's no real
1898 * use for the callback address. Use the peer address
1899 * as a reasonable default for now, but consider fixing
1900 * the rpc client not to require an address in the
1901 * future:
1902 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001903 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1904 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001905 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001906}
1907
Benny Halevy9089f1b2010-05-12 00:12:26 +03001908/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001909static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001910__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001911{
1912 struct nfsd4_session *elem;
1913 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001914 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001915
Trond Myklebust0a880a22014-07-30 08:27:10 -04001916 lockdep_assert_held(&nn->client_lock);
1917
Marc Eshel5282fd72009-04-03 08:27:52 +03001918 dump_sessionid(__func__, sessionid);
1919 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001920 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001921 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001922 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1923 NFS4_MAX_SESSIONID_LEN)) {
1924 return elem;
1925 }
1926 }
1927
1928 dprintk("%s: session not found\n", __func__);
1929 return NULL;
1930}
1931
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001932static struct nfsd4_session *
1933find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1934 __be32 *ret)
1935{
1936 struct nfsd4_session *session;
1937 __be32 status = nfserr_badsession;
1938
1939 session = __find_in_sessionid_hashtbl(sessionid, net);
1940 if (!session)
1941 goto out;
1942 status = nfsd4_get_session_locked(session);
1943 if (status)
1944 session = NULL;
1945out:
1946 *ret = status;
1947 return session;
1948}
1949
Benny Halevy9089f1b2010-05-12 00:12:26 +03001950/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001951static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001952unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001953{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001954 struct nfs4_client *clp = ses->se_client;
1955 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1956
1957 lockdep_assert_held(&nn->client_lock);
1958
Andy Adamson7116ed62009-04-03 08:27:43 +03001959 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001960 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001961 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001962 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001963}
1964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1966static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001967STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001969 /*
1970 * We're assuming the clid was not given out from a boot
1971 * precisely 2^32 (about 136 years) before this one. That seems
1972 * a safe assumption:
1973 */
1974 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 return 0;
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04001976 trace_nfsd_clid_stale(clid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return 1;
1978}
1979
1980/*
1981 * XXX Should we use a slab cache ?
1982 * This type of memory management is somewhat inefficient, but we use it
1983 * anyway since SETCLIENTID is not a common operation.
1984 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001985static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
1987 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001988 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Jeff Layton9258a2d2018-03-16 09:47:22 -04001990 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001991 if (clp == NULL)
1992 return NULL;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04001993 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001994 if (clp->cl_name.data == NULL)
1995 goto err_no_name;
Kees Cook6da2ec52018-06-12 13:55:00 -07001996 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1997 sizeof(struct list_head),
1998 GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001999 if (!clp->cl_ownerstr_hashtbl)
2000 goto err_no_hashtbl;
2001 for (i = 0; i < OWNER_HASH_SIZE; i++)
2002 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
Trond Myklebust5694c932014-04-18 14:43:56 -04002003 INIT_LIST_HEAD(&clp->cl_sessions);
2004 idr_init(&clp->cl_stateids);
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04002005 atomic_set(&clp->cl_rpc_users, 0);
Trond Myklebust5694c932014-04-18 14:43:56 -04002006 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
2007 INIT_LIST_HEAD(&clp->cl_idhash);
2008 INIT_LIST_HEAD(&clp->cl_openowners);
2009 INIT_LIST_HEAD(&clp->cl_delegations);
2010 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04002011 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002012#ifdef CONFIG_NFSD_PNFS
2013 INIT_LIST_HEAD(&clp->cl_lo_states);
2014#endif
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002015 INIT_LIST_HEAD(&clp->async_copies);
2016 spin_lock_init(&clp->async_lock);
Trond Myklebust5694c932014-04-18 14:43:56 -04002017 spin_lock_init(&clp->cl_lock);
2018 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04002020err_no_hashtbl:
2021 kfree(clp->cl_name.data);
2022err_no_name:
Jeff Layton9258a2d2018-03-16 09:47:22 -04002023 kmem_cache_free(client_slab, clp);
Trond Myklebustd4f04892014-07-29 21:34:36 -04002024 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002027static void __free_client(struct kref *k)
2028{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002029 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
2030 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002031
2032 free_svc_cred(&clp->cl_cred);
2033 kfree(clp->cl_ownerstr_hashtbl);
2034 kfree(clp->cl_name.data);
J. Bruce Fields79123442019-06-05 12:42:05 -04002035 kfree(clp->cl_nii_domain.data);
2036 kfree(clp->cl_nii_name.data);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002037 idr_destroy(&clp->cl_stateids);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002038 kmem_cache_free(client_slab, clp);
2039}
2040
YueHaibing297e57a2019-07-08 15:29:33 +08002041static void drop_client(struct nfs4_client *clp)
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002042{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002043 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002044}
2045
Trond Myklebust4dd86e152014-04-18 14:43:58 -04002046static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047free_client(struct nfs4_client *clp)
2048{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002049 while (!list_empty(&clp->cl_sessions)) {
2050 struct nfsd4_session *ses;
2051 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2052 se_perclnt);
2053 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002054 WARN_ON_ONCE(atomic_read(&ses->se_ref));
2055 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002056 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04002057 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002058 if (clp->cl_nfsd_dentry) {
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002059 nfsd_client_rmdir(clp->cl_nfsd_dentry);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002060 clp->cl_nfsd_dentry = NULL;
2061 wake_up_all(&expiry_wq);
2062 }
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002063 drop_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
2065
Benny Halevy84d38ac2010-05-12 00:13:16 +03002066/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04002067static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03002068unhash_client_locked(struct nfs4_client *clp)
2069{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002070 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002071 struct nfsd4_session *ses;
2072
Trond Myklebust0a880a22014-07-30 08:27:10 -04002073 lockdep_assert_held(&nn->client_lock);
2074
Trond Myklebust4beb3452014-07-30 08:27:02 -04002075 /* Mark the client as expired! */
2076 clp->cl_time = 0;
2077 /* Make it invisible */
2078 if (!list_empty(&clp->cl_idhash)) {
2079 list_del_init(&clp->cl_idhash);
2080 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2081 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2082 else
2083 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2084 }
2085 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04002086 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002087 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2088 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04002089 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03002090}
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04002093unhash_client(struct nfs4_client *clp)
2094{
2095 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2096
2097 spin_lock(&nn->client_lock);
2098 unhash_client_locked(clp);
2099 spin_unlock(&nn->client_lock);
2100}
2101
Jeff Layton97403d92014-07-30 08:27:12 -04002102static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2103{
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04002104 if (atomic_read(&clp->cl_rpc_users))
Jeff Layton97403d92014-07-30 08:27:12 -04002105 return nfserr_jukebox;
2106 unhash_client_locked(clp);
2107 return nfs_ok;
2108}
2109
Trond Myklebust4beb3452014-07-30 08:27:02 -04002110static void
2111__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
Jeff Layton68ef3bc2018-03-16 11:32:02 -04002113 int i;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002114 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 struct list_head reaplist;
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04002119 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07002120 while (!list_empty(&clp->cl_delegations)) {
2121 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04002122 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04002123 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
Benny Halevycdc975052014-05-30 09:09:30 -04002125 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 while (!list_empty(&reaplist)) {
2127 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04002128 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05002129 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04002131 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05002132 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04002133 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04002134 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02002135 }
NeilBrownea1da632005-06-23 22:04:17 -07002136 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002137 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04002138 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002139 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
Jeff Layton68ef3bc2018-03-16 11:32:02 -04002141 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2142 struct nfs4_stateowner *so, *tmp;
2143
2144 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2145 so_strhash) {
2146 /* Should be no openowners at this point */
2147 WARN_ON_ONCE(so->so_is_open_owner);
2148 remove_blocked_locks(lockowner(so));
2149 }
2150 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002151 nfsd4_return_all_client_layouts(clp);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002152 nfsd4_shutdown_copy(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04002153 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002154 if (clp->cl_cb_conn.cb_xprt)
2155 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002156 free_client(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002157 wake_up_all(&expiry_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158}
2159
Trond Myklebust4beb3452014-07-30 08:27:02 -04002160static void
2161destroy_client(struct nfs4_client *clp)
2162{
2163 unhash_client(clp);
2164 __destroy_client(clp);
2165}
2166
Scott Mayhew362063a2019-03-26 18:06:28 -04002167static void inc_reclaim_complete(struct nfs4_client *clp)
2168{
2169 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2170
2171 if (!nn->track_reclaim_completes)
2172 return;
2173 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2174 return;
2175 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2176 nn->reclaim_str_hashtbl_size) {
2177 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2178 clp->net->ns.inum);
2179 nfsd4_end_grace(nn);
2180 }
2181}
2182
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002183static void expire_client(struct nfs4_client *clp)
2184{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002185 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002186 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04002187 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002188}
2189
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002190static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2191{
2192 memcpy(target->cl_verifier.data, source->data,
2193 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194}
2195
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002196static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2197{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2199 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2200}
2201
J. Bruce Fields50043852015-11-20 15:58:37 -05002202static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2203{
NeilBrown2f10fdc2017-03-23 16:57:36 +08002204 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2205 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2206 GFP_KERNEL);
Chuck Lever9abdda52018-08-16 12:05:59 -04002207 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2208 if ((source->cr_principal && !target->cr_principal) ||
2209 (source->cr_raw_principal && !target->cr_raw_principal) ||
2210 (source->cr_targ_princ && !target->cr_targ_princ))
NeilBrown2f10fdc2017-03-23 16:57:36 +08002211 return -ENOMEM;
J. Bruce Fields50043852015-11-20 15:58:37 -05002212
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04002213 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 target->cr_uid = source->cr_uid;
2215 target->cr_gid = source->cr_gid;
2216 target->cr_group_info = source->cr_group_info;
2217 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04002218 target->cr_gss_mech = source->cr_gss_mech;
2219 if (source->cr_gss_mech)
2220 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002221 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222}
2223
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002224static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002225compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2226{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002227 if (o1->len < o2->len)
2228 return -1;
2229 if (o1->len > o2->len)
2230 return 1;
2231 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002232}
2233
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002235same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2236{
2237 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
2240static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002241same_clid(clientid_t *cl1, clientid_t *cl2)
2242{
2243 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244}
2245
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002246static bool groups_equal(struct group_info *g1, struct group_info *g2)
2247{
2248 int i;
2249
2250 if (g1->ngroups != g2->ngroups)
2251 return false;
2252 for (i=0; i<g1->ngroups; i++)
Alexey Dobriyan81243ea2016-10-07 17:03:12 -07002253 if (!gid_eq(g1->gid[i], g2->gid[i]))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002254 return false;
2255 return true;
2256}
2257
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002258/*
2259 * RFC 3530 language requires clid_inuse be returned when the
2260 * "principal" associated with a requests differs from that previously
2261 * used. We use uid, gid's, and gss principal string as our best
2262 * approximation. We also don't want to allow non-gss use of a client
2263 * established using gss: in theory cr_principal should catch that
2264 * change, but in practice cr_principal can be null even in the gss case
2265 * since gssd doesn't always pass down a principal string.
2266 */
2267static bool is_gss_cred(struct svc_cred *cr)
2268{
2269 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2270 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2271}
2272
2273
Vivek Trivedi5559b502012-07-24 21:18:20 +05302274static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002275same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2276{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002277 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002278 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2279 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002280 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2281 return false;
Chuck Lever9abdda52018-08-16 12:05:59 -04002282 /* XXX: check that cr_targ_princ fields match ? */
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002283 if (cr1->cr_principal == cr2->cr_principal)
2284 return true;
2285 if (!cr1->cr_principal || !cr2->cr_principal)
2286 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05302287 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288}
2289
J. Bruce Fields57266a62013-04-13 14:27:29 -04002290static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2291{
2292 struct svc_cred *cr = &rqstp->rq_cred;
2293 u32 service;
2294
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04002295 if (!cr->cr_gss_mech)
2296 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002297 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2298 return service == RPC_GSS_SVC_INTEGRITY ||
2299 service == RPC_GSS_SVC_PRIVACY;
2300}
2301
Andrew Elblededeb132016-06-15 12:52:08 -04002302bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
J. Bruce Fields57266a62013-04-13 14:27:29 -04002303{
2304 struct svc_cred *cr = &rqstp->rq_cred;
2305
2306 if (!cl->cl_mach_cred)
2307 return true;
2308 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2309 return false;
2310 if (!svc_rqst_integrity_protected(rqstp))
2311 return false;
J. Bruce Fields414ca012015-11-20 10:48:02 -05002312 if (cl->cl_cred.cr_raw_principal)
2313 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2314 cr->cr_raw_principal);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002315 if (!cr->cr_principal)
2316 return false;
2317 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2318}
2319
Jeff Layton294ac322014-07-30 08:27:15 -04002320static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05002321{
Chuck Leverab4684d2012-03-02 17:13:50 -05002322 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Jeff Laytonf4199922014-06-17 07:44:11 -04002324 /*
2325 * This is opaque to client, so no need to byte-swap. Use
2326 * __force to keep sparse happy
2327 */
Arnd Bergmann9104ae42019-11-04 16:45:30 +01002328 verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08002329 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05002330 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331}
2332
Jeff Layton294ac322014-07-30 08:27:15 -04002333static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2334{
Arnd Bergmann9cc76802019-10-31 15:53:13 +01002335 clp->cl_clientid.cl_boot = (u32)nn->boot_time;
Jeff Layton294ac322014-07-30 08:27:15 -04002336 clp->cl_clientid.cl_id = nn->clientid_counter++;
2337 gen_confirm(clp, nn);
2338}
2339
Jeff Layton4770d722014-07-29 21:34:10 -04002340static struct nfs4_stid *
2341find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04002342{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002343 struct nfs4_stid *ret;
2344
2345 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2346 if (!ret || !ret->sc_type)
2347 return NULL;
2348 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04002349}
2350
Jeff Layton4770d722014-07-29 21:34:10 -04002351static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04002352find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002353{
2354 struct nfs4_stid *s;
2355
Jeff Layton4770d722014-07-29 21:34:10 -04002356 spin_lock(&cl->cl_lock);
2357 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002358 if (s != NULL) {
2359 if (typemask & s->sc_type)
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03002360 refcount_inc(&s->sc_count);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002361 else
2362 s = NULL;
2363 }
Jeff Layton4770d722014-07-29 21:34:10 -04002364 spin_unlock(&cl->cl_lock);
2365 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04002366}
2367
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002368static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2369{
2370 struct nfsdfs_client *nc;
2371 nc = get_nfsdfs_client(inode);
2372 if (!nc)
2373 return NULL;
2374 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2375}
2376
J. Bruce Fields169319f2019-06-19 12:39:46 -04002377static void seq_quote_mem(struct seq_file *m, char *data, int len)
2378{
2379 seq_printf(m, "\"");
Andy Shevchenkoc0546392021-06-30 18:55:43 -07002380 seq_escape_mem(m, data, len, ESCAPE_HEX | ESCAPE_NAP | ESCAPE_APPEND, "\"\\");
J. Bruce Fields169319f2019-06-19 12:39:46 -04002381 seq_printf(m, "\"");
2382}
2383
Dave Wysochanski3518c862021-06-02 13:51:39 -04002384static const char *cb_state2str(int state)
2385{
2386 switch (state) {
2387 case NFSD4_CB_UP:
2388 return "UP";
2389 case NFSD4_CB_UNKNOWN:
2390 return "UNKNOWN";
2391 case NFSD4_CB_DOWN:
2392 return "DOWN";
2393 case NFSD4_CB_FAULT:
2394 return "FAULT";
2395 }
2396 return "UNDEFINED";
2397}
2398
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002399static int client_info_show(struct seq_file *m, void *v)
2400{
2401 struct inode *inode = m->private;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002402 struct nfs4_client *clp;
2403 u64 clid;
2404
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002405 clp = get_nfsdfs_clp(inode);
2406 if (!clp)
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002407 return -ENXIO;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002408 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2409 seq_printf(m, "clientid: 0x%llx\n", clid);
J. Bruce Fields169319f2019-06-19 12:39:46 -04002410 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
NeilBrown472d1552021-03-20 09:38:04 +11002411 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2412 seq_puts(m, "status: confirmed\n");
2413 else
2414 seq_puts(m, "status: unconfirmed\n");
J. Bruce Fields169319f2019-06-19 12:39:46 -04002415 seq_printf(m, "name: ");
2416 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2417 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
J. Bruce Fields79123442019-06-05 12:42:05 -04002418 if (clp->cl_nii_domain.data) {
2419 seq_printf(m, "Implementation domain: ");
2420 seq_quote_mem(m, clp->cl_nii_domain.data,
2421 clp->cl_nii_domain.len);
2422 seq_printf(m, "\nImplementation name: ");
2423 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
Arnd Bergmanne29f4702019-10-31 14:52:43 +01002424 seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
J. Bruce Fields79123442019-06-05 12:42:05 -04002425 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2426 }
Dave Wysochanski3518c862021-06-02 13:51:39 -04002427 seq_printf(m, "callback state: %s\n", cb_state2str(clp->cl_cb_state));
2428 seq_printf(m, "callback address: %pISpc\n", &clp->cl_cb_conn.cb_addr);
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002429 drop_client(clp);
2430
2431 return 0;
2432}
2433
2434static int client_info_open(struct inode *inode, struct file *file)
2435{
2436 return single_open(file, client_info_show, inode);
2437}
2438
2439static const struct file_operations client_info_fops = {
2440 .open = client_info_open,
2441 .read = seq_read,
2442 .llseek = seq_lseek,
2443 .release = single_release,
2444};
2445
J. Bruce Fields78599c42019-04-22 15:26:23 -04002446static void *states_start(struct seq_file *s, loff_t *pos)
2447 __acquires(&clp->cl_lock)
2448{
2449 struct nfs4_client *clp = s->private;
2450 unsigned long id = *pos;
2451 void *ret;
2452
2453 spin_lock(&clp->cl_lock);
2454 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2455 *pos = id;
2456 return ret;
2457}
2458
2459static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2460{
2461 struct nfs4_client *clp = s->private;
2462 unsigned long id = *pos;
2463 void *ret;
2464
2465 id = *pos;
2466 id++;
2467 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2468 *pos = id;
2469 return ret;
2470}
2471
2472static void states_stop(struct seq_file *s, void *v)
2473 __releases(&clp->cl_lock)
2474{
2475 struct nfs4_client *clp = s->private;
2476
2477 spin_unlock(&clp->cl_lock);
2478}
2479
Achilles Gaikwad580da462020-04-20 18:20:31 +05302480static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
2481{
2482 seq_printf(s, "filename: \"%pD2\"", f->nf_file);
2483}
2484
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002485static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002486{
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002487 struct inode *inode = f->nf_inode;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002488
2489 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2490 MAJOR(inode->i_sb->s_dev),
2491 MINOR(inode->i_sb->s_dev),
2492 inode->i_ino);
2493}
2494
2495static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2496{
2497 seq_printf(s, "owner: ");
2498 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2499}
2500
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002501static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
2502{
J. Bruce Fieldsee590d22020-04-13 22:03:06 -04002503 seq_printf(s, "0x%.8x", stid->si_generation);
2504 seq_printf(s, "%12phN", &stid->si_opaque);
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002505}
2506
J. Bruce Fields78599c42019-04-22 15:26:23 -04002507static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2508{
2509 struct nfs4_ol_stateid *ols;
2510 struct nfs4_file *nf;
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002511 struct nfsd_file *file;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002512 struct nfs4_stateowner *oo;
2513 unsigned int access, deny;
2514
2515 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2516 return 0; /* XXX: or SEQ_SKIP? */
2517 ols = openlockstateid(st);
2518 oo = ols->st_stateowner;
2519 nf = st->sc_file;
2520 file = find_any_file(nf);
J. Bruce Fields9affa432020-07-15 13:31:36 -04002521 if (!file)
2522 return 0;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002523
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002524 seq_printf(s, "- ");
2525 nfs4_show_stateid(s, &st->sc_stateid);
2526 seq_printf(s, ": { type: open, ");
J. Bruce Fields78599c42019-04-22 15:26:23 -04002527
2528 access = bmap_to_share_mode(ols->st_access_bmap);
2529 deny = bmap_to_share_mode(ols->st_deny_bmap);
2530
J. Bruce Fieldsc4b77ed2019-10-03 12:33:08 -04002531 seq_printf(s, "access: %s%s, ",
J. Bruce Fields78599c42019-04-22 15:26:23 -04002532 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2533 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
J. Bruce Fieldsc4b77ed2019-10-03 12:33:08 -04002534 seq_printf(s, "deny: %s%s, ",
J. Bruce Fields78599c42019-04-22 15:26:23 -04002535 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2536 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2537
2538 nfs4_show_superblock(s, file);
2539 seq_printf(s, ", ");
Achilles Gaikwad580da462020-04-20 18:20:31 +05302540 nfs4_show_fname(s, file);
2541 seq_printf(s, ", ");
J. Bruce Fields78599c42019-04-22 15:26:23 -04002542 nfs4_show_owner(s, oo);
2543 seq_printf(s, " }\n");
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002544 nfsd_file_put(file);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002545
2546 return 0;
2547}
2548
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002549static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2550{
2551 struct nfs4_ol_stateid *ols;
2552 struct nfs4_file *nf;
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002553 struct nfsd_file *file;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002554 struct nfs4_stateowner *oo;
2555
2556 ols = openlockstateid(st);
2557 oo = ols->st_stateowner;
2558 nf = st->sc_file;
2559 file = find_any_file(nf);
J. Bruce Fields9affa432020-07-15 13:31:36 -04002560 if (!file)
2561 return 0;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002562
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002563 seq_printf(s, "- ");
2564 nfs4_show_stateid(s, &st->sc_stateid);
2565 seq_printf(s, ": { type: lock, ");
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002566
2567 /*
2568 * Note: a lock stateid isn't really the same thing as a lock,
2569 * it's the locking state held by one owner on a file, and there
2570 * may be multiple (or no) lock ranges associated with it.
2571 * (Same for the matter is true of open stateids.)
2572 */
2573
2574 nfs4_show_superblock(s, file);
2575 /* XXX: open stateid? */
2576 seq_printf(s, ", ");
Achilles Gaikwad580da462020-04-20 18:20:31 +05302577 nfs4_show_fname(s, file);
2578 seq_printf(s, ", ");
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002579 nfs4_show_owner(s, oo);
2580 seq_printf(s, " }\n");
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002581 nfsd_file_put(file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002582
2583 return 0;
2584}
2585
2586static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2587{
2588 struct nfs4_delegation *ds;
2589 struct nfs4_file *nf;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04002590 struct nfsd_file *file;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002591
2592 ds = delegstateid(st);
2593 nf = st->sc_file;
J. Bruce Fields9affa432020-07-15 13:31:36 -04002594 file = find_deleg_file(nf);
2595 if (!file)
2596 return 0;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002597
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002598 seq_printf(s, "- ");
2599 nfs4_show_stateid(s, &st->sc_stateid);
2600 seq_printf(s, ": { type: deleg, ");
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002601
2602 /* Kinda dead code as long as we only support read delegs: */
2603 seq_printf(s, "access: %s, ",
2604 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2605
2606 /* XXX: lease time, whether it's being recalled. */
2607
2608 nfs4_show_superblock(s, file);
Achilles Gaikwad580da462020-04-20 18:20:31 +05302609 seq_printf(s, ", ");
2610 nfs4_show_fname(s, file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002611 seq_printf(s, " }\n");
J. Bruce Fields9affa432020-07-15 13:31:36 -04002612 nfsd_file_put(file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002613
2614 return 0;
2615}
2616
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002617static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2618{
2619 struct nfs4_layout_stateid *ls;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04002620 struct nfsd_file *file;
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002621
2622 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2623 file = ls->ls_file;
2624
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002625 seq_printf(s, "- ");
2626 nfs4_show_stateid(s, &st->sc_stateid);
2627 seq_printf(s, ": { type: layout, ");
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002628
2629 /* XXX: What else would be useful? */
2630
2631 nfs4_show_superblock(s, file);
Achilles Gaikwad580da462020-04-20 18:20:31 +05302632 seq_printf(s, ", ");
2633 nfs4_show_fname(s, file);
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002634 seq_printf(s, " }\n");
2635
2636 return 0;
2637}
2638
J. Bruce Fields78599c42019-04-22 15:26:23 -04002639static int states_show(struct seq_file *s, void *v)
2640{
2641 struct nfs4_stid *st = v;
2642
2643 switch (st->sc_type) {
2644 case NFS4_OPEN_STID:
2645 return nfs4_show_open(s, st);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002646 case NFS4_LOCK_STID:
2647 return nfs4_show_lock(s, st);
2648 case NFS4_DELEG_STID:
2649 return nfs4_show_deleg(s, st);
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002650 case NFS4_LAYOUT_STID:
2651 return nfs4_show_layout(s, st);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002652 default:
2653 return 0; /* XXX: or SEQ_SKIP? */
2654 }
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002655 /* XXX: copy stateids? */
J. Bruce Fields78599c42019-04-22 15:26:23 -04002656}
2657
2658static struct seq_operations states_seq_ops = {
2659 .start = states_start,
2660 .next = states_next,
2661 .stop = states_stop,
2662 .show = states_show
2663};
2664
2665static int client_states_open(struct inode *inode, struct file *file)
2666{
J. Bruce Fields78599c42019-04-22 15:26:23 -04002667 struct seq_file *s;
2668 struct nfs4_client *clp;
2669 int ret;
2670
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002671 clp = get_nfsdfs_clp(inode);
2672 if (!clp)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002673 return -ENXIO;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002674
2675 ret = seq_open(file, &states_seq_ops);
2676 if (ret)
2677 return ret;
2678 s = file->private_data;
2679 s->private = clp;
2680 return 0;
2681}
2682
2683static int client_opens_release(struct inode *inode, struct file *file)
2684{
2685 struct seq_file *m = file->private_data;
2686 struct nfs4_client *clp = m->private;
2687
2688 /* XXX: alternatively, we could get/drop in seq start/stop */
2689 drop_client(clp);
2690 return 0;
2691}
2692
2693static const struct file_operations client_states_fops = {
2694 .open = client_states_open,
2695 .read = seq_read,
2696 .llseek = seq_lseek,
2697 .release = client_opens_release,
2698};
2699
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002700/*
2701 * Normally we refuse to destroy clients that are in use, but here the
2702 * administrator is telling us to just do it. We also want to wait
2703 * so the caller has a guarantee that the client's locks are gone by
2704 * the time the write returns:
2705 */
YueHaibing297e57a2019-07-08 15:29:33 +08002706static void force_expire_client(struct nfs4_client *clp)
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002707{
2708 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2709 bool already_expired;
2710
Chuck Lever2958d2e2021-05-14 15:56:06 -04002711 trace_nfsd_clid_admin_expired(&clp->cl_clientid);
2712
J. Bruce Fieldsf7104cc2021-08-12 16:41:43 -04002713 spin_lock(&nn->client_lock);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002714 clp->cl_time = 0;
J. Bruce Fieldsf7104cc2021-08-12 16:41:43 -04002715 spin_unlock(&nn->client_lock);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002716
2717 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2718 spin_lock(&nn->client_lock);
2719 already_expired = list_empty(&clp->cl_lru);
2720 if (!already_expired)
2721 unhash_client_locked(clp);
2722 spin_unlock(&nn->client_lock);
2723
2724 if (!already_expired)
2725 expire_client(clp);
2726 else
2727 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2728}
2729
2730static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2731 size_t size, loff_t *pos)
2732{
2733 char *data;
2734 struct nfs4_client *clp;
2735
2736 data = simple_transaction_get(file, buf, size);
2737 if (IS_ERR(data))
2738 return PTR_ERR(data);
2739 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2740 return -EINVAL;
2741 clp = get_nfsdfs_clp(file_inode(file));
2742 if (!clp)
2743 return -ENXIO;
2744 force_expire_client(clp);
2745 drop_client(clp);
2746 return 7;
2747}
2748
2749static const struct file_operations client_ctl_fops = {
2750 .write = client_ctl_write,
2751 .release = simple_transaction_release,
2752};
2753
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002754static const struct tree_descr client_files[] = {
2755 [0] = {"info", &client_info_fops, S_IRUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002756 [1] = {"states", &client_states_fops, S_IRUSR},
Petr Vorel6cbfad52020-03-13 13:39:57 +01002757 [2] = {"ctl", &client_ctl_fops, S_IWUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002758 [3] = {""},
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002759};
2760
Jeff Layton2216d442012-11-12 15:00:57 -05002761static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002762 struct svc_rqst *rqstp, nfs4_verifier *verf)
2763{
2764 struct nfs4_client *clp;
2765 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002766 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002767 struct net *net = SVC_NET(rqstp);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002768 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
NeilBrown472d1552021-03-20 09:38:04 +11002769 struct dentry *dentries[ARRAY_SIZE(client_files)];
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002770
2771 clp = alloc_client(name);
2772 if (clp == NULL)
2773 return NULL;
2774
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002775 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2776 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002777 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002778 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002779 }
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002780 gen_clid(clp, nn);
2781 kref_init(&clp->cl_nfsdfs.cl_ref);
Christoph Hellwig0162ac22014-09-24 12:19:19 +02002782 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01002783 clp->cl_time = ktime_get_boottime_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002784 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002785 copy_verf(clp, verf);
J. Bruce Fields3bade242019-05-14 21:38:11 -04002786 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04002787 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002788 clp->net = net;
NeilBrown472d1552021-03-20 09:38:04 +11002789 clp->cl_nfsd_dentry = nfsd_client_mkdir(
2790 nn, &clp->cl_nfsdfs,
2791 clp->cl_clientid.cl_id - nn->clientid_base,
2792 client_files, dentries);
2793 clp->cl_nfsd_info_dentry = dentries[0];
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002794 if (!clp->cl_nfsd_dentry) {
2795 free_client(clp);
2796 return NULL;
2797 }
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002798 return clp;
2799}
2800
NeilBrownfd39ca92005-06-23 22:04:03 -07002801static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002802add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2803{
2804 struct rb_node **new = &(root->rb_node), *parent = NULL;
2805 struct nfs4_client *clp;
2806
2807 while (*new) {
2808 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2809 parent = *new;
2810
2811 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2812 new = &((*new)->rb_left);
2813 else
2814 new = &((*new)->rb_right);
2815 }
2816
2817 rb_link_node(&new_clp->cl_namenode, parent, new);
2818 rb_insert_color(&new_clp->cl_namenode, root);
2819}
2820
2821static struct nfs4_client *
2822find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2823{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002824 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002825 struct rb_node *node = root->rb_node;
2826 struct nfs4_client *clp;
2827
2828 while (node) {
2829 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2830 cmp = compare_blob(&clp->cl_name, name);
2831 if (cmp > 0)
2832 node = node->rb_left;
2833 else if (cmp < 0)
2834 node = node->rb_right;
2835 else
2836 return clp;
2837 }
2838 return NULL;
2839}
2840
2841static void
2842add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843{
2844 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002845 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
Trond Myklebust0a880a22014-07-30 08:27:10 -04002847 lockdep_assert_held(&nn->client_lock);
2848
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002849 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002850 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002852 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002853 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854}
2855
NeilBrownfd39ca92005-06-23 22:04:03 -07002856static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857move_to_confirmed(struct nfs4_client *clp)
2858{
2859 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002860 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
Trond Myklebust0a880a22014-07-30 08:27:10 -04002862 lockdep_assert_held(&nn->client_lock);
2863
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002864 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002865 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002866 add_clp_to_name_tree(clp, &nn->conf_name_tree);
J. Bruce Fields934bd072021-05-25 14:53:44 -04002867 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2868 trace_nfsd_clid_confirmed(&clp->cl_clientid);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002869 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870}
2871
2872static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002873find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874{
2875 struct nfs4_client *clp;
2876 unsigned int idhashval = clientid_hashval(clid->cl_id);
2877
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002878 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002879 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002880 if ((bool)clp->cl_minorversion != sessions)
2881 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002882 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 }
2886 return NULL;
2887}
2888
2889static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002890find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2891{
2892 struct list_head *tbl = nn->conf_id_hashtbl;
2893
Trond Myklebust0a880a22014-07-30 08:27:10 -04002894 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002895 return find_client_in_id_table(tbl, clid, sessions);
2896}
2897
2898static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002899find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002901 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
Trond Myklebust0a880a22014-07-30 08:27:10 -04002903 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002904 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905}
2906
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002907static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002908{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002909 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002910}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002911
NeilBrown28ce6052005-06-23 22:03:56 -07002912static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002913find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002914{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002915 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002916 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002917}
2918
2919static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002920find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002921{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002922 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002923 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002924}
2925
NeilBrownfd39ca92005-06-23 22:04:03 -07002926static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002927gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002929 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002930 struct sockaddr *sa = svc_addr(rqstp);
2931 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002932 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Jeff Layton7077ecb2009-08-14 12:57:58 -04002934 /* Currently, we only support tcp and tcp6 for the callback channel */
2935 if (se->se_callback_netid_len == 3 &&
2936 !memcmp(se->se_callback_netid_val, "tcp", 3))
2937 expected_family = AF_INET;
2938 else if (se->se_callback_netid_len == 4 &&
2939 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2940 expected_family = AF_INET6;
2941 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 goto out_err;
2943
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002944 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002945 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002946 (struct sockaddr *)&conn->cb_addr,
2947 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002948
J. Bruce Fields07263f12010-05-31 19:09:40 -04002949 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002951
J. Bruce Fields07263f12010-05-31 19:09:40 -04002952 if (conn->cb_addr.ss_family == AF_INET6)
2953 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002954
J. Bruce Fields07263f12010-05-31 19:09:40 -04002955 conn->cb_prog = se->se_callback_prog;
2956 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002957 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Chuck Lever1eace0d2020-04-05 14:15:29 -04002958 trace_nfsd_cb_args(clp, conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 return;
2960out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002961 conn->cb_addr.ss_family = AF_UNSPEC;
2962 conn->cb_addrlen = 0;
Chuck Lever1eace0d2020-04-05 14:15:29 -04002963 trace_nfsd_cb_nodelegs(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 return;
2965}
2966
Andy Adamson074fe892009-04-03 08:28:15 +03002967/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002968 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002969 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002970static void
Andy Adamson074fe892009-04-03 08:28:15 +03002971nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2972{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04002973 struct xdr_buf *buf = resp->xdr->buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002974 struct nfsd4_slot *slot = resp->cstate.slot;
2975 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002976
Andy Adamson557ce262009-08-28 08:45:04 -04002977 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002978
J. Bruce Fields085def32017-10-18 16:17:18 -04002979 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamson557ce262009-08-28 08:45:04 -04002980 slot->sl_opcnt = resp->opcnt;
2981 slot->sl_status = resp->cstate.status;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04002982 free_svc_cred(&slot->sl_cred);
2983 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
Andy Adamsonbf864a32009-04-03 08:28:35 +03002984
J. Bruce Fields085def32017-10-18 16:17:18 -04002985 if (!nfsd4_cache_this(resp)) {
2986 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002987 return;
2988 }
J. Bruce Fields085def32017-10-18 16:17:18 -04002989 slot->sl_flags |= NFSD4_SLOT_CACHED;
2990
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002991 base = resp->cstate.data_offset;
2992 slot->sl_datalen = buf->len - base;
2993 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Dan Carpenterd3f03402015-11-21 13:28:50 +03002994 WARN(1, "%s: sessions DRC could not cache compound\n",
2995 __func__);
Andy Adamson557ce262009-08-28 08:45:04 -04002996 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002997}
2998
2999/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04003000 * Encode the replay sequence operation from the slot values.
3001 * If cachethis is FALSE encode the uncached rep error on the next
3002 * operation which sets resp->p and increments resp->opcnt for
3003 * nfs4svc_encode_compoundres.
3004 *
Andy Adamson074fe892009-04-03 08:28:15 +03003005 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04003006static __be32
3007nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
3008 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03003009{
Andy Adamsonabfabf82009-07-23 19:02:18 -04003010 struct nfsd4_op *op;
3011 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03003012
Andy Adamsonabfabf82009-07-23 19:02:18 -04003013 /* Encode the replayed sequence operation */
3014 op = &args->ops[resp->opcnt - 1];
3015 nfsd4_encode_operation(resp, op);
3016
J. Bruce Fields085def32017-10-18 16:17:18 -04003017 if (slot->sl_flags & NFSD4_SLOT_CACHED)
3018 return op->status;
3019 if (args->opcnt == 1) {
3020 /*
3021 * The original operation wasn't a solo sequence--we
3022 * always cache those--so this retry must not match the
3023 * original:
3024 */
3025 op->status = nfserr_seq_false_retry;
3026 } else {
Andy Adamsonabfabf82009-07-23 19:02:18 -04003027 op = &args->ops[resp->opcnt++];
3028 op->status = nfserr_retry_uncached_rep;
3029 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03003030 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04003031 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03003032}
3033
3034/*
Andy Adamson557ce262009-08-28 08:45:04 -04003035 * The sequence operation is not cached because we can use the slot and
3036 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03003037 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04003038static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03003039nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
3040 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03003041{
Andy Adamson557ce262009-08-28 08:45:04 -04003042 struct nfsd4_slot *slot = resp->cstate.slot;
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003043 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04003044 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03003045 __be32 status;
3046
Andy Adamson557ce262009-08-28 08:45:04 -04003047 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03003048
Andy Adamsonabfabf82009-07-23 19:02:18 -04003049 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04003050 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04003051 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03003052
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04003053 p = xdr_reserve_space(xdr, slot->sl_datalen);
3054 if (!p) {
3055 WARN_ON_ONCE(1);
3056 return nfserr_serverfault;
3057 }
3058 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
3059 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03003060
Andy Adamson557ce262009-08-28 08:45:04 -04003061 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04003062 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03003063}
3064
Andy Adamson0733d212009-04-03 08:28:01 +03003065/*
3066 * Set the exchange_id flags returned by the server.
3067 */
3068static void
3069nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
3070{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003071#ifdef CONFIG_NFSD_PNFS
3072 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
3073#else
Andy Adamson0733d212009-04-03 08:28:01 +03003074 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003075#endif
Andy Adamson0733d212009-04-03 08:28:01 +03003076
3077 /* Referrals are supported, Migration is not. */
3078 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
3079
3080 /* set the wire flags to return to client. */
3081 clid->flags = new->cl_exchange_flags;
3082}
3083
J. Bruce Fields4eaea132015-10-15 16:11:01 -04003084static bool client_has_openowners(struct nfs4_client *clp)
3085{
3086 struct nfs4_openowner *oo;
3087
3088 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
3089 if (!list_empty(&oo->oo_owner.so_stateids))
3090 return true;
3091 }
3092 return false;
3093}
3094
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003095static bool client_has_state(struct nfs4_client *clp)
3096{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04003097 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08003098#ifdef CONFIG_NFSD_PNFS
3099 || !list_empty(&clp->cl_lo_states)
3100#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04003101 || !list_empty(&clp->cl_delegations)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04003102 || !list_empty(&clp->cl_sessions)
3103 || !list_empty(&clp->async_copies);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003104}
3105
J. Bruce Fields79123442019-06-05 12:42:05 -04003106static __be32 copy_impl_id(struct nfs4_client *clp,
3107 struct nfsd4_exchange_id *exid)
3108{
3109 if (!exid->nii_domain.data)
3110 return 0;
3111 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
3112 if (!clp->cl_nii_domain.data)
3113 return nfserr_jukebox;
3114 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
3115 if (!clp->cl_nii_name.data)
3116 return nfserr_jukebox;
Arnd Bergmanne29f4702019-10-31 14:52:43 +01003117 clp->cl_nii_time = exid->nii_time;
J. Bruce Fields79123442019-06-05 12:42:05 -04003118 return 0;
3119}
3120
Al Virob37ad282006-10-19 23:28:59 -07003121__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003122nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3123 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003124{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003125 struct nfsd4_exchange_id *exid = &u->exchange_id;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003126 struct nfs4_client *conf, *new;
3127 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003128 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04003129 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03003130 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04003131 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04003132 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003133 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03003134
Jeff Layton363168b2009-08-14 12:57:56 -04003135 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03003136 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Chuck Lever523ec6e2020-11-02 15:19:12 -05003137 "ip_addr=%s flags %x, spa_how %u\n",
Andy Adamson0733d212009-04-03 08:28:01 +03003138 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04003139 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03003140
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003141 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03003142 return nfserr_inval;
3143
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003144 new = create_client(exid->clname, rqstp, &verf);
3145 if (new == NULL)
3146 return nfserr_jukebox;
J. Bruce Fields79123442019-06-05 12:42:05 -04003147 status = copy_impl_id(new, exid);
3148 if (status)
3149 goto out_nolock;
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003150
Andy Adamson0733d212009-04-03 08:28:01 +03003151 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003152 case SP4_MACH_CRED:
Andrew Elbleed941642016-06-15 12:52:09 -04003153 exid->spo_must_enforce[0] = 0;
3154 exid->spo_must_enforce[1] = (
3155 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3156 1 << (OP_EXCHANGE_ID - 32) |
3157 1 << (OP_CREATE_SESSION - 32) |
3158 1 << (OP_DESTROY_SESSION - 32) |
3159 1 << (OP_DESTROY_CLIENTID - 32));
3160
3161 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3162 1 << (OP_OPEN_DOWNGRADE) |
3163 1 << (OP_LOCKU) |
3164 1 << (OP_DELEGRETURN));
3165
3166 exid->spo_must_allow[1] &= (
3167 1 << (OP_TEST_STATEID - 32) |
3168 1 << (OP_FREE_STATEID - 32));
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003169 if (!svc_rqst_integrity_protected(rqstp)) {
3170 status = nfserr_inval;
3171 goto out_nolock;
3172 }
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05003173 /*
3174 * Sometimes userspace doesn't give us a principal.
3175 * Which is a bug, really. Anyway, we can't enforce
3176 * MACH_CRED in that case, better to give up now:
3177 */
J. Bruce Fields414ca012015-11-20 10:48:02 -05003178 if (!new->cl_cred.cr_principal &&
3179 !new->cl_cred.cr_raw_principal) {
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05003180 status = nfserr_serverfault;
3181 goto out_nolock;
3182 }
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003183 new->cl_mach_cred = true;
Gustavo A. R. Silva76c50eb2020-11-20 12:26:40 -06003184 break;
Andy Adamson0733d212009-04-03 08:28:01 +03003185 case SP4_NONE:
3186 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003187 default: /* checked by xdr code */
3188 WARN_ON_ONCE(1);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003189 fallthrough;
Andy Adamson0733d212009-04-03 08:28:01 +03003190 case SP4_SSV:
Kinglong Mee8edf4b02016-02-26 22:36:42 +08003191 status = nfserr_encr_alg_unsupp;
3192 goto out_nolock;
Andy Adamson0733d212009-04-03 08:28:01 +03003193 }
3194
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003195 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003196 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003197 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03003198 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04003199 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3200 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3201
J. Bruce Fields136e6582012-05-12 20:37:23 -04003202 if (update) {
3203 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003204 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03003205 goto out;
3206 }
Andrew Elblededeb132016-06-15 12:52:08 -04003207 if (!nfsd4_mach_creds_match(conf, rqstp)) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003208 status = nfserr_wrong_cred;
3209 goto out;
3210 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003211 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03003212 status = nfserr_perm;
3213 goto out;
3214 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003215 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03003216 status = nfserr_not_same;
3217 goto out;
3218 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003219 /* case 6 */
3220 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
Chuck Levere8f80c52021-05-14 15:56:19 -04003221 trace_nfsd_clid_confirmed_r(conf);
J. Bruce Fields136e6582012-05-12 20:37:23 -04003222 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03003223 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003224 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003225 if (client_has_state(conf)) {
3226 status = nfserr_clid_inuse;
Chuck Lever27787732021-05-14 15:55:29 -04003227 trace_nfsd_clid_cred_mismatch(conf, rqstp);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003228 goto out;
3229 }
Andy Adamson0733d212009-04-03 08:28:01 +03003230 goto out_new;
3231 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003232 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04003233 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
Chuck Levere8f80c52021-05-14 15:56:19 -04003234 trace_nfsd_clid_confirmed_r(conf);
J. Bruce Fields136e6582012-05-12 20:37:23 -04003235 goto out_copy;
3236 }
3237 /* case 5, client reboot */
Chuck Lever744ea542021-05-14 15:55:36 -04003238 trace_nfsd_clid_verf_mismatch(conf, rqstp, &verf);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003239 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003240 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003241 }
3242
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003243 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003244 status = nfserr_noent;
3245 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03003246 }
3247
Chuck Levere8f80c52021-05-14 15:56:19 -04003248 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003249 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003250 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03003251
Chuck Levere8f80c52021-05-14 15:56:19 -04003252 /* case 1, new owner ID */
3253 trace_nfsd_clid_fresh(new);
3254
Andy Adamson0733d212009-04-03 08:28:01 +03003255out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04003256 if (conf) {
3257 status = mark_client_expired_locked(conf);
3258 if (status)
3259 goto out;
Chuck Lever2958d2e2021-05-14 15:56:06 -04003260 trace_nfsd_clid_replaced(&conf->cl_clientid);
Jeff Laytonfd699b82014-07-30 08:27:14 -04003261 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04003262 new->cl_minorversion = cstate->minorversion;
Andrew Elbleed941642016-06-15 12:52:09 -04003263 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3264 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
Andy Adamson0733d212009-04-03 08:28:01 +03003265
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003266 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003267 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03003268out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003269 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3270 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03003271
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003272 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3273 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03003274
3275 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003276 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03003277 status = nfs_ok;
3278
3279out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003280 spin_unlock(&nn->client_lock);
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003281out_nolock:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003282 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003283 expire_client(new);
Chuck Levere8f80c52021-05-14 15:56:19 -04003284 if (unconf) {
3285 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003286 expire_client(unconf);
Chuck Levere8f80c52021-05-14 15:56:19 -04003287 }
Andy Adamson0733d212009-04-03 08:28:01 +03003288 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003289}
3290
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003291static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04003292check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003293{
Andy Adamson88e588d2009-07-23 19:02:15 -04003294 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3295 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003296
3297 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04003298 if (slot_inuse) {
3299 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003300 return nfserr_jukebox;
3301 else
3302 return nfserr_seq_misordered;
3303 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05003304 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04003305 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03003306 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04003307 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003308 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003309 return nfserr_seq_misordered;
3310}
3311
Andy Adamson49557cc2009-07-23 19:02:16 -04003312/*
3313 * Cache the create session result into the create session single DRC
3314 * slot cache by saving the xdr structure. sl_seqid has been set.
3315 * Do this for solo or embedded create session operations.
3316 */
3317static void
3318nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003319 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04003320{
3321 slot->sl_status = nfserr;
3322 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3323}
3324
3325static __be32
3326nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3327 struct nfsd4_clid_slot *slot)
3328{
3329 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3330 return slot->sl_status;
3331}
3332
Mi Jinlong1b74c252011-07-14 14:50:17 +08003333#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3334 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3335 1 + /* MIN tag is length with zero, only length */ \
3336 3 + /* version, opcount, opcode */ \
3337 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3338 /* seqid, slotID, slotID, cache */ \
3339 4 ) * sizeof(__be32))
3340
3341#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3342 2 + /* verifier: AUTH_NULL, length 0 */\
3343 1 + /* status */ \
3344 1 + /* MIN tag is length with zero, only length */ \
3345 3 + /* opcount, opcode, opstatus*/ \
3346 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3347 /* seqid, slotID, slotID, slotID, status */ \
3348 5 ) * sizeof(__be32))
3349
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003350static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08003351{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003352 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3353
J. Bruce Fields373cd402013-04-08 15:42:12 -04003354 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3355 return nfserr_toosmall;
3356 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3357 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003358 ca->headerpadsz = 0;
3359 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3360 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3361 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3362 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3363 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3364 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3365 /*
3366 * Note decreasing slot size below client's request may make it
3367 * difficult for client to function correctly, whereas
3368 * decreasing the number of slots will (just?) affect
3369 * performance. When short on memory we therefore prefer to
3370 * decrease number of slots instead of their size. Clients that
3371 * request larger slots than they need will get poor results:
NeilBrown7f49fd5d2019-09-20 16:33:16 +10003372 * Note that we always allow at least one slot, because our
3373 * accounting is soft and provides no guarantees either way.
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003374 */
NeilBrown2030ca52019-09-20 16:36:45 +10003375 ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003376
J. Bruce Fields373cd402013-04-08 15:42:12 -04003377 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08003378}
3379
Chuck Lever45006322016-03-01 13:06:02 -05003380/*
3381 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3382 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3383 */
3384#define RPC_MAX_HEADER_WITH_AUTH_SYS \
3385 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3386
3387#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3388 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3389
Kinglong Mee8a891632013-12-23 18:11:02 +08003390#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003391 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003392#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003393 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3394 sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003395
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003396static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3397{
3398 ca->headerpadsz = 0;
3399
Kinglong Mee8a891632013-12-23 18:11:02 +08003400 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003401 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08003402 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003403 return nfserr_toosmall;
3404 ca->maxresp_cached = 0;
3405 if (ca->maxops < 2)
3406 return nfserr_toosmall;
3407
3408 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003409}
3410
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003411static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3412{
3413 switch (cbs->flavor) {
3414 case RPC_AUTH_NULL:
3415 case RPC_AUTH_UNIX:
3416 return nfs_ok;
3417 default:
3418 /*
3419 * GSS case: the spec doesn't allow us to return this
3420 * error. But it also doesn't allow us not to support
3421 * GSS.
3422 * I'd rather this fail hard than return some error the
3423 * client might think it can already handle:
3424 */
3425 return nfserr_encr_alg_unsupp;
3426 }
3427}
3428
Andy Adamson069b6ad2009-04-03 08:27:58 +03003429__be32
3430nfsd4_create_session(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003431 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003432{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003433 struct nfsd4_create_session *cr_ses = &u->create_session;
Jeff Layton363168b2009-08-14 12:57:56 -04003434 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003435 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003436 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003437 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003438 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003439 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003440 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003441 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003442
Mi Jinlonga62573d2011-03-23 17:57:07 +08003443 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3444 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003445 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3446 if (status)
3447 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003448 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04003449 if (status)
3450 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003451 status = check_backchannel_attrs(&cr_ses->back_channel);
3452 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08003453 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003454 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08003455 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003456 if (!new)
3457 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003458 conn = alloc_conn_from_crses(rqstp, cr_ses);
3459 if (!conn)
3460 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08003461
Jeff Laytond20c11d2014-07-30 08:27:07 -04003462 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003463 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003464 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003465 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003466
3467 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003468 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003469 if (!nfsd4_mach_creds_match(conf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003470 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003471 cs_slot = &conf->cl_cs_slot;
3472 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08003473 if (status) {
3474 if (status == nfserr_replay_cache)
3475 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003476 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003477 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003478 } else if (unconf) {
Chuck Lever27787732021-05-14 15:55:29 -04003479 status = nfserr_clid_inuse;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003480 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04003481 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Chuck Lever27787732021-05-14 15:55:29 -04003482 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003483 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003484 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04003485 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003486 if (!nfsd4_mach_creds_match(unconf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003487 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003488 cs_slot = &unconf->cl_cs_slot;
3489 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03003490 if (status) {
3491 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003492 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003493 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003494 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003495 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003496 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04003497 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003498 if (status) {
3499 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003500 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003501 }
Chuck Lever2958d2e2021-05-14 15:56:06 -04003502 trace_nfsd_clid_replaced(&old->cl_clientid);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003503 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04003504 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003505 conf = unconf;
3506 } else {
3507 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003508 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003509 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003510 status = nfs_ok;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003511 /* Persistent sessions are not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003512 cr_ses->flags &= ~SESSION4_PERSIST;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003513 /* Upshifting from TCP to RDMA is not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003514 cr_ses->flags &= ~SESSION4_RDMA;
3515
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003516 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003517 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003518
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003519 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003520 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04003521 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04003522 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003523
Jeff Laytond20c11d2014-07-30 08:27:07 -04003524 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04003525 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003526 spin_unlock(&nn->client_lock);
J. Bruce Fields934bd072021-05-25 14:53:44 -04003527 if (conf == unconf)
3528 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003529 /* init connection and backchannel */
3530 nfsd4_init_conn(rqstp, conn, new);
3531 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003532 if (old)
3533 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003534 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003535out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003536 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003537 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003538 if (old)
3539 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003540out_free_session:
3541 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003542out_release_drc_mem:
3543 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04003544 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003545}
3546
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003547static __be32 nfsd4_map_bcts_dir(u32 *dir)
3548{
3549 switch (*dir) {
3550 case NFS4_CDFC4_FORE:
3551 case NFS4_CDFC4_BACK:
3552 return nfs_ok;
3553 case NFS4_CDFC4_FORE_OR_BOTH:
3554 case NFS4_CDFC4_BACK_OR_BOTH:
3555 *dir = NFS4_CDFC4_BOTH;
3556 return nfs_ok;
zhengbinfc5fc5d2019-12-19 17:29:20 +08003557 }
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003558 return nfserr_inval;
3559}
3560
Christoph Hellwigeb698532017-05-08 20:58:35 +02003561__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3562 struct nfsd4_compound_state *cstate,
3563 union nfsd4_op_u *u)
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003564{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003565 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003566 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003567 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003568 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003569
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003570 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3571 if (status)
3572 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003573 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003574 session->se_cb_prog = bc->bc_cb_program;
3575 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003576 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003577
3578 nfsd4_probe_callback(session->se_client);
3579
3580 return nfs_ok;
3581}
3582
J. Bruce Fieldsc2d715a2020-04-27 17:59:01 -04003583static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3584{
3585 struct nfsd4_conn *c;
3586
3587 list_for_each_entry(c, &s->se_conns, cn_persession) {
3588 if (c->cn_xprt == xpt) {
3589 return c;
3590 }
3591 }
3592 return NULL;
3593}
3594
3595static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst,
Dai Ngo02579b22021-09-16 14:22:12 -04003596 struct nfsd4_session *session, u32 req, struct nfsd4_conn **conn)
J. Bruce Fieldsc2d715a2020-04-27 17:59:01 -04003597{
3598 struct nfs4_client *clp = session->se_client;
3599 struct svc_xprt *xpt = rqst->rq_xprt;
3600 struct nfsd4_conn *c;
3601 __be32 status;
3602
3603 /* Following the last paragraph of RFC 5661 Section 18.34.3: */
3604 spin_lock(&clp->cl_lock);
3605 c = __nfsd4_find_conn(xpt, session);
3606 if (!c)
3607 status = nfserr_noent;
3608 else if (req == c->cn_flags)
3609 status = nfs_ok;
3610 else if (req == NFS4_CDFC4_FORE_OR_BOTH &&
3611 c->cn_flags != NFS4_CDFC4_BACK)
3612 status = nfs_ok;
3613 else if (req == NFS4_CDFC4_BACK_OR_BOTH &&
3614 c->cn_flags != NFS4_CDFC4_FORE)
3615 status = nfs_ok;
3616 else
3617 status = nfserr_inval;
3618 spin_unlock(&clp->cl_lock);
Dai Ngo02579b22021-09-16 14:22:12 -04003619 if (status == nfs_ok && conn)
3620 *conn = c;
J. Bruce Fieldsc2d715a2020-04-27 17:59:01 -04003621 return status;
3622}
3623
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003624__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3625 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003626 union nfsd4_op_u *u)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003627{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003628 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003629 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003630 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003631 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003632 struct net *net = SVC_NET(rqstp);
3633 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003634
3635 if (!nfsd4_last_compound_op(rqstp))
3636 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003637 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003638 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003639 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003640 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003641 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003642 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003643 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003644 goto out;
Dai Ngo02579b22021-09-16 14:22:12 -04003645 status = nfsd4_match_existing_connection(rqstp, session,
3646 bcts->dir, &conn);
3647 if (status == nfs_ok) {
3648 if (bcts->dir == NFS4_CDFC4_FORE_OR_BOTH ||
3649 bcts->dir == NFS4_CDFC4_BACK)
3650 conn->cn_flags |= NFS4_CDFC4_BACK;
3651 nfsd4_probe_callback(session->se_client);
3652 goto out;
3653 }
3654 if (status == nfserr_inval)
J. Bruce Fieldsc2d715a2020-04-27 17:59:01 -04003655 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003656 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003657 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003658 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003659 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003660 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003661 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003662 goto out;
3663 nfsd4_init_conn(rqstp, conn, session);
3664 status = nfs_ok;
3665out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003666 nfsd4_put_session(session);
3667out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003668 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003669}
3670
J. Bruce Fields665d5072018-04-11 16:53:36 -04003671static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003672{
J. Bruce Fields665d5072018-04-11 16:53:36 -04003673 if (!cstate->session)
Fengguang Wu51d87bc2018-03-22 13:37:20 +08003674 return false;
J. Bruce Fields665d5072018-04-11 16:53:36 -04003675 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003676}
3677
Andy Adamson069b6ad2009-04-03 08:27:58 +03003678__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003679nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3680 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003681{
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003682 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003683 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003684 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003685 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003686 struct net *net = SVC_NET(r);
3687 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003688
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003689 status = nfserr_not_only_op;
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003690 if (nfsd4_compound_in_session(cstate, sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04003691 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003692 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003693 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04003694 }
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003695 dump_sessionid(__func__, sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003696 spin_lock(&nn->client_lock);
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003697 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003698 if (!ses)
3699 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003700 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003701 if (!nfsd4_mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003702 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003703 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003704 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003705 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003706 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003707 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003708
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05003709 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04003710
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003711 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003712 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003713out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003714 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003715out_client_lock:
3716 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003717out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03003718 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003719}
3720
J. Bruce Fields57266a62013-04-13 14:27:29 -04003721static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003722{
3723 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003724 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003725 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003726 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003727
3728 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003729 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003730 if (c)
3731 goto out_free;
3732 status = nfserr_conn_not_bound_to_session;
3733 if (clp->cl_mach_cred)
3734 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003735 __nfsd4_hash_conn(new, ses);
3736 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003737 ret = nfsd4_register_conn(new);
3738 if (ret)
3739 /* oops; xprt is already down: */
3740 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003741 return nfs_ok;
3742out_free:
3743 spin_unlock(&clp->cl_lock);
3744 free_conn(new);
3745 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003746}
3747
Mi Jinlong868b89c2011-04-27 09:09:58 +08003748static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3749{
3750 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3751
3752 return args->opcnt > session->se_fchannel.maxops;
3753}
3754
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003755static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3756 struct nfsd4_session *session)
3757{
3758 struct xdr_buf *xb = &rqstp->rq_arg;
3759
3760 return xb->len > session->se_fchannel.maxreq_sz;
3761}
3762
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003763static bool replay_matches_cache(struct svc_rqst *rqstp,
3764 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3765{
3766 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3767
3768 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3769 (bool)seq->cachethis)
3770 return false;
3771 /*
Scott Mayhew6e73e922019-10-09 15:11:37 -04003772 * If there's an error then the reply can have fewer ops than
3773 * the call.
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003774 */
Scott Mayhew6e73e922019-10-09 15:11:37 -04003775 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3776 return false;
3777 /*
3778 * But if we cached a reply with *more* ops than the call you're
3779 * sending us now, then this new call is clearly not really a
3780 * replay of the old one:
3781 */
3782 if (slot->sl_opcnt > argp->opcnt)
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003783 return false;
3784 /* This is the only check explicitly called by spec: */
3785 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3786 return false;
3787 /*
3788 * There may be more comparisons we could actually do, but the
3789 * spec doesn't require us to catch every case where the calls
3790 * don't match (that would require caching the call as well as
3791 * the reply), so we don't bother.
3792 */
3793 return true;
3794}
3795
Andy Adamson069b6ad2009-04-03 08:27:58 +03003796__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003797nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3798 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003799{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003800 struct nfsd4_sequence *seq = &u->sequence;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003801 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003802 struct xdr_stream *xdr = resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003803 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003804 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003805 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003806 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003807 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003808 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003809 struct net *net = SVC_NET(rqstp);
3810 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003811
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003812 if (resp->opcnt != 1)
3813 return nfserr_sequence_pos;
3814
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003815 /*
3816 * Will be either used or freed by nfsd4_sequence_check_conn
3817 * below.
3818 */
3819 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3820 if (!conn)
3821 return nfserr_jukebox;
3822
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003823 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003824 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003825 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003826 goto out_no_session;
3827 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003828
Mi Jinlong868b89c2011-04-27 09:09:58 +08003829 status = nfserr_too_many_ops;
3830 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003831 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003832
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003833 status = nfserr_req_too_big;
3834 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003835 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003836
Benny Halevyb85d4c02009-04-03 08:28:08 +03003837 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003838 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003839 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003840
Andy Adamson557ce262009-08-28 08:45:04 -04003841 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003842 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3843
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003844 /* We do not negotiate the number of slots yet, so set the
3845 * maxslots to the session maxreqs which is used to encode
3846 * sr_highest_slotid and the sr_target_slot id to maxslots */
3847 seq->maxslots = session->se_fchannel.maxreqs;
3848
J. Bruce Fields73e79482012-02-13 16:39:00 -05003849 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3850 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003851 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003852 status = nfserr_seq_misordered;
3853 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003854 goto out_put_session;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003855 status = nfserr_seq_false_retry;
3856 if (!replay_matches_cache(rqstp, seq, slot))
3857 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003858 cstate->slot = slot;
3859 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003860 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003861 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003862 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003863 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003864 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003865 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003866 }
3867 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003868 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003869
J. Bruce Fields57266a62013-04-13 14:27:29 -04003870 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003871 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003872 if (status)
3873 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003874
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003875 buflen = (seq->cachethis) ?
3876 session->se_fchannel.maxresp_cached :
3877 session->se_fchannel.maxresp_sz;
3878 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3879 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003880 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003881 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003882 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003883
3884 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003885 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003886 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003887 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003888 if (seq->cachethis)
3889 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003890 else
3891 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003892
3893 cstate->slot = slot;
3894 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003895 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003896
Benny Halevyb85d4c02009-04-03 08:28:08 +03003897out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003898 switch (clp->cl_cb_state) {
3899 case NFSD4_CB_DOWN:
3900 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3901 break;
3902 case NFSD4_CB_FAULT:
3903 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3904 break;
3905 default:
3906 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003907 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003908 if (!list_empty(&clp->cl_revoked))
3909 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003910out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08003911 if (conn)
3912 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003913 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003914 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003915out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003916 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003917 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003918}
3919
Trond Myklebustb6076642014-06-30 11:48:35 -04003920void
3921nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3922{
3923 struct nfsd4_compound_state *cs = &resp->cstate;
3924
3925 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04003926 if (cs->status != nfserr_replay_cache) {
3927 nfsd4_store_cache_entry(resp);
3928 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3929 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003930 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04003931 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003932 } else if (cs->clp)
3933 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04003934}
3935
Mi Jinlong345c2842011-10-20 17:51:39 +08003936__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003937nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3938 struct nfsd4_compound_state *cstate,
3939 union nfsd4_op_u *u)
Mi Jinlong345c2842011-10-20 17:51:39 +08003940{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003941 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003942 struct nfs4_client *conf, *unconf;
3943 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003944 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003945 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08003946
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003947 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003948 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003949 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003950 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08003951
3952 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04003953 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08003954 status = nfserr_clientid_busy;
3955 goto out;
3956 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04003957 status = mark_client_expired_locked(conf);
3958 if (status)
3959 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003960 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08003961 } else if (unconf)
3962 clp = unconf;
3963 else {
3964 status = nfserr_stale_clientid;
3965 goto out;
3966 }
Andrew Elblededeb132016-06-15 12:52:08 -04003967 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003968 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003969 status = nfserr_wrong_cred;
3970 goto out;
3971 }
Chuck Leverc41a9b72021-05-14 15:56:00 -04003972 trace_nfsd_clid_destroyed(&clp->cl_clientid);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003973 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003974out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003975 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003976 if (clp)
3977 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003978 return status;
3979}
3980
Andy Adamson069b6ad2009-04-03 08:27:58 +03003981__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003982nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3983 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003984{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003985 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05003986 struct nfs4_client *clp = cstate->clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003987 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003988
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003989 if (rc->rca_one_fs) {
3990 if (!cstate->current_fh.fh_dentry)
3991 return nfserr_nofilehandle;
3992 /*
3993 * We don't take advantage of the rca_one_fs case.
3994 * That's OK, it's optional, we can safely ignore it.
3995 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02003996 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003997 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003998
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003999 status = nfserr_complete_already;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05004000 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08004001 goto out;
4002
4003 status = nfserr_stale_clientid;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05004004 if (is_client_expired(clp))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04004005 /*
4006 * The following error isn't really legal.
4007 * But we only get here if the client just explicitly
4008 * destroyed the client. Surely it no longer cares what
4009 * error it gets back on an operation for the dead
4010 * client.
4011 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08004012 goto out;
4013
4014 status = nfs_ok;
Chuck Levercee8aa02021-05-14 15:55:54 -04004015 trace_nfsd_clid_reclaim_complete(&clp->cl_clientid);
J. Bruce Fieldsec596592021-01-21 17:57:45 -05004016 nfsd4_client_record_create(clp);
4017 inc_reclaim_complete(clp);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08004018out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08004019 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04004020}
4021
4022__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004023nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02004024 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025{
Christoph Hellwigeb698532017-05-08 20:58:35 +02004026 struct nfsd4_setclientid *setclid = &u->setclientid;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04004027 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04004029 struct nfs4_client *conf, *new;
4030 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004031 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03004032 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4033
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04004034 new = create_client(clname, rqstp, &clverifier);
4035 if (new == NULL)
4036 return nfserr_jukebox;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04004037 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004038 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04004039 if (conf && client_has_state(conf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05004041 if (clp_used_exchangeid(conf))
4042 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04004043 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Chuck Lever27787732021-05-14 15:55:29 -04004044 trace_nfsd_clid_cred_mismatch(conf, rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 goto out;
4046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03004048 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04004049 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04004050 unhash_client_locked(unconf);
Chuck Lever744ea542021-05-14 15:55:36 -04004051 if (conf) {
4052 if (same_verf(&conf->cl_verifier, &clverifier)) {
4053 copy_clid(new, conf);
4054 gen_confirm(new, nn);
4055 } else
4056 trace_nfsd_clid_verf_mismatch(conf, rqstp,
4057 &clverifier);
Chuck Lever237f91c2021-05-14 15:56:13 -04004058 } else
4059 trace_nfsd_clid_fresh(new);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04004060 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09004061 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05004062 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
4064 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
4065 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04004066 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 status = nfs_ok;
4068out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04004069 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04004070 if (new)
4071 free_client(new);
Chuck Lever237f91c2021-05-14 15:56:13 -04004072 if (unconf) {
4073 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04004074 expire_client(unconf);
Chuck Lever237f91c2021-05-14 15:56:13 -04004075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 return status;
4077}
4078
Al Virob37ad282006-10-19 23:28:59 -07004079__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004080nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02004081 struct nfsd4_compound_state *cstate,
4082 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083{
Christoph Hellwigeb698532017-05-08 20:58:35 +02004084 struct nfsd4_setclientid_confirm *setclientid_confirm =
4085 &u->setclientid_confirm;
NeilBrown21ab45a2005-06-23 22:04:14 -07004086 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04004087 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
4089 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07004090 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004091 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004093 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07004095
Jeff Laytond20c11d2014-07-30 08:27:07 -04004096 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004097 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004098 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05004099 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04004100 * We try hard to give out unique clientid's, so if we get an
4101 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04004102 * the client may be buggy; this should never happen.
4103 *
4104 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05004105 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04004106 status = nfserr_clid_inuse;
Chuck Lever27787732021-05-14 15:55:29 -04004107 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
4108 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
J. Bruce Fields8695b902012-05-19 10:05:58 -04004109 goto out;
Chuck Lever27787732021-05-14 15:55:29 -04004110 }
4111 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4112 trace_nfsd_clid_cred_mismatch(conf, rqstp);
J. Bruce Fields8695b902012-05-19 10:05:58 -04004113 goto out;
Chuck Lever27787732021-05-14 15:55:29 -04004114 }
J. Bruce Fields90d700b2012-05-19 13:55:22 -04004115 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04004116 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 status = nfs_ok;
Chuck Lever237f91c2021-05-14 15:56:13 -04004118 } else
J. Bruce Fields90d700b2012-05-19 13:55:22 -04004119 status = nfserr_stale_clientid;
4120 goto out;
4121 }
4122 status = nfs_ok;
Chuck Lever237f91c2021-05-14 15:56:13 -04004123 if (conf) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04004124 old = unconf;
4125 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04004126 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
Chuck Lever237f91c2021-05-14 15:56:13 -04004127 } else {
Jeff Laytond20c11d2014-07-30 08:27:07 -04004128 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4129 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04004130 status = nfserr_clid_inuse;
4131 if (client_has_state(old)
4132 && !same_creds(&unconf->cl_cred,
Dai Ngoab451ea2022-01-26 13:13:38 -08004133 &old->cl_cred)) {
4134 old = NULL;
J. Bruce Fields2b634822015-10-15 15:33:23 -04004135 goto out;
Dai Ngoab451ea2022-01-26 13:13:38 -08004136 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04004137 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04004138 if (status) {
4139 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04004140 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04004141 }
Chuck Lever2958d2e2021-05-14 15:56:06 -04004142 trace_nfsd_clid_replaced(&old->cl_clientid);
J. Bruce Fields221a6872013-04-01 22:23:49 -04004143 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04004144 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04004145 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07004146 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04004147 get_client_locked(conf);
4148 spin_unlock(&nn->client_lock);
J. Bruce Fields934bd072021-05-25 14:53:44 -04004149 if (conf == unconf)
4150 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
Jeff Laytond20c11d2014-07-30 08:27:07 -04004151 nfsd4_probe_callback(conf);
4152 spin_lock(&nn->client_lock);
4153 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04004155 spin_unlock(&nn->client_lock);
4156 if (old)
4157 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 return status;
4159}
4160
J. Bruce Fields32513b42011-10-13 16:00:16 -04004161static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162{
J. Bruce Fields32513b42011-10-13 16:00:16 -04004163 return kmem_cache_alloc(file_slab, GFP_KERNEL);
4164}
4165
4166/* OPEN Share state helper functions */
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004167static void nfsd4_init_file(struct svc_fh *fh, unsigned int hashval,
Jeff Layton5b095e92014-10-23 08:01:02 -04004168 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04004169{
Trond Myklebust950e0112014-06-30 11:48:31 -04004170 lockdep_assert_held(&state_lock);
4171
Elena Reshetova818a34e2017-10-20 12:53:30 +03004172 refcount_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004173 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04004174 INIT_LIST_HEAD(&fp->fi_stateids);
4175 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004176 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004177 fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle);
Jeff Layton0c637be2014-08-22 12:05:43 -04004178 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004179 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004180 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004181 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4182 memset(fp->fi_access, 0, sizeof(fp->fi_access));
J. Bruce Fieldsa0ce4832021-04-16 14:00:16 -04004183 fp->fi_aliased = false;
4184 fp->fi_inode = d_inode(fh->fh_dentry);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004185#ifdef CONFIG_NFSD_PNFS
4186 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02004187 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004188#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04004189 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190}
4191
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004192void
NeilBrowne60d4392005-06-23 22:03:01 -07004193nfsd4_free_slabs(void)
4194{
Jeff Layton9258a2d2018-03-16 09:47:22 -04004195 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004196 kmem_cache_destroy(openowner_slab);
4197 kmem_cache_destroy(lockowner_slab);
4198 kmem_cache_destroy(file_slab);
4199 kmem_cache_destroy(stateid_slab);
4200 kmem_cache_destroy(deleg_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04004201 kmem_cache_destroy(odstate_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07004202}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
Bryan Schumaker72083392011-11-01 15:24:59 -04004204int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205nfsd4_init_slabs(void)
4206{
Jeff Layton9258a2d2018-03-16 09:47:22 -04004207 client_slab = kmem_cache_create("nfsd4_clients",
4208 sizeof(struct nfs4_client), 0, 0, NULL);
4209 if (client_slab == NULL)
4210 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004211 openowner_slab = kmem_cache_create("nfsd4_openowners",
4212 sizeof(struct nfs4_openowner), 0, 0, NULL);
4213 if (openowner_slab == NULL)
Jeff Layton9258a2d2018-03-16 09:47:22 -04004214 goto out_free_client_slab;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004215 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08004216 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004217 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004218 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07004219 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09004220 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07004221 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004222 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07004223 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004224 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07004225 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004226 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07004227 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09004228 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07004229 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004230 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004231 odstate_slab = kmem_cache_create("nfsd4_odstate",
4232 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4233 if (odstate_slab == NULL)
4234 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07004236
Sachin Bhamare8287f002015-04-27 14:50:14 +02004237out_free_deleg_slab:
4238 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004239out_free_stateid_slab:
4240 kmem_cache_destroy(stateid_slab);
4241out_free_file_slab:
4242 kmem_cache_destroy(file_slab);
4243out_free_lockowner_slab:
4244 kmem_cache_destroy(lockowner_slab);
4245out_free_openowner_slab:
4246 kmem_cache_destroy(openowner_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04004247out_free_client_slab:
4248 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004249out:
NeilBrowne60d4392005-06-23 22:03:01 -07004250 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251}
4252
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004253static void init_nfs4_replay(struct nfs4_replay *rp)
4254{
4255 rp->rp_status = nfserr_serverfault;
4256 rp->rp_buflen = 0;
4257 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04004258 mutex_init(&rp->rp_mutex);
4259}
4260
4261static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4262 struct nfs4_stateowner *so)
4263{
4264 if (!nfsd4_has_session(cstate)) {
4265 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004266 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004267 }
4268}
4269
4270void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4271{
4272 struct nfs4_stateowner *so = cstate->replay_owner;
4273
4274 if (so != NULL) {
4275 cstate->replay_owner = NULL;
4276 mutex_unlock(&so->so_replay.rp_mutex);
4277 nfs4_put_stateowner(so);
4278 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004279}
4280
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004281static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282{
4283 struct nfs4_stateowner *sop;
4284
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004285 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004286 if (!sop)
4287 return NULL;
4288
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04004289 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004290 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004291 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004292 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004294
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004295 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004296 sop->so_client = clp;
4297 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04004298 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004299 return sop;
4300}
4301
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004302static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004303{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004304 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004305
Trond Myklebustd4f04892014-07-29 21:34:36 -04004306 list_add(&oo->oo_owner.so_strhash,
4307 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004308 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309}
4310
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004311static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4312{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004313 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004314}
4315
Jeff Layton6b180f02014-07-29 21:34:26 -04004316static void nfs4_free_openowner(struct nfs4_stateowner *so)
4317{
4318 struct nfs4_openowner *oo = openowner(so);
4319
4320 kmem_cache_free(openowner_slab, oo);
4321}
4322
4323static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004324 .so_unhash = nfs4_unhash_openowner,
4325 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004326};
4327
Andrew Elble7fc05642015-11-05 20:42:43 -05004328static struct nfs4_ol_stateid *
4329nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4330{
4331 struct nfs4_ol_stateid *local, *ret = NULL;
4332 struct nfs4_openowner *oo = open->op_openowner;
4333
4334 lockdep_assert_held(&fp->fi_lock);
4335
4336 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4337 /* ignore lock owners */
4338 if (local->st_stateowner->so_is_open_owner == 0)
4339 continue;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004340 if (local->st_stateowner != &oo->oo_owner)
4341 continue;
4342 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
Andrew Elble7fc05642015-11-05 20:42:43 -05004343 ret = local;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004344 refcount_inc(&ret->st_stid.sc_count);
Andrew Elble7fc05642015-11-05 20:42:43 -05004345 break;
4346 }
4347 }
4348 return ret;
4349}
4350
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004351static __be32
4352nfsd4_verify_open_stid(struct nfs4_stid *s)
4353{
4354 __be32 ret = nfs_ok;
4355
4356 switch (s->sc_type) {
4357 default:
4358 break;
Trond Myklebust4f176412018-01-12 17:42:30 -05004359 case 0:
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004360 case NFS4_CLOSED_STID:
4361 case NFS4_CLOSED_DELEG_STID:
4362 ret = nfserr_bad_stateid;
4363 break;
4364 case NFS4_REVOKED_DELEG_STID:
4365 ret = nfserr_deleg_revoked;
4366 }
4367 return ret;
4368}
4369
4370/* Lock the stateid st_mutex, and deal with races with CLOSE */
4371static __be32
4372nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4373{
4374 __be32 ret;
4375
Andrew Elble4f34bd02017-11-08 17:29:51 -05004376 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004377 ret = nfsd4_verify_open_stid(&stp->st_stid);
4378 if (ret != nfs_ok)
4379 mutex_unlock(&stp->st_mutex);
4380 return ret;
4381}
4382
4383static struct nfs4_ol_stateid *
4384nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4385{
4386 struct nfs4_ol_stateid *stp;
4387 for (;;) {
4388 spin_lock(&fp->fi_lock);
4389 stp = nfsd4_find_existing_open(fp, open);
4390 spin_unlock(&fp->fi_lock);
4391 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4392 break;
4393 nfs4_put_stid(&stp->st_stid);
4394 }
4395 return stp;
4396}
4397
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004398static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04004399alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004400 struct nfsd4_compound_state *cstate)
4401{
Trond Myklebust13d6f662014-06-30 11:48:45 -04004402 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004403 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004405 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4406 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04004408 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004409 oo->oo_owner.so_is_open_owner = 1;
4410 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004411 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004412 if (nfsd4_has_session(cstate))
4413 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004414 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004415 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004416 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04004417 spin_lock(&clp->cl_lock);
4418 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004419 if (ret == NULL) {
4420 hash_openowner(oo, clp, strhashval);
4421 ret = oo;
4422 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08004423 nfs4_free_stateowner(&oo->oo_owner);
4424
Trond Myklebustd4f04892014-07-29 21:34:36 -04004425 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04004426 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427}
4428
Andrew Elble7fc05642015-11-05 20:42:43 -05004429static struct nfs4_ol_stateid *
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004430init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
Andrew Elble7fc05642015-11-05 20:42:43 -05004431{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
Andrew Elble7fc05642015-11-05 20:42:43 -05004433 struct nfs4_openowner *oo = open->op_openowner;
4434 struct nfs4_ol_stateid *retstp = NULL;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004435 struct nfs4_ol_stateid *stp;
Andrew Elble7fc05642015-11-05 20:42:43 -05004436
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004437 stp = open->op_stp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004438 /* We are moving these outside of the spinlocks to avoid the warnings */
4439 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05004440 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004441
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004442retry:
Andrew Elble7fc05642015-11-05 20:42:43 -05004443 spin_lock(&oo->oo_owner.so_client->cl_lock);
4444 spin_lock(&fp->fi_lock);
4445
4446 retstp = nfsd4_find_existing_open(fp, open);
4447 if (retstp)
4448 goto out_unlock;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004449
4450 open->op_stp = NULL;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004451 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004452 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004453 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004454 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07004455 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004456 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 stp->st_access_bmap = 0;
4458 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07004459 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04004460 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004461 list_add(&stp->st_perfile, &fp->fi_stateids);
Andrew Elble7fc05642015-11-05 20:42:43 -05004462
4463out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04004464 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004465 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004466 if (retstp) {
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004467 /* Handle races with CLOSE */
4468 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4469 nfs4_put_stid(&retstp->st_stid);
4470 goto retry;
4471 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004472 /* To keep mutex tracking happy */
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004473 mutex_unlock(&stp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004474 stp = retstp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004475 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004476 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477}
4478
Jeff Laytond3134b12014-07-29 21:34:32 -04004479/*
4480 * In the 4.0 case we need to keep the owners around a little while to handle
4481 * CLOSE replay. We still do need to release any file access that is held by
4482 * them before returning however.
4483 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484static void
Jeff Laytond3134b12014-07-29 21:34:32 -04004485move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486{
Jeff Layton217526e2014-07-30 08:27:11 -04004487 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04004488 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4489 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4490 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004491
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004492 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493
Jeff Laytonb401be222014-07-29 21:34:33 -04004494 /*
4495 * We know that we hold one reference via nfsd4_close, and another
4496 * "persistent" reference for the client. If the refcount is higher
4497 * than 2, then there are still calls in progress that are using this
4498 * stateid. We can't put the sc_file reference until they are finished.
4499 * Wait for the refcount to drop to 2. Since it has been unhashed,
4500 * there should be no danger of the refcount going back up again at
4501 * this point.
4502 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004503 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
Jeff Laytonb401be222014-07-29 21:34:33 -04004504
Jeff Laytond3134b12014-07-29 21:34:32 -04004505 release_all_access(s);
4506 if (s->st_stid.sc_file) {
4507 put_nfs4_file(s->st_stid.sc_file);
4508 s->st_stid.sc_file = NULL;
4509 }
Jeff Layton217526e2014-07-30 08:27:11 -04004510
4511 spin_lock(&nn->client_lock);
4512 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004513 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004514 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01004515 oo->oo_time = ktime_get_boottime_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04004516 spin_unlock(&nn->client_lock);
4517 if (last)
4518 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519}
4520
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521/* search file_hashtbl[] for file */
4522static struct nfs4_file *
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004523find_file_locked(struct svc_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 struct nfs4_file *fp;
4526
Madhuparna Bhowmik36a80492020-02-13 20:33:31 +05304527 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4528 lockdep_is_held(&state_lock)) {
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004529 if (fh_match(&fp->fi_fhandle, &fh->fh_handle)) {
Elena Reshetova818a34e2017-10-20 12:53:30 +03004530 if (refcount_inc_not_zero(&fp->fi_ref))
Jeff Layton5b095e92014-10-23 08:01:02 -04004531 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07004532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 }
4534 return NULL;
4535}
4536
J. Bruce Fieldsa0ce4832021-04-16 14:00:16 -04004537static struct nfs4_file *insert_file(struct nfs4_file *new, struct svc_fh *fh,
4538 unsigned int hashval)
4539{
4540 struct nfs4_file *fp;
4541 struct nfs4_file *ret = NULL;
4542 bool alias_found = false;
4543
4544 spin_lock(&state_lock);
4545 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4546 lockdep_is_held(&state_lock)) {
4547 if (fh_match(&fp->fi_fhandle, &fh->fh_handle)) {
4548 if (refcount_inc_not_zero(&fp->fi_ref))
4549 ret = fp;
4550 } else if (d_inode(fh->fh_dentry) == fp->fi_inode)
4551 fp->fi_aliased = alias_found = true;
4552 }
4553 if (likely(ret == NULL)) {
4554 nfsd4_init_file(fh, hashval, new);
4555 new->fi_aliased = alias_found;
4556 ret = new;
4557 }
4558 spin_unlock(&state_lock);
4559 return ret;
4560}
4561
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004562static struct nfs4_file * find_file(struct svc_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004563{
4564 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004565 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04004566
Jeff Layton5b095e92014-10-23 08:01:02 -04004567 rcu_read_lock();
4568 fp = find_file_locked(fh, hashval);
4569 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04004570 return fp;
4571}
4572
4573static struct nfs4_file *
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004574find_or_add_file(struct nfs4_file *new, struct svc_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004575{
4576 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004577 unsigned int hashval = file_hashval(fh);
4578
4579 rcu_read_lock();
4580 fp = find_file_locked(fh, hashval);
4581 rcu_read_unlock();
4582 if (fp)
4583 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04004584
J. Bruce Fieldsa0ce4832021-04-16 14:00:16 -04004585 return insert_file(new, fh, hashval);
Trond Myklebust950e0112014-06-30 11:48:31 -04004586}
4587
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04004588/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 * Called to check deny when READ with all zero stateid or
4590 * WRITE with all zero or all one stateid
4591 */
Al Virob37ad282006-10-19 23:28:59 -07004592static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4594{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004596 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004598 fp = find_file(current_fh);
NeilBrown13cd2182005-06-23 22:03:10 -07004599 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004600 return ret;
4601 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04004602 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004603 if (fp->fi_share_deny & deny_type)
4604 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04004605 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07004606 put_nfs4_file(fp);
4607 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608}
4609
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004610static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004612 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04004613 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4614 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004615
Trond Myklebust11b91642014-07-29 21:34:08 -04004616 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04004617
Jeff Layton02e12152014-07-16 10:31:57 -04004618 /*
4619 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04004620 * already holding inode->i_lock.
4621 *
Jeff Laytondff13992014-07-08 14:02:49 -04004622 * If the dl_time != 0, then we know that it has already been
4623 * queued for a lease break. Don't queue it again.
4624 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04004625 spin_lock(&state_lock);
J. Bruce Fields548ec082021-11-29 15:08:00 -05004626 if (delegation_hashed(dp) && dp->dl_time == 0) {
Arnd Bergmann20b7d862019-11-04 16:31:52 +01004627 dp->dl_time = ktime_get_boottime_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04004628 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04004629 }
Jeff Layton02e12152014-07-16 10:31:57 -04004630 spin_unlock(&state_lock);
4631}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004633static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4634 struct rpc_task *task)
4635{
4636 struct nfs4_delegation *dp = cb_to_delegation(cb);
4637
J. Bruce Fields12ed22f2020-07-31 16:20:05 -04004638 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID ||
4639 dp->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID)
Andrew Elblea4579742015-08-31 12:06:41 -04004640 return 1;
4641
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004642 switch (task->tk_status) {
4643 case 0:
4644 return 1;
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004645 case -NFS4ERR_DELAY:
4646 rpc_delay(task, 2 * HZ);
4647 return 0;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004648 case -EBADHANDLE:
4649 case -NFS4ERR_BAD_STATEID:
4650 /*
4651 * Race: client probably got cb_recall before open reply
4652 * granting delegation.
4653 */
4654 if (dp->dl_retries--) {
4655 rpc_delay(task, 2 * HZ);
4656 return 0;
4657 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004658 fallthrough;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004659 default:
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004660 return 1;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004661 }
4662}
4663
4664static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4665{
4666 struct nfs4_delegation *dp = cb_to_delegation(cb);
4667
4668 nfs4_put_stid(&dp->dl_stid);
4669}
4670
Julia Lawallc4cb8972015-11-21 22:57:39 +01004671static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004672 .prepare = nfsd4_cb_recall_prepare,
4673 .done = nfsd4_cb_recall_done,
4674 .release = nfsd4_cb_recall_release,
4675};
4676
Jeff Layton02e12152014-07-16 10:31:57 -04004677static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4678{
4679 /*
4680 * We're assuming the state code never drops its reference
4681 * without first removing the lease. Since we're in this lease
J. Bruce Fields4a269efb2018-04-13 17:34:35 -04004682 * callback (and since the lease code is serialized by the
4683 * i_lock) we know the server hasn't removed the lease yet, and
4684 * we know it's safe to take a reference.
Jeff Layton02e12152014-07-16 10:31:57 -04004685 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004686 refcount_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02004687 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004688}
4689
Jeff Layton1c8c6012013-06-21 08:58:15 -04004690/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004691static bool
4692nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004693{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004694 bool ret = false;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004695 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4696 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004697
Chuck Lever17d76dd2021-05-14 15:57:20 -04004698 trace_nfsd_cb_recall(&dp->dl_stid);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04004699
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004700 /*
4701 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004702 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004703 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004704 */
4705 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706
Jeff Layton02e12152014-07-16 10:31:57 -04004707 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004708 fp->fi_had_conflict = true;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004709 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04004710 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004711 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712}
4713
J. Bruce Fields28df3d12017-07-28 16:35:15 -04004714static bool nfsd_breaker_owns_lease(struct file_lock *fl)
4715{
4716 struct nfs4_delegation *dl = fl->fl_owner;
4717 struct svc_rqst *rqst;
4718 struct nfs4_client *clp;
4719
4720 if (!i_am_nfsd())
4721 return NULL;
4722 rqst = kthread_data(current);
J. Bruce Fields13956162020-09-25 10:12:39 -04004723 /* Note rq_prog == NFS_ACL_PROGRAM is also possible: */
4724 if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4)
J. Bruce Fields34b09af2020-08-05 15:10:11 -04004725 return NULL;
J. Bruce Fields28df3d12017-07-28 16:35:15 -04004726 clp = *(rqst->rq_lease_breaker);
4727 return dl->dl_stid.sc_client == clp;
4728}
4729
Jeff Laytonc45198e2014-09-01 07:12:07 -04004730static int
Jeff Layton7448cc32015-01-16 15:05:57 -05004731nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4732 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733{
4734 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04004735 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 else
4737 return -EAGAIN;
4738}
4739
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004740static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields28df3d12017-07-28 16:35:15 -04004741 .lm_breaker_owns_lease = nfsd_breaker_owns_lease,
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04004742 .lm_break = nfsd_break_deleg_cb,
4743 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744};
4745
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004746static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4747{
4748 if (nfsd4_has_session(cstate))
4749 return nfs_ok;
4750 if (seqid == so->so_seqid - 1)
4751 return nfserr_replay_me;
4752 if (seqid == so->so_seqid)
4753 return nfs_ok;
4754 return nfserr_bad_seqid;
4755}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756
J. Bruce Fields7950b532021-01-21 17:57:41 -05004757static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions,
4758 struct nfsd_net *nn)
4759{
4760 struct nfs4_client *found;
4761
4762 spin_lock(&nn->client_lock);
4763 found = find_confirmed_client(clid, sessions, nn);
4764 if (found)
4765 atomic_inc(&found->cl_rpc_users);
4766 spin_unlock(&nn->client_lock);
4767 return found;
4768}
4769
J. Bruce Fields460d2702021-01-21 17:57:40 -05004770static __be32 set_client(clientid_t *clid,
Jeff Layton4b24ca72014-06-30 11:48:44 -04004771 struct nfsd4_compound_state *cstate,
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004772 struct nfsd_net *nn)
Jeff Layton4b24ca72014-06-30 11:48:44 -04004773{
Jeff Layton4b24ca72014-06-30 11:48:44 -04004774 if (cstate->clp) {
J. Bruce Fields7950b532021-01-21 17:57:41 -05004775 if (!same_clid(&cstate->clp->cl_clientid, clid))
Jeff Layton4b24ca72014-06-30 11:48:44 -04004776 return nfserr_stale_clientid;
4777 return nfs_ok;
4778 }
Jeff Layton4b24ca72014-06-30 11:48:44 -04004779 if (STALE_CLIENTID(clid, nn))
4780 return nfserr_stale_clientid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004781 /*
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004782 * We're in the 4.0 case (otherwise the SEQUENCE op would have
4783 * set cstate->clp), so session = false:
Jeff Layton4b24ca72014-06-30 11:48:44 -04004784 */
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004785 cstate->clp = lookup_clientid(clid, false, nn);
J. Bruce Fields7950b532021-01-21 17:57:41 -05004786 if (!cstate->clp)
Jeff Layton4b24ca72014-06-30 11:48:44 -04004787 return nfserr_expired;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004788 return nfs_ok;
4789}
4790
Al Virob37ad282006-10-19 23:28:59 -07004791__be32
Andy Adamson66689582009-04-03 08:28:45 +03004792nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004793 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 clientid_t *clientid = &open->op_clientid;
4796 struct nfs4_client *clp = NULL;
4797 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004798 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004799 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800
J. Bruce Fields32513b42011-10-13 16:00:16 -04004801 /*
4802 * In case we need it later, after we've already created the
4803 * file and don't want to risk a further failure:
4804 */
4805 open->op_file = nfsd4_alloc_file();
4806 if (open->op_file == NULL)
4807 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004809 status = set_client(clientid, cstate, nn);
Trond Myklebust2d91e892014-06-30 11:48:46 -04004810 if (status)
4811 return status;
4812 clp = cstate->clp;
4813
Trond Myklebustd4f04892014-07-29 21:34:36 -04004814 strhashval = ownerstr_hashval(&open->op_owner);
4815 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004816 open->op_openowner = oo;
4817 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004818 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004820 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004821 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004822 release_openowner(oo);
4823 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004824 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004825 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004826 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4827 if (status)
4828 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004829 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004830new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04004831 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004832 if (oo == NULL)
4833 return nfserr_jukebox;
4834 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004835alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04004836 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004837 if (!open->op_stp)
4838 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004839
4840 if (nfsd4_has_session(cstate) &&
4841 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4842 open->op_odstate = alloc_clnt_odstate(clp);
4843 if (!open->op_odstate)
4844 return nfserr_jukebox;
4845 }
4846
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004847 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848}
4849
Al Virob37ad282006-10-19 23:28:59 -07004850static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07004851nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4852{
4853 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4854 return nfserr_openmode;
4855 else
4856 return nfs_ok;
4857}
4858
Daniel Mackc47d8322011-05-16 16:38:14 +02004859static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04004860{
J. Bruce Fields24a01112010-05-18 20:01:35 -04004861 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4862}
4863
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004864static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004865{
4866 struct nfs4_stid *ret;
4867
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004868 ret = find_stateid_by_type(cl, s,
4869 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004870 if (!ret)
4871 return NULL;
4872 return delegstateid(ret);
4873}
4874
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004875static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4876{
4877 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4878 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4879}
4880
Al Virob37ad282006-10-19 23:28:59 -07004881static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04004882nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07004883 struct nfs4_delegation **dp)
4884{
4885 int flags;
Al Virob37ad282006-10-19 23:28:59 -07004886 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004887 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07004888
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004889 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4890 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07004891 goto out;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004892 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4893 nfs4_put_stid(&deleg->dl_stid);
4894 if (cl->cl_minorversion)
4895 status = nfserr_deleg_revoked;
4896 goto out;
4897 }
J. Bruce Fields24a01112010-05-18 20:01:35 -04004898 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004899 status = nfs4_check_delegmode(deleg, flags);
4900 if (status) {
4901 nfs4_put_stid(&deleg->dl_stid);
4902 goto out;
4903 }
4904 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004905out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004906 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004907 return nfs_ok;
4908 if (status)
4909 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004910 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004911 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07004912}
4913
J. Bruce Fields21fb4012010-07-28 12:21:23 -04004914static inline int nfs4_access_to_access(u32 nfs4_access)
4915{
4916 int flags = 0;
4917
4918 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4919 flags |= NFSD_MAY_READ;
4920 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4921 flags |= NFSD_MAY_WRITE;
4922 return flags;
4923}
4924
Al Virob37ad282006-10-19 23:28:59 -07004925static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4927 struct nfsd4_open *open)
4928{
4929 struct iattr iattr = {
4930 .ia_valid = ATTR_SIZE,
4931 .ia_size = 0,
4932 };
4933 if (!open->op_truncate)
4934 return 0;
4935 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08004936 return nfserr_inval;
Arnd Bergmann2a1aa482019-11-03 17:50:18 +01004937 return nfsd_setattr(rqstp, fh, &iattr, 0, (time64_t)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938}
4939
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004940static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004941 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4942 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004943{
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004944 struct nfsd_file *nf = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004945 __be32 status;
4946 int oflag = nfs4_access_to_omode(open->op_share_access);
4947 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004948 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004949
Trond Myklebustde186432014-07-10 14:07:26 -04004950 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004951
4952 /*
4953 * Are we trying to set a deny mode that would conflict with
4954 * current access?
4955 */
4956 status = nfs4_file_check_deny(fp, open->op_share_deny);
4957 if (status != nfs_ok) {
4958 spin_unlock(&fp->fi_lock);
4959 goto out;
4960 }
4961
4962 /* set access to the file */
4963 status = nfs4_file_get_access(fp, open->op_share_access);
4964 if (status != nfs_ok) {
4965 spin_unlock(&fp->fi_lock);
4966 goto out;
4967 }
4968
4969 /* Set access bits in stateid */
4970 old_access_bmap = stp->st_access_bmap;
4971 set_access(open->op_share_access, stp);
4972
4973 /* Set new deny mask */
4974 old_deny_bmap = stp->st_deny_bmap;
4975 set_deny(open->op_share_deny, stp);
4976 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4977
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004978 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04004979 spin_unlock(&fp->fi_lock);
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004980 status = nfsd_file_acquire(rqstp, cur_fh, access, &nf);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004981 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004982 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04004983 spin_lock(&fp->fi_lock);
4984 if (!fp->fi_fds[oflag]) {
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004985 fp->fi_fds[oflag] = nf;
4986 nf = NULL;
Trond Myklebustde186432014-07-10 14:07:26 -04004987 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004988 }
Trond Myklebustde186432014-07-10 14:07:26 -04004989 spin_unlock(&fp->fi_lock);
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004990 if (nf)
4991 nfsd_file_put(nf);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004992
J. Bruce Fields217fd6f2021-04-16 14:00:14 -04004993 status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode,
4994 access));
4995 if (status)
4996 goto out_put_access;
4997
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004998 status = nfsd4_truncate(rqstp, cur_fh, open);
4999 if (status)
5000 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04005001out:
5002 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04005003out_put_access:
5004 stp->st_access_bmap = old_access_bmap;
5005 nfs4_file_put_access(fp, open->op_share_access);
5006 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
5007 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04005008}
5009
Al Virob37ad282006-10-19 23:28:59 -07005010static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005011nfs4_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 -07005012{
Al Virob37ad282006-10-19 23:28:59 -07005013 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02005014 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005016 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04005017 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04005018
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04005019 /* test and set deny mode */
5020 spin_lock(&fp->fi_lock);
5021 status = nfs4_file_check_deny(fp, open->op_share_deny);
5022 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04005023 set_deny(open->op_share_deny, stp);
5024 fp->fi_share_deny |=
5025 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5026 }
5027 spin_unlock(&fp->fi_lock);
5028
5029 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04005032 status = nfsd4_truncate(rqstp, cur_fh, open);
5033 if (status != nfs_ok)
5034 reset_union_bmap_deny(old_deny_bmap, stp);
5035 return status;
5036}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037
J. Bruce Fields14a24e92010-12-10 19:02:49 -05005038/* Should we give out recallable state?: */
5039static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
5040{
5041 if (clp->cl_cb_state == NFSD4_CB_UP)
5042 return true;
5043 /*
5044 * In the sessions case, since we don't have to establish a
5045 * separate connection for callbacks, we assume it's OK
5046 * until we hear otherwise:
5047 */
5048 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
5049}
5050
J. Bruce Fields653e5142018-02-15 22:08:45 -05005051static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
5052 int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05005053{
5054 struct file_lock *fl;
5055
5056 fl = locks_alloc_lock();
5057 if (!fl)
5058 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05005059 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04005060 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05005061 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
5062 fl->fl_end = OFFSET_MAX;
J. Bruce Fields653e5142018-02-15 22:08:45 -05005063 fl->fl_owner = (fl_owner_t)dp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05005064 fl->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005065 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05005066 return fl;
5067}
5068
J. Bruce Fieldsaba20722021-04-16 14:00:18 -04005069static int nfsd4_check_conflicting_opens(struct nfs4_client *clp,
5070 struct nfs4_file *fp)
5071{
5072 struct nfs4_ol_stateid *st;
5073 struct file *f = fp->fi_deleg_file->nf_file;
5074 struct inode *ino = locks_inode(f);
5075 int writes;
5076
5077 writes = atomic_read(&ino->i_writecount);
5078 if (!writes)
5079 return 0;
5080 /*
5081 * There could be multiple filehandles (hence multiple
5082 * nfs4_files) referencing this file, but that's not too
5083 * common; let's just give up in that case rather than
5084 * trying to go look up all the clients using that other
5085 * nfs4_file as well:
5086 */
5087 if (fp->fi_aliased)
5088 return -EAGAIN;
5089 /*
5090 * If there's a close in progress, make sure that we see it
5091 * clear any fi_fds[] entries before we see it decrement
5092 * i_writecount:
5093 */
5094 smp_mb__after_atomic();
5095
5096 if (fp->fi_fds[O_WRONLY])
5097 writes--;
5098 if (fp->fi_fds[O_RDWR])
5099 writes--;
5100 if (writes > 0)
5101 return -EAGAIN; /* There may be non-NFSv4 writers */
5102 /*
5103 * It's possible there are non-NFSv4 write opens in progress,
5104 * but if they haven't incremented i_writecount yet then they
5105 * also haven't called break lease yet; so, they'll break this
5106 * lease soon enough. So, all that's left to check for is NFSv4
5107 * opens:
5108 */
5109 spin_lock(&fp->fi_lock);
5110 list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
5111 if (st->st_openstp == NULL /* it's an open */ &&
5112 access_permit_write(st) &&
5113 st->st_stid.sc_client != clp) {
5114 spin_unlock(&fp->fi_lock);
5115 return -EAGAIN;
5116 }
5117 }
5118 spin_unlock(&fp->fi_lock);
5119 /*
5120 * There's a small chance that we could be racing with another
5121 * NFSv4 open. However, any open that hasn't added itself to
5122 * the fi_stateids list also hasn't called break_lease yet; so,
5123 * they'll break this lease soon enough.
5124 */
5125 return 0;
5126}
5127
Jeff Layton0b266932014-07-25 07:34:25 -04005128static struct nfs4_delegation *
5129nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02005130 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05005131{
J. Bruce Fields68b18f52018-02-19 11:38:33 -05005132 int status = 0;
Jeff Layton0b266932014-07-25 07:34:25 -04005133 struct nfs4_delegation *dp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005134 struct nfsd_file *nf;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005135 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04005136
J. Bruce Fields353601e2018-02-16 14:29:42 -05005137 /*
5138 * The fi_had_conflict and nfs_get_existing_delegation checks
5139 * here are just optimizations; we'll need to recheck them at
5140 * the end:
5141 */
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04005142 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04005143 return ERR_PTR(-EAGAIN);
5144
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005145 nf = find_readable_file(fp);
5146 if (!nf) {
J. Bruce Fieldsaba20722021-04-16 14:00:18 -04005147 /*
5148 * We probably could attempt another open and get a read
5149 * delegation, but for now, don't bother until the
5150 * client actually sends us one.
5151 */
5152 return ERR_PTR(-EAGAIN);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005153 }
Andrew Elble34ed9872015-10-15 12:07:28 -04005154 spin_lock(&state_lock);
5155 spin_lock(&fp->fi_lock);
J. Bruce Fields68b18f52018-02-19 11:38:33 -05005156 if (nfs4_delegation_exists(clp, fp))
5157 status = -EAGAIN;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005158 else if (!fp->fi_deleg_file) {
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005159 fp->fi_deleg_file = nf;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005160 /* increment early to prevent fi_deleg_file from being
5161 * cleared */
5162 fp->fi_delegees = 1;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005163 nf = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005164 } else
5165 fp->fi_delegees++;
5166 spin_unlock(&fp->fi_lock);
5167 spin_unlock(&state_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005168 if (nf)
5169 nfsd_file_put(nf);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005170 if (status)
5171 return ERR_PTR(status);
5172
5173 status = -ENOMEM;
5174 dp = alloc_init_deleg(clp, fp, fh, odstate);
5175 if (!dp)
5176 goto out_delegees;
5177
5178 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
5179 if (!fl)
Andrew Elblebd8d7252018-10-05 09:32:08 -04005180 goto out_clnt_odstate;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005181
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005182 status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005183 if (fl)
5184 locks_free_lock(fl);
5185 if (status)
5186 goto out_clnt_odstate;
J. Bruce Fieldsaba20722021-04-16 14:00:18 -04005187 status = nfsd4_check_conflicting_opens(clp, fp);
5188 if (status)
5189 goto out_unlock;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005190
5191 spin_lock(&state_lock);
5192 spin_lock(&fp->fi_lock);
5193 if (fp->fi_had_conflict)
5194 status = -EAGAIN;
5195 else
5196 status = hash_delegation_locked(dp, fp);
Andrew Elble34ed9872015-10-15 12:07:28 -04005197 spin_unlock(&fp->fi_lock);
5198 spin_unlock(&state_lock);
5199
5200 if (status)
Andrew Elble692ad282018-04-18 17:04:37 -04005201 goto out_unlock;
5202
Jeff Layton0b266932014-07-25 07:34:25 -04005203 return dp;
Andrew Elble692ad282018-04-18 17:04:37 -04005204out_unlock:
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005205 vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005206out_clnt_odstate:
5207 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005208 nfs4_put_stid(&dp->dl_stid);
5209out_delegees:
5210 put_deleg_file(fp);
5211 return ERR_PTR(status);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05005212}
5213
Benny Halevy4aa89132012-02-21 14:16:44 -08005214static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
5215{
5216 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5217 if (status == -EAGAIN)
5218 open->op_why_no_deleg = WND4_CONTENTION;
5219 else {
5220 open->op_why_no_deleg = WND4_RESOURCE;
5221 switch (open->op_deleg_want) {
5222 case NFS4_SHARE_WANT_READ_DELEG:
5223 case NFS4_SHARE_WANT_WRITE_DELEG:
5224 case NFS4_SHARE_WANT_ANY_DELEG:
5225 break;
5226 case NFS4_SHARE_WANT_CANCEL:
5227 open->op_why_no_deleg = WND4_CANCELLED;
5228 break;
5229 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005230 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08005231 }
5232 }
5233}
5234
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235/*
5236 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04005237 *
5238 * Note we don't support write delegations, and won't until the vfs has
5239 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 */
5241static void
Jeff Layton4cf59222014-07-25 07:34:24 -04005242nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
5243 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244{
5245 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04005246 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
5247 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05005248 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04005249 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005251 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07005252 open->op_recall = 0;
5253 switch (open->op_claim_type) {
5254 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05005255 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07005256 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04005257 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
5258 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005259 break;
5260 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00005261 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005262 /*
5263 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005264 * had the chance to reclaim theirs, *and* until
5265 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005266 */
Jeff Layton4cf59222014-07-25 07:34:24 -04005267 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04005268 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04005269 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04005270 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005271 break;
5272 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005273 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005274 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02005275 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04005276 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005277 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04005279 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280
Hou Tao3caf9172020-08-28 15:02:55 +08005281 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
J. Bruce Fields99c41512013-05-21 16:21:25 -04005282 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04005283 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005284 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005285out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005286 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
5287 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04005288 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04005289 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04005290 open->op_recall = 1;
5291 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04005292
5293 /* 4.1 client asking for a delegation? */
5294 if (open->op_deleg_want)
5295 nfsd4_open_deleg_none_ext(open, status);
5296 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297}
5298
Benny Halevye27f49c2012-02-21 14:16:54 -08005299static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5300 struct nfs4_delegation *dp)
5301{
5302 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5303 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5304 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5305 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5306 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5307 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5308 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5309 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5310 }
5311 /* Otherwise the client must be confused wanting a delegation
5312 * it already has, therefore we don't return
5313 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5314 */
5315}
5316
Al Virob37ad282006-10-19 23:28:59 -07005317__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5319{
Andy Adamson66689582009-04-03 08:28:45 +03005320 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005321 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005323 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07005324 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005325 __be32 status;
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005326 bool new_stp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 /*
5329 * Lookup file; if found, lookup stateid and check open request,
5330 * and check for delegations in the process of being recalled.
5331 * If not found, create the nfs4_file struct
5332 */
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04005333 fp = find_or_add_file(open->op_file, current_fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04005334 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04005335 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07005336 if (status)
5337 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005338 stp = nfsd4_find_and_lock_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04005340 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07005341 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04005342 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07005343 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 }
5345
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005346 if (!stp) {
5347 stp = init_open_stateid(fp, open);
5348 if (!open->op_stp)
5349 new_stp = true;
5350 }
5351
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 /*
5353 * OPEN the file, or upgrade an existing OPEN.
5354 * If truncate fails, the OPEN fails.
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005355 *
5356 * stp is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 */
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005358 if (!new_stp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005360 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005361 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005362 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 } else {
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005366 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
5367 if (status) {
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005368 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005369 release_open_stateid(stp);
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005370 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005371 goto out;
5372 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02005373
5374 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5375 open->op_odstate);
5376 if (stp->st_clnt_odstate == open->op_odstate)
5377 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 }
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005379
Jeff Layton9767feb2015-10-01 09:05:50 -04005380 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005381 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382
Benny Halevyd24433c2012-02-16 20:57:17 +02005383 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02005384 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5385 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5386 open->op_why_no_deleg = WND4_NOT_WANTED;
5387 goto nodeleg;
5388 }
5389 }
5390
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 /*
5392 * Attempt to hand out a delegation. No error return, because the
5393 * OPEN succeeds even if we fail.
5394 */
Jeff Layton4cf59222014-07-25 07:34:24 -04005395 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005396nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 status = nfs_ok;
Hou Tao3caf9172020-08-28 15:02:55 +08005398 trace_nfsd_open(&stp->st_stid.sc_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399out:
Benny Halevyd24433c2012-02-16 20:57:17 +02005400 /* 4.1 client trying to upgrade/downgrade delegation? */
5401 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08005402 open->op_deleg_want)
5403 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005404
NeilBrown13cd2182005-06-23 22:03:10 -07005405 if (fp)
5406 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07005407 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08005408 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 /*
5410 * To finish the open response, we just need to set the rflags.
5411 */
5412 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Jeff Layton19e4c342016-09-16 16:28:27 -04005413 if (nfsd4_has_session(&resp->cstate))
5414 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5415 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Jeff Layton19e4c342016-09-16 16:28:27 -04005417
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04005418 if (dp)
5419 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04005420 if (stp)
5421 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422
5423 return status;
5424}
5425
Jeff Layton58fb12e2014-07-29 21:34:27 -04005426void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04005427 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005428{
5429 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04005430 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005431
Jeff Laytond3134b12014-07-29 21:34:32 -04005432 nfsd4_cstate_assign_replay(cstate, so);
5433 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005434 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04005435 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04005436 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04005437 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04005438 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02005439 if (open->op_odstate)
5440 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005441}
5442
Al Virob37ad282006-10-19 23:28:59 -07005443__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005444nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005445 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005447 clientid_t *clid = &u->renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07005449 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005450 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005452 trace_nfsd_clid_renew(clid);
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05005453 status = set_client(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005454 if (status)
J. Bruce Fieldsb4587eb2021-01-21 17:57:39 -05005455 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005456 clp = cstate->clp;
NeilBrownea1da632005-06-23 22:04:17 -07005457 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04005458 && clp->cl_cb_state != NFSD4_CB_UP)
J. Bruce Fieldsb4587eb2021-01-21 17:57:39 -05005459 return nfserr_cb_path_down;
5460 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461}
5462
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04005463void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005464nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07005465{
Jeff Layton33dcc482012-04-10 11:08:48 -04005466 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005467 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04005468 return;
5469
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005470 trace_nfsd_grace_complete(nn);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005471 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04005472 /*
5473 * If the server goes down again right now, an NFSv4
5474 * client will still be allowed to reclaim after it comes back up,
5475 * even if it hasn't yet had a chance to reclaim state this time.
5476 *
5477 */
Jeff Layton919b8042014-09-12 16:40:20 -04005478 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005479 /*
5480 * At this point, NFSv4 clients can still reclaim. But if the
5481 * server crashes, any that have not yet reclaimed will be out
5482 * of luck on the next boot.
5483 *
5484 * (NFSv4.1+ clients are considered to have reclaimed once they
5485 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5486 * have reclaimed after their first OPEN.)
5487 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005488 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005489 /*
5490 * At this point, and once lockd and/or any other containers
5491 * exit their grace period, further reclaims will fail and
5492 * regular locking can resume.
5493 */
NeilBrowna76b4312005-06-23 22:04:01 -07005494}
5495
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005496/*
5497 * If we've waited a lease period but there are still clients trying to
5498 * reclaim, wait a little longer to give them a chance to finish.
5499 */
5500static bool clients_still_reclaiming(struct nfsd_net *nn)
5501{
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005502 time64_t double_grace_period_end = nn->boot_time +
5503 2 * nn->nfsd4_lease;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005504
Scott Mayhew362063a2019-03-26 18:06:28 -04005505 if (nn->track_reclaim_completes &&
5506 atomic_read(&nn->nr_reclaim_complete) ==
5507 nn->reclaim_str_hashtbl_size)
5508 return false;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005509 if (!nn->somebody_reclaimed)
5510 return false;
5511 nn->somebody_reclaimed = false;
5512 /*
5513 * If we've given them *two* lease times to reclaim, and they're
5514 * still not done, give up:
5515 */
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005516 if (ktime_get_boottime_seconds() > double_grace_period_end)
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005517 return false;
5518 return true;
5519}
5520
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005521struct laundry_time {
5522 time64_t cutoff;
5523 time64_t new_timeo;
5524};
5525
5526static bool state_expired(struct laundry_time *lt, time64_t last_refresh)
5527{
5528 time64_t time_remaining;
5529
5530 if (last_refresh < lt->cutoff)
5531 return true;
5532 time_remaining = last_refresh - lt->cutoff;
5533 lt->new_timeo = min(lt->new_timeo, time_remaining);
5534 return false;
5535}
5536
Dai Ngof4e44b32021-05-21 15:09:37 -04005537#ifdef CONFIG_NFSD_V4_2_INTER_SSC
5538void nfsd4_ssc_init_umount_work(struct nfsd_net *nn)
5539{
5540 spin_lock_init(&nn->nfsd_ssc_lock);
5541 INIT_LIST_HEAD(&nn->nfsd_ssc_mount_list);
5542 init_waitqueue_head(&nn->nfsd_ssc_waitq);
5543}
5544EXPORT_SYMBOL_GPL(nfsd4_ssc_init_umount_work);
5545
5546/*
5547 * This is called when nfsd is being shutdown, after all inter_ssc
5548 * cleanup were done, to destroy the ssc delayed unmount list.
5549 */
5550static void nfsd4_ssc_shutdown_umount(struct nfsd_net *nn)
5551{
Dai Ngof47dc2d2021-06-03 20:02:26 -04005552 struct nfsd4_ssc_umount_item *ni = NULL;
Dai Ngof4e44b32021-05-21 15:09:37 -04005553 struct nfsd4_ssc_umount_item *tmp;
5554
5555 spin_lock(&nn->nfsd_ssc_lock);
5556 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
5557 list_del(&ni->nsui_list);
5558 spin_unlock(&nn->nfsd_ssc_lock);
5559 mntput(ni->nsui_vfsmount);
5560 kfree(ni);
5561 spin_lock(&nn->nfsd_ssc_lock);
5562 }
5563 spin_unlock(&nn->nfsd_ssc_lock);
5564}
5565
5566static void nfsd4_ssc_expire_umount(struct nfsd_net *nn)
5567{
5568 bool do_wakeup = false;
Colin Ian King8e70bf22021-09-25 23:58:41 +01005569 struct nfsd4_ssc_umount_item *ni = NULL;
Dai Ngof4e44b32021-05-21 15:09:37 -04005570 struct nfsd4_ssc_umount_item *tmp;
5571
5572 spin_lock(&nn->nfsd_ssc_lock);
5573 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
5574 if (time_after(jiffies, ni->nsui_expire)) {
5575 if (refcount_read(&ni->nsui_refcnt) > 1)
5576 continue;
5577
5578 /* mark being unmount */
5579 ni->nsui_busy = true;
5580 spin_unlock(&nn->nfsd_ssc_lock);
5581 mntput(ni->nsui_vfsmount);
5582 spin_lock(&nn->nfsd_ssc_lock);
5583
5584 /* waiters need to start from begin of list */
5585 list_del(&ni->nsui_list);
5586 kfree(ni);
5587
5588 /* wakeup ssc_connect waiters */
5589 do_wakeup = true;
5590 continue;
5591 }
5592 break;
5593 }
5594 if (do_wakeup)
5595 wake_up_all(&nn->nfsd_ssc_waitq);
5596 spin_unlock(&nn->nfsd_ssc_lock);
5597}
5598#endif
5599
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005600static time64_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005601nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602{
5603 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005604 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04005606 struct nfs4_ol_stateid *stp;
Jeff Layton7919d0a2016-09-16 16:28:25 -04005607 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 struct list_head *pos, *next, reaplist;
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005609 struct laundry_time lt = {
5610 .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease,
5611 .new_timeo = nn->nfsd4_lease
5612 };
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005613 struct nfs4_cpntf_state *cps;
5614 copy_stateid_t *cps_t;
5615 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005617 if (clients_still_reclaiming(nn)) {
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005618 lt.new_timeo = 0;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005619 goto out;
5620 }
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005621 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03005622 INIT_LIST_HEAD(&reaplist);
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005623
5624 spin_lock(&nn->s2s_cp_lock);
5625 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
5626 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
5627 if (cps->cp_stateid.sc_type == NFS4_COPYNOTIFY_STID &&
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005628 state_expired(&lt, cps->cpntf_time))
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005629 _free_cpntf_state_locked(nn, cps);
5630 }
5631 spin_unlock(&nn->s2s_cp_lock);
5632
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005633 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005634 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 clp = list_entry(pos, struct nfs4_client, cl_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005636 if (!state_expired(&lt, clp->cl_time))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 break;
Chuck Lever2958d2e2021-05-14 15:56:06 -04005638 if (mark_client_expired_locked(clp))
Benny Halevyd7682982010-05-12 00:13:54 +03005639 continue;
Trond Myklebust4864af92014-07-30 08:27:03 -04005640 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03005641 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005642 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03005643 list_for_each_safe(pos, next, &reaplist) {
5644 clp = list_entry(pos, struct nfs4_client, cl_lru);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005645 trace_nfsd_clid_purged(&clp->cl_clientid);
Trond Myklebust4864af92014-07-30 08:27:03 -04005646 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 expire_client(clp);
5648 }
Benny Halevycdc975052014-05-30 09:09:30 -04005649 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005650 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005652 if (!state_expired(&lt, dp->dl_time))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 break;
Jeff Layton3fcbbd22015-08-24 12:41:48 -04005654 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04005655 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 }
Benny Halevycdc975052014-05-30 09:09:30 -04005657 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005658 while (!list_empty(&reaplist)) {
5659 dp = list_first_entry(&reaplist, struct nfs4_delegation,
5660 dl_recall_lru);
5661 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005662 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 }
Jeff Layton217526e2014-07-30 08:27:11 -04005664
5665 spin_lock(&nn->client_lock);
5666 while (!list_empty(&nn->close_lru)) {
5667 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
5668 oo_close_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005669 if (!state_expired(&lt, oo->oo_time))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 break;
Jeff Layton217526e2014-07-30 08:27:11 -04005671 list_del_init(&oo->oo_close_lru);
5672 stp = oo->oo_last_closed_stid;
5673 oo->oo_last_closed_stid = NULL;
5674 spin_unlock(&nn->client_lock);
5675 nfs4_put_stid(&stp->st_stid);
5676 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 }
Jeff Layton217526e2014-07-30 08:27:11 -04005678 spin_unlock(&nn->client_lock);
5679
Jeff Layton7919d0a2016-09-16 16:28:25 -04005680 /*
5681 * It's possible for a client to try and acquire an already held lock
5682 * that is being held for a long time, and then lose interest in it.
5683 * So, we clean out any un-revisited request after a lease period
5684 * under the assumption that the client is no longer interested.
5685 *
5686 * RFC5661, sec. 9.6 states that the client must not rely on getting
5687 * notifications and must continue to poll for locks, even when the
5688 * server supports them. Thus this shouldn't lead to clients blocking
5689 * indefinitely once the lock does become free.
5690 */
5691 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04005692 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005693 while (!list_empty(&nn->blocked_locks_lru)) {
5694 nbl = list_first_entry(&nn->blocked_locks_lru,
5695 struct nfsd4_blocked_lock, nbl_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005696 if (!state_expired(&lt, nbl->nbl_time))
Jeff Layton7919d0a2016-09-16 16:28:25 -04005697 break;
Jeff Layton7919d0a2016-09-16 16:28:25 -04005698 list_move(&nbl->nbl_lru, &reaplist);
5699 list_del_init(&nbl->nbl_list);
5700 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04005701 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005702
5703 while (!list_empty(&reaplist)) {
Naofumi Honda64ebe1242017-11-09 10:57:16 -05005704 nbl = list_first_entry(&reaplist,
Jeff Layton7919d0a2016-09-16 16:28:25 -04005705 struct nfsd4_blocked_lock, nbl_lru);
5706 list_del_init(&nbl->nbl_lru);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005707 free_blocked_lock(nbl);
5708 }
Dai Ngof4e44b32021-05-21 15:09:37 -04005709#ifdef CONFIG_NFSD_V4_2_INTER_SSC
5710 /* service the server-to-server copy delayed unmount list */
5711 nfsd4_ssc_expire_umount(nn);
5712#endif
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005713out:
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005714 return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715}
5716
Harvey Harrisona254b242008-02-20 12:49:00 -08005717static struct workqueue_struct *laundry_wq;
5718static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08005719
5720static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005721laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722{
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005723 time64_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08005724 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005725 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
5726 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005728 t = nfs4_laundromat(nn);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005729 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730}
5731
Jeff Layton8fcd4612015-07-30 06:57:46 -04005732static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07005733{
Jeff Layton8fcd4612015-07-30 06:57:46 -04005734 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005735 return nfserr_bad_stateid;
5736 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737}
5738
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005740__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741{
Al Virob37ad282006-10-19 23:28:59 -07005742 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743
J. Bruce Fields02921912010-07-29 15:16:59 -04005744 /* For lock stateid's, we test the parent open, not the lock: */
5745 if (stp->st_openstp)
5746 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005747 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005749 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 goto out;
5751 status = nfs_ok;
5752out:
5753 return status;
5754}
5755
Al Virob37ad282006-10-19 23:28:59 -07005756static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005757check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005759 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005761 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005762 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 * conflicting state; so we must wait out the grace period. */
5764 return nfserr_grace;
5765 } else if (flags & WR_STATE)
5766 return nfs4_share_conflict(current_fh,
5767 NFS4_SHARE_DENY_WRITE);
5768 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
5769 return nfs4_share_conflict(current_fh,
5770 NFS4_SHARE_DENY_READ);
5771}
5772
J. Bruce Fields57b7b432012-04-25 17:58:50 -04005773static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05005774{
Andy Adamson66689582009-04-03 08:28:45 +03005775 /*
5776 * When sessions are used the stateid generation number is ignored
5777 * when it is zero.
5778 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04005779 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04005780 return nfs_ok;
5781
5782 if (in->si_generation == ref->si_generation)
5783 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03005784
J. Bruce Fields0836f582008-01-26 19:08:12 -05005785 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04005786 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05005787 return nfserr_bad_stateid;
5788 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04005789 * However, we could see a stateid from the past, even from a
5790 * non-buggy client. For example, if the client sends a lock
5791 * while some IO is outstanding, the lock may bump si_generation
5792 * while the IO is still in flight. The client could avoid that
5793 * situation by waiting for responses on all the IO requests,
5794 * but better performance may result in retrying IO that
5795 * receives an old_stateid error if requests are rarely
5796 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05005797 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04005798 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05005799}
5800
Trond Myklebust03da3162017-11-03 08:00:16 -04005801static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
5802{
5803 __be32 ret;
5804
5805 spin_lock(&s->sc_lock);
5806 ret = nfsd4_verify_open_stid(s);
5807 if (ret == nfs_ok)
5808 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
5809 spin_unlock(&s->sc_lock);
5810 return ret;
5811}
5812
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005813static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
5814{
5815 if (ols->st_stateowner->so_is_open_owner &&
5816 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5817 return nfserr_bad_stateid;
5818 return nfs_ok;
5819}
5820
Chuck Lever7df302f2012-05-29 13:56:37 -04005821static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04005822{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005823 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005824 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04005825
Andrew Elbleae254dac2017-11-09 13:41:10 -05005826 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5827 CLOSE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04005828 return status;
J. Bruce Fields663e36f2020-03-19 10:18:49 -04005829 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04005830 return status;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005831 spin_lock(&cl->cl_lock);
5832 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005833 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005834 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005835 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04005836 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005837 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04005838 switch (s->sc_type) {
5839 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005840 status = nfs_ok;
5841 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005842 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005843 status = nfserr_deleg_revoked;
5844 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005845 case NFS4_OPEN_STID:
5846 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005847 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04005848 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005849 default:
5850 printk("unknown stateid type %x\n", s->sc_type);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05005851 fallthrough;
J. Bruce Fields23340032013-04-09 17:42:28 -04005852 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005853 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005854 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04005855 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005856out_unlock:
5857 spin_unlock(&cl->cl_lock);
5858 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04005859}
5860
Christoph Hellwigcd61c522014-08-14 08:44:57 +02005861__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005862nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5863 stateid_t *stateid, unsigned char typemask,
5864 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005865{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005866 __be32 status;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005867 bool return_revoked = false;
5868
5869 /*
5870 * only return revoked delegations if explicitly asked.
5871 * otherwise we report revoked or bad_stateid status.
5872 */
5873 if (typemask & NFS4_REVOKED_DELEG_STID)
5874 return_revoked = true;
5875 else if (typemask & NFS4_DELEG_STID)
5876 typemask |= NFS4_REVOKED_DELEG_STID;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005877
Andrew Elbleae254dac2017-11-09 13:41:10 -05005878 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5879 CLOSE_STATEID(stateid))
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005880 return nfserr_bad_stateid;
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05005881 status = set_client(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005882 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005883 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005884 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005885 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005886 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005887 if (status)
5888 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005889 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005890 if (!*s)
5891 return nfserr_bad_stateid;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005892 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5893 nfs4_put_stid(*s);
5894 if (cstate->minorversion)
5895 return nfserr_deleg_revoked;
5896 return nfserr_bad_stateid;
5897 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005898 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005899}
5900
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005901static struct nfsd_file *
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005902nfs4_find_file(struct nfs4_stid *s, int flags)
5903{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005904 if (!s)
5905 return NULL;
5906
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005907 switch (s->sc_type) {
5908 case NFS4_DELEG_STID:
5909 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5910 return NULL;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005911 return nfsd_file_get(s->sc_file->fi_deleg_file);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005912 case NFS4_OPEN_STID:
5913 case NFS4_LOCK_STID:
5914 if (flags & RD_STATE)
5915 return find_readable_file(s->sc_file);
5916 else
5917 return find_writeable_file(s->sc_file);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005918 }
5919
5920 return NULL;
5921}
5922
5923static __be32
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005924nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005925{
5926 __be32 status;
5927
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005928 status = nfsd4_check_openowner_confirmed(ols);
5929 if (status)
5930 return status;
5931 return nfs4_check_openmode(ols, flags);
5932}
5933
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005934static __be32
5935nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
Jeff Layton5c4583b2019-08-18 14:18:54 -04005936 struct nfsd_file **nfp, int flags)
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005937{
5938 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005939 struct nfsd_file *nf;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005940 __be32 status;
5941
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005942 nf = nfs4_find_file(s, flags);
5943 if (nf) {
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005944 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5945 acc | NFSD_MAY_OWNER_OVERRIDE);
Jeff Layton5c4583b2019-08-18 14:18:54 -04005946 if (status) {
5947 nfsd_file_put(nf);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005948 goto out;
Jeff Layton5c4583b2019-08-18 14:18:54 -04005949 }
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005950 } else {
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005951 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005952 if (status)
5953 return status;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005954 }
Jeff Layton5c4583b2019-08-18 14:18:54 -04005955 *nfp = nf;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005956out:
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005957 return status;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005958}
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005959static void
5960_free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5961{
5962 WARN_ON_ONCE(cps->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID);
5963 if (!refcount_dec_and_test(&cps->cp_stateid.sc_count))
5964 return;
5965 list_del(&cps->cp_list);
5966 idr_remove(&nn->s2s_cp_stateids,
5967 cps->cp_stateid.stid.si_opaque.so_id);
5968 kfree(cps);
5969}
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005970/*
5971 * A READ from an inter server to server COPY will have a
5972 * copy stateid. Look up the copy notify stateid from the
5973 * idr structure and take a reference on it.
5974 */
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005975__be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5976 struct nfs4_client *clp,
5977 struct nfs4_cpntf_state **cps)
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005978{
5979 copy_stateid_t *cps_t;
5980 struct nfs4_cpntf_state *state = NULL;
5981
5982 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
5983 return nfserr_bad_stateid;
5984 spin_lock(&nn->s2s_cp_lock);
5985 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
5986 if (cps_t) {
5987 state = container_of(cps_t, struct nfs4_cpntf_state,
5988 cp_stateid);
Dan Carpenter5277a792019-12-04 10:59:36 +03005989 if (state->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID) {
5990 state = NULL;
5991 goto unlock;
5992 }
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005993 if (!clp)
5994 refcount_inc(&state->cp_stateid.sc_count);
5995 else
5996 _free_cpntf_state_locked(nn, state);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005997 }
Dan Carpenter5277a792019-12-04 10:59:36 +03005998unlock:
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005999 spin_unlock(&nn->s2s_cp_lock);
6000 if (!state)
6001 return nfserr_bad_stateid;
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04006002 if (!clp && state)
6003 *cps = state;
Olga Kornievskaiab7342202019-09-06 15:17:21 -04006004 return 0;
6005}
6006
6007static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
6008 struct nfs4_stid **stid)
6009{
6010 __be32 status;
6011 struct nfs4_cpntf_state *cps = NULL;
J. Bruce Fields47fdb222021-01-21 17:57:42 -05006012 struct nfs4_client *found;
Olga Kornievskaiab7342202019-09-06 15:17:21 -04006013
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04006014 status = manage_cpntf_state(nn, st, NULL, &cps);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04006015 if (status)
6016 return status;
6017
Arnd Bergmann20b7d862019-11-04 16:31:52 +01006018 cps->cpntf_time = ktime_get_boottime_seconds();
J. Bruce Fields47fdb222021-01-21 17:57:42 -05006019
6020 status = nfserr_expired;
6021 found = lookup_clientid(&cps->cp_p_clid, true, nn);
6022 if (!found)
Olga Kornievskaiab7342202019-09-06 15:17:21 -04006023 goto out;
J. Bruce Fields47fdb222021-01-21 17:57:42 -05006024
6025 *stid = find_stateid_by_type(found, &cps->cp_p_stateid,
6026 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID);
6027 if (*stid)
6028 status = nfs_ok;
6029 else
6030 status = nfserr_bad_stateid;
6031
6032 put_client_renew(found);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04006033out:
6034 nfs4_put_cpntf_state(nn, cps);
6035 return status;
6036}
Olga Kornievskaia624322f2019-10-04 16:34:26 -04006037
6038void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
6039{
6040 spin_lock(&nn->s2s_cp_lock);
6041 _free_cpntf_state_locked(nn, cps);
6042 spin_unlock(&nn->s2s_cp_lock);
6043}
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006044
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02006046 * Checks for stateid operations
6047 */
Al Virob37ad282006-10-19 23:28:59 -07006048__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006049nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01006050 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04006051 stateid_t *stateid, int flags, struct nfsd_file **nfp,
6052 struct nfs4_stid **cstid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006054 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006055 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006056 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006057 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058
Jeff Layton5c4583b2019-08-18 14:18:54 -04006059 if (nfp)
6060 *nfp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006062 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
J. Bruce Fields074b07d2022-01-05 14:15:03 -05006063 if (cstid)
6064 status = nfserr_bad_stateid;
6065 else
6066 status = check_special_stateids(net, fhp, stateid,
6067 flags);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006068 goto done;
6069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006071 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04006072 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006073 &s, nn);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04006074 if (status == nfserr_bad_stateid)
6075 status = find_cpntf_state(nn, stateid, &s);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006076 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04006077 return status;
Trond Myklebust03da3162017-11-03 08:00:16 -04006078 status = nfsd4_stid_check_stateid_generation(stateid, s,
Christoph Hellwiga0649b22015-06-18 16:44:59 +02006079 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04006080 if (status)
6081 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02006082
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006083 switch (s->sc_type) {
6084 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02006085 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006086 break;
6087 case NFS4_OPEN_STID:
6088 case NFS4_LOCK_STID:
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05006089 status = nfs4_check_olstateid(openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006090 break;
6091 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04006092 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02006093 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04006095 if (status)
6096 goto out;
6097 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02006098
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006099done:
Jeff Layton5c4583b2019-08-18 14:18:54 -04006100 if (status == nfs_ok && nfp)
6101 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102out:
Olga Kornievskaia624322f2019-10-04 16:34:26 -04006103 if (s) {
6104 if (!status && cstid)
6105 *cstid = s;
6106 else
6107 nfs4_put_stid(s);
6108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109 return status;
6110}
6111
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006112/*
Bryan Schumaker17456802011-07-13 10:50:48 -04006113 * Test if the stateid is valid
6114 */
6115__be32
6116nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006117 union nfsd4_op_u *u)
Bryan Schumaker17456802011-07-13 10:50:48 -04006118{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006119 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05006120 struct nfsd4_test_stateid_id *stateid;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05006121 struct nfs4_client *cl = cstate->clp;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05006122
Bryan Schumaker03cfb422012-01-27 10:22:49 -05006123 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04006124 stateid->ts_id_status =
6125 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05006126
Bryan Schumaker17456802011-07-13 10:50:48 -04006127 return nfs_ok;
6128}
6129
Chuck Lever42691392016-08-11 10:37:30 -04006130static __be32
6131nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
6132{
6133 struct nfs4_ol_stateid *stp = openlockstateid(s);
6134 __be32 ret;
6135
Trond Myklebust659aefb2017-11-03 08:00:13 -04006136 ret = nfsd4_lock_ol_stateid(stp);
6137 if (ret)
6138 goto out_put_stid;
Chuck Lever42691392016-08-11 10:37:30 -04006139
6140 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6141 if (ret)
6142 goto out;
6143
6144 ret = nfserr_locks_held;
6145 if (check_for_locks(stp->st_stid.sc_file,
6146 lockowner(stp->st_stateowner)))
6147 goto out;
6148
6149 release_lock_stateid(stp);
6150 ret = nfs_ok;
6151
6152out:
6153 mutex_unlock(&stp->st_mutex);
Trond Myklebust659aefb2017-11-03 08:00:13 -04006154out_put_stid:
Chuck Lever42691392016-08-11 10:37:30 -04006155 nfs4_put_stid(s);
6156 return ret;
6157}
6158
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006159__be32
6160nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006161 union nfsd4_op_u *u)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006162{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006163 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006164 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04006165 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006166 struct nfs4_delegation *dp;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05006167 struct nfs4_client *cl = cstate->clp;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04006168 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006169
Jeff Layton1af71cc2014-07-29 21:34:14 -04006170 spin_lock(&cl->cl_lock);
6171 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04006172 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04006173 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04006174 spin_lock(&s->sc_lock);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04006175 switch (s->sc_type) {
6176 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006177 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04006178 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04006179 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04006180 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6181 if (ret)
6182 break;
6183 ret = nfserr_locks_held;
6184 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04006185 case NFS4_LOCK_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04006186 spin_unlock(&s->sc_lock);
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03006187 refcount_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04006188 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04006189 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04006190 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006191 case NFS4_REVOKED_DELEG_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04006192 spin_unlock(&s->sc_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006193 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04006194 list_del_init(&dp->dl_recall_lru);
6195 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04006196 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006197 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04006198 goto out;
6199 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006200 }
Trond Myklebust03da3162017-11-03 08:00:16 -04006201 spin_unlock(&s->sc_lock);
Jeff Layton1af71cc2014-07-29 21:34:14 -04006202out_unlock:
6203 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006204out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006205 return ret;
6206}
6207
NeilBrown4c4cd222005-07-07 17:59:27 -07006208static inline int
6209setlkflg (int type)
6210{
6211 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
6212 RD_STATE : WR_STATE;
6213}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006215static __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 -04006216{
6217 struct svc_fh *current_fh = &cstate->current_fh;
6218 struct nfs4_stateowner *sop = stp->st_stateowner;
6219 __be32 status;
6220
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006221 status = nfsd4_check_seqid(cstate, sop, seqid);
6222 if (status)
6223 return status;
Trond Myklebust9271d7e2017-11-03 08:00:15 -04006224 status = nfsd4_lock_ol_stateid(stp);
6225 if (status != nfs_ok)
6226 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006227 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04006228 if (status == nfs_ok)
6229 status = nfs4_check_fh(current_fh, &stp->st_stid);
6230 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006231 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04006232 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006233}
6234
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235/*
6236 * Checks for sequence id mutating operations.
6237 */
Al Virob37ad282006-10-19 23:28:59 -07006238static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03006239nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04006240 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006241 struct nfs4_ol_stateid **stpp,
6242 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243{
J. Bruce Fields0836f582008-01-26 19:08:12 -05006244 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006245 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006246 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04006248 trace_nfsd_preprocess(seqid, stateid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07006249
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006251 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006252 if (status)
6253 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006254 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04006255 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006257 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04006258 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006259 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04006260 else
6261 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006262 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006263}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05006264
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006265static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6266 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006267{
6268 __be32 status;
6269 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006270 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006272 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006273 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04006274 if (status)
6275 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006276 oo = openowner(stp->st_stateowner);
6277 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006278 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006279 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07006280 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006281 }
6282 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07006283 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284}
6285
Al Virob37ad282006-10-19 23:28:59 -07006286__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006287nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006288 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006290 struct nfsd4_open_confirm *oc = &u->open_confirm;
Al Virob37ad282006-10-19 23:28:59 -07006291 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006292 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006293 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006294 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295
Al Viroa6a9f182013-09-16 10:57:01 -04006296 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
6297 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006299 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07006300 if (status)
6301 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006303 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006304 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006305 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006306 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006307 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006308 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006309 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04006310 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006311 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04006312 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04006313 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04006314 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04006315 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006316 mutex_unlock(&stp->st_mutex);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04006317 trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
Jeff Layton2a4317c2012-03-21 16:42:43 -04006318 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006319 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04006320put_stateid:
6321 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006323 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 return status;
6325}
6326
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006327static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328{
Jeff Layton82c5ff12012-05-11 09:45:13 -04006329 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006330 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04006331 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006332 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006333}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04006334
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006335static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
6336{
6337 switch (to_access) {
6338 case NFS4_SHARE_ACCESS_READ:
6339 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
6340 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6341 break;
6342 case NFS4_SHARE_ACCESS_WRITE:
6343 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
6344 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6345 break;
6346 case NFS4_SHARE_ACCESS_BOTH:
6347 break;
6348 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006349 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 }
6351}
6352
Al Virob37ad282006-10-19 23:28:59 -07006353__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006354nfsd4_open_downgrade(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006355 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006357 struct nfsd4_open_downgrade *od = &u->open_downgrade;
Al Virob37ad282006-10-19 23:28:59 -07006358 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006359 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006360 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361
Al Viroa6a9f182013-09-16 10:57:01 -04006362 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
6363 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04006365 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02006366 if (od->od_deleg_want)
6367 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
6368 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006370 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006371 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006372 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04006375 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04006376 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006378 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04006380 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04006381 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006383 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006385 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04006386 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04006387 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006389put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006390 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006391 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006393 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394 return status;
6395}
6396
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006397static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
6398{
Trond Myklebustacf92952014-06-30 11:48:37 -04006399 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04006400 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04006401 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04006402
Jeff Layton2c41beb2014-07-29 21:34:41 -04006403 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04006404 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04006405
Jeff Laytond83017f2014-07-29 21:34:42 -04006406 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04006407 if (unhashed)
6408 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04006409 spin_unlock(&clp->cl_lock);
6410 free_ol_stateid_reaplist(&reaplist);
6411 } else {
6412 spin_unlock(&clp->cl_lock);
6413 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04006414 if (unhashed)
6415 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04006416 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04006417}
6418
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419/*
6420 * nfs4_unlock_state() called after encode
6421 */
Al Virob37ad282006-10-19 23:28:59 -07006422__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006423nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006424 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006426 struct nfsd4_close *close = &u->close;
Al Virob37ad282006-10-19 23:28:59 -07006427 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006428 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006429 struct net *net = SVC_NET(rqstp);
6430 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431
Al Viroa6a9f182013-09-16 10:57:01 -04006432 dprintk("NFSD: nfsd4_close on file %pd\n",
6433 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006435 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
6436 &close->cl_stateid,
6437 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006438 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006439 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006440 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04006442
6443 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Laytonbd2deca2018-02-15 15:16:18 -05006444
6445 /*
6446 * Technically we don't _really_ have to increment or copy it, since
6447 * it should just be gone after this operation and we clobber the
6448 * copied value below, but we continue to do so here just to ensure
6449 * that racing ops see that there was a state change.
6450 */
Jeff Layton9767feb2015-10-01 09:05:50 -04006451 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006453 nfsd4_close_open_stateid(stp);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04006454 mutex_unlock(&stp->st_mutex);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04006455
Jeff Laytonbd2deca2018-02-15 15:16:18 -05006456 /* v4.1+ suggests that we send a special stateid in here, since the
6457 * clients should just ignore this anyway. Since this is not useful
6458 * for v4.0 clients either, we set it to the special close_stateid
6459 * universally.
6460 *
6461 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
6462 */
6463 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
Trond Myklebustfb500a72017-11-03 08:00:12 -04006464
Trond Myklebust8a0b5892014-07-29 21:34:20 -04006465 /* put reference from nfs4_preprocess_seqid_op */
6466 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468 return status;
6469}
6470
Al Virob37ad282006-10-19 23:28:59 -07006471__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006472nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006473 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006475 struct nfsd4_delegreturn *dr = &u->delegreturn;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006476 struct nfs4_delegation *dp;
6477 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006478 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07006479 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006480 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006482 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006483 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006485 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006486 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006487 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006488 dp = delegstateid(s);
Trond Myklebust03da3162017-11-03 08:00:16 -04006489 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006490 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04006491 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006492
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006493 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04006494put_stateid:
6495 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496out:
6497 return status;
6498}
6499
Benny Halevy87df4de2008-12-15 19:42:03 +02006500/* last octet in a range */
6501static inline u64
6502last_byte_offset(u64 start, u64 len)
6503{
6504 u64 end;
6505
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006506 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02006507 end = start + len;
6508 return end > start ? end - 1: NFS4_MAX_UINT64;
6509}
6510
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511/*
6512 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6513 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6514 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6515 * locking, this prevents us from being completely protocol-compliant. The
6516 * real solution to this problem is to start using unsigned file offsets in
6517 * the VFS, but this is a very deep change!
6518 */
6519static inline void
6520nfs4_transform_lock_offset(struct file_lock *lock)
6521{
6522 if (lock->fl_start < 0)
6523 lock->fl_start = OFFSET_MAX;
6524 if (lock->fl_end < 0)
6525 lock->fl_end = OFFSET_MAX;
6526}
6527
Jeff Laytoncae80b32015-04-03 09:04:04 -04006528static fl_owner_t
6529nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006530{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006531 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6532
6533 nfs4_get_stateowner(&lo->lo_owner);
6534 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006535}
6536
Jeff Laytoncae80b32015-04-03 09:04:04 -04006537static void
6538nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006539{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006540 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006541
Jeff Laytoncae80b32015-04-03 09:04:04 -04006542 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04006543 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04006544}
6545
Jeff Layton76d348f2016-09-16 16:28:24 -04006546static void
6547nfsd4_lm_notify(struct file_lock *fl)
6548{
6549 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
6550 struct net *net = lo->lo_owner.so_client->net;
6551 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6552 struct nfsd4_blocked_lock *nbl = container_of(fl,
6553 struct nfsd4_blocked_lock, nbl_lock);
6554 bool queue = false;
6555
Jeff Layton7919d0a2016-09-16 16:28:25 -04006556 /* An empty list means that something else is going to be using it */
Jeff Layton0cc11a62016-10-20 09:34:31 -04006557 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006558 if (!list_empty(&nbl->nbl_list)) {
6559 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006560 list_del_init(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -04006561 queue = true;
6562 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04006563 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006564
Chuck Lever2cde7f82021-05-14 15:57:08 -04006565 if (queue) {
6566 trace_nfsd_cb_notify_lock(lo, nbl);
Jeff Layton76d348f2016-09-16 16:28:24 -04006567 nfsd4_run_cb(&nbl->nbl_cb);
Chuck Lever2cde7f82021-05-14 15:57:08 -04006568 }
Jeff Layton76d348f2016-09-16 16:28:24 -04006569}
6570
Alexey Dobriyan7b021962009-09-21 17:01:12 -07006571static const struct lock_manager_operations nfsd_posix_mng_ops = {
Jeff Layton76d348f2016-09-16 16:28:24 -04006572 .lm_notify = nfsd4_lm_notify,
Kinglong Meeaef95832014-08-22 10:18:44 -04006573 .lm_get_owner = nfsd4_fl_get_owner,
6574 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07006575};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576
6577static inline void
6578nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
6579{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006580 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581
NeilBrownd5b90262006-04-10 22:55:22 -07006582 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006583 lo = (struct nfs4_lockowner *) fl->fl_owner;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04006584 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
6585 GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006586 if (!deny->ld_owner.data)
6587 /* We just don't care that much */
6588 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006589 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07006590 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006591nevermind:
6592 deny->ld_owner.len = 0;
6593 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07006594 deny->ld_clientid.cl_boot = 0;
6595 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006596 }
6597 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02006598 deny->ld_length = NFS4_MAX_UINT64;
6599 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600 deny->ld_length = fl->fl_end - fl->fl_start + 1;
6601 deny->ld_type = NFS4_READ_LT;
6602 if (fl->fl_type != F_RDLCK)
6603 deny->ld_type = NFS4_WRITE_LT;
6604}
6605
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006606static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006607find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608{
Trond Myklebustd4f04892014-07-29 21:34:36 -04006609 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006610 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611
Trond Myklebust0a880a22014-07-30 08:27:10 -04006612 lockdep_assert_held(&clp->cl_lock);
6613
Trond Myklebustd4f04892014-07-29 21:34:36 -04006614 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
6615 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006616 if (so->so_is_open_owner)
6617 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006618 if (same_owner_str(so, owner))
6619 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620 }
6621 return NULL;
6622}
6623
Trond Myklebustc58c6612014-07-29 21:34:35 -04006624static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006625find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04006626{
6627 struct nfs4_lockowner *lo;
6628
Trond Myklebustd4f04892014-07-29 21:34:36 -04006629 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006630 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04006631 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006632 return lo;
6633}
6634
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006635static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
6636{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006637 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006638}
6639
Jeff Layton6b180f02014-07-29 21:34:26 -04006640static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
6641{
6642 struct nfs4_lockowner *lo = lockowner(sop);
6643
6644 kmem_cache_free(lockowner_slab, lo);
6645}
6646
6647static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006648 .so_unhash = nfs4_unhash_lockowner,
6649 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04006650};
6651
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652/*
6653 * Alloc a lock owner structure.
6654 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006655 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006657 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006659static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04006660alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
6661 struct nfs4_ol_stateid *open_stp,
6662 struct nfsd4_lock *lock)
6663{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006664 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006666 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
6667 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006669 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006670 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
6671 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04006672 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04006673 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006674 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006675 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006676 if (ret == NULL) {
6677 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04006678 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006679 ret = lo;
6680 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08006681 nfs4_free_stateowner(&lo->lo_owner);
6682
Trond Myklebustd4f04892014-07-29 21:34:36 -04006683 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04006684 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685}
6686
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006687static struct nfs4_ol_stateid *
Trond Myklebusta451b122020-03-01 18:21:38 -05006688find_lock_stateid(const struct nfs4_lockowner *lo,
6689 const struct nfs4_ol_stateid *ost)
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006690{
6691 struct nfs4_ol_stateid *lst;
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006692
Trond Myklebusta451b122020-03-01 18:21:38 -05006693 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006694
Trond Myklebusta451b122020-03-01 18:21:38 -05006695 /* If ost is not hashed, ost->st_locks will not be valid */
6696 if (!nfs4_ol_stateid_unhashed(ost))
6697 list_for_each_entry(lst, &ost->st_locks, st_locks) {
6698 if (lst->st_stateowner == &lo->lo_owner) {
6699 refcount_inc(&lst->st_stid.sc_count);
6700 return lst;
6701 }
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006702 }
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006703 return NULL;
6704}
6705
Trond Myklebustbeeca192017-11-03 08:00:14 -04006706static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006707init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
6708 struct nfs4_file *fp, struct inode *inode,
6709 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04006711 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustbeeca192017-11-03 08:00:14 -04006712 struct nfs4_ol_stateid *retstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713
Trond Myklebustbeeca192017-11-03 08:00:14 -04006714 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05006715 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006716retry:
6717 spin_lock(&clp->cl_lock);
Trond Myklebusta451b122020-03-01 18:21:38 -05006718 if (nfs4_ol_stateid_unhashed(open_stp))
6719 goto out_close;
6720 retstp = find_lock_stateid(lo, open_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006721 if (retstp)
Trond Myklebusta451b122020-03-01 18:21:38 -05006722 goto out_found;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03006723 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05006724 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006725 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07006726 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04006727 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006728 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07006730 stp->st_openstp = open_stp;
Trond Myklebusta451b122020-03-01 18:21:38 -05006731 spin_lock(&fp->fi_lock);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006732 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04006733 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04006734 list_add(&stp->st_perfile, &fp->fi_stateids);
6735 spin_unlock(&fp->fi_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006736 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006737 return stp;
Trond Myklebusta451b122020-03-01 18:21:38 -05006738out_found:
6739 spin_unlock(&clp->cl_lock);
6740 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
6741 nfs4_put_stid(&retstp->st_stid);
6742 goto retry;
6743 }
6744 /* To keep mutex tracking happy */
6745 mutex_unlock(&stp->st_mutex);
6746 return retstp;
6747out_close:
6748 spin_unlock(&clp->cl_lock);
6749 mutex_unlock(&stp->st_mutex);
6750 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751}
6752
Jeff Laytonc53530d2014-06-30 11:48:39 -04006753static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006754find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
6755 struct inode *inode, struct nfs4_ol_stateid *ost,
6756 bool *new)
6757{
6758 struct nfs4_stid *ns = NULL;
6759 struct nfs4_ol_stateid *lst;
6760 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6761 struct nfs4_client *clp = oo->oo_owner.so_client;
6762
Trond Myklebustbeeca192017-11-03 08:00:14 -04006763 *new = false;
Jeff Layton356a95e2014-07-29 21:34:13 -04006764 spin_lock(&clp->cl_lock);
Trond Myklebusta451b122020-03-01 18:21:38 -05006765 lst = find_lock_stateid(lo, ost);
Jeff Layton356a95e2014-07-29 21:34:13 -04006766 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006767 if (lst != NULL) {
6768 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
6769 goto out;
6770 nfs4_put_stid(&lst->st_stid);
6771 }
6772 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
6773 if (ns == NULL)
6774 return NULL;
6775
6776 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
6777 if (lst == openlockstateid(ns))
6778 *new = true;
6779 else
Jeff Layton356a95e2014-07-29 21:34:13 -04006780 nfs4_put_stid(ns);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006781out:
Jeff Layton356a95e2014-07-29 21:34:13 -04006782 return lst;
6783}
Jeff Laytonc53530d2014-06-30 11:48:39 -04006784
NeilBrownfd39ca92005-06-23 22:04:03 -07006785static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786check_lock_length(u64 offset, u64 length)
6787{
Kinglong Meee7969312015-07-13 17:34:19 +08006788 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
6789 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790}
6791
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006792static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006793{
Trond Myklebust11b91642014-07-29 21:34:08 -04006794 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006795
Jeff Layton7214e862014-07-10 14:07:33 -04006796 lockdep_assert_held(&fp->fi_lock);
6797
Jeff Layton82c5ff12012-05-11 09:45:13 -04006798 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05006799 return;
Jeff Layton12659652014-07-10 14:07:28 -04006800 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006801 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006802}
6803
Jeff Layton356a95e2014-07-29 21:34:13 -04006804static __be32
6805lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
6806 struct nfs4_ol_stateid *ost,
6807 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04006808 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006809{
Jeff Layton5db1c032014-07-29 21:34:28 -04006810 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04006811 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006812 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6813 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00006814 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006815 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04006816 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006817 unsigned int strhashval;
6818
Kinglong Meec8623992015-07-13 17:35:37 +08006819 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006820 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006821 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006822 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
6823 if (lo == NULL)
6824 return nfserr_jukebox;
6825 } else {
6826 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04006827 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04006828 if (!cstate->minorversion &&
6829 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04006830 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006831 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04006832
Jeff Laytondd257932016-08-11 10:37:39 -04006833 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
6834 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04006835 status = nfserr_jukebox;
6836 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006837 }
Jeff Laytondd257932016-08-11 10:37:39 -04006838
Jeff Layton5db1c032014-07-29 21:34:28 -04006839 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04006840 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04006841out:
6842 nfs4_put_stateowner(&lo->lo_owner);
6843 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006844}
6845
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846/*
6847 * LOCK operation
6848 */
Al Virob37ad282006-10-19 23:28:59 -07006849__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006850nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006851 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006853 struct nfsd4_lock *lock = &u->lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006854 struct nfs4_openowner *open_sop = NULL;
6855 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006856 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006857 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04006858 struct nfs4_file *fp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006859 struct nfsd_file *nf = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006860 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006861 struct file_lock *file_lock = NULL;
6862 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006863 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006864 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07006865 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04006866 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04006867 unsigned char fl_type;
6868 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006869 struct net *net = SVC_NET(rqstp);
6870 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871
6872 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
6873 (long long) lock->lk_offset,
6874 (long long) lock->lk_length);
6875
Linus Torvalds1da177e2005-04-16 15:20:36 -07006876 if (check_lock_length(lock->lk_offset, lock->lk_length))
6877 return nfserr_inval;
6878
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006879 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02006880 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08006881 dprintk("NFSD: nfsd4_lock: permission denied!\n");
6882 return status;
6883 }
6884
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04006886 if (nfsd4_has_session(cstate))
6887 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006888 memcpy(&lock->lk_new_clientid,
J. Bruce Fieldsec596592021-01-21 17:57:45 -05006889 &cstate->clp->cl_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04006890 sizeof(clientid_t));
6891
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006893 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894 lock->lk_new_open_seqid,
6895 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006896 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07006897 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006899 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006900 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006901 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04006902 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006903 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006904 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006905 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04006906 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006907 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03006908 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006909 lock->lk_old_lock_seqid,
6910 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006911 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006912 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04006913 if (status)
6914 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006915 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006917 lkflg = setlkflg(lock->lk_type);
6918 status = nfs4_check_openmode(lock_stp, lkflg);
6919 if (status)
6920 goto out;
6921
NeilBrown0dd395d2005-07-07 17:59:15 -07006922 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006923 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006924 goto out;
6925 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006926 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006927 goto out;
6928
J. Bruce Fieldsbb0a55b2021-08-20 17:02:06 -04006929 if (lock->lk_reclaim)
6930 fl_flags |= FL_RECLAIM;
6931
Trond Myklebust11b91642014-07-29 21:34:08 -04006932 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006934 case NFS4_READW_LT:
J. Bruce Fields40595cd2021-12-16 12:20:13 -05006935 if (nfsd4_has_session(cstate))
Jeff Layton76d348f2016-09-16 16:28:24 -04006936 fl_flags |= FL_SLEEP;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006937 fallthrough;
Jeff Layton76d348f2016-09-16 16:28:24 -04006938 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006939 spin_lock(&fp->fi_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006940 nf = find_readable_file_locked(fp);
6941 if (nf)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006942 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04006943 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006944 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006945 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946 case NFS4_WRITEW_LT:
J. Bruce Fields40595cd2021-12-16 12:20:13 -05006947 if (nfsd4_has_session(cstate))
Jeff Layton76d348f2016-09-16 16:28:24 -04006948 fl_flags |= FL_SLEEP;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006949 fallthrough;
Jeff Layton76d348f2016-09-16 16:28:24 -04006950 case NFS4_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006951 spin_lock(&fp->fi_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006952 nf = find_writeable_file_locked(fp);
6953 if (nf)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006954 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04006955 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006956 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006957 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958 default:
6959 status = nfserr_inval;
6960 goto out;
6961 }
Jeff Layton76d348f2016-09-16 16:28:24 -04006962
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006963 if (!nf) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006964 status = nfserr_openmode;
6965 goto out;
6966 }
Kinglong Meeaef95832014-08-22 10:18:44 -04006967
J. Bruce Fields40595cd2021-12-16 12:20:13 -05006968 /*
6969 * Most filesystems with their own ->lock operations will block
6970 * the nfsd thread waiting to acquire the lock. That leads to
6971 * deadlocks (we don't want every nfsd thread tied up waiting
6972 * for file locks), so don't attempt blocking lock notifications
6973 * on those filesystems:
6974 */
6975 if (nf->nf_file->f_op->lock)
6976 fl_flags &= ~FL_SLEEP;
6977
Jeff Layton76d348f2016-09-16 16:28:24 -04006978 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6979 if (!nbl) {
6980 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6981 status = nfserr_jukebox;
6982 goto out;
6983 }
6984
6985 file_lock = &nbl->nbl_lock;
6986 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04006987 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04006988 file_lock->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006989 file_lock->fl_file = nf->nf_file;
Jeff Layton76d348f2016-09-16 16:28:24 -04006990 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04006991 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6992 file_lock->fl_start = lock->lk_offset;
6993 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6994 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995
Jeff Layton21179d82012-08-21 08:03:32 -04006996 conflock = locks_alloc_lock();
6997 if (!conflock) {
6998 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6999 status = nfserr_jukebox;
7000 goto out;
7001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002
Jeff Layton76d348f2016-09-16 16:28:24 -04007003 if (fl_flags & FL_SLEEP) {
Arnd Bergmann20b7d862019-11-04 16:31:52 +01007004 nbl->nbl_time = ktime_get_boottime_seconds();
Jeff Layton0cc11a62016-10-20 09:34:31 -04007005 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04007006 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
Jeff Layton7919d0a2016-09-16 16:28:25 -04007007 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
Vasily Averin47446d72021-12-17 09:49:39 +03007008 kref_get(&nbl->nbl_kref);
Jeff Layton0cc11a62016-10-20 09:34:31 -04007009 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04007010 }
7011
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007012 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04007013 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007014 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04007015 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07007016 status = 0;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04007017 if (lock->lk_reclaim)
7018 nn->somebody_reclaimed = true;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08007019 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04007020 case FILE_LOCK_DEFERRED:
Vasily Averin47446d72021-12-17 09:49:39 +03007021 kref_put(&nbl->nbl_kref, free_nbl);
Jeff Layton76d348f2016-09-16 16:28:24 -04007022 nbl = NULL;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05007023 fallthrough;
Jeff Layton76d348f2016-09-16 16:28:24 -04007024 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08007025 status = nfserr_denied;
7026 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04007027 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08007028 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04007029 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08007031 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04007032 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05007033 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04007034 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08007035 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037out:
Jeff Layton76d348f2016-09-16 16:28:24 -04007038 if (nbl) {
7039 /* dequeue it if we queued it before */
7040 if (fl_flags & FL_SLEEP) {
Jeff Layton0cc11a62016-10-20 09:34:31 -04007041 spin_lock(&nn->blocked_locks_lock);
Vasily Averin47446d72021-12-17 09:49:39 +03007042 if (!list_empty(&nbl->nbl_list) &&
7043 !list_empty(&nbl->nbl_lru)) {
7044 list_del_init(&nbl->nbl_list);
7045 list_del_init(&nbl->nbl_lru);
7046 kref_put(&nbl->nbl_kref, free_nbl);
7047 }
7048 /* nbl can use one of lists to be linked to reaplist */
Jeff Layton0cc11a62016-10-20 09:34:31 -04007049 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04007050 }
7051 free_blocked_lock(nbl);
7052 }
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007053 if (nf)
7054 nfsd_file_put(nf);
Jeff Layton5db1c032014-07-29 21:34:28 -04007055 if (lock_stp) {
7056 /* Bump seqid manually if the 4.0 replay owner is openowner */
7057 if (cstate->replay_owner &&
7058 cstate->replay_owner != &lock_sop->lo_owner &&
7059 seqid_mutating_err(ntohl(status)))
7060 lock_sop->lo_owner.so_seqid++;
7061
7062 /*
7063 * If this is a new, never-before-used stateid, and we are
7064 * returning an error, then just go ahead and release it.
7065 */
J. Bruce Fields25020722018-01-17 16:25:59 -05007066 if (status && new)
Jeff Layton5db1c032014-07-29 21:34:28 -04007067 release_lock_stateid(lock_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04007068
7069 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton5db1c032014-07-29 21:34:28 -04007070
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04007071 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04007072 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04007073 if (open_stp)
7074 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04007075 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04007076 if (conflock)
7077 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007078 return status;
7079}
7080
7081/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08007082 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
7083 * so we do a temporary open here just to get an open file to pass to
J. Bruce Fields0bcc7ca2021-08-24 22:36:03 -04007084 * vfs_test_lock.
J. Bruce Fields55ef1272008-12-20 11:58:38 -08007085 */
Al Viro04da6e92012-04-13 00:00:04 -04007086static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08007087{
Jeff Layton6b556ca2019-08-18 14:18:55 -04007088 struct nfsd_file *nf;
J. Bruce Fields217fd6f2021-04-16 14:00:14 -04007089 __be32 err;
7090
7091 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
7092 if (err)
7093 return err;
7094 fh_lock(fhp); /* to block new leases till after test_lock: */
7095 err = nfserrno(nfsd_open_break_lease(fhp->fh_dentry->d_inode,
7096 NFSD_MAY_READ));
7097 if (err)
7098 goto out;
J. Bruce Fields0bcc7ca2021-08-24 22:36:03 -04007099 lock->fl_file = nf->nf_file;
J. Bruce Fields217fd6f2021-04-16 14:00:14 -04007100 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
J. Bruce Fields0bcc7ca2021-08-24 22:36:03 -04007101 lock->fl_file = NULL;
J. Bruce Fields217fd6f2021-04-16 14:00:14 -04007102out:
7103 fh_unlock(fhp);
7104 nfsd_file_put(nf);
J. Bruce Fields55ef1272008-12-20 11:58:38 -08007105 return err;
7106}
7107
7108/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109 * LOCKT operation
7110 */
Al Virob37ad282006-10-19 23:28:59 -07007111__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08007112nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02007113 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114{
Christoph Hellwigeb698532017-05-08 20:58:35 +02007115 struct nfsd4_lockt *lockt = &u->lockt;
Jeff Layton21179d82012-08-21 08:03:32 -04007116 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04007117 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07007118 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03007119 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04007121 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122 return nfserr_grace;
7123
7124 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
7125 return nfserr_inval;
7126
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05007127 if (!nfsd4_has_session(cstate)) {
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05007128 status = set_client(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05007129 if (status)
7130 goto out;
7131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132
J. Bruce Fields75c096f2011-08-15 18:39:32 -04007133 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135
Jeff Layton21179d82012-08-21 08:03:32 -04007136 file_lock = locks_alloc_lock();
7137 if (!file_lock) {
7138 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7139 status = nfserr_jukebox;
7140 goto out;
7141 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08007142
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143 switch (lockt->lt_type) {
7144 case NFS4_READ_LT:
7145 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04007146 file_lock->fl_type = F_RDLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00007147 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148 case NFS4_WRITE_LT:
7149 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04007150 file_lock->fl_type = F_WRLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00007151 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04007153 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154 status = nfserr_inval;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00007155 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156 }
7157
Kinglong Meec8623992015-07-13 17:35:37 +08007158 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04007159 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04007160 file_lock->fl_owner = (fl_owner_t)lo;
7161 file_lock->fl_pid = current->tgid;
7162 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163
Jeff Layton21179d82012-08-21 08:03:32 -04007164 file_lock->fl_start = lockt->lt_offset;
7165 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166
Jeff Layton21179d82012-08-21 08:03:32 -04007167 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168
Jeff Layton21179d82012-08-21 08:03:32 -04007169 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04007170 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05007171 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04007172
Jeff Layton21179d82012-08-21 08:03:32 -04007173 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04007175 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176 }
7177out:
Jeff Layton5db1c032014-07-29 21:34:28 -04007178 if (lo)
7179 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04007180 if (file_lock)
7181 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007182 return status;
7183}
7184
Al Virob37ad282006-10-19 23:28:59 -07007185__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08007186nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02007187 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188{
Christoph Hellwigeb698532017-05-08 20:58:35 +02007189 struct nfsd4_locku *locku = &u->locku;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04007190 struct nfs4_ol_stateid *stp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007191 struct nfsd_file *nf = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04007192 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07007193 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07007194 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007195 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7196
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
7198 (long long) locku->lu_offset,
7199 (long long) locku->lu_length);
7200
7201 if (check_lock_length(locku->lu_offset, locku->lu_length))
7202 return nfserr_inval;
7203
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04007204 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007205 &locku->lu_stateid, NFS4_LOCK_STID,
7206 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04007207 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208 goto out;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007209 nf = find_any_file(stp->st_stid.sc_file);
7210 if (!nf) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04007211 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04007212 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04007213 }
Jeff Layton21179d82012-08-21 08:03:32 -04007214 file_lock = locks_alloc_lock();
7215 if (!file_lock) {
7216 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7217 status = nfserr_jukebox;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007218 goto put_file;
Jeff Layton21179d82012-08-21 08:03:32 -04007219 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08007220
Jeff Layton21179d82012-08-21 08:03:32 -04007221 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04007222 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04007223 file_lock->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007224 file_lock->fl_file = nf->nf_file;
Jeff Layton21179d82012-08-21 08:03:32 -04007225 file_lock->fl_flags = FL_POSIX;
7226 file_lock->fl_lmops = &nfsd_posix_mng_ops;
7227 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228
Jeff Layton21179d82012-08-21 08:03:32 -04007229 file_lock->fl_end = last_byte_offset(locku->lu_offset,
7230 locku->lu_length);
7231 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007232
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007233 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07007234 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05007235 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236 goto out_nfserr;
7237 }
Jeff Layton9767feb2015-10-01 09:05:50 -04007238 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007239put_file:
7240 nfsd_file_put(nf);
Trond Myklebust858cc572014-07-29 21:34:16 -04007241put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04007242 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04007243 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04007245 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04007246 if (file_lock)
7247 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248 return status;
7249
7250out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07007251 status = nfserrno(err);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007252 goto put_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253}
7254
7255/*
7256 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007257 * true: locks held by lockowner
7258 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07007259 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007260static bool
7261check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007262{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007263 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007264 int status = false;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007265 struct nfsd_file *nf = find_any_file(fp);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007266 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007267 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007268
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007269 if (!nf) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007270 /* Any valid lock stateid should have some sort of access */
7271 WARN_ON_ONCE(1);
7272 return status;
7273 }
7274
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007275 inode = locks_inode(nf->nf_file);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007276 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007277
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007278 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05007279 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007280 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
7281 if (fl->fl_owner == (fl_owner_t)lowner) {
7282 status = true;
7283 break;
7284 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08007285 }
Jeff Layton6109c852015-01-16 15:05:57 -05007286 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287 }
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007288 nfsd_file_put(nf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007289 return status;
7290}
7291
Al Virob37ad282006-10-19 23:28:59 -07007292__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08007293nfsd4_release_lockowner(struct svc_rqst *rqstp,
7294 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02007295 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007296{
Christoph Hellwigeb698532017-05-08 20:58:35 +02007297 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007298 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04007299 struct nfs4_stateowner *sop;
7300 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04007301 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007302 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04007303 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07007304 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03007305 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04007306 struct nfs4_client *clp;
Chuck Lever88584812016-07-13 16:40:14 -04007307 LIST_HEAD (reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308
7309 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
7310 clid->cl_boot, clid->cl_id);
7311
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05007312 status = set_client(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05007313 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04007314 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05007315
Trond Myklebustd4f04892014-07-29 21:34:36 -04007316 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04007317 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04007318 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04007319 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04007320 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04007321
7322 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05007323 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04007324
Jeff Layton882e9d22014-07-29 21:34:37 -04007325 /* see if there are still any locks associated with it */
7326 lo = lockowner(sop);
7327 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
7328 if (check_for_locks(stp->st_stid.sc_file, lo)) {
7329 status = nfserr_locks_held;
7330 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04007331 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04007332 }
Jeff Layton5adfd882014-07-29 21:34:31 -04007333 }
Jeff Layton882e9d22014-07-29 21:34:37 -04007334
Kinglong Meeb5971af2014-08-22 10:18:43 -04007335 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04007336 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04007337 }
Chuck Lever88584812016-07-13 16:40:14 -04007338 if (!lo) {
7339 spin_unlock(&clp->cl_lock);
7340 return status;
7341 }
7342
7343 unhash_lockowner_locked(lo);
7344 while (!list_empty(&lo->lo_owner.so_stateids)) {
7345 stp = list_first_entry(&lo->lo_owner.so_stateids,
7346 struct nfs4_ol_stateid,
7347 st_perstateowner);
7348 WARN_ON(!unhash_lock_stateid(stp));
7349 put_ol_stateid_locked(stp, &reaplist);
7350 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04007351 spin_unlock(&clp->cl_lock);
Chuck Lever88584812016-07-13 16:40:14 -04007352 free_ol_stateid_reaplist(&reaplist);
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007353 remove_blocked_locks(lo);
Chuck Lever88584812016-07-13 16:40:14 -04007354 nfs4_put_stateowner(&lo->lo_owner);
7355
Linus Torvalds1da177e2005-04-16 15:20:36 -07007356 return status;
7357}
7358
7359static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07007360alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361{
NeilBrowna55370a2005-06-23 22:03:52 -07007362 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363}
7364
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007365bool
Scott Mayhew6b189102019-03-26 18:06:26 -04007366nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07007367{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007368 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07007369
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007370 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007371 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07007372}
7373
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374/*
7375 * failure => all reset bets are off, nfserr_no_grace...
Scott Mayhew6b189102019-03-26 18:06:26 -04007376 *
7377 * The caller is responsible for freeing name.data if NULL is returned (it
7378 * will be freed in nfs4_remove_reclaim_record in the normal case).
Linus Torvalds1da177e2005-04-16 15:20:36 -07007379 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05007380struct nfs4_client_reclaim *
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007381nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
7382 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383{
7384 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05007385 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007386
NeilBrowna55370a2005-06-23 22:03:52 -07007387 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05007388 if (crp) {
7389 strhashval = clientstr_hashval(name);
7390 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007391 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Scott Mayhew6b189102019-03-26 18:06:26 -04007392 crp->cr_name.data = name.data;
7393 crp->cr_name.len = name.len;
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007394 crp->cr_princhash.data = princhash.data;
7395 crp->cr_princhash.len = princhash.len;
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007396 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007397 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05007398 }
7399 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400}
7401
Jeff Layton2a4317c2012-03-21 16:42:43 -04007402void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007403nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05007404{
7405 list_del(&crp->cr_strhash);
Scott Mayhew6b189102019-03-26 18:06:26 -04007406 kfree(crp->cr_name.data);
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007407 kfree(crp->cr_princhash.data);
Jeff Laytonce30e532012-11-12 15:00:53 -05007408 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007409 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05007410}
7411
7412void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007413nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007414{
7415 struct nfs4_client_reclaim *crp = NULL;
7416 int i;
7417
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007419 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
7420 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007422 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423 }
7424 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05007425 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426}
7427
7428/*
7429 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04007430struct nfs4_client_reclaim *
Scott Mayhew6b189102019-03-26 18:06:26 -04007431nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007432{
7433 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007434 struct nfs4_client_reclaim *crp = NULL;
7435
Scott Mayhew6b189102019-03-26 18:06:26 -04007436 strhashval = clientstr_hashval(name);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007437 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Scott Mayhew6b189102019-03-26 18:06:26 -04007438 if (compare_blob(&crp->cr_name, &name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439 return crp;
7440 }
7441 }
7442 return NULL;
7443}
7444
Al Virob37ad282006-10-19 23:28:59 -07007445__be32
J. Bruce Fields1722b042021-01-21 17:57:44 -05007446nfs4_check_open_reclaim(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007447{
J. Bruce Fields1722b042021-01-21 17:57:44 -05007448 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
Jeff Layton3b3e7b72014-09-12 16:40:22 -04007449 return nfserr_no_grace;
7450
J. Bruce Fields1722b042021-01-21 17:57:44 -05007451 if (nfsd4_client_record_check(clp))
Trond Myklebust0fe492d2014-06-30 11:48:47 -04007452 return nfserr_reclaim_bad;
7453
7454 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455}
7456
Meelap Shahc2f1a552007-07-17 04:04:39 -07007457/*
7458 * Since the lifetime of a delegation isn't limited to that of an open, a
7459 * client may quite reasonably hang on to a delegation as long as it has
7460 * the inode cached. This becomes an obvious problem the first time a
7461 * client's inode cache approaches the size of the server's total memory.
7462 *
7463 * For now we avoid this problem by imposing a hard limit on the number
7464 * of delegations, which varies according to the server's memory size.
7465 */
7466static void
7467set_max_delegations(void)
7468{
7469 /*
7470 * Allow at most 4 delegations per megabyte of RAM. Quick
7471 * estimates suggest that in the worst case (where every delegation
7472 * is for a different inode), a delegation could take about 1.5K,
7473 * giving a worst case usage of about 6% of memory.
7474 */
7475 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7476}
7477
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007478static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007479{
7480 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7481 int i;
7482
Kees Cook6da2ec52018-06-12 13:55:00 -07007483 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7484 sizeof(struct list_head),
7485 GFP_KERNEL);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007486 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007487 goto err;
Kees Cook6da2ec52018-06-12 13:55:00 -07007488 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7489 sizeof(struct list_head),
7490 GFP_KERNEL);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007491 if (!nn->unconf_id_hashtbl)
7492 goto err_unconf_id;
Kees Cook6da2ec52018-06-12 13:55:00 -07007493 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7494 sizeof(struct list_head),
7495 GFP_KERNEL);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007496 if (!nn->sessionid_hashtbl)
7497 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007498
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007499 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007500 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007501 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007502 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007503 for (i = 0; i < SESSION_HASH_SIZE; i++)
7504 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007505 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007506 nn->unconf_name_tree = RB_ROOT;
Arnd Bergmann9cc76802019-10-31 15:53:13 +01007507 nn->boot_time = ktime_get_real_seconds();
Vasily Averin81833de2017-11-13 07:25:40 +03007508 nn->grace_ended = false;
7509 nn->nfsd4_manager.block_opens = true;
7510 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03007511 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03007512 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007513 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03007514 spin_lock_init(&nn->client_lock);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04007515 spin_lock_init(&nn->s2s_cp_lock);
7516 idr_init(&nn->s2s_cp_stateids);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007517
Jeff Layton0cc11a62016-10-20 09:34:31 -04007518 spin_lock_init(&nn->blocked_locks_lock);
7519 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7520
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007521 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007522 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007523
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007524 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007525
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007526err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03007527 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007528err_unconf_id:
7529 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007530err:
7531 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007532}
7533
7534static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007535nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007536{
7537 int i;
7538 struct nfs4_client *clp = NULL;
7539 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7540
7541 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7542 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7543 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7544 destroy_client(clp);
7545 }
7546 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007547
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007548 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7549
Kinglong Mee2b905632014-03-26 22:09:30 +08007550 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7551 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7552 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7553 destroy_client(clp);
7554 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007555 }
7556
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007557 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007558 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007559 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007560 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007561}
7562
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007563int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007564nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07007565{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04007566 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007567 int ret;
7568
Trond Myklebustc6c7f2a2021-03-13 16:08:47 -05007569 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007570 if (ret)
7571 return ret;
Jeff Laytond4318ac2014-09-12 16:40:21 -04007572 locks_start_grace(net, &nn->nfsd4_manager);
7573 nfsd4_client_tracking_init(net);
Scott Mayhew362063a2019-03-26 18:06:28 -04007574 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7575 goto skip_grace;
Arnd Bergmann20b7d862019-11-04 16:31:52 +01007576 printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
Vasily Averin7e981a82017-11-06 16:46:04 +03007577 nn->nfsd4_grace, net->ns.inum);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04007578 trace_nfsd_grace_start(nn);
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03007579 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007580 return 0;
Scott Mayhew362063a2019-03-26 18:06:28 -04007581
7582skip_grace:
7583 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7584 net->ns.inum);
7585 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7586 nfsd4_end_grace(nn);
7587 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007588}
7589
7590/* initialization to perform when the nfsd service is started: */
7591
7592int
7593nfs4_state_start(void)
7594{
7595 int ret;
7596
Jeff Layton51a54562015-08-20 07:17:01 -04007597 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05007598 if (laundry_wq == NULL) {
7599 ret = -ENOMEM;
Chuck Levera26dd642018-08-16 12:06:09 -04007600 goto out;
Jeff Laytona6d6b782012-03-05 11:42:36 -05007601 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007602 ret = nfsd4_create_callback_queue();
7603 if (ret)
7604 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007605
Meelap Shahc2f1a552007-07-17 04:04:39 -07007606 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007607 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007608
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007609out_free_laundry:
7610 destroy_workqueue(laundry_wq);
Chuck Levera26dd642018-08-16 12:06:09 -04007611out:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007612 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007613}
7614
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007615void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007616nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007617{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007618 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007620 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007621
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007622 cancel_delayed_work_sync(&nn->laundromat_work);
7623 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05007624
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04007626 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007627 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007628 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007629 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007630 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007631 }
Benny Halevycdc975052014-05-30 09:09:30 -04007632 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633 list_for_each_safe(pos, next, &reaplist) {
7634 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04007635 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05007636 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007637 }
7638
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007639 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007640 nfs4_state_destroy_net(net);
Dai Ngof4e44b32021-05-21 15:09:37 -04007641#ifdef CONFIG_NFSD_V4_2_INTER_SSC
7642 nfsd4_ssc_shutdown_umount(nn);
7643#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007644}
7645
7646void
7647nfs4_state_shutdown(void)
7648{
NeilBrown5e8d5c22006-04-10 22:55:37 -07007649 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04007650 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007651}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007652
7653static void
7654get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7655{
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007656 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
7657 CURRENT_STATEID(stateid))
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007658 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007659}
7660
7661static void
7662put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7663{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007664 if (cstate->minorversion) {
7665 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007666 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007667 }
7668}
7669
7670void
7671clear_current_stateid(struct nfsd4_compound_state *cstate)
7672{
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007673 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007674}
7675
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007676/*
7677 * functions to set current state id
7678 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007679void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007680nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7681 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007682{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007683 put_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007684}
7685
7686void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007687nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7688 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007689{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007690 put_stateid(cstate, &u->open.op_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007691}
7692
7693void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007694nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7695 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007696{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007697 put_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007698}
7699
7700void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007701nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7702 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007703{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007704 put_stateid(cstate, &u->lock.lk_resp_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007705}
7706
7707/*
7708 * functions to consume current state id
7709 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007710
7711void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007712nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7713 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007714{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007715 get_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007716}
7717
7718void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007719nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7720 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007721{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007722 get_stateid(cstate, &u->delegreturn.dr_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007723}
7724
7725void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007726nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7727 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007728{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007729 get_stateid(cstate, &u->free_stateid.fr_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007730}
7731
7732void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007733nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7734 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007735{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007736 get_stateid(cstate, &u->setattr.sa_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007737}
7738
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007739void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007740nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7741 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007742{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007743 get_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007744}
7745
7746void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007747nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7748 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007749{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007750 get_stateid(cstate, &u->locku.lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007751}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007752
7753void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007754nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7755 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007756{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007757 get_stateid(cstate, &u->read.rd_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007758}
7759
7760void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007761nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7762 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007763{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007764 get_stateid(cstate, &u->write.wr_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007765}