blob: 2e7bbaa9268405c17ac8c7cf4ce31b515e0680b8 [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>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020047#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040048#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050049#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040050#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040052#include "netns.h"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020053#include "pnfs.h"
Jeff Laytonfd4f83f2019-08-18 14:18:52 -040054#include "filecache.h"
Chuck Leverdd5e3fb2020-04-05 11:42:29 -040055#include "trace.h"
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define NFSDDBG_FACILITY NFSDDBG_PROC
58
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050059#define all_ones {{~0,~0},~0}
60static const stateid_t one_stateid = {
61 .si_generation = ~0,
62 .si_opaque = all_ones,
63};
64static const stateid_t zero_stateid = {
65 /* all fields zero */
66};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010067static const stateid_t currentstateid = {
68 .si_generation = 1,
69};
Trond Myklebustfb500a72017-11-03 08:00:12 -040070static const stateid_t close_stateid = {
71 .si_generation = 0xffffffffU,
72};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050073
Andy Adamsonec6b5d72009-04-03 08:28:28 +030074static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070075
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050076#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
77#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010078#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Andrew Elbleae254dac2017-11-09 13:41:10 -050079#define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040082static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040083static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Scott Mayhew362063a2019-03-26 18:06:28 -040084void nfsd4_end_grace(struct nfsd_net *nn);
Olga Kornievskaia624322f2019-10-04 16:34:26 -040085static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
J. Bruce Fields8b671b82009-02-22 14:51:34 -080087/* Locking: */
88
J. Bruce Fields8b671b82009-02-22 14:51:34 -080089/*
90 * Currently used for the del_recall_lru and file hash table. In an
91 * effort to decrease the scope of the client_mutex, this spinlock may
92 * eventually cover more:
93 */
Benny Halevycdc975052014-05-30 09:09:30 -040094static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080095
Andrew Elble4f34bd02017-11-08 17:29:51 -050096enum nfsd4_st_mutex_lock_subclass {
97 OPEN_STATEID_MUTEX = 0,
98 LOCK_STATEID_MUTEX = 1,
99};
100
Jeff Laytonb401be222014-07-29 21:34:33 -0400101/*
102 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
103 * the refcount on the open stateid to drop.
104 */
105static DECLARE_WAIT_QUEUE_HEAD(close_wq);
106
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400107/*
108 * A waitqueue where a writer to clients/#/ctl destroying a client can
109 * wait for cl_rpc_users to drop to 0 and then for the client to be
110 * unhashed.
111 */
112static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
113
Jeff Layton9258a2d2018-03-16 09:47:22 -0400114static struct kmem_cache *client_slab;
Christoph Hellwigabf11352014-05-21 07:43:03 -0700115static struct kmem_cache *openowner_slab;
116static struct kmem_cache *lockowner_slab;
117static struct kmem_cache *file_slab;
118static struct kmem_cache *stateid_slab;
119static struct kmem_cache *deleg_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +0200120static struct kmem_cache *odstate_slab;
NeilBrowne60d4392005-06-23 22:03:01 -0700121
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400122static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800123
Julia Lawallc4cb8972015-11-21 22:57:39 +0100124static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
Jeff Layton76d348f2016-09-16 16:28:24 -0400125static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200126
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400127static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800128{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400129 return ses->se_flags & NFS4_SESSION_DEAD;
130}
131
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400132static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
133{
134 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400135 return nfserr_jukebox;
136 ses->se_flags |= NFS4_SESSION_DEAD;
137 return nfs_ok;
138}
139
J. Bruce Fields221a6872013-04-01 22:23:49 -0400140static bool is_client_expired(struct nfs4_client *clp)
141{
142 return clp->cl_time == 0;
143}
144
J. Bruce Fields221a6872013-04-01 22:23:49 -0400145static __be32 get_client_locked(struct nfs4_client *clp)
146{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400147 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
148
149 lockdep_assert_held(&nn->client_lock);
150
J. Bruce Fields221a6872013-04-01 22:23:49 -0400151 if (is_client_expired(clp))
152 return nfserr_expired;
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400153 atomic_inc(&clp->cl_rpc_users);
J. Bruce Fields221a6872013-04-01 22:23:49 -0400154 return nfs_ok;
155}
156
157/* must be called under the client_lock */
158static inline void
159renew_client_locked(struct nfs4_client *clp)
160{
161 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
162
163 if (is_client_expired(clp)) {
164 WARN_ON(1);
165 printk("%s: client (clientid %08x/%08x) already expired\n",
166 __func__,
167 clp->cl_clientid.cl_boot,
168 clp->cl_clientid.cl_id);
169 return;
170 }
171
J. Bruce Fields221a6872013-04-01 22:23:49 -0400172 list_move_tail(&clp->cl_lru, &nn->client_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +0100173 clp->cl_time = ktime_get_boottime_seconds();
J. Bruce Fields221a6872013-04-01 22:23:49 -0400174}
175
Fengguang Wuba138432013-04-16 22:14:15 -0400176static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400177{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400178 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
179
180 lockdep_assert_held(&nn->client_lock);
181
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400182 if (!atomic_dec_and_test(&clp->cl_rpc_users))
J. Bruce Fields221a6872013-04-01 22:23:49 -0400183 return;
184 if (!is_client_expired(clp))
185 renew_client_locked(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400186 else
187 wake_up_all(&expiry_wq);
J. Bruce Fields221a6872013-04-01 22:23:49 -0400188}
189
Jeff Layton4b24ca72014-06-30 11:48:44 -0400190static void put_client_renew(struct nfs4_client *clp)
191{
192 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
193
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400194 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
Jeff Laytond6c249b2014-07-08 14:02:50 -0400195 return;
196 if (!is_client_expired(clp))
197 renew_client_locked(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400198 else
199 wake_up_all(&expiry_wq);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400200 spin_unlock(&nn->client_lock);
201}
202
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400203static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
204{
205 __be32 status;
206
207 if (is_session_dead(ses))
208 return nfserr_badsession;
209 status = get_client_locked(ses->se_client);
210 if (status)
211 return status;
212 atomic_inc(&ses->se_ref);
213 return nfs_ok;
214}
215
216static void nfsd4_put_session_locked(struct nfsd4_session *ses)
217{
218 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400219 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
220
221 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400222
223 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
224 free_session(ses);
225 put_client_renew_locked(clp);
226}
227
228static void nfsd4_put_session(struct nfsd4_session *ses)
229{
230 struct nfs4_client *clp = ses->se_client;
231 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
232
233 spin_lock(&nn->client_lock);
234 nfsd4_put_session_locked(ses);
235 spin_unlock(&nn->client_lock);
236}
237
Jeff Layton76d348f2016-09-16 16:28:24 -0400238static struct nfsd4_blocked_lock *
239find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
240 struct nfsd_net *nn)
241{
242 struct nfsd4_blocked_lock *cur, *found = NULL;
243
Jeff Layton0cc11a62016-10-20 09:34:31 -0400244 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400245 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
246 if (fh_match(fh, &cur->nbl_fh)) {
247 list_del_init(&cur->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -0400248 list_del_init(&cur->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400249 found = cur;
250 break;
251 }
252 }
Jeff Layton0cc11a62016-10-20 09:34:31 -0400253 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400254 if (found)
NeilBrowncb03f942018-11-30 10:04:08 +1100255 locks_delete_block(&found->nbl_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400256 return found;
257}
258
259static struct nfsd4_blocked_lock *
260find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
261 struct nfsd_net *nn)
262{
263 struct nfsd4_blocked_lock *nbl;
264
265 nbl = find_blocked_lock(lo, fh, nn);
266 if (!nbl) {
267 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
268 if (nbl) {
Vasily Averine1e83992020-03-27 07:50:40 +0300269 INIT_LIST_HEAD(&nbl->nbl_list);
270 INIT_LIST_HEAD(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400271 fh_copy_shallow(&nbl->nbl_fh, fh);
272 locks_init_lock(&nbl->nbl_lock);
273 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
274 &nfsd4_cb_notify_lock_ops,
275 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
276 }
277 }
278 return nbl;
279}
280
281static void
282free_blocked_lock(struct nfsd4_blocked_lock *nbl)
283{
Jeff Layton6aaafc42019-04-22 12:34:23 -0400284 locks_delete_block(&nbl->nbl_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400285 locks_release_private(&nbl->nbl_lock);
286 kfree(nbl);
287}
288
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400289static void
290remove_blocked_locks(struct nfs4_lockowner *lo)
291{
292 struct nfs4_client *clp = lo->lo_owner.so_client;
293 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
294 struct nfsd4_blocked_lock *nbl;
295 LIST_HEAD(reaplist);
296
297 /* Dequeue all blocked locks */
298 spin_lock(&nn->blocked_locks_lock);
299 while (!list_empty(&lo->lo_blocked)) {
300 nbl = list_first_entry(&lo->lo_blocked,
301 struct nfsd4_blocked_lock,
302 nbl_list);
303 list_del_init(&nbl->nbl_list);
304 list_move(&nbl->nbl_lru, &reaplist);
305 }
306 spin_unlock(&nn->blocked_locks_lock);
307
308 /* Now free them */
309 while (!list_empty(&reaplist)) {
310 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
311 nbl_lru);
312 list_del_init(&nbl->nbl_lru);
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400313 free_blocked_lock(nbl);
314 }
315}
316
Jeff Laytonf4564582019-04-22 12:34:24 -0400317static void
318nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
319{
320 struct nfsd4_blocked_lock *nbl = container_of(cb,
321 struct nfsd4_blocked_lock, nbl_cb);
322 locks_delete_block(&nbl->nbl_lock);
323}
324
Jeff Layton76d348f2016-09-16 16:28:24 -0400325static int
326nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
327{
328 /*
329 * Since this is just an optimization, we don't try very hard if it
330 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
331 * just quit trying on anything else.
332 */
333 switch (task->tk_status) {
334 case -NFS4ERR_DELAY:
335 rpc_delay(task, 1 * HZ);
336 return 0;
337 default:
338 return 1;
339 }
340}
341
342static void
343nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
344{
345 struct nfsd4_blocked_lock *nbl = container_of(cb,
346 struct nfsd4_blocked_lock, nbl_cb);
347
348 free_blocked_lock(nbl);
349}
350
351static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
Jeff Laytonf4564582019-04-22 12:34:24 -0400352 .prepare = nfsd4_cb_notify_lock_prepare,
Jeff Layton76d348f2016-09-16 16:28:24 -0400353 .done = nfsd4_cb_notify_lock_done,
354 .release = nfsd4_cb_notify_lock_release,
355};
356
J. Bruce Fieldsebd9d2c2021-04-16 14:00:17 -0400357/*
358 * We store the NONE, READ, WRITE, and BOTH bits separately in the
359 * st_{access,deny}_bmap field of the stateid, in order to track not
360 * only what share bits are currently in force, but also what
361 * combinations of share bits previous opens have used. This allows us
362 * to enforce the recommendation of rfc 3530 14.2.19 that the server
363 * return an error if the client attempt to downgrade to a combination
364 * of share bits not explicable by closing some of its previous opens.
365 *
366 * XXX: This enforcement is actually incomplete, since we don't keep
367 * track of access/deny bit combinations; so, e.g., we allow:
368 *
369 * OPEN allow read, deny write
370 * OPEN allow both, deny none
371 * DOWNGRADE allow read, deny none
372 *
373 * which we should reject.
374 */
375static unsigned int
376bmap_to_share_mode(unsigned long bmap)
377{
378 int i;
379 unsigned int access = 0;
380
381 for (i = 1; i < 4; i++) {
382 if (test_bit(i, &bmap))
383 access |= i;
384 }
385 return access;
386}
387
388/* set share access for a given stateid */
389static inline void
390set_access(u32 access, struct nfs4_ol_stateid *stp)
391{
392 unsigned char mask = 1 << access;
393
394 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
395 stp->st_access_bmap |= mask;
396}
397
398/* clear share access for a given stateid */
399static inline void
400clear_access(u32 access, struct nfs4_ol_stateid *stp)
401{
402 unsigned char mask = 1 << access;
403
404 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
405 stp->st_access_bmap &= ~mask;
406}
407
408/* test whether a given stateid has access */
409static inline bool
410test_access(u32 access, struct nfs4_ol_stateid *stp)
411{
412 unsigned char mask = 1 << access;
413
414 return (bool)(stp->st_access_bmap & mask);
415}
416
417/* set share deny for a given stateid */
418static inline void
419set_deny(u32 deny, struct nfs4_ol_stateid *stp)
420{
421 unsigned char mask = 1 << deny;
422
423 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
424 stp->st_deny_bmap |= mask;
425}
426
427/* clear share deny for a given stateid */
428static inline void
429clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
430{
431 unsigned char mask = 1 << deny;
432
433 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
434 stp->st_deny_bmap &= ~mask;
435}
436
437/* test whether a given stateid is denying specific access */
438static inline bool
439test_deny(u32 deny, struct nfs4_ol_stateid *stp)
440{
441 unsigned char mask = 1 << deny;
442
443 return (bool)(stp->st_deny_bmap & mask);
444}
445
446static int nfs4_access_to_omode(u32 access)
447{
448 switch (access & NFS4_SHARE_ACCESS_BOTH) {
449 case NFS4_SHARE_ACCESS_READ:
450 return O_RDONLY;
451 case NFS4_SHARE_ACCESS_WRITE:
452 return O_WRONLY;
453 case NFS4_SHARE_ACCESS_BOTH:
454 return O_RDWR;
455 }
456 WARN_ON_ONCE(1);
457 return O_RDONLY;
458}
459
460static inline int
461access_permit_read(struct nfs4_ol_stateid *stp)
462{
463 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
464 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
465 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
466}
467
468static inline int
469access_permit_write(struct nfs4_ol_stateid *stp)
470{
471 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
472 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
473}
474
Kinglong Meeb5971af2014-08-22 10:18:43 -0400475static inline struct nfs4_stateowner *
476nfs4_get_stateowner(struct nfs4_stateowner *sop)
477{
478 atomic_inc(&sop->so_count);
479 return sop;
480}
481
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400482static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400483same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400484{
485 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400486 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400487}
488
489static struct nfs4_openowner *
490find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400491 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400492{
493 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400494
Trond Myklebustd4f04892014-07-29 21:34:36 -0400495 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400496
Trond Myklebustd4f04892014-07-29 21:34:36 -0400497 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
498 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400499 if (!so->so_is_open_owner)
500 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400501 if (same_owner_str(so, &open->op_owner))
502 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400503 }
504 return NULL;
505}
506
507static struct nfs4_openowner *
508find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400509 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400510{
511 struct nfs4_openowner *oo;
512
Trond Myklebustd4f04892014-07-29 21:34:36 -0400513 spin_lock(&clp->cl_lock);
514 oo = find_openstateowner_str_locked(hashval, open, clp);
515 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400516 return oo;
517}
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519static inline u32
520opaque_hashval(const void *ptr, int nbytes)
521{
522 unsigned char *cptr = (unsigned char *) ptr;
523
524 u32 x = 0;
525 while (nbytes--) {
526 x *= 37;
527 x += *cptr++;
528 }
529 return x;
530}
531
Jeff Layton5b095e92014-10-23 08:01:02 -0400532static void nfsd4_free_file_rcu(struct rcu_head *rcu)
J. Bruce Fields32513b42011-10-13 16:00:16 -0400533{
Jeff Layton5b095e92014-10-23 08:01:02 -0400534 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
535
536 kmem_cache_free(file_slab, fp);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400537}
538
Christoph Hellwige6ba76e2014-08-14 08:50:16 +0200539void
NeilBrown13cd2182005-06-23 22:03:10 -0700540put_nfs4_file(struct nfs4_file *fi)
541{
Jeff Layton02e12152014-07-16 10:31:57 -0400542 might_lock(&state_lock);
543
Elena Reshetova818a34e2017-10-20 12:53:30 +0300544 if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton5b095e92014-10-23 08:01:02 -0400545 hlist_del_rcu(&fi->fi_hash);
Benny Halevycdc975052014-05-30 09:09:30 -0400546 spin_unlock(&state_lock);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200547 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
Jeff Layton5b095e92014-10-23 08:01:02 -0400548 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
549 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800550 }
NeilBrown13cd2182005-06-23 22:03:10 -0700551}
552
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400553static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400554__nfs4_get_fd(struct nfs4_file *f, int oflag)
555{
556 if (f->fi_fds[oflag])
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400557 return nfsd_file_get(f->fi_fds[oflag]);
Trond Myklebustde186432014-07-10 14:07:26 -0400558 return NULL;
559}
560
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400561static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400562find_writeable_file_locked(struct nfs4_file *f)
563{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400564 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400565
566 lockdep_assert_held(&f->fi_lock);
567
568 ret = __nfs4_get_fd(f, O_WRONLY);
569 if (!ret)
570 ret = __nfs4_get_fd(f, O_RDWR);
571 return ret;
572}
573
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400574static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400575find_writeable_file(struct nfs4_file *f)
576{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400577 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400578
579 spin_lock(&f->fi_lock);
580 ret = find_writeable_file_locked(f);
581 spin_unlock(&f->fi_lock);
582
583 return ret;
584}
585
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400586static struct nfsd_file *
587find_readable_file_locked(struct nfs4_file *f)
Trond Myklebustde186432014-07-10 14:07:26 -0400588{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400589 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400590
591 lockdep_assert_held(&f->fi_lock);
592
593 ret = __nfs4_get_fd(f, O_RDONLY);
594 if (!ret)
595 ret = __nfs4_get_fd(f, O_RDWR);
596 return ret;
597}
598
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400599static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400600find_readable_file(struct nfs4_file *f)
601{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400602 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400603
604 spin_lock(&f->fi_lock);
605 ret = find_readable_file_locked(f);
606 spin_unlock(&f->fi_lock);
607
608 return ret;
609}
610
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400611struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400612find_any_file(struct nfs4_file *f)
613{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400614 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400615
Trond Myklebusta451b122020-03-01 18:21:38 -0500616 if (!f)
617 return NULL;
Trond Myklebustde186432014-07-10 14:07:26 -0400618 spin_lock(&f->fi_lock);
619 ret = __nfs4_get_fd(f, O_RDWR);
620 if (!ret) {
621 ret = __nfs4_get_fd(f, O_WRONLY);
622 if (!ret)
623 ret = __nfs4_get_fd(f, O_RDONLY);
624 }
625 spin_unlock(&f->fi_lock);
626 return ret;
627}
628
J. Bruce Fields9affa432020-07-15 13:31:36 -0400629static struct nfsd_file *find_deleg_file(struct nfs4_file *f)
630{
631 struct nfsd_file *ret = NULL;
632
633 spin_lock(&f->fi_lock);
634 if (f->fi_deleg_file)
635 ret = nfsd_file_get(f->fi_deleg_file);
636 spin_unlock(&f->fi_lock);
637 return ret;
638}
639
Trond Myklebust02a35082014-07-25 07:34:22 -0400640static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800641unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700642
643/*
644 * Open owner state (share locks)
645 */
646
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500647/* hash tables for lock and open owners */
648#define OWNER_HASH_BITS 8
649#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
650#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700651
Trond Myklebustd4f04892014-07-29 21:34:36 -0400652static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400653{
654 unsigned int ret;
655
656 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500657 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400658}
NeilBrownef0f3392006-04-10 22:55:41 -0700659
NeilBrownef0f3392006-04-10 22:55:41 -0700660/* hash table for nfs4_file */
661#define FILE_HASH_BITS 8
662#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800663
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -0400664static unsigned int file_hashval(struct svc_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400665{
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -0400666 struct inode *inode = d_inode(fh->fh_dentry);
Trond Myklebustca943212014-07-23 16:17:39 -0400667
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -0400668 /* XXX: why not (here & in file cache) use inode? */
669 return (unsigned int)hash_long(inode->i_ino, FILE_HASH_BITS);
Trond Myklebustca943212014-07-23 16:17:39 -0400670}
671
Jeff Layton89876f82013-04-02 09:01:59 -0400672static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700673
Jeff Layton12659652014-07-10 14:07:28 -0400674static void
675__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400676{
Jeff Layton7214e862014-07-10 14:07:33 -0400677 lockdep_assert_held(&fp->fi_lock);
678
Jeff Layton12659652014-07-10 14:07:28 -0400679 if (access & NFS4_SHARE_ACCESS_WRITE)
680 atomic_inc(&fp->fi_access[O_WRONLY]);
681 if (access & NFS4_SHARE_ACCESS_READ)
682 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400683}
684
Jeff Layton12659652014-07-10 14:07:28 -0400685static __be32
686nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400687{
Jeff Layton7214e862014-07-10 14:07:33 -0400688 lockdep_assert_held(&fp->fi_lock);
689
Jeff Layton12659652014-07-10 14:07:28 -0400690 /* Does this access mode make sense? */
691 if (access & ~NFS4_SHARE_ACCESS_BOTH)
692 return nfserr_inval;
693
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400694 /* Does it conflict with a deny mode already set? */
695 if ((access & fp->fi_share_deny) != 0)
696 return nfserr_share_denied;
697
Jeff Layton12659652014-07-10 14:07:28 -0400698 __nfs4_file_get_access(fp, access);
699 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400700}
701
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400702static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
703{
704 /* Common case is that there is no deny mode. */
705 if (deny) {
706 /* Does this deny mode make sense? */
707 if (deny & ~NFS4_SHARE_DENY_BOTH)
708 return nfserr_inval;
709
710 if ((deny & NFS4_SHARE_DENY_READ) &&
711 atomic_read(&fp->fi_access[O_RDONLY]))
712 return nfserr_share_denied;
713
714 if ((deny & NFS4_SHARE_DENY_WRITE) &&
715 atomic_read(&fp->fi_access[O_WRONLY]))
716 return nfserr_share_denied;
717 }
718 return nfs_ok;
719}
720
J. Bruce Fields998db522010-08-07 09:21:41 -0400721static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400722{
Trond Myklebustde186432014-07-10 14:07:26 -0400723 might_lock(&fp->fi_lock);
724
725 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400726 struct nfsd_file *f1 = NULL;
727 struct nfsd_file *f2 = NULL;
Trond Myklebustde186432014-07-10 14:07:26 -0400728
Jeff Layton6d338b52014-07-10 14:07:29 -0400729 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400730 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400731 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400732 spin_unlock(&fp->fi_lock);
733 if (f1)
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400734 nfsd_file_put(f1);
Trond Myklebustde186432014-07-10 14:07:26 -0400735 if (f2)
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400736 nfsd_file_put(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400737 }
738}
739
Jeff Layton12659652014-07-10 14:07:28 -0400740static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400741{
Jeff Layton12659652014-07-10 14:07:28 -0400742 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
743
744 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400745 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400746 if (access & NFS4_SHARE_ACCESS_READ)
747 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400748}
749
Sachin Bhamare8287f002015-04-27 14:50:14 +0200750/*
751 * Allocate a new open/delegation state counter. This is needed for
752 * pNFS for proper return on close semantics.
753 *
754 * Note that we only allocate it for pNFS-enabled exports, otherwise
755 * all pointers to struct nfs4_clnt_odstate are always NULL.
756 */
757static struct nfs4_clnt_odstate *
758alloc_clnt_odstate(struct nfs4_client *clp)
759{
760 struct nfs4_clnt_odstate *co;
761
762 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
763 if (co) {
764 co->co_client = clp;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300765 refcount_set(&co->co_odcount, 1);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200766 }
767 return co;
768}
769
770static void
771hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
772{
773 struct nfs4_file *fp = co->co_file;
774
775 lockdep_assert_held(&fp->fi_lock);
776 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
777}
778
779static inline void
780get_clnt_odstate(struct nfs4_clnt_odstate *co)
781{
782 if (co)
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300783 refcount_inc(&co->co_odcount);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200784}
785
786static void
787put_clnt_odstate(struct nfs4_clnt_odstate *co)
788{
789 struct nfs4_file *fp;
790
791 if (!co)
792 return;
793
794 fp = co->co_file;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300795 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
Sachin Bhamare8287f002015-04-27 14:50:14 +0200796 list_del(&co->co_perfile);
797 spin_unlock(&fp->fi_lock);
798
799 nfsd4_return_all_file_layouts(co->co_client, fp);
800 kmem_cache_free(odstate_slab, co);
801 }
802}
803
804static struct nfs4_clnt_odstate *
805find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
806{
807 struct nfs4_clnt_odstate *co;
808 struct nfs4_client *cl;
809
810 if (!new)
811 return NULL;
812
813 cl = new->co_client;
814
815 spin_lock(&fp->fi_lock);
816 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
817 if (co->co_client == cl) {
818 get_clnt_odstate(co);
819 goto out;
820 }
821 }
822 co = new;
823 co->co_file = fp;
824 hash_clnt_odstate_locked(new);
825out:
826 spin_unlock(&fp->fi_lock);
827 return co;
828}
829
Kinglong Meed19fb702017-01-18 19:04:42 +0800830struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
831 void (*sc_free)(struct nfs4_stid *))
J. Bruce Fields996e0932011-10-17 11:14:48 -0400832{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500833 struct nfs4_stid *stid;
834 int new_id;
835
Trond Myklebustf8338832014-07-25 07:34:19 -0400836 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500837 if (!stid)
838 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400839
Jeff Layton4770d722014-07-29 21:34:10 -0400840 idr_preload(GFP_KERNEL);
841 spin_lock(&cl->cl_lock);
J. Bruce Fields78599c42019-04-22 15:26:23 -0400842 /* Reserving 0 for start of file in nfsdfs "states" file: */
843 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
Jeff Layton4770d722014-07-29 21:34:10 -0400844 spin_unlock(&cl->cl_lock);
845 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700846 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500847 goto out_free;
Kinglong Meed19fb702017-01-18 19:04:42 +0800848
849 stid->sc_free = sc_free;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500850 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500851 stid->sc_stateid.si_opaque.so_id = new_id;
852 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
853 /* Will be incremented before return to client: */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300854 refcount_set(&stid->sc_count, 1);
Jeff Layton9767feb2015-10-01 09:05:50 -0400855 spin_lock_init(&stid->sc_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400856 INIT_LIST_HEAD(&stid->sc_cp_list);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500857
J. Bruce Fields996e0932011-10-17 11:14:48 -0400858 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500859 * It shouldn't be a problem to reuse an opaque stateid value.
860 * I don't think it is for 4.1. But with 4.0 I worry that, for
861 * example, a stray write retransmission could be accepted by
862 * the server when it should have been rejected. Therefore,
863 * adopt a trick from the sctp code to attempt to maximize the
864 * amount of time until an id is reused, by ensuring they always
865 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400866 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500867 return stid;
868out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800869 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500870 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400871}
872
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400873/*
874 * Create a unique stateid_t to represent each COPY.
875 */
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400876static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
877 unsigned char sc_type)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400878{
879 int new_id;
880
Arnd Bergmann9cc76802019-10-31 15:53:13 +0100881 stid->stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400882 stid->stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
883 stid->sc_type = sc_type;
884
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400885 idr_preload(GFP_KERNEL);
886 spin_lock(&nn->s2s_cp_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400887 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
888 stid->stid.si_opaque.so_id = new_id;
Dai Ngoca9364d2020-11-30 16:24:49 -0500889 stid->stid.si_generation = 1;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400890 spin_unlock(&nn->s2s_cp_lock);
891 idr_preload_end();
892 if (new_id < 0)
893 return 0;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400894 return 1;
895}
896
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400897int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
898{
899 return nfs4_init_cp_state(nn, &copy->cp_stateid, NFS4_COPY_STID);
900}
901
902struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
903 struct nfs4_stid *p_stid)
904{
905 struct nfs4_cpntf_state *cps;
906
907 cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
908 if (!cps)
909 return NULL;
Arnd Bergmann20b7d862019-11-04 16:31:52 +0100910 cps->cpntf_time = ktime_get_boottime_seconds();
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400911 refcount_set(&cps->cp_stateid.sc_count, 1);
912 if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
913 goto out_free;
914 spin_lock(&nn->s2s_cp_lock);
915 list_add(&cps->cp_list, &p_stid->sc_cp_list);
916 spin_unlock(&nn->s2s_cp_lock);
917 return cps;
918out_free:
919 kfree(cps);
920 return NULL;
921}
922
923void nfs4_free_copy_state(struct nfsd4_copy *copy)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400924{
925 struct nfsd_net *nn;
926
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400927 WARN_ON_ONCE(copy->cp_stateid.sc_type != NFS4_COPY_STID);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400928 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
929 spin_lock(&nn->s2s_cp_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400930 idr_remove(&nn->s2s_cp_stateids,
931 copy->cp_stateid.stid.si_opaque.so_id);
932 spin_unlock(&nn->s2s_cp_lock);
933}
934
935static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
936{
937 struct nfs4_cpntf_state *cps;
938 struct nfsd_net *nn;
939
940 nn = net_generic(net, nfsd_net_id);
941 spin_lock(&nn->s2s_cp_lock);
942 while (!list_empty(&stid->sc_cp_list)) {
943 cps = list_first_entry(&stid->sc_cp_list,
944 struct nfs4_cpntf_state, cp_list);
945 _free_cpntf_state_locked(nn, cps);
946 }
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400947 spin_unlock(&nn->s2s_cp_lock);
948}
949
Jeff Laytonb49e0842014-07-29 21:34:11 -0400950static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400951{
Trond Myklebust60116952014-07-29 21:34:06 -0400952 struct nfs4_stid *stid;
Trond Myklebust60116952014-07-29 21:34:06 -0400953
Kinglong Meed19fb702017-01-18 19:04:42 +0800954 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
Trond Myklebust60116952014-07-29 21:34:06 -0400955 if (!stid)
956 return NULL;
957
Kinglong Meed19fb702017-01-18 19:04:42 +0800958 return openlockstateid(stid);
Trond Myklebust60116952014-07-29 21:34:06 -0400959}
960
961static void nfs4_free_deleg(struct nfs4_stid *stid)
962{
Trond Myklebust60116952014-07-29 21:34:06 -0400963 kmem_cache_free(deleg_slab, stid);
964 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400965}
966
NeilBrown6282cd52014-06-04 17:39:26 +1000967/*
968 * When we recall a delegation, we should be careful not to hand it
969 * out again straight away.
970 * To ensure this we keep a pair of bloom filters ('new' and 'old')
971 * in which the filehandles of recalled delegations are "stored".
972 * If a filehandle appear in either filter, a delegation is blocked.
973 * When a delegation is recalled, the filehandle is stored in the "new"
974 * filter.
975 * Every 30 seconds we swap the filters and clear the "new" one,
976 * unless both are empty of course.
977 *
978 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
979 * low 3 bytes as hash-table indices.
980 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400981 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000982 * is used to manage concurrent access. Testing does not need the lock
983 * except when swapping the two filters.
984 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400985static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000986static struct bloom_pair {
987 int entries, old_entries;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200988 time64_t swap_time;
NeilBrown6282cd52014-06-04 17:39:26 +1000989 int new; /* index into 'set' */
990 DECLARE_BITMAP(set[2], 256);
991} blocked_delegations;
992
993static int delegation_blocked(struct knfsd_fh *fh)
994{
995 u32 hash;
996 struct bloom_pair *bd = &blocked_delegations;
997
998 if (bd->entries == 0)
999 return 0;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +02001000 if (ktime_get_seconds() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -04001001 spin_lock(&blocked_delegations_lock);
Arnd Bergmannb3f255e2017-10-20 11:25:34 +02001002 if (ktime_get_seconds() - bd->swap_time > 30) {
NeilBrown6282cd52014-06-04 17:39:26 +10001003 bd->entries -= bd->old_entries;
1004 bd->old_entries = bd->entries;
1005 memset(bd->set[bd->new], 0,
1006 sizeof(bd->set[0]));
1007 bd->new = 1-bd->new;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +02001008 bd->swap_time = ktime_get_seconds();
NeilBrown6282cd52014-06-04 17:39:26 +10001009 }
Jeff Laytonf54fe962014-07-25 07:34:26 -04001010 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +10001011 }
Daniel Borkmann87545892014-12-10 16:33:11 +01001012 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +10001013 if (test_bit(hash&255, bd->set[0]) &&
1014 test_bit((hash>>8)&255, bd->set[0]) &&
1015 test_bit((hash>>16)&255, bd->set[0]))
1016 return 1;
1017
1018 if (test_bit(hash&255, bd->set[1]) &&
1019 test_bit((hash>>8)&255, bd->set[1]) &&
1020 test_bit((hash>>16)&255, bd->set[1]))
1021 return 1;
1022
1023 return 0;
1024}
1025
1026static void block_delegations(struct knfsd_fh *fh)
1027{
1028 u32 hash;
1029 struct bloom_pair *bd = &blocked_delegations;
1030
Daniel Borkmann87545892014-12-10 16:33:11 +01001031 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +10001032
Jeff Laytonf54fe962014-07-25 07:34:26 -04001033 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +10001034 __set_bit(hash&255, bd->set[bd->new]);
1035 __set_bit((hash>>8)&255, bd->set[bd->new]);
1036 __set_bit((hash>>16)&255, bd->set[bd->new]);
1037 if (bd->entries == 0)
Arnd Bergmannb3f255e2017-10-20 11:25:34 +02001038 bd->swap_time = ktime_get_seconds();
NeilBrown6282cd52014-06-04 17:39:26 +10001039 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -04001040 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +10001041}
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043static struct nfs4_delegation *
J. Bruce Fields86d29b12018-02-21 15:27:28 -05001044alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
1045 struct svc_fh *current_fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02001046 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
1048 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -04001049 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -04001052 n = atomic_long_inc_return(&num_delegations);
1053 if (n < 0 || n > max_delegations)
1054 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +10001055 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -04001056 goto out_dec;
Kinglong Meed19fb702017-01-18 19:04:42 +08001057 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
NeilBrown5b2d21c2005-06-23 22:03:04 -07001058 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -04001059 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -04001060
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04001061 /*
1062 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001063 * meaning of seqid 0 isn't meaningful, really, but let's avoid
1064 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04001065 */
1066 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -07001067 INIT_LIST_HEAD(&dp->dl_perfile);
1068 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +02001070 dp->dl_clnt_odstate = odstate;
1071 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -04001072 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02001073 dp->dl_retries = 1;
1074 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +02001075 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
J. Bruce Fields86d29b12018-02-21 15:27:28 -05001076 get_nfs4_file(fp);
1077 dp->dl_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -04001079out_dec:
1080 atomic_long_dec(&num_delegations);
1081 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
1084void
Trond Myklebust60116952014-07-29 21:34:06 -04001085nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Trond Myklebust11b91642014-07-29 21:34:08 -04001087 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -04001088 struct nfs4_client *clp = s->sc_client;
1089
Jeff Layton4770d722014-07-29 21:34:10 -04001090 might_lock(&clp->cl_lock);
1091
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001092 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
Jeff Laytonb401be222014-07-29 21:34:33 -04001093 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -04001094 return;
Jeff Laytonb401be222014-07-29 21:34:33 -04001095 }
Trond Myklebust60116952014-07-29 21:34:06 -04001096 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001097 nfs4_free_cpntf_statelist(clp->net, s);
Jeff Layton4770d722014-07-29 21:34:10 -04001098 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04001099 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -04001100 if (fp)
1101 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
Jeff Layton9767feb2015-10-01 09:05:50 -04001104void
1105nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
1106{
1107 stateid_t *src = &stid->sc_stateid;
1108
1109 spin_lock(&stid->sc_lock);
1110 if (unlikely(++src->si_generation == 0))
1111 src->si_generation = 1;
1112 memcpy(dst, src, sizeof(*dst));
1113 spin_unlock(&stid->sc_lock);
1114}
1115
J. Bruce Fields353601e2018-02-16 14:29:42 -05001116static void put_deleg_file(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001118 struct nfsd_file *nf = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -05001119
1120 spin_lock(&fp->fi_lock);
1121 if (--fp->fi_delegees == 0)
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001122 swap(nf, fp->fi_deleg_file);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001123 spin_unlock(&fp->fi_lock);
1124
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001125 if (nf)
1126 nfsd_file_put(nf);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001127}
1128
1129static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1130{
J. Bruce Fieldscba7b3d2018-02-15 15:50:49 -05001131 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001132 struct nfsd_file *nf = fp->fi_deleg_file;
Jeff Layton417c6622014-07-21 09:34:57 -04001133
J. Bruce Fieldsb8232d32018-02-15 15:29:15 -05001134 WARN_ON_ONCE(!fp->fi_delegees);
1135
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001136 vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001137 put_deleg_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
1139
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001140static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1141{
1142 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001143 nfs4_unlock_deleg_lease(dp);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001144 nfs4_put_stid(&dp->dl_stid);
1145}
1146
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001147void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001148{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001149 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001150}
1151
Andrew Elble34ed9872015-10-15 12:07:28 -04001152/**
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001153 * nfs4_delegation_exists - Discover if this delegation already exists
Andrew Elble34ed9872015-10-15 12:07:28 -04001154 * @clp: a pointer to the nfs4_client we're granting a delegation to
1155 * @fp: a pointer to the nfs4_file we're granting a delegation on
1156 *
1157 * Return:
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001158 * On success: true iff an existing delegation is found
Andrew Elble34ed9872015-10-15 12:07:28 -04001159 */
1160
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001161static bool
1162nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
Benny Halevy931ee562014-05-30 09:09:27 -04001163{
Andrew Elble34ed9872015-10-15 12:07:28 -04001164 struct nfs4_delegation *searchdp = NULL;
1165 struct nfs4_client *searchclp = NULL;
1166
Benny Halevycdc975052014-05-30 09:09:30 -04001167 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04001168 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001169
Andrew Elble34ed9872015-10-15 12:07:28 -04001170 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1171 searchclp = searchdp->dl_stid.sc_client;
1172 if (clp == searchclp) {
Fengguang Wu51d87bc2018-03-22 13:37:20 +08001173 return true;
Andrew Elble34ed9872015-10-15 12:07:28 -04001174 }
1175 }
Fengguang Wu51d87bc2018-03-22 13:37:20 +08001176 return false;
Andrew Elble34ed9872015-10-15 12:07:28 -04001177}
1178
1179/**
1180 * hash_delegation_locked - Add a delegation to the appropriate lists
1181 * @dp: a pointer to the nfs4_delegation we are adding.
1182 * @fp: a pointer to the nfs4_file we're granting a delegation on
1183 *
1184 * Return:
1185 * On success: NULL if the delegation was successfully hashed.
1186 *
1187 * On error: -EAGAIN if one was previously granted to this
1188 * nfs4_client for this nfs4_file. Delegation is not hashed.
1189 *
1190 */
1191
1192static int
1193hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1194{
Andrew Elble34ed9872015-10-15 12:07:28 -04001195 struct nfs4_client *clp = dp->dl_stid.sc_client;
1196
1197 lockdep_assert_held(&state_lock);
1198 lockdep_assert_held(&fp->fi_lock);
1199
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001200 if (nfs4_delegation_exists(clp, fp))
1201 return -EAGAIN;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001202 refcount_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -04001203 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -04001204 list_add(&dp->dl_perfile, &fp->fi_delegations);
Andrew Elble34ed9872015-10-15 12:07:28 -04001205 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1206 return 0;
Benny Halevy931ee562014-05-30 09:09:27 -04001207}
1208
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001209static bool
Jeff Layton42690672014-07-25 07:34:20 -04001210unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Trond Myklebust11b91642014-07-29 21:34:08 -04001212 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -04001213
Jeff Layton42690672014-07-25 07:34:20 -04001214 lockdep_assert_held(&state_lock);
1215
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001216 if (list_empty(&dp->dl_perfile))
1217 return false;
1218
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04001219 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -04001220 /* Ensure that deleg break won't try to requeue it */
1221 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -04001222 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001223 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -04001225 list_del_init(&dp->dl_perfile);
1226 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001227 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001228}
1229
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001230static void destroy_delegation(struct nfs4_delegation *dp)
1231{
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001232 bool unhashed;
1233
Jeff Layton42690672014-07-25 07:34:20 -04001234 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001235 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -04001236 spin_unlock(&state_lock);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001237 if (unhashed)
1238 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001239}
1240
1241static void revoke_delegation(struct nfs4_delegation *dp)
1242{
1243 struct nfs4_client *clp = dp->dl_stid.sc_client;
1244
Jeff Layton2d4a5322014-07-25 07:34:21 -04001245 WARN_ON(!list_empty(&dp->dl_recall_lru));
1246
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001247 if (clp->cl_minorversion) {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001248 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001249 refcount_inc(&dp->dl_stid.sc_count);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001250 spin_lock(&clp->cl_lock);
1251 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1252 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001253 }
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001254 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001255}
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257/*
1258 * SETCLIENTID state
1259 */
1260
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001261static unsigned int clientid_hashval(u32 id)
1262{
1263 return id & CLIENT_HASH_MASK;
1264}
1265
Scott Mayhew6b189102019-03-26 18:06:26 -04001266static unsigned int clientstr_hashval(struct xdr_netobj name)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001267{
Scott Mayhew6b189102019-03-26 18:06:26 -04001268 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001269}
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271/*
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001272 * A stateid that had a deny mode associated with it is being released
1273 * or downgraded. Recalculate the deny mode on the file.
1274 */
1275static void
1276recalculate_deny_mode(struct nfs4_file *fp)
1277{
1278 struct nfs4_ol_stateid *stp;
1279
1280 spin_lock(&fp->fi_lock);
1281 fp->fi_share_deny = 0;
1282 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1283 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1284 spin_unlock(&fp->fi_lock);
1285}
1286
1287static void
1288reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1289{
1290 int i;
1291 bool change = false;
1292
1293 for (i = 1; i < 4; i++) {
1294 if ((i & deny) != i) {
1295 change = true;
1296 clear_deny(i, stp);
1297 }
1298 }
1299
1300 /* Recalculate per-file deny mode if there was a change */
1301 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -04001302 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001303}
1304
Jeff Layton82c5ff12012-05-11 09:45:13 -04001305/* release all access and file references for a given stateid */
1306static void
1307release_all_access(struct nfs4_ol_stateid *stp)
1308{
1309 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -04001310 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001311
1312 if (fp && stp->st_deny_bmap != 0)
1313 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001314
1315 for (i = 1; i < 4; i++) {
1316 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -04001317 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001318 clear_access(i, stp);
1319 }
1320}
1321
Kinglong Meed50ffde2015-07-16 12:05:07 +08001322static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1323{
1324 kfree(sop->so_owner.data);
1325 sop->so_ops->so_free(sop);
1326}
1327
Jeff Layton6b180f02014-07-29 21:34:26 -04001328static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1329{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001330 struct nfs4_client *clp = sop->so_client;
1331
1332 might_lock(&clp->cl_lock);
1333
1334 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001335 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001336 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001337 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001338 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001339}
1340
Trond Myklebusta451b122020-03-01 18:21:38 -05001341static bool
1342nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1343{
1344 return list_empty(&stp->st_perfile);
1345}
1346
Jeff Laytone8568732015-08-24 12:41:47 -04001347static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001348{
Trond Myklebust11b91642014-07-29 21:34:08 -04001349 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001350
Jeff Layton1c755dc2014-07-29 21:34:12 -04001351 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1352
Jeff Laytone8568732015-08-24 12:41:47 -04001353 if (list_empty(&stp->st_perfile))
1354 return false;
1355
Trond Myklebust1d31a252014-07-10 14:07:25 -04001356 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001357 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001358 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001359 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001360 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001361}
1362
Trond Myklebust60116952014-07-29 21:34:06 -04001363static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001364{
Trond Myklebust60116952014-07-29 21:34:06 -04001365 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001366
Sachin Bhamare8287f002015-04-27 14:50:14 +02001367 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001368 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001369 if (stp->st_stateowner)
1370 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -04001371 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001372}
1373
Jeff Laytonb49e0842014-07-29 21:34:11 -04001374static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001375{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001376 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1377 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001378 struct nfsd_file *nf;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001379
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001380 nf = find_any_file(stp->st_stid.sc_file);
1381 if (nf) {
1382 get_file(nf->nf_file);
1383 filp_close(nf->nf_file, (fl_owner_t)lo);
1384 nfsd_file_put(nf);
1385 }
Jeff Laytonb49e0842014-07-29 21:34:11 -04001386 nfs4_free_ol_stateid(stid);
1387}
1388
Jeff Layton2c41beb2014-07-29 21:34:41 -04001389/*
1390 * Put the persistent reference to an already unhashed generic stateid, while
1391 * holding the cl_lock. If it's the last reference, then put it onto the
1392 * reaplist for later destruction.
1393 */
1394static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1395 struct list_head *reaplist)
1396{
1397 struct nfs4_stid *s = &stp->st_stid;
1398 struct nfs4_client *clp = s->sc_client;
1399
1400 lockdep_assert_held(&clp->cl_lock);
1401
1402 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1403
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001404 if (!refcount_dec_and_test(&s->sc_count)) {
Jeff Layton2c41beb2014-07-29 21:34:41 -04001405 wake_up_all(&close_wq);
1406 return;
1407 }
1408
1409 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1410 list_add(&stp->st_locks, reaplist);
1411}
1412
Jeff Laytone8568732015-08-24 12:41:47 -04001413static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001414{
Chuck Leverf46c4452016-10-29 18:19:03 -04001415 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001416
Trond Myklebusta451b122020-03-01 18:21:38 -05001417 if (!unhash_ol_stateid(stp))
1418 return false;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001419 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001420 nfs4_unhash_stid(&stp->st_stid);
Trond Myklebusta451b122020-03-01 18:21:38 -05001421 return true;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001422}
1423
Jeff Layton5adfd882014-07-29 21:34:31 -04001424static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001425{
Chuck Leverf46c4452016-10-29 18:19:03 -04001426 struct nfs4_client *clp = stp->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04001427 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001428
Chuck Leverf46c4452016-10-29 18:19:03 -04001429 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001430 unhashed = unhash_lock_stateid(stp);
Chuck Leverf46c4452016-10-29 18:19:03 -04001431 spin_unlock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001432 if (unhashed)
1433 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001434}
1435
Trond Myklebustc58c6612014-07-29 21:34:35 -04001436static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001437{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001438 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001439
Trond Myklebustd4f04892014-07-29 21:34:36 -04001440 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001441
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001442 list_del_init(&lo->lo_owner.so_strhash);
1443}
1444
Jeff Layton2c41beb2014-07-29 21:34:41 -04001445/*
1446 * Free a list of generic stateids that were collected earlier after being
1447 * fully unhashed.
1448 */
1449static void
1450free_ol_stateid_reaplist(struct list_head *reaplist)
1451{
1452 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001453 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001454
1455 might_sleep();
1456
1457 while (!list_empty(reaplist)) {
1458 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1459 st_locks);
1460 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001461 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001462 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001463 if (fp)
1464 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001465 }
1466}
1467
Jeff Laytond83017f2014-07-29 21:34:42 -04001468static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1469 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001470{
1471 struct nfs4_ol_stateid *stp;
1472
Jeff Laytone8568732015-08-24 12:41:47 -04001473 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1474
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001475 while (!list_empty(&open_stp->st_locks)) {
1476 stp = list_entry(open_stp->st_locks.next,
1477 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001478 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001479 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001480 }
1481}
1482
Jeff Laytone8568732015-08-24 12:41:47 -04001483static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001484 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001485{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001486 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1487
Trond Myklebusta451b122020-03-01 18:21:38 -05001488 if (!unhash_ol_stateid(stp))
1489 return false;
Jeff Laytond83017f2014-07-29 21:34:42 -04001490 release_open_stateid_locks(stp, reaplist);
Trond Myklebusta451b122020-03-01 18:21:38 -05001491 return true;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001492}
1493
1494static void release_open_stateid(struct nfs4_ol_stateid *stp)
1495{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001496 LIST_HEAD(reaplist);
1497
1498 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001499 if (unhash_open_stateid(stp, &reaplist))
1500 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001501 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1502 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001503}
1504
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001505static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001506{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001507 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001508
Trond Myklebustd4f04892014-07-29 21:34:36 -04001509 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001510
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001511 list_del_init(&oo->oo_owner.so_strhash);
1512 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001513}
1514
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001515static void release_last_closed_stateid(struct nfs4_openowner *oo)
1516{
Jeff Layton217526e2014-07-30 08:27:11 -04001517 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1518 nfsd_net_id);
1519 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001520
Jeff Layton217526e2014-07-30 08:27:11 -04001521 spin_lock(&nn->client_lock);
1522 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001523 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001524 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001525 oo->oo_last_closed_stid = NULL;
1526 }
Jeff Layton217526e2014-07-30 08:27:11 -04001527 spin_unlock(&nn->client_lock);
1528 if (s)
1529 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001530}
1531
Jeff Layton2c41beb2014-07-29 21:34:41 -04001532static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001533{
1534 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001535 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001536 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001537
Jeff Layton2c41beb2014-07-29 21:34:41 -04001538 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001539
Trond Myklebustd4f04892014-07-29 21:34:36 -04001540 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001541 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001542 while (!list_empty(&oo->oo_owner.so_stateids)) {
1543 stp = list_first_entry(&oo->oo_owner.so_stateids,
1544 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001545 if (unhash_open_stateid(stp, &reaplist))
1546 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001547 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001548 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001549 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001550 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001551 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001552}
1553
Marc Eshel5282fd72009-04-03 08:27:52 +03001554static inline int
1555hash_sessionid(struct nfs4_sessionid *sessionid)
1556{
1557 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1558
1559 return sid->sequence % SESSION_HASH_SIZE;
1560}
1561
Mark Salter135dd002015-04-06 09:46:00 -04001562#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001563static inline void
1564dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1565{
1566 u32 *ptr = (u32 *)(&sessionid->data[0]);
1567 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1568}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001569#else
1570static inline void
1571dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1572{
1573}
1574#endif
1575
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001576/*
1577 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1578 * won't be used for replay.
1579 */
1580void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1581{
1582 struct nfs4_stateowner *so = cstate->replay_owner;
1583
1584 if (nfserr == nfserr_replay_me)
1585 return;
1586
1587 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001588 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001589 return;
1590 }
1591 if (!so)
1592 return;
1593 if (so->so_is_open_owner)
1594 release_last_closed_stateid(openowner(so));
1595 so->so_seqid++;
1596 return;
1597}
Marc Eshel5282fd72009-04-03 08:27:52 +03001598
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001599static void
1600gen_sessionid(struct nfsd4_session *ses)
1601{
1602 struct nfs4_client *clp = ses->se_client;
1603 struct nfsd4_sessionid *sid;
1604
1605 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1606 sid->clientid = clp->cl_clientid;
1607 sid->sequence = current_sessionid++;
1608 sid->reserved = 0;
1609}
1610
1611/*
Andy Adamsona649637c72009-08-28 08:45:01 -04001612 * The protocol defines ca_maxresponssize_cached to include the size of
1613 * the rpc header, but all we need to cache is the data starting after
1614 * the end of the initial SEQUENCE operation--the rest we regenerate
1615 * each time. Therefore we can advertise a ca_maxresponssize_cached
1616 * value that is the number of bytes in our cache plus a few additional
1617 * bytes. In order to stay on the safe side, and not promise more than
1618 * we can cache, those additional bytes must be the minimum possible: 24
1619 * bytes of rpc header (xid through accept state, with AUTH_NULL
1620 * verifier), 12 for the compound header (with zero-length tag), and 44
1621 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001622 */
Andy Adamsona649637c72009-08-28 08:45:01 -04001623#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1624
Andy Adamson557ce262009-08-28 08:45:04 -04001625static void
1626free_session_slots(struct nfsd4_session *ses)
1627{
1628 int i;
1629
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001630 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1631 free_svc_cred(&ses->se_slots[i]->sl_cred);
Andy Adamson557ce262009-08-28 08:45:04 -04001632 kfree(ses->se_slots[i]);
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001633 }
Andy Adamson557ce262009-08-28 08:45:04 -04001634}
1635
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001636/*
1637 * We don't actually need to cache the rpc and session headers, so we
1638 * can allocate a little less for each slot:
1639 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001640static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001641{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001642 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001643
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001644 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1645 size = 0;
1646 else
1647 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1648 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001649}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001650
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001651/*
1652 * XXX: If we run out of reserved DRC memory we could (up to a point)
1653 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001654 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001655 */
NeilBrown2030ca52019-09-20 16:36:45 +10001656static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001657{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001658 u32 slotsize = slot_bytes(ca);
1659 u32 num = ca->maxreqs;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001660 unsigned long avail, total_avail;
NeilBrown2030ca52019-09-20 16:36:45 +10001661 unsigned int scale_factor;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001662
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001663 spin_lock(&nfsd_drc_lock);
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001664 if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1665 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1666 else
1667 /* We have handed out more space than we chose in
1668 * set_max_drc() to allow. That isn't really a
1669 * problem as long as that doesn't make us think we
1670 * have lots more due to integer overflow.
1671 */
1672 total_avail = 0;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001673 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001674 /*
NeilBrown2030ca52019-09-20 16:36:45 +10001675 * Never use more than a fraction of the remaining memory,
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001676 * unless it's the only way to give this client a slot.
NeilBrown2030ca52019-09-20 16:36:45 +10001677 * The chosen fraction is either 1/8 or 1/number of threads,
1678 * whichever is smaller. This ensures there are adequate
1679 * slots to support multiple clients per thread.
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001680 * Give the client one slot even if that would require
1681 * over-allocation--it is better than failure.
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001682 */
NeilBrown2030ca52019-09-20 16:36:45 +10001683 scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1684
1685 avail = clamp_t(unsigned long, avail, slotsize,
1686 total_avail/scale_factor);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001687 num = min_t(int, num, avail / slotsize);
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001688 num = max_t(int, num, 1);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001689 nfsd_drc_mem_used += num * slotsize;
1690 spin_unlock(&nfsd_drc_lock);
1691
1692 return num;
1693}
1694
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001695static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001696{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001697 int slotsize = slot_bytes(ca);
1698
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001699 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001700 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001701 spin_unlock(&nfsd_drc_lock);
1702}
1703
Kinglong Mee60810e52014-01-01 00:35:47 +08001704static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1705 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001706{
Kinglong Mee60810e52014-01-01 00:35:47 +08001707 int numslots = fattrs->maxreqs;
1708 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001709 struct nfsd4_session *new;
1710 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001711
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001712 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001713 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1714 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001715
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001716 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001717 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001718 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001719 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001720 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001721 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001722 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001723 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001724 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001725
1726 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1727 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1728
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001729 return new;
1730out_free:
1731 while (i--)
1732 kfree(new->se_slots[i]);
1733 kfree(new);
1734 return NULL;
1735}
1736
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001737static void free_conn(struct nfsd4_conn *c)
1738{
1739 svc_xprt_put(c->cn_xprt);
1740 kfree(c);
1741}
1742
1743static void nfsd4_conn_lost(struct svc_xpt_user *u)
1744{
1745 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1746 struct nfs4_client *clp = c->cn_session->se_client;
1747
1748 spin_lock(&clp->cl_lock);
1749 if (!list_empty(&c->cn_persession)) {
1750 list_del(&c->cn_persession);
1751 free_conn(c);
1752 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001753 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001754 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001755}
1756
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001757static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001758{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001759 struct nfsd4_conn *conn;
1760
1761 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1762 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001763 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001764 svc_xprt_get(rqstp->rq_xprt);
1765 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001766 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001767 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1768 return conn;
1769}
1770
J. Bruce Fields328ead22010-09-29 16:11:06 -04001771static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1772{
1773 conn->cn_session = ses;
1774 list_add(&conn->cn_persession, &ses->se_conns);
1775}
1776
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001777static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1778{
1779 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001780
1781 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001782 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001783 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001784}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001785
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001786static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001787{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001788 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001789 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001790}
1791
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001792static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001793{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001794 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001795
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001796 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001797 ret = nfsd4_register_conn(conn);
1798 if (ret)
1799 /* oops; xprt is already down: */
1800 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001801 /* We may have gained or lost a callback channel: */
1802 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001803}
1804
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001805static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001806{
1807 u32 dir = NFS4_CDFC4_FORE;
1808
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001809 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001810 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001811 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001812}
1813
1814/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001815static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001816{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001817 struct nfs4_client *clp = s->se_client;
1818 struct nfsd4_conn *c;
1819
1820 spin_lock(&clp->cl_lock);
1821 while (!list_empty(&s->se_conns)) {
1822 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1823 list_del_init(&c->cn_persession);
1824 spin_unlock(&clp->cl_lock);
1825
1826 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1827 free_conn(c);
1828
1829 spin_lock(&clp->cl_lock);
1830 }
1831 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001832}
1833
J. Bruce Fields1377b692012-09-11 21:42:40 -04001834static void __free_session(struct nfsd4_session *ses)
1835{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001836 free_session_slots(ses);
1837 kfree(ses);
1838}
1839
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001840static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001841{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001842 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001843 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001844 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001845}
Andy Adamson557ce262009-08-28 08:45:04 -04001846
Fengguang Wu135ae822012-11-10 07:20:25 -05001847static 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 -04001848{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001849 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001850 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001851
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001852 new->se_client = clp;
1853 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001854
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001855 INIT_LIST_HEAD(&new->se_conns);
1856
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001857 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001858 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001859 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001860 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001861 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001862 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001863 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001864 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001865 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001866 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001867
Chuck Leverb0d2e422014-08-22 15:10:59 -04001868 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001869 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001870 /*
1871 * This is a little silly; with sessions there's no real
1872 * use for the callback address. Use the peer address
1873 * as a reasonable default for now, but consider fixing
1874 * the rpc client not to require an address in the
1875 * future:
1876 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001877 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1878 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001879 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001880}
1881
Benny Halevy9089f1b2010-05-12 00:12:26 +03001882/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001883static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001884__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001885{
1886 struct nfsd4_session *elem;
1887 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001888 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001889
Trond Myklebust0a880a22014-07-30 08:27:10 -04001890 lockdep_assert_held(&nn->client_lock);
1891
Marc Eshel5282fd72009-04-03 08:27:52 +03001892 dump_sessionid(__func__, sessionid);
1893 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001894 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001895 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001896 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1897 NFS4_MAX_SESSIONID_LEN)) {
1898 return elem;
1899 }
1900 }
1901
1902 dprintk("%s: session not found\n", __func__);
1903 return NULL;
1904}
1905
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001906static struct nfsd4_session *
1907find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1908 __be32 *ret)
1909{
1910 struct nfsd4_session *session;
1911 __be32 status = nfserr_badsession;
1912
1913 session = __find_in_sessionid_hashtbl(sessionid, net);
1914 if (!session)
1915 goto out;
1916 status = nfsd4_get_session_locked(session);
1917 if (status)
1918 session = NULL;
1919out:
1920 *ret = status;
1921 return session;
1922}
1923
Benny Halevy9089f1b2010-05-12 00:12:26 +03001924/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001925static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001926unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001927{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001928 struct nfs4_client *clp = ses->se_client;
1929 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1930
1931 lockdep_assert_held(&nn->client_lock);
1932
Andy Adamson7116ed62009-04-03 08:27:43 +03001933 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001934 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001935 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001936 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001937}
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1940static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001941STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001943 /*
1944 * We're assuming the clid was not given out from a boot
1945 * precisely 2^32 (about 136 years) before this one. That seems
1946 * a safe assumption:
1947 */
1948 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 return 0;
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04001950 trace_nfsd_clid_stale(clid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 return 1;
1952}
1953
1954/*
1955 * XXX Should we use a slab cache ?
1956 * This type of memory management is somewhat inefficient, but we use it
1957 * anyway since SETCLIENTID is not a common operation.
1958 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001959static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
1961 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001962 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Jeff Layton9258a2d2018-03-16 09:47:22 -04001964 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001965 if (clp == NULL)
1966 return NULL;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04001967 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001968 if (clp->cl_name.data == NULL)
1969 goto err_no_name;
Kees Cook6da2ec52018-06-12 13:55:00 -07001970 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1971 sizeof(struct list_head),
1972 GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001973 if (!clp->cl_ownerstr_hashtbl)
1974 goto err_no_hashtbl;
1975 for (i = 0; i < OWNER_HASH_SIZE; i++)
1976 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
Trond Myklebust5694c932014-04-18 14:43:56 -04001977 INIT_LIST_HEAD(&clp->cl_sessions);
1978 idr_init(&clp->cl_stateids);
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04001979 atomic_set(&clp->cl_rpc_users, 0);
Trond Myklebust5694c932014-04-18 14:43:56 -04001980 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1981 INIT_LIST_HEAD(&clp->cl_idhash);
1982 INIT_LIST_HEAD(&clp->cl_openowners);
1983 INIT_LIST_HEAD(&clp->cl_delegations);
1984 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04001985 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001986#ifdef CONFIG_NFSD_PNFS
1987 INIT_LIST_HEAD(&clp->cl_lo_states);
1988#endif
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001989 INIT_LIST_HEAD(&clp->async_copies);
1990 spin_lock_init(&clp->async_lock);
Trond Myklebust5694c932014-04-18 14:43:56 -04001991 spin_lock_init(&clp->cl_lock);
1992 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001994err_no_hashtbl:
1995 kfree(clp->cl_name.data);
1996err_no_name:
Jeff Layton9258a2d2018-03-16 09:47:22 -04001997 kmem_cache_free(client_slab, clp);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001998 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
2000
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002001static void __free_client(struct kref *k)
2002{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002003 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
2004 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002005
2006 free_svc_cred(&clp->cl_cred);
2007 kfree(clp->cl_ownerstr_hashtbl);
2008 kfree(clp->cl_name.data);
J. Bruce Fields79123442019-06-05 12:42:05 -04002009 kfree(clp->cl_nii_domain.data);
2010 kfree(clp->cl_nii_name.data);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002011 idr_destroy(&clp->cl_stateids);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002012 kmem_cache_free(client_slab, clp);
2013}
2014
YueHaibing297e57a2019-07-08 15:29:33 +08002015static void drop_client(struct nfs4_client *clp)
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002016{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002017 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002018}
2019
Trond Myklebust4dd86e152014-04-18 14:43:58 -04002020static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021free_client(struct nfs4_client *clp)
2022{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002023 while (!list_empty(&clp->cl_sessions)) {
2024 struct nfsd4_session *ses;
2025 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2026 se_perclnt);
2027 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002028 WARN_ON_ONCE(atomic_read(&ses->se_ref));
2029 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002030 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04002031 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002032 if (clp->cl_nfsd_dentry) {
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002033 nfsd_client_rmdir(clp->cl_nfsd_dentry);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002034 clp->cl_nfsd_dentry = NULL;
2035 wake_up_all(&expiry_wq);
2036 }
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002037 drop_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
2039
Benny Halevy84d38ac2010-05-12 00:13:16 +03002040/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04002041static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03002042unhash_client_locked(struct nfs4_client *clp)
2043{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002044 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002045 struct nfsd4_session *ses;
2046
Trond Myklebust0a880a22014-07-30 08:27:10 -04002047 lockdep_assert_held(&nn->client_lock);
2048
Trond Myklebust4beb3452014-07-30 08:27:02 -04002049 /* Mark the client as expired! */
2050 clp->cl_time = 0;
2051 /* Make it invisible */
2052 if (!list_empty(&clp->cl_idhash)) {
2053 list_del_init(&clp->cl_idhash);
2054 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2055 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2056 else
2057 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2058 }
2059 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04002060 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002061 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2062 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04002063 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03002064}
2065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04002067unhash_client(struct nfs4_client *clp)
2068{
2069 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2070
2071 spin_lock(&nn->client_lock);
2072 unhash_client_locked(clp);
2073 spin_unlock(&nn->client_lock);
2074}
2075
Jeff Layton97403d92014-07-30 08:27:12 -04002076static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2077{
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04002078 if (atomic_read(&clp->cl_rpc_users))
Jeff Layton97403d92014-07-30 08:27:12 -04002079 return nfserr_jukebox;
2080 unhash_client_locked(clp);
2081 return nfs_ok;
2082}
2083
Trond Myklebust4beb3452014-07-30 08:27:02 -04002084static void
2085__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
Jeff Layton68ef3bc2018-03-16 11:32:02 -04002087 int i;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002088 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 struct list_head reaplist;
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04002093 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07002094 while (!list_empty(&clp->cl_delegations)) {
2095 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04002096 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04002097 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
Benny Halevycdc975052014-05-30 09:09:30 -04002099 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 while (!list_empty(&reaplist)) {
2101 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04002102 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05002103 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04002105 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05002106 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04002107 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04002108 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02002109 }
NeilBrownea1da632005-06-23 22:04:17 -07002110 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002111 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04002112 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002113 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 }
Jeff Layton68ef3bc2018-03-16 11:32:02 -04002115 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2116 struct nfs4_stateowner *so, *tmp;
2117
2118 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2119 so_strhash) {
2120 /* Should be no openowners at this point */
2121 WARN_ON_ONCE(so->so_is_open_owner);
2122 remove_blocked_locks(lockowner(so));
2123 }
2124 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002125 nfsd4_return_all_client_layouts(clp);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002126 nfsd4_shutdown_copy(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04002127 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002128 if (clp->cl_cb_conn.cb_xprt)
2129 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002130 free_client(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002131 wake_up_all(&expiry_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132}
2133
Trond Myklebust4beb3452014-07-30 08:27:02 -04002134static void
2135destroy_client(struct nfs4_client *clp)
2136{
2137 unhash_client(clp);
2138 __destroy_client(clp);
2139}
2140
Scott Mayhew362063a2019-03-26 18:06:28 -04002141static void inc_reclaim_complete(struct nfs4_client *clp)
2142{
2143 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2144
2145 if (!nn->track_reclaim_completes)
2146 return;
2147 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2148 return;
2149 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2150 nn->reclaim_str_hashtbl_size) {
2151 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2152 clp->net->ns.inum);
2153 nfsd4_end_grace(nn);
2154 }
2155}
2156
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002157static void expire_client(struct nfs4_client *clp)
2158{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002159 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002160 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04002161 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002162}
2163
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002164static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2165{
2166 memcpy(target->cl_verifier.data, source->data,
2167 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168}
2169
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002170static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2171{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2173 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2174}
2175
J. Bruce Fields50043852015-11-20 15:58:37 -05002176static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2177{
NeilBrown2f10fdc2017-03-23 16:57:36 +08002178 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2179 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2180 GFP_KERNEL);
Chuck Lever9abdda52018-08-16 12:05:59 -04002181 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2182 if ((source->cr_principal && !target->cr_principal) ||
2183 (source->cr_raw_principal && !target->cr_raw_principal) ||
2184 (source->cr_targ_princ && !target->cr_targ_princ))
NeilBrown2f10fdc2017-03-23 16:57:36 +08002185 return -ENOMEM;
J. Bruce Fields50043852015-11-20 15:58:37 -05002186
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04002187 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 target->cr_uid = source->cr_uid;
2189 target->cr_gid = source->cr_gid;
2190 target->cr_group_info = source->cr_group_info;
2191 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04002192 target->cr_gss_mech = source->cr_gss_mech;
2193 if (source->cr_gss_mech)
2194 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002195 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196}
2197
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002198static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002199compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2200{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002201 if (o1->len < o2->len)
2202 return -1;
2203 if (o1->len > o2->len)
2204 return 1;
2205 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002206}
2207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002209same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2210{
2211 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212}
2213
2214static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002215same_clid(clientid_t *cl1, clientid_t *cl2)
2216{
2217 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218}
2219
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002220static bool groups_equal(struct group_info *g1, struct group_info *g2)
2221{
2222 int i;
2223
2224 if (g1->ngroups != g2->ngroups)
2225 return false;
2226 for (i=0; i<g1->ngroups; i++)
Alexey Dobriyan81243ea2016-10-07 17:03:12 -07002227 if (!gid_eq(g1->gid[i], g2->gid[i]))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002228 return false;
2229 return true;
2230}
2231
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002232/*
2233 * RFC 3530 language requires clid_inuse be returned when the
2234 * "principal" associated with a requests differs from that previously
2235 * used. We use uid, gid's, and gss principal string as our best
2236 * approximation. We also don't want to allow non-gss use of a client
2237 * established using gss: in theory cr_principal should catch that
2238 * change, but in practice cr_principal can be null even in the gss case
2239 * since gssd doesn't always pass down a principal string.
2240 */
2241static bool is_gss_cred(struct svc_cred *cr)
2242{
2243 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2244 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2245}
2246
2247
Vivek Trivedi5559b502012-07-24 21:18:20 +05302248static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002249same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2250{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002251 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002252 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2253 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002254 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2255 return false;
Chuck Lever9abdda52018-08-16 12:05:59 -04002256 /* XXX: check that cr_targ_princ fields match ? */
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002257 if (cr1->cr_principal == cr2->cr_principal)
2258 return true;
2259 if (!cr1->cr_principal || !cr2->cr_principal)
2260 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05302261 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262}
2263
J. Bruce Fields57266a62013-04-13 14:27:29 -04002264static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2265{
2266 struct svc_cred *cr = &rqstp->rq_cred;
2267 u32 service;
2268
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04002269 if (!cr->cr_gss_mech)
2270 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002271 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2272 return service == RPC_GSS_SVC_INTEGRITY ||
2273 service == RPC_GSS_SVC_PRIVACY;
2274}
2275
Andrew Elblededeb132016-06-15 12:52:08 -04002276bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
J. Bruce Fields57266a62013-04-13 14:27:29 -04002277{
2278 struct svc_cred *cr = &rqstp->rq_cred;
2279
2280 if (!cl->cl_mach_cred)
2281 return true;
2282 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2283 return false;
2284 if (!svc_rqst_integrity_protected(rqstp))
2285 return false;
J. Bruce Fields414ca012015-11-20 10:48:02 -05002286 if (cl->cl_cred.cr_raw_principal)
2287 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2288 cr->cr_raw_principal);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002289 if (!cr->cr_principal)
2290 return false;
2291 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2292}
2293
Jeff Layton294ac322014-07-30 08:27:15 -04002294static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05002295{
Chuck Leverab4684d2012-03-02 17:13:50 -05002296 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Jeff Laytonf4199922014-06-17 07:44:11 -04002298 /*
2299 * This is opaque to client, so no need to byte-swap. Use
2300 * __force to keep sparse happy
2301 */
Arnd Bergmann9104ae42019-11-04 16:45:30 +01002302 verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08002303 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05002304 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305}
2306
Jeff Layton294ac322014-07-30 08:27:15 -04002307static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2308{
Arnd Bergmann9cc76802019-10-31 15:53:13 +01002309 clp->cl_clientid.cl_boot = (u32)nn->boot_time;
Jeff Layton294ac322014-07-30 08:27:15 -04002310 clp->cl_clientid.cl_id = nn->clientid_counter++;
2311 gen_confirm(clp, nn);
2312}
2313
Jeff Layton4770d722014-07-29 21:34:10 -04002314static struct nfs4_stid *
2315find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04002316{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002317 struct nfs4_stid *ret;
2318
2319 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2320 if (!ret || !ret->sc_type)
2321 return NULL;
2322 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04002323}
2324
Jeff Layton4770d722014-07-29 21:34:10 -04002325static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04002326find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002327{
2328 struct nfs4_stid *s;
2329
Jeff Layton4770d722014-07-29 21:34:10 -04002330 spin_lock(&cl->cl_lock);
2331 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002332 if (s != NULL) {
2333 if (typemask & s->sc_type)
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03002334 refcount_inc(&s->sc_count);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002335 else
2336 s = NULL;
2337 }
Jeff Layton4770d722014-07-29 21:34:10 -04002338 spin_unlock(&cl->cl_lock);
2339 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04002340}
2341
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002342static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2343{
2344 struct nfsdfs_client *nc;
2345 nc = get_nfsdfs_client(inode);
2346 if (!nc)
2347 return NULL;
2348 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2349}
2350
J. Bruce Fields169319f2019-06-19 12:39:46 -04002351static void seq_quote_mem(struct seq_file *m, char *data, int len)
2352{
2353 seq_printf(m, "\"");
2354 seq_escape_mem_ascii(m, data, len);
2355 seq_printf(m, "\"");
2356}
2357
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002358static int client_info_show(struct seq_file *m, void *v)
2359{
2360 struct inode *inode = m->private;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002361 struct nfs4_client *clp;
2362 u64 clid;
2363
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002364 clp = get_nfsdfs_clp(inode);
2365 if (!clp)
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002366 return -ENXIO;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002367 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2368 seq_printf(m, "clientid: 0x%llx\n", clid);
J. Bruce Fields169319f2019-06-19 12:39:46 -04002369 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
NeilBrown472d1552021-03-20 09:38:04 +11002370 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2371 seq_puts(m, "status: confirmed\n");
2372 else
2373 seq_puts(m, "status: unconfirmed\n");
J. Bruce Fields169319f2019-06-19 12:39:46 -04002374 seq_printf(m, "name: ");
2375 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2376 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
J. Bruce Fields79123442019-06-05 12:42:05 -04002377 if (clp->cl_nii_domain.data) {
2378 seq_printf(m, "Implementation domain: ");
2379 seq_quote_mem(m, clp->cl_nii_domain.data,
2380 clp->cl_nii_domain.len);
2381 seq_printf(m, "\nImplementation name: ");
2382 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
Arnd Bergmanne29f4702019-10-31 14:52:43 +01002383 seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
J. Bruce Fields79123442019-06-05 12:42:05 -04002384 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2385 }
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002386 drop_client(clp);
2387
2388 return 0;
2389}
2390
2391static int client_info_open(struct inode *inode, struct file *file)
2392{
2393 return single_open(file, client_info_show, inode);
2394}
2395
2396static const struct file_operations client_info_fops = {
2397 .open = client_info_open,
2398 .read = seq_read,
2399 .llseek = seq_lseek,
2400 .release = single_release,
2401};
2402
J. Bruce Fields78599c42019-04-22 15:26:23 -04002403static void *states_start(struct seq_file *s, loff_t *pos)
2404 __acquires(&clp->cl_lock)
2405{
2406 struct nfs4_client *clp = s->private;
2407 unsigned long id = *pos;
2408 void *ret;
2409
2410 spin_lock(&clp->cl_lock);
2411 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2412 *pos = id;
2413 return ret;
2414}
2415
2416static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2417{
2418 struct nfs4_client *clp = s->private;
2419 unsigned long id = *pos;
2420 void *ret;
2421
2422 id = *pos;
2423 id++;
2424 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2425 *pos = id;
2426 return ret;
2427}
2428
2429static void states_stop(struct seq_file *s, void *v)
2430 __releases(&clp->cl_lock)
2431{
2432 struct nfs4_client *clp = s->private;
2433
2434 spin_unlock(&clp->cl_lock);
2435}
2436
Achilles Gaikwad580da462020-04-20 18:20:31 +05302437static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
2438{
2439 seq_printf(s, "filename: \"%pD2\"", f->nf_file);
2440}
2441
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002442static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002443{
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002444 struct inode *inode = f->nf_inode;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002445
2446 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2447 MAJOR(inode->i_sb->s_dev),
2448 MINOR(inode->i_sb->s_dev),
2449 inode->i_ino);
2450}
2451
2452static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2453{
2454 seq_printf(s, "owner: ");
2455 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2456}
2457
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002458static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
2459{
J. Bruce Fieldsee590d22020-04-13 22:03:06 -04002460 seq_printf(s, "0x%.8x", stid->si_generation);
2461 seq_printf(s, "%12phN", &stid->si_opaque);
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002462}
2463
J. Bruce Fields78599c42019-04-22 15:26:23 -04002464static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2465{
2466 struct nfs4_ol_stateid *ols;
2467 struct nfs4_file *nf;
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002468 struct nfsd_file *file;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002469 struct nfs4_stateowner *oo;
2470 unsigned int access, deny;
2471
2472 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2473 return 0; /* XXX: or SEQ_SKIP? */
2474 ols = openlockstateid(st);
2475 oo = ols->st_stateowner;
2476 nf = st->sc_file;
2477 file = find_any_file(nf);
J. Bruce Fields9affa432020-07-15 13:31:36 -04002478 if (!file)
2479 return 0;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002480
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002481 seq_printf(s, "- ");
2482 nfs4_show_stateid(s, &st->sc_stateid);
2483 seq_printf(s, ": { type: open, ");
J. Bruce Fields78599c42019-04-22 15:26:23 -04002484
2485 access = bmap_to_share_mode(ols->st_access_bmap);
2486 deny = bmap_to_share_mode(ols->st_deny_bmap);
2487
J. Bruce Fieldsc4b77ed2019-10-03 12:33:08 -04002488 seq_printf(s, "access: %s%s, ",
J. Bruce Fields78599c42019-04-22 15:26:23 -04002489 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2490 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
J. Bruce Fieldsc4b77ed2019-10-03 12:33:08 -04002491 seq_printf(s, "deny: %s%s, ",
J. Bruce Fields78599c42019-04-22 15:26:23 -04002492 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2493 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2494
2495 nfs4_show_superblock(s, file);
2496 seq_printf(s, ", ");
Achilles Gaikwad580da462020-04-20 18:20:31 +05302497 nfs4_show_fname(s, file);
2498 seq_printf(s, ", ");
J. Bruce Fields78599c42019-04-22 15:26:23 -04002499 nfs4_show_owner(s, oo);
2500 seq_printf(s, " }\n");
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002501 nfsd_file_put(file);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002502
2503 return 0;
2504}
2505
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002506static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2507{
2508 struct nfs4_ol_stateid *ols;
2509 struct nfs4_file *nf;
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002510 struct nfsd_file *file;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002511 struct nfs4_stateowner *oo;
2512
2513 ols = openlockstateid(st);
2514 oo = ols->st_stateowner;
2515 nf = st->sc_file;
2516 file = find_any_file(nf);
J. Bruce Fields9affa432020-07-15 13:31:36 -04002517 if (!file)
2518 return 0;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002519
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002520 seq_printf(s, "- ");
2521 nfs4_show_stateid(s, &st->sc_stateid);
2522 seq_printf(s, ": { type: lock, ");
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002523
2524 /*
2525 * Note: a lock stateid isn't really the same thing as a lock,
2526 * it's the locking state held by one owner on a file, and there
2527 * may be multiple (or no) lock ranges associated with it.
2528 * (Same for the matter is true of open stateids.)
2529 */
2530
2531 nfs4_show_superblock(s, file);
2532 /* XXX: open stateid? */
2533 seq_printf(s, ", ");
Achilles Gaikwad580da462020-04-20 18:20:31 +05302534 nfs4_show_fname(s, file);
2535 seq_printf(s, ", ");
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002536 nfs4_show_owner(s, oo);
2537 seq_printf(s, " }\n");
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002538 nfsd_file_put(file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002539
2540 return 0;
2541}
2542
2543static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2544{
2545 struct nfs4_delegation *ds;
2546 struct nfs4_file *nf;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04002547 struct nfsd_file *file;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002548
2549 ds = delegstateid(st);
2550 nf = st->sc_file;
J. Bruce Fields9affa432020-07-15 13:31:36 -04002551 file = find_deleg_file(nf);
2552 if (!file)
2553 return 0;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002554
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002555 seq_printf(s, "- ");
2556 nfs4_show_stateid(s, &st->sc_stateid);
2557 seq_printf(s, ": { type: deleg, ");
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002558
2559 /* Kinda dead code as long as we only support read delegs: */
2560 seq_printf(s, "access: %s, ",
2561 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2562
2563 /* XXX: lease time, whether it's being recalled. */
2564
2565 nfs4_show_superblock(s, file);
Achilles Gaikwad580da462020-04-20 18:20:31 +05302566 seq_printf(s, ", ");
2567 nfs4_show_fname(s, file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002568 seq_printf(s, " }\n");
J. Bruce Fields9affa432020-07-15 13:31:36 -04002569 nfsd_file_put(file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002570
2571 return 0;
2572}
2573
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002574static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2575{
2576 struct nfs4_layout_stateid *ls;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04002577 struct nfsd_file *file;
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002578
2579 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2580 file = ls->ls_file;
2581
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002582 seq_printf(s, "- ");
2583 nfs4_show_stateid(s, &st->sc_stateid);
2584 seq_printf(s, ": { type: layout, ");
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002585
2586 /* XXX: What else would be useful? */
2587
2588 nfs4_show_superblock(s, file);
Achilles Gaikwad580da462020-04-20 18:20:31 +05302589 seq_printf(s, ", ");
2590 nfs4_show_fname(s, file);
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002591 seq_printf(s, " }\n");
2592
2593 return 0;
2594}
2595
J. Bruce Fields78599c42019-04-22 15:26:23 -04002596static int states_show(struct seq_file *s, void *v)
2597{
2598 struct nfs4_stid *st = v;
2599
2600 switch (st->sc_type) {
2601 case NFS4_OPEN_STID:
2602 return nfs4_show_open(s, st);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002603 case NFS4_LOCK_STID:
2604 return nfs4_show_lock(s, st);
2605 case NFS4_DELEG_STID:
2606 return nfs4_show_deleg(s, st);
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002607 case NFS4_LAYOUT_STID:
2608 return nfs4_show_layout(s, st);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002609 default:
2610 return 0; /* XXX: or SEQ_SKIP? */
2611 }
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002612 /* XXX: copy stateids? */
J. Bruce Fields78599c42019-04-22 15:26:23 -04002613}
2614
2615static struct seq_operations states_seq_ops = {
2616 .start = states_start,
2617 .next = states_next,
2618 .stop = states_stop,
2619 .show = states_show
2620};
2621
2622static int client_states_open(struct inode *inode, struct file *file)
2623{
J. Bruce Fields78599c42019-04-22 15:26:23 -04002624 struct seq_file *s;
2625 struct nfs4_client *clp;
2626 int ret;
2627
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002628 clp = get_nfsdfs_clp(inode);
2629 if (!clp)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002630 return -ENXIO;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002631
2632 ret = seq_open(file, &states_seq_ops);
2633 if (ret)
2634 return ret;
2635 s = file->private_data;
2636 s->private = clp;
2637 return 0;
2638}
2639
2640static int client_opens_release(struct inode *inode, struct file *file)
2641{
2642 struct seq_file *m = file->private_data;
2643 struct nfs4_client *clp = m->private;
2644
2645 /* XXX: alternatively, we could get/drop in seq start/stop */
2646 drop_client(clp);
2647 return 0;
2648}
2649
2650static const struct file_operations client_states_fops = {
2651 .open = client_states_open,
2652 .read = seq_read,
2653 .llseek = seq_lseek,
2654 .release = client_opens_release,
2655};
2656
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002657/*
2658 * Normally we refuse to destroy clients that are in use, but here the
2659 * administrator is telling us to just do it. We also want to wait
2660 * so the caller has a guarantee that the client's locks are gone by
2661 * the time the write returns:
2662 */
YueHaibing297e57a2019-07-08 15:29:33 +08002663static void force_expire_client(struct nfs4_client *clp)
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002664{
2665 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2666 bool already_expired;
2667
Chuck Lever2958d2e2021-05-14 15:56:06 -04002668 trace_nfsd_clid_admin_expired(&clp->cl_clientid);
2669
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002670 spin_lock(&clp->cl_lock);
2671 clp->cl_time = 0;
2672 spin_unlock(&clp->cl_lock);
2673
2674 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2675 spin_lock(&nn->client_lock);
2676 already_expired = list_empty(&clp->cl_lru);
2677 if (!already_expired)
2678 unhash_client_locked(clp);
2679 spin_unlock(&nn->client_lock);
2680
2681 if (!already_expired)
2682 expire_client(clp);
2683 else
2684 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2685}
2686
2687static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2688 size_t size, loff_t *pos)
2689{
2690 char *data;
2691 struct nfs4_client *clp;
2692
2693 data = simple_transaction_get(file, buf, size);
2694 if (IS_ERR(data))
2695 return PTR_ERR(data);
2696 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2697 return -EINVAL;
2698 clp = get_nfsdfs_clp(file_inode(file));
2699 if (!clp)
2700 return -ENXIO;
2701 force_expire_client(clp);
2702 drop_client(clp);
2703 return 7;
2704}
2705
2706static const struct file_operations client_ctl_fops = {
2707 .write = client_ctl_write,
2708 .release = simple_transaction_release,
2709};
2710
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002711static const struct tree_descr client_files[] = {
2712 [0] = {"info", &client_info_fops, S_IRUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002713 [1] = {"states", &client_states_fops, S_IRUSR},
Petr Vorel6cbfad52020-03-13 13:39:57 +01002714 [2] = {"ctl", &client_ctl_fops, S_IWUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002715 [3] = {""},
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002716};
2717
Jeff Layton2216d442012-11-12 15:00:57 -05002718static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002719 struct svc_rqst *rqstp, nfs4_verifier *verf)
2720{
2721 struct nfs4_client *clp;
2722 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002723 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002724 struct net *net = SVC_NET(rqstp);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002725 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
NeilBrown472d1552021-03-20 09:38:04 +11002726 struct dentry *dentries[ARRAY_SIZE(client_files)];
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002727
2728 clp = alloc_client(name);
2729 if (clp == NULL)
2730 return NULL;
2731
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002732 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2733 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002734 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002735 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002736 }
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002737 gen_clid(clp, nn);
2738 kref_init(&clp->cl_nfsdfs.cl_ref);
Christoph Hellwig0162ac22014-09-24 12:19:19 +02002739 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01002740 clp->cl_time = ktime_get_boottime_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002741 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002742 copy_verf(clp, verf);
J. Bruce Fields3bade242019-05-14 21:38:11 -04002743 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04002744 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002745 clp->net = net;
NeilBrown472d1552021-03-20 09:38:04 +11002746 clp->cl_nfsd_dentry = nfsd_client_mkdir(
2747 nn, &clp->cl_nfsdfs,
2748 clp->cl_clientid.cl_id - nn->clientid_base,
2749 client_files, dentries);
2750 clp->cl_nfsd_info_dentry = dentries[0];
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002751 if (!clp->cl_nfsd_dentry) {
2752 free_client(clp);
2753 return NULL;
2754 }
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002755 return clp;
2756}
2757
NeilBrownfd39ca92005-06-23 22:04:03 -07002758static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002759add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2760{
2761 struct rb_node **new = &(root->rb_node), *parent = NULL;
2762 struct nfs4_client *clp;
2763
2764 while (*new) {
2765 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2766 parent = *new;
2767
2768 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2769 new = &((*new)->rb_left);
2770 else
2771 new = &((*new)->rb_right);
2772 }
2773
2774 rb_link_node(&new_clp->cl_namenode, parent, new);
2775 rb_insert_color(&new_clp->cl_namenode, root);
2776}
2777
2778static struct nfs4_client *
2779find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2780{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002781 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002782 struct rb_node *node = root->rb_node;
2783 struct nfs4_client *clp;
2784
2785 while (node) {
2786 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2787 cmp = compare_blob(&clp->cl_name, name);
2788 if (cmp > 0)
2789 node = node->rb_left;
2790 else if (cmp < 0)
2791 node = node->rb_right;
2792 else
2793 return clp;
2794 }
2795 return NULL;
2796}
2797
2798static void
2799add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800{
2801 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002802 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
Trond Myklebust0a880a22014-07-30 08:27:10 -04002804 lockdep_assert_held(&nn->client_lock);
2805
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002806 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002807 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002809 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002810 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811}
2812
NeilBrownfd39ca92005-06-23 22:04:03 -07002813static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814move_to_confirmed(struct nfs4_client *clp)
2815{
2816 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002817 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
Trond Myklebust0a880a22014-07-30 08:27:10 -04002819 lockdep_assert_held(&nn->client_lock);
2820
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002821 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002822 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002823 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Chuck Lever7e3b32a2021-05-14 15:55:48 -04002824 if (!test_and_set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) {
2825 trace_nfsd_clid_confirmed(&clp->cl_clientid);
2826 if (clp->cl_nfsd_dentry && clp->cl_nfsd_info_dentry)
2827 fsnotify_dentry(clp->cl_nfsd_info_dentry, FS_MODIFY);
2828 }
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002829 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830}
2831
2832static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002833find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834{
2835 struct nfs4_client *clp;
2836 unsigned int idhashval = clientid_hashval(clid->cl_id);
2837
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002838 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002839 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002840 if ((bool)clp->cl_minorversion != sessions)
2841 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002842 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 }
2846 return NULL;
2847}
2848
2849static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002850find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2851{
2852 struct list_head *tbl = nn->conf_id_hashtbl;
2853
Trond Myklebust0a880a22014-07-30 08:27:10 -04002854 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002855 return find_client_in_id_table(tbl, clid, sessions);
2856}
2857
2858static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002859find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002861 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
Trond Myklebust0a880a22014-07-30 08:27:10 -04002863 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002864 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865}
2866
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002867static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002868{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002869 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002870}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002871
NeilBrown28ce6052005-06-23 22:03:56 -07002872static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002873find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002874{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002875 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002876 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002877}
2878
2879static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002880find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002881{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002882 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002883 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002884}
2885
NeilBrownfd39ca92005-06-23 22:04:03 -07002886static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002887gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002889 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002890 struct sockaddr *sa = svc_addr(rqstp);
2891 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002892 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
Jeff Layton7077ecb2009-08-14 12:57:58 -04002894 /* Currently, we only support tcp and tcp6 for the callback channel */
2895 if (se->se_callback_netid_len == 3 &&
2896 !memcmp(se->se_callback_netid_val, "tcp", 3))
2897 expected_family = AF_INET;
2898 else if (se->se_callback_netid_len == 4 &&
2899 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2900 expected_family = AF_INET6;
2901 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 goto out_err;
2903
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002904 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002905 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002906 (struct sockaddr *)&conn->cb_addr,
2907 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002908
J. Bruce Fields07263f12010-05-31 19:09:40 -04002909 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002911
J. Bruce Fields07263f12010-05-31 19:09:40 -04002912 if (conn->cb_addr.ss_family == AF_INET6)
2913 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002914
J. Bruce Fields07263f12010-05-31 19:09:40 -04002915 conn->cb_prog = se->se_callback_prog;
2916 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002917 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Chuck Lever1eace0d2020-04-05 14:15:29 -04002918 trace_nfsd_cb_args(clp, conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 return;
2920out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002921 conn->cb_addr.ss_family = AF_UNSPEC;
2922 conn->cb_addrlen = 0;
Chuck Lever1eace0d2020-04-05 14:15:29 -04002923 trace_nfsd_cb_nodelegs(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 return;
2925}
2926
Andy Adamson074fe892009-04-03 08:28:15 +03002927/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002928 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002929 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002930static void
Andy Adamson074fe892009-04-03 08:28:15 +03002931nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2932{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04002933 struct xdr_buf *buf = resp->xdr->buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002934 struct nfsd4_slot *slot = resp->cstate.slot;
2935 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002936
Andy Adamson557ce262009-08-28 08:45:04 -04002937 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002938
J. Bruce Fields085def32017-10-18 16:17:18 -04002939 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamson557ce262009-08-28 08:45:04 -04002940 slot->sl_opcnt = resp->opcnt;
2941 slot->sl_status = resp->cstate.status;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04002942 free_svc_cred(&slot->sl_cred);
2943 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
Andy Adamsonbf864a32009-04-03 08:28:35 +03002944
J. Bruce Fields085def32017-10-18 16:17:18 -04002945 if (!nfsd4_cache_this(resp)) {
2946 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002947 return;
2948 }
J. Bruce Fields085def32017-10-18 16:17:18 -04002949 slot->sl_flags |= NFSD4_SLOT_CACHED;
2950
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002951 base = resp->cstate.data_offset;
2952 slot->sl_datalen = buf->len - base;
2953 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Dan Carpenterd3f03402015-11-21 13:28:50 +03002954 WARN(1, "%s: sessions DRC could not cache compound\n",
2955 __func__);
Andy Adamson557ce262009-08-28 08:45:04 -04002956 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002957}
2958
2959/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002960 * Encode the replay sequence operation from the slot values.
2961 * If cachethis is FALSE encode the uncached rep error on the next
2962 * operation which sets resp->p and increments resp->opcnt for
2963 * nfs4svc_encode_compoundres.
2964 *
Andy Adamson074fe892009-04-03 08:28:15 +03002965 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002966static __be32
2967nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2968 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002969{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002970 struct nfsd4_op *op;
2971 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002972
Andy Adamsonabfabf82009-07-23 19:02:18 -04002973 /* Encode the replayed sequence operation */
2974 op = &args->ops[resp->opcnt - 1];
2975 nfsd4_encode_operation(resp, op);
2976
J. Bruce Fields085def32017-10-18 16:17:18 -04002977 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2978 return op->status;
2979 if (args->opcnt == 1) {
2980 /*
2981 * The original operation wasn't a solo sequence--we
2982 * always cache those--so this retry must not match the
2983 * original:
2984 */
2985 op->status = nfserr_seq_false_retry;
2986 } else {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002987 op = &args->ops[resp->opcnt++];
2988 op->status = nfserr_retry_uncached_rep;
2989 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002990 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002991 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002992}
2993
2994/*
Andy Adamson557ce262009-08-28 08:45:04 -04002995 * The sequence operation is not cached because we can use the slot and
2996 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002997 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002998static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002999nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
3000 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03003001{
Andy Adamson557ce262009-08-28 08:45:04 -04003002 struct nfsd4_slot *slot = resp->cstate.slot;
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003003 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04003004 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03003005 __be32 status;
3006
Andy Adamson557ce262009-08-28 08:45:04 -04003007 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03003008
Andy Adamsonabfabf82009-07-23 19:02:18 -04003009 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04003010 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04003011 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03003012
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04003013 p = xdr_reserve_space(xdr, slot->sl_datalen);
3014 if (!p) {
3015 WARN_ON_ONCE(1);
3016 return nfserr_serverfault;
3017 }
3018 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
3019 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03003020
Andy Adamson557ce262009-08-28 08:45:04 -04003021 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04003022 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03003023}
3024
Andy Adamson0733d212009-04-03 08:28:01 +03003025/*
3026 * Set the exchange_id flags returned by the server.
3027 */
3028static void
3029nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
3030{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003031#ifdef CONFIG_NFSD_PNFS
3032 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
3033#else
Andy Adamson0733d212009-04-03 08:28:01 +03003034 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003035#endif
Andy Adamson0733d212009-04-03 08:28:01 +03003036
3037 /* Referrals are supported, Migration is not. */
3038 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
3039
3040 /* set the wire flags to return to client. */
3041 clid->flags = new->cl_exchange_flags;
3042}
3043
J. Bruce Fields4eaea132015-10-15 16:11:01 -04003044static bool client_has_openowners(struct nfs4_client *clp)
3045{
3046 struct nfs4_openowner *oo;
3047
3048 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
3049 if (!list_empty(&oo->oo_owner.so_stateids))
3050 return true;
3051 }
3052 return false;
3053}
3054
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003055static bool client_has_state(struct nfs4_client *clp)
3056{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04003057 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08003058#ifdef CONFIG_NFSD_PNFS
3059 || !list_empty(&clp->cl_lo_states)
3060#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04003061 || !list_empty(&clp->cl_delegations)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04003062 || !list_empty(&clp->cl_sessions)
3063 || !list_empty(&clp->async_copies);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003064}
3065
J. Bruce Fields79123442019-06-05 12:42:05 -04003066static __be32 copy_impl_id(struct nfs4_client *clp,
3067 struct nfsd4_exchange_id *exid)
3068{
3069 if (!exid->nii_domain.data)
3070 return 0;
3071 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
3072 if (!clp->cl_nii_domain.data)
3073 return nfserr_jukebox;
3074 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
3075 if (!clp->cl_nii_name.data)
3076 return nfserr_jukebox;
Arnd Bergmanne29f4702019-10-31 14:52:43 +01003077 clp->cl_nii_time = exid->nii_time;
J. Bruce Fields79123442019-06-05 12:42:05 -04003078 return 0;
3079}
3080
Al Virob37ad282006-10-19 23:28:59 -07003081__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003082nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3083 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003084{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003085 struct nfsd4_exchange_id *exid = &u->exchange_id;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003086 struct nfs4_client *conf, *new;
3087 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003088 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04003089 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03003090 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04003091 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04003092 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003093 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03003094
Jeff Layton363168b2009-08-14 12:57:56 -04003095 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03003096 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Chuck Lever523ec6e2020-11-02 15:19:12 -05003097 "ip_addr=%s flags %x, spa_how %u\n",
Andy Adamson0733d212009-04-03 08:28:01 +03003098 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04003099 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03003100
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003101 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03003102 return nfserr_inval;
3103
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003104 new = create_client(exid->clname, rqstp, &verf);
3105 if (new == NULL)
3106 return nfserr_jukebox;
J. Bruce Fields79123442019-06-05 12:42:05 -04003107 status = copy_impl_id(new, exid);
3108 if (status)
3109 goto out_nolock;
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003110
Andy Adamson0733d212009-04-03 08:28:01 +03003111 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003112 case SP4_MACH_CRED:
Andrew Elbleed941642016-06-15 12:52:09 -04003113 exid->spo_must_enforce[0] = 0;
3114 exid->spo_must_enforce[1] = (
3115 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3116 1 << (OP_EXCHANGE_ID - 32) |
3117 1 << (OP_CREATE_SESSION - 32) |
3118 1 << (OP_DESTROY_SESSION - 32) |
3119 1 << (OP_DESTROY_CLIENTID - 32));
3120
3121 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3122 1 << (OP_OPEN_DOWNGRADE) |
3123 1 << (OP_LOCKU) |
3124 1 << (OP_DELEGRETURN));
3125
3126 exid->spo_must_allow[1] &= (
3127 1 << (OP_TEST_STATEID - 32) |
3128 1 << (OP_FREE_STATEID - 32));
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003129 if (!svc_rqst_integrity_protected(rqstp)) {
3130 status = nfserr_inval;
3131 goto out_nolock;
3132 }
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05003133 /*
3134 * Sometimes userspace doesn't give us a principal.
3135 * Which is a bug, really. Anyway, we can't enforce
3136 * MACH_CRED in that case, better to give up now:
3137 */
J. Bruce Fields414ca012015-11-20 10:48:02 -05003138 if (!new->cl_cred.cr_principal &&
3139 !new->cl_cred.cr_raw_principal) {
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05003140 status = nfserr_serverfault;
3141 goto out_nolock;
3142 }
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003143 new->cl_mach_cred = true;
Gustavo A. R. Silva76c50eb2020-11-20 12:26:40 -06003144 break;
Andy Adamson0733d212009-04-03 08:28:01 +03003145 case SP4_NONE:
3146 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003147 default: /* checked by xdr code */
3148 WARN_ON_ONCE(1);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003149 fallthrough;
Andy Adamson0733d212009-04-03 08:28:01 +03003150 case SP4_SSV:
Kinglong Mee8edf4b02016-02-26 22:36:42 +08003151 status = nfserr_encr_alg_unsupp;
3152 goto out_nolock;
Andy Adamson0733d212009-04-03 08:28:01 +03003153 }
3154
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003155 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003156 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003157 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03003158 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04003159 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3160 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3161
J. Bruce Fields136e6582012-05-12 20:37:23 -04003162 if (update) {
3163 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003164 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03003165 goto out;
3166 }
Andrew Elblededeb132016-06-15 12:52:08 -04003167 if (!nfsd4_mach_creds_match(conf, rqstp)) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003168 status = nfserr_wrong_cred;
3169 goto out;
3170 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003171 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03003172 status = nfserr_perm;
3173 goto out;
3174 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003175 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03003176 status = nfserr_not_same;
3177 goto out;
3178 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003179 /* case 6 */
3180 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003181 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03003182 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003183 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003184 if (client_has_state(conf)) {
3185 status = nfserr_clid_inuse;
Chuck Lever27787732021-05-14 15:55:29 -04003186 trace_nfsd_clid_cred_mismatch(conf, rqstp);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003187 goto out;
3188 }
Andy Adamson0733d212009-04-03 08:28:01 +03003189 goto out_new;
3190 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003191 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04003192 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003193 goto out_copy;
3194 }
3195 /* case 5, client reboot */
Chuck Lever744ea542021-05-14 15:55:36 -04003196 trace_nfsd_clid_verf_mismatch(conf, rqstp, &verf);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003197 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003198 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003199 }
3200
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003201 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003202 status = nfserr_noent;
3203 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03003204 }
3205
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003206 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003207 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003208 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03003209
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003210 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03003211out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04003212 if (conf) {
3213 status = mark_client_expired_locked(conf);
3214 if (status)
3215 goto out;
Chuck Lever2958d2e2021-05-14 15:56:06 -04003216 trace_nfsd_clid_replaced(&conf->cl_clientid);
Jeff Laytonfd699b82014-07-30 08:27:14 -04003217 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04003218 new->cl_minorversion = cstate->minorversion;
Andrew Elbleed941642016-06-15 12:52:09 -04003219 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3220 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
Andy Adamson0733d212009-04-03 08:28:01 +03003221
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003222 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003223 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03003224out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003225 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3226 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03003227
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003228 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3229 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03003230
3231 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003232 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03003233 status = nfs_ok;
3234
3235out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003236 spin_unlock(&nn->client_lock);
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003237out_nolock:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003238 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003239 expire_client(new);
3240 if (unconf)
3241 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03003242 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003243}
3244
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003245static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04003246check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003247{
Andy Adamson88e588d2009-07-23 19:02:15 -04003248 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3249 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003250
3251 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04003252 if (slot_inuse) {
3253 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003254 return nfserr_jukebox;
3255 else
3256 return nfserr_seq_misordered;
3257 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05003258 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04003259 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03003260 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04003261 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003262 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003263 return nfserr_seq_misordered;
3264}
3265
Andy Adamson49557cc2009-07-23 19:02:16 -04003266/*
3267 * Cache the create session result into the create session single DRC
3268 * slot cache by saving the xdr structure. sl_seqid has been set.
3269 * Do this for solo or embedded create session operations.
3270 */
3271static void
3272nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003273 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04003274{
3275 slot->sl_status = nfserr;
3276 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3277}
3278
3279static __be32
3280nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3281 struct nfsd4_clid_slot *slot)
3282{
3283 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3284 return slot->sl_status;
3285}
3286
Mi Jinlong1b74c252011-07-14 14:50:17 +08003287#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3288 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3289 1 + /* MIN tag is length with zero, only length */ \
3290 3 + /* version, opcount, opcode */ \
3291 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3292 /* seqid, slotID, slotID, cache */ \
3293 4 ) * sizeof(__be32))
3294
3295#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3296 2 + /* verifier: AUTH_NULL, length 0 */\
3297 1 + /* status */ \
3298 1 + /* MIN tag is length with zero, only length */ \
3299 3 + /* opcount, opcode, opstatus*/ \
3300 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3301 /* seqid, slotID, slotID, slotID, status */ \
3302 5 ) * sizeof(__be32))
3303
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003304static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08003305{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003306 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3307
J. Bruce Fields373cd402013-04-08 15:42:12 -04003308 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3309 return nfserr_toosmall;
3310 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3311 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003312 ca->headerpadsz = 0;
3313 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3314 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3315 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3316 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3317 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3318 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3319 /*
3320 * Note decreasing slot size below client's request may make it
3321 * difficult for client to function correctly, whereas
3322 * decreasing the number of slots will (just?) affect
3323 * performance. When short on memory we therefore prefer to
3324 * decrease number of slots instead of their size. Clients that
3325 * request larger slots than they need will get poor results:
NeilBrown7f49fd5d2019-09-20 16:33:16 +10003326 * Note that we always allow at least one slot, because our
3327 * accounting is soft and provides no guarantees either way.
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003328 */
NeilBrown2030ca52019-09-20 16:36:45 +10003329 ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003330
J. Bruce Fields373cd402013-04-08 15:42:12 -04003331 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08003332}
3333
Chuck Lever45006322016-03-01 13:06:02 -05003334/*
3335 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3336 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3337 */
3338#define RPC_MAX_HEADER_WITH_AUTH_SYS \
3339 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3340
3341#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3342 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3343
Kinglong Mee8a891632013-12-23 18:11:02 +08003344#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003345 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003346#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003347 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3348 sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003349
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003350static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3351{
3352 ca->headerpadsz = 0;
3353
Kinglong Mee8a891632013-12-23 18:11:02 +08003354 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003355 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08003356 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003357 return nfserr_toosmall;
3358 ca->maxresp_cached = 0;
3359 if (ca->maxops < 2)
3360 return nfserr_toosmall;
3361
3362 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003363}
3364
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003365static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3366{
3367 switch (cbs->flavor) {
3368 case RPC_AUTH_NULL:
3369 case RPC_AUTH_UNIX:
3370 return nfs_ok;
3371 default:
3372 /*
3373 * GSS case: the spec doesn't allow us to return this
3374 * error. But it also doesn't allow us not to support
3375 * GSS.
3376 * I'd rather this fail hard than return some error the
3377 * client might think it can already handle:
3378 */
3379 return nfserr_encr_alg_unsupp;
3380 }
3381}
3382
Andy Adamson069b6ad2009-04-03 08:27:58 +03003383__be32
3384nfsd4_create_session(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003385 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003386{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003387 struct nfsd4_create_session *cr_ses = &u->create_session;
Jeff Layton363168b2009-08-14 12:57:56 -04003388 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003389 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003390 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003391 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003392 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003393 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003394 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003395 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003396
Mi Jinlonga62573d2011-03-23 17:57:07 +08003397 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3398 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003399 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3400 if (status)
3401 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003402 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04003403 if (status)
3404 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003405 status = check_backchannel_attrs(&cr_ses->back_channel);
3406 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08003407 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003408 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08003409 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003410 if (!new)
3411 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003412 conn = alloc_conn_from_crses(rqstp, cr_ses);
3413 if (!conn)
3414 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08003415
Jeff Laytond20c11d2014-07-30 08:27:07 -04003416 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003417 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003418 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003419 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003420
3421 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003422 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003423 if (!nfsd4_mach_creds_match(conf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003424 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003425 cs_slot = &conf->cl_cs_slot;
3426 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08003427 if (status) {
3428 if (status == nfserr_replay_cache)
3429 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003430 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003431 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003432 } else if (unconf) {
Chuck Lever27787732021-05-14 15:55:29 -04003433 status = nfserr_clid_inuse;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003434 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04003435 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Chuck Lever27787732021-05-14 15:55:29 -04003436 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003437 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003438 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04003439 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003440 if (!nfsd4_mach_creds_match(unconf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003441 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003442 cs_slot = &unconf->cl_cs_slot;
3443 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03003444 if (status) {
3445 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003446 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003447 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003448 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003449 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003450 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04003451 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003452 if (status) {
3453 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003454 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003455 }
Chuck Lever2958d2e2021-05-14 15:56:06 -04003456 trace_nfsd_clid_replaced(&old->cl_clientid);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003457 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04003458 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003459 conf = unconf;
3460 } else {
3461 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003462 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003463 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003464 status = nfs_ok;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003465 /* Persistent sessions are not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003466 cr_ses->flags &= ~SESSION4_PERSIST;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003467 /* Upshifting from TCP to RDMA is not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003468 cr_ses->flags &= ~SESSION4_RDMA;
3469
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003470 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003471 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003472
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003473 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003474 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04003475 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04003476 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003477
Jeff Laytond20c11d2014-07-30 08:27:07 -04003478 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04003479 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003480 spin_unlock(&nn->client_lock);
3481 /* init connection and backchannel */
3482 nfsd4_init_conn(rqstp, conn, new);
3483 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003484 if (old)
3485 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003486 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003487out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003488 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003489 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003490 if (old)
3491 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003492out_free_session:
3493 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003494out_release_drc_mem:
3495 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04003496 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003497}
3498
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003499static __be32 nfsd4_map_bcts_dir(u32 *dir)
3500{
3501 switch (*dir) {
3502 case NFS4_CDFC4_FORE:
3503 case NFS4_CDFC4_BACK:
3504 return nfs_ok;
3505 case NFS4_CDFC4_FORE_OR_BOTH:
3506 case NFS4_CDFC4_BACK_OR_BOTH:
3507 *dir = NFS4_CDFC4_BOTH;
3508 return nfs_ok;
zhengbinfc5fc5d2019-12-19 17:29:20 +08003509 }
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003510 return nfserr_inval;
3511}
3512
Christoph Hellwigeb698532017-05-08 20:58:35 +02003513__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3514 struct nfsd4_compound_state *cstate,
3515 union nfsd4_op_u *u)
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003516{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003517 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003518 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003519 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003520 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003521
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003522 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3523 if (status)
3524 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003525 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003526 session->se_cb_prog = bc->bc_cb_program;
3527 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003528 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003529
3530 nfsd4_probe_callback(session->se_client);
3531
3532 return nfs_ok;
3533}
3534
J. Bruce Fieldsc2d715a2020-04-27 17:59:01 -04003535static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3536{
3537 struct nfsd4_conn *c;
3538
3539 list_for_each_entry(c, &s->se_conns, cn_persession) {
3540 if (c->cn_xprt == xpt) {
3541 return c;
3542 }
3543 }
3544 return NULL;
3545}
3546
3547static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst,
3548 struct nfsd4_session *session, u32 req)
3549{
3550 struct nfs4_client *clp = session->se_client;
3551 struct svc_xprt *xpt = rqst->rq_xprt;
3552 struct nfsd4_conn *c;
3553 __be32 status;
3554
3555 /* Following the last paragraph of RFC 5661 Section 18.34.3: */
3556 spin_lock(&clp->cl_lock);
3557 c = __nfsd4_find_conn(xpt, session);
3558 if (!c)
3559 status = nfserr_noent;
3560 else if (req == c->cn_flags)
3561 status = nfs_ok;
3562 else if (req == NFS4_CDFC4_FORE_OR_BOTH &&
3563 c->cn_flags != NFS4_CDFC4_BACK)
3564 status = nfs_ok;
3565 else if (req == NFS4_CDFC4_BACK_OR_BOTH &&
3566 c->cn_flags != NFS4_CDFC4_FORE)
3567 status = nfs_ok;
3568 else
3569 status = nfserr_inval;
3570 spin_unlock(&clp->cl_lock);
3571 return status;
3572}
3573
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003574__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3575 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003576 union nfsd4_op_u *u)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003577{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003578 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003579 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003580 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003581 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003582 struct net *net = SVC_NET(rqstp);
3583 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003584
3585 if (!nfsd4_last_compound_op(rqstp))
3586 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003587 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003588 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003589 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003590 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003591 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003592 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003593 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003594 goto out;
J. Bruce Fieldsc2d715a2020-04-27 17:59:01 -04003595 status = nfsd4_match_existing_connection(rqstp, session, bcts->dir);
3596 if (status == nfs_ok || status == nfserr_inval)
3597 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003598 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003599 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003600 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003601 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003602 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003603 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003604 goto out;
3605 nfsd4_init_conn(rqstp, conn, session);
3606 status = nfs_ok;
3607out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003608 nfsd4_put_session(session);
3609out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003610 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003611}
3612
J. Bruce Fields665d5072018-04-11 16:53:36 -04003613static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003614{
J. Bruce Fields665d5072018-04-11 16:53:36 -04003615 if (!cstate->session)
Fengguang Wu51d87bc2018-03-22 13:37:20 +08003616 return false;
J. Bruce Fields665d5072018-04-11 16:53:36 -04003617 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003618}
3619
Andy Adamson069b6ad2009-04-03 08:27:58 +03003620__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003621nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3622 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003623{
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003624 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003625 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003626 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003627 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003628 struct net *net = SVC_NET(r);
3629 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003630
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003631 status = nfserr_not_only_op;
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003632 if (nfsd4_compound_in_session(cstate, sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04003633 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003634 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003635 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04003636 }
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003637 dump_sessionid(__func__, sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003638 spin_lock(&nn->client_lock);
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003639 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003640 if (!ses)
3641 goto out_client_lock;
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(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003644 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003645 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003646 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003647 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003648 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003649 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003650
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05003651 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04003652
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003653 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003654 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003655out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003656 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003657out_client_lock:
3658 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003659out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03003660 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003661}
3662
J. Bruce Fields57266a62013-04-13 14:27:29 -04003663static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003664{
3665 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003666 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003667 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003668 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003669
3670 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003671 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003672 if (c)
3673 goto out_free;
3674 status = nfserr_conn_not_bound_to_session;
3675 if (clp->cl_mach_cred)
3676 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003677 __nfsd4_hash_conn(new, ses);
3678 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003679 ret = nfsd4_register_conn(new);
3680 if (ret)
3681 /* oops; xprt is already down: */
3682 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003683 return nfs_ok;
3684out_free:
3685 spin_unlock(&clp->cl_lock);
3686 free_conn(new);
3687 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003688}
3689
Mi Jinlong868b89c2011-04-27 09:09:58 +08003690static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3691{
3692 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3693
3694 return args->opcnt > session->se_fchannel.maxops;
3695}
3696
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003697static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3698 struct nfsd4_session *session)
3699{
3700 struct xdr_buf *xb = &rqstp->rq_arg;
3701
3702 return xb->len > session->se_fchannel.maxreq_sz;
3703}
3704
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003705static bool replay_matches_cache(struct svc_rqst *rqstp,
3706 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3707{
3708 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3709
3710 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3711 (bool)seq->cachethis)
3712 return false;
3713 /*
Scott Mayhew6e73e922019-10-09 15:11:37 -04003714 * If there's an error then the reply can have fewer ops than
3715 * the call.
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003716 */
Scott Mayhew6e73e922019-10-09 15:11:37 -04003717 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3718 return false;
3719 /*
3720 * But if we cached a reply with *more* ops than the call you're
3721 * sending us now, then this new call is clearly not really a
3722 * replay of the old one:
3723 */
3724 if (slot->sl_opcnt > argp->opcnt)
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003725 return false;
3726 /* This is the only check explicitly called by spec: */
3727 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3728 return false;
3729 /*
3730 * There may be more comparisons we could actually do, but the
3731 * spec doesn't require us to catch every case where the calls
3732 * don't match (that would require caching the call as well as
3733 * the reply), so we don't bother.
3734 */
3735 return true;
3736}
3737
Andy Adamson069b6ad2009-04-03 08:27:58 +03003738__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003739nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3740 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003741{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003742 struct nfsd4_sequence *seq = &u->sequence;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003743 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003744 struct xdr_stream *xdr = resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003745 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003746 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003747 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003748 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003749 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003750 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003751 struct net *net = SVC_NET(rqstp);
3752 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003753
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003754 if (resp->opcnt != 1)
3755 return nfserr_sequence_pos;
3756
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003757 /*
3758 * Will be either used or freed by nfsd4_sequence_check_conn
3759 * below.
3760 */
3761 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3762 if (!conn)
3763 return nfserr_jukebox;
3764
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003765 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003766 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003767 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003768 goto out_no_session;
3769 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003770
Mi Jinlong868b89c2011-04-27 09:09:58 +08003771 status = nfserr_too_many_ops;
3772 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003773 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003774
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003775 status = nfserr_req_too_big;
3776 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003777 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003778
Benny Halevyb85d4c02009-04-03 08:28:08 +03003779 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003780 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003781 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003782
Andy Adamson557ce262009-08-28 08:45:04 -04003783 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003784 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3785
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003786 /* We do not negotiate the number of slots yet, so set the
3787 * maxslots to the session maxreqs which is used to encode
3788 * sr_highest_slotid and the sr_target_slot id to maxslots */
3789 seq->maxslots = session->se_fchannel.maxreqs;
3790
J. Bruce Fields73e79482012-02-13 16:39:00 -05003791 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3792 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003793 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003794 status = nfserr_seq_misordered;
3795 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003796 goto out_put_session;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003797 status = nfserr_seq_false_retry;
3798 if (!replay_matches_cache(rqstp, seq, slot))
3799 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003800 cstate->slot = slot;
3801 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003802 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003803 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003804 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003805 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003806 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003807 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003808 }
3809 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003810 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003811
J. Bruce Fields57266a62013-04-13 14:27:29 -04003812 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003813 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003814 if (status)
3815 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003816
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003817 buflen = (seq->cachethis) ?
3818 session->se_fchannel.maxresp_cached :
3819 session->se_fchannel.maxresp_sz;
3820 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3821 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003822 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003823 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003824 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003825
3826 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003827 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003828 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003829 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003830 if (seq->cachethis)
3831 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003832 else
3833 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003834
3835 cstate->slot = slot;
3836 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003837 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003838
Benny Halevyb85d4c02009-04-03 08:28:08 +03003839out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003840 switch (clp->cl_cb_state) {
3841 case NFSD4_CB_DOWN:
3842 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3843 break;
3844 case NFSD4_CB_FAULT:
3845 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3846 break;
3847 default:
3848 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003849 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003850 if (!list_empty(&clp->cl_revoked))
3851 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003852out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08003853 if (conn)
3854 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003855 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003856 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003857out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003858 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003859 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003860}
3861
Trond Myklebustb6076642014-06-30 11:48:35 -04003862void
3863nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3864{
3865 struct nfsd4_compound_state *cs = &resp->cstate;
3866
3867 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04003868 if (cs->status != nfserr_replay_cache) {
3869 nfsd4_store_cache_entry(resp);
3870 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3871 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003872 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04003873 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003874 } else if (cs->clp)
3875 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04003876}
3877
Mi Jinlong345c2842011-10-20 17:51:39 +08003878__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003879nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3880 struct nfsd4_compound_state *cstate,
3881 union nfsd4_op_u *u)
Mi Jinlong345c2842011-10-20 17:51:39 +08003882{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003883 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003884 struct nfs4_client *conf, *unconf;
3885 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003886 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003887 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08003888
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003889 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003890 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003891 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003892 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08003893
3894 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04003895 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08003896 status = nfserr_clientid_busy;
3897 goto out;
3898 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04003899 status = mark_client_expired_locked(conf);
3900 if (status)
3901 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003902 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08003903 } else if (unconf)
3904 clp = unconf;
3905 else {
3906 status = nfserr_stale_clientid;
3907 goto out;
3908 }
Andrew Elblededeb132016-06-15 12:52:08 -04003909 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003910 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003911 status = nfserr_wrong_cred;
3912 goto out;
3913 }
Chuck Leverc41a9b72021-05-14 15:56:00 -04003914 trace_nfsd_clid_destroyed(&clp->cl_clientid);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003915 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003916out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003917 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003918 if (clp)
3919 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003920 return status;
3921}
3922
Andy Adamson069b6ad2009-04-03 08:27:58 +03003923__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003924nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3925 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003926{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003927 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05003928 struct nfs4_client *clp = cstate->clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003929 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003930
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003931 if (rc->rca_one_fs) {
3932 if (!cstate->current_fh.fh_dentry)
3933 return nfserr_nofilehandle;
3934 /*
3935 * We don't take advantage of the rca_one_fs case.
3936 * That's OK, it's optional, we can safely ignore it.
3937 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02003938 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003939 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003940
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003941 status = nfserr_complete_already;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05003942 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003943 goto out;
3944
3945 status = nfserr_stale_clientid;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05003946 if (is_client_expired(clp))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003947 /*
3948 * The following error isn't really legal.
3949 * But we only get here if the client just explicitly
3950 * destroyed the client. Surely it no longer cares what
3951 * error it gets back on an operation for the dead
3952 * client.
3953 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003954 goto out;
3955
3956 status = nfs_ok;
Chuck Levercee8aa02021-05-14 15:55:54 -04003957 trace_nfsd_clid_reclaim_complete(&clp->cl_clientid);
J. Bruce Fieldsec596592021-01-21 17:57:45 -05003958 nfsd4_client_record_create(clp);
3959 inc_reclaim_complete(clp);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003960out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003961 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003962}
3963
3964__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003965nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003966 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003968 struct nfsd4_setclientid *setclid = &u->setclientid;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003969 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003971 struct nfs4_client *conf, *new;
3972 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003973 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003974 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3975
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003976 new = create_client(clname, rqstp, &clverifier);
3977 if (new == NULL)
3978 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003979 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003980 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003981 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04003982 if (conf && client_has_state(conf)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003983 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003985 if (clp_used_exchangeid(conf))
3986 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003987 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Chuck Lever27787732021-05-14 15:55:29 -04003988 trace_nfsd_clid_cred_mismatch(conf, rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 goto out;
3990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003992 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003993 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003994 unhash_client_locked(unconf);
Chuck Lever45f56da2020-04-05 14:40:25 -04003995 /* We need to handle only case 1: probable callback update */
Chuck Lever744ea542021-05-14 15:55:36 -04003996 if (conf) {
3997 if (same_verf(&conf->cl_verifier, &clverifier)) {
3998 copy_clid(new, conf);
3999 gen_confirm(new, nn);
4000 } else
4001 trace_nfsd_clid_verf_mismatch(conf, rqstp,
4002 &clverifier);
Chuck Lever45f56da2020-04-05 14:40:25 -04004003 }
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04004004 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09004005 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05004006 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
4008 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
4009 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04004010 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 status = nfs_ok;
4012out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04004013 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04004014 if (new)
4015 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04004016 if (unconf)
4017 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 return status;
4019}
4020
4021
Al Virob37ad282006-10-19 23:28:59 -07004022__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004023nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02004024 struct nfsd4_compound_state *cstate,
4025 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026{
Christoph Hellwigeb698532017-05-08 20:58:35 +02004027 struct nfsd4_setclientid_confirm *setclientid_confirm =
4028 &u->setclientid_confirm;
NeilBrown21ab45a2005-06-23 22:04:14 -07004029 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04004030 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
4032 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07004033 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004034 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004036 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07004038
Jeff Laytond20c11d2014-07-30 08:27:07 -04004039 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004040 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004041 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05004042 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04004043 * We try hard to give out unique clientid's, so if we get an
4044 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04004045 * the client may be buggy; this should never happen.
4046 *
4047 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05004048 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04004049 status = nfserr_clid_inuse;
Chuck Lever27787732021-05-14 15:55:29 -04004050 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
4051 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
J. Bruce Fields8695b902012-05-19 10:05:58 -04004052 goto out;
Chuck Lever27787732021-05-14 15:55:29 -04004053 }
4054 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4055 trace_nfsd_clid_cred_mismatch(conf, rqstp);
J. Bruce Fields8695b902012-05-19 10:05:58 -04004056 goto out;
Chuck Lever27787732021-05-14 15:55:29 -04004057 }
J. Bruce Fields63db4632012-05-18 21:54:19 -04004058 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04004059 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04004060 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
4061 /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 status = nfs_ok;
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04004063 } else /* case 4: client hasn't noticed we rebooted yet? */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04004064 status = nfserr_stale_clientid;
4065 goto out;
4066 }
4067 status = nfs_ok;
4068 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04004069 old = unconf;
4070 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04004071 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04004072 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04004073 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4074 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04004075 status = nfserr_clid_inuse;
4076 if (client_has_state(old)
4077 && !same_creds(&unconf->cl_cred,
4078 &old->cl_cred))
4079 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04004080 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04004081 if (status) {
4082 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04004083 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04004084 }
Chuck Lever2958d2e2021-05-14 15:56:06 -04004085 trace_nfsd_clid_replaced(&old->cl_clientid);
J. Bruce Fields221a6872013-04-01 22:23:49 -04004086 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04004087 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04004088 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07004089 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04004090 get_client_locked(conf);
4091 spin_unlock(&nn->client_lock);
4092 nfsd4_probe_callback(conf);
4093 spin_lock(&nn->client_lock);
4094 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04004096 spin_unlock(&nn->client_lock);
4097 if (old)
4098 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 return status;
4100}
4101
J. Bruce Fields32513b42011-10-13 16:00:16 -04004102static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103{
J. Bruce Fields32513b42011-10-13 16:00:16 -04004104 return kmem_cache_alloc(file_slab, GFP_KERNEL);
4105}
4106
4107/* OPEN Share state helper functions */
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004108static void nfsd4_init_file(struct svc_fh *fh, unsigned int hashval,
Jeff Layton5b095e92014-10-23 08:01:02 -04004109 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04004110{
Trond Myklebust950e0112014-06-30 11:48:31 -04004111 lockdep_assert_held(&state_lock);
4112
Elena Reshetova818a34e2017-10-20 12:53:30 +03004113 refcount_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004114 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04004115 INIT_LIST_HEAD(&fp->fi_stateids);
4116 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004117 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004118 fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle);
Jeff Layton0c637be2014-08-22 12:05:43 -04004119 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004120 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004121 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004122 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4123 memset(fp->fi_access, 0, sizeof(fp->fi_access));
J. Bruce Fieldsa0ce4832021-04-16 14:00:16 -04004124 fp->fi_aliased = false;
4125 fp->fi_inode = d_inode(fh->fh_dentry);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004126#ifdef CONFIG_NFSD_PNFS
4127 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02004128 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004129#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04004130 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131}
4132
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004133void
NeilBrowne60d4392005-06-23 22:03:01 -07004134nfsd4_free_slabs(void)
4135{
Jeff Layton9258a2d2018-03-16 09:47:22 -04004136 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004137 kmem_cache_destroy(openowner_slab);
4138 kmem_cache_destroy(lockowner_slab);
4139 kmem_cache_destroy(file_slab);
4140 kmem_cache_destroy(stateid_slab);
4141 kmem_cache_destroy(deleg_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04004142 kmem_cache_destroy(odstate_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07004143}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144
Bryan Schumaker72083392011-11-01 15:24:59 -04004145int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146nfsd4_init_slabs(void)
4147{
Jeff Layton9258a2d2018-03-16 09:47:22 -04004148 client_slab = kmem_cache_create("nfsd4_clients",
4149 sizeof(struct nfs4_client), 0, 0, NULL);
4150 if (client_slab == NULL)
4151 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004152 openowner_slab = kmem_cache_create("nfsd4_openowners",
4153 sizeof(struct nfs4_openowner), 0, 0, NULL);
4154 if (openowner_slab == NULL)
Jeff Layton9258a2d2018-03-16 09:47:22 -04004155 goto out_free_client_slab;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004156 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08004157 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004158 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004159 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07004160 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09004161 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07004162 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004163 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07004164 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004165 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07004166 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004167 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07004168 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09004169 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07004170 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004171 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004172 odstate_slab = kmem_cache_create("nfsd4_odstate",
4173 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4174 if (odstate_slab == NULL)
4175 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07004177
Sachin Bhamare8287f002015-04-27 14:50:14 +02004178out_free_deleg_slab:
4179 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004180out_free_stateid_slab:
4181 kmem_cache_destroy(stateid_slab);
4182out_free_file_slab:
4183 kmem_cache_destroy(file_slab);
4184out_free_lockowner_slab:
4185 kmem_cache_destroy(lockowner_slab);
4186out_free_openowner_slab:
4187 kmem_cache_destroy(openowner_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04004188out_free_client_slab:
4189 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004190out:
NeilBrowne60d4392005-06-23 22:03:01 -07004191 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192}
4193
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004194static void init_nfs4_replay(struct nfs4_replay *rp)
4195{
4196 rp->rp_status = nfserr_serverfault;
4197 rp->rp_buflen = 0;
4198 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04004199 mutex_init(&rp->rp_mutex);
4200}
4201
4202static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4203 struct nfs4_stateowner *so)
4204{
4205 if (!nfsd4_has_session(cstate)) {
4206 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004207 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004208 }
4209}
4210
4211void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4212{
4213 struct nfs4_stateowner *so = cstate->replay_owner;
4214
4215 if (so != NULL) {
4216 cstate->replay_owner = NULL;
4217 mutex_unlock(&so->so_replay.rp_mutex);
4218 nfs4_put_stateowner(so);
4219 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004220}
4221
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004222static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223{
4224 struct nfs4_stateowner *sop;
4225
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004226 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004227 if (!sop)
4228 return NULL;
4229
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04004230 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004231 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004232 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004233 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004235
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004236 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004237 sop->so_client = clp;
4238 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04004239 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004240 return sop;
4241}
4242
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004243static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004244{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004245 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004246
Trond Myklebustd4f04892014-07-29 21:34:36 -04004247 list_add(&oo->oo_owner.so_strhash,
4248 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004249 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250}
4251
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004252static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4253{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004254 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004255}
4256
Jeff Layton6b180f02014-07-29 21:34:26 -04004257static void nfs4_free_openowner(struct nfs4_stateowner *so)
4258{
4259 struct nfs4_openowner *oo = openowner(so);
4260
4261 kmem_cache_free(openowner_slab, oo);
4262}
4263
4264static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004265 .so_unhash = nfs4_unhash_openowner,
4266 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004267};
4268
Andrew Elble7fc05642015-11-05 20:42:43 -05004269static struct nfs4_ol_stateid *
4270nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4271{
4272 struct nfs4_ol_stateid *local, *ret = NULL;
4273 struct nfs4_openowner *oo = open->op_openowner;
4274
4275 lockdep_assert_held(&fp->fi_lock);
4276
4277 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4278 /* ignore lock owners */
4279 if (local->st_stateowner->so_is_open_owner == 0)
4280 continue;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004281 if (local->st_stateowner != &oo->oo_owner)
4282 continue;
4283 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
Andrew Elble7fc05642015-11-05 20:42:43 -05004284 ret = local;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004285 refcount_inc(&ret->st_stid.sc_count);
Andrew Elble7fc05642015-11-05 20:42:43 -05004286 break;
4287 }
4288 }
4289 return ret;
4290}
4291
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004292static __be32
4293nfsd4_verify_open_stid(struct nfs4_stid *s)
4294{
4295 __be32 ret = nfs_ok;
4296
4297 switch (s->sc_type) {
4298 default:
4299 break;
Trond Myklebust4f176412018-01-12 17:42:30 -05004300 case 0:
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004301 case NFS4_CLOSED_STID:
4302 case NFS4_CLOSED_DELEG_STID:
4303 ret = nfserr_bad_stateid;
4304 break;
4305 case NFS4_REVOKED_DELEG_STID:
4306 ret = nfserr_deleg_revoked;
4307 }
4308 return ret;
4309}
4310
4311/* Lock the stateid st_mutex, and deal with races with CLOSE */
4312static __be32
4313nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4314{
4315 __be32 ret;
4316
Andrew Elble4f34bd02017-11-08 17:29:51 -05004317 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004318 ret = nfsd4_verify_open_stid(&stp->st_stid);
4319 if (ret != nfs_ok)
4320 mutex_unlock(&stp->st_mutex);
4321 return ret;
4322}
4323
4324static struct nfs4_ol_stateid *
4325nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4326{
4327 struct nfs4_ol_stateid *stp;
4328 for (;;) {
4329 spin_lock(&fp->fi_lock);
4330 stp = nfsd4_find_existing_open(fp, open);
4331 spin_unlock(&fp->fi_lock);
4332 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4333 break;
4334 nfs4_put_stid(&stp->st_stid);
4335 }
4336 return stp;
4337}
4338
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004339static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04004340alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004341 struct nfsd4_compound_state *cstate)
4342{
Trond Myklebust13d6f662014-06-30 11:48:45 -04004343 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004344 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004346 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4347 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04004349 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004350 oo->oo_owner.so_is_open_owner = 1;
4351 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004352 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004353 if (nfsd4_has_session(cstate))
4354 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004355 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004356 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004357 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04004358 spin_lock(&clp->cl_lock);
4359 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004360 if (ret == NULL) {
4361 hash_openowner(oo, clp, strhashval);
4362 ret = oo;
4363 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08004364 nfs4_free_stateowner(&oo->oo_owner);
4365
Trond Myklebustd4f04892014-07-29 21:34:36 -04004366 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04004367 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368}
4369
Andrew Elble7fc05642015-11-05 20:42:43 -05004370static struct nfs4_ol_stateid *
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004371init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
Andrew Elble7fc05642015-11-05 20:42:43 -05004372{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373
Andrew Elble7fc05642015-11-05 20:42:43 -05004374 struct nfs4_openowner *oo = open->op_openowner;
4375 struct nfs4_ol_stateid *retstp = NULL;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004376 struct nfs4_ol_stateid *stp;
Andrew Elble7fc05642015-11-05 20:42:43 -05004377
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004378 stp = open->op_stp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004379 /* We are moving these outside of the spinlocks to avoid the warnings */
4380 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05004381 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004382
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004383retry:
Andrew Elble7fc05642015-11-05 20:42:43 -05004384 spin_lock(&oo->oo_owner.so_client->cl_lock);
4385 spin_lock(&fp->fi_lock);
4386
4387 retstp = nfsd4_find_existing_open(fp, open);
4388 if (retstp)
4389 goto out_unlock;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004390
4391 open->op_stp = NULL;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004392 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004393 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004394 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004395 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07004396 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004397 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 stp->st_access_bmap = 0;
4399 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07004400 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04004401 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004402 list_add(&stp->st_perfile, &fp->fi_stateids);
Andrew Elble7fc05642015-11-05 20:42:43 -05004403
4404out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04004405 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004406 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004407 if (retstp) {
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004408 /* Handle races with CLOSE */
4409 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4410 nfs4_put_stid(&retstp->st_stid);
4411 goto retry;
4412 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004413 /* To keep mutex tracking happy */
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004414 mutex_unlock(&stp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004415 stp = retstp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004416 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004417 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418}
4419
Jeff Laytond3134b12014-07-29 21:34:32 -04004420/*
4421 * In the 4.0 case we need to keep the owners around a little while to handle
4422 * CLOSE replay. We still do need to release any file access that is held by
4423 * them before returning however.
4424 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425static void
Jeff Laytond3134b12014-07-29 21:34:32 -04004426move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427{
Jeff Layton217526e2014-07-30 08:27:11 -04004428 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04004429 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4430 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4431 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004432
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004433 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
Jeff Laytonb401be222014-07-29 21:34:33 -04004435 /*
4436 * We know that we hold one reference via nfsd4_close, and another
4437 * "persistent" reference for the client. If the refcount is higher
4438 * than 2, then there are still calls in progress that are using this
4439 * stateid. We can't put the sc_file reference until they are finished.
4440 * Wait for the refcount to drop to 2. Since it has been unhashed,
4441 * there should be no danger of the refcount going back up again at
4442 * this point.
4443 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004444 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
Jeff Laytonb401be222014-07-29 21:34:33 -04004445
Jeff Laytond3134b12014-07-29 21:34:32 -04004446 release_all_access(s);
4447 if (s->st_stid.sc_file) {
4448 put_nfs4_file(s->st_stid.sc_file);
4449 s->st_stid.sc_file = NULL;
4450 }
Jeff Layton217526e2014-07-30 08:27:11 -04004451
4452 spin_lock(&nn->client_lock);
4453 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004454 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004455 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01004456 oo->oo_time = ktime_get_boottime_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04004457 spin_unlock(&nn->client_lock);
4458 if (last)
4459 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460}
4461
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462/* search file_hashtbl[] for file */
4463static struct nfs4_file *
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004464find_file_locked(struct svc_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 struct nfs4_file *fp;
4467
Madhuparna Bhowmik36a80492020-02-13 20:33:31 +05304468 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4469 lockdep_is_held(&state_lock)) {
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004470 if (fh_match(&fp->fi_fhandle, &fh->fh_handle)) {
Elena Reshetova818a34e2017-10-20 12:53:30 +03004471 if (refcount_inc_not_zero(&fp->fi_ref))
Jeff Layton5b095e92014-10-23 08:01:02 -04004472 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07004473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 }
4475 return NULL;
4476}
4477
J. Bruce Fieldsa0ce4832021-04-16 14:00:16 -04004478static struct nfs4_file *insert_file(struct nfs4_file *new, struct svc_fh *fh,
4479 unsigned int hashval)
4480{
4481 struct nfs4_file *fp;
4482 struct nfs4_file *ret = NULL;
4483 bool alias_found = false;
4484
4485 spin_lock(&state_lock);
4486 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4487 lockdep_is_held(&state_lock)) {
4488 if (fh_match(&fp->fi_fhandle, &fh->fh_handle)) {
4489 if (refcount_inc_not_zero(&fp->fi_ref))
4490 ret = fp;
4491 } else if (d_inode(fh->fh_dentry) == fp->fi_inode)
4492 fp->fi_aliased = alias_found = true;
4493 }
4494 if (likely(ret == NULL)) {
4495 nfsd4_init_file(fh, hashval, new);
4496 new->fi_aliased = alias_found;
4497 ret = new;
4498 }
4499 spin_unlock(&state_lock);
4500 return ret;
4501}
4502
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004503static struct nfs4_file * find_file(struct svc_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004504{
4505 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004506 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04004507
Jeff Layton5b095e92014-10-23 08:01:02 -04004508 rcu_read_lock();
4509 fp = find_file_locked(fh, hashval);
4510 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04004511 return fp;
4512}
4513
4514static struct nfs4_file *
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004515find_or_add_file(struct nfs4_file *new, struct svc_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004516{
4517 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004518 unsigned int hashval = file_hashval(fh);
4519
4520 rcu_read_lock();
4521 fp = find_file_locked(fh, hashval);
4522 rcu_read_unlock();
4523 if (fp)
4524 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04004525
J. Bruce Fieldsa0ce4832021-04-16 14:00:16 -04004526 return insert_file(new, fh, hashval);
Trond Myklebust950e0112014-06-30 11:48:31 -04004527}
4528
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04004529/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 * Called to check deny when READ with all zero stateid or
4531 * WRITE with all zero or all one stateid
4532 */
Al Virob37ad282006-10-19 23:28:59 -07004533static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4535{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004537 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004539 fp = find_file(current_fh);
NeilBrown13cd2182005-06-23 22:03:10 -07004540 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004541 return ret;
4542 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04004543 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004544 if (fp->fi_share_deny & deny_type)
4545 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04004546 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07004547 put_nfs4_file(fp);
4548 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549}
4550
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004551static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004553 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04004554 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4555 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004556
Trond Myklebust11b91642014-07-29 21:34:08 -04004557 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04004558
Jeff Layton02e12152014-07-16 10:31:57 -04004559 /*
4560 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04004561 * already holding inode->i_lock.
4562 *
Jeff Laytondff13992014-07-08 14:02:49 -04004563 * If the dl_time != 0, then we know that it has already been
4564 * queued for a lease break. Don't queue it again.
4565 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04004566 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04004567 if (dp->dl_time == 0) {
Arnd Bergmann20b7d862019-11-04 16:31:52 +01004568 dp->dl_time = ktime_get_boottime_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04004569 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04004570 }
Jeff Layton02e12152014-07-16 10:31:57 -04004571 spin_unlock(&state_lock);
4572}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004574static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4575 struct rpc_task *task)
4576{
4577 struct nfs4_delegation *dp = cb_to_delegation(cb);
4578
J. Bruce Fields12ed22f2020-07-31 16:20:05 -04004579 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID ||
4580 dp->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID)
Andrew Elblea4579742015-08-31 12:06:41 -04004581 return 1;
4582
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004583 switch (task->tk_status) {
4584 case 0:
4585 return 1;
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004586 case -NFS4ERR_DELAY:
4587 rpc_delay(task, 2 * HZ);
4588 return 0;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004589 case -EBADHANDLE:
4590 case -NFS4ERR_BAD_STATEID:
4591 /*
4592 * Race: client probably got cb_recall before open reply
4593 * granting delegation.
4594 */
4595 if (dp->dl_retries--) {
4596 rpc_delay(task, 2 * HZ);
4597 return 0;
4598 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004599 fallthrough;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004600 default:
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004601 return 1;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004602 }
4603}
4604
4605static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4606{
4607 struct nfs4_delegation *dp = cb_to_delegation(cb);
4608
4609 nfs4_put_stid(&dp->dl_stid);
4610}
4611
Julia Lawallc4cb8972015-11-21 22:57:39 +01004612static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004613 .prepare = nfsd4_cb_recall_prepare,
4614 .done = nfsd4_cb_recall_done,
4615 .release = nfsd4_cb_recall_release,
4616};
4617
Jeff Layton02e12152014-07-16 10:31:57 -04004618static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4619{
4620 /*
4621 * We're assuming the state code never drops its reference
4622 * without first removing the lease. Since we're in this lease
J. Bruce Fields4a269efb2018-04-13 17:34:35 -04004623 * callback (and since the lease code is serialized by the
4624 * i_lock) we know the server hasn't removed the lease yet, and
4625 * we know it's safe to take a reference.
Jeff Layton02e12152014-07-16 10:31:57 -04004626 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004627 refcount_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02004628 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004629}
4630
Jeff Layton1c8c6012013-06-21 08:58:15 -04004631/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004632static bool
4633nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004634{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004635 bool ret = false;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004636 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4637 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004638
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04004639 trace_nfsd_deleg_break(&dp->dl_stid.sc_stateid);
4640
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004641 /*
4642 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004643 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004644 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004645 */
4646 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647
Jeff Layton02e12152014-07-16 10:31:57 -04004648 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004649 fp->fi_had_conflict = true;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004650 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04004651 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004652 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653}
4654
J. Bruce Fields28df3d12017-07-28 16:35:15 -04004655static bool nfsd_breaker_owns_lease(struct file_lock *fl)
4656{
4657 struct nfs4_delegation *dl = fl->fl_owner;
4658 struct svc_rqst *rqst;
4659 struct nfs4_client *clp;
4660
4661 if (!i_am_nfsd())
4662 return NULL;
4663 rqst = kthread_data(current);
J. Bruce Fields13956162020-09-25 10:12:39 -04004664 /* Note rq_prog == NFS_ACL_PROGRAM is also possible: */
4665 if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4)
J. Bruce Fields34b09af2020-08-05 15:10:11 -04004666 return NULL;
J. Bruce Fields28df3d12017-07-28 16:35:15 -04004667 clp = *(rqst->rq_lease_breaker);
4668 return dl->dl_stid.sc_client == clp;
4669}
4670
Jeff Laytonc45198e2014-09-01 07:12:07 -04004671static int
Jeff Layton7448cc32015-01-16 15:05:57 -05004672nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4673 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674{
4675 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04004676 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 else
4678 return -EAGAIN;
4679}
4680
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004681static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields28df3d12017-07-28 16:35:15 -04004682 .lm_breaker_owns_lease = nfsd_breaker_owns_lease,
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04004683 .lm_break = nfsd_break_deleg_cb,
4684 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685};
4686
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004687static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4688{
4689 if (nfsd4_has_session(cstate))
4690 return nfs_ok;
4691 if (seqid == so->so_seqid - 1)
4692 return nfserr_replay_me;
4693 if (seqid == so->so_seqid)
4694 return nfs_ok;
4695 return nfserr_bad_seqid;
4696}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697
J. Bruce Fields7950b532021-01-21 17:57:41 -05004698static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions,
4699 struct nfsd_net *nn)
4700{
4701 struct nfs4_client *found;
4702
4703 spin_lock(&nn->client_lock);
4704 found = find_confirmed_client(clid, sessions, nn);
4705 if (found)
4706 atomic_inc(&found->cl_rpc_users);
4707 spin_unlock(&nn->client_lock);
4708 return found;
4709}
4710
J. Bruce Fields460d2702021-01-21 17:57:40 -05004711static __be32 set_client(clientid_t *clid,
Jeff Layton4b24ca72014-06-30 11:48:44 -04004712 struct nfsd4_compound_state *cstate,
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004713 struct nfsd_net *nn)
Jeff Layton4b24ca72014-06-30 11:48:44 -04004714{
Jeff Layton4b24ca72014-06-30 11:48:44 -04004715 if (cstate->clp) {
J. Bruce Fields7950b532021-01-21 17:57:41 -05004716 if (!same_clid(&cstate->clp->cl_clientid, clid))
Jeff Layton4b24ca72014-06-30 11:48:44 -04004717 return nfserr_stale_clientid;
4718 return nfs_ok;
4719 }
Jeff Layton4b24ca72014-06-30 11:48:44 -04004720 if (STALE_CLIENTID(clid, nn))
4721 return nfserr_stale_clientid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004722 /*
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004723 * We're in the 4.0 case (otherwise the SEQUENCE op would have
4724 * set cstate->clp), so session = false:
Jeff Layton4b24ca72014-06-30 11:48:44 -04004725 */
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004726 cstate->clp = lookup_clientid(clid, false, nn);
J. Bruce Fields7950b532021-01-21 17:57:41 -05004727 if (!cstate->clp)
Jeff Layton4b24ca72014-06-30 11:48:44 -04004728 return nfserr_expired;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004729 return nfs_ok;
4730}
4731
Al Virob37ad282006-10-19 23:28:59 -07004732__be32
Andy Adamson66689582009-04-03 08:28:45 +03004733nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004734 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 clientid_t *clientid = &open->op_clientid;
4737 struct nfs4_client *clp = NULL;
4738 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004739 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004740 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741
J. Bruce Fields32513b42011-10-13 16:00:16 -04004742 /*
4743 * In case we need it later, after we've already created the
4744 * file and don't want to risk a further failure:
4745 */
4746 open->op_file = nfsd4_alloc_file();
4747 if (open->op_file == NULL)
4748 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004750 status = set_client(clientid, cstate, nn);
Trond Myklebust2d91e892014-06-30 11:48:46 -04004751 if (status)
4752 return status;
4753 clp = cstate->clp;
4754
Trond Myklebustd4f04892014-07-29 21:34:36 -04004755 strhashval = ownerstr_hashval(&open->op_owner);
4756 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004757 open->op_openowner = oo;
4758 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004759 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004761 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004762 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004763 release_openowner(oo);
4764 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004765 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004766 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004767 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4768 if (status)
4769 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004770 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004771new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04004772 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004773 if (oo == NULL)
4774 return nfserr_jukebox;
4775 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004776alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04004777 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004778 if (!open->op_stp)
4779 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004780
4781 if (nfsd4_has_session(cstate) &&
4782 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4783 open->op_odstate = alloc_clnt_odstate(clp);
4784 if (!open->op_odstate)
4785 return nfserr_jukebox;
4786 }
4787
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004788 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789}
4790
Al Virob37ad282006-10-19 23:28:59 -07004791static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07004792nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4793{
4794 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4795 return nfserr_openmode;
4796 else
4797 return nfs_ok;
4798}
4799
Daniel Mackc47d8322011-05-16 16:38:14 +02004800static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04004801{
J. Bruce Fields24a01112010-05-18 20:01:35 -04004802 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4803}
4804
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004805static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004806{
4807 struct nfs4_stid *ret;
4808
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004809 ret = find_stateid_by_type(cl, s,
4810 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004811 if (!ret)
4812 return NULL;
4813 return delegstateid(ret);
4814}
4815
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004816static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4817{
4818 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4819 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4820}
4821
Al Virob37ad282006-10-19 23:28:59 -07004822static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04004823nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07004824 struct nfs4_delegation **dp)
4825{
4826 int flags;
Al Virob37ad282006-10-19 23:28:59 -07004827 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004828 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07004829
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004830 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4831 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07004832 goto out;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004833 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4834 nfs4_put_stid(&deleg->dl_stid);
4835 if (cl->cl_minorversion)
4836 status = nfserr_deleg_revoked;
4837 goto out;
4838 }
J. Bruce Fields24a01112010-05-18 20:01:35 -04004839 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004840 status = nfs4_check_delegmode(deleg, flags);
4841 if (status) {
4842 nfs4_put_stid(&deleg->dl_stid);
4843 goto out;
4844 }
4845 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004846out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004847 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004848 return nfs_ok;
4849 if (status)
4850 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004851 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004852 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07004853}
4854
J. Bruce Fields21fb4012010-07-28 12:21:23 -04004855static inline int nfs4_access_to_access(u32 nfs4_access)
4856{
4857 int flags = 0;
4858
4859 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4860 flags |= NFSD_MAY_READ;
4861 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4862 flags |= NFSD_MAY_WRITE;
4863 return flags;
4864}
4865
Al Virob37ad282006-10-19 23:28:59 -07004866static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4868 struct nfsd4_open *open)
4869{
4870 struct iattr iattr = {
4871 .ia_valid = ATTR_SIZE,
4872 .ia_size = 0,
4873 };
4874 if (!open->op_truncate)
4875 return 0;
4876 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08004877 return nfserr_inval;
Arnd Bergmann2a1aa482019-11-03 17:50:18 +01004878 return nfsd_setattr(rqstp, fh, &iattr, 0, (time64_t)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879}
4880
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004881static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004882 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4883 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004884{
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004885 struct nfsd_file *nf = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004886 __be32 status;
4887 int oflag = nfs4_access_to_omode(open->op_share_access);
4888 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004889 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004890
Trond Myklebustde186432014-07-10 14:07:26 -04004891 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004892
4893 /*
4894 * Are we trying to set a deny mode that would conflict with
4895 * current access?
4896 */
4897 status = nfs4_file_check_deny(fp, open->op_share_deny);
4898 if (status != nfs_ok) {
4899 spin_unlock(&fp->fi_lock);
4900 goto out;
4901 }
4902
4903 /* set access to the file */
4904 status = nfs4_file_get_access(fp, open->op_share_access);
4905 if (status != nfs_ok) {
4906 spin_unlock(&fp->fi_lock);
4907 goto out;
4908 }
4909
4910 /* Set access bits in stateid */
4911 old_access_bmap = stp->st_access_bmap;
4912 set_access(open->op_share_access, stp);
4913
4914 /* Set new deny mask */
4915 old_deny_bmap = stp->st_deny_bmap;
4916 set_deny(open->op_share_deny, stp);
4917 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4918
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004919 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04004920 spin_unlock(&fp->fi_lock);
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004921 status = nfsd_file_acquire(rqstp, cur_fh, access, &nf);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004922 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004923 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04004924 spin_lock(&fp->fi_lock);
4925 if (!fp->fi_fds[oflag]) {
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004926 fp->fi_fds[oflag] = nf;
4927 nf = NULL;
Trond Myklebustde186432014-07-10 14:07:26 -04004928 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004929 }
Trond Myklebustde186432014-07-10 14:07:26 -04004930 spin_unlock(&fp->fi_lock);
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004931 if (nf)
4932 nfsd_file_put(nf);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004933
J. Bruce Fields217fd6f2021-04-16 14:00:14 -04004934 status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode,
4935 access));
4936 if (status)
4937 goto out_put_access;
4938
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004939 status = nfsd4_truncate(rqstp, cur_fh, open);
4940 if (status)
4941 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004942out:
4943 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004944out_put_access:
4945 stp->st_access_bmap = old_access_bmap;
4946 nfs4_file_put_access(fp, open->op_share_access);
4947 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4948 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004949}
4950
Al Virob37ad282006-10-19 23:28:59 -07004951static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004952nfs4_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 -07004953{
Al Virob37ad282006-10-19 23:28:59 -07004954 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02004955 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004957 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004958 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004959
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004960 /* test and set deny mode */
4961 spin_lock(&fp->fi_lock);
4962 status = nfs4_file_check_deny(fp, open->op_share_deny);
4963 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004964 set_deny(open->op_share_deny, stp);
4965 fp->fi_share_deny |=
4966 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4967 }
4968 spin_unlock(&fp->fi_lock);
4969
4970 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004973 status = nfsd4_truncate(rqstp, cur_fh, open);
4974 if (status != nfs_ok)
4975 reset_union_bmap_deny(old_deny_bmap, stp);
4976 return status;
4977}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004979/* Should we give out recallable state?: */
4980static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4981{
4982 if (clp->cl_cb_state == NFSD4_CB_UP)
4983 return true;
4984 /*
4985 * In the sessions case, since we don't have to establish a
4986 * separate connection for callbacks, we assume it's OK
4987 * until we hear otherwise:
4988 */
4989 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4990}
4991
J. Bruce Fields653e5142018-02-15 22:08:45 -05004992static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4993 int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004994{
4995 struct file_lock *fl;
4996
4997 fl = locks_alloc_lock();
4998 if (!fl)
4999 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05005000 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04005001 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05005002 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
5003 fl->fl_end = OFFSET_MAX;
J. Bruce Fields653e5142018-02-15 22:08:45 -05005004 fl->fl_owner = (fl_owner_t)dp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05005005 fl->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005006 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05005007 return fl;
5008}
5009
J. Bruce Fieldsaba20722021-04-16 14:00:18 -04005010static int nfsd4_check_conflicting_opens(struct nfs4_client *clp,
5011 struct nfs4_file *fp)
5012{
5013 struct nfs4_ol_stateid *st;
5014 struct file *f = fp->fi_deleg_file->nf_file;
5015 struct inode *ino = locks_inode(f);
5016 int writes;
5017
5018 writes = atomic_read(&ino->i_writecount);
5019 if (!writes)
5020 return 0;
5021 /*
5022 * There could be multiple filehandles (hence multiple
5023 * nfs4_files) referencing this file, but that's not too
5024 * common; let's just give up in that case rather than
5025 * trying to go look up all the clients using that other
5026 * nfs4_file as well:
5027 */
5028 if (fp->fi_aliased)
5029 return -EAGAIN;
5030 /*
5031 * If there's a close in progress, make sure that we see it
5032 * clear any fi_fds[] entries before we see it decrement
5033 * i_writecount:
5034 */
5035 smp_mb__after_atomic();
5036
5037 if (fp->fi_fds[O_WRONLY])
5038 writes--;
5039 if (fp->fi_fds[O_RDWR])
5040 writes--;
5041 if (writes > 0)
5042 return -EAGAIN; /* There may be non-NFSv4 writers */
5043 /*
5044 * It's possible there are non-NFSv4 write opens in progress,
5045 * but if they haven't incremented i_writecount yet then they
5046 * also haven't called break lease yet; so, they'll break this
5047 * lease soon enough. So, all that's left to check for is NFSv4
5048 * opens:
5049 */
5050 spin_lock(&fp->fi_lock);
5051 list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
5052 if (st->st_openstp == NULL /* it's an open */ &&
5053 access_permit_write(st) &&
5054 st->st_stid.sc_client != clp) {
5055 spin_unlock(&fp->fi_lock);
5056 return -EAGAIN;
5057 }
5058 }
5059 spin_unlock(&fp->fi_lock);
5060 /*
5061 * There's a small chance that we could be racing with another
5062 * NFSv4 open. However, any open that hasn't added itself to
5063 * the fi_stateids list also hasn't called break_lease yet; so,
5064 * they'll break this lease soon enough.
5065 */
5066 return 0;
5067}
5068
Jeff Layton0b266932014-07-25 07:34:25 -04005069static struct nfs4_delegation *
5070nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02005071 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05005072{
J. Bruce Fields68b18f52018-02-19 11:38:33 -05005073 int status = 0;
Jeff Layton0b266932014-07-25 07:34:25 -04005074 struct nfs4_delegation *dp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005075 struct nfsd_file *nf;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005076 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04005077
J. Bruce Fields353601e2018-02-16 14:29:42 -05005078 /*
5079 * The fi_had_conflict and nfs_get_existing_delegation checks
5080 * here are just optimizations; we'll need to recheck them at
5081 * the end:
5082 */
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04005083 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04005084 return ERR_PTR(-EAGAIN);
5085
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005086 nf = find_readable_file(fp);
5087 if (!nf) {
J. Bruce Fieldsaba20722021-04-16 14:00:18 -04005088 /*
5089 * We probably could attempt another open and get a read
5090 * delegation, but for now, don't bother until the
5091 * client actually sends us one.
5092 */
5093 return ERR_PTR(-EAGAIN);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005094 }
Andrew Elble34ed9872015-10-15 12:07:28 -04005095 spin_lock(&state_lock);
5096 spin_lock(&fp->fi_lock);
J. Bruce Fields68b18f52018-02-19 11:38:33 -05005097 if (nfs4_delegation_exists(clp, fp))
5098 status = -EAGAIN;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005099 else if (!fp->fi_deleg_file) {
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005100 fp->fi_deleg_file = nf;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005101 /* increment early to prevent fi_deleg_file from being
5102 * cleared */
5103 fp->fi_delegees = 1;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005104 nf = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005105 } else
5106 fp->fi_delegees++;
5107 spin_unlock(&fp->fi_lock);
5108 spin_unlock(&state_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005109 if (nf)
5110 nfsd_file_put(nf);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005111 if (status)
5112 return ERR_PTR(status);
5113
5114 status = -ENOMEM;
5115 dp = alloc_init_deleg(clp, fp, fh, odstate);
5116 if (!dp)
5117 goto out_delegees;
5118
5119 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
5120 if (!fl)
Andrew Elblebd8d7252018-10-05 09:32:08 -04005121 goto out_clnt_odstate;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005122
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005123 status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005124 if (fl)
5125 locks_free_lock(fl);
5126 if (status)
5127 goto out_clnt_odstate;
J. Bruce Fieldsaba20722021-04-16 14:00:18 -04005128 status = nfsd4_check_conflicting_opens(clp, fp);
5129 if (status)
5130 goto out_unlock;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005131
5132 spin_lock(&state_lock);
5133 spin_lock(&fp->fi_lock);
5134 if (fp->fi_had_conflict)
5135 status = -EAGAIN;
5136 else
5137 status = hash_delegation_locked(dp, fp);
Andrew Elble34ed9872015-10-15 12:07:28 -04005138 spin_unlock(&fp->fi_lock);
5139 spin_unlock(&state_lock);
5140
5141 if (status)
Andrew Elble692ad282018-04-18 17:04:37 -04005142 goto out_unlock;
5143
Jeff Layton0b266932014-07-25 07:34:25 -04005144 return dp;
Andrew Elble692ad282018-04-18 17:04:37 -04005145out_unlock:
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005146 vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005147out_clnt_odstate:
5148 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005149 nfs4_put_stid(&dp->dl_stid);
5150out_delegees:
5151 put_deleg_file(fp);
5152 return ERR_PTR(status);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05005153}
5154
Benny Halevy4aa89132012-02-21 14:16:44 -08005155static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
5156{
5157 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5158 if (status == -EAGAIN)
5159 open->op_why_no_deleg = WND4_CONTENTION;
5160 else {
5161 open->op_why_no_deleg = WND4_RESOURCE;
5162 switch (open->op_deleg_want) {
5163 case NFS4_SHARE_WANT_READ_DELEG:
5164 case NFS4_SHARE_WANT_WRITE_DELEG:
5165 case NFS4_SHARE_WANT_ANY_DELEG:
5166 break;
5167 case NFS4_SHARE_WANT_CANCEL:
5168 open->op_why_no_deleg = WND4_CANCELLED;
5169 break;
5170 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005171 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08005172 }
5173 }
5174}
5175
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176/*
5177 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04005178 *
5179 * Note we don't support write delegations, and won't until the vfs has
5180 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 */
5182static void
Jeff Layton4cf59222014-07-25 07:34:24 -04005183nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
5184 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185{
5186 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04005187 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
5188 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05005189 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04005190 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005192 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07005193 open->op_recall = 0;
5194 switch (open->op_claim_type) {
5195 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05005196 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07005197 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04005198 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
5199 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005200 break;
5201 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00005202 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005203 /*
5204 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005205 * had the chance to reclaim theirs, *and* until
5206 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005207 */
Jeff Layton4cf59222014-07-25 07:34:24 -04005208 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04005209 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04005210 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04005211 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005212 break;
5213 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005214 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005215 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02005216 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04005217 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005218 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04005220 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221
Hou Tao3caf9172020-08-28 15:02:55 +08005222 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
J. Bruce Fields99c41512013-05-21 16:21:25 -04005223 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04005224 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005225 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005226out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005227 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
5228 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04005229 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04005230 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04005231 open->op_recall = 1;
5232 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04005233
5234 /* 4.1 client asking for a delegation? */
5235 if (open->op_deleg_want)
5236 nfsd4_open_deleg_none_ext(open, status);
5237 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238}
5239
Benny Halevye27f49c2012-02-21 14:16:54 -08005240static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5241 struct nfs4_delegation *dp)
5242{
5243 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5244 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5245 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5246 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5247 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5248 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5249 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5250 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5251 }
5252 /* Otherwise the client must be confused wanting a delegation
5253 * it already has, therefore we don't return
5254 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5255 */
5256}
5257
Al Virob37ad282006-10-19 23:28:59 -07005258__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5260{
Andy Adamson66689582009-04-03 08:28:45 +03005261 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005262 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005264 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07005265 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005266 __be32 status;
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005267 bool new_stp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 /*
5270 * Lookup file; if found, lookup stateid and check open request,
5271 * and check for delegations in the process of being recalled.
5272 * If not found, create the nfs4_file struct
5273 */
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04005274 fp = find_or_add_file(open->op_file, current_fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04005275 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04005276 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07005277 if (status)
5278 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005279 stp = nfsd4_find_and_lock_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04005281 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07005282 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04005283 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07005284 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 }
5286
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005287 if (!stp) {
5288 stp = init_open_stateid(fp, open);
5289 if (!open->op_stp)
5290 new_stp = true;
5291 }
5292
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 /*
5294 * OPEN the file, or upgrade an existing OPEN.
5295 * If truncate fails, the OPEN fails.
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005296 *
5297 * stp is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 */
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005299 if (!new_stp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005301 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005302 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005303 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 } else {
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005307 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
5308 if (status) {
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005309 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005310 release_open_stateid(stp);
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005311 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005312 goto out;
5313 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02005314
5315 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5316 open->op_odstate);
5317 if (stp->st_clnt_odstate == open->op_odstate)
5318 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 }
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005320
Jeff Layton9767feb2015-10-01 09:05:50 -04005321 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005322 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323
Benny Halevyd24433c2012-02-16 20:57:17 +02005324 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02005325 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5326 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5327 open->op_why_no_deleg = WND4_NOT_WANTED;
5328 goto nodeleg;
5329 }
5330 }
5331
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 /*
5333 * Attempt to hand out a delegation. No error return, because the
5334 * OPEN succeeds even if we fail.
5335 */
Jeff Layton4cf59222014-07-25 07:34:24 -04005336 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005337nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 status = nfs_ok;
Hou Tao3caf9172020-08-28 15:02:55 +08005339 trace_nfsd_open(&stp->st_stid.sc_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340out:
Benny Halevyd24433c2012-02-16 20:57:17 +02005341 /* 4.1 client trying to upgrade/downgrade delegation? */
5342 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08005343 open->op_deleg_want)
5344 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005345
NeilBrown13cd2182005-06-23 22:03:10 -07005346 if (fp)
5347 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07005348 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08005349 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 /*
5351 * To finish the open response, we just need to set the rflags.
5352 */
5353 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Jeff Layton19e4c342016-09-16 16:28:27 -04005354 if (nfsd4_has_session(&resp->cstate))
5355 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5356 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Jeff Layton19e4c342016-09-16 16:28:27 -04005358
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04005359 if (dp)
5360 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04005361 if (stp)
5362 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363
5364 return status;
5365}
5366
Jeff Layton58fb12e2014-07-29 21:34:27 -04005367void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04005368 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005369{
5370 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04005371 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005372
Jeff Laytond3134b12014-07-29 21:34:32 -04005373 nfsd4_cstate_assign_replay(cstate, so);
5374 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005375 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04005376 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04005377 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04005378 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04005379 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02005380 if (open->op_odstate)
5381 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005382}
5383
Al Virob37ad282006-10-19 23:28:59 -07005384__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005385nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005386 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005388 clientid_t *clid = &u->renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07005390 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005391 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005393 trace_nfsd_clid_renew(clid);
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05005394 status = set_client(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005395 if (status)
J. Bruce Fieldsb4587eb2021-01-21 17:57:39 -05005396 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005397 clp = cstate->clp;
NeilBrownea1da632005-06-23 22:04:17 -07005398 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04005399 && clp->cl_cb_state != NFSD4_CB_UP)
J. Bruce Fieldsb4587eb2021-01-21 17:57:39 -05005400 return nfserr_cb_path_down;
5401 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402}
5403
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04005404void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005405nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07005406{
Jeff Layton33dcc482012-04-10 11:08:48 -04005407 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005408 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04005409 return;
5410
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005411 trace_nfsd_grace_complete(nn);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005412 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04005413 /*
5414 * If the server goes down again right now, an NFSv4
5415 * client will still be allowed to reclaim after it comes back up,
5416 * even if it hasn't yet had a chance to reclaim state this time.
5417 *
5418 */
Jeff Layton919b8042014-09-12 16:40:20 -04005419 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005420 /*
5421 * At this point, NFSv4 clients can still reclaim. But if the
5422 * server crashes, any that have not yet reclaimed will be out
5423 * of luck on the next boot.
5424 *
5425 * (NFSv4.1+ clients are considered to have reclaimed once they
5426 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5427 * have reclaimed after their first OPEN.)
5428 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005429 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005430 /*
5431 * At this point, and once lockd and/or any other containers
5432 * exit their grace period, further reclaims will fail and
5433 * regular locking can resume.
5434 */
NeilBrowna76b4312005-06-23 22:04:01 -07005435}
5436
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005437/*
5438 * If we've waited a lease period but there are still clients trying to
5439 * reclaim, wait a little longer to give them a chance to finish.
5440 */
5441static bool clients_still_reclaiming(struct nfsd_net *nn)
5442{
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005443 time64_t double_grace_period_end = nn->boot_time +
5444 2 * nn->nfsd4_lease;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005445
Scott Mayhew362063a2019-03-26 18:06:28 -04005446 if (nn->track_reclaim_completes &&
5447 atomic_read(&nn->nr_reclaim_complete) ==
5448 nn->reclaim_str_hashtbl_size)
5449 return false;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005450 if (!nn->somebody_reclaimed)
5451 return false;
5452 nn->somebody_reclaimed = false;
5453 /*
5454 * If we've given them *two* lease times to reclaim, and they're
5455 * still not done, give up:
5456 */
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005457 if (ktime_get_boottime_seconds() > double_grace_period_end)
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005458 return false;
5459 return true;
5460}
5461
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005462struct laundry_time {
5463 time64_t cutoff;
5464 time64_t new_timeo;
5465};
5466
5467static bool state_expired(struct laundry_time *lt, time64_t last_refresh)
5468{
5469 time64_t time_remaining;
5470
5471 if (last_refresh < lt->cutoff)
5472 return true;
5473 time_remaining = last_refresh - lt->cutoff;
5474 lt->new_timeo = min(lt->new_timeo, time_remaining);
5475 return false;
5476}
5477
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005478static time64_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005479nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480{
5481 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005482 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04005484 struct nfs4_ol_stateid *stp;
Jeff Layton7919d0a2016-09-16 16:28:25 -04005485 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 struct list_head *pos, *next, reaplist;
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005487 struct laundry_time lt = {
5488 .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease,
5489 .new_timeo = nn->nfsd4_lease
5490 };
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005491 struct nfs4_cpntf_state *cps;
5492 copy_stateid_t *cps_t;
5493 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005495 if (clients_still_reclaiming(nn)) {
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005496 lt.new_timeo = 0;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005497 goto out;
5498 }
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005499 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03005500 INIT_LIST_HEAD(&reaplist);
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005501
5502 spin_lock(&nn->s2s_cp_lock);
5503 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
5504 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
5505 if (cps->cp_stateid.sc_type == NFS4_COPYNOTIFY_STID &&
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005506 state_expired(&lt, cps->cpntf_time))
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005507 _free_cpntf_state_locked(nn, cps);
5508 }
5509 spin_unlock(&nn->s2s_cp_lock);
5510
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005511 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005512 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 clp = list_entry(pos, struct nfs4_client, cl_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005514 if (!state_expired(&lt, clp->cl_time))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 break;
Chuck Lever2958d2e2021-05-14 15:56:06 -04005516 if (mark_client_expired_locked(clp))
Benny Halevyd7682982010-05-12 00:13:54 +03005517 continue;
Trond Myklebust4864af92014-07-30 08:27:03 -04005518 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03005519 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005520 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03005521 list_for_each_safe(pos, next, &reaplist) {
5522 clp = list_entry(pos, struct nfs4_client, cl_lru);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005523 trace_nfsd_clid_purged(&clp->cl_clientid);
Trond Myklebust4864af92014-07-30 08:27:03 -04005524 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 expire_client(clp);
5526 }
Benny Halevycdc975052014-05-30 09:09:30 -04005527 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005528 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005530 if (!state_expired(&lt, dp->dl_time))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 break;
Jeff Layton3fcbbd22015-08-24 12:41:48 -04005532 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04005533 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 }
Benny Halevycdc975052014-05-30 09:09:30 -04005535 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005536 while (!list_empty(&reaplist)) {
5537 dp = list_first_entry(&reaplist, struct nfs4_delegation,
5538 dl_recall_lru);
5539 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005540 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 }
Jeff Layton217526e2014-07-30 08:27:11 -04005542
5543 spin_lock(&nn->client_lock);
5544 while (!list_empty(&nn->close_lru)) {
5545 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
5546 oo_close_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005547 if (!state_expired(&lt, oo->oo_time))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 break;
Jeff Layton217526e2014-07-30 08:27:11 -04005549 list_del_init(&oo->oo_close_lru);
5550 stp = oo->oo_last_closed_stid;
5551 oo->oo_last_closed_stid = NULL;
5552 spin_unlock(&nn->client_lock);
5553 nfs4_put_stid(&stp->st_stid);
5554 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 }
Jeff Layton217526e2014-07-30 08:27:11 -04005556 spin_unlock(&nn->client_lock);
5557
Jeff Layton7919d0a2016-09-16 16:28:25 -04005558 /*
5559 * It's possible for a client to try and acquire an already held lock
5560 * that is being held for a long time, and then lose interest in it.
5561 * So, we clean out any un-revisited request after a lease period
5562 * under the assumption that the client is no longer interested.
5563 *
5564 * RFC5661, sec. 9.6 states that the client must not rely on getting
5565 * notifications and must continue to poll for locks, even when the
5566 * server supports them. Thus this shouldn't lead to clients blocking
5567 * indefinitely once the lock does become free.
5568 */
5569 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04005570 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005571 while (!list_empty(&nn->blocked_locks_lru)) {
5572 nbl = list_first_entry(&nn->blocked_locks_lru,
5573 struct nfsd4_blocked_lock, nbl_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005574 if (!state_expired(&lt, nbl->nbl_time))
Jeff Layton7919d0a2016-09-16 16:28:25 -04005575 break;
Jeff Layton7919d0a2016-09-16 16:28:25 -04005576 list_move(&nbl->nbl_lru, &reaplist);
5577 list_del_init(&nbl->nbl_list);
5578 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04005579 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005580
5581 while (!list_empty(&reaplist)) {
Naofumi Honda64ebe1242017-11-09 10:57:16 -05005582 nbl = list_first_entry(&reaplist,
Jeff Layton7919d0a2016-09-16 16:28:25 -04005583 struct nfsd4_blocked_lock, nbl_lru);
5584 list_del_init(&nbl->nbl_lru);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005585 free_blocked_lock(nbl);
5586 }
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005587out:
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005588 return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589}
5590
Harvey Harrisona254b242008-02-20 12:49:00 -08005591static struct workqueue_struct *laundry_wq;
5592static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08005593
5594static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005595laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596{
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005597 time64_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08005598 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005599 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
5600 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005602 t = nfs4_laundromat(nn);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005603 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604}
5605
Jeff Layton8fcd4612015-07-30 06:57:46 -04005606static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07005607{
Jeff Layton8fcd4612015-07-30 06:57:46 -04005608 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005609 return nfserr_bad_stateid;
5610 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611}
5612
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005614__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615{
Al Virob37ad282006-10-19 23:28:59 -07005616 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617
J. Bruce Fields02921912010-07-29 15:16:59 -04005618 /* For lock stateid's, we test the parent open, not the lock: */
5619 if (stp->st_openstp)
5620 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005621 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005623 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 goto out;
5625 status = nfs_ok;
5626out:
5627 return status;
5628}
5629
Al Virob37ad282006-10-19 23:28:59 -07005630static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005631check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005633 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005635 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005636 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 * conflicting state; so we must wait out the grace period. */
5638 return nfserr_grace;
5639 } else if (flags & WR_STATE)
5640 return nfs4_share_conflict(current_fh,
5641 NFS4_SHARE_DENY_WRITE);
5642 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
5643 return nfs4_share_conflict(current_fh,
5644 NFS4_SHARE_DENY_READ);
5645}
5646
5647/*
5648 * Allow READ/WRITE during grace period on recovered state only for files
5649 * that are not able to provide mandatory locking.
5650 */
5651static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005652grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005654 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655}
5656
J. Bruce Fields57b7b432012-04-25 17:58:50 -04005657static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05005658{
Andy Adamson66689582009-04-03 08:28:45 +03005659 /*
5660 * When sessions are used the stateid generation number is ignored
5661 * when it is zero.
5662 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04005663 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04005664 return nfs_ok;
5665
5666 if (in->si_generation == ref->si_generation)
5667 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03005668
J. Bruce Fields0836f582008-01-26 19:08:12 -05005669 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04005670 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05005671 return nfserr_bad_stateid;
5672 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04005673 * However, we could see a stateid from the past, even from a
5674 * non-buggy client. For example, if the client sends a lock
5675 * while some IO is outstanding, the lock may bump si_generation
5676 * while the IO is still in flight. The client could avoid that
5677 * situation by waiting for responses on all the IO requests,
5678 * but better performance may result in retrying IO that
5679 * receives an old_stateid error if requests are rarely
5680 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05005681 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04005682 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05005683}
5684
Trond Myklebust03da3162017-11-03 08:00:16 -04005685static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
5686{
5687 __be32 ret;
5688
5689 spin_lock(&s->sc_lock);
5690 ret = nfsd4_verify_open_stid(s);
5691 if (ret == nfs_ok)
5692 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
5693 spin_unlock(&s->sc_lock);
5694 return ret;
5695}
5696
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005697static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
5698{
5699 if (ols->st_stateowner->so_is_open_owner &&
5700 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5701 return nfserr_bad_stateid;
5702 return nfs_ok;
5703}
5704
Chuck Lever7df302f2012-05-29 13:56:37 -04005705static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04005706{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005707 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005708 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04005709
Andrew Elbleae254dac2017-11-09 13:41:10 -05005710 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5711 CLOSE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04005712 return status;
J. Bruce Fields663e36f2020-03-19 10:18:49 -04005713 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04005714 return status;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005715 spin_lock(&cl->cl_lock);
5716 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005717 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005718 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005719 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04005720 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005721 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04005722 switch (s->sc_type) {
5723 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005724 status = nfs_ok;
5725 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005726 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005727 status = nfserr_deleg_revoked;
5728 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005729 case NFS4_OPEN_STID:
5730 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005731 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04005732 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005733 default:
5734 printk("unknown stateid type %x\n", s->sc_type);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05005735 fallthrough;
J. Bruce Fields23340032013-04-09 17:42:28 -04005736 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005737 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005738 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04005739 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005740out_unlock:
5741 spin_unlock(&cl->cl_lock);
5742 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04005743}
5744
Christoph Hellwigcd61c522014-08-14 08:44:57 +02005745__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005746nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5747 stateid_t *stateid, unsigned char typemask,
5748 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005749{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005750 __be32 status;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005751 bool return_revoked = false;
5752
5753 /*
5754 * only return revoked delegations if explicitly asked.
5755 * otherwise we report revoked or bad_stateid status.
5756 */
5757 if (typemask & NFS4_REVOKED_DELEG_STID)
5758 return_revoked = true;
5759 else if (typemask & NFS4_DELEG_STID)
5760 typemask |= NFS4_REVOKED_DELEG_STID;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005761
Andrew Elbleae254dac2017-11-09 13:41:10 -05005762 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5763 CLOSE_STATEID(stateid))
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005764 return nfserr_bad_stateid;
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05005765 status = set_client(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005766 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005767 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005768 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005769 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005770 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005771 if (status)
5772 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005773 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005774 if (!*s)
5775 return nfserr_bad_stateid;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005776 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5777 nfs4_put_stid(*s);
5778 if (cstate->minorversion)
5779 return nfserr_deleg_revoked;
5780 return nfserr_bad_stateid;
5781 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005782 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005783}
5784
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005785static struct nfsd_file *
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005786nfs4_find_file(struct nfs4_stid *s, int flags)
5787{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005788 if (!s)
5789 return NULL;
5790
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005791 switch (s->sc_type) {
5792 case NFS4_DELEG_STID:
5793 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5794 return NULL;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005795 return nfsd_file_get(s->sc_file->fi_deleg_file);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005796 case NFS4_OPEN_STID:
5797 case NFS4_LOCK_STID:
5798 if (flags & RD_STATE)
5799 return find_readable_file(s->sc_file);
5800 else
5801 return find_writeable_file(s->sc_file);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005802 }
5803
5804 return NULL;
5805}
5806
5807static __be32
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005808nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005809{
5810 __be32 status;
5811
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005812 status = nfsd4_check_openowner_confirmed(ols);
5813 if (status)
5814 return status;
5815 return nfs4_check_openmode(ols, flags);
5816}
5817
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005818static __be32
5819nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
Jeff Layton5c4583b2019-08-18 14:18:54 -04005820 struct nfsd_file **nfp, int flags)
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005821{
5822 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005823 struct nfsd_file *nf;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005824 __be32 status;
5825
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005826 nf = nfs4_find_file(s, flags);
5827 if (nf) {
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005828 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5829 acc | NFSD_MAY_OWNER_OVERRIDE);
Jeff Layton5c4583b2019-08-18 14:18:54 -04005830 if (status) {
5831 nfsd_file_put(nf);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005832 goto out;
Jeff Layton5c4583b2019-08-18 14:18:54 -04005833 }
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005834 } else {
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005835 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005836 if (status)
5837 return status;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005838 }
Jeff Layton5c4583b2019-08-18 14:18:54 -04005839 *nfp = nf;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005840out:
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005841 return status;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005842}
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005843static void
5844_free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5845{
5846 WARN_ON_ONCE(cps->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID);
5847 if (!refcount_dec_and_test(&cps->cp_stateid.sc_count))
5848 return;
5849 list_del(&cps->cp_list);
5850 idr_remove(&nn->s2s_cp_stateids,
5851 cps->cp_stateid.stid.si_opaque.so_id);
5852 kfree(cps);
5853}
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005854/*
5855 * A READ from an inter server to server COPY will have a
5856 * copy stateid. Look up the copy notify stateid from the
5857 * idr structure and take a reference on it.
5858 */
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005859__be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5860 struct nfs4_client *clp,
5861 struct nfs4_cpntf_state **cps)
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005862{
5863 copy_stateid_t *cps_t;
5864 struct nfs4_cpntf_state *state = NULL;
5865
5866 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
5867 return nfserr_bad_stateid;
5868 spin_lock(&nn->s2s_cp_lock);
5869 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
5870 if (cps_t) {
5871 state = container_of(cps_t, struct nfs4_cpntf_state,
5872 cp_stateid);
Dan Carpenter5277a792019-12-04 10:59:36 +03005873 if (state->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID) {
5874 state = NULL;
5875 goto unlock;
5876 }
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005877 if (!clp)
5878 refcount_inc(&state->cp_stateid.sc_count);
5879 else
5880 _free_cpntf_state_locked(nn, state);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005881 }
Dan Carpenter5277a792019-12-04 10:59:36 +03005882unlock:
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005883 spin_unlock(&nn->s2s_cp_lock);
5884 if (!state)
5885 return nfserr_bad_stateid;
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005886 if (!clp && state)
5887 *cps = state;
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005888 return 0;
5889}
5890
5891static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5892 struct nfs4_stid **stid)
5893{
5894 __be32 status;
5895 struct nfs4_cpntf_state *cps = NULL;
J. Bruce Fields47fdb222021-01-21 17:57:42 -05005896 struct nfs4_client *found;
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005897
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005898 status = manage_cpntf_state(nn, st, NULL, &cps);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005899 if (status)
5900 return status;
5901
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005902 cps->cpntf_time = ktime_get_boottime_seconds();
J. Bruce Fields47fdb222021-01-21 17:57:42 -05005903
5904 status = nfserr_expired;
5905 found = lookup_clientid(&cps->cp_p_clid, true, nn);
5906 if (!found)
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005907 goto out;
J. Bruce Fields47fdb222021-01-21 17:57:42 -05005908
5909 *stid = find_stateid_by_type(found, &cps->cp_p_stateid,
5910 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID);
5911 if (*stid)
5912 status = nfs_ok;
5913 else
5914 status = nfserr_bad_stateid;
5915
5916 put_client_renew(found);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005917out:
5918 nfs4_put_cpntf_state(nn, cps);
5919 return status;
5920}
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005921
5922void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5923{
5924 spin_lock(&nn->s2s_cp_lock);
5925 _free_cpntf_state_locked(nn, cps);
5926 spin_unlock(&nn->s2s_cp_lock);
5927}
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005928
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005930 * Checks for stateid operations
5931 */
Al Virob37ad282006-10-19 23:28:59 -07005932__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005933nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01005934 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005935 stateid_t *stateid, int flags, struct nfsd_file **nfp,
5936 struct nfs4_stid **cstid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005938 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005939 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005940 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005941 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005942 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943
Jeff Layton5c4583b2019-08-18 14:18:54 -04005944 if (nfp)
5945 *nfp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005947 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 return nfserr_grace;
5949
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005950 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5951 status = check_special_stateids(net, fhp, stateid, flags);
5952 goto done;
5953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005955 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04005956 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005957 &s, nn);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005958 if (status == nfserr_bad_stateid)
5959 status = find_cpntf_state(nn, stateid, &s);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005960 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04005961 return status;
Trond Myklebust03da3162017-11-03 08:00:16 -04005962 status = nfsd4_stid_check_stateid_generation(stateid, s,
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005963 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04005964 if (status)
5965 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005966
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005967 switch (s->sc_type) {
5968 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005969 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005970 break;
5971 case NFS4_OPEN_STID:
5972 case NFS4_LOCK_STID:
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005973 status = nfs4_check_olstateid(openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005974 break;
5975 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04005976 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005977 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04005979 if (status)
5980 goto out;
5981 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005982
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005983done:
Jeff Layton5c4583b2019-08-18 14:18:54 -04005984 if (status == nfs_ok && nfp)
5985 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986out:
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005987 if (s) {
5988 if (!status && cstid)
5989 *cstid = s;
5990 else
5991 nfs4_put_stid(s);
5992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 return status;
5994}
5995
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005996/*
Bryan Schumaker17456802011-07-13 10:50:48 -04005997 * Test if the stateid is valid
5998 */
5999__be32
6000nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006001 union nfsd4_op_u *u)
Bryan Schumaker17456802011-07-13 10:50:48 -04006002{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006003 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05006004 struct nfsd4_test_stateid_id *stateid;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05006005 struct nfs4_client *cl = cstate->clp;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05006006
Bryan Schumaker03cfb422012-01-27 10:22:49 -05006007 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04006008 stateid->ts_id_status =
6009 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05006010
Bryan Schumaker17456802011-07-13 10:50:48 -04006011 return nfs_ok;
6012}
6013
Chuck Lever42691392016-08-11 10:37:30 -04006014static __be32
6015nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
6016{
6017 struct nfs4_ol_stateid *stp = openlockstateid(s);
6018 __be32 ret;
6019
Trond Myklebust659aefb2017-11-03 08:00:13 -04006020 ret = nfsd4_lock_ol_stateid(stp);
6021 if (ret)
6022 goto out_put_stid;
Chuck Lever42691392016-08-11 10:37:30 -04006023
6024 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6025 if (ret)
6026 goto out;
6027
6028 ret = nfserr_locks_held;
6029 if (check_for_locks(stp->st_stid.sc_file,
6030 lockowner(stp->st_stateowner)))
6031 goto out;
6032
6033 release_lock_stateid(stp);
6034 ret = nfs_ok;
6035
6036out:
6037 mutex_unlock(&stp->st_mutex);
Trond Myklebust659aefb2017-11-03 08:00:13 -04006038out_put_stid:
Chuck Lever42691392016-08-11 10:37:30 -04006039 nfs4_put_stid(s);
6040 return ret;
6041}
6042
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006043__be32
6044nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006045 union nfsd4_op_u *u)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006046{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006047 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006048 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04006049 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006050 struct nfs4_delegation *dp;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05006051 struct nfs4_client *cl = cstate->clp;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04006052 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006053
Jeff Layton1af71cc2014-07-29 21:34:14 -04006054 spin_lock(&cl->cl_lock);
6055 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04006056 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04006057 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04006058 spin_lock(&s->sc_lock);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04006059 switch (s->sc_type) {
6060 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006061 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04006062 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04006063 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04006064 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6065 if (ret)
6066 break;
6067 ret = nfserr_locks_held;
6068 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04006069 case NFS4_LOCK_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04006070 spin_unlock(&s->sc_lock);
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03006071 refcount_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04006072 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04006073 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04006074 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006075 case NFS4_REVOKED_DELEG_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04006076 spin_unlock(&s->sc_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006077 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04006078 list_del_init(&dp->dl_recall_lru);
6079 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04006080 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006081 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04006082 goto out;
6083 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006084 }
Trond Myklebust03da3162017-11-03 08:00:16 -04006085 spin_unlock(&s->sc_lock);
Jeff Layton1af71cc2014-07-29 21:34:14 -04006086out_unlock:
6087 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006088out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006089 return ret;
6090}
6091
NeilBrown4c4cd222005-07-07 17:59:27 -07006092static inline int
6093setlkflg (int type)
6094{
6095 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
6096 RD_STATE : WR_STATE;
6097}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006099static __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 -04006100{
6101 struct svc_fh *current_fh = &cstate->current_fh;
6102 struct nfs4_stateowner *sop = stp->st_stateowner;
6103 __be32 status;
6104
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006105 status = nfsd4_check_seqid(cstate, sop, seqid);
6106 if (status)
6107 return status;
Trond Myklebust9271d7e2017-11-03 08:00:15 -04006108 status = nfsd4_lock_ol_stateid(stp);
6109 if (status != nfs_ok)
6110 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006111 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04006112 if (status == nfs_ok)
6113 status = nfs4_check_fh(current_fh, &stp->st_stid);
6114 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006115 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04006116 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006117}
6118
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119/*
6120 * Checks for sequence id mutating operations.
6121 */
Al Virob37ad282006-10-19 23:28:59 -07006122static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03006123nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04006124 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006125 struct nfs4_ol_stateid **stpp,
6126 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127{
J. Bruce Fields0836f582008-01-26 19:08:12 -05006128 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006129 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006130 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04006132 trace_nfsd_preprocess(seqid, stateid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07006133
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006135 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006136 if (status)
6137 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006138 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04006139 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006141 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04006142 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006143 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04006144 else
6145 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006146 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006147}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05006148
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006149static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6150 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006151{
6152 __be32 status;
6153 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006154 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006156 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006157 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04006158 if (status)
6159 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006160 oo = openowner(stp->st_stateowner);
6161 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006162 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006163 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07006164 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006165 }
6166 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07006167 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168}
6169
Al Virob37ad282006-10-19 23:28:59 -07006170__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006171nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006172 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006174 struct nfsd4_open_confirm *oc = &u->open_confirm;
Al Virob37ad282006-10-19 23:28:59 -07006175 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006176 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006177 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006178 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179
Al Viroa6a9f182013-09-16 10:57:01 -04006180 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
6181 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006183 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07006184 if (status)
6185 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006187 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006188 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006189 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006190 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006191 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006192 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006193 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04006194 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006195 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04006196 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04006197 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04006198 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04006199 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006200 mutex_unlock(&stp->st_mutex);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04006201 trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
Jeff Layton2a4317c2012-03-21 16:42:43 -04006202 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006203 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04006204put_stateid:
6205 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006207 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208 return status;
6209}
6210
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006211static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212{
Jeff Layton82c5ff12012-05-11 09:45:13 -04006213 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006214 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04006215 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006216 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006217}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04006218
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006219static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
6220{
6221 switch (to_access) {
6222 case NFS4_SHARE_ACCESS_READ:
6223 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
6224 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6225 break;
6226 case NFS4_SHARE_ACCESS_WRITE:
6227 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
6228 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6229 break;
6230 case NFS4_SHARE_ACCESS_BOTH:
6231 break;
6232 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006233 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234 }
6235}
6236
Al Virob37ad282006-10-19 23:28:59 -07006237__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006238nfsd4_open_downgrade(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006239 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006241 struct nfsd4_open_downgrade *od = &u->open_downgrade;
Al Virob37ad282006-10-19 23:28:59 -07006242 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006243 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006244 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245
Al Viroa6a9f182013-09-16 10:57:01 -04006246 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
6247 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04006249 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02006250 if (od->od_deleg_want)
6251 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
6252 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006254 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006255 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006256 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04006259 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04006260 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006262 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04006264 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04006265 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006267 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006269 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04006270 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04006271 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006273put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006274 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006275 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006277 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278 return status;
6279}
6280
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006281static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
6282{
Trond Myklebustacf92952014-06-30 11:48:37 -04006283 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04006284 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04006285 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04006286
Jeff Layton2c41beb2014-07-29 21:34:41 -04006287 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04006288 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04006289
Jeff Laytond83017f2014-07-29 21:34:42 -04006290 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04006291 if (unhashed)
6292 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04006293 spin_unlock(&clp->cl_lock);
6294 free_ol_stateid_reaplist(&reaplist);
6295 } else {
6296 spin_unlock(&clp->cl_lock);
6297 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04006298 if (unhashed)
6299 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04006300 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04006301}
6302
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303/*
6304 * nfs4_unlock_state() called after encode
6305 */
Al Virob37ad282006-10-19 23:28:59 -07006306__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006307nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006308 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006310 struct nfsd4_close *close = &u->close;
Al Virob37ad282006-10-19 23:28:59 -07006311 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006312 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006313 struct net *net = SVC_NET(rqstp);
6314 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315
Al Viroa6a9f182013-09-16 10:57:01 -04006316 dprintk("NFSD: nfsd4_close on file %pd\n",
6317 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006319 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
6320 &close->cl_stateid,
6321 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006322 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006323 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006324 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04006326
6327 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Laytonbd2deca2018-02-15 15:16:18 -05006328
6329 /*
6330 * Technically we don't _really_ have to increment or copy it, since
6331 * it should just be gone after this operation and we clobber the
6332 * copied value below, but we continue to do so here just to ensure
6333 * that racing ops see that there was a state change.
6334 */
Jeff Layton9767feb2015-10-01 09:05:50 -04006335 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006337 nfsd4_close_open_stateid(stp);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04006338 mutex_unlock(&stp->st_mutex);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04006339
Jeff Laytonbd2deca2018-02-15 15:16:18 -05006340 /* v4.1+ suggests that we send a special stateid in here, since the
6341 * clients should just ignore this anyway. Since this is not useful
6342 * for v4.0 clients either, we set it to the special close_stateid
6343 * universally.
6344 *
6345 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
6346 */
6347 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
Trond Myklebustfb500a72017-11-03 08:00:12 -04006348
Trond Myklebust8a0b5892014-07-29 21:34:20 -04006349 /* put reference from nfs4_preprocess_seqid_op */
6350 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352 return status;
6353}
6354
Al Virob37ad282006-10-19 23:28:59 -07006355__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006356nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006357 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006359 struct nfsd4_delegreturn *dr = &u->delegreturn;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006360 struct nfs4_delegation *dp;
6361 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006362 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07006363 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006364 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006366 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006367 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006369 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006370 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006371 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006372 dp = delegstateid(s);
Trond Myklebust03da3162017-11-03 08:00:16 -04006373 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006374 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04006375 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006376
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006377 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04006378put_stateid:
6379 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380out:
6381 return status;
6382}
6383
Benny Halevy87df4de2008-12-15 19:42:03 +02006384/* last octet in a range */
6385static inline u64
6386last_byte_offset(u64 start, u64 len)
6387{
6388 u64 end;
6389
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006390 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02006391 end = start + len;
6392 return end > start ? end - 1: NFS4_MAX_UINT64;
6393}
6394
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395/*
6396 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6397 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6398 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6399 * locking, this prevents us from being completely protocol-compliant. The
6400 * real solution to this problem is to start using unsigned file offsets in
6401 * the VFS, but this is a very deep change!
6402 */
6403static inline void
6404nfs4_transform_lock_offset(struct file_lock *lock)
6405{
6406 if (lock->fl_start < 0)
6407 lock->fl_start = OFFSET_MAX;
6408 if (lock->fl_end < 0)
6409 lock->fl_end = OFFSET_MAX;
6410}
6411
Jeff Laytoncae80b32015-04-03 09:04:04 -04006412static fl_owner_t
6413nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006414{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006415 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6416
6417 nfs4_get_stateowner(&lo->lo_owner);
6418 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006419}
6420
Jeff Laytoncae80b32015-04-03 09:04:04 -04006421static void
6422nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006423{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006424 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006425
Jeff Laytoncae80b32015-04-03 09:04:04 -04006426 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04006427 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04006428}
6429
Jeff Layton76d348f2016-09-16 16:28:24 -04006430static void
6431nfsd4_lm_notify(struct file_lock *fl)
6432{
6433 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
6434 struct net *net = lo->lo_owner.so_client->net;
6435 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6436 struct nfsd4_blocked_lock *nbl = container_of(fl,
6437 struct nfsd4_blocked_lock, nbl_lock);
6438 bool queue = false;
6439
Jeff Layton7919d0a2016-09-16 16:28:25 -04006440 /* An empty list means that something else is going to be using it */
Jeff Layton0cc11a62016-10-20 09:34:31 -04006441 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006442 if (!list_empty(&nbl->nbl_list)) {
6443 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006444 list_del_init(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -04006445 queue = true;
6446 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04006447 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006448
6449 if (queue)
6450 nfsd4_run_cb(&nbl->nbl_cb);
6451}
6452
Alexey Dobriyan7b021962009-09-21 17:01:12 -07006453static const struct lock_manager_operations nfsd_posix_mng_ops = {
Jeff Layton76d348f2016-09-16 16:28:24 -04006454 .lm_notify = nfsd4_lm_notify,
Kinglong Meeaef95832014-08-22 10:18:44 -04006455 .lm_get_owner = nfsd4_fl_get_owner,
6456 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07006457};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458
6459static inline void
6460nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
6461{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006462 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463
NeilBrownd5b90262006-04-10 22:55:22 -07006464 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006465 lo = (struct nfs4_lockowner *) fl->fl_owner;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04006466 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
6467 GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006468 if (!deny->ld_owner.data)
6469 /* We just don't care that much */
6470 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006471 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07006472 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006473nevermind:
6474 deny->ld_owner.len = 0;
6475 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07006476 deny->ld_clientid.cl_boot = 0;
6477 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478 }
6479 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02006480 deny->ld_length = NFS4_MAX_UINT64;
6481 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482 deny->ld_length = fl->fl_end - fl->fl_start + 1;
6483 deny->ld_type = NFS4_READ_LT;
6484 if (fl->fl_type != F_RDLCK)
6485 deny->ld_type = NFS4_WRITE_LT;
6486}
6487
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006488static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006489find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490{
Trond Myklebustd4f04892014-07-29 21:34:36 -04006491 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006492 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493
Trond Myklebust0a880a22014-07-30 08:27:10 -04006494 lockdep_assert_held(&clp->cl_lock);
6495
Trond Myklebustd4f04892014-07-29 21:34:36 -04006496 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
6497 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006498 if (so->so_is_open_owner)
6499 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006500 if (same_owner_str(so, owner))
6501 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502 }
6503 return NULL;
6504}
6505
Trond Myklebustc58c6612014-07-29 21:34:35 -04006506static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006507find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04006508{
6509 struct nfs4_lockowner *lo;
6510
Trond Myklebustd4f04892014-07-29 21:34:36 -04006511 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006512 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04006513 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006514 return lo;
6515}
6516
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006517static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
6518{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006519 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006520}
6521
Jeff Layton6b180f02014-07-29 21:34:26 -04006522static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
6523{
6524 struct nfs4_lockowner *lo = lockowner(sop);
6525
6526 kmem_cache_free(lockowner_slab, lo);
6527}
6528
6529static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006530 .so_unhash = nfs4_unhash_lockowner,
6531 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04006532};
6533
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534/*
6535 * Alloc a lock owner structure.
6536 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006537 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006539 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006541static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04006542alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
6543 struct nfs4_ol_stateid *open_stp,
6544 struct nfsd4_lock *lock)
6545{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006546 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006548 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
6549 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006551 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006552 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
6553 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04006554 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04006555 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006556 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006557 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006558 if (ret == NULL) {
6559 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04006560 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006561 ret = lo;
6562 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08006563 nfs4_free_stateowner(&lo->lo_owner);
6564
Trond Myklebustd4f04892014-07-29 21:34:36 -04006565 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04006566 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567}
6568
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006569static struct nfs4_ol_stateid *
Trond Myklebusta451b122020-03-01 18:21:38 -05006570find_lock_stateid(const struct nfs4_lockowner *lo,
6571 const struct nfs4_ol_stateid *ost)
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006572{
6573 struct nfs4_ol_stateid *lst;
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006574
Trond Myklebusta451b122020-03-01 18:21:38 -05006575 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006576
Trond Myklebusta451b122020-03-01 18:21:38 -05006577 /* If ost is not hashed, ost->st_locks will not be valid */
6578 if (!nfs4_ol_stateid_unhashed(ost))
6579 list_for_each_entry(lst, &ost->st_locks, st_locks) {
6580 if (lst->st_stateowner == &lo->lo_owner) {
6581 refcount_inc(&lst->st_stid.sc_count);
6582 return lst;
6583 }
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006584 }
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006585 return NULL;
6586}
6587
Trond Myklebustbeeca192017-11-03 08:00:14 -04006588static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006589init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
6590 struct nfs4_file *fp, struct inode *inode,
6591 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04006593 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustbeeca192017-11-03 08:00:14 -04006594 struct nfs4_ol_stateid *retstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595
Trond Myklebustbeeca192017-11-03 08:00:14 -04006596 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05006597 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006598retry:
6599 spin_lock(&clp->cl_lock);
Trond Myklebusta451b122020-03-01 18:21:38 -05006600 if (nfs4_ol_stateid_unhashed(open_stp))
6601 goto out_close;
6602 retstp = find_lock_stateid(lo, open_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006603 if (retstp)
Trond Myklebusta451b122020-03-01 18:21:38 -05006604 goto out_found;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03006605 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05006606 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006607 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07006608 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04006609 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006610 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07006612 stp->st_openstp = open_stp;
Trond Myklebusta451b122020-03-01 18:21:38 -05006613 spin_lock(&fp->fi_lock);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006614 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04006615 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04006616 list_add(&stp->st_perfile, &fp->fi_stateids);
6617 spin_unlock(&fp->fi_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006618 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006619 return stp;
Trond Myklebusta451b122020-03-01 18:21:38 -05006620out_found:
6621 spin_unlock(&clp->cl_lock);
6622 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
6623 nfs4_put_stid(&retstp->st_stid);
6624 goto retry;
6625 }
6626 /* To keep mutex tracking happy */
6627 mutex_unlock(&stp->st_mutex);
6628 return retstp;
6629out_close:
6630 spin_unlock(&clp->cl_lock);
6631 mutex_unlock(&stp->st_mutex);
6632 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633}
6634
Jeff Laytonc53530d2014-06-30 11:48:39 -04006635static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006636find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
6637 struct inode *inode, struct nfs4_ol_stateid *ost,
6638 bool *new)
6639{
6640 struct nfs4_stid *ns = NULL;
6641 struct nfs4_ol_stateid *lst;
6642 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6643 struct nfs4_client *clp = oo->oo_owner.so_client;
6644
Trond Myklebustbeeca192017-11-03 08:00:14 -04006645 *new = false;
Jeff Layton356a95e2014-07-29 21:34:13 -04006646 spin_lock(&clp->cl_lock);
Trond Myklebusta451b122020-03-01 18:21:38 -05006647 lst = find_lock_stateid(lo, ost);
Jeff Layton356a95e2014-07-29 21:34:13 -04006648 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006649 if (lst != NULL) {
6650 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
6651 goto out;
6652 nfs4_put_stid(&lst->st_stid);
6653 }
6654 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
6655 if (ns == NULL)
6656 return NULL;
6657
6658 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
6659 if (lst == openlockstateid(ns))
6660 *new = true;
6661 else
Jeff Layton356a95e2014-07-29 21:34:13 -04006662 nfs4_put_stid(ns);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006663out:
Jeff Layton356a95e2014-07-29 21:34:13 -04006664 return lst;
6665}
Jeff Laytonc53530d2014-06-30 11:48:39 -04006666
NeilBrownfd39ca92005-06-23 22:04:03 -07006667static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668check_lock_length(u64 offset, u64 length)
6669{
Kinglong Meee7969312015-07-13 17:34:19 +08006670 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
6671 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672}
6673
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006674static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006675{
Trond Myklebust11b91642014-07-29 21:34:08 -04006676 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006677
Jeff Layton7214e862014-07-10 14:07:33 -04006678 lockdep_assert_held(&fp->fi_lock);
6679
Jeff Layton82c5ff12012-05-11 09:45:13 -04006680 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05006681 return;
Jeff Layton12659652014-07-10 14:07:28 -04006682 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006683 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006684}
6685
Jeff Layton356a95e2014-07-29 21:34:13 -04006686static __be32
6687lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
6688 struct nfs4_ol_stateid *ost,
6689 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04006690 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006691{
Jeff Layton5db1c032014-07-29 21:34:28 -04006692 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04006693 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006694 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6695 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00006696 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006697 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04006698 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006699 unsigned int strhashval;
6700
Kinglong Meec8623992015-07-13 17:35:37 +08006701 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006702 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006703 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006704 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
6705 if (lo == NULL)
6706 return nfserr_jukebox;
6707 } else {
6708 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04006709 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04006710 if (!cstate->minorversion &&
6711 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04006712 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006713 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04006714
Jeff Laytondd257932016-08-11 10:37:39 -04006715 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
6716 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04006717 status = nfserr_jukebox;
6718 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006719 }
Jeff Laytondd257932016-08-11 10:37:39 -04006720
Jeff Layton5db1c032014-07-29 21:34:28 -04006721 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04006722 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04006723out:
6724 nfs4_put_stateowner(&lo->lo_owner);
6725 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006726}
6727
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728/*
6729 * LOCK operation
6730 */
Al Virob37ad282006-10-19 23:28:59 -07006731__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006732nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006733 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006735 struct nfsd4_lock *lock = &u->lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006736 struct nfs4_openowner *open_sop = NULL;
6737 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006738 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006739 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04006740 struct nfs4_file *fp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006741 struct nfsd_file *nf = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006742 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006743 struct file_lock *file_lock = NULL;
6744 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006745 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006746 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07006747 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04006748 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04006749 unsigned char fl_type;
6750 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006751 struct net *net = SVC_NET(rqstp);
6752 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753
6754 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
6755 (long long) lock->lk_offset,
6756 (long long) lock->lk_length);
6757
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758 if (check_lock_length(lock->lk_offset, lock->lk_length))
6759 return nfserr_inval;
6760
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006761 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02006762 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08006763 dprintk("NFSD: nfsd4_lock: permission denied!\n");
6764 return status;
6765 }
6766
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04006768 if (nfsd4_has_session(cstate))
6769 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006770 memcpy(&lock->lk_new_clientid,
J. Bruce Fieldsec596592021-01-21 17:57:45 -05006771 &cstate->clp->cl_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04006772 sizeof(clientid_t));
6773
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006775 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776 lock->lk_new_open_seqid,
6777 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006778 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07006779 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006780 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006781 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006782 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006783 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04006784 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006785 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006786 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006787 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04006788 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006789 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03006790 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006791 lock->lk_old_lock_seqid,
6792 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006793 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006794 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04006795 if (status)
6796 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006797 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006799 lkflg = setlkflg(lock->lk_type);
6800 status = nfs4_check_openmode(lock_stp, lkflg);
6801 if (status)
6802 goto out;
6803
NeilBrown0dd395d2005-07-07 17:59:15 -07006804 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006805 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006806 goto out;
6807 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006808 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006809 goto out;
6810
Trond Myklebust11b91642014-07-29 21:34:08 -04006811 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813 case NFS4_READW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006814 if (nfsd4_has_session(cstate))
6815 fl_flags |= FL_SLEEP;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006816 fallthrough;
Jeff Layton76d348f2016-09-16 16:28:24 -04006817 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006818 spin_lock(&fp->fi_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006819 nf = find_readable_file_locked(fp);
6820 if (nf)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006821 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04006822 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006823 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006824 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825 case NFS4_WRITEW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006826 if (nfsd4_has_session(cstate))
6827 fl_flags |= FL_SLEEP;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006828 fallthrough;
Jeff Layton76d348f2016-09-16 16:28:24 -04006829 case NFS4_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006830 spin_lock(&fp->fi_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006831 nf = find_writeable_file_locked(fp);
6832 if (nf)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006833 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04006834 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006835 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006836 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837 default:
6838 status = nfserr_inval;
6839 goto out;
6840 }
Jeff Layton76d348f2016-09-16 16:28:24 -04006841
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006842 if (!nf) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006843 status = nfserr_openmode;
6844 goto out;
6845 }
Kinglong Meeaef95832014-08-22 10:18:44 -04006846
Jeff Layton76d348f2016-09-16 16:28:24 -04006847 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6848 if (!nbl) {
6849 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6850 status = nfserr_jukebox;
6851 goto out;
6852 }
6853
6854 file_lock = &nbl->nbl_lock;
6855 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04006856 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04006857 file_lock->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006858 file_lock->fl_file = nf->nf_file;
Jeff Layton76d348f2016-09-16 16:28:24 -04006859 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04006860 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6861 file_lock->fl_start = lock->lk_offset;
6862 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6863 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864
Jeff Layton21179d82012-08-21 08:03:32 -04006865 conflock = locks_alloc_lock();
6866 if (!conflock) {
6867 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6868 status = nfserr_jukebox;
6869 goto out;
6870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871
Jeff Layton76d348f2016-09-16 16:28:24 -04006872 if (fl_flags & FL_SLEEP) {
Arnd Bergmann20b7d862019-11-04 16:31:52 +01006873 nbl->nbl_time = ktime_get_boottime_seconds();
Jeff Layton0cc11a62016-10-20 09:34:31 -04006874 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006875 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006876 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006877 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006878 }
6879
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006880 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006881 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04006883 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07006884 status = 0;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04006885 if (lock->lk_reclaim)
6886 nn->somebody_reclaimed = true;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006887 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006888 case FILE_LOCK_DEFERRED:
6889 nbl = NULL;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006890 fallthrough;
Jeff Layton76d348f2016-09-16 16:28:24 -04006891 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006892 status = nfserr_denied;
6893 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04006894 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006895 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006896 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006898 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04006899 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05006900 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04006901 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006902 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904out:
Jeff Layton76d348f2016-09-16 16:28:24 -04006905 if (nbl) {
6906 /* dequeue it if we queued it before */
6907 if (fl_flags & FL_SLEEP) {
Jeff Layton0cc11a62016-10-20 09:34:31 -04006908 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006909 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006910 list_del_init(&nbl->nbl_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006911 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006912 }
6913 free_blocked_lock(nbl);
6914 }
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006915 if (nf)
6916 nfsd_file_put(nf);
Jeff Layton5db1c032014-07-29 21:34:28 -04006917 if (lock_stp) {
6918 /* Bump seqid manually if the 4.0 replay owner is openowner */
6919 if (cstate->replay_owner &&
6920 cstate->replay_owner != &lock_sop->lo_owner &&
6921 seqid_mutating_err(ntohl(status)))
6922 lock_sop->lo_owner.so_seqid++;
6923
6924 /*
6925 * If this is a new, never-before-used stateid, and we are
6926 * returning an error, then just go ahead and release it.
6927 */
J. Bruce Fields25020722018-01-17 16:25:59 -05006928 if (status && new)
Jeff Layton5db1c032014-07-29 21:34:28 -04006929 release_lock_stateid(lock_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006930
6931 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton5db1c032014-07-29 21:34:28 -04006932
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006933 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04006934 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006935 if (open_stp)
6936 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006937 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006938 if (conflock)
6939 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940 return status;
6941}
6942
6943/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006944 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6945 * so we do a temporary open here just to get an open file to pass to
6946 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6947 * inode operation.)
6948 */
Al Viro04da6e92012-04-13 00:00:04 -04006949static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006950{
Jeff Layton6b556ca2019-08-18 14:18:55 -04006951 struct nfsd_file *nf;
J. Bruce Fields217fd6f2021-04-16 14:00:14 -04006952 __be32 err;
6953
6954 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
6955 if (err)
6956 return err;
6957 fh_lock(fhp); /* to block new leases till after test_lock: */
6958 err = nfserrno(nfsd_open_break_lease(fhp->fh_dentry->d_inode,
6959 NFSD_MAY_READ));
6960 if (err)
6961 goto out;
6962 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
6963out:
6964 fh_unlock(fhp);
6965 nfsd_file_put(nf);
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006966 return err;
6967}
6968
6969/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970 * LOCKT operation
6971 */
Al Virob37ad282006-10-19 23:28:59 -07006972__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006973nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006974 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006976 struct nfsd4_lockt *lockt = &u->lockt;
Jeff Layton21179d82012-08-21 08:03:32 -04006977 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04006978 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006979 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006980 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006982 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983 return nfserr_grace;
6984
6985 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6986 return nfserr_inval;
6987
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006988 if (!nfsd4_has_session(cstate)) {
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05006989 status = set_client(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006990 if (status)
6991 goto out;
6992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993
J. Bruce Fields75c096f2011-08-15 18:39:32 -04006994 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996
Jeff Layton21179d82012-08-21 08:03:32 -04006997 file_lock = locks_alloc_lock();
6998 if (!file_lock) {
6999 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7000 status = nfserr_jukebox;
7001 goto out;
7002 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08007003
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004 switch (lockt->lt_type) {
7005 case NFS4_READ_LT:
7006 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04007007 file_lock->fl_type = F_RDLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00007008 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007009 case NFS4_WRITE_LT:
7010 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04007011 file_lock->fl_type = F_WRLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00007012 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04007014 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015 status = nfserr_inval;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00007016 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017 }
7018
Kinglong Meec8623992015-07-13 17:35:37 +08007019 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04007020 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04007021 file_lock->fl_owner = (fl_owner_t)lo;
7022 file_lock->fl_pid = current->tgid;
7023 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024
Jeff Layton21179d82012-08-21 08:03:32 -04007025 file_lock->fl_start = lockt->lt_offset;
7026 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027
Jeff Layton21179d82012-08-21 08:03:32 -04007028 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029
Jeff Layton21179d82012-08-21 08:03:32 -04007030 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04007031 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05007032 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04007033
Jeff Layton21179d82012-08-21 08:03:32 -04007034 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04007036 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037 }
7038out:
Jeff Layton5db1c032014-07-29 21:34:28 -04007039 if (lo)
7040 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04007041 if (file_lock)
7042 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043 return status;
7044}
7045
Al Virob37ad282006-10-19 23:28:59 -07007046__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08007047nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02007048 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049{
Christoph Hellwigeb698532017-05-08 20:58:35 +02007050 struct nfsd4_locku *locku = &u->locku;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04007051 struct nfs4_ol_stateid *stp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007052 struct nfsd_file *nf = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04007053 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07007054 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07007055 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007056 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7057
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
7059 (long long) locku->lu_offset,
7060 (long long) locku->lu_length);
7061
7062 if (check_lock_length(locku->lu_offset, locku->lu_length))
7063 return nfserr_inval;
7064
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04007065 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007066 &locku->lu_stateid, NFS4_LOCK_STID,
7067 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04007068 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069 goto out;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007070 nf = find_any_file(stp->st_stid.sc_file);
7071 if (!nf) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04007072 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04007073 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04007074 }
Jeff Layton21179d82012-08-21 08:03:32 -04007075 file_lock = locks_alloc_lock();
7076 if (!file_lock) {
7077 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7078 status = nfserr_jukebox;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007079 goto put_file;
Jeff Layton21179d82012-08-21 08:03:32 -04007080 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08007081
Jeff Layton21179d82012-08-21 08:03:32 -04007082 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04007083 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04007084 file_lock->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007085 file_lock->fl_file = nf->nf_file;
Jeff Layton21179d82012-08-21 08:03:32 -04007086 file_lock->fl_flags = FL_POSIX;
7087 file_lock->fl_lmops = &nfsd_posix_mng_ops;
7088 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089
Jeff Layton21179d82012-08-21 08:03:32 -04007090 file_lock->fl_end = last_byte_offset(locku->lu_offset,
7091 locku->lu_length);
7092 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007094 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07007095 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05007096 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097 goto out_nfserr;
7098 }
Jeff Layton9767feb2015-10-01 09:05:50 -04007099 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007100put_file:
7101 nfsd_file_put(nf);
Trond Myklebust858cc572014-07-29 21:34:16 -04007102put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04007103 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04007104 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04007106 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04007107 if (file_lock)
7108 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109 return status;
7110
7111out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07007112 status = nfserrno(err);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007113 goto put_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114}
7115
7116/*
7117 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007118 * true: locks held by lockowner
7119 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007121static bool
7122check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007124 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007125 int status = false;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007126 struct nfsd_file *nf = find_any_file(fp);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007127 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007128 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007129
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007130 if (!nf) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007131 /* Any valid lock stateid should have some sort of access */
7132 WARN_ON_ONCE(1);
7133 return status;
7134 }
7135
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007136 inode = locks_inode(nf->nf_file);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007137 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007139 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05007140 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007141 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
7142 if (fl->fl_owner == (fl_owner_t)lowner) {
7143 status = true;
7144 break;
7145 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08007146 }
Jeff Layton6109c852015-01-16 15:05:57 -05007147 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148 }
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007149 nfsd_file_put(nf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150 return status;
7151}
7152
Al Virob37ad282006-10-19 23:28:59 -07007153__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08007154nfsd4_release_lockowner(struct svc_rqst *rqstp,
7155 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02007156 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157{
Christoph Hellwigeb698532017-05-08 20:58:35 +02007158 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04007160 struct nfs4_stateowner *sop;
7161 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04007162 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04007164 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07007165 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03007166 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04007167 struct nfs4_client *clp;
Chuck Lever88584812016-07-13 16:40:14 -04007168 LIST_HEAD (reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169
7170 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
7171 clid->cl_boot, clid->cl_id);
7172
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05007173 status = set_client(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05007174 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04007175 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05007176
Trond Myklebustd4f04892014-07-29 21:34:36 -04007177 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04007178 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04007179 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04007180 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04007181 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04007182
7183 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05007184 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04007185
Jeff Layton882e9d22014-07-29 21:34:37 -04007186 /* see if there are still any locks associated with it */
7187 lo = lockowner(sop);
7188 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
7189 if (check_for_locks(stp->st_stid.sc_file, lo)) {
7190 status = nfserr_locks_held;
7191 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04007192 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04007193 }
Jeff Layton5adfd882014-07-29 21:34:31 -04007194 }
Jeff Layton882e9d22014-07-29 21:34:37 -04007195
Kinglong Meeb5971af2014-08-22 10:18:43 -04007196 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04007197 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04007198 }
Chuck Lever88584812016-07-13 16:40:14 -04007199 if (!lo) {
7200 spin_unlock(&clp->cl_lock);
7201 return status;
7202 }
7203
7204 unhash_lockowner_locked(lo);
7205 while (!list_empty(&lo->lo_owner.so_stateids)) {
7206 stp = list_first_entry(&lo->lo_owner.so_stateids,
7207 struct nfs4_ol_stateid,
7208 st_perstateowner);
7209 WARN_ON(!unhash_lock_stateid(stp));
7210 put_ol_stateid_locked(stp, &reaplist);
7211 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04007212 spin_unlock(&clp->cl_lock);
Chuck Lever88584812016-07-13 16:40:14 -04007213 free_ol_stateid_reaplist(&reaplist);
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007214 remove_blocked_locks(lo);
Chuck Lever88584812016-07-13 16:40:14 -04007215 nfs4_put_stateowner(&lo->lo_owner);
7216
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217 return status;
7218}
7219
7220static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07007221alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007222{
NeilBrowna55370a2005-06-23 22:03:52 -07007223 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224}
7225
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007226bool
Scott Mayhew6b189102019-03-26 18:06:26 -04007227nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07007228{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007229 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07007230
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007231 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007232 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07007233}
7234
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235/*
7236 * failure => all reset bets are off, nfserr_no_grace...
Scott Mayhew6b189102019-03-26 18:06:26 -04007237 *
7238 * The caller is responsible for freeing name.data if NULL is returned (it
7239 * will be freed in nfs4_remove_reclaim_record in the normal case).
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05007241struct nfs4_client_reclaim *
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007242nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
7243 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244{
7245 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05007246 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007247
NeilBrowna55370a2005-06-23 22:03:52 -07007248 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05007249 if (crp) {
7250 strhashval = clientstr_hashval(name);
7251 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007252 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Scott Mayhew6b189102019-03-26 18:06:26 -04007253 crp->cr_name.data = name.data;
7254 crp->cr_name.len = name.len;
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007255 crp->cr_princhash.data = princhash.data;
7256 crp->cr_princhash.len = princhash.len;
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007257 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007258 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05007259 }
7260 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007261}
7262
Jeff Layton2a4317c2012-03-21 16:42:43 -04007263void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007264nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05007265{
7266 list_del(&crp->cr_strhash);
Scott Mayhew6b189102019-03-26 18:06:26 -04007267 kfree(crp->cr_name.data);
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007268 kfree(crp->cr_princhash.data);
Jeff Laytonce30e532012-11-12 15:00:53 -05007269 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007270 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05007271}
7272
7273void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007274nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007275{
7276 struct nfs4_client_reclaim *crp = NULL;
7277 int i;
7278
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007280 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
7281 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007283 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284 }
7285 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05007286 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287}
7288
7289/*
7290 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04007291struct nfs4_client_reclaim *
Scott Mayhew6b189102019-03-26 18:06:26 -04007292nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007293{
7294 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295 struct nfs4_client_reclaim *crp = NULL;
7296
Scott Mayhew6b189102019-03-26 18:06:26 -04007297 strhashval = clientstr_hashval(name);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007298 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Scott Mayhew6b189102019-03-26 18:06:26 -04007299 if (compare_blob(&crp->cr_name, &name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007300 return crp;
7301 }
7302 }
7303 return NULL;
7304}
7305
Al Virob37ad282006-10-19 23:28:59 -07007306__be32
J. Bruce Fields1722b042021-01-21 17:57:44 -05007307nfs4_check_open_reclaim(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308{
J. Bruce Fields1722b042021-01-21 17:57:44 -05007309 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
Jeff Layton3b3e7b72014-09-12 16:40:22 -04007310 return nfserr_no_grace;
7311
J. Bruce Fields1722b042021-01-21 17:57:44 -05007312 if (nfsd4_client_record_check(clp))
Trond Myklebust0fe492d2014-06-30 11:48:47 -04007313 return nfserr_reclaim_bad;
7314
7315 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316}
7317
Meelap Shahc2f1a552007-07-17 04:04:39 -07007318/*
7319 * Since the lifetime of a delegation isn't limited to that of an open, a
7320 * client may quite reasonably hang on to a delegation as long as it has
7321 * the inode cached. This becomes an obvious problem the first time a
7322 * client's inode cache approaches the size of the server's total memory.
7323 *
7324 * For now we avoid this problem by imposing a hard limit on the number
7325 * of delegations, which varies according to the server's memory size.
7326 */
7327static void
7328set_max_delegations(void)
7329{
7330 /*
7331 * Allow at most 4 delegations per megabyte of RAM. Quick
7332 * estimates suggest that in the worst case (where every delegation
7333 * is for a different inode), a delegation could take about 1.5K,
7334 * giving a worst case usage of about 6% of memory.
7335 */
7336 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7337}
7338
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007339static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007340{
7341 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7342 int i;
7343
Kees Cook6da2ec52018-06-12 13:55:00 -07007344 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7345 sizeof(struct list_head),
7346 GFP_KERNEL);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007347 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007348 goto err;
Kees Cook6da2ec52018-06-12 13:55:00 -07007349 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7350 sizeof(struct list_head),
7351 GFP_KERNEL);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007352 if (!nn->unconf_id_hashtbl)
7353 goto err_unconf_id;
Kees Cook6da2ec52018-06-12 13:55:00 -07007354 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7355 sizeof(struct list_head),
7356 GFP_KERNEL);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007357 if (!nn->sessionid_hashtbl)
7358 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007359
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007360 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007361 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007362 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007363 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007364 for (i = 0; i < SESSION_HASH_SIZE; i++)
7365 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007366 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007367 nn->unconf_name_tree = RB_ROOT;
Arnd Bergmann9cc76802019-10-31 15:53:13 +01007368 nn->boot_time = ktime_get_real_seconds();
Vasily Averin81833de2017-11-13 07:25:40 +03007369 nn->grace_ended = false;
7370 nn->nfsd4_manager.block_opens = true;
7371 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03007372 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03007373 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007374 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03007375 spin_lock_init(&nn->client_lock);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04007376 spin_lock_init(&nn->s2s_cp_lock);
7377 idr_init(&nn->s2s_cp_stateids);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007378
Jeff Layton0cc11a62016-10-20 09:34:31 -04007379 spin_lock_init(&nn->blocked_locks_lock);
7380 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7381
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007382 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007383 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007384
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007385 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007386
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007387err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03007388 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007389err_unconf_id:
7390 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007391err:
7392 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007393}
7394
7395static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007396nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007397{
7398 int i;
7399 struct nfs4_client *clp = NULL;
7400 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7401
7402 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7403 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7404 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7405 destroy_client(clp);
7406 }
7407 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007408
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007409 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7410
Kinglong Mee2b905632014-03-26 22:09:30 +08007411 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7412 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7413 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7414 destroy_client(clp);
7415 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007416 }
7417
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007418 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007419 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007420 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007421 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007422}
7423
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007424int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007425nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07007426{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04007427 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007428 int ret;
7429
Trond Myklebustc6c7f2a2021-03-13 16:08:47 -05007430 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007431 if (ret)
7432 return ret;
Jeff Laytond4318ac2014-09-12 16:40:21 -04007433 locks_start_grace(net, &nn->nfsd4_manager);
7434 nfsd4_client_tracking_init(net);
Scott Mayhew362063a2019-03-26 18:06:28 -04007435 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7436 goto skip_grace;
Arnd Bergmann20b7d862019-11-04 16:31:52 +01007437 printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
Vasily Averin7e981a82017-11-06 16:46:04 +03007438 nn->nfsd4_grace, net->ns.inum);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04007439 trace_nfsd_grace_start(nn);
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03007440 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007441 return 0;
Scott Mayhew362063a2019-03-26 18:06:28 -04007442
7443skip_grace:
7444 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7445 net->ns.inum);
7446 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7447 nfsd4_end_grace(nn);
7448 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007449}
7450
7451/* initialization to perform when the nfsd service is started: */
7452
7453int
7454nfs4_state_start(void)
7455{
7456 int ret;
7457
Jeff Layton51a54562015-08-20 07:17:01 -04007458 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05007459 if (laundry_wq == NULL) {
7460 ret = -ENOMEM;
Chuck Levera26dd642018-08-16 12:06:09 -04007461 goto out;
Jeff Laytona6d6b782012-03-05 11:42:36 -05007462 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007463 ret = nfsd4_create_callback_queue();
7464 if (ret)
7465 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007466
Meelap Shahc2f1a552007-07-17 04:04:39 -07007467 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007468 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007469
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007470out_free_laundry:
7471 destroy_workqueue(laundry_wq);
Chuck Levera26dd642018-08-16 12:06:09 -04007472out:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007473 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474}
7475
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007476void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007477nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007481 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007483 cancel_delayed_work_sync(&nn->laundromat_work);
7484 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05007485
Linus Torvalds1da177e2005-04-16 15:20:36 -07007486 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04007487 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007488 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007490 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007491 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007492 }
Benny Halevycdc975052014-05-30 09:09:30 -04007493 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007494 list_for_each_safe(pos, next, &reaplist) {
7495 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04007496 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05007497 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007498 }
7499
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007500 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007501 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502}
7503
7504void
7505nfs4_state_shutdown(void)
7506{
NeilBrown5e8d5c22006-04-10 22:55:37 -07007507 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04007508 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007509}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007510
7511static void
7512get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7513{
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007514 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
7515 CURRENT_STATEID(stateid))
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007516 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007517}
7518
7519static void
7520put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7521{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007522 if (cstate->minorversion) {
7523 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007524 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007525 }
7526}
7527
7528void
7529clear_current_stateid(struct nfsd4_compound_state *cstate)
7530{
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007531 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007532}
7533
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007534/*
7535 * functions to set current state id
7536 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007537void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007538nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7539 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007540{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007541 put_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007542}
7543
7544void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007545nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7546 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007547{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007548 put_stateid(cstate, &u->open.op_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007549}
7550
7551void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007552nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7553 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007554{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007555 put_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007556}
7557
7558void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007559nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7560 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007561{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007562 put_stateid(cstate, &u->lock.lk_resp_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007563}
7564
7565/*
7566 * functions to consume current state id
7567 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007568
7569void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007570nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7571 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007572{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007573 get_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007574}
7575
7576void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007577nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7578 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007579{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007580 get_stateid(cstate, &u->delegreturn.dr_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007581}
7582
7583void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007584nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7585 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007586{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007587 get_stateid(cstate, &u->free_stateid.fr_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007588}
7589
7590void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007591nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7592 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007593{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007594 get_stateid(cstate, &u->setattr.sa_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007595}
7596
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007597void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007598nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7599 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007600{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007601 get_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007602}
7603
7604void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007605nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7606 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007607{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007608 get_stateid(cstate, &u->locku.lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007609}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007610
7611void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007612nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7613 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007614{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007615 get_stateid(cstate, &u->read.rd_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007616}
7617
7618void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007619nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7620 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007621{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007622 get_stateid(cstate, &u->write.wr_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007623}