blob: a6c722dc7e5e66394511926eb599f45469a66c7f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Chuck Lever7df302f2012-05-29 13:56:37 -040041#include <linux/ratelimit.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050042#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton59766872013-02-04 12:50:00 -050043#include <linux/sunrpc/addr.h>
Daniel Borkmann87545892014-12-10 16:33:11 +010044#include <linux/jhash.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020045#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040046#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050047#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040048#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040050#include "netns.h"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020051#include "pnfs.h"
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define NFSDDBG_FACILITY NFSDDBG_PROC
54
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050055#define all_ones {{~0,~0},~0}
56static const stateid_t one_stateid = {
57 .si_generation = ~0,
58 .si_opaque = all_ones,
59};
60static const stateid_t zero_stateid = {
61 /* all fields zero */
62};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010063static const stateid_t currentstateid = {
64 .si_generation = 1,
65};
Trond Myklebustfb500a72017-11-03 08:00:12 -040066static const stateid_t close_stateid = {
67 .si_generation = 0xffffffffU,
68};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050069
Andy Adamsonec6b5d72009-04-03 08:28:28 +030070static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070071
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050072#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
73#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010074#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Andrew Elbleae254dac2017-11-09 13:41:10 -050075#define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040078static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040079static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Scott Mayhew362063a2019-03-26 18:06:28 -040080void nfsd4_end_grace(struct nfsd_net *nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
J. Bruce Fields8b671b82009-02-22 14:51:34 -080082/* Locking: */
83
J. Bruce Fields8b671b82009-02-22 14:51:34 -080084/*
85 * Currently used for the del_recall_lru and file hash table. In an
86 * effort to decrease the scope of the client_mutex, this spinlock may
87 * eventually cover more:
88 */
Benny Halevycdc975052014-05-30 09:09:30 -040089static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080090
Andrew Elble4f34bd02017-11-08 17:29:51 -050091enum nfsd4_st_mutex_lock_subclass {
92 OPEN_STATEID_MUTEX = 0,
93 LOCK_STATEID_MUTEX = 1,
94};
95
Jeff Laytonb401be222014-07-29 21:34:33 -040096/*
97 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
98 * the refcount on the open stateid to drop.
99 */
100static DECLARE_WAIT_QUEUE_HEAD(close_wq);
101
Jeff Layton9258a2d2018-03-16 09:47:22 -0400102static struct kmem_cache *client_slab;
Christoph Hellwigabf11352014-05-21 07:43:03 -0700103static struct kmem_cache *openowner_slab;
104static struct kmem_cache *lockowner_slab;
105static struct kmem_cache *file_slab;
106static struct kmem_cache *stateid_slab;
107static struct kmem_cache *deleg_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +0200108static struct kmem_cache *odstate_slab;
NeilBrowne60d4392005-06-23 22:03:01 -0700109
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400110static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800111
Julia Lawallc4cb8972015-11-21 22:57:39 +0100112static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
Jeff Layton76d348f2016-09-16 16:28:24 -0400113static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200114
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400115static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800116{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400117 return ses->se_flags & NFS4_SESSION_DEAD;
118}
119
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400120static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
121{
122 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400123 return nfserr_jukebox;
124 ses->se_flags |= NFS4_SESSION_DEAD;
125 return nfs_ok;
126}
127
J. Bruce Fields221a6872013-04-01 22:23:49 -0400128static bool is_client_expired(struct nfs4_client *clp)
129{
130 return clp->cl_time == 0;
131}
132
J. Bruce Fields221a6872013-04-01 22:23:49 -0400133static __be32 get_client_locked(struct nfs4_client *clp)
134{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400135 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
136
137 lockdep_assert_held(&nn->client_lock);
138
J. Bruce Fields221a6872013-04-01 22:23:49 -0400139 if (is_client_expired(clp))
140 return nfserr_expired;
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400141 atomic_inc(&clp->cl_rpc_users);
J. Bruce Fields221a6872013-04-01 22:23:49 -0400142 return nfs_ok;
143}
144
145/* must be called under the client_lock */
146static inline void
147renew_client_locked(struct nfs4_client *clp)
148{
149 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
150
151 if (is_client_expired(clp)) {
152 WARN_ON(1);
153 printk("%s: client (clientid %08x/%08x) already expired\n",
154 __func__,
155 clp->cl_clientid.cl_boot,
156 clp->cl_clientid.cl_id);
157 return;
158 }
159
160 dprintk("renewing client (clientid %08x/%08x)\n",
161 clp->cl_clientid.cl_boot,
162 clp->cl_clientid.cl_id);
163 list_move_tail(&clp->cl_lru, &nn->client_lru);
164 clp->cl_time = get_seconds();
165}
166
Fengguang Wuba138432013-04-16 22:14:15 -0400167static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400168{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400169 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
170
171 lockdep_assert_held(&nn->client_lock);
172
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400173 if (!atomic_dec_and_test(&clp->cl_rpc_users))
J. Bruce Fields221a6872013-04-01 22:23:49 -0400174 return;
175 if (!is_client_expired(clp))
176 renew_client_locked(clp);
177}
178
Jeff Layton4b24ca72014-06-30 11:48:44 -0400179static void put_client_renew(struct nfs4_client *clp)
180{
181 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
182
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400183 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
Jeff Laytond6c249b2014-07-08 14:02:50 -0400184 return;
185 if (!is_client_expired(clp))
186 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400187 spin_unlock(&nn->client_lock);
188}
189
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400190static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
191{
192 __be32 status;
193
194 if (is_session_dead(ses))
195 return nfserr_badsession;
196 status = get_client_locked(ses->se_client);
197 if (status)
198 return status;
199 atomic_inc(&ses->se_ref);
200 return nfs_ok;
201}
202
203static void nfsd4_put_session_locked(struct nfsd4_session *ses)
204{
205 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400206 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
207
208 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400209
210 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
211 free_session(ses);
212 put_client_renew_locked(clp);
213}
214
215static void nfsd4_put_session(struct nfsd4_session *ses)
216{
217 struct nfs4_client *clp = ses->se_client;
218 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
219
220 spin_lock(&nn->client_lock);
221 nfsd4_put_session_locked(ses);
222 spin_unlock(&nn->client_lock);
223}
224
Jeff Layton76d348f2016-09-16 16:28:24 -0400225static struct nfsd4_blocked_lock *
226find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
227 struct nfsd_net *nn)
228{
229 struct nfsd4_blocked_lock *cur, *found = NULL;
230
Jeff Layton0cc11a62016-10-20 09:34:31 -0400231 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400232 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
233 if (fh_match(fh, &cur->nbl_fh)) {
234 list_del_init(&cur->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -0400235 list_del_init(&cur->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400236 found = cur;
237 break;
238 }
239 }
Jeff Layton0cc11a62016-10-20 09:34:31 -0400240 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400241 if (found)
NeilBrowncb03f942018-11-30 10:04:08 +1100242 locks_delete_block(&found->nbl_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400243 return found;
244}
245
246static struct nfsd4_blocked_lock *
247find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
248 struct nfsd_net *nn)
249{
250 struct nfsd4_blocked_lock *nbl;
251
252 nbl = find_blocked_lock(lo, fh, nn);
253 if (!nbl) {
254 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
255 if (nbl) {
256 fh_copy_shallow(&nbl->nbl_fh, fh);
257 locks_init_lock(&nbl->nbl_lock);
258 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
259 &nfsd4_cb_notify_lock_ops,
260 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
261 }
262 }
263 return nbl;
264}
265
266static void
267free_blocked_lock(struct nfsd4_blocked_lock *nbl)
268{
Jeff Layton6aaafc42019-04-22 12:34:23 -0400269 locks_delete_block(&nbl->nbl_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400270 locks_release_private(&nbl->nbl_lock);
271 kfree(nbl);
272}
273
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400274static void
275remove_blocked_locks(struct nfs4_lockowner *lo)
276{
277 struct nfs4_client *clp = lo->lo_owner.so_client;
278 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
279 struct nfsd4_blocked_lock *nbl;
280 LIST_HEAD(reaplist);
281
282 /* Dequeue all blocked locks */
283 spin_lock(&nn->blocked_locks_lock);
284 while (!list_empty(&lo->lo_blocked)) {
285 nbl = list_first_entry(&lo->lo_blocked,
286 struct nfsd4_blocked_lock,
287 nbl_list);
288 list_del_init(&nbl->nbl_list);
289 list_move(&nbl->nbl_lru, &reaplist);
290 }
291 spin_unlock(&nn->blocked_locks_lock);
292
293 /* Now free them */
294 while (!list_empty(&reaplist)) {
295 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
296 nbl_lru);
297 list_del_init(&nbl->nbl_lru);
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400298 free_blocked_lock(nbl);
299 }
300}
301
Jeff Laytonf4564582019-04-22 12:34:24 -0400302static void
303nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
304{
305 struct nfsd4_blocked_lock *nbl = container_of(cb,
306 struct nfsd4_blocked_lock, nbl_cb);
307 locks_delete_block(&nbl->nbl_lock);
308}
309
Jeff Layton76d348f2016-09-16 16:28:24 -0400310static int
311nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
312{
313 /*
314 * Since this is just an optimization, we don't try very hard if it
315 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
316 * just quit trying on anything else.
317 */
318 switch (task->tk_status) {
319 case -NFS4ERR_DELAY:
320 rpc_delay(task, 1 * HZ);
321 return 0;
322 default:
323 return 1;
324 }
325}
326
327static void
328nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
329{
330 struct nfsd4_blocked_lock *nbl = container_of(cb,
331 struct nfsd4_blocked_lock, nbl_cb);
332
333 free_blocked_lock(nbl);
334}
335
336static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
Jeff Laytonf4564582019-04-22 12:34:24 -0400337 .prepare = nfsd4_cb_notify_lock_prepare,
Jeff Layton76d348f2016-09-16 16:28:24 -0400338 .done = nfsd4_cb_notify_lock_done,
339 .release = nfsd4_cb_notify_lock_release,
340};
341
Kinglong Meeb5971af2014-08-22 10:18:43 -0400342static inline struct nfs4_stateowner *
343nfs4_get_stateowner(struct nfs4_stateowner *sop)
344{
345 atomic_inc(&sop->so_count);
346 return sop;
347}
348
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400349static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400350same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400351{
352 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400353 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400354}
355
356static struct nfs4_openowner *
357find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400358 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400359{
360 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400361
Trond Myklebustd4f04892014-07-29 21:34:36 -0400362 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400363
Trond Myklebustd4f04892014-07-29 21:34:36 -0400364 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
365 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400366 if (!so->so_is_open_owner)
367 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400368 if (same_owner_str(so, &open->op_owner))
369 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400370 }
371 return NULL;
372}
373
374static struct nfs4_openowner *
375find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400376 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400377{
378 struct nfs4_openowner *oo;
379
Trond Myklebustd4f04892014-07-29 21:34:36 -0400380 spin_lock(&clp->cl_lock);
381 oo = find_openstateowner_str_locked(hashval, open, clp);
382 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400383 return oo;
384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386static inline u32
387opaque_hashval(const void *ptr, int nbytes)
388{
389 unsigned char *cptr = (unsigned char *) ptr;
390
391 u32 x = 0;
392 while (nbytes--) {
393 x *= 37;
394 x += *cptr++;
395 }
396 return x;
397}
398
Jeff Layton5b095e92014-10-23 08:01:02 -0400399static void nfsd4_free_file_rcu(struct rcu_head *rcu)
J. Bruce Fields32513b42011-10-13 16:00:16 -0400400{
Jeff Layton5b095e92014-10-23 08:01:02 -0400401 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
402
403 kmem_cache_free(file_slab, fp);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400404}
405
Christoph Hellwige6ba76e2014-08-14 08:50:16 +0200406void
NeilBrown13cd2182005-06-23 22:03:10 -0700407put_nfs4_file(struct nfs4_file *fi)
408{
Jeff Layton02e12152014-07-16 10:31:57 -0400409 might_lock(&state_lock);
410
Elena Reshetova818a34e2017-10-20 12:53:30 +0300411 if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton5b095e92014-10-23 08:01:02 -0400412 hlist_del_rcu(&fi->fi_hash);
Benny Halevycdc975052014-05-30 09:09:30 -0400413 spin_unlock(&state_lock);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200414 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
Jeff Layton5b095e92014-10-23 08:01:02 -0400415 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
416 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800417 }
NeilBrown13cd2182005-06-23 22:03:10 -0700418}
419
Trond Myklebustde186432014-07-10 14:07:26 -0400420static struct file *
421__nfs4_get_fd(struct nfs4_file *f, int oflag)
422{
423 if (f->fi_fds[oflag])
424 return get_file(f->fi_fds[oflag]);
425 return NULL;
426}
427
428static struct file *
429find_writeable_file_locked(struct nfs4_file *f)
430{
431 struct file *ret;
432
433 lockdep_assert_held(&f->fi_lock);
434
435 ret = __nfs4_get_fd(f, O_WRONLY);
436 if (!ret)
437 ret = __nfs4_get_fd(f, O_RDWR);
438 return ret;
439}
440
441static struct file *
442find_writeable_file(struct nfs4_file *f)
443{
444 struct file *ret;
445
446 spin_lock(&f->fi_lock);
447 ret = find_writeable_file_locked(f);
448 spin_unlock(&f->fi_lock);
449
450 return ret;
451}
452
453static struct file *find_readable_file_locked(struct nfs4_file *f)
454{
455 struct file *ret;
456
457 lockdep_assert_held(&f->fi_lock);
458
459 ret = __nfs4_get_fd(f, O_RDONLY);
460 if (!ret)
461 ret = __nfs4_get_fd(f, O_RDWR);
462 return ret;
463}
464
465static struct file *
466find_readable_file(struct nfs4_file *f)
467{
468 struct file *ret;
469
470 spin_lock(&f->fi_lock);
471 ret = find_readable_file_locked(f);
472 spin_unlock(&f->fi_lock);
473
474 return ret;
475}
476
Christoph Hellwig4d227fc2014-08-17 07:40:00 -0500477struct file *
Trond Myklebustde186432014-07-10 14:07:26 -0400478find_any_file(struct nfs4_file *f)
479{
480 struct file *ret;
481
482 spin_lock(&f->fi_lock);
483 ret = __nfs4_get_fd(f, O_RDWR);
484 if (!ret) {
485 ret = __nfs4_get_fd(f, O_WRONLY);
486 if (!ret)
487 ret = __nfs4_get_fd(f, O_RDONLY);
488 }
489 spin_unlock(&f->fi_lock);
490 return ret;
491}
492
Trond Myklebust02a35082014-07-25 07:34:22 -0400493static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800494unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700495
496/*
497 * Open owner state (share locks)
498 */
499
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500500/* hash tables for lock and open owners */
501#define OWNER_HASH_BITS 8
502#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
503#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700504
Trond Myklebustd4f04892014-07-29 21:34:36 -0400505static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400506{
507 unsigned int ret;
508
509 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500510 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400511}
NeilBrownef0f3392006-04-10 22:55:41 -0700512
NeilBrownef0f3392006-04-10 22:55:41 -0700513/* hash table for nfs4_file */
514#define FILE_HASH_BITS 8
515#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800516
Trond Myklebustca943212014-07-23 16:17:39 -0400517static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400518{
Trond Myklebustca943212014-07-23 16:17:39 -0400519 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
520}
521
522static unsigned int file_hashval(struct knfsd_fh *fh)
523{
524 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
525}
526
Jeff Layton89876f82013-04-02 09:01:59 -0400527static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700528
Jeff Layton12659652014-07-10 14:07:28 -0400529static void
530__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400531{
Jeff Layton7214e862014-07-10 14:07:33 -0400532 lockdep_assert_held(&fp->fi_lock);
533
Jeff Layton12659652014-07-10 14:07:28 -0400534 if (access & NFS4_SHARE_ACCESS_WRITE)
535 atomic_inc(&fp->fi_access[O_WRONLY]);
536 if (access & NFS4_SHARE_ACCESS_READ)
537 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400538}
539
Jeff Layton12659652014-07-10 14:07:28 -0400540static __be32
541nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400542{
Jeff Layton7214e862014-07-10 14:07:33 -0400543 lockdep_assert_held(&fp->fi_lock);
544
Jeff Layton12659652014-07-10 14:07:28 -0400545 /* Does this access mode make sense? */
546 if (access & ~NFS4_SHARE_ACCESS_BOTH)
547 return nfserr_inval;
548
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400549 /* Does it conflict with a deny mode already set? */
550 if ((access & fp->fi_share_deny) != 0)
551 return nfserr_share_denied;
552
Jeff Layton12659652014-07-10 14:07:28 -0400553 __nfs4_file_get_access(fp, access);
554 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400555}
556
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400557static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
558{
559 /* Common case is that there is no deny mode. */
560 if (deny) {
561 /* Does this deny mode make sense? */
562 if (deny & ~NFS4_SHARE_DENY_BOTH)
563 return nfserr_inval;
564
565 if ((deny & NFS4_SHARE_DENY_READ) &&
566 atomic_read(&fp->fi_access[O_RDONLY]))
567 return nfserr_share_denied;
568
569 if ((deny & NFS4_SHARE_DENY_WRITE) &&
570 atomic_read(&fp->fi_access[O_WRONLY]))
571 return nfserr_share_denied;
572 }
573 return nfs_ok;
574}
575
J. Bruce Fields998db522010-08-07 09:21:41 -0400576static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400577{
Trond Myklebustde186432014-07-10 14:07:26 -0400578 might_lock(&fp->fi_lock);
579
580 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
581 struct file *f1 = NULL;
582 struct file *f2 = NULL;
583
Jeff Layton6d338b52014-07-10 14:07:29 -0400584 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400585 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400586 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400587 spin_unlock(&fp->fi_lock);
588 if (f1)
589 fput(f1);
590 if (f2)
591 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400592 }
593}
594
Jeff Layton12659652014-07-10 14:07:28 -0400595static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400596{
Jeff Layton12659652014-07-10 14:07:28 -0400597 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
598
599 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400600 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400601 if (access & NFS4_SHARE_ACCESS_READ)
602 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400603}
604
Sachin Bhamare8287f002015-04-27 14:50:14 +0200605/*
606 * Allocate a new open/delegation state counter. This is needed for
607 * pNFS for proper return on close semantics.
608 *
609 * Note that we only allocate it for pNFS-enabled exports, otherwise
610 * all pointers to struct nfs4_clnt_odstate are always NULL.
611 */
612static struct nfs4_clnt_odstate *
613alloc_clnt_odstate(struct nfs4_client *clp)
614{
615 struct nfs4_clnt_odstate *co;
616
617 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
618 if (co) {
619 co->co_client = clp;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300620 refcount_set(&co->co_odcount, 1);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200621 }
622 return co;
623}
624
625static void
626hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
627{
628 struct nfs4_file *fp = co->co_file;
629
630 lockdep_assert_held(&fp->fi_lock);
631 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
632}
633
634static inline void
635get_clnt_odstate(struct nfs4_clnt_odstate *co)
636{
637 if (co)
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300638 refcount_inc(&co->co_odcount);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200639}
640
641static void
642put_clnt_odstate(struct nfs4_clnt_odstate *co)
643{
644 struct nfs4_file *fp;
645
646 if (!co)
647 return;
648
649 fp = co->co_file;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300650 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
Sachin Bhamare8287f002015-04-27 14:50:14 +0200651 list_del(&co->co_perfile);
652 spin_unlock(&fp->fi_lock);
653
654 nfsd4_return_all_file_layouts(co->co_client, fp);
655 kmem_cache_free(odstate_slab, co);
656 }
657}
658
659static struct nfs4_clnt_odstate *
660find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
661{
662 struct nfs4_clnt_odstate *co;
663 struct nfs4_client *cl;
664
665 if (!new)
666 return NULL;
667
668 cl = new->co_client;
669
670 spin_lock(&fp->fi_lock);
671 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
672 if (co->co_client == cl) {
673 get_clnt_odstate(co);
674 goto out;
675 }
676 }
677 co = new;
678 co->co_file = fp;
679 hash_clnt_odstate_locked(new);
680out:
681 spin_unlock(&fp->fi_lock);
682 return co;
683}
684
Kinglong Meed19fb702017-01-18 19:04:42 +0800685struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
686 void (*sc_free)(struct nfs4_stid *))
J. Bruce Fields996e0932011-10-17 11:14:48 -0400687{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500688 struct nfs4_stid *stid;
689 int new_id;
690
Trond Myklebustf8338832014-07-25 07:34:19 -0400691 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500692 if (!stid)
693 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400694
Jeff Layton4770d722014-07-29 21:34:10 -0400695 idr_preload(GFP_KERNEL);
696 spin_lock(&cl->cl_lock);
697 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
698 spin_unlock(&cl->cl_lock);
699 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700700 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500701 goto out_free;
Kinglong Meed19fb702017-01-18 19:04:42 +0800702
703 stid->sc_free = sc_free;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500704 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500705 stid->sc_stateid.si_opaque.so_id = new_id;
706 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
707 /* Will be incremented before return to client: */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300708 refcount_set(&stid->sc_count, 1);
Jeff Layton9767feb2015-10-01 09:05:50 -0400709 spin_lock_init(&stid->sc_lock);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500710
J. Bruce Fields996e0932011-10-17 11:14:48 -0400711 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500712 * It shouldn't be a problem to reuse an opaque stateid value.
713 * I don't think it is for 4.1. But with 4.0 I worry that, for
714 * example, a stray write retransmission could be accepted by
715 * the server when it should have been rejected. Therefore,
716 * adopt a trick from the sctp code to attempt to maximize the
717 * amount of time until an id is reused, by ensuring they always
718 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400719 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500720 return stid;
721out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800722 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500723 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400724}
725
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400726/*
727 * Create a unique stateid_t to represent each COPY.
728 */
729int nfs4_init_cp_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
730{
731 int new_id;
732
733 idr_preload(GFP_KERNEL);
734 spin_lock(&nn->s2s_cp_lock);
735 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, copy, 0, 0, GFP_NOWAIT);
736 spin_unlock(&nn->s2s_cp_lock);
737 idr_preload_end();
738 if (new_id < 0)
739 return 0;
740 copy->cp_stateid.si_opaque.so_id = new_id;
741 copy->cp_stateid.si_opaque.so_clid.cl_boot = nn->boot_time;
742 copy->cp_stateid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
743 return 1;
744}
745
746void nfs4_free_cp_state(struct nfsd4_copy *copy)
747{
748 struct nfsd_net *nn;
749
750 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
751 spin_lock(&nn->s2s_cp_lock);
752 idr_remove(&nn->s2s_cp_stateids, copy->cp_stateid.si_opaque.so_id);
753 spin_unlock(&nn->s2s_cp_lock);
754}
755
Jeff Laytonb49e0842014-07-29 21:34:11 -0400756static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400757{
Trond Myklebust60116952014-07-29 21:34:06 -0400758 struct nfs4_stid *stid;
Trond Myklebust60116952014-07-29 21:34:06 -0400759
Kinglong Meed19fb702017-01-18 19:04:42 +0800760 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
Trond Myklebust60116952014-07-29 21:34:06 -0400761 if (!stid)
762 return NULL;
763
Kinglong Meed19fb702017-01-18 19:04:42 +0800764 return openlockstateid(stid);
Trond Myklebust60116952014-07-29 21:34:06 -0400765}
766
767static void nfs4_free_deleg(struct nfs4_stid *stid)
768{
Trond Myklebust60116952014-07-29 21:34:06 -0400769 kmem_cache_free(deleg_slab, stid);
770 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400771}
772
NeilBrown6282cd52014-06-04 17:39:26 +1000773/*
774 * When we recall a delegation, we should be careful not to hand it
775 * out again straight away.
776 * To ensure this we keep a pair of bloom filters ('new' and 'old')
777 * in which the filehandles of recalled delegations are "stored".
778 * If a filehandle appear in either filter, a delegation is blocked.
779 * When a delegation is recalled, the filehandle is stored in the "new"
780 * filter.
781 * Every 30 seconds we swap the filters and clear the "new" one,
782 * unless both are empty of course.
783 *
784 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
785 * low 3 bytes as hash-table indices.
786 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400787 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000788 * is used to manage concurrent access. Testing does not need the lock
789 * except when swapping the two filters.
790 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400791static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000792static struct bloom_pair {
793 int entries, old_entries;
794 time_t swap_time;
795 int new; /* index into 'set' */
796 DECLARE_BITMAP(set[2], 256);
797} blocked_delegations;
798
799static int delegation_blocked(struct knfsd_fh *fh)
800{
801 u32 hash;
802 struct bloom_pair *bd = &blocked_delegations;
803
804 if (bd->entries == 0)
805 return 0;
806 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400807 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000808 if (seconds_since_boot() - bd->swap_time > 30) {
809 bd->entries -= bd->old_entries;
810 bd->old_entries = bd->entries;
811 memset(bd->set[bd->new], 0,
812 sizeof(bd->set[0]));
813 bd->new = 1-bd->new;
814 bd->swap_time = seconds_since_boot();
815 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400816 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000817 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100818 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000819 if (test_bit(hash&255, bd->set[0]) &&
820 test_bit((hash>>8)&255, bd->set[0]) &&
821 test_bit((hash>>16)&255, bd->set[0]))
822 return 1;
823
824 if (test_bit(hash&255, bd->set[1]) &&
825 test_bit((hash>>8)&255, bd->set[1]) &&
826 test_bit((hash>>16)&255, bd->set[1]))
827 return 1;
828
829 return 0;
830}
831
832static void block_delegations(struct knfsd_fh *fh)
833{
834 u32 hash;
835 struct bloom_pair *bd = &blocked_delegations;
836
Daniel Borkmann87545892014-12-10 16:33:11 +0100837 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000838
Jeff Laytonf54fe962014-07-25 07:34:26 -0400839 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000840 __set_bit(hash&255, bd->set[bd->new]);
841 __set_bit((hash>>8)&255, bd->set[bd->new]);
842 __set_bit((hash>>16)&255, bd->set[bd->new]);
843 if (bd->entries == 0)
844 bd->swap_time = seconds_since_boot();
845 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400846 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000847}
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849static struct nfs4_delegation *
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500850alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
851 struct svc_fh *current_fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +0200852 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
854 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400855 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400858 n = atomic_long_inc_return(&num_delegations);
859 if (n < 0 || n > max_delegations)
860 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000861 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400862 goto out_dec;
Kinglong Meed19fb702017-01-18 19:04:42 +0800863 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700864 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400865 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400866
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400867 /*
868 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400869 * meaning of seqid 0 isn't meaningful, really, but let's avoid
870 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400871 */
872 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700873 INIT_LIST_HEAD(&dp->dl_perfile);
874 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200876 dp->dl_clnt_odstate = odstate;
877 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400878 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200879 dp->dl_retries = 1;
880 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200881 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500882 get_nfs4_file(fp);
883 dp->dl_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400885out_dec:
886 atomic_long_dec(&num_delegations);
887 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
890void
Trond Myklebust60116952014-07-29 21:34:06 -0400891nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Trond Myklebust11b91642014-07-29 21:34:08 -0400893 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400894 struct nfs4_client *clp = s->sc_client;
895
Jeff Layton4770d722014-07-29 21:34:10 -0400896 might_lock(&clp->cl_lock);
897
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300898 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
Jeff Laytonb401be222014-07-29 21:34:33 -0400899 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400900 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400901 }
Trond Myklebust60116952014-07-29 21:34:06 -0400902 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400903 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400904 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400905 if (fp)
906 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
Jeff Layton9767feb2015-10-01 09:05:50 -0400909void
910nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
911{
912 stateid_t *src = &stid->sc_stateid;
913
914 spin_lock(&stid->sc_lock);
915 if (unlikely(++src->si_generation == 0))
916 src->si_generation = 1;
917 memcpy(dst, src, sizeof(*dst));
918 spin_unlock(&stid->sc_lock);
919}
920
J. Bruce Fields353601e2018-02-16 14:29:42 -0500921static void put_deleg_file(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Jeff Layton6bcc0342014-08-09 10:22:40 -0400923 struct file *filp = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -0500924
925 spin_lock(&fp->fi_lock);
926 if (--fp->fi_delegees == 0)
927 swap(filp, fp->fi_deleg_file);
928 spin_unlock(&fp->fi_lock);
929
930 if (filp)
931 fput(filp);
932}
933
934static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
935{
J. Bruce Fieldscba7b3d2018-02-15 15:50:49 -0500936 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fields353601e2018-02-16 14:29:42 -0500937 struct file *filp = fp->fi_deleg_file;
Jeff Layton417c6622014-07-21 09:34:57 -0400938
J. Bruce Fieldsb8232d32018-02-15 15:29:15 -0500939 WARN_ON_ONCE(!fp->fi_delegees);
940
J. Bruce Fields353601e2018-02-16 14:29:42 -0500941 vfs_setlease(filp, F_UNLCK, NULL, (void **)&dp);
942 put_deleg_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
J. Bruce Fields0af6e692018-02-21 15:11:03 -0500945static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
946{
947 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -0500948 nfs4_unlock_deleg_lease(dp);
J. Bruce Fields0af6e692018-02-21 15:11:03 -0500949 nfs4_put_stid(&dp->dl_stid);
950}
951
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200952void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400953{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500954 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400955}
956
Andrew Elble34ed9872015-10-15 12:07:28 -0400957/**
J. Bruce Fields68b18f52018-02-19 11:38:33 -0500958 * nfs4_delegation_exists - Discover if this delegation already exists
Andrew Elble34ed9872015-10-15 12:07:28 -0400959 * @clp: a pointer to the nfs4_client we're granting a delegation to
960 * @fp: a pointer to the nfs4_file we're granting a delegation on
961 *
962 * Return:
J. Bruce Fields68b18f52018-02-19 11:38:33 -0500963 * On success: true iff an existing delegation is found
Andrew Elble34ed9872015-10-15 12:07:28 -0400964 */
965
J. Bruce Fields68b18f52018-02-19 11:38:33 -0500966static bool
967nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
Benny Halevy931ee562014-05-30 09:09:27 -0400968{
Andrew Elble34ed9872015-10-15 12:07:28 -0400969 struct nfs4_delegation *searchdp = NULL;
970 struct nfs4_client *searchclp = NULL;
971
Benny Halevycdc975052014-05-30 09:09:30 -0400972 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400973 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400974
Andrew Elble34ed9872015-10-15 12:07:28 -0400975 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
976 searchclp = searchdp->dl_stid.sc_client;
977 if (clp == searchclp) {
Fengguang Wu51d87bc2018-03-22 13:37:20 +0800978 return true;
Andrew Elble34ed9872015-10-15 12:07:28 -0400979 }
980 }
Fengguang Wu51d87bc2018-03-22 13:37:20 +0800981 return false;
Andrew Elble34ed9872015-10-15 12:07:28 -0400982}
983
984/**
985 * hash_delegation_locked - Add a delegation to the appropriate lists
986 * @dp: a pointer to the nfs4_delegation we are adding.
987 * @fp: a pointer to the nfs4_file we're granting a delegation on
988 *
989 * Return:
990 * On success: NULL if the delegation was successfully hashed.
991 *
992 * On error: -EAGAIN if one was previously granted to this
993 * nfs4_client for this nfs4_file. Delegation is not hashed.
994 *
995 */
996
997static int
998hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
999{
Andrew Elble34ed9872015-10-15 12:07:28 -04001000 struct nfs4_client *clp = dp->dl_stid.sc_client;
1001
1002 lockdep_assert_held(&state_lock);
1003 lockdep_assert_held(&fp->fi_lock);
1004
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001005 if (nfs4_delegation_exists(clp, fp))
1006 return -EAGAIN;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001007 refcount_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -04001008 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -04001009 list_add(&dp->dl_perfile, &fp->fi_delegations);
Andrew Elble34ed9872015-10-15 12:07:28 -04001010 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1011 return 0;
Benny Halevy931ee562014-05-30 09:09:27 -04001012}
1013
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001014static bool
Jeff Layton42690672014-07-25 07:34:20 -04001015unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Trond Myklebust11b91642014-07-29 21:34:08 -04001017 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -04001018
Jeff Layton42690672014-07-25 07:34:20 -04001019 lockdep_assert_held(&state_lock);
1020
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001021 if (list_empty(&dp->dl_perfile))
1022 return false;
1023
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04001024 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -04001025 /* Ensure that deleg break won't try to requeue it */
1026 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -04001027 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001028 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -04001030 list_del_init(&dp->dl_perfile);
1031 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001032 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001033}
1034
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001035static void destroy_delegation(struct nfs4_delegation *dp)
1036{
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001037 bool unhashed;
1038
Jeff Layton42690672014-07-25 07:34:20 -04001039 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001040 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -04001041 spin_unlock(&state_lock);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001042 if (unhashed)
1043 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001044}
1045
1046static void revoke_delegation(struct nfs4_delegation *dp)
1047{
1048 struct nfs4_client *clp = dp->dl_stid.sc_client;
1049
Jeff Layton2d4a5322014-07-25 07:34:21 -04001050 WARN_ON(!list_empty(&dp->dl_recall_lru));
1051
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001052 if (clp->cl_minorversion) {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001053 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001054 refcount_inc(&dp->dl_stid.sc_count);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001055 spin_lock(&clp->cl_lock);
1056 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1057 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001058 }
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001059 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001060}
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062/*
1063 * SETCLIENTID state
1064 */
1065
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001066static unsigned int clientid_hashval(u32 id)
1067{
1068 return id & CLIENT_HASH_MASK;
1069}
1070
Scott Mayhew6b189102019-03-26 18:06:26 -04001071static unsigned int clientstr_hashval(struct xdr_netobj name)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001072{
Scott Mayhew6b189102019-03-26 18:06:26 -04001073 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001074}
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001077 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1078 * st_{access,deny}_bmap field of the stateid, in order to track not
1079 * only what share bits are currently in force, but also what
1080 * combinations of share bits previous opens have used. This allows us
1081 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1082 * return an error if the client attempt to downgrade to a combination
1083 * of share bits not explicable by closing some of its previous opens.
1084 *
1085 * XXX: This enforcement is actually incomplete, since we don't keep
1086 * track of access/deny bit combinations; so, e.g., we allow:
1087 *
1088 * OPEN allow read, deny write
1089 * OPEN allow both, deny none
1090 * DOWNGRADE allow read, deny none
1091 *
1092 * which we should reject.
1093 */
Jeff Layton5ae037e2012-05-11 09:45:11 -04001094static unsigned int
1095bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001096 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -04001097 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001098
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001099 for (i = 1; i < 4; i++) {
1100 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -04001101 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001102 }
Jeff Layton5ae037e2012-05-11 09:45:11 -04001103 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001104}
1105
Jeff Layton82c5ff12012-05-11 09:45:13 -04001106/* set share access for a given stateid */
1107static inline void
1108set_access(u32 access, struct nfs4_ol_stateid *stp)
1109{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001110 unsigned char mask = 1 << access;
1111
1112 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1113 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001114}
1115
1116/* clear share access for a given stateid */
1117static inline void
1118clear_access(u32 access, struct nfs4_ol_stateid *stp)
1119{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001120 unsigned char mask = 1 << access;
1121
1122 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1123 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001124}
1125
1126/* test whether a given stateid has access */
1127static inline bool
1128test_access(u32 access, struct nfs4_ol_stateid *stp)
1129{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001130 unsigned char mask = 1 << access;
1131
1132 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001133}
1134
Jeff Laytonce0fc432012-05-11 09:45:14 -04001135/* set share deny for a given stateid */
1136static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001137set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001138{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001139 unsigned char mask = 1 << deny;
1140
1141 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1142 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001143}
1144
1145/* clear share deny for a given stateid */
1146static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001147clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001148{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001149 unsigned char mask = 1 << deny;
1150
1151 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1152 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001153}
1154
1155/* test whether a given stateid is denying specific access */
1156static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -04001157test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001158{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001159 unsigned char mask = 1 << deny;
1160
1161 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001162}
1163
1164static int nfs4_access_to_omode(u32 access)
1165{
J. Bruce Fields8f34a432010-09-02 15:23:16 -04001166 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001167 case NFS4_SHARE_ACCESS_READ:
1168 return O_RDONLY;
1169 case NFS4_SHARE_ACCESS_WRITE:
1170 return O_WRONLY;
1171 case NFS4_SHARE_ACCESS_BOTH:
1172 return O_RDWR;
1173 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001174 WARN_ON_ONCE(1);
1175 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001176}
1177
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001178/*
1179 * A stateid that had a deny mode associated with it is being released
1180 * or downgraded. Recalculate the deny mode on the file.
1181 */
1182static void
1183recalculate_deny_mode(struct nfs4_file *fp)
1184{
1185 struct nfs4_ol_stateid *stp;
1186
1187 spin_lock(&fp->fi_lock);
1188 fp->fi_share_deny = 0;
1189 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1190 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1191 spin_unlock(&fp->fi_lock);
1192}
1193
1194static void
1195reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1196{
1197 int i;
1198 bool change = false;
1199
1200 for (i = 1; i < 4; i++) {
1201 if ((i & deny) != i) {
1202 change = true;
1203 clear_deny(i, stp);
1204 }
1205 }
1206
1207 /* Recalculate per-file deny mode if there was a change */
1208 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -04001209 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001210}
1211
Jeff Layton82c5ff12012-05-11 09:45:13 -04001212/* release all access and file references for a given stateid */
1213static void
1214release_all_access(struct nfs4_ol_stateid *stp)
1215{
1216 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -04001217 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001218
1219 if (fp && stp->st_deny_bmap != 0)
1220 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001221
1222 for (i = 1; i < 4; i++) {
1223 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -04001224 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001225 clear_access(i, stp);
1226 }
1227}
1228
Kinglong Meed50ffde2015-07-16 12:05:07 +08001229static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1230{
1231 kfree(sop->so_owner.data);
1232 sop->so_ops->so_free(sop);
1233}
1234
Jeff Layton6b180f02014-07-29 21:34:26 -04001235static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1236{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001237 struct nfs4_client *clp = sop->so_client;
1238
1239 might_lock(&clp->cl_lock);
1240
1241 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001242 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001243 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001244 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001245 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001246}
1247
Jeff Laytone8568732015-08-24 12:41:47 -04001248static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001249{
Trond Myklebust11b91642014-07-29 21:34:08 -04001250 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001251
Jeff Layton1c755dc2014-07-29 21:34:12 -04001252 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1253
Jeff Laytone8568732015-08-24 12:41:47 -04001254 if (list_empty(&stp->st_perfile))
1255 return false;
1256
Trond Myklebust1d31a252014-07-10 14:07:25 -04001257 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001258 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001259 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001260 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001261 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001262}
1263
Trond Myklebust60116952014-07-29 21:34:06 -04001264static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001265{
Trond Myklebust60116952014-07-29 21:34:06 -04001266 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001267
Sachin Bhamare8287f002015-04-27 14:50:14 +02001268 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001269 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001270 if (stp->st_stateowner)
1271 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -04001272 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001273}
1274
Jeff Laytonb49e0842014-07-29 21:34:11 -04001275static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001276{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001277 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1278 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001279 struct file *file;
1280
Jeff Laytonb49e0842014-07-29 21:34:11 -04001281 file = find_any_file(stp->st_stid.sc_file);
1282 if (file)
1283 filp_close(file, (fl_owner_t)lo);
1284 nfs4_free_ol_stateid(stid);
1285}
1286
Jeff Layton2c41beb2014-07-29 21:34:41 -04001287/*
1288 * Put the persistent reference to an already unhashed generic stateid, while
1289 * holding the cl_lock. If it's the last reference, then put it onto the
1290 * reaplist for later destruction.
1291 */
1292static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1293 struct list_head *reaplist)
1294{
1295 struct nfs4_stid *s = &stp->st_stid;
1296 struct nfs4_client *clp = s->sc_client;
1297
1298 lockdep_assert_held(&clp->cl_lock);
1299
1300 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1301
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001302 if (!refcount_dec_and_test(&s->sc_count)) {
Jeff Layton2c41beb2014-07-29 21:34:41 -04001303 wake_up_all(&close_wq);
1304 return;
1305 }
1306
1307 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1308 list_add(&stp->st_locks, reaplist);
1309}
1310
Jeff Laytone8568732015-08-24 12:41:47 -04001311static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001312{
Chuck Leverf46c4452016-10-29 18:19:03 -04001313 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001314
1315 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001316 nfs4_unhash_stid(&stp->st_stid);
Jeff Laytone8568732015-08-24 12:41:47 -04001317 return unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001318}
1319
Jeff Layton5adfd882014-07-29 21:34:31 -04001320static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001321{
Chuck Leverf46c4452016-10-29 18:19:03 -04001322 struct nfs4_client *clp = stp->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04001323 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001324
Chuck Leverf46c4452016-10-29 18:19:03 -04001325 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001326 unhashed = unhash_lock_stateid(stp);
Chuck Leverf46c4452016-10-29 18:19:03 -04001327 spin_unlock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001328 if (unhashed)
1329 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001330}
1331
Trond Myklebustc58c6612014-07-29 21:34:35 -04001332static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001333{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001334 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001335
Trond Myklebustd4f04892014-07-29 21:34:36 -04001336 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001337
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001338 list_del_init(&lo->lo_owner.so_strhash);
1339}
1340
Jeff Layton2c41beb2014-07-29 21:34:41 -04001341/*
1342 * Free a list of generic stateids that were collected earlier after being
1343 * fully unhashed.
1344 */
1345static void
1346free_ol_stateid_reaplist(struct list_head *reaplist)
1347{
1348 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001349 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001350
1351 might_sleep();
1352
1353 while (!list_empty(reaplist)) {
1354 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1355 st_locks);
1356 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001357 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001358 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001359 if (fp)
1360 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001361 }
1362}
1363
Jeff Laytond83017f2014-07-29 21:34:42 -04001364static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1365 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001366{
1367 struct nfs4_ol_stateid *stp;
1368
Jeff Laytone8568732015-08-24 12:41:47 -04001369 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1370
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001371 while (!list_empty(&open_stp->st_locks)) {
1372 stp = list_entry(open_stp->st_locks.next,
1373 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001374 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001375 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001376 }
1377}
1378
Jeff Laytone8568732015-08-24 12:41:47 -04001379static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001380 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001381{
Jeff Laytone8568732015-08-24 12:41:47 -04001382 bool unhashed;
1383
Jeff Layton2c41beb2014-07-29 21:34:41 -04001384 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1385
Jeff Laytone8568732015-08-24 12:41:47 -04001386 unhashed = unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001387 release_open_stateid_locks(stp, reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04001388 return unhashed;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001389}
1390
1391static void release_open_stateid(struct nfs4_ol_stateid *stp)
1392{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001393 LIST_HEAD(reaplist);
1394
1395 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001396 if (unhash_open_stateid(stp, &reaplist))
1397 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001398 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1399 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001400}
1401
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001402static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001403{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001404 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001405
Trond Myklebustd4f04892014-07-29 21:34:36 -04001406 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001407
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001408 list_del_init(&oo->oo_owner.so_strhash);
1409 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001410}
1411
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001412static void release_last_closed_stateid(struct nfs4_openowner *oo)
1413{
Jeff Layton217526e2014-07-30 08:27:11 -04001414 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1415 nfsd_net_id);
1416 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001417
Jeff Layton217526e2014-07-30 08:27:11 -04001418 spin_lock(&nn->client_lock);
1419 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001420 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001421 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001422 oo->oo_last_closed_stid = NULL;
1423 }
Jeff Layton217526e2014-07-30 08:27:11 -04001424 spin_unlock(&nn->client_lock);
1425 if (s)
1426 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001427}
1428
Jeff Layton2c41beb2014-07-29 21:34:41 -04001429static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001430{
1431 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001432 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001433 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001434
Jeff Layton2c41beb2014-07-29 21:34:41 -04001435 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001436
Trond Myklebustd4f04892014-07-29 21:34:36 -04001437 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001438 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001439 while (!list_empty(&oo->oo_owner.so_stateids)) {
1440 stp = list_first_entry(&oo->oo_owner.so_stateids,
1441 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001442 if (unhash_open_stateid(stp, &reaplist))
1443 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001444 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001445 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001446 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001447 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001448 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001449}
1450
Marc Eshel5282fd72009-04-03 08:27:52 +03001451static inline int
1452hash_sessionid(struct nfs4_sessionid *sessionid)
1453{
1454 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1455
1456 return sid->sequence % SESSION_HASH_SIZE;
1457}
1458
Mark Salter135dd002015-04-06 09:46:00 -04001459#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001460static inline void
1461dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1462{
1463 u32 *ptr = (u32 *)(&sessionid->data[0]);
1464 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1465}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001466#else
1467static inline void
1468dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1469{
1470}
1471#endif
1472
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001473/*
1474 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1475 * won't be used for replay.
1476 */
1477void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1478{
1479 struct nfs4_stateowner *so = cstate->replay_owner;
1480
1481 if (nfserr == nfserr_replay_me)
1482 return;
1483
1484 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001485 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001486 return;
1487 }
1488 if (!so)
1489 return;
1490 if (so->so_is_open_owner)
1491 release_last_closed_stateid(openowner(so));
1492 so->so_seqid++;
1493 return;
1494}
Marc Eshel5282fd72009-04-03 08:27:52 +03001495
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001496static void
1497gen_sessionid(struct nfsd4_session *ses)
1498{
1499 struct nfs4_client *clp = ses->se_client;
1500 struct nfsd4_sessionid *sid;
1501
1502 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1503 sid->clientid = clp->cl_clientid;
1504 sid->sequence = current_sessionid++;
1505 sid->reserved = 0;
1506}
1507
1508/*
Andy Adamsona649637c72009-08-28 08:45:01 -04001509 * The protocol defines ca_maxresponssize_cached to include the size of
1510 * the rpc header, but all we need to cache is the data starting after
1511 * the end of the initial SEQUENCE operation--the rest we regenerate
1512 * each time. Therefore we can advertise a ca_maxresponssize_cached
1513 * value that is the number of bytes in our cache plus a few additional
1514 * bytes. In order to stay on the safe side, and not promise more than
1515 * we can cache, those additional bytes must be the minimum possible: 24
1516 * bytes of rpc header (xid through accept state, with AUTH_NULL
1517 * verifier), 12 for the compound header (with zero-length tag), and 44
1518 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001519 */
Andy Adamsona649637c72009-08-28 08:45:01 -04001520#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1521
Andy Adamson557ce262009-08-28 08:45:04 -04001522static void
1523free_session_slots(struct nfsd4_session *ses)
1524{
1525 int i;
1526
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001527 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1528 free_svc_cred(&ses->se_slots[i]->sl_cred);
Andy Adamson557ce262009-08-28 08:45:04 -04001529 kfree(ses->se_slots[i]);
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001530 }
Andy Adamson557ce262009-08-28 08:45:04 -04001531}
1532
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001533/*
1534 * We don't actually need to cache the rpc and session headers, so we
1535 * can allocate a little less for each slot:
1536 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001537static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001538{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001539 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001540
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001541 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1542 size = 0;
1543 else
1544 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1545 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001546}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001547
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001548/*
1549 * XXX: If we run out of reserved DRC memory we could (up to a point)
1550 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001551 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001552 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001553static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001554{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001555 u32 slotsize = slot_bytes(ca);
1556 u32 num = ca->maxreqs;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001557 unsigned long avail, total_avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001558
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001559 spin_lock(&nfsd_drc_lock);
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001560 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1561 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001562 /*
1563 * Never use more than a third of the remaining memory,
1564 * unless it's the only way to give this client a slot:
1565 */
Paul Menzel3b2d4dc2019-07-03 13:28:15 +02001566 avail = clamp_t(unsigned long, avail, slotsize, total_avail/3);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001567 num = min_t(int, num, avail / slotsize);
1568 nfsd_drc_mem_used += num * slotsize;
1569 spin_unlock(&nfsd_drc_lock);
1570
1571 return num;
1572}
1573
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001574static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001575{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001576 int slotsize = slot_bytes(ca);
1577
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001578 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001579 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001580 spin_unlock(&nfsd_drc_lock);
1581}
1582
Kinglong Mee60810e52014-01-01 00:35:47 +08001583static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1584 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001585{
Kinglong Mee60810e52014-01-01 00:35:47 +08001586 int numslots = fattrs->maxreqs;
1587 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001588 struct nfsd4_session *new;
1589 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001590
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001591 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001592 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1593 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001594
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001595 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001596 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001597 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001598 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001599 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001600 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001601 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001602 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001603 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001604
1605 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1606 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1607
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001608 return new;
1609out_free:
1610 while (i--)
1611 kfree(new->se_slots[i]);
1612 kfree(new);
1613 return NULL;
1614}
1615
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001616static void free_conn(struct nfsd4_conn *c)
1617{
1618 svc_xprt_put(c->cn_xprt);
1619 kfree(c);
1620}
1621
1622static void nfsd4_conn_lost(struct svc_xpt_user *u)
1623{
1624 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1625 struct nfs4_client *clp = c->cn_session->se_client;
1626
1627 spin_lock(&clp->cl_lock);
1628 if (!list_empty(&c->cn_persession)) {
1629 list_del(&c->cn_persession);
1630 free_conn(c);
1631 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001632 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001633 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001634}
1635
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001636static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001637{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001638 struct nfsd4_conn *conn;
1639
1640 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1641 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001642 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001643 svc_xprt_get(rqstp->rq_xprt);
1644 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001645 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001646 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1647 return conn;
1648}
1649
J. Bruce Fields328ead22010-09-29 16:11:06 -04001650static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1651{
1652 conn->cn_session = ses;
1653 list_add(&conn->cn_persession, &ses->se_conns);
1654}
1655
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001656static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1657{
1658 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001659
1660 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001661 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001662 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001663}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001664
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001665static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001666{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001667 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001668 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001669}
1670
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001671static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001672{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001673 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001674
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001675 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001676 ret = nfsd4_register_conn(conn);
1677 if (ret)
1678 /* oops; xprt is already down: */
1679 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001680 /* We may have gained or lost a callback channel: */
1681 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001682}
1683
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001684static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001685{
1686 u32 dir = NFS4_CDFC4_FORE;
1687
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001688 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001689 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001690 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001691}
1692
1693/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001694static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001695{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001696 struct nfs4_client *clp = s->se_client;
1697 struct nfsd4_conn *c;
1698
1699 spin_lock(&clp->cl_lock);
1700 while (!list_empty(&s->se_conns)) {
1701 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1702 list_del_init(&c->cn_persession);
1703 spin_unlock(&clp->cl_lock);
1704
1705 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1706 free_conn(c);
1707
1708 spin_lock(&clp->cl_lock);
1709 }
1710 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001711}
1712
J. Bruce Fields1377b692012-09-11 21:42:40 -04001713static void __free_session(struct nfsd4_session *ses)
1714{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001715 free_session_slots(ses);
1716 kfree(ses);
1717}
1718
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001719static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001720{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001721 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001722 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001723 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001724}
Andy Adamson557ce262009-08-28 08:45:04 -04001725
Fengguang Wu135ae822012-11-10 07:20:25 -05001726static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001727{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001728 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001729 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001730
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001731 new->se_client = clp;
1732 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001733
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001734 INIT_LIST_HEAD(&new->se_conns);
1735
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001736 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001737 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001738 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001739 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001740 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001741 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001742 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001743 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001744 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001745 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001746
Chuck Leverb0d2e422014-08-22 15:10:59 -04001747 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001748 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001749 /*
1750 * This is a little silly; with sessions there's no real
1751 * use for the callback address. Use the peer address
1752 * as a reasonable default for now, but consider fixing
1753 * the rpc client not to require an address in the
1754 * future:
1755 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001756 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1757 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001758 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001759}
1760
Benny Halevy9089f1b2010-05-12 00:12:26 +03001761/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001762static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001763__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001764{
1765 struct nfsd4_session *elem;
1766 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001767 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001768
Trond Myklebust0a880a22014-07-30 08:27:10 -04001769 lockdep_assert_held(&nn->client_lock);
1770
Marc Eshel5282fd72009-04-03 08:27:52 +03001771 dump_sessionid(__func__, sessionid);
1772 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001773 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001774 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001775 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1776 NFS4_MAX_SESSIONID_LEN)) {
1777 return elem;
1778 }
1779 }
1780
1781 dprintk("%s: session not found\n", __func__);
1782 return NULL;
1783}
1784
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001785static struct nfsd4_session *
1786find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1787 __be32 *ret)
1788{
1789 struct nfsd4_session *session;
1790 __be32 status = nfserr_badsession;
1791
1792 session = __find_in_sessionid_hashtbl(sessionid, net);
1793 if (!session)
1794 goto out;
1795 status = nfsd4_get_session_locked(session);
1796 if (status)
1797 session = NULL;
1798out:
1799 *ret = status;
1800 return session;
1801}
1802
Benny Halevy9089f1b2010-05-12 00:12:26 +03001803/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001804static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001805unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001806{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001807 struct nfs4_client *clp = ses->se_client;
1808 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1809
1810 lockdep_assert_held(&nn->client_lock);
1811
Andy Adamson7116ed62009-04-03 08:27:43 +03001812 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001813 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001814 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001815 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001816}
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1819static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001820STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001822 /*
1823 * We're assuming the clid was not given out from a boot
1824 * precisely 2^32 (about 136 years) before this one. That seems
1825 * a safe assumption:
1826 */
1827 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001829 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001830 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return 1;
1832}
1833
1834/*
1835 * XXX Should we use a slab cache ?
1836 * This type of memory management is somewhat inefficient, but we use it
1837 * anyway since SETCLIENTID is not a common operation.
1838 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001839static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
1841 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001842 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Jeff Layton9258a2d2018-03-16 09:47:22 -04001844 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001845 if (clp == NULL)
1846 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001847 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001848 if (clp->cl_name.data == NULL)
1849 goto err_no_name;
Kees Cook6da2ec52018-06-12 13:55:00 -07001850 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1851 sizeof(struct list_head),
1852 GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001853 if (!clp->cl_ownerstr_hashtbl)
1854 goto err_no_hashtbl;
1855 for (i = 0; i < OWNER_HASH_SIZE; i++)
1856 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001857 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001858 INIT_LIST_HEAD(&clp->cl_sessions);
1859 idr_init(&clp->cl_stateids);
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04001860 atomic_set(&clp->cl_rpc_users, 0);
Trond Myklebust5694c932014-04-18 14:43:56 -04001861 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1862 INIT_LIST_HEAD(&clp->cl_idhash);
1863 INIT_LIST_HEAD(&clp->cl_openowners);
1864 INIT_LIST_HEAD(&clp->cl_delegations);
1865 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04001866 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001867#ifdef CONFIG_NFSD_PNFS
1868 INIT_LIST_HEAD(&clp->cl_lo_states);
1869#endif
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001870 INIT_LIST_HEAD(&clp->async_copies);
1871 spin_lock_init(&clp->async_lock);
Trond Myklebust5694c932014-04-18 14:43:56 -04001872 spin_lock_init(&clp->cl_lock);
1873 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001875err_no_hashtbl:
1876 kfree(clp->cl_name.data);
1877err_no_name:
Jeff Layton9258a2d2018-03-16 09:47:22 -04001878 kmem_cache_free(client_slab, clp);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001879 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001882static void __free_client(struct kref *k)
1883{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04001884 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
1885 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001886
1887 free_svc_cred(&clp->cl_cred);
1888 kfree(clp->cl_ownerstr_hashtbl);
1889 kfree(clp->cl_name.data);
1890 idr_destroy(&clp->cl_stateids);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001891 kmem_cache_free(client_slab, clp);
1892}
1893
1894void drop_client(struct nfs4_client *clp)
1895{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04001896 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001897}
1898
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001899static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900free_client(struct nfs4_client *clp)
1901{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001902 while (!list_empty(&clp->cl_sessions)) {
1903 struct nfsd4_session *ses;
1904 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1905 se_perclnt);
1906 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001907 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1908 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001909 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001910 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04001911 if (clp->cl_nfsd_dentry)
1912 nfsd_client_rmdir(clp->cl_nfsd_dentry);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001913 drop_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914}
1915
Benny Halevy84d38ac2010-05-12 00:13:16 +03001916/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001917static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001918unhash_client_locked(struct nfs4_client *clp)
1919{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001920 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001921 struct nfsd4_session *ses;
1922
Trond Myklebust0a880a22014-07-30 08:27:10 -04001923 lockdep_assert_held(&nn->client_lock);
1924
Trond Myklebust4beb3452014-07-30 08:27:02 -04001925 /* Mark the client as expired! */
1926 clp->cl_time = 0;
1927 /* Make it invisible */
1928 if (!list_empty(&clp->cl_idhash)) {
1929 list_del_init(&clp->cl_idhash);
1930 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1931 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1932 else
1933 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1934 }
1935 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001936 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001937 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1938 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001939 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001940}
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001943unhash_client(struct nfs4_client *clp)
1944{
1945 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1946
1947 spin_lock(&nn->client_lock);
1948 unhash_client_locked(clp);
1949 spin_unlock(&nn->client_lock);
1950}
1951
Jeff Layton97403d92014-07-30 08:27:12 -04001952static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1953{
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04001954 if (atomic_read(&clp->cl_rpc_users))
Jeff Layton97403d92014-07-30 08:27:12 -04001955 return nfserr_jukebox;
1956 unhash_client_locked(clp);
1957 return nfs_ok;
1958}
1959
Trond Myklebust4beb3452014-07-30 08:27:02 -04001960static void
1961__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
Jeff Layton68ef3bc2018-03-16 11:32:02 -04001963 int i;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001964 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 struct list_head reaplist;
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04001969 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001970 while (!list_empty(&clp->cl_delegations)) {
1971 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001972 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04001973 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 }
Benny Halevycdc975052014-05-30 09:09:30 -04001975 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 while (!list_empty(&reaplist)) {
1977 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001978 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001979 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001981 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05001982 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001983 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001984 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001985 }
NeilBrownea1da632005-06-23 22:04:17 -07001986 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001987 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04001988 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001989 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 }
Jeff Layton68ef3bc2018-03-16 11:32:02 -04001991 for (i = 0; i < OWNER_HASH_SIZE; i++) {
1992 struct nfs4_stateowner *so, *tmp;
1993
1994 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
1995 so_strhash) {
1996 /* Should be no openowners at this point */
1997 WARN_ON_ONCE(so->so_is_open_owner);
1998 remove_blocked_locks(lockowner(so));
1999 }
2000 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002001 nfsd4_return_all_client_layouts(clp);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002002 nfsd4_shutdown_copy(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04002003 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002004 if (clp->cl_cb_conn.cb_xprt)
2005 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002006 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008
Trond Myklebust4beb3452014-07-30 08:27:02 -04002009static void
2010destroy_client(struct nfs4_client *clp)
2011{
2012 unhash_client(clp);
2013 __destroy_client(clp);
2014}
2015
Scott Mayhew362063a2019-03-26 18:06:28 -04002016static void inc_reclaim_complete(struct nfs4_client *clp)
2017{
2018 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2019
2020 if (!nn->track_reclaim_completes)
2021 return;
2022 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2023 return;
2024 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2025 nn->reclaim_str_hashtbl_size) {
2026 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2027 clp->net->ns.inum);
2028 nfsd4_end_grace(nn);
2029 }
2030}
2031
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002032static void expire_client(struct nfs4_client *clp)
2033{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002034 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002035 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04002036 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002037}
2038
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002039static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2040{
2041 memcpy(target->cl_verifier.data, source->data,
2042 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043}
2044
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002045static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2046{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2048 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2049}
2050
J. Bruce Fields50043852015-11-20 15:58:37 -05002051static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2052{
NeilBrown2f10fdc2017-03-23 16:57:36 +08002053 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2054 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2055 GFP_KERNEL);
Chuck Lever9abdda52018-08-16 12:05:59 -04002056 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2057 if ((source->cr_principal && !target->cr_principal) ||
2058 (source->cr_raw_principal && !target->cr_raw_principal) ||
2059 (source->cr_targ_princ && !target->cr_targ_princ))
NeilBrown2f10fdc2017-03-23 16:57:36 +08002060 return -ENOMEM;
J. Bruce Fields50043852015-11-20 15:58:37 -05002061
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04002062 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 target->cr_uid = source->cr_uid;
2064 target->cr_gid = source->cr_gid;
2065 target->cr_group_info = source->cr_group_info;
2066 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04002067 target->cr_gss_mech = source->cr_gss_mech;
2068 if (source->cr_gss_mech)
2069 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002070 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071}
2072
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002073static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002074compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2075{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002076 if (o1->len < o2->len)
2077 return -1;
2078 if (o1->len > o2->len)
2079 return 1;
2080 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002081}
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002084same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2085{
2086 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087}
2088
2089static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002090same_clid(clientid_t *cl1, clientid_t *cl2)
2091{
2092 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093}
2094
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002095static bool groups_equal(struct group_info *g1, struct group_info *g2)
2096{
2097 int i;
2098
2099 if (g1->ngroups != g2->ngroups)
2100 return false;
2101 for (i=0; i<g1->ngroups; i++)
Alexey Dobriyan81243ea2016-10-07 17:03:12 -07002102 if (!gid_eq(g1->gid[i], g2->gid[i]))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002103 return false;
2104 return true;
2105}
2106
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002107/*
2108 * RFC 3530 language requires clid_inuse be returned when the
2109 * "principal" associated with a requests differs from that previously
2110 * used. We use uid, gid's, and gss principal string as our best
2111 * approximation. We also don't want to allow non-gss use of a client
2112 * established using gss: in theory cr_principal should catch that
2113 * change, but in practice cr_principal can be null even in the gss case
2114 * since gssd doesn't always pass down a principal string.
2115 */
2116static bool is_gss_cred(struct svc_cred *cr)
2117{
2118 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2119 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2120}
2121
2122
Vivek Trivedi5559b502012-07-24 21:18:20 +05302123static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002124same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2125{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002126 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002127 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2128 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002129 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2130 return false;
Chuck Lever9abdda52018-08-16 12:05:59 -04002131 /* XXX: check that cr_targ_princ fields match ? */
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002132 if (cr1->cr_principal == cr2->cr_principal)
2133 return true;
2134 if (!cr1->cr_principal || !cr2->cr_principal)
2135 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05302136 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137}
2138
J. Bruce Fields57266a62013-04-13 14:27:29 -04002139static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2140{
2141 struct svc_cred *cr = &rqstp->rq_cred;
2142 u32 service;
2143
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04002144 if (!cr->cr_gss_mech)
2145 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002146 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2147 return service == RPC_GSS_SVC_INTEGRITY ||
2148 service == RPC_GSS_SVC_PRIVACY;
2149}
2150
Andrew Elblededeb132016-06-15 12:52:08 -04002151bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
J. Bruce Fields57266a62013-04-13 14:27:29 -04002152{
2153 struct svc_cred *cr = &rqstp->rq_cred;
2154
2155 if (!cl->cl_mach_cred)
2156 return true;
2157 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2158 return false;
2159 if (!svc_rqst_integrity_protected(rqstp))
2160 return false;
J. Bruce Fields414ca012015-11-20 10:48:02 -05002161 if (cl->cl_cred.cr_raw_principal)
2162 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2163 cr->cr_raw_principal);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002164 if (!cr->cr_principal)
2165 return false;
2166 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2167}
2168
Jeff Layton294ac322014-07-30 08:27:15 -04002169static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05002170{
Chuck Leverab4684d2012-03-02 17:13:50 -05002171 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Jeff Laytonf4199922014-06-17 07:44:11 -04002173 /*
2174 * This is opaque to client, so no need to byte-swap. Use
2175 * __force to keep sparse happy
2176 */
2177 verf[0] = (__force __be32)get_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08002178 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05002179 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
Jeff Layton294ac322014-07-30 08:27:15 -04002182static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2183{
2184 clp->cl_clientid.cl_boot = nn->boot_time;
2185 clp->cl_clientid.cl_id = nn->clientid_counter++;
2186 gen_confirm(clp, nn);
2187}
2188
Jeff Layton4770d722014-07-29 21:34:10 -04002189static struct nfs4_stid *
2190find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04002191{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002192 struct nfs4_stid *ret;
2193
2194 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2195 if (!ret || !ret->sc_type)
2196 return NULL;
2197 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04002198}
2199
Jeff Layton4770d722014-07-29 21:34:10 -04002200static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04002201find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002202{
2203 struct nfs4_stid *s;
2204
Jeff Layton4770d722014-07-29 21:34:10 -04002205 spin_lock(&cl->cl_lock);
2206 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002207 if (s != NULL) {
2208 if (typemask & s->sc_type)
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03002209 refcount_inc(&s->sc_count);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002210 else
2211 s = NULL;
2212 }
Jeff Layton4770d722014-07-29 21:34:10 -04002213 spin_unlock(&cl->cl_lock);
2214 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04002215}
2216
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002217static int client_info_show(struct seq_file *m, void *v)
2218{
2219 struct inode *inode = m->private;
2220 struct nfsdfs_client *nc;
2221 struct nfs4_client *clp;
2222 u64 clid;
2223
2224 nc = get_nfsdfs_client(inode);
2225 if (!nc)
2226 return -ENXIO;
2227 clp = container_of(nc, struct nfs4_client, cl_nfsdfs);
2228 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2229 seq_printf(m, "clientid: 0x%llx\n", clid);
2230 drop_client(clp);
2231
2232 return 0;
2233}
2234
2235static int client_info_open(struct inode *inode, struct file *file)
2236{
2237 return single_open(file, client_info_show, inode);
2238}
2239
2240static const struct file_operations client_info_fops = {
2241 .open = client_info_open,
2242 .read = seq_read,
2243 .llseek = seq_lseek,
2244 .release = single_release,
2245};
2246
2247static const struct tree_descr client_files[] = {
2248 [0] = {"info", &client_info_fops, S_IRUSR},
2249 [1] = {""},
2250};
2251
Jeff Layton2216d442012-11-12 15:00:57 -05002252static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002253 struct svc_rqst *rqstp, nfs4_verifier *verf)
2254{
2255 struct nfs4_client *clp;
2256 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002257 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002258 struct net *net = SVC_NET(rqstp);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002259 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002260
2261 clp = alloc_client(name);
2262 if (clp == NULL)
2263 return NULL;
2264
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002265 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2266 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002267 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002268 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002269 }
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002270 gen_clid(clp, nn);
2271 kref_init(&clp->cl_nfsdfs.cl_ref);
Christoph Hellwig0162ac22014-09-24 12:19:19 +02002272 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Benny Halevy07cd4902010-05-12 00:13:41 +03002273 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002274 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002275 copy_verf(clp, verf);
2276 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04002277 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002278 clp->net = net;
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002279 clp->cl_nfsd_dentry = nfsd_client_mkdir(nn, &clp->cl_nfsdfs,
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002280 clp->cl_clientid.cl_id - nn->clientid_base,
2281 client_files);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002282 if (!clp->cl_nfsd_dentry) {
2283 free_client(clp);
2284 return NULL;
2285 }
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002286 return clp;
2287}
2288
NeilBrownfd39ca92005-06-23 22:04:03 -07002289static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002290add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2291{
2292 struct rb_node **new = &(root->rb_node), *parent = NULL;
2293 struct nfs4_client *clp;
2294
2295 while (*new) {
2296 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2297 parent = *new;
2298
2299 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2300 new = &((*new)->rb_left);
2301 else
2302 new = &((*new)->rb_right);
2303 }
2304
2305 rb_link_node(&new_clp->cl_namenode, parent, new);
2306 rb_insert_color(&new_clp->cl_namenode, root);
2307}
2308
2309static struct nfs4_client *
2310find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2311{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002312 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002313 struct rb_node *node = root->rb_node;
2314 struct nfs4_client *clp;
2315
2316 while (node) {
2317 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2318 cmp = compare_blob(&clp->cl_name, name);
2319 if (cmp > 0)
2320 node = node->rb_left;
2321 else if (cmp < 0)
2322 node = node->rb_right;
2323 else
2324 return clp;
2325 }
2326 return NULL;
2327}
2328
2329static void
2330add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331{
2332 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002333 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Trond Myklebust0a880a22014-07-30 08:27:10 -04002335 lockdep_assert_held(&nn->client_lock);
2336
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002337 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002338 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002340 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002341 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342}
2343
NeilBrownfd39ca92005-06-23 22:04:03 -07002344static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345move_to_confirmed(struct nfs4_client *clp)
2346{
2347 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002348 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Trond Myklebust0a880a22014-07-30 08:27:10 -04002350 lockdep_assert_held(&nn->client_lock);
2351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002353 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002354 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002355 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002356 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002357 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358}
2359
2360static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002361find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
2363 struct nfs4_client *clp;
2364 unsigned int idhashval = clientid_hashval(clid->cl_id);
2365
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002366 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002367 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002368 if ((bool)clp->cl_minorversion != sessions)
2369 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002370 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 }
2374 return NULL;
2375}
2376
2377static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002378find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2379{
2380 struct list_head *tbl = nn->conf_id_hashtbl;
2381
Trond Myklebust0a880a22014-07-30 08:27:10 -04002382 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002383 return find_client_in_id_table(tbl, clid, sessions);
2384}
2385
2386static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002387find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002389 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Trond Myklebust0a880a22014-07-30 08:27:10 -04002391 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002392 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393}
2394
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002395static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002396{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002397 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002398}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002399
NeilBrown28ce6052005-06-23 22:03:56 -07002400static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002401find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002402{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002403 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002404 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002405}
2406
2407static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002408find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002409{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002410 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002411 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002412}
2413
NeilBrownfd39ca92005-06-23 22:04:03 -07002414static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002415gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002417 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002418 struct sockaddr *sa = svc_addr(rqstp);
2419 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002420 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
Jeff Layton7077ecb2009-08-14 12:57:58 -04002422 /* Currently, we only support tcp and tcp6 for the callback channel */
2423 if (se->se_callback_netid_len == 3 &&
2424 !memcmp(se->se_callback_netid_val, "tcp", 3))
2425 expected_family = AF_INET;
2426 else if (se->se_callback_netid_len == 4 &&
2427 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2428 expected_family = AF_INET6;
2429 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 goto out_err;
2431
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002432 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002433 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002434 (struct sockaddr *)&conn->cb_addr,
2435 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002436
J. Bruce Fields07263f12010-05-31 19:09:40 -04002437 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002439
J. Bruce Fields07263f12010-05-31 19:09:40 -04002440 if (conn->cb_addr.ss_family == AF_INET6)
2441 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002442
J. Bruce Fields07263f12010-05-31 19:09:40 -04002443 conn->cb_prog = se->se_callback_prog;
2444 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002445 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 return;
2447out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002448 conn->cb_addr.ss_family = AF_UNSPEC;
2449 conn->cb_addrlen = 0;
Rasmus Villemoes4ab495b2017-02-24 01:15:55 +01002450 dprintk("NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 "will not receive delegations\n",
2452 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 return;
2455}
2456
Andy Adamson074fe892009-04-03 08:28:15 +03002457/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002458 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002459 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002460static void
Andy Adamson074fe892009-04-03 08:28:15 +03002461nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2462{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002463 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002464 struct nfsd4_slot *slot = resp->cstate.slot;
2465 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002466
Andy Adamson557ce262009-08-28 08:45:04 -04002467 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002468
J. Bruce Fields085def32017-10-18 16:17:18 -04002469 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamson557ce262009-08-28 08:45:04 -04002470 slot->sl_opcnt = resp->opcnt;
2471 slot->sl_status = resp->cstate.status;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04002472 free_svc_cred(&slot->sl_cred);
2473 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
Andy Adamsonbf864a32009-04-03 08:28:35 +03002474
J. Bruce Fields085def32017-10-18 16:17:18 -04002475 if (!nfsd4_cache_this(resp)) {
2476 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002477 return;
2478 }
J. Bruce Fields085def32017-10-18 16:17:18 -04002479 slot->sl_flags |= NFSD4_SLOT_CACHED;
2480
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002481 base = resp->cstate.data_offset;
2482 slot->sl_datalen = buf->len - base;
2483 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Dan Carpenterd3f03402015-11-21 13:28:50 +03002484 WARN(1, "%s: sessions DRC could not cache compound\n",
2485 __func__);
Andy Adamson557ce262009-08-28 08:45:04 -04002486 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002487}
2488
2489/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002490 * Encode the replay sequence operation from the slot values.
2491 * If cachethis is FALSE encode the uncached rep error on the next
2492 * operation which sets resp->p and increments resp->opcnt for
2493 * nfs4svc_encode_compoundres.
2494 *
Andy Adamson074fe892009-04-03 08:28:15 +03002495 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002496static __be32
2497nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2498 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002499{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002500 struct nfsd4_op *op;
2501 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002502
Andy Adamsonabfabf82009-07-23 19:02:18 -04002503 /* Encode the replayed sequence operation */
2504 op = &args->ops[resp->opcnt - 1];
2505 nfsd4_encode_operation(resp, op);
2506
J. Bruce Fields085def32017-10-18 16:17:18 -04002507 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2508 return op->status;
2509 if (args->opcnt == 1) {
2510 /*
2511 * The original operation wasn't a solo sequence--we
2512 * always cache those--so this retry must not match the
2513 * original:
2514 */
2515 op->status = nfserr_seq_false_retry;
2516 } else {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002517 op = &args->ops[resp->opcnt++];
2518 op->status = nfserr_retry_uncached_rep;
2519 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002520 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002521 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002522}
2523
2524/*
Andy Adamson557ce262009-08-28 08:45:04 -04002525 * The sequence operation is not cached because we can use the slot and
2526 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002527 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002528static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002529nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2530 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002531{
Andy Adamson557ce262009-08-28 08:45:04 -04002532 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002533 struct xdr_stream *xdr = &resp->xdr;
2534 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002535 __be32 status;
2536
Andy Adamson557ce262009-08-28 08:45:04 -04002537 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002538
Andy Adamsonabfabf82009-07-23 19:02:18 -04002539 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002540 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002541 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002542
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002543 p = xdr_reserve_space(xdr, slot->sl_datalen);
2544 if (!p) {
2545 WARN_ON_ONCE(1);
2546 return nfserr_serverfault;
2547 }
2548 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2549 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002550
Andy Adamson557ce262009-08-28 08:45:04 -04002551 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002552 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002553}
2554
Andy Adamson0733d212009-04-03 08:28:01 +03002555/*
2556 * Set the exchange_id flags returned by the server.
2557 */
2558static void
2559nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2560{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002561#ifdef CONFIG_NFSD_PNFS
2562 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2563#else
Andy Adamson0733d212009-04-03 08:28:01 +03002564 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002565#endif
Andy Adamson0733d212009-04-03 08:28:01 +03002566
2567 /* Referrals are supported, Migration is not. */
2568 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2569
2570 /* set the wire flags to return to client. */
2571 clid->flags = new->cl_exchange_flags;
2572}
2573
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002574static bool client_has_openowners(struct nfs4_client *clp)
2575{
2576 struct nfs4_openowner *oo;
2577
2578 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2579 if (!list_empty(&oo->oo_owner.so_stateids))
2580 return true;
2581 }
2582 return false;
2583}
2584
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002585static bool client_has_state(struct nfs4_client *clp)
2586{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002587 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08002588#ifdef CONFIG_NFSD_PNFS
2589 || !list_empty(&clp->cl_lo_states)
2590#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002591 || !list_empty(&clp->cl_delegations)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002592 || !list_empty(&clp->cl_sessions)
2593 || !list_empty(&clp->async_copies);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002594}
2595
Al Virob37ad282006-10-19 23:28:59 -07002596__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02002597nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2598 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03002599{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002600 struct nfsd4_exchange_id *exid = &u->exchange_id;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002601 struct nfs4_client *conf, *new;
2602 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002603 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002604 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002605 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002606 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002607 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002608 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002609
Jeff Layton363168b2009-08-14 12:57:56 -04002610 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002611 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002612 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002613 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002614 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002615
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002616 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002617 return nfserr_inval;
2618
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002619 new = create_client(exid->clname, rqstp, &verf);
2620 if (new == NULL)
2621 return nfserr_jukebox;
2622
Andy Adamson0733d212009-04-03 08:28:01 +03002623 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002624 case SP4_MACH_CRED:
Andrew Elbleed941642016-06-15 12:52:09 -04002625 exid->spo_must_enforce[0] = 0;
2626 exid->spo_must_enforce[1] = (
2627 1 << (OP_BIND_CONN_TO_SESSION - 32) |
2628 1 << (OP_EXCHANGE_ID - 32) |
2629 1 << (OP_CREATE_SESSION - 32) |
2630 1 << (OP_DESTROY_SESSION - 32) |
2631 1 << (OP_DESTROY_CLIENTID - 32));
2632
2633 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
2634 1 << (OP_OPEN_DOWNGRADE) |
2635 1 << (OP_LOCKU) |
2636 1 << (OP_DELEGRETURN));
2637
2638 exid->spo_must_allow[1] &= (
2639 1 << (OP_TEST_STATEID - 32) |
2640 1 << (OP_FREE_STATEID - 32));
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002641 if (!svc_rqst_integrity_protected(rqstp)) {
2642 status = nfserr_inval;
2643 goto out_nolock;
2644 }
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002645 /*
2646 * Sometimes userspace doesn't give us a principal.
2647 * Which is a bug, really. Anyway, we can't enforce
2648 * MACH_CRED in that case, better to give up now:
2649 */
J. Bruce Fields414ca012015-11-20 10:48:02 -05002650 if (!new->cl_cred.cr_principal &&
2651 !new->cl_cred.cr_raw_principal) {
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002652 status = nfserr_serverfault;
2653 goto out_nolock;
2654 }
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002655 new->cl_mach_cred = true;
Andy Adamson0733d212009-04-03 08:28:01 +03002656 case SP4_NONE:
2657 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002658 default: /* checked by xdr code */
2659 WARN_ON_ONCE(1);
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -06002660 /* fall through */
Andy Adamson0733d212009-04-03 08:28:01 +03002661 case SP4_SSV:
Kinglong Mee8edf4b02016-02-26 22:36:42 +08002662 status = nfserr_encr_alg_unsupp;
2663 goto out_nolock;
Andy Adamson0733d212009-04-03 08:28:01 +03002664 }
2665
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002666 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002667 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002668 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002669 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002670 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2671 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2672
J. Bruce Fields136e6582012-05-12 20:37:23 -04002673 if (update) {
2674 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002675 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002676 goto out;
2677 }
Andrew Elblededeb132016-06-15 12:52:08 -04002678 if (!nfsd4_mach_creds_match(conf, rqstp)) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002679 status = nfserr_wrong_cred;
2680 goto out;
2681 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002682 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002683 status = nfserr_perm;
2684 goto out;
2685 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002686 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002687 status = nfserr_not_same;
2688 goto out;
2689 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002690 /* case 6 */
2691 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002692 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002693 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002694 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002695 if (client_has_state(conf)) {
2696 status = nfserr_clid_inuse;
2697 goto out;
2698 }
Andy Adamson0733d212009-04-03 08:28:01 +03002699 goto out_new;
2700 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002701 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002702 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002703 goto out_copy;
2704 }
2705 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002706 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002707 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002708 }
2709
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002710 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002711 status = nfserr_noent;
2712 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002713 }
2714
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002715 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002716 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002717 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002718
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002719 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002720out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002721 if (conf) {
2722 status = mark_client_expired_locked(conf);
2723 if (status)
2724 goto out;
2725 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002726 new->cl_minorversion = cstate->minorversion;
Andrew Elbleed941642016-06-15 12:52:09 -04002727 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
2728 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
Andy Adamson0733d212009-04-03 08:28:01 +03002729
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002730 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002731 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002732out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002733 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2734 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002735
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002736 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2737 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002738
2739 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002740 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002741 status = nfs_ok;
2742
2743out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002744 spin_unlock(&nn->client_lock);
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002745out_nolock:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002746 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002747 expire_client(new);
2748 if (unconf)
2749 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002750 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002751}
2752
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002753static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002754check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002755{
Andy Adamson88e588d2009-07-23 19:02:15 -04002756 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2757 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002758
2759 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002760 if (slot_inuse) {
2761 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002762 return nfserr_jukebox;
2763 else
2764 return nfserr_seq_misordered;
2765 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002766 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002767 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002768 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002769 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002770 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002771 return nfserr_seq_misordered;
2772}
2773
Andy Adamson49557cc2009-07-23 19:02:16 -04002774/*
2775 * Cache the create session result into the create session single DRC
2776 * slot cache by saving the xdr structure. sl_seqid has been set.
2777 * Do this for solo or embedded create session operations.
2778 */
2779static void
2780nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002781 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002782{
2783 slot->sl_status = nfserr;
2784 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2785}
2786
2787static __be32
2788nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2789 struct nfsd4_clid_slot *slot)
2790{
2791 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2792 return slot->sl_status;
2793}
2794
Mi Jinlong1b74c252011-07-14 14:50:17 +08002795#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2796 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2797 1 + /* MIN tag is length with zero, only length */ \
2798 3 + /* version, opcount, opcode */ \
2799 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2800 /* seqid, slotID, slotID, cache */ \
2801 4 ) * sizeof(__be32))
2802
2803#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2804 2 + /* verifier: AUTH_NULL, length 0 */\
2805 1 + /* status */ \
2806 1 + /* MIN tag is length with zero, only length */ \
2807 3 + /* opcount, opcode, opstatus*/ \
2808 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2809 /* seqid, slotID, slotID, slotID, status */ \
2810 5 ) * sizeof(__be32))
2811
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002812static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002813{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002814 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2815
J. Bruce Fields373cd402013-04-08 15:42:12 -04002816 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2817 return nfserr_toosmall;
2818 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2819 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002820 ca->headerpadsz = 0;
2821 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2822 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2823 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2824 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2825 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2826 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2827 /*
2828 * Note decreasing slot size below client's request may make it
2829 * difficult for client to function correctly, whereas
2830 * decreasing the number of slots will (just?) affect
2831 * performance. When short on memory we therefore prefer to
2832 * decrease number of slots instead of their size. Clients that
2833 * request larger slots than they need will get poor results:
2834 */
2835 ca->maxreqs = nfsd4_get_drc_mem(ca);
2836 if (!ca->maxreqs)
2837 return nfserr_jukebox;
2838
J. Bruce Fields373cd402013-04-08 15:42:12 -04002839 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002840}
2841
Chuck Lever45006322016-03-01 13:06:02 -05002842/*
2843 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
2844 * These are based on similar macros in linux/sunrpc/msg_prot.h .
2845 */
2846#define RPC_MAX_HEADER_WITH_AUTH_SYS \
2847 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
2848
2849#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
2850 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
2851
Kinglong Mee8a891632013-12-23 18:11:02 +08002852#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05002853 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08002854#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05002855 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
2856 sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08002857
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002858static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2859{
2860 ca->headerpadsz = 0;
2861
Kinglong Mee8a891632013-12-23 18:11:02 +08002862 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002863 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002864 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002865 return nfserr_toosmall;
2866 ca->maxresp_cached = 0;
2867 if (ca->maxops < 2)
2868 return nfserr_toosmall;
2869
2870 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002871}
2872
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002873static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2874{
2875 switch (cbs->flavor) {
2876 case RPC_AUTH_NULL:
2877 case RPC_AUTH_UNIX:
2878 return nfs_ok;
2879 default:
2880 /*
2881 * GSS case: the spec doesn't allow us to return this
2882 * error. But it also doesn't allow us not to support
2883 * GSS.
2884 * I'd rather this fail hard than return some error the
2885 * client might think it can already handle:
2886 */
2887 return nfserr_encr_alg_unsupp;
2888 }
2889}
2890
Andy Adamson069b6ad2009-04-03 08:27:58 +03002891__be32
2892nfsd4_create_session(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02002893 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03002894{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002895 struct nfsd4_create_session *cr_ses = &u->create_session;
Jeff Layton363168b2009-08-14 12:57:56 -04002896 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002897 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002898 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04002899 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002900 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002901 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002902 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002903 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002904
Mi Jinlonga62573d2011-03-23 17:57:07 +08002905 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2906 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002907 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2908 if (status)
2909 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002910 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002911 if (status)
2912 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002913 status = check_backchannel_attrs(&cr_ses->back_channel);
2914 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002915 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002916 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002917 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002918 if (!new)
2919 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002920 conn = alloc_conn_from_crses(rqstp, cr_ses);
2921 if (!conn)
2922 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002923
Jeff Laytond20c11d2014-07-30 08:27:07 -04002924 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002925 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002926 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002927 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002928
2929 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002930 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002931 if (!nfsd4_mach_creds_match(conf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002932 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002933 cs_slot = &conf->cl_cs_slot;
2934 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08002935 if (status) {
2936 if (status == nfserr_replay_cache)
2937 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002938 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002939 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002940 } else if (unconf) {
2941 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002942 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002943 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002944 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002945 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002946 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002947 if (!nfsd4_mach_creds_match(unconf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002948 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002949 cs_slot = &unconf->cl_cs_slot;
2950 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002951 if (status) {
2952 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002953 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002954 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002955 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002956 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002957 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002958 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002959 if (status) {
2960 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002961 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002962 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002963 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002964 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002965 conf = unconf;
2966 } else {
2967 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002968 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002969 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002970 status = nfs_ok;
Chuck Lever4ce85c82016-03-01 13:05:27 -05002971 /* Persistent sessions are not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002972 cr_ses->flags &= ~SESSION4_PERSIST;
Chuck Lever4ce85c82016-03-01 13:05:27 -05002973 /* Upshifting from TCP to RDMA is not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002974 cr_ses->flags &= ~SESSION4_RDMA;
2975
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002976 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002977 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002978
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04002979 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002980 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002981 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002982 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002983
Jeff Laytond20c11d2014-07-30 08:27:07 -04002984 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002985 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002986 spin_unlock(&nn->client_lock);
2987 /* init connection and backchannel */
2988 nfsd4_init_conn(rqstp, conn, new);
2989 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002990 if (old)
2991 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002992 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002993out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002994 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002995 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002996 if (old)
2997 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002998out_free_session:
2999 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003000out_release_drc_mem:
3001 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04003002 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003003}
3004
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003005static __be32 nfsd4_map_bcts_dir(u32 *dir)
3006{
3007 switch (*dir) {
3008 case NFS4_CDFC4_FORE:
3009 case NFS4_CDFC4_BACK:
3010 return nfs_ok;
3011 case NFS4_CDFC4_FORE_OR_BOTH:
3012 case NFS4_CDFC4_BACK_OR_BOTH:
3013 *dir = NFS4_CDFC4_BOTH;
3014 return nfs_ok;
3015 };
3016 return nfserr_inval;
3017}
3018
Christoph Hellwigeb698532017-05-08 20:58:35 +02003019__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3020 struct nfsd4_compound_state *cstate,
3021 union nfsd4_op_u *u)
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003022{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003023 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003024 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003025 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003026 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003027
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003028 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3029 if (status)
3030 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003031 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003032 session->se_cb_prog = bc->bc_cb_program;
3033 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003034 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003035
3036 nfsd4_probe_callback(session->se_client);
3037
3038 return nfs_ok;
3039}
3040
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003041__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3042 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003043 union nfsd4_op_u *u)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003044{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003045 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003046 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003047 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003048 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003049 struct net *net = SVC_NET(rqstp);
3050 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003051
3052 if (!nfsd4_last_compound_op(rqstp))
3053 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003054 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003055 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003056 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003057 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003058 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003059 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003060 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003061 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003062 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003063 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003064 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003065 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003066 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003067 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003068 goto out;
3069 nfsd4_init_conn(rqstp, conn, session);
3070 status = nfs_ok;
3071out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003072 nfsd4_put_session(session);
3073out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003074 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003075}
3076
J. Bruce Fields665d5072018-04-11 16:53:36 -04003077static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003078{
J. Bruce Fields665d5072018-04-11 16:53:36 -04003079 if (!cstate->session)
Fengguang Wu51d87bc2018-03-22 13:37:20 +08003080 return false;
J. Bruce Fields665d5072018-04-11 16:53:36 -04003081 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003082}
3083
Andy Adamson069b6ad2009-04-03 08:27:58 +03003084__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003085nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3086 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003087{
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003088 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003089 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003090 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003091 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003092 struct net *net = SVC_NET(r);
3093 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003094
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003095 status = nfserr_not_only_op;
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003096 if (nfsd4_compound_in_session(cstate, sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04003097 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003098 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003099 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04003100 }
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003101 dump_sessionid(__func__, sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003102 spin_lock(&nn->client_lock);
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003103 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003104 if (!ses)
3105 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003106 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003107 if (!nfsd4_mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003108 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003109 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003110 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003111 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003112 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003113 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003114
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05003115 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04003116
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003117 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003118 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003119out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003120 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003121out_client_lock:
3122 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003123out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03003124 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003125}
3126
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003127static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003128{
3129 struct nfsd4_conn *c;
3130
3131 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003132 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04003133 return c;
3134 }
3135 }
3136 return NULL;
3137}
3138
J. Bruce Fields57266a62013-04-13 14:27:29 -04003139static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003140{
3141 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003142 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003143 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003144 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003145
3146 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003147 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003148 if (c)
3149 goto out_free;
3150 status = nfserr_conn_not_bound_to_session;
3151 if (clp->cl_mach_cred)
3152 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003153 __nfsd4_hash_conn(new, ses);
3154 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003155 ret = nfsd4_register_conn(new);
3156 if (ret)
3157 /* oops; xprt is already down: */
3158 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003159 return nfs_ok;
3160out_free:
3161 spin_unlock(&clp->cl_lock);
3162 free_conn(new);
3163 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003164}
3165
Mi Jinlong868b89c2011-04-27 09:09:58 +08003166static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3167{
3168 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3169
3170 return args->opcnt > session->se_fchannel.maxops;
3171}
3172
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003173static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3174 struct nfsd4_session *session)
3175{
3176 struct xdr_buf *xb = &rqstp->rq_arg;
3177
3178 return xb->len > session->se_fchannel.maxreq_sz;
3179}
3180
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003181static bool replay_matches_cache(struct svc_rqst *rqstp,
3182 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3183{
3184 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3185
3186 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3187 (bool)seq->cachethis)
3188 return false;
3189 /*
3190 * If there's an error than the reply can have fewer ops than
3191 * the call. But if we cached a reply with *more* ops than the
3192 * call you're sending us now, then this new call is clearly not
3193 * really a replay of the old one:
3194 */
3195 if (slot->sl_opcnt < argp->opcnt)
3196 return false;
3197 /* This is the only check explicitly called by spec: */
3198 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3199 return false;
3200 /*
3201 * There may be more comparisons we could actually do, but the
3202 * spec doesn't require us to catch every case where the calls
3203 * don't match (that would require caching the call as well as
3204 * the reply), so we don't bother.
3205 */
3206 return true;
3207}
3208
Andy Adamson069b6ad2009-04-03 08:27:58 +03003209__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003210nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3211 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003212{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003213 struct nfsd4_sequence *seq = &u->sequence;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003214 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003215 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003216 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003217 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003218 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003219 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003220 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003221 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003222 struct net *net = SVC_NET(rqstp);
3223 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003224
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003225 if (resp->opcnt != 1)
3226 return nfserr_sequence_pos;
3227
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003228 /*
3229 * Will be either used or freed by nfsd4_sequence_check_conn
3230 * below.
3231 */
3232 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3233 if (!conn)
3234 return nfserr_jukebox;
3235
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003236 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003237 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003238 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003239 goto out_no_session;
3240 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003241
Mi Jinlong868b89c2011-04-27 09:09:58 +08003242 status = nfserr_too_many_ops;
3243 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003244 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003245
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003246 status = nfserr_req_too_big;
3247 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003248 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003249
Benny Halevyb85d4c02009-04-03 08:28:08 +03003250 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003251 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003252 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003253
Andy Adamson557ce262009-08-28 08:45:04 -04003254 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003255 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3256
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003257 /* We do not negotiate the number of slots yet, so set the
3258 * maxslots to the session maxreqs which is used to encode
3259 * sr_highest_slotid and the sr_target_slot id to maxslots */
3260 seq->maxslots = session->se_fchannel.maxreqs;
3261
J. Bruce Fields73e79482012-02-13 16:39:00 -05003262 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3263 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003264 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003265 status = nfserr_seq_misordered;
3266 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003267 goto out_put_session;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003268 status = nfserr_seq_false_retry;
3269 if (!replay_matches_cache(rqstp, seq, slot))
3270 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003271 cstate->slot = slot;
3272 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003273 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003274 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003275 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003276 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003277 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003278 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003279 }
3280 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003281 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003282
J. Bruce Fields57266a62013-04-13 14:27:29 -04003283 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003284 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003285 if (status)
3286 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003287
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003288 buflen = (seq->cachethis) ?
3289 session->se_fchannel.maxresp_cached :
3290 session->se_fchannel.maxresp_sz;
3291 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3292 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003293 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003294 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003295 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003296
3297 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003298 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003299 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003300 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003301 if (seq->cachethis)
3302 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003303 else
3304 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003305
3306 cstate->slot = slot;
3307 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003308 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003309
Benny Halevyb85d4c02009-04-03 08:28:08 +03003310out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003311 switch (clp->cl_cb_state) {
3312 case NFSD4_CB_DOWN:
3313 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3314 break;
3315 case NFSD4_CB_FAULT:
3316 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3317 break;
3318 default:
3319 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003320 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003321 if (!list_empty(&clp->cl_revoked))
3322 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003323out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08003324 if (conn)
3325 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003326 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003327 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003328out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003329 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003330 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003331}
3332
Trond Myklebustb6076642014-06-30 11:48:35 -04003333void
3334nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3335{
3336 struct nfsd4_compound_state *cs = &resp->cstate;
3337
3338 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04003339 if (cs->status != nfserr_replay_cache) {
3340 nfsd4_store_cache_entry(resp);
3341 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3342 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003343 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04003344 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003345 } else if (cs->clp)
3346 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04003347}
3348
Mi Jinlong345c2842011-10-20 17:51:39 +08003349__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003350nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3351 struct nfsd4_compound_state *cstate,
3352 union nfsd4_op_u *u)
Mi Jinlong345c2842011-10-20 17:51:39 +08003353{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003354 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003355 struct nfs4_client *conf, *unconf;
3356 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003357 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003358 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08003359
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003360 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003361 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003362 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003363 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08003364
3365 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04003366 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08003367 status = nfserr_clientid_busy;
3368 goto out;
3369 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04003370 status = mark_client_expired_locked(conf);
3371 if (status)
3372 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003373 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08003374 } else if (unconf)
3375 clp = unconf;
3376 else {
3377 status = nfserr_stale_clientid;
3378 goto out;
3379 }
Andrew Elblededeb132016-06-15 12:52:08 -04003380 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003381 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003382 status = nfserr_wrong_cred;
3383 goto out;
3384 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003385 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003386out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003387 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003388 if (clp)
3389 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003390 return status;
3391}
3392
Andy Adamson069b6ad2009-04-03 08:27:58 +03003393__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003394nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3395 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003396{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003397 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003398 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003399
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003400 if (rc->rca_one_fs) {
3401 if (!cstate->current_fh.fh_dentry)
3402 return nfserr_nofilehandle;
3403 /*
3404 * We don't take advantage of the rca_one_fs case.
3405 * That's OK, it's optional, we can safely ignore it.
3406 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02003407 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003408 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003409
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003410 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04003411 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3412 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003413 goto out;
3414
3415 status = nfserr_stale_clientid;
3416 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003417 /*
3418 * The following error isn't really legal.
3419 * But we only get here if the client just explicitly
3420 * destroyed the client. Surely it no longer cares what
3421 * error it gets back on an operation for the dead
3422 * client.
3423 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003424 goto out;
3425
3426 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04003427 nfsd4_client_record_create(cstate->session->se_client);
Scott Mayhew362063a2019-03-26 18:06:28 -04003428 inc_reclaim_complete(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003429out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003430 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003431}
3432
3433__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003434nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003435 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003437 struct nfsd4_setclientid *setclid = &u->setclientid;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003438 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003440 struct nfs4_client *conf, *new;
3441 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003442 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003443 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3444
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003445 new = create_client(clname, rqstp, &clverifier);
3446 if (new == NULL)
3447 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003448 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003449 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003450 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04003451 if (conf && client_has_state(conf)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003452 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003454 if (clp_used_exchangeid(conf))
3455 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003456 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04003457 char addr_str[INET6_ADDRSTRLEN];
3458 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3459 sizeof(addr_str));
3460 dprintk("NFSD: setclientid: string in use by client "
3461 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 goto out;
3463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003465 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003466 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003467 unhash_client_locked(unconf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003468 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003469 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003471 gen_confirm(new, nn);
3472 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04003473 ;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003474 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003475 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003476 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3478 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3479 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003480 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 status = nfs_ok;
3482out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003483 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003484 if (new)
3485 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003486 if (unconf)
3487 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 return status;
3489}
3490
3491
Al Virob37ad282006-10-19 23:28:59 -07003492__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003493nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003494 struct nfsd4_compound_state *cstate,
3495 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003497 struct nfsd4_setclientid_confirm *setclientid_confirm =
3498 &u->setclientid_confirm;
NeilBrown21ab45a2005-06-23 22:04:14 -07003499 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003500 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3502 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003503 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003504 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003506 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003508
Jeff Laytond20c11d2014-07-30 08:27:07 -04003509 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003510 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003511 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003512 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003513 * We try hard to give out unique clientid's, so if we get an
3514 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003515 * the client may be buggy; this should never happen.
3516 *
3517 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003518 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003519 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04003520 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3521 goto out;
3522 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3523 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003524 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003525 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003526 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3527 /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 status = nfs_ok;
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003529 } else /* case 4: client hasn't noticed we rebooted yet? */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003530 status = nfserr_stale_clientid;
3531 goto out;
3532 }
3533 status = nfs_ok;
3534 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003535 old = unconf;
3536 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003537 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003538 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003539 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3540 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04003541 status = nfserr_clid_inuse;
3542 if (client_has_state(old)
3543 && !same_creds(&unconf->cl_cred,
3544 &old->cl_cred))
3545 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003546 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003547 if (status) {
3548 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003549 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003550 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003551 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003552 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003553 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003554 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003555 get_client_locked(conf);
3556 spin_unlock(&nn->client_lock);
3557 nfsd4_probe_callback(conf);
3558 spin_lock(&nn->client_lock);
3559 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003561 spin_unlock(&nn->client_lock);
3562 if (old)
3563 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 return status;
3565}
3566
J. Bruce Fields32513b42011-10-13 16:00:16 -04003567static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003569 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3570}
3571
3572/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003573static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3574 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003575{
Trond Myklebust950e0112014-06-30 11:48:31 -04003576 lockdep_assert_held(&state_lock);
3577
Elena Reshetova818a34e2017-10-20 12:53:30 +03003578 refcount_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003579 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003580 INIT_LIST_HEAD(&fp->fi_stateids);
3581 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02003582 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003583 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003584 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003585 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003586 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003587 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3588 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003589#ifdef CONFIG_NFSD_PNFS
3590 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02003591 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003592#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04003593 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594}
3595
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003596void
NeilBrowne60d4392005-06-23 22:03:01 -07003597nfsd4_free_slabs(void)
3598{
Jeff Layton9258a2d2018-03-16 09:47:22 -04003599 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003600 kmem_cache_destroy(openowner_slab);
3601 kmem_cache_destroy(lockowner_slab);
3602 kmem_cache_destroy(file_slab);
3603 kmem_cache_destroy(stateid_slab);
3604 kmem_cache_destroy(deleg_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04003605 kmem_cache_destroy(odstate_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003606}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607
Bryan Schumaker72083392011-11-01 15:24:59 -04003608int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609nfsd4_init_slabs(void)
3610{
Jeff Layton9258a2d2018-03-16 09:47:22 -04003611 client_slab = kmem_cache_create("nfsd4_clients",
3612 sizeof(struct nfs4_client), 0, 0, NULL);
3613 if (client_slab == NULL)
3614 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003615 openowner_slab = kmem_cache_create("nfsd4_openowners",
3616 sizeof(struct nfs4_openowner), 0, 0, NULL);
3617 if (openowner_slab == NULL)
Jeff Layton9258a2d2018-03-16 09:47:22 -04003618 goto out_free_client_slab;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003619 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003620 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003621 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003622 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003623 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003624 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003625 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003626 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003627 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003628 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003629 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003630 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003631 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003632 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003633 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003634 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003635 odstate_slab = kmem_cache_create("nfsd4_odstate",
3636 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3637 if (odstate_slab == NULL)
3638 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003640
Sachin Bhamare8287f002015-04-27 14:50:14 +02003641out_free_deleg_slab:
3642 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003643out_free_stateid_slab:
3644 kmem_cache_destroy(stateid_slab);
3645out_free_file_slab:
3646 kmem_cache_destroy(file_slab);
3647out_free_lockowner_slab:
3648 kmem_cache_destroy(lockowner_slab);
3649out_free_openowner_slab:
3650 kmem_cache_destroy(openowner_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04003651out_free_client_slab:
3652 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003653out:
NeilBrowne60d4392005-06-23 22:03:01 -07003654 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3655 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656}
3657
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003658static void init_nfs4_replay(struct nfs4_replay *rp)
3659{
3660 rp->rp_status = nfserr_serverfault;
3661 rp->rp_buflen = 0;
3662 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003663 mutex_init(&rp->rp_mutex);
3664}
3665
3666static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3667 struct nfs4_stateowner *so)
3668{
3669 if (!nfsd4_has_session(cstate)) {
3670 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003671 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04003672 }
3673}
3674
3675void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3676{
3677 struct nfs4_stateowner *so = cstate->replay_owner;
3678
3679 if (so != NULL) {
3680 cstate->replay_owner = NULL;
3681 mutex_unlock(&so->so_replay.rp_mutex);
3682 nfs4_put_stateowner(so);
3683 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003684}
3685
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003686static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687{
3688 struct nfs4_stateowner *sop;
3689
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003690 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003691 if (!sop)
3692 return NULL;
3693
3694 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3695 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003696 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003697 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003699 sop->so_owner.len = owner->len;
3700
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003701 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003702 sop->so_client = clp;
3703 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003704 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003705 return sop;
3706}
3707
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003708static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003709{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003710 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003711
Trond Myklebustd4f04892014-07-29 21:34:36 -04003712 list_add(&oo->oo_owner.so_strhash,
3713 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003714 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715}
3716
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003717static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3718{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003719 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003720}
3721
Jeff Layton6b180f02014-07-29 21:34:26 -04003722static void nfs4_free_openowner(struct nfs4_stateowner *so)
3723{
3724 struct nfs4_openowner *oo = openowner(so);
3725
3726 kmem_cache_free(openowner_slab, oo);
3727}
3728
3729static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003730 .so_unhash = nfs4_unhash_openowner,
3731 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003732};
3733
Andrew Elble7fc05642015-11-05 20:42:43 -05003734static struct nfs4_ol_stateid *
3735nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3736{
3737 struct nfs4_ol_stateid *local, *ret = NULL;
3738 struct nfs4_openowner *oo = open->op_openowner;
3739
3740 lockdep_assert_held(&fp->fi_lock);
3741
3742 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3743 /* ignore lock owners */
3744 if (local->st_stateowner->so_is_open_owner == 0)
3745 continue;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003746 if (local->st_stateowner != &oo->oo_owner)
3747 continue;
3748 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
Andrew Elble7fc05642015-11-05 20:42:43 -05003749 ret = local;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03003750 refcount_inc(&ret->st_stid.sc_count);
Andrew Elble7fc05642015-11-05 20:42:43 -05003751 break;
3752 }
3753 }
3754 return ret;
3755}
3756
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003757static __be32
3758nfsd4_verify_open_stid(struct nfs4_stid *s)
3759{
3760 __be32 ret = nfs_ok;
3761
3762 switch (s->sc_type) {
3763 default:
3764 break;
Trond Myklebust4f176412018-01-12 17:42:30 -05003765 case 0:
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003766 case NFS4_CLOSED_STID:
3767 case NFS4_CLOSED_DELEG_STID:
3768 ret = nfserr_bad_stateid;
3769 break;
3770 case NFS4_REVOKED_DELEG_STID:
3771 ret = nfserr_deleg_revoked;
3772 }
3773 return ret;
3774}
3775
3776/* Lock the stateid st_mutex, and deal with races with CLOSE */
3777static __be32
3778nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
3779{
3780 __be32 ret;
3781
Andrew Elble4f34bd02017-11-08 17:29:51 -05003782 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003783 ret = nfsd4_verify_open_stid(&stp->st_stid);
3784 if (ret != nfs_ok)
3785 mutex_unlock(&stp->st_mutex);
3786 return ret;
3787}
3788
3789static struct nfs4_ol_stateid *
3790nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3791{
3792 struct nfs4_ol_stateid *stp;
3793 for (;;) {
3794 spin_lock(&fp->fi_lock);
3795 stp = nfsd4_find_existing_open(fp, open);
3796 spin_unlock(&fp->fi_lock);
3797 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
3798 break;
3799 nfs4_put_stid(&stp->st_stid);
3800 }
3801 return stp;
3802}
3803
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003804static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003805alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003806 struct nfsd4_compound_state *cstate)
3807{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003808 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003809 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003811 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3812 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003814 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003815 oo->oo_owner.so_is_open_owner = 1;
3816 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003817 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003818 if (nfsd4_has_session(cstate))
3819 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003820 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003821 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003822 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003823 spin_lock(&clp->cl_lock);
3824 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003825 if (ret == NULL) {
3826 hash_openowner(oo, clp, strhashval);
3827 ret = oo;
3828 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08003829 nfs4_free_stateowner(&oo->oo_owner);
3830
Trond Myklebustd4f04892014-07-29 21:34:36 -04003831 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04003832 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833}
3834
Andrew Elble7fc05642015-11-05 20:42:43 -05003835static struct nfs4_ol_stateid *
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003836init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
Andrew Elble7fc05642015-11-05 20:42:43 -05003837{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838
Andrew Elble7fc05642015-11-05 20:42:43 -05003839 struct nfs4_openowner *oo = open->op_openowner;
3840 struct nfs4_ol_stateid *retstp = NULL;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003841 struct nfs4_ol_stateid *stp;
Andrew Elble7fc05642015-11-05 20:42:43 -05003842
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003843 stp = open->op_stp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003844 /* We are moving these outside of the spinlocks to avoid the warnings */
3845 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05003846 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003847
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003848retry:
Andrew Elble7fc05642015-11-05 20:42:43 -05003849 spin_lock(&oo->oo_owner.so_client->cl_lock);
3850 spin_lock(&fp->fi_lock);
3851
3852 retstp = nfsd4_find_existing_open(fp, open);
3853 if (retstp)
3854 goto out_unlock;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003855
3856 open->op_stp = NULL;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03003857 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003858 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003859 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003860 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07003861 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003862 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 stp->st_access_bmap = 0;
3864 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003865 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003866 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003867 list_add(&stp->st_perfile, &fp->fi_stateids);
Andrew Elble7fc05642015-11-05 20:42:43 -05003868
3869out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04003870 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003871 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003872 if (retstp) {
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003873 /* Handle races with CLOSE */
3874 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
3875 nfs4_put_stid(&retstp->st_stid);
3876 goto retry;
3877 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003878 /* To keep mutex tracking happy */
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003879 mutex_unlock(&stp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003880 stp = retstp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003881 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003882 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883}
3884
Jeff Laytond3134b12014-07-29 21:34:32 -04003885/*
3886 * In the 4.0 case we need to keep the owners around a little while to handle
3887 * CLOSE replay. We still do need to release any file access that is held by
3888 * them before returning however.
3889 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003891move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892{
Jeff Layton217526e2014-07-30 08:27:11 -04003893 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003894 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3895 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3896 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003897
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003898 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899
Jeff Laytonb401be222014-07-29 21:34:33 -04003900 /*
3901 * We know that we hold one reference via nfsd4_close, and another
3902 * "persistent" reference for the client. If the refcount is higher
3903 * than 2, then there are still calls in progress that are using this
3904 * stateid. We can't put the sc_file reference until they are finished.
3905 * Wait for the refcount to drop to 2. Since it has been unhashed,
3906 * there should be no danger of the refcount going back up again at
3907 * this point.
3908 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03003909 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
Jeff Laytonb401be222014-07-29 21:34:33 -04003910
Jeff Laytond3134b12014-07-29 21:34:32 -04003911 release_all_access(s);
3912 if (s->st_stid.sc_file) {
3913 put_nfs4_file(s->st_stid.sc_file);
3914 s->st_stid.sc_file = NULL;
3915 }
Jeff Layton217526e2014-07-30 08:27:11 -04003916
3917 spin_lock(&nn->client_lock);
3918 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003919 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003920 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003921 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003922 spin_unlock(&nn->client_lock);
3923 if (last)
3924 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925}
3926
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927/* search file_hashtbl[] for file */
3928static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04003929find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 struct nfs4_file *fp;
3932
Jeff Layton5b095e92014-10-23 08:01:02 -04003933 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02003934 if (fh_match(&fp->fi_fhandle, fh)) {
Elena Reshetova818a34e2017-10-20 12:53:30 +03003935 if (refcount_inc_not_zero(&fp->fi_ref))
Jeff Layton5b095e92014-10-23 08:01:02 -04003936 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 }
3939 return NULL;
3940}
3941
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02003942struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003943find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003944{
3945 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003946 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003947
Jeff Layton5b095e92014-10-23 08:01:02 -04003948 rcu_read_lock();
3949 fp = find_file_locked(fh, hashval);
3950 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04003951 return fp;
3952}
3953
3954static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003955find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003956{
3957 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003958 unsigned int hashval = file_hashval(fh);
3959
3960 rcu_read_lock();
3961 fp = find_file_locked(fh, hashval);
3962 rcu_read_unlock();
3963 if (fp)
3964 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04003965
3966 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04003967 fp = find_file_locked(fh, hashval);
3968 if (likely(fp == NULL)) {
3969 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04003970 fp = new;
3971 }
3972 spin_unlock(&state_lock);
3973
3974 return fp;
3975}
3976
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003977/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 * Called to check deny when READ with all zero stateid or
3979 * WRITE with all zero or all one stateid
3980 */
Al Virob37ad282006-10-19 23:28:59 -07003981static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3983{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003985 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986
Trond Myklebustca943212014-07-23 16:17:39 -04003987 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003988 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003989 return ret;
3990 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003991 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003992 if (fp->fi_share_deny & deny_type)
3993 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003994 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003995 put_nfs4_file(fp);
3996 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997}
3998
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003999static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004001 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04004002 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4003 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004004
Trond Myklebust11b91642014-07-29 21:34:08 -04004005 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04004006
Jeff Layton02e12152014-07-16 10:31:57 -04004007 /*
4008 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04004009 * already holding inode->i_lock.
4010 *
Jeff Laytondff13992014-07-08 14:02:49 -04004011 * If the dl_time != 0, then we know that it has already been
4012 * queued for a lease break. Don't queue it again.
4013 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04004014 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04004015 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04004016 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04004017 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04004018 }
Jeff Layton02e12152014-07-16 10:31:57 -04004019 spin_unlock(&state_lock);
4020}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004022static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4023 struct rpc_task *task)
4024{
4025 struct nfs4_delegation *dp = cb_to_delegation(cb);
4026
Andrew Elblea4579742015-08-31 12:06:41 -04004027 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
4028 return 1;
4029
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004030 switch (task->tk_status) {
4031 case 0:
4032 return 1;
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004033 case -NFS4ERR_DELAY:
4034 rpc_delay(task, 2 * HZ);
4035 return 0;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004036 case -EBADHANDLE:
4037 case -NFS4ERR_BAD_STATEID:
4038 /*
4039 * Race: client probably got cb_recall before open reply
4040 * granting delegation.
4041 */
4042 if (dp->dl_retries--) {
4043 rpc_delay(task, 2 * HZ);
4044 return 0;
4045 }
4046 /*FALLTHRU*/
4047 default:
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004048 return 1;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004049 }
4050}
4051
4052static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4053{
4054 struct nfs4_delegation *dp = cb_to_delegation(cb);
4055
4056 nfs4_put_stid(&dp->dl_stid);
4057}
4058
Julia Lawallc4cb8972015-11-21 22:57:39 +01004059static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004060 .prepare = nfsd4_cb_recall_prepare,
4061 .done = nfsd4_cb_recall_done,
4062 .release = nfsd4_cb_recall_release,
4063};
4064
Jeff Layton02e12152014-07-16 10:31:57 -04004065static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4066{
4067 /*
4068 * We're assuming the state code never drops its reference
4069 * without first removing the lease. Since we're in this lease
J. Bruce Fields4a269efb2018-04-13 17:34:35 -04004070 * callback (and since the lease code is serialized by the
4071 * i_lock) we know the server hasn't removed the lease yet, and
4072 * we know it's safe to take a reference.
Jeff Layton02e12152014-07-16 10:31:57 -04004073 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004074 refcount_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02004075 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004076}
4077
Jeff Layton1c8c6012013-06-21 08:58:15 -04004078/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004079static bool
4080nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004081{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004082 bool ret = false;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004083 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4084 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004085
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004086 /*
4087 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004088 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004089 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004090 */
4091 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092
Jeff Layton02e12152014-07-16 10:31:57 -04004093 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004094 fp->fi_had_conflict = true;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004095 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04004096 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004097 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098}
4099
Jeff Laytonc45198e2014-09-01 07:12:07 -04004100static int
Jeff Layton7448cc32015-01-16 15:05:57 -05004101nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4102 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103{
4104 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04004105 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 else
4107 return -EAGAIN;
4108}
4109
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004110static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04004111 .lm_break = nfsd_break_deleg_cb,
4112 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113};
4114
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004115static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4116{
4117 if (nfsd4_has_session(cstate))
4118 return nfs_ok;
4119 if (seqid == so->so_seqid - 1)
4120 return nfserr_replay_me;
4121 if (seqid == so->so_seqid)
4122 return nfs_ok;
4123 return nfserr_bad_seqid;
4124}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125
Jeff Layton4b24ca72014-06-30 11:48:44 -04004126static __be32 lookup_clientid(clientid_t *clid,
4127 struct nfsd4_compound_state *cstate,
4128 struct nfsd_net *nn)
4129{
4130 struct nfs4_client *found;
4131
4132 if (cstate->clp) {
4133 found = cstate->clp;
4134 if (!same_clid(&found->cl_clientid, clid))
4135 return nfserr_stale_clientid;
4136 return nfs_ok;
4137 }
4138
4139 if (STALE_CLIENTID(clid, nn))
4140 return nfserr_stale_clientid;
4141
4142 /*
4143 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4144 * cached already then we know this is for is for v4.0 and "sessions"
4145 * will be false.
4146 */
4147 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004148 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004149 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004150 if (!found) {
4151 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004152 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04004153 }
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04004154 atomic_inc(&found->cl_rpc_users);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004155 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004156
4157 /* Cache the nfs4_client in cstate! */
4158 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004159 return nfs_ok;
4160}
4161
Al Virob37ad282006-10-19 23:28:59 -07004162__be32
Andy Adamson66689582009-04-03 08:28:45 +03004163nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004164 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 clientid_t *clientid = &open->op_clientid;
4167 struct nfs4_client *clp = NULL;
4168 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004169 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004170 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004172 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004174 /*
4175 * In case we need it later, after we've already created the
4176 * file and don't want to risk a further failure:
4177 */
4178 open->op_file = nfsd4_alloc_file();
4179 if (open->op_file == NULL)
4180 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181
Trond Myklebust2d91e892014-06-30 11:48:46 -04004182 status = lookup_clientid(clientid, cstate, nn);
4183 if (status)
4184 return status;
4185 clp = cstate->clp;
4186
Trond Myklebustd4f04892014-07-29 21:34:36 -04004187 strhashval = ownerstr_hashval(&open->op_owner);
4188 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004189 open->op_openowner = oo;
4190 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004191 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004193 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004194 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004195 release_openowner(oo);
4196 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004197 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004198 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004199 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4200 if (status)
4201 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004202 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004203new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04004204 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004205 if (oo == NULL)
4206 return nfserr_jukebox;
4207 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004208alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04004209 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004210 if (!open->op_stp)
4211 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004212
4213 if (nfsd4_has_session(cstate) &&
4214 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4215 open->op_odstate = alloc_clnt_odstate(clp);
4216 if (!open->op_odstate)
4217 return nfserr_jukebox;
4218 }
4219
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004220 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221}
4222
Al Virob37ad282006-10-19 23:28:59 -07004223static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07004224nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4225{
4226 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4227 return nfserr_openmode;
4228 else
4229 return nfs_ok;
4230}
4231
Daniel Mackc47d8322011-05-16 16:38:14 +02004232static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04004233{
J. Bruce Fields24a01112010-05-18 20:01:35 -04004234 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4235}
4236
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004237static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004238{
4239 struct nfs4_stid *ret;
4240
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004241 ret = find_stateid_by_type(cl, s,
4242 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004243 if (!ret)
4244 return NULL;
4245 return delegstateid(ret);
4246}
4247
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004248static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4249{
4250 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4251 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4252}
4253
Al Virob37ad282006-10-19 23:28:59 -07004254static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04004255nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07004256 struct nfs4_delegation **dp)
4257{
4258 int flags;
Al Virob37ad282006-10-19 23:28:59 -07004259 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004260 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07004261
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004262 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4263 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07004264 goto out;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004265 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4266 nfs4_put_stid(&deleg->dl_stid);
4267 if (cl->cl_minorversion)
4268 status = nfserr_deleg_revoked;
4269 goto out;
4270 }
J. Bruce Fields24a01112010-05-18 20:01:35 -04004271 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004272 status = nfs4_check_delegmode(deleg, flags);
4273 if (status) {
4274 nfs4_put_stid(&deleg->dl_stid);
4275 goto out;
4276 }
4277 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004278out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004279 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004280 return nfs_ok;
4281 if (status)
4282 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004283 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004284 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07004285}
4286
J. Bruce Fields21fb4012010-07-28 12:21:23 -04004287static inline int nfs4_access_to_access(u32 nfs4_access)
4288{
4289 int flags = 0;
4290
4291 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4292 flags |= NFSD_MAY_READ;
4293 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4294 flags |= NFSD_MAY_WRITE;
4295 return flags;
4296}
4297
Al Virob37ad282006-10-19 23:28:59 -07004298static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4300 struct nfsd4_open *open)
4301{
4302 struct iattr iattr = {
4303 .ia_valid = ATTR_SIZE,
4304 .ia_size = 0,
4305 };
4306 if (!open->op_truncate)
4307 return 0;
4308 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08004309 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
4311}
4312
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004313static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004314 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4315 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004316{
Trond Myklebustde186432014-07-10 14:07:26 -04004317 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004318 __be32 status;
4319 int oflag = nfs4_access_to_omode(open->op_share_access);
4320 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004321 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004322
Trond Myklebustde186432014-07-10 14:07:26 -04004323 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004324
4325 /*
4326 * Are we trying to set a deny mode that would conflict with
4327 * current access?
4328 */
4329 status = nfs4_file_check_deny(fp, open->op_share_deny);
4330 if (status != nfs_ok) {
4331 spin_unlock(&fp->fi_lock);
4332 goto out;
4333 }
4334
4335 /* set access to the file */
4336 status = nfs4_file_get_access(fp, open->op_share_access);
4337 if (status != nfs_ok) {
4338 spin_unlock(&fp->fi_lock);
4339 goto out;
4340 }
4341
4342 /* Set access bits in stateid */
4343 old_access_bmap = stp->st_access_bmap;
4344 set_access(open->op_share_access, stp);
4345
4346 /* Set new deny mask */
4347 old_deny_bmap = stp->st_deny_bmap;
4348 set_deny(open->op_share_deny, stp);
4349 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4350
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004351 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04004352 spin_unlock(&fp->fi_lock);
4353 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004354 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004355 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04004356 spin_lock(&fp->fi_lock);
4357 if (!fp->fi_fds[oflag]) {
4358 fp->fi_fds[oflag] = filp;
4359 filp = NULL;
4360 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004361 }
Trond Myklebustde186432014-07-10 14:07:26 -04004362 spin_unlock(&fp->fi_lock);
4363 if (filp)
4364 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004365
4366 status = nfsd4_truncate(rqstp, cur_fh, open);
4367 if (status)
4368 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004369out:
4370 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004371out_put_access:
4372 stp->st_access_bmap = old_access_bmap;
4373 nfs4_file_put_access(fp, open->op_share_access);
4374 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4375 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004376}
4377
Al Virob37ad282006-10-19 23:28:59 -07004378static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004379nfs4_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 -07004380{
Al Virob37ad282006-10-19 23:28:59 -07004381 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02004382 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004384 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004385 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004386
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004387 /* test and set deny mode */
4388 spin_lock(&fp->fi_lock);
4389 status = nfs4_file_check_deny(fp, open->op_share_deny);
4390 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004391 set_deny(open->op_share_deny, stp);
4392 fp->fi_share_deny |=
4393 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4394 }
4395 spin_unlock(&fp->fi_lock);
4396
4397 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004400 status = nfsd4_truncate(rqstp, cur_fh, open);
4401 if (status != nfs_ok)
4402 reset_union_bmap_deny(old_deny_bmap, stp);
4403 return status;
4404}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004406/* Should we give out recallable state?: */
4407static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4408{
4409 if (clp->cl_cb_state == NFSD4_CB_UP)
4410 return true;
4411 /*
4412 * In the sessions case, since we don't have to establish a
4413 * separate connection for callbacks, we assume it's OK
4414 * until we hear otherwise:
4415 */
4416 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4417}
4418
J. Bruce Fields653e5142018-02-15 22:08:45 -05004419static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4420 int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004421{
4422 struct file_lock *fl;
4423
4424 fl = locks_alloc_lock();
4425 if (!fl)
4426 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004427 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04004428 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004429 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4430 fl->fl_end = OFFSET_MAX;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004431 fl->fl_owner = (fl_owner_t)dp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004432 fl->fl_pid = current->tgid;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004433 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004434 return fl;
4435}
4436
Jeff Layton0b266932014-07-25 07:34:25 -04004437static struct nfs4_delegation *
4438nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004439 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004440{
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004441 int status = 0;
Jeff Layton0b266932014-07-25 07:34:25 -04004442 struct nfs4_delegation *dp;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004443 struct file *filp;
4444 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04004445
J. Bruce Fields353601e2018-02-16 14:29:42 -05004446 /*
4447 * The fi_had_conflict and nfs_get_existing_delegation checks
4448 * here are just optimizations; we'll need to recheck them at
4449 * the end:
4450 */
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004451 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004452 return ERR_PTR(-EAGAIN);
4453
J. Bruce Fields353601e2018-02-16 14:29:42 -05004454 filp = find_readable_file(fp);
4455 if (!filp) {
4456 /* We should always have a readable file here */
4457 WARN_ON_ONCE(1);
4458 return ERR_PTR(-EBADF);
4459 }
Andrew Elble34ed9872015-10-15 12:07:28 -04004460 spin_lock(&state_lock);
4461 spin_lock(&fp->fi_lock);
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004462 if (nfs4_delegation_exists(clp, fp))
4463 status = -EAGAIN;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004464 else if (!fp->fi_deleg_file) {
4465 fp->fi_deleg_file = filp;
4466 /* increment early to prevent fi_deleg_file from being
4467 * cleared */
4468 fp->fi_delegees = 1;
4469 filp = NULL;
4470 } else
4471 fp->fi_delegees++;
4472 spin_unlock(&fp->fi_lock);
4473 spin_unlock(&state_lock);
4474 if (filp)
4475 fput(filp);
4476 if (status)
4477 return ERR_PTR(status);
4478
4479 status = -ENOMEM;
4480 dp = alloc_init_deleg(clp, fp, fh, odstate);
4481 if (!dp)
4482 goto out_delegees;
4483
4484 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
4485 if (!fl)
Andrew Elblebd8d7252018-10-05 09:32:08 -04004486 goto out_clnt_odstate;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004487
4488 status = vfs_setlease(fp->fi_deleg_file, fl->fl_type, &fl, NULL);
4489 if (fl)
4490 locks_free_lock(fl);
4491 if (status)
4492 goto out_clnt_odstate;
4493
4494 spin_lock(&state_lock);
4495 spin_lock(&fp->fi_lock);
4496 if (fp->fi_had_conflict)
4497 status = -EAGAIN;
4498 else
4499 status = hash_delegation_locked(dp, fp);
Andrew Elble34ed9872015-10-15 12:07:28 -04004500 spin_unlock(&fp->fi_lock);
4501 spin_unlock(&state_lock);
4502
4503 if (status)
Andrew Elble692ad282018-04-18 17:04:37 -04004504 goto out_unlock;
4505
Jeff Layton0b266932014-07-25 07:34:25 -04004506 return dp;
Andrew Elble692ad282018-04-18 17:04:37 -04004507out_unlock:
4508 vfs_setlease(fp->fi_deleg_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004509out_clnt_odstate:
4510 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004511 nfs4_put_stid(&dp->dl_stid);
4512out_delegees:
4513 put_deleg_file(fp);
4514 return ERR_PTR(status);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004515}
4516
Benny Halevy4aa89132012-02-21 14:16:44 -08004517static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4518{
4519 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4520 if (status == -EAGAIN)
4521 open->op_why_no_deleg = WND4_CONTENTION;
4522 else {
4523 open->op_why_no_deleg = WND4_RESOURCE;
4524 switch (open->op_deleg_want) {
4525 case NFS4_SHARE_WANT_READ_DELEG:
4526 case NFS4_SHARE_WANT_WRITE_DELEG:
4527 case NFS4_SHARE_WANT_ANY_DELEG:
4528 break;
4529 case NFS4_SHARE_WANT_CANCEL:
4530 open->op_why_no_deleg = WND4_CANCELLED;
4531 break;
4532 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004533 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08004534 }
4535 }
4536}
4537
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538/*
4539 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04004540 *
4541 * Note we don't support write delegations, and won't until the vfs has
4542 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 */
4544static void
Jeff Layton4cf59222014-07-25 07:34:24 -04004545nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4546 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547{
4548 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04004549 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4550 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004551 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004552 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004554 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07004555 open->op_recall = 0;
4556 switch (open->op_claim_type) {
4557 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05004558 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07004559 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004560 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4561 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004562 break;
4563 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00004564 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004565 /*
4566 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004567 * had the chance to reclaim theirs, *and* until
4568 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004569 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004570 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004571 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004572 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004573 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04004574 /*
4575 * Also, if the file was opened for write or
4576 * create, there's a good chance the client's
4577 * about to write to it, resulting in an
4578 * immediate recall (since we don't support
4579 * write delegations):
4580 */
NeilBrown7b190fe2005-06-23 22:03:23 -07004581 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04004582 goto out_no_deleg;
4583 if (open->op_create == NFS4_OPEN_CREATE)
4584 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004585 break;
4586 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004587 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004588 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004589 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004590 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004591 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004593 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004595 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004596 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04004597 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04004598 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004599 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004600out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004601 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4602 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004603 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04004604 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004605 open->op_recall = 1;
4606 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004607
4608 /* 4.1 client asking for a delegation? */
4609 if (open->op_deleg_want)
4610 nfsd4_open_deleg_none_ext(open, status);
4611 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612}
4613
Benny Halevye27f49c2012-02-21 14:16:54 -08004614static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4615 struct nfs4_delegation *dp)
4616{
4617 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4618 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4619 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4620 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4621 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4622 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4623 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4624 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4625 }
4626 /* Otherwise the client must be confused wanting a delegation
4627 * it already has, therefore we don't return
4628 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4629 */
4630}
4631
Al Virob37ad282006-10-19 23:28:59 -07004632__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4634{
Andy Adamson66689582009-04-03 08:28:45 +03004635 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004636 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004638 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004639 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004640 __be32 status;
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004641 bool new_stp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 /*
4644 * Lookup file; if found, lookup stateid and check open request,
4645 * and check for delegations in the process of being recalled.
4646 * If not found, create the nfs4_file struct
4647 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004648 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004649 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004650 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004651 if (status)
4652 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004653 stp = nfsd4_find_and_lock_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004655 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004656 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004657 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004658 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 }
4660
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004661 if (!stp) {
4662 stp = init_open_stateid(fp, open);
4663 if (!open->op_stp)
4664 new_stp = true;
4665 }
4666
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 /*
4668 * OPEN the file, or upgrade an existing OPEN.
4669 * If truncate fails, the OPEN fails.
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004670 *
4671 * stp is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 */
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004673 if (!new_stp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004675 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04004676 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004677 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04004679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 } else {
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004681 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4682 if (status) {
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004683 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004684 release_open_stateid(stp);
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004685 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004686 goto out;
4687 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004688
4689 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4690 open->op_odstate);
4691 if (stp->st_clnt_odstate == open->op_odstate)
4692 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 }
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004694
Jeff Layton9767feb2015-10-01 09:05:50 -04004695 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004696 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697
Benny Halevyd24433c2012-02-16 20:57:17 +02004698 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004699 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4700 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4701 open->op_why_no_deleg = WND4_NOT_WANTED;
4702 goto nodeleg;
4703 }
4704 }
4705
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 /*
4707 * Attempt to hand out a delegation. No error return, because the
4708 * OPEN succeeds even if we fail.
4709 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004710 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004711nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 status = nfs_ok;
4713
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004714 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004715 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004717 /* 4.1 client trying to upgrade/downgrade delegation? */
4718 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004719 open->op_deleg_want)
4720 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004721
NeilBrown13cd2182005-06-23 22:03:10 -07004722 if (fp)
4723 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004724 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08004725 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 /*
4727 * To finish the open response, we just need to set the rflags.
4728 */
4729 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Jeff Layton19e4c342016-09-16 16:28:27 -04004730 if (nfsd4_has_session(&resp->cstate))
4731 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
4732 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Jeff Layton19e4c342016-09-16 16:28:27 -04004734
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004735 if (dp)
4736 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004737 if (stp)
4738 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739
4740 return status;
4741}
4742
Jeff Layton58fb12e2014-07-29 21:34:27 -04004743void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04004744 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004745{
4746 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004747 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004748
Jeff Laytond3134b12014-07-29 21:34:32 -04004749 nfsd4_cstate_assign_replay(cstate, so);
4750 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004751 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004752 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04004753 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004754 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004755 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004756 if (open->op_odstate)
4757 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004758}
4759
Al Virob37ad282006-10-19 23:28:59 -07004760__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004761nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02004762 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763{
Christoph Hellwigeb698532017-05-08 20:58:35 +02004764 clientid_t *clid = &u->renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004766 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004767 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 dprintk("process_renew(%08x/%08x): starting\n",
4770 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004771 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004772 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004774 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004776 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004777 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 goto out;
4779 status = nfs_ok;
4780out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 return status;
4782}
4783
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004784void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004785nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004786{
Jeff Layton33dcc482012-04-10 11:08:48 -04004787 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004788 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004789 return;
4790
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004791 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004792 /*
4793 * If the server goes down again right now, an NFSv4
4794 * client will still be allowed to reclaim after it comes back up,
4795 * even if it hasn't yet had a chance to reclaim state this time.
4796 *
4797 */
Jeff Layton919b8042014-09-12 16:40:20 -04004798 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004799 /*
4800 * At this point, NFSv4 clients can still reclaim. But if the
4801 * server crashes, any that have not yet reclaimed will be out
4802 * of luck on the next boot.
4803 *
4804 * (NFSv4.1+ clients are considered to have reclaimed once they
4805 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4806 * have reclaimed after their first OPEN.)
4807 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004808 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004809 /*
4810 * At this point, and once lockd and/or any other containers
4811 * exit their grace period, further reclaims will fail and
4812 * regular locking can resume.
4813 */
NeilBrowna76b4312005-06-23 22:04:01 -07004814}
4815
J. Bruce Fields03f318c2018-06-08 12:28:47 -04004816/*
4817 * If we've waited a lease period but there are still clients trying to
4818 * reclaim, wait a little longer to give them a chance to finish.
4819 */
4820static bool clients_still_reclaiming(struct nfsd_net *nn)
4821{
4822 unsigned long now = get_seconds();
4823 unsigned long double_grace_period_end = nn->boot_time +
4824 2 * nn->nfsd4_lease;
4825
Scott Mayhew362063a2019-03-26 18:06:28 -04004826 if (nn->track_reclaim_completes &&
4827 atomic_read(&nn->nr_reclaim_complete) ==
4828 nn->reclaim_str_hashtbl_size)
4829 return false;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04004830 if (!nn->somebody_reclaimed)
4831 return false;
4832 nn->somebody_reclaimed = false;
4833 /*
4834 * If we've given them *two* lease times to reclaim, and they're
4835 * still not done, give up:
4836 */
4837 if (time_after(now, double_grace_period_end))
4838 return false;
4839 return true;
4840}
4841
NeilBrownfd39ca92005-06-23 22:04:03 -07004842static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004843nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844{
4845 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004846 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004848 struct nfs4_ol_stateid *stp;
Jeff Layton7919d0a2016-09-16 16:28:25 -04004849 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004851 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004852 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 dprintk("NFSD: laundromat service - starting\n");
J. Bruce Fields03f318c2018-06-08 12:28:47 -04004855
4856 if (clients_still_reclaiming(nn)) {
4857 new_timeo = 0;
4858 goto out;
4859 }
Scott Mayhew362063a2019-03-26 18:06:28 -04004860 dprintk("NFSD: end of grace period\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004861 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004862 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004863 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004864 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 clp = list_entry(pos, struct nfs4_client, cl_lru);
4866 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4867 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004868 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 break;
4870 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004871 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004872 dprintk("NFSD: client in use (clientid %08x)\n",
4873 clp->cl_clientid.cl_id);
4874 continue;
4875 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004876 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004877 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004878 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004879 list_for_each_safe(pos, next, &reaplist) {
4880 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 dprintk("NFSD: purging unused client (clientid %08x)\n",
4882 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004883 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 expire_client(clp);
4885 }
Benny Halevycdc975052014-05-30 09:09:30 -04004886 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004887 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4889 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004890 t = dp->dl_time - cutoff;
4891 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 break;
4893 }
Jeff Layton3fcbbd22015-08-24 12:41:48 -04004894 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04004895 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 }
Benny Halevycdc975052014-05-30 09:09:30 -04004897 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004898 while (!list_empty(&reaplist)) {
4899 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4900 dl_recall_lru);
4901 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004902 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 }
Jeff Layton217526e2014-07-30 08:27:11 -04004904
4905 spin_lock(&nn->client_lock);
4906 while (!list_empty(&nn->close_lru)) {
4907 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4908 oo_close_lru);
4909 if (time_after((unsigned long)oo->oo_time,
4910 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004911 t = oo->oo_time - cutoff;
4912 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 break;
4914 }
Jeff Layton217526e2014-07-30 08:27:11 -04004915 list_del_init(&oo->oo_close_lru);
4916 stp = oo->oo_last_closed_stid;
4917 oo->oo_last_closed_stid = NULL;
4918 spin_unlock(&nn->client_lock);
4919 nfs4_put_stid(&stp->st_stid);
4920 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 }
Jeff Layton217526e2014-07-30 08:27:11 -04004922 spin_unlock(&nn->client_lock);
4923
Jeff Layton7919d0a2016-09-16 16:28:25 -04004924 /*
4925 * It's possible for a client to try and acquire an already held lock
4926 * that is being held for a long time, and then lose interest in it.
4927 * So, we clean out any un-revisited request after a lease period
4928 * under the assumption that the client is no longer interested.
4929 *
4930 * RFC5661, sec. 9.6 states that the client must not rely on getting
4931 * notifications and must continue to poll for locks, even when the
4932 * server supports them. Thus this shouldn't lead to clients blocking
4933 * indefinitely once the lock does become free.
4934 */
4935 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04004936 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04004937 while (!list_empty(&nn->blocked_locks_lru)) {
4938 nbl = list_first_entry(&nn->blocked_locks_lru,
4939 struct nfsd4_blocked_lock, nbl_lru);
4940 if (time_after((unsigned long)nbl->nbl_time,
4941 (unsigned long)cutoff)) {
4942 t = nbl->nbl_time - cutoff;
4943 new_timeo = min(new_timeo, t);
4944 break;
4945 }
4946 list_move(&nbl->nbl_lru, &reaplist);
4947 list_del_init(&nbl->nbl_list);
4948 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04004949 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04004950
4951 while (!list_empty(&reaplist)) {
Naofumi Honda64ebe1242017-11-09 10:57:16 -05004952 nbl = list_first_entry(&reaplist,
Jeff Layton7919d0a2016-09-16 16:28:25 -04004953 struct nfsd4_blocked_lock, nbl_lru);
4954 list_del_init(&nbl->nbl_lru);
Jeff Layton7919d0a2016-09-16 16:28:25 -04004955 free_blocked_lock(nbl);
4956 }
J. Bruce Fields03f318c2018-06-08 12:28:47 -04004957out:
Jeff Laytona832e7a2014-05-30 09:09:26 -04004958 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004959 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960}
4961
Harvey Harrisona254b242008-02-20 12:49:00 -08004962static struct workqueue_struct *laundry_wq;
4963static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004964
4965static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004966laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967{
4968 time_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08004969 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004970 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4971 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004973 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004975 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976}
4977
Jeff Layton8fcd4612015-07-30 06:57:46 -04004978static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004979{
Jeff Layton8fcd4612015-07-30 06:57:46 -04004980 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004981 return nfserr_bad_stateid;
4982 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983}
4984
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004986access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004988 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4989 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4990 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991}
4992
4993static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004994access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004996 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4997 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998}
4999
5000static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005001__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002{
Al Virob37ad282006-10-19 23:28:59 -07005003 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004
J. Bruce Fields02921912010-07-29 15:16:59 -04005005 /* For lock stateid's, we test the parent open, not the lock: */
5006 if (stp->st_openstp)
5007 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005008 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005010 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 goto out;
5012 status = nfs_ok;
5013out:
5014 return status;
5015}
5016
Al Virob37ad282006-10-19 23:28:59 -07005017static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005018check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005020 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005022 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005023 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 * conflicting state; so we must wait out the grace period. */
5025 return nfserr_grace;
5026 } else if (flags & WR_STATE)
5027 return nfs4_share_conflict(current_fh,
5028 NFS4_SHARE_DENY_WRITE);
5029 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
5030 return nfs4_share_conflict(current_fh,
5031 NFS4_SHARE_DENY_READ);
5032}
5033
5034/*
5035 * Allow READ/WRITE during grace period on recovered state only for files
5036 * that are not able to provide mandatory locking.
5037 */
5038static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005039grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005041 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042}
5043
J. Bruce Fields57b7b432012-04-25 17:58:50 -04005044static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05005045{
Andy Adamson66689582009-04-03 08:28:45 +03005046 /*
5047 * When sessions are used the stateid generation number is ignored
5048 * when it is zero.
5049 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04005050 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04005051 return nfs_ok;
5052
5053 if (in->si_generation == ref->si_generation)
5054 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03005055
J. Bruce Fields0836f582008-01-26 19:08:12 -05005056 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04005057 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05005058 return nfserr_bad_stateid;
5059 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04005060 * However, we could see a stateid from the past, even from a
5061 * non-buggy client. For example, if the client sends a lock
5062 * while some IO is outstanding, the lock may bump si_generation
5063 * while the IO is still in flight. The client could avoid that
5064 * situation by waiting for responses on all the IO requests,
5065 * but better performance may result in retrying IO that
5066 * receives an old_stateid error if requests are rarely
5067 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05005068 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04005069 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05005070}
5071
Trond Myklebust03da3162017-11-03 08:00:16 -04005072static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
5073{
5074 __be32 ret;
5075
5076 spin_lock(&s->sc_lock);
5077 ret = nfsd4_verify_open_stid(s);
5078 if (ret == nfs_ok)
5079 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
5080 spin_unlock(&s->sc_lock);
5081 return ret;
5082}
5083
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005084static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
5085{
5086 if (ols->st_stateowner->so_is_open_owner &&
5087 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5088 return nfserr_bad_stateid;
5089 return nfs_ok;
5090}
5091
Chuck Lever7df302f2012-05-29 13:56:37 -04005092static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04005093{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005094 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005095 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04005096
Andrew Elbleae254dac2017-11-09 13:41:10 -05005097 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5098 CLOSE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04005099 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04005100 /* Client debugging aid. */
5101 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
5102 char addr_str[INET6_ADDRSTRLEN];
5103 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
5104 sizeof(addr_str));
5105 pr_warn_ratelimited("NFSD: client %s testing state ID "
5106 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005107 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04005108 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005109 spin_lock(&cl->cl_lock);
5110 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005111 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005112 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005113 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04005114 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005115 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04005116 switch (s->sc_type) {
5117 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005118 status = nfs_ok;
5119 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005120 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005121 status = nfserr_deleg_revoked;
5122 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005123 case NFS4_OPEN_STID:
5124 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005125 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04005126 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005127 default:
5128 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005129 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04005130 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005131 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005132 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04005133 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005134out_unlock:
5135 spin_unlock(&cl->cl_lock);
5136 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04005137}
5138
Christoph Hellwigcd61c522014-08-14 08:44:57 +02005139__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005140nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5141 stateid_t *stateid, unsigned char typemask,
5142 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005143{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005144 __be32 status;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005145 bool return_revoked = false;
5146
5147 /*
5148 * only return revoked delegations if explicitly asked.
5149 * otherwise we report revoked or bad_stateid status.
5150 */
5151 if (typemask & NFS4_REVOKED_DELEG_STID)
5152 return_revoked = true;
5153 else if (typemask & NFS4_DELEG_STID)
5154 typemask |= NFS4_REVOKED_DELEG_STID;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005155
Andrew Elbleae254dac2017-11-09 13:41:10 -05005156 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5157 CLOSE_STATEID(stateid))
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005158 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005159 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005160 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005161 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005162 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005163 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005164 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005165 if (status)
5166 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005167 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005168 if (!*s)
5169 return nfserr_bad_stateid;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005170 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5171 nfs4_put_stid(*s);
5172 if (cstate->minorversion)
5173 return nfserr_deleg_revoked;
5174 return nfserr_bad_stateid;
5175 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005176 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005177}
5178
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005179static struct file *
5180nfs4_find_file(struct nfs4_stid *s, int flags)
5181{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005182 if (!s)
5183 return NULL;
5184
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005185 switch (s->sc_type) {
5186 case NFS4_DELEG_STID:
5187 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5188 return NULL;
5189 return get_file(s->sc_file->fi_deleg_file);
5190 case NFS4_OPEN_STID:
5191 case NFS4_LOCK_STID:
5192 if (flags & RD_STATE)
5193 return find_readable_file(s->sc_file);
5194 else
5195 return find_writeable_file(s->sc_file);
5196 break;
5197 }
5198
5199 return NULL;
5200}
5201
5202static __be32
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005203nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005204{
5205 __be32 status;
5206
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005207 status = nfsd4_check_openowner_confirmed(ols);
5208 if (status)
5209 return status;
5210 return nfs4_check_openmode(ols, flags);
5211}
5212
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005213static __be32
5214nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
5215 struct file **filpp, bool *tmp_file, int flags)
5216{
5217 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
5218 struct file *file;
5219 __be32 status;
5220
5221 file = nfs4_find_file(s, flags);
5222 if (file) {
5223 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5224 acc | NFSD_MAY_OWNER_OVERRIDE);
5225 if (status) {
5226 fput(file);
5227 return status;
5228 }
5229
5230 *filpp = file;
5231 } else {
5232 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
5233 if (status)
5234 return status;
5235
5236 if (tmp_file)
5237 *tmp_file = true;
5238 }
5239
5240 return 0;
5241}
5242
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005244 * Checks for stateid operations
5245 */
Al Virob37ad282006-10-19 23:28:59 -07005246__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005247nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01005248 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
5249 stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005251 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005252 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005253 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005254 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005255 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 if (filpp)
5258 *filpp = NULL;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005259 if (tmp_file)
5260 *tmp_file = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005262 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 return nfserr_grace;
5264
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005265 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5266 status = check_special_stateids(net, fhp, stateid, flags);
5267 goto done;
5268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005270 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04005271 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005272 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005273 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04005274 return status;
Trond Myklebust03da3162017-11-03 08:00:16 -04005275 status = nfsd4_stid_check_stateid_generation(stateid, s,
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005276 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04005277 if (status)
5278 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005279
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005280 switch (s->sc_type) {
5281 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005282 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005283 break;
5284 case NFS4_OPEN_STID:
5285 case NFS4_LOCK_STID:
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005286 status = nfs4_check_olstateid(openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005287 break;
5288 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04005289 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005290 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04005292 if (status)
5293 goto out;
5294 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005295
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005296done:
5297 if (!status && filpp)
5298 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299out:
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005300 if (s)
5301 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 return status;
5303}
5304
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005305/*
Bryan Schumaker17456802011-07-13 10:50:48 -04005306 * Test if the stateid is valid
5307 */
5308__be32
5309nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005310 union nfsd4_op_u *u)
Bryan Schumaker17456802011-07-13 10:50:48 -04005311{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005312 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005313 struct nfsd4_test_stateid_id *stateid;
5314 struct nfs4_client *cl = cstate->session->se_client;
5315
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005316 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04005317 stateid->ts_id_status =
5318 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005319
Bryan Schumaker17456802011-07-13 10:50:48 -04005320 return nfs_ok;
5321}
5322
Chuck Lever42691392016-08-11 10:37:30 -04005323static __be32
5324nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5325{
5326 struct nfs4_ol_stateid *stp = openlockstateid(s);
5327 __be32 ret;
5328
Trond Myklebust659aefb2017-11-03 08:00:13 -04005329 ret = nfsd4_lock_ol_stateid(stp);
5330 if (ret)
5331 goto out_put_stid;
Chuck Lever42691392016-08-11 10:37:30 -04005332
5333 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5334 if (ret)
5335 goto out;
5336
5337 ret = nfserr_locks_held;
5338 if (check_for_locks(stp->st_stid.sc_file,
5339 lockowner(stp->st_stateowner)))
5340 goto out;
5341
5342 release_lock_stateid(stp);
5343 ret = nfs_ok;
5344
5345out:
5346 mutex_unlock(&stp->st_mutex);
Trond Myklebust659aefb2017-11-03 08:00:13 -04005347out_put_stid:
Chuck Lever42691392016-08-11 10:37:30 -04005348 nfs4_put_stid(s);
5349 return ret;
5350}
5351
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005352__be32
5353nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005354 union nfsd4_op_u *u)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005355{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005356 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005357 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005358 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005359 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005360 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005361 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005362
Jeff Layton1af71cc2014-07-29 21:34:14 -04005363 spin_lock(&cl->cl_lock);
5364 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005365 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005366 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005367 spin_lock(&s->sc_lock);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005368 switch (s->sc_type) {
5369 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005370 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005371 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005372 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005373 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5374 if (ret)
5375 break;
5376 ret = nfserr_locks_held;
5377 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005378 case NFS4_LOCK_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005379 spin_unlock(&s->sc_lock);
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03005380 refcount_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005381 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04005382 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005383 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005384 case NFS4_REVOKED_DELEG_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005385 spin_unlock(&s->sc_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005386 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005387 list_del_init(&dp->dl_recall_lru);
5388 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04005389 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005390 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005391 goto out;
5392 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005393 }
Trond Myklebust03da3162017-11-03 08:00:16 -04005394 spin_unlock(&s->sc_lock);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005395out_unlock:
5396 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005397out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005398 return ret;
5399}
5400
NeilBrown4c4cd222005-07-07 17:59:27 -07005401static inline int
5402setlkflg (int type)
5403{
5404 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5405 RD_STATE : WR_STATE;
5406}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005408static __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 -04005409{
5410 struct svc_fh *current_fh = &cstate->current_fh;
5411 struct nfs4_stateowner *sop = stp->st_stateowner;
5412 __be32 status;
5413
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005414 status = nfsd4_check_seqid(cstate, sop, seqid);
5415 if (status)
5416 return status;
Trond Myklebust9271d7e2017-11-03 08:00:15 -04005417 status = nfsd4_lock_ol_stateid(stp);
5418 if (status != nfs_ok)
5419 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005420 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04005421 if (status == nfs_ok)
5422 status = nfs4_check_fh(current_fh, &stp->st_stid);
5423 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005424 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005425 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005426}
5427
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428/*
5429 * Checks for sequence id mutating operations.
5430 */
Al Virob37ad282006-10-19 23:28:59 -07005431static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03005432nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04005433 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005434 struct nfs4_ol_stateid **stpp,
5435 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436{
J. Bruce Fields0836f582008-01-26 19:08:12 -05005437 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005438 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005439 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005441 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5442 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07005443
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005445 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005446 if (status)
5447 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005448 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005449 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005451 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005452 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005453 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04005454 else
5455 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005456 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005457}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05005458
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005459static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5460 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005461{
5462 __be32 status;
5463 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005464 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005466 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005467 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04005468 if (status)
5469 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005470 oo = openowner(stp->st_stateowner);
5471 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005472 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005473 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07005474 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005475 }
5476 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07005477 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478}
5479
Al Virob37ad282006-10-19 23:28:59 -07005480__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005481nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005482 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005484 struct nfsd4_open_confirm *oc = &u->open_confirm;
Al Virob37ad282006-10-19 23:28:59 -07005485 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005486 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005487 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005488 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489
Al Viroa6a9f182013-09-16 10:57:01 -04005490 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5491 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005493 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07005494 if (status)
5495 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005497 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005498 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005499 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005500 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005501 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005502 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005503 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005504 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005505 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04005506 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005507 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04005508 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04005509 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005510 mutex_unlock(&stp->st_mutex);
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005511 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005512 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07005513
Jeff Layton2a4317c2012-03-21 16:42:43 -04005514 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005515 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04005516put_stateid:
5517 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005519 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 return status;
5521}
5522
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005523static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005525 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005526 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04005527 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005528 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005529}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04005530
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005531static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5532{
5533 switch (to_access) {
5534 case NFS4_SHARE_ACCESS_READ:
5535 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5536 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5537 break;
5538 case NFS4_SHARE_ACCESS_WRITE:
5539 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5540 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5541 break;
5542 case NFS4_SHARE_ACCESS_BOTH:
5543 break;
5544 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005545 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 }
5547}
5548
Al Virob37ad282006-10-19 23:28:59 -07005549__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005550nfsd4_open_downgrade(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005551 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005553 struct nfsd4_open_downgrade *od = &u->open_downgrade;
Al Virob37ad282006-10-19 23:28:59 -07005554 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005555 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005556 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557
Al Viroa6a9f182013-09-16 10:57:01 -04005558 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5559 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04005561 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02005562 if (od->od_deleg_want)
5563 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5564 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005566 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005567 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005568 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005571 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005572 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005574 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04005576 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005577 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005579 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005581 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04005582 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04005583 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005585put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005586 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005587 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005589 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 return status;
5591}
5592
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005593static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5594{
Trond Myklebustacf92952014-06-30 11:48:37 -04005595 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04005596 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04005597 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005598
Jeff Layton2c41beb2014-07-29 21:34:41 -04005599 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04005600 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005601
Jeff Laytond83017f2014-07-29 21:34:42 -04005602 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04005603 if (unhashed)
5604 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04005605 spin_unlock(&clp->cl_lock);
5606 free_ol_stateid_reaplist(&reaplist);
5607 } else {
5608 spin_unlock(&clp->cl_lock);
5609 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04005610 if (unhashed)
5611 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04005612 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04005613}
5614
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615/*
5616 * nfs4_unlock_state() called after encode
5617 */
Al Virob37ad282006-10-19 23:28:59 -07005618__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005619nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005620 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005622 struct nfsd4_close *close = &u->close;
Al Virob37ad282006-10-19 23:28:59 -07005623 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005624 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005625 struct net *net = SVC_NET(rqstp);
5626 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627
Al Viroa6a9f182013-09-16 10:57:01 -04005628 dprintk("NFSD: nfsd4_close on file %pd\n",
5629 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005631 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5632 &close->cl_stateid,
5633 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005634 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005635 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005636 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005638
5639 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Laytonbd2deca2018-02-15 15:16:18 -05005640
5641 /*
5642 * Technically we don't _really_ have to increment or copy it, since
5643 * it should just be gone after this operation and we clobber the
5644 * copied value below, but we continue to do so here just to ensure
5645 * that racing ops see that there was a state change.
5646 */
Jeff Layton9767feb2015-10-01 09:05:50 -04005647 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005649 nfsd4_close_open_stateid(stp);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005650 mutex_unlock(&stp->st_mutex);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005651
Jeff Laytonbd2deca2018-02-15 15:16:18 -05005652 /* v4.1+ suggests that we send a special stateid in here, since the
5653 * clients should just ignore this anyway. Since this is not useful
5654 * for v4.0 clients either, we set it to the special close_stateid
5655 * universally.
5656 *
5657 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
5658 */
5659 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
Trond Myklebustfb500a72017-11-03 08:00:12 -04005660
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005661 /* put reference from nfs4_preprocess_seqid_op */
5662 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 return status;
5665}
5666
Al Virob37ad282006-10-19 23:28:59 -07005667__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005668nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005669 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005671 struct nfsd4_delegreturn *dr = &u->delegreturn;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005672 struct nfs4_delegation *dp;
5673 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005674 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07005675 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005676 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005678 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005679 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005681 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005682 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005683 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005684 dp = delegstateid(s);
Trond Myklebust03da3162017-11-03 08:00:16 -04005685 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005686 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04005687 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005688
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005689 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005690put_stateid:
5691 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692out:
5693 return status;
5694}
5695
Benny Halevy87df4de2008-12-15 19:42:03 +02005696static inline u64
5697end_offset(u64 start, u64 len)
5698{
5699 u64 end;
5700
5701 end = start + len;
5702 return end >= start ? end: NFS4_MAX_UINT64;
5703}
5704
5705/* last octet in a range */
5706static inline u64
5707last_byte_offset(u64 start, u64 len)
5708{
5709 u64 end;
5710
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005711 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02005712 end = start + len;
5713 return end > start ? end - 1: NFS4_MAX_UINT64;
5714}
5715
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716/*
5717 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5718 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5719 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5720 * locking, this prevents us from being completely protocol-compliant. The
5721 * real solution to this problem is to start using unsigned file offsets in
5722 * the VFS, but this is a very deep change!
5723 */
5724static inline void
5725nfs4_transform_lock_offset(struct file_lock *lock)
5726{
5727 if (lock->fl_start < 0)
5728 lock->fl_start = OFFSET_MAX;
5729 if (lock->fl_end < 0)
5730 lock->fl_end = OFFSET_MAX;
5731}
5732
Jeff Laytoncae80b32015-04-03 09:04:04 -04005733static fl_owner_t
5734nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005735{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005736 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5737
5738 nfs4_get_stateowner(&lo->lo_owner);
5739 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005740}
5741
Jeff Laytoncae80b32015-04-03 09:04:04 -04005742static void
5743nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005744{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005745 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005746
Jeff Laytoncae80b32015-04-03 09:04:04 -04005747 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04005748 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04005749}
5750
Jeff Layton76d348f2016-09-16 16:28:24 -04005751static void
5752nfsd4_lm_notify(struct file_lock *fl)
5753{
5754 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
5755 struct net *net = lo->lo_owner.so_client->net;
5756 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5757 struct nfsd4_blocked_lock *nbl = container_of(fl,
5758 struct nfsd4_blocked_lock, nbl_lock);
5759 bool queue = false;
5760
Jeff Layton7919d0a2016-09-16 16:28:25 -04005761 /* An empty list means that something else is going to be using it */
Jeff Layton0cc11a62016-10-20 09:34:31 -04005762 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005763 if (!list_empty(&nbl->nbl_list)) {
5764 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005765 list_del_init(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -04005766 queue = true;
5767 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04005768 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005769
5770 if (queue)
5771 nfsd4_run_cb(&nbl->nbl_cb);
5772}
5773
Alexey Dobriyan7b021962009-09-21 17:01:12 -07005774static const struct lock_manager_operations nfsd_posix_mng_ops = {
Jeff Layton76d348f2016-09-16 16:28:24 -04005775 .lm_notify = nfsd4_lm_notify,
Kinglong Meeaef95832014-08-22 10:18:44 -04005776 .lm_get_owner = nfsd4_fl_get_owner,
5777 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07005778};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779
5780static inline void
5781nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5782{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005783 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784
NeilBrownd5b90262006-04-10 22:55:22 -07005785 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005786 lo = (struct nfs4_lockowner *) fl->fl_owner;
5787 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5788 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005789 if (!deny->ld_owner.data)
5790 /* We just don't care that much */
5791 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005792 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5793 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07005794 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005795nevermind:
5796 deny->ld_owner.len = 0;
5797 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07005798 deny->ld_clientid.cl_boot = 0;
5799 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 }
5801 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02005802 deny->ld_length = NFS4_MAX_UINT64;
5803 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5805 deny->ld_type = NFS4_READ_LT;
5806 if (fl->fl_type != F_RDLCK)
5807 deny->ld_type = NFS4_WRITE_LT;
5808}
5809
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005810static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005811find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812{
Trond Myklebustd4f04892014-07-29 21:34:36 -04005813 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005814 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815
Trond Myklebust0a880a22014-07-30 08:27:10 -04005816 lockdep_assert_held(&clp->cl_lock);
5817
Trond Myklebustd4f04892014-07-29 21:34:36 -04005818 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5819 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005820 if (so->so_is_open_owner)
5821 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005822 if (same_owner_str(so, owner))
5823 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824 }
5825 return NULL;
5826}
5827
Trond Myklebustc58c6612014-07-29 21:34:35 -04005828static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005829find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04005830{
5831 struct nfs4_lockowner *lo;
5832
Trond Myklebustd4f04892014-07-29 21:34:36 -04005833 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005834 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005835 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005836 return lo;
5837}
5838
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005839static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5840{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005841 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005842}
5843
Jeff Layton6b180f02014-07-29 21:34:26 -04005844static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5845{
5846 struct nfs4_lockowner *lo = lockowner(sop);
5847
5848 kmem_cache_free(lockowner_slab, lo);
5849}
5850
5851static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005852 .so_unhash = nfs4_unhash_lockowner,
5853 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005854};
5855
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856/*
5857 * Alloc a lock owner structure.
5858 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005859 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005861 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005863static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005864alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5865 struct nfs4_ol_stateid *open_stp,
5866 struct nfsd4_lock *lock)
5867{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005868 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005870 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5871 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04005873 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005874 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5875 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005876 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005877 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005878 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005879 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005880 if (ret == NULL) {
5881 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005882 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005883 ret = lo;
5884 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08005885 nfs4_free_stateowner(&lo->lo_owner);
5886
Trond Myklebustd4f04892014-07-29 21:34:36 -04005887 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04005888 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889}
5890
Trond Myklebustfd1fd682017-11-03 08:00:14 -04005891static struct nfs4_ol_stateid *
5892find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5893{
5894 struct nfs4_ol_stateid *lst;
5895 struct nfs4_client *clp = lo->lo_owner.so_client;
5896
5897 lockdep_assert_held(&clp->cl_lock);
5898
5899 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
5900 if (lst->st_stid.sc_type != NFS4_LOCK_STID)
5901 continue;
5902 if (lst->st_stid.sc_file == fp) {
5903 refcount_inc(&lst->st_stid.sc_count);
5904 return lst;
5905 }
5906 }
5907 return NULL;
5908}
5909
Trond Myklebustbeeca192017-11-03 08:00:14 -04005910static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04005911init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5912 struct nfs4_file *fp, struct inode *inode,
5913 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005915 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustbeeca192017-11-03 08:00:14 -04005916 struct nfs4_ol_stateid *retstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917
Trond Myklebustbeeca192017-11-03 08:00:14 -04005918 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05005919 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Trond Myklebustbeeca192017-11-03 08:00:14 -04005920retry:
5921 spin_lock(&clp->cl_lock);
5922 spin_lock(&fp->fi_lock);
5923 retstp = find_lock_stateid(lo, fp);
5924 if (retstp)
5925 goto out_unlock;
Jeff Layton356a95e2014-07-29 21:34:13 -04005926
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03005927 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005928 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005929 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07005930 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005931 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005932 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005934 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005935 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005936 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005937 list_add(&stp->st_perfile, &fp->fi_stateids);
Trond Myklebustbeeca192017-11-03 08:00:14 -04005938out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04005939 spin_unlock(&fp->fi_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04005940 spin_unlock(&clp->cl_lock);
5941 if (retstp) {
5942 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
5943 nfs4_put_stid(&retstp->st_stid);
5944 goto retry;
5945 }
5946 /* To keep mutex tracking happy */
5947 mutex_unlock(&stp->st_mutex);
5948 stp = retstp;
5949 }
5950 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951}
5952
Jeff Laytonc53530d2014-06-30 11:48:39 -04005953static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04005954find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5955 struct inode *inode, struct nfs4_ol_stateid *ost,
5956 bool *new)
5957{
5958 struct nfs4_stid *ns = NULL;
5959 struct nfs4_ol_stateid *lst;
5960 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5961 struct nfs4_client *clp = oo->oo_owner.so_client;
5962
Trond Myklebustbeeca192017-11-03 08:00:14 -04005963 *new = false;
Jeff Layton356a95e2014-07-29 21:34:13 -04005964 spin_lock(&clp->cl_lock);
5965 lst = find_lock_stateid(lo, fi);
Jeff Layton356a95e2014-07-29 21:34:13 -04005966 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04005967 if (lst != NULL) {
5968 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
5969 goto out;
5970 nfs4_put_stid(&lst->st_stid);
5971 }
5972 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
5973 if (ns == NULL)
5974 return NULL;
5975
5976 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
5977 if (lst == openlockstateid(ns))
5978 *new = true;
5979 else
Jeff Layton356a95e2014-07-29 21:34:13 -04005980 nfs4_put_stid(ns);
Trond Myklebustbeeca192017-11-03 08:00:14 -04005981out:
Jeff Layton356a95e2014-07-29 21:34:13 -04005982 return lst;
5983}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005984
NeilBrownfd39ca92005-06-23 22:04:03 -07005985static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986check_lock_length(u64 offset, u64 length)
5987{
Kinglong Meee7969312015-07-13 17:34:19 +08005988 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5989 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990}
5991
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005992static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005993{
Trond Myklebust11b91642014-07-29 21:34:08 -04005994 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005995
Jeff Layton7214e862014-07-10 14:07:33 -04005996 lockdep_assert_held(&fp->fi_lock);
5997
Jeff Layton82c5ff12012-05-11 09:45:13 -04005998 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005999 return;
Jeff Layton12659652014-07-10 14:07:28 -04006000 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006001 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006002}
6003
Jeff Layton356a95e2014-07-29 21:34:13 -04006004static __be32
6005lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
6006 struct nfs4_ol_stateid *ost,
6007 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04006008 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006009{
Jeff Layton5db1c032014-07-29 21:34:28 -04006010 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04006011 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006012 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6013 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00006014 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006015 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04006016 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006017 unsigned int strhashval;
6018
Kinglong Meec8623992015-07-13 17:35:37 +08006019 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006020 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006021 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006022 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
6023 if (lo == NULL)
6024 return nfserr_jukebox;
6025 } else {
6026 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04006027 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04006028 if (!cstate->minorversion &&
6029 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04006030 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006031 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04006032
Jeff Laytondd257932016-08-11 10:37:39 -04006033 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
6034 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04006035 status = nfserr_jukebox;
6036 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006037 }
Jeff Laytondd257932016-08-11 10:37:39 -04006038
Jeff Layton5db1c032014-07-29 21:34:28 -04006039 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04006040 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04006041out:
6042 nfs4_put_stateowner(&lo->lo_owner);
6043 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006044}
6045
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046/*
6047 * LOCK operation
6048 */
Al Virob37ad282006-10-19 23:28:59 -07006049__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006050nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006051 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006053 struct nfsd4_lock *lock = &u->lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006054 struct nfs4_openowner *open_sop = NULL;
6055 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006056 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006057 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04006058 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04006059 struct file *filp = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006060 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006061 struct file_lock *file_lock = NULL;
6062 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006063 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006064 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07006065 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04006066 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04006067 unsigned char fl_type;
6068 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006069 struct net *net = SVC_NET(rqstp);
6070 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071
6072 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
6073 (long long) lock->lk_offset,
6074 (long long) lock->lk_length);
6075
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 if (check_lock_length(lock->lk_offset, lock->lk_length))
6077 return nfserr_inval;
6078
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006079 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02006080 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08006081 dprintk("NFSD: nfsd4_lock: permission denied!\n");
6082 return status;
6083 }
6084
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04006086 if (nfsd4_has_session(cstate))
6087 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006088 memcpy(&lock->lk_new_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04006089 &cstate->session->se_client->cl_clientid,
6090 sizeof(clientid_t));
6091
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006093 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006097 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 lock->lk_new_open_seqid,
6099 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006100 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07006101 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006103 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006104 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006105 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04006106 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006107 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006108 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006109 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04006110 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006111 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03006112 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006113 lock->lk_old_lock_seqid,
6114 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006115 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006116 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04006117 if (status)
6118 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006119 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006121 lkflg = setlkflg(lock->lk_type);
6122 status = nfs4_check_openmode(lock_stp, lkflg);
6123 if (status)
6124 goto out;
6125
NeilBrown0dd395d2005-07-07 17:59:15 -07006126 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006127 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006128 goto out;
6129 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006130 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006131 goto out;
6132
Trond Myklebust11b91642014-07-29 21:34:08 -04006133 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 case NFS4_READW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006136 if (nfsd4_has_session(cstate))
6137 fl_flags |= FL_SLEEP;
6138 /* Fallthrough */
6139 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006140 spin_lock(&fp->fi_lock);
6141 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006142 if (filp)
6143 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04006144 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006145 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006146 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 case NFS4_WRITEW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006148 if (nfsd4_has_session(cstate))
6149 fl_flags |= FL_SLEEP;
6150 /* Fallthrough */
6151 case NFS4_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006152 spin_lock(&fp->fi_lock);
6153 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006154 if (filp)
6155 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04006156 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006157 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006158 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159 default:
6160 status = nfserr_inval;
6161 goto out;
6162 }
Jeff Layton76d348f2016-09-16 16:28:24 -04006163
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006164 if (!filp) {
6165 status = nfserr_openmode;
6166 goto out;
6167 }
Kinglong Meeaef95832014-08-22 10:18:44 -04006168
Jeff Layton76d348f2016-09-16 16:28:24 -04006169 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6170 if (!nbl) {
6171 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6172 status = nfserr_jukebox;
6173 goto out;
6174 }
6175
6176 file_lock = &nbl->nbl_lock;
6177 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04006178 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04006179 file_lock->fl_pid = current->tgid;
6180 file_lock->fl_file = filp;
Jeff Layton76d348f2016-09-16 16:28:24 -04006181 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04006182 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6183 file_lock->fl_start = lock->lk_offset;
6184 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6185 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186
Jeff Layton21179d82012-08-21 08:03:32 -04006187 conflock = locks_alloc_lock();
6188 if (!conflock) {
6189 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6190 status = nfserr_jukebox;
6191 goto out;
6192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193
Jeff Layton76d348f2016-09-16 16:28:24 -04006194 if (fl_flags & FL_SLEEP) {
Jeff Layton7919d0a2016-09-16 16:28:25 -04006195 nbl->nbl_time = jiffies;
Jeff Layton0cc11a62016-10-20 09:34:31 -04006196 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006197 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006198 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006199 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006200 }
6201
Jeff Layton21179d82012-08-21 08:03:32 -04006202 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006203 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04006205 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07006206 status = 0;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04006207 if (lock->lk_reclaim)
6208 nn->somebody_reclaimed = true;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006209 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006210 case FILE_LOCK_DEFERRED:
6211 nbl = NULL;
6212 /* Fallthrough */
6213 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006214 status = nfserr_denied;
6215 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04006216 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006217 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006218 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006220 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04006221 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05006222 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04006223 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006224 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226out:
Jeff Layton76d348f2016-09-16 16:28:24 -04006227 if (nbl) {
6228 /* dequeue it if we queued it before */
6229 if (fl_flags & FL_SLEEP) {
Jeff Layton0cc11a62016-10-20 09:34:31 -04006230 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006231 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006232 list_del_init(&nbl->nbl_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006233 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006234 }
6235 free_blocked_lock(nbl);
6236 }
Trond Myklebustde186432014-07-10 14:07:26 -04006237 if (filp)
6238 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04006239 if (lock_stp) {
6240 /* Bump seqid manually if the 4.0 replay owner is openowner */
6241 if (cstate->replay_owner &&
6242 cstate->replay_owner != &lock_sop->lo_owner &&
6243 seqid_mutating_err(ntohl(status)))
6244 lock_sop->lo_owner.so_seqid++;
6245
6246 /*
6247 * If this is a new, never-before-used stateid, and we are
6248 * returning an error, then just go ahead and release it.
6249 */
J. Bruce Fields25020722018-01-17 16:25:59 -05006250 if (status && new)
Jeff Layton5db1c032014-07-29 21:34:28 -04006251 release_lock_stateid(lock_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006252
6253 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton5db1c032014-07-29 21:34:28 -04006254
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006255 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04006256 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006257 if (open_stp)
6258 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006259 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006260 if (conflock)
6261 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 return status;
6263}
6264
6265/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006266 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6267 * so we do a temporary open here just to get an open file to pass to
6268 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6269 * inode operation.)
6270 */
Al Viro04da6e92012-04-13 00:00:04 -04006271static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006272{
6273 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04006274 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
6275 if (!err) {
6276 err = nfserrno(vfs_test_lock(file, lock));
Christoph Hellwigfd891452015-04-28 15:41:16 +02006277 fput(file);
Al Viro04da6e92012-04-13 00:00:04 -04006278 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006279 return err;
6280}
6281
6282/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 * LOCKT operation
6284 */
Al Virob37ad282006-10-19 23:28:59 -07006285__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006286nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006287 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006289 struct nfsd4_lockt *lockt = &u->lockt;
Jeff Layton21179d82012-08-21 08:03:32 -04006290 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04006291 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006292 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006293 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006295 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 return nfserr_grace;
6297
6298 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6299 return nfserr_inval;
6300
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006301 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04006302 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006303 if (status)
6304 goto out;
6305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306
J. Bruce Fields75c096f2011-08-15 18:39:32 -04006307 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309
Jeff Layton21179d82012-08-21 08:03:32 -04006310 file_lock = locks_alloc_lock();
6311 if (!file_lock) {
6312 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6313 status = nfserr_jukebox;
6314 goto out;
6315 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006316
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 switch (lockt->lt_type) {
6318 case NFS4_READ_LT:
6319 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006320 file_lock->fl_type = F_RDLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006321 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322 case NFS4_WRITE_LT:
6323 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006324 file_lock->fl_type = F_WRLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006325 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04006327 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328 status = nfserr_inval;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006329 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 }
6331
Kinglong Meec8623992015-07-13 17:35:37 +08006332 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006333 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04006334 file_lock->fl_owner = (fl_owner_t)lo;
6335 file_lock->fl_pid = current->tgid;
6336 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337
Jeff Layton21179d82012-08-21 08:03:32 -04006338 file_lock->fl_start = lockt->lt_offset;
6339 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340
Jeff Layton21179d82012-08-21 08:03:32 -04006341 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342
Jeff Layton21179d82012-08-21 08:03:32 -04006343 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04006344 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05006345 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04006346
Jeff Layton21179d82012-08-21 08:03:32 -04006347 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04006349 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 }
6351out:
Jeff Layton5db1c032014-07-29 21:34:28 -04006352 if (lo)
6353 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04006354 if (file_lock)
6355 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356 return status;
6357}
6358
Al Virob37ad282006-10-19 23:28:59 -07006359__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006360nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006361 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006363 struct nfsd4_locku *locku = &u->locku;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006364 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006366 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006367 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07006368 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006369 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6370
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6372 (long long) locku->lu_offset,
6373 (long long) locku->lu_length);
6374
6375 if (check_lock_length(locku->lu_offset, locku->lu_length))
6376 return nfserr_inval;
6377
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006378 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006379 &locku->lu_stateid, NFS4_LOCK_STID,
6380 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006381 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04006383 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006384 if (!filp) {
6385 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04006386 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006387 }
Jeff Layton21179d82012-08-21 08:03:32 -04006388 file_lock = locks_alloc_lock();
6389 if (!file_lock) {
6390 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6391 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04006392 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04006393 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006394
Jeff Layton21179d82012-08-21 08:03:32 -04006395 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04006396 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04006397 file_lock->fl_pid = current->tgid;
6398 file_lock->fl_file = filp;
6399 file_lock->fl_flags = FL_POSIX;
6400 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6401 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402
Jeff Layton21179d82012-08-21 08:03:32 -04006403 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6404 locku->lu_length);
6405 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406
Jeff Layton21179d82012-08-21 08:03:32 -04006407 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07006408 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05006409 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 goto out_nfserr;
6411 }
Jeff Layton9767feb2015-10-01 09:05:50 -04006412 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Trond Myklebustde186432014-07-10 14:07:26 -04006413fput:
6414 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04006415put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006416 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04006417 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006419 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006420 if (file_lock)
6421 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422 return status;
6423
6424out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07006425 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04006426 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427}
6428
6429/*
6430 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006431 * true: locks held by lockowner
6432 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006434static bool
6435check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006437 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006438 int status = false;
6439 struct file *filp = find_any_file(fp);
6440 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006441 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006442
6443 if (!filp) {
6444 /* Any valid lock stateid should have some sort of access */
6445 WARN_ON_ONCE(1);
6446 return status;
6447 }
6448
Amir Goldstein64bed6c2018-07-13 17:22:24 +03006449 inode = locks_inode(filp);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006450 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006452 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05006453 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006454 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6455 if (fl->fl_owner == (fl_owner_t)lowner) {
6456 status = true;
6457 break;
6458 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08006459 }
Jeff Layton6109c852015-01-16 15:05:57 -05006460 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461 }
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006462 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463 return status;
6464}
6465
Al Virob37ad282006-10-19 23:28:59 -07006466__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08006467nfsd4_release_lockowner(struct svc_rqst *rqstp,
6468 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006469 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006471 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04006473 struct nfs4_stateowner *sop;
6474 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006475 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006477 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07006478 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006479 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006480 struct nfs4_client *clp;
Chuck Lever88584812016-07-13 16:40:14 -04006481 LIST_HEAD (reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482
6483 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6484 clid->cl_boot, clid->cl_id);
6485
Jeff Layton4b24ca72014-06-30 11:48:44 -04006486 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006487 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04006488 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006489
Trond Myklebustd4f04892014-07-29 21:34:36 -04006490 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04006491 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04006492 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04006493 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04006494 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04006495
6496 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006497 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04006498
Jeff Layton882e9d22014-07-29 21:34:37 -04006499 /* see if there are still any locks associated with it */
6500 lo = lockowner(sop);
6501 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6502 if (check_for_locks(stp->st_stid.sc_file, lo)) {
6503 status = nfserr_locks_held;
6504 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04006505 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04006506 }
Jeff Layton5adfd882014-07-29 21:34:31 -04006507 }
Jeff Layton882e9d22014-07-29 21:34:37 -04006508
Kinglong Meeb5971af2014-08-22 10:18:43 -04006509 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04006510 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04006511 }
Chuck Lever88584812016-07-13 16:40:14 -04006512 if (!lo) {
6513 spin_unlock(&clp->cl_lock);
6514 return status;
6515 }
6516
6517 unhash_lockowner_locked(lo);
6518 while (!list_empty(&lo->lo_owner.so_stateids)) {
6519 stp = list_first_entry(&lo->lo_owner.so_stateids,
6520 struct nfs4_ol_stateid,
6521 st_perstateowner);
6522 WARN_ON(!unhash_lock_stateid(stp));
6523 put_ol_stateid_locked(stp, &reaplist);
6524 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04006525 spin_unlock(&clp->cl_lock);
Chuck Lever88584812016-07-13 16:40:14 -04006526 free_ol_stateid_reaplist(&reaplist);
Jeff Layton68ef3bc2018-03-16 11:32:02 -04006527 remove_blocked_locks(lo);
Chuck Lever88584812016-07-13 16:40:14 -04006528 nfs4_put_stateowner(&lo->lo_owner);
6529
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530 return status;
6531}
6532
6533static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07006534alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535{
NeilBrowna55370a2005-06-23 22:03:52 -07006536 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537}
6538
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006539bool
Scott Mayhew6b189102019-03-26 18:06:26 -04006540nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07006541{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006542 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07006543
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006544 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006545 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07006546}
6547
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548/*
6549 * failure => all reset bets are off, nfserr_no_grace...
Scott Mayhew6b189102019-03-26 18:06:26 -04006550 *
6551 * The caller is responsible for freeing name.data if NULL is returned (it
6552 * will be freed in nfs4_remove_reclaim_record in the normal case).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05006554struct nfs4_client_reclaim *
Scott Mayhew6b189102019-03-26 18:06:26 -04006555nfs4_client_to_reclaim(struct xdr_netobj name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556{
6557 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006558 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559
Scott Mayhew6b189102019-03-26 18:06:26 -04006560 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", name.len, name.data);
NeilBrowna55370a2005-06-23 22:03:52 -07006561 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05006562 if (crp) {
6563 strhashval = clientstr_hashval(name);
6564 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006565 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Scott Mayhew6b189102019-03-26 18:06:26 -04006566 crp->cr_name.data = name.data;
6567 crp->cr_name.len = name.len;
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006568 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006569 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006570 }
6571 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572}
6573
Jeff Layton2a4317c2012-03-21 16:42:43 -04006574void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006575nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05006576{
6577 list_del(&crp->cr_strhash);
Scott Mayhew6b189102019-03-26 18:06:26 -04006578 kfree(crp->cr_name.data);
Jeff Laytonce30e532012-11-12 15:00:53 -05006579 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006580 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05006581}
6582
6583void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006584nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585{
6586 struct nfs4_client_reclaim *crp = NULL;
6587 int i;
6588
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006590 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6591 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006593 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594 }
6595 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006596 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597}
6598
6599/*
6600 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04006601struct nfs4_client_reclaim *
Scott Mayhew6b189102019-03-26 18:06:26 -04006602nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603{
6604 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 struct nfs4_client_reclaim *crp = NULL;
6606
Scott Mayhew6b189102019-03-26 18:06:26 -04006607 dprintk("NFSD: nfs4_find_reclaim_client for name %.*s\n", name.len, name.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608
Scott Mayhew6b189102019-03-26 18:06:26 -04006609 strhashval = clientstr_hashval(name);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006610 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Scott Mayhew6b189102019-03-26 18:06:26 -04006611 if (compare_blob(&crp->cr_name, &name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612 return crp;
6613 }
6614 }
6615 return NULL;
6616}
6617
6618/*
6619* Called from OPEN. Look for clientid in reclaim list.
6620*/
Al Virob37ad282006-10-19 23:28:59 -07006621__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006622nfs4_check_open_reclaim(clientid_t *clid,
6623 struct nfsd4_compound_state *cstate,
6624 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006626 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04006627
6628 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006629 status = lookup_clientid(clid, cstate, nn);
6630 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04006631 return nfserr_reclaim_bad;
6632
Jeff Layton3b3e7b72014-09-12 16:40:22 -04006633 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6634 return nfserr_no_grace;
6635
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006636 if (nfsd4_client_record_check(cstate->clp))
6637 return nfserr_reclaim_bad;
6638
6639 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640}
6641
Bryan Schumaker65178db2011-11-01 13:35:21 -04006642#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04006643static inline void
6644put_client(struct nfs4_client *clp)
6645{
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04006646 atomic_dec(&clp->cl_rpc_users);
Jeff Layton016200c2014-07-30 08:27:21 -04006647}
6648
Jeff Layton285abde2014-07-30 08:27:24 -04006649static struct nfs4_client *
6650nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6651{
6652 struct nfs4_client *clp;
6653 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6654 nfsd_net_id);
6655
6656 if (!nfsd_netns_ready(nn))
6657 return NULL;
6658
6659 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6660 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
6661 return clp;
6662 }
6663 return NULL;
6664}
6665
Jeff Layton7ec0e362014-07-30 08:27:17 -04006666u64
Jeff Layton285abde2014-07-30 08:27:24 -04006667nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04006668{
6669 struct nfs4_client *clp;
6670 u64 count = 0;
6671 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6672 nfsd_net_id);
6673 char buf[INET6_ADDRSTRLEN];
6674
6675 if (!nfsd_netns_ready(nn))
6676 return 0;
6677
6678 spin_lock(&nn->client_lock);
6679 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6680 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6681 pr_info("NFS Client: %s\n", buf);
6682 ++count;
6683 }
6684 spin_unlock(&nn->client_lock);
6685
6686 return count;
6687}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006688
Jeff Laytona0926d12014-07-30 08:27:18 -04006689u64
Jeff Layton285abde2014-07-30 08:27:24 -04006690nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04006691{
6692 u64 count = 0;
6693 struct nfs4_client *clp;
6694 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6695 nfsd_net_id);
6696
6697 if (!nfsd_netns_ready(nn))
6698 return count;
6699
6700 spin_lock(&nn->client_lock);
6701 clp = nfsd_find_client(addr, addr_size);
6702 if (clp) {
6703 if (mark_client_expired_locked(clp) == nfs_ok)
6704 ++count;
6705 else
6706 clp = NULL;
6707 }
6708 spin_unlock(&nn->client_lock);
6709
6710 if (clp)
6711 expire_client(clp);
6712
6713 return count;
6714}
6715
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006716u64
Jeff Layton285abde2014-07-30 08:27:24 -04006717nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006718{
6719 u64 count = 0;
6720 struct nfs4_client *clp, *next;
6721 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6722 nfsd_net_id);
6723 LIST_HEAD(reaplist);
6724
6725 if (!nfsd_netns_ready(nn))
6726 return count;
6727
6728 spin_lock(&nn->client_lock);
6729 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6730 if (mark_client_expired_locked(clp) == nfs_ok) {
6731 list_add(&clp->cl_lru, &reaplist);
6732 if (max != 0 && ++count >= max)
6733 break;
6734 }
6735 }
6736 spin_unlock(&nn->client_lock);
6737
6738 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6739 expire_client(clp);
6740
6741 return count;
6742}
6743
Bryan Schumaker184c1842012-11-29 11:40:44 -05006744static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6745 const char *type)
6746{
6747 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05006748 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05006749 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6750}
6751
Jeff Layton016200c2014-07-30 08:27:21 -04006752static void
6753nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6754 struct list_head *collect)
6755{
6756 struct nfs4_client *clp = lst->st_stid.sc_client;
6757 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6758 nfsd_net_id);
6759
6760 if (!collect)
6761 return;
6762
6763 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04006764 atomic_inc(&clp->cl_rpc_users);
Jeff Layton016200c2014-07-30 08:27:21 -04006765 list_add(&lst->st_locks, collect);
6766}
6767
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006768static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04006769 struct list_head *collect,
Jeff Laytone8568732015-08-24 12:41:47 -04006770 bool (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05006771{
6772 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006773 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006774 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006775 u64 count = 0;
6776
Jeff Layton016200c2014-07-30 08:27:21 -04006777 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006778 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006779 list_for_each_entry_safe(stp, st_next,
6780 &oop->oo_owner.so_stateids, st_perstateowner) {
6781 list_for_each_entry_safe(lst, lst_next,
6782 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04006783 if (func) {
Jeff Laytone8568732015-08-24 12:41:47 -04006784 if (func(lst))
6785 nfsd_inject_add_lock_to_list(lst,
6786 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04006787 }
Jeff Layton016200c2014-07-30 08:27:21 -04006788 ++count;
6789 /*
6790 * Despite the fact that these functions deal
6791 * with 64-bit integers for "count", we must
6792 * ensure that it doesn't blow up the
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04006793 * clp->cl_rpc_users. Throw a warning if we
Jeff Layton016200c2014-07-30 08:27:21 -04006794 * start to approach INT_MAX here.
6795 */
6796 WARN_ON_ONCE(count == (INT_MAX / 2));
6797 if (count == max)
6798 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006799 }
6800 }
6801 }
Jeff Layton016200c2014-07-30 08:27:21 -04006802out:
6803 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006804
6805 return count;
6806}
6807
Jeff Layton016200c2014-07-30 08:27:21 -04006808static u64
6809nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6810 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05006811{
Jeff Layton016200c2014-07-30 08:27:21 -04006812 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006813}
6814
Jeff Layton016200c2014-07-30 08:27:21 -04006815static u64
6816nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006817{
Jeff Layton016200c2014-07-30 08:27:21 -04006818 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006819 nfsd_print_count(clp, count, "locked files");
6820 return count;
6821}
6822
Jeff Layton016200c2014-07-30 08:27:21 -04006823u64
Jeff Layton285abde2014-07-30 08:27:24 -04006824nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04006825{
6826 struct nfs4_client *clp;
6827 u64 count = 0;
6828 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6829 nfsd_net_id);
6830
6831 if (!nfsd_netns_ready(nn))
6832 return 0;
6833
6834 spin_lock(&nn->client_lock);
6835 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6836 count += nfsd_print_client_locks(clp);
6837 spin_unlock(&nn->client_lock);
6838
6839 return count;
6840}
6841
6842static void
6843nfsd_reap_locks(struct list_head *reaplist)
6844{
6845 struct nfs4_client *clp;
6846 struct nfs4_ol_stateid *stp, *next;
6847
6848 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6849 list_del_init(&stp->st_locks);
6850 clp = stp->st_stid.sc_client;
6851 nfs4_put_stid(&stp->st_stid);
6852 put_client(clp);
6853 }
6854}
6855
6856u64
Jeff Layton285abde2014-07-30 08:27:24 -04006857nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04006858{
6859 unsigned int count = 0;
6860 struct nfs4_client *clp;
6861 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6862 nfsd_net_id);
6863 LIST_HEAD(reaplist);
6864
6865 if (!nfsd_netns_ready(nn))
6866 return count;
6867
6868 spin_lock(&nn->client_lock);
6869 clp = nfsd_find_client(addr, addr_size);
6870 if (clp)
6871 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6872 spin_unlock(&nn->client_lock);
6873 nfsd_reap_locks(&reaplist);
6874 return count;
6875}
6876
6877u64
Jeff Layton285abde2014-07-30 08:27:24 -04006878nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04006879{
6880 u64 count = 0;
6881 struct nfs4_client *clp;
6882 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6883 nfsd_net_id);
6884 LIST_HEAD(reaplist);
6885
6886 if (!nfsd_netns_ready(nn))
6887 return count;
6888
6889 spin_lock(&nn->client_lock);
6890 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6891 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6892 if (max != 0 && count >= max)
6893 break;
6894 }
6895 spin_unlock(&nn->client_lock);
6896 nfsd_reap_locks(&reaplist);
6897 return count;
6898}
6899
Jeff Layton82e05ef2014-07-30 08:27:22 -04006900static u64
6901nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6902 struct list_head *collect,
6903 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006904{
6905 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04006906 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6907 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006908 u64 count = 0;
6909
Jeff Layton82e05ef2014-07-30 08:27:22 -04006910 lockdep_assert_held(&nn->client_lock);
6911
6912 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006913 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04006914 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006915 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04006916 if (collect) {
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04006917 atomic_inc(&clp->cl_rpc_users);
Jeff Layton82e05ef2014-07-30 08:27:22 -04006918 list_add(&oop->oo_perclient, collect);
6919 }
6920 }
6921 ++count;
6922 /*
6923 * Despite the fact that these functions deal with
6924 * 64-bit integers for "count", we must ensure that
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04006925 * it doesn't blow up the clp->cl_rpc_users. Throw a
Jeff Layton82e05ef2014-07-30 08:27:22 -04006926 * warning if we start to approach INT_MAX here.
6927 */
6928 WARN_ON_ONCE(count == (INT_MAX / 2));
6929 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006930 break;
6931 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04006932 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006933
6934 return count;
6935}
6936
Jeff Layton82e05ef2014-07-30 08:27:22 -04006937static u64
6938nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006939{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006940 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6941
6942 nfsd_print_count(clp, count, "openowners");
6943 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006944}
6945
Jeff Layton82e05ef2014-07-30 08:27:22 -04006946static u64
6947nfsd_collect_client_openowners(struct nfs4_client *clp,
6948 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006949{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006950 return nfsd_foreach_client_openowner(clp, max, collect,
6951 unhash_openowner_locked);
6952}
6953
6954u64
Jeff Layton285abde2014-07-30 08:27:24 -04006955nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006956{
6957 struct nfs4_client *clp;
6958 u64 count = 0;
6959 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6960 nfsd_net_id);
6961
6962 if (!nfsd_netns_ready(nn))
6963 return 0;
6964
6965 spin_lock(&nn->client_lock);
6966 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6967 count += nfsd_print_client_openowners(clp);
6968 spin_unlock(&nn->client_lock);
6969
6970 return count;
6971}
6972
6973static void
6974nfsd_reap_openowners(struct list_head *reaplist)
6975{
6976 struct nfs4_client *clp;
6977 struct nfs4_openowner *oop, *next;
6978
6979 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6980 list_del_init(&oop->oo_perclient);
6981 clp = oop->oo_owner.so_client;
6982 release_openowner(oop);
6983 put_client(clp);
6984 }
6985}
6986
6987u64
Jeff Layton285abde2014-07-30 08:27:24 -04006988nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6989 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006990{
6991 unsigned int count = 0;
6992 struct nfs4_client *clp;
6993 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6994 nfsd_net_id);
6995 LIST_HEAD(reaplist);
6996
6997 if (!nfsd_netns_ready(nn))
6998 return count;
6999
7000 spin_lock(&nn->client_lock);
7001 clp = nfsd_find_client(addr, addr_size);
7002 if (clp)
7003 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
7004 spin_unlock(&nn->client_lock);
7005 nfsd_reap_openowners(&reaplist);
7006 return count;
7007}
7008
7009u64
Jeff Layton285abde2014-07-30 08:27:24 -04007010nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04007011{
7012 u64 count = 0;
7013 struct nfs4_client *clp;
7014 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7015 nfsd_net_id);
7016 LIST_HEAD(reaplist);
7017
7018 if (!nfsd_netns_ready(nn))
7019 return count;
7020
7021 spin_lock(&nn->client_lock);
7022 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7023 count += nfsd_collect_client_openowners(clp, &reaplist,
7024 max - count);
7025 if (max != 0 && count >= max)
7026 break;
7027 }
7028 spin_unlock(&nn->client_lock);
7029 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05007030 return count;
7031}
7032
Bryan Schumaker269de302012-11-29 11:40:42 -05007033static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
7034 struct list_head *victims)
7035{
7036 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007037 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7038 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05007039 u64 count = 0;
7040
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007041 lockdep_assert_held(&nn->client_lock);
7042
7043 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05007044 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04007045 if (victims) {
7046 /*
7047 * It's not safe to mess with delegations that have a
7048 * non-zero dl_time. They might have already been broken
7049 * and could be processed by the laundromat outside of
7050 * the state_lock. Just leave them be.
7051 */
7052 if (dp->dl_time != 0)
7053 continue;
7054
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007055 atomic_inc(&clp->cl_rpc_users);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007056 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007057 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04007058 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007059 ++count;
7060 /*
7061 * Despite the fact that these functions deal with
7062 * 64-bit integers for "count", we must ensure that
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007063 * it doesn't blow up the clp->cl_rpc_users. Throw a
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007064 * warning if we start to approach INT_MAX here.
7065 */
7066 WARN_ON_ONCE(count == (INT_MAX / 2));
7067 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05007068 break;
7069 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007070 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05007071 return count;
7072}
7073
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007074static u64
7075nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05007076{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007077 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05007078
7079 nfsd_print_count(clp, count, "delegations");
7080 return count;
7081}
7082
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007083u64
Jeff Layton285abde2014-07-30 08:27:24 -04007084nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007085{
7086 struct nfs4_client *clp;
7087 u64 count = 0;
7088 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7089 nfsd_net_id);
7090
7091 if (!nfsd_netns_ready(nn))
7092 return 0;
7093
7094 spin_lock(&nn->client_lock);
7095 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7096 count += nfsd_print_client_delegations(clp);
7097 spin_unlock(&nn->client_lock);
7098
7099 return count;
7100}
7101
7102static void
7103nfsd_forget_delegations(struct list_head *reaplist)
7104{
7105 struct nfs4_client *clp;
7106 struct nfs4_delegation *dp, *next;
7107
7108 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7109 list_del_init(&dp->dl_recall_lru);
7110 clp = dp->dl_stid.sc_client;
7111 revoke_delegation(dp);
7112 put_client(clp);
7113 }
7114}
7115
7116u64
Jeff Layton285abde2014-07-30 08:27:24 -04007117nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
7118 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007119{
7120 u64 count = 0;
7121 struct nfs4_client *clp;
7122 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7123 nfsd_net_id);
7124 LIST_HEAD(reaplist);
7125
7126 if (!nfsd_netns_ready(nn))
7127 return count;
7128
7129 spin_lock(&nn->client_lock);
7130 clp = nfsd_find_client(addr, addr_size);
7131 if (clp)
7132 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7133 spin_unlock(&nn->client_lock);
7134
7135 nfsd_forget_delegations(&reaplist);
7136 return count;
7137}
7138
7139u64
Jeff Layton285abde2014-07-30 08:27:24 -04007140nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007141{
7142 u64 count = 0;
7143 struct nfs4_client *clp;
7144 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7145 nfsd_net_id);
7146 LIST_HEAD(reaplist);
7147
7148 if (!nfsd_netns_ready(nn))
7149 return count;
7150
7151 spin_lock(&nn->client_lock);
7152 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7153 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7154 if (max != 0 && count >= max)
7155 break;
7156 }
7157 spin_unlock(&nn->client_lock);
7158 nfsd_forget_delegations(&reaplist);
7159 return count;
7160}
7161
7162static void
7163nfsd_recall_delegations(struct list_head *reaplist)
7164{
7165 struct nfs4_client *clp;
7166 struct nfs4_delegation *dp, *next;
7167
7168 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7169 list_del_init(&dp->dl_recall_lru);
7170 clp = dp->dl_stid.sc_client;
7171 /*
7172 * We skipped all entries that had a zero dl_time before,
7173 * so we can now reset the dl_time back to 0. If a delegation
7174 * break comes in now, then it won't make any difference since
7175 * we're recalling it either way.
7176 */
7177 spin_lock(&state_lock);
7178 dp->dl_time = 0;
7179 spin_unlock(&state_lock);
7180 nfsd_break_one_deleg(dp);
7181 put_client(clp);
7182 }
7183}
7184
7185u64
Jeff Layton285abde2014-07-30 08:27:24 -04007186nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007187 size_t addr_size)
7188{
7189 u64 count = 0;
7190 struct nfs4_client *clp;
7191 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7192 nfsd_net_id);
7193 LIST_HEAD(reaplist);
7194
7195 if (!nfsd_netns_ready(nn))
7196 return count;
7197
7198 spin_lock(&nn->client_lock);
7199 clp = nfsd_find_client(addr, addr_size);
7200 if (clp)
7201 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7202 spin_unlock(&nn->client_lock);
7203
7204 nfsd_recall_delegations(&reaplist);
7205 return count;
7206}
7207
7208u64
Jeff Layton285abde2014-07-30 08:27:24 -04007209nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007210{
7211 u64 count = 0;
7212 struct nfs4_client *clp, *next;
7213 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7214 nfsd_net_id);
7215 LIST_HEAD(reaplist);
7216
7217 if (!nfsd_netns_ready(nn))
7218 return count;
7219
7220 spin_lock(&nn->client_lock);
7221 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7222 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7223 if (max != 0 && ++count >= max)
7224 break;
7225 }
7226 spin_unlock(&nn->client_lock);
7227 nfsd_recall_delegations(&reaplist);
7228 return count;
7229}
Bryan Schumaker65178db2011-11-01 13:35:21 -04007230#endif /* CONFIG_NFSD_FAULT_INJECTION */
7231
Meelap Shahc2f1a552007-07-17 04:04:39 -07007232/*
7233 * Since the lifetime of a delegation isn't limited to that of an open, a
7234 * client may quite reasonably hang on to a delegation as long as it has
7235 * the inode cached. This becomes an obvious problem the first time a
7236 * client's inode cache approaches the size of the server's total memory.
7237 *
7238 * For now we avoid this problem by imposing a hard limit on the number
7239 * of delegations, which varies according to the server's memory size.
7240 */
7241static void
7242set_max_delegations(void)
7243{
7244 /*
7245 * Allow at most 4 delegations per megabyte of RAM. Quick
7246 * estimates suggest that in the worst case (where every delegation
7247 * is for a different inode), a delegation could take about 1.5K,
7248 * giving a worst case usage of about 6% of memory.
7249 */
7250 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7251}
7252
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007253static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007254{
7255 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7256 int i;
7257
Kees Cook6da2ec52018-06-12 13:55:00 -07007258 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7259 sizeof(struct list_head),
7260 GFP_KERNEL);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007261 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007262 goto err;
Kees Cook6da2ec52018-06-12 13:55:00 -07007263 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7264 sizeof(struct list_head),
7265 GFP_KERNEL);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007266 if (!nn->unconf_id_hashtbl)
7267 goto err_unconf_id;
Kees Cook6da2ec52018-06-12 13:55:00 -07007268 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7269 sizeof(struct list_head),
7270 GFP_KERNEL);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007271 if (!nn->sessionid_hashtbl)
7272 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007273
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007274 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007275 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007276 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007277 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007278 for (i = 0; i < SESSION_HASH_SIZE; i++)
7279 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007280 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007281 nn->unconf_name_tree = RB_ROOT;
Vasily Averin81833de2017-11-13 07:25:40 +03007282 nn->boot_time = get_seconds();
7283 nn->grace_ended = false;
7284 nn->nfsd4_manager.block_opens = true;
7285 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03007286 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03007287 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007288 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03007289 spin_lock_init(&nn->client_lock);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04007290 spin_lock_init(&nn->s2s_cp_lock);
7291 idr_init(&nn->s2s_cp_stateids);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007292
Jeff Layton0cc11a62016-10-20 09:34:31 -04007293 spin_lock_init(&nn->blocked_locks_lock);
7294 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7295
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007296 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007297 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007298
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007299 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007300
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007301err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03007302 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007303err_unconf_id:
7304 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007305err:
7306 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007307}
7308
7309static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007310nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007311{
7312 int i;
7313 struct nfs4_client *clp = NULL;
7314 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7315
7316 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7317 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7318 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7319 destroy_client(clp);
7320 }
7321 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007322
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007323 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7324
Kinglong Mee2b905632014-03-26 22:09:30 +08007325 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7326 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7327 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7328 destroy_client(clp);
7329 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007330 }
7331
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007332 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007333 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007334 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007335 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007336}
7337
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007338int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007339nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07007340{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04007341 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007342 int ret;
7343
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007344 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007345 if (ret)
7346 return ret;
Jeff Laytond4318ac2014-09-12 16:40:21 -04007347 locks_start_grace(net, &nn->nfsd4_manager);
7348 nfsd4_client_tracking_init(net);
Scott Mayhew362063a2019-03-26 18:06:28 -04007349 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7350 goto skip_grace;
Vasily Averin7e981a82017-11-06 16:46:04 +03007351 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %x)\n",
7352 nn->nfsd4_grace, net->ns.inum);
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03007353 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007354 return 0;
Scott Mayhew362063a2019-03-26 18:06:28 -04007355
7356skip_grace:
7357 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7358 net->ns.inum);
7359 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7360 nfsd4_end_grace(nn);
7361 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007362}
7363
7364/* initialization to perform when the nfsd service is started: */
7365
7366int
7367nfs4_state_start(void)
7368{
7369 int ret;
7370
Jeff Layton51a54562015-08-20 07:17:01 -04007371 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05007372 if (laundry_wq == NULL) {
7373 ret = -ENOMEM;
Chuck Levera26dd642018-08-16 12:06:09 -04007374 goto out;
Jeff Laytona6d6b782012-03-05 11:42:36 -05007375 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007376 ret = nfsd4_create_callback_queue();
7377 if (ret)
7378 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007379
Meelap Shahc2f1a552007-07-17 04:04:39 -07007380 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007381 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007382
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007383out_free_laundry:
7384 destroy_workqueue(laundry_wq);
Chuck Levera26dd642018-08-16 12:06:09 -04007385out:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007386 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387}
7388
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007389void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007390nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007394 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007395
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007396 cancel_delayed_work_sync(&nn->laundromat_work);
7397 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05007398
Linus Torvalds1da177e2005-04-16 15:20:36 -07007399 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04007400 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007401 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007402 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007403 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007404 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405 }
Benny Halevycdc975052014-05-30 09:09:30 -04007406 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407 list_for_each_safe(pos, next, &reaplist) {
7408 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04007409 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05007410 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007411 }
7412
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007413 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007414 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007415}
7416
7417void
7418nfs4_state_shutdown(void)
7419{
NeilBrown5e8d5c22006-04-10 22:55:37 -07007420 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04007421 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007422}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007423
7424static void
7425get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7426{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007427 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
7428 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007429}
7430
7431static void
7432put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7433{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007434 if (cstate->minorversion) {
7435 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7436 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7437 }
7438}
7439
7440void
7441clear_current_stateid(struct nfsd4_compound_state *cstate)
7442{
7443 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007444}
7445
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007446/*
7447 * functions to set current state id
7448 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007449void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007450nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7451 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007452{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007453 put_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007454}
7455
7456void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007457nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7458 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007459{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007460 put_stateid(cstate, &u->open.op_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007461}
7462
7463void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007464nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7465 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007466{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007467 put_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007468}
7469
7470void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007471nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7472 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007473{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007474 put_stateid(cstate, &u->lock.lk_resp_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007475}
7476
7477/*
7478 * functions to consume current state id
7479 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007480
7481void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007482nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7483 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007484{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007485 get_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007486}
7487
7488void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007489nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7490 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007491{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007492 get_stateid(cstate, &u->delegreturn.dr_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007493}
7494
7495void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007496nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7497 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007498{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007499 get_stateid(cstate, &u->free_stateid.fr_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007500}
7501
7502void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007503nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7504 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007505{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007506 get_stateid(cstate, &u->setattr.sa_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007507}
7508
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007509void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007510nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7511 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007512{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007513 get_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007514}
7515
7516void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007517nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7518 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007519{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007520 get_stateid(cstate, &u->locku.lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007521}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007522
7523void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007524nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7525 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007526{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007527 get_stateid(cstate, &u->read.rd_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007528}
7529
7530void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007531nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7532 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007533{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007534 get_stateid(cstate, &u->write.wr_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007535}