blob: 1e078fb8d6b56b03cd11a33ebba12f50c9913251 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Chuck Lever7df302f2012-05-29 13:56:37 -040041#include <linux/ratelimit.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050042#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton59766872013-02-04 12:50:00 -050043#include <linux/sunrpc/addr.h>
Daniel Borkmann87545892014-12-10 16:33:11 +010044#include <linux/jhash.h>
J. Bruce Fields169319f2019-06-19 12:39:46 -040045#include <linux/string_helpers.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020046#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040047#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050048#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040049#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040051#include "netns.h"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020052#include "pnfs.h"
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define NFSDDBG_FACILITY NFSDDBG_PROC
55
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050056#define all_ones {{~0,~0},~0}
57static const stateid_t one_stateid = {
58 .si_generation = ~0,
59 .si_opaque = all_ones,
60};
61static const stateid_t zero_stateid = {
62 /* all fields zero */
63};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010064static const stateid_t currentstateid = {
65 .si_generation = 1,
66};
Trond Myklebustfb500a72017-11-03 08:00:12 -040067static const stateid_t close_stateid = {
68 .si_generation = 0xffffffffU,
69};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050070
Andy Adamsonec6b5d72009-04-03 08:28:28 +030071static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070072
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050073#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
74#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010075#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Andrew Elbleae254dac2017-11-09 13:41:10 -050076#define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040079static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040080static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Scott Mayhew362063a2019-03-26 18:06:28 -040081void nfsd4_end_grace(struct nfsd_net *nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
J. Bruce Fields8b671b82009-02-22 14:51:34 -080083/* Locking: */
84
J. Bruce Fields8b671b82009-02-22 14:51:34 -080085/*
86 * Currently used for the del_recall_lru and file hash table. In an
87 * effort to decrease the scope of the client_mutex, this spinlock may
88 * eventually cover more:
89 */
Benny Halevycdc975052014-05-30 09:09:30 -040090static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080091
Andrew Elble4f34bd02017-11-08 17:29:51 -050092enum nfsd4_st_mutex_lock_subclass {
93 OPEN_STATEID_MUTEX = 0,
94 LOCK_STATEID_MUTEX = 1,
95};
96
Jeff Laytonb401be222014-07-29 21:34:33 -040097/*
98 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
99 * the refcount on the open stateid to drop.
100 */
101static DECLARE_WAIT_QUEUE_HEAD(close_wq);
102
Jeff Layton9258a2d2018-03-16 09:47:22 -0400103static struct kmem_cache *client_slab;
Christoph Hellwigabf11352014-05-21 07:43:03 -0700104static struct kmem_cache *openowner_slab;
105static struct kmem_cache *lockowner_slab;
106static struct kmem_cache *file_slab;
107static struct kmem_cache *stateid_slab;
108static struct kmem_cache *deleg_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +0200109static struct kmem_cache *odstate_slab;
NeilBrowne60d4392005-06-23 22:03:01 -0700110
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400111static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800112
Julia Lawallc4cb8972015-11-21 22:57:39 +0100113static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
Jeff Layton76d348f2016-09-16 16:28:24 -0400114static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200115
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400116static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800117{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400118 return ses->se_flags & NFS4_SESSION_DEAD;
119}
120
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400121static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
122{
123 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400124 return nfserr_jukebox;
125 ses->se_flags |= NFS4_SESSION_DEAD;
126 return nfs_ok;
127}
128
J. Bruce Fields221a6872013-04-01 22:23:49 -0400129static bool is_client_expired(struct nfs4_client *clp)
130{
131 return clp->cl_time == 0;
132}
133
J. Bruce Fields221a6872013-04-01 22:23:49 -0400134static __be32 get_client_locked(struct nfs4_client *clp)
135{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400136 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
137
138 lockdep_assert_held(&nn->client_lock);
139
J. Bruce Fields221a6872013-04-01 22:23:49 -0400140 if (is_client_expired(clp))
141 return nfserr_expired;
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400142 atomic_inc(&clp->cl_rpc_users);
J. Bruce Fields221a6872013-04-01 22:23:49 -0400143 return nfs_ok;
144}
145
146/* must be called under the client_lock */
147static inline void
148renew_client_locked(struct nfs4_client *clp)
149{
150 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
151
152 if (is_client_expired(clp)) {
153 WARN_ON(1);
154 printk("%s: client (clientid %08x/%08x) already expired\n",
155 __func__,
156 clp->cl_clientid.cl_boot,
157 clp->cl_clientid.cl_id);
158 return;
159 }
160
161 dprintk("renewing client (clientid %08x/%08x)\n",
162 clp->cl_clientid.cl_boot,
163 clp->cl_clientid.cl_id);
164 list_move_tail(&clp->cl_lru, &nn->client_lru);
165 clp->cl_time = get_seconds();
166}
167
Fengguang Wuba138432013-04-16 22:14:15 -0400168static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400169{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400170 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
171
172 lockdep_assert_held(&nn->client_lock);
173
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400174 if (!atomic_dec_and_test(&clp->cl_rpc_users))
J. Bruce Fields221a6872013-04-01 22:23:49 -0400175 return;
176 if (!is_client_expired(clp))
177 renew_client_locked(clp);
178}
179
Jeff Layton4b24ca72014-06-30 11:48:44 -0400180static void put_client_renew(struct nfs4_client *clp)
181{
182 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
183
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400184 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
Jeff Laytond6c249b2014-07-08 14:02:50 -0400185 return;
186 if (!is_client_expired(clp))
187 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400188 spin_unlock(&nn->client_lock);
189}
190
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400191static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
192{
193 __be32 status;
194
195 if (is_session_dead(ses))
196 return nfserr_badsession;
197 status = get_client_locked(ses->se_client);
198 if (status)
199 return status;
200 atomic_inc(&ses->se_ref);
201 return nfs_ok;
202}
203
204static void nfsd4_put_session_locked(struct nfsd4_session *ses)
205{
206 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400207 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
208
209 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400210
211 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
212 free_session(ses);
213 put_client_renew_locked(clp);
214}
215
216static void nfsd4_put_session(struct nfsd4_session *ses)
217{
218 struct nfs4_client *clp = ses->se_client;
219 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
220
221 spin_lock(&nn->client_lock);
222 nfsd4_put_session_locked(ses);
223 spin_unlock(&nn->client_lock);
224}
225
Jeff Layton76d348f2016-09-16 16:28:24 -0400226static struct nfsd4_blocked_lock *
227find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
228 struct nfsd_net *nn)
229{
230 struct nfsd4_blocked_lock *cur, *found = NULL;
231
Jeff Layton0cc11a62016-10-20 09:34:31 -0400232 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400233 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
234 if (fh_match(fh, &cur->nbl_fh)) {
235 list_del_init(&cur->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -0400236 list_del_init(&cur->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400237 found = cur;
238 break;
239 }
240 }
Jeff Layton0cc11a62016-10-20 09:34:31 -0400241 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400242 if (found)
NeilBrowncb03f942018-11-30 10:04:08 +1100243 locks_delete_block(&found->nbl_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400244 return found;
245}
246
247static struct nfsd4_blocked_lock *
248find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
249 struct nfsd_net *nn)
250{
251 struct nfsd4_blocked_lock *nbl;
252
253 nbl = find_blocked_lock(lo, fh, nn);
254 if (!nbl) {
255 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
256 if (nbl) {
257 fh_copy_shallow(&nbl->nbl_fh, fh);
258 locks_init_lock(&nbl->nbl_lock);
259 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
260 &nfsd4_cb_notify_lock_ops,
261 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
262 }
263 }
264 return nbl;
265}
266
267static void
268free_blocked_lock(struct nfsd4_blocked_lock *nbl)
269{
Jeff Layton6aaafc42019-04-22 12:34:23 -0400270 locks_delete_block(&nbl->nbl_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400271 locks_release_private(&nbl->nbl_lock);
272 kfree(nbl);
273}
274
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400275static void
276remove_blocked_locks(struct nfs4_lockowner *lo)
277{
278 struct nfs4_client *clp = lo->lo_owner.so_client;
279 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
280 struct nfsd4_blocked_lock *nbl;
281 LIST_HEAD(reaplist);
282
283 /* Dequeue all blocked locks */
284 spin_lock(&nn->blocked_locks_lock);
285 while (!list_empty(&lo->lo_blocked)) {
286 nbl = list_first_entry(&lo->lo_blocked,
287 struct nfsd4_blocked_lock,
288 nbl_list);
289 list_del_init(&nbl->nbl_list);
290 list_move(&nbl->nbl_lru, &reaplist);
291 }
292 spin_unlock(&nn->blocked_locks_lock);
293
294 /* Now free them */
295 while (!list_empty(&reaplist)) {
296 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
297 nbl_lru);
298 list_del_init(&nbl->nbl_lru);
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400299 free_blocked_lock(nbl);
300 }
301}
302
Jeff Laytonf4564582019-04-22 12:34:24 -0400303static void
304nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
305{
306 struct nfsd4_blocked_lock *nbl = container_of(cb,
307 struct nfsd4_blocked_lock, nbl_cb);
308 locks_delete_block(&nbl->nbl_lock);
309}
310
Jeff Layton76d348f2016-09-16 16:28:24 -0400311static int
312nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
313{
314 /*
315 * Since this is just an optimization, we don't try very hard if it
316 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
317 * just quit trying on anything else.
318 */
319 switch (task->tk_status) {
320 case -NFS4ERR_DELAY:
321 rpc_delay(task, 1 * HZ);
322 return 0;
323 default:
324 return 1;
325 }
326}
327
328static void
329nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
330{
331 struct nfsd4_blocked_lock *nbl = container_of(cb,
332 struct nfsd4_blocked_lock, nbl_cb);
333
334 free_blocked_lock(nbl);
335}
336
337static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
Jeff Laytonf4564582019-04-22 12:34:24 -0400338 .prepare = nfsd4_cb_notify_lock_prepare,
Jeff Layton76d348f2016-09-16 16:28:24 -0400339 .done = nfsd4_cb_notify_lock_done,
340 .release = nfsd4_cb_notify_lock_release,
341};
342
Kinglong Meeb5971af2014-08-22 10:18:43 -0400343static inline struct nfs4_stateowner *
344nfs4_get_stateowner(struct nfs4_stateowner *sop)
345{
346 atomic_inc(&sop->so_count);
347 return sop;
348}
349
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400350static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400351same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400352{
353 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400354 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400355}
356
357static struct nfs4_openowner *
358find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400359 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400360{
361 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400362
Trond Myklebustd4f04892014-07-29 21:34:36 -0400363 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400364
Trond Myklebustd4f04892014-07-29 21:34:36 -0400365 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
366 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400367 if (!so->so_is_open_owner)
368 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400369 if (same_owner_str(so, &open->op_owner))
370 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400371 }
372 return NULL;
373}
374
375static struct nfs4_openowner *
376find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400377 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400378{
379 struct nfs4_openowner *oo;
380
Trond Myklebustd4f04892014-07-29 21:34:36 -0400381 spin_lock(&clp->cl_lock);
382 oo = find_openstateowner_str_locked(hashval, open, clp);
383 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400384 return oo;
385}
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387static inline u32
388opaque_hashval(const void *ptr, int nbytes)
389{
390 unsigned char *cptr = (unsigned char *) ptr;
391
392 u32 x = 0;
393 while (nbytes--) {
394 x *= 37;
395 x += *cptr++;
396 }
397 return x;
398}
399
Jeff Layton5b095e92014-10-23 08:01:02 -0400400static void nfsd4_free_file_rcu(struct rcu_head *rcu)
J. Bruce Fields32513b42011-10-13 16:00:16 -0400401{
Jeff Layton5b095e92014-10-23 08:01:02 -0400402 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
403
404 kmem_cache_free(file_slab, fp);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400405}
406
Christoph Hellwige6ba76e2014-08-14 08:50:16 +0200407void
NeilBrown13cd2182005-06-23 22:03:10 -0700408put_nfs4_file(struct nfs4_file *fi)
409{
Jeff Layton02e12152014-07-16 10:31:57 -0400410 might_lock(&state_lock);
411
Elena Reshetova818a34e2017-10-20 12:53:30 +0300412 if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton5b095e92014-10-23 08:01:02 -0400413 hlist_del_rcu(&fi->fi_hash);
Benny Halevycdc975052014-05-30 09:09:30 -0400414 spin_unlock(&state_lock);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200415 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
Jeff Layton5b095e92014-10-23 08:01:02 -0400416 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
417 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800418 }
NeilBrown13cd2182005-06-23 22:03:10 -0700419}
420
Trond Myklebustde186432014-07-10 14:07:26 -0400421static struct file *
422__nfs4_get_fd(struct nfs4_file *f, int oflag)
423{
424 if (f->fi_fds[oflag])
425 return get_file(f->fi_fds[oflag]);
426 return NULL;
427}
428
429static struct file *
430find_writeable_file_locked(struct nfs4_file *f)
431{
432 struct file *ret;
433
434 lockdep_assert_held(&f->fi_lock);
435
436 ret = __nfs4_get_fd(f, O_WRONLY);
437 if (!ret)
438 ret = __nfs4_get_fd(f, O_RDWR);
439 return ret;
440}
441
442static struct file *
443find_writeable_file(struct nfs4_file *f)
444{
445 struct file *ret;
446
447 spin_lock(&f->fi_lock);
448 ret = find_writeable_file_locked(f);
449 spin_unlock(&f->fi_lock);
450
451 return ret;
452}
453
454static struct file *find_readable_file_locked(struct nfs4_file *f)
455{
456 struct file *ret;
457
458 lockdep_assert_held(&f->fi_lock);
459
460 ret = __nfs4_get_fd(f, O_RDONLY);
461 if (!ret)
462 ret = __nfs4_get_fd(f, O_RDWR);
463 return ret;
464}
465
466static struct file *
467find_readable_file(struct nfs4_file *f)
468{
469 struct file *ret;
470
471 spin_lock(&f->fi_lock);
472 ret = find_readable_file_locked(f);
473 spin_unlock(&f->fi_lock);
474
475 return ret;
476}
477
Christoph Hellwig4d227fc2014-08-17 07:40:00 -0500478struct file *
Trond Myklebustde186432014-07-10 14:07:26 -0400479find_any_file(struct nfs4_file *f)
480{
481 struct file *ret;
482
483 spin_lock(&f->fi_lock);
484 ret = __nfs4_get_fd(f, O_RDWR);
485 if (!ret) {
486 ret = __nfs4_get_fd(f, O_WRONLY);
487 if (!ret)
488 ret = __nfs4_get_fd(f, O_RDONLY);
489 }
490 spin_unlock(&f->fi_lock);
491 return ret;
492}
493
Trond Myklebust02a35082014-07-25 07:34:22 -0400494static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800495unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700496
497/*
498 * Open owner state (share locks)
499 */
500
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500501/* hash tables for lock and open owners */
502#define OWNER_HASH_BITS 8
503#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
504#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700505
Trond Myklebustd4f04892014-07-29 21:34:36 -0400506static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400507{
508 unsigned int ret;
509
510 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500511 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400512}
NeilBrownef0f3392006-04-10 22:55:41 -0700513
NeilBrownef0f3392006-04-10 22:55:41 -0700514/* hash table for nfs4_file */
515#define FILE_HASH_BITS 8
516#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800517
Trond Myklebustca943212014-07-23 16:17:39 -0400518static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400519{
Trond Myklebustca943212014-07-23 16:17:39 -0400520 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
521}
522
523static unsigned int file_hashval(struct knfsd_fh *fh)
524{
525 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
526}
527
Jeff Layton89876f82013-04-02 09:01:59 -0400528static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700529
Jeff Layton12659652014-07-10 14:07:28 -0400530static void
531__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400532{
Jeff Layton7214e862014-07-10 14:07:33 -0400533 lockdep_assert_held(&fp->fi_lock);
534
Jeff Layton12659652014-07-10 14:07:28 -0400535 if (access & NFS4_SHARE_ACCESS_WRITE)
536 atomic_inc(&fp->fi_access[O_WRONLY]);
537 if (access & NFS4_SHARE_ACCESS_READ)
538 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400539}
540
Jeff Layton12659652014-07-10 14:07:28 -0400541static __be32
542nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400543{
Jeff Layton7214e862014-07-10 14:07:33 -0400544 lockdep_assert_held(&fp->fi_lock);
545
Jeff Layton12659652014-07-10 14:07:28 -0400546 /* Does this access mode make sense? */
547 if (access & ~NFS4_SHARE_ACCESS_BOTH)
548 return nfserr_inval;
549
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400550 /* Does it conflict with a deny mode already set? */
551 if ((access & fp->fi_share_deny) != 0)
552 return nfserr_share_denied;
553
Jeff Layton12659652014-07-10 14:07:28 -0400554 __nfs4_file_get_access(fp, access);
555 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400556}
557
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400558static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
559{
560 /* Common case is that there is no deny mode. */
561 if (deny) {
562 /* Does this deny mode make sense? */
563 if (deny & ~NFS4_SHARE_DENY_BOTH)
564 return nfserr_inval;
565
566 if ((deny & NFS4_SHARE_DENY_READ) &&
567 atomic_read(&fp->fi_access[O_RDONLY]))
568 return nfserr_share_denied;
569
570 if ((deny & NFS4_SHARE_DENY_WRITE) &&
571 atomic_read(&fp->fi_access[O_WRONLY]))
572 return nfserr_share_denied;
573 }
574 return nfs_ok;
575}
576
J. Bruce Fields998db522010-08-07 09:21:41 -0400577static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400578{
Trond Myklebustde186432014-07-10 14:07:26 -0400579 might_lock(&fp->fi_lock);
580
581 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
582 struct file *f1 = NULL;
583 struct file *f2 = NULL;
584
Jeff Layton6d338b52014-07-10 14:07:29 -0400585 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400586 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400587 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400588 spin_unlock(&fp->fi_lock);
589 if (f1)
590 fput(f1);
591 if (f2)
592 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400593 }
594}
595
Jeff Layton12659652014-07-10 14:07:28 -0400596static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400597{
Jeff Layton12659652014-07-10 14:07:28 -0400598 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
599
600 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400601 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400602 if (access & NFS4_SHARE_ACCESS_READ)
603 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400604}
605
Sachin Bhamare8287f002015-04-27 14:50:14 +0200606/*
607 * Allocate a new open/delegation state counter. This is needed for
608 * pNFS for proper return on close semantics.
609 *
610 * Note that we only allocate it for pNFS-enabled exports, otherwise
611 * all pointers to struct nfs4_clnt_odstate are always NULL.
612 */
613static struct nfs4_clnt_odstate *
614alloc_clnt_odstate(struct nfs4_client *clp)
615{
616 struct nfs4_clnt_odstate *co;
617
618 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
619 if (co) {
620 co->co_client = clp;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300621 refcount_set(&co->co_odcount, 1);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200622 }
623 return co;
624}
625
626static void
627hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
628{
629 struct nfs4_file *fp = co->co_file;
630
631 lockdep_assert_held(&fp->fi_lock);
632 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
633}
634
635static inline void
636get_clnt_odstate(struct nfs4_clnt_odstate *co)
637{
638 if (co)
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300639 refcount_inc(&co->co_odcount);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200640}
641
642static void
643put_clnt_odstate(struct nfs4_clnt_odstate *co)
644{
645 struct nfs4_file *fp;
646
647 if (!co)
648 return;
649
650 fp = co->co_file;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300651 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
Sachin Bhamare8287f002015-04-27 14:50:14 +0200652 list_del(&co->co_perfile);
653 spin_unlock(&fp->fi_lock);
654
655 nfsd4_return_all_file_layouts(co->co_client, fp);
656 kmem_cache_free(odstate_slab, co);
657 }
658}
659
660static struct nfs4_clnt_odstate *
661find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
662{
663 struct nfs4_clnt_odstate *co;
664 struct nfs4_client *cl;
665
666 if (!new)
667 return NULL;
668
669 cl = new->co_client;
670
671 spin_lock(&fp->fi_lock);
672 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
673 if (co->co_client == cl) {
674 get_clnt_odstate(co);
675 goto out;
676 }
677 }
678 co = new;
679 co->co_file = fp;
680 hash_clnt_odstate_locked(new);
681out:
682 spin_unlock(&fp->fi_lock);
683 return co;
684}
685
Kinglong Meed19fb702017-01-18 19:04:42 +0800686struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
687 void (*sc_free)(struct nfs4_stid *))
J. Bruce Fields996e0932011-10-17 11:14:48 -0400688{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500689 struct nfs4_stid *stid;
690 int new_id;
691
Trond Myklebustf8338832014-07-25 07:34:19 -0400692 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500693 if (!stid)
694 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400695
Jeff Layton4770d722014-07-29 21:34:10 -0400696 idr_preload(GFP_KERNEL);
697 spin_lock(&cl->cl_lock);
J. Bruce Fields78599c42019-04-22 15:26:23 -0400698 /* Reserving 0 for start of file in nfsdfs "states" file: */
699 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
Jeff Layton4770d722014-07-29 21:34:10 -0400700 spin_unlock(&cl->cl_lock);
701 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700702 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500703 goto out_free;
Kinglong Meed19fb702017-01-18 19:04:42 +0800704
705 stid->sc_free = sc_free;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500706 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500707 stid->sc_stateid.si_opaque.so_id = new_id;
708 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
709 /* Will be incremented before return to client: */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300710 refcount_set(&stid->sc_count, 1);
Jeff Layton9767feb2015-10-01 09:05:50 -0400711 spin_lock_init(&stid->sc_lock);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500712
J. Bruce Fields996e0932011-10-17 11:14:48 -0400713 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500714 * It shouldn't be a problem to reuse an opaque stateid value.
715 * I don't think it is for 4.1. But with 4.0 I worry that, for
716 * example, a stray write retransmission could be accepted by
717 * the server when it should have been rejected. Therefore,
718 * adopt a trick from the sctp code to attempt to maximize the
719 * amount of time until an id is reused, by ensuring they always
720 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400721 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500722 return stid;
723out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800724 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500725 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400726}
727
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400728/*
729 * Create a unique stateid_t to represent each COPY.
730 */
731int nfs4_init_cp_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
732{
733 int new_id;
734
735 idr_preload(GFP_KERNEL);
736 spin_lock(&nn->s2s_cp_lock);
737 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, copy, 0, 0, GFP_NOWAIT);
738 spin_unlock(&nn->s2s_cp_lock);
739 idr_preload_end();
740 if (new_id < 0)
741 return 0;
742 copy->cp_stateid.si_opaque.so_id = new_id;
743 copy->cp_stateid.si_opaque.so_clid.cl_boot = nn->boot_time;
744 copy->cp_stateid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
745 return 1;
746}
747
748void nfs4_free_cp_state(struct nfsd4_copy *copy)
749{
750 struct nfsd_net *nn;
751
752 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
753 spin_lock(&nn->s2s_cp_lock);
754 idr_remove(&nn->s2s_cp_stateids, copy->cp_stateid.si_opaque.so_id);
755 spin_unlock(&nn->s2s_cp_lock);
756}
757
Jeff Laytonb49e0842014-07-29 21:34:11 -0400758static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400759{
Trond Myklebust60116952014-07-29 21:34:06 -0400760 struct nfs4_stid *stid;
Trond Myklebust60116952014-07-29 21:34:06 -0400761
Kinglong Meed19fb702017-01-18 19:04:42 +0800762 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
Trond Myklebust60116952014-07-29 21:34:06 -0400763 if (!stid)
764 return NULL;
765
Kinglong Meed19fb702017-01-18 19:04:42 +0800766 return openlockstateid(stid);
Trond Myklebust60116952014-07-29 21:34:06 -0400767}
768
769static void nfs4_free_deleg(struct nfs4_stid *stid)
770{
Trond Myklebust60116952014-07-29 21:34:06 -0400771 kmem_cache_free(deleg_slab, stid);
772 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400773}
774
NeilBrown6282cd52014-06-04 17:39:26 +1000775/*
776 * When we recall a delegation, we should be careful not to hand it
777 * out again straight away.
778 * To ensure this we keep a pair of bloom filters ('new' and 'old')
779 * in which the filehandles of recalled delegations are "stored".
780 * If a filehandle appear in either filter, a delegation is blocked.
781 * When a delegation is recalled, the filehandle is stored in the "new"
782 * filter.
783 * Every 30 seconds we swap the filters and clear the "new" one,
784 * unless both are empty of course.
785 *
786 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
787 * low 3 bytes as hash-table indices.
788 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400789 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000790 * is used to manage concurrent access. Testing does not need the lock
791 * except when swapping the two filters.
792 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400793static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000794static struct bloom_pair {
795 int entries, old_entries;
796 time_t swap_time;
797 int new; /* index into 'set' */
798 DECLARE_BITMAP(set[2], 256);
799} blocked_delegations;
800
801static int delegation_blocked(struct knfsd_fh *fh)
802{
803 u32 hash;
804 struct bloom_pair *bd = &blocked_delegations;
805
806 if (bd->entries == 0)
807 return 0;
808 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400809 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000810 if (seconds_since_boot() - bd->swap_time > 30) {
811 bd->entries -= bd->old_entries;
812 bd->old_entries = bd->entries;
813 memset(bd->set[bd->new], 0,
814 sizeof(bd->set[0]));
815 bd->new = 1-bd->new;
816 bd->swap_time = seconds_since_boot();
817 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400818 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000819 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100820 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000821 if (test_bit(hash&255, bd->set[0]) &&
822 test_bit((hash>>8)&255, bd->set[0]) &&
823 test_bit((hash>>16)&255, bd->set[0]))
824 return 1;
825
826 if (test_bit(hash&255, bd->set[1]) &&
827 test_bit((hash>>8)&255, bd->set[1]) &&
828 test_bit((hash>>16)&255, bd->set[1]))
829 return 1;
830
831 return 0;
832}
833
834static void block_delegations(struct knfsd_fh *fh)
835{
836 u32 hash;
837 struct bloom_pair *bd = &blocked_delegations;
838
Daniel Borkmann87545892014-12-10 16:33:11 +0100839 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000840
Jeff Laytonf54fe962014-07-25 07:34:26 -0400841 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000842 __set_bit(hash&255, bd->set[bd->new]);
843 __set_bit((hash>>8)&255, bd->set[bd->new]);
844 __set_bit((hash>>16)&255, bd->set[bd->new]);
845 if (bd->entries == 0)
846 bd->swap_time = seconds_since_boot();
847 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400848 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000849}
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851static struct nfs4_delegation *
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500852alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
853 struct svc_fh *current_fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +0200854 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400857 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400860 n = atomic_long_inc_return(&num_delegations);
861 if (n < 0 || n > max_delegations)
862 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000863 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400864 goto out_dec;
Kinglong Meed19fb702017-01-18 19:04:42 +0800865 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700866 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400867 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400868
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400869 /*
870 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400871 * meaning of seqid 0 isn't meaningful, really, but let's avoid
872 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400873 */
874 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700875 INIT_LIST_HEAD(&dp->dl_perfile);
876 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200878 dp->dl_clnt_odstate = odstate;
879 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400880 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200881 dp->dl_retries = 1;
882 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200883 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500884 get_nfs4_file(fp);
885 dp->dl_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400887out_dec:
888 atomic_long_dec(&num_delegations);
889 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
892void
Trond Myklebust60116952014-07-29 21:34:06 -0400893nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Trond Myklebust11b91642014-07-29 21:34:08 -0400895 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400896 struct nfs4_client *clp = s->sc_client;
897
Jeff Layton4770d722014-07-29 21:34:10 -0400898 might_lock(&clp->cl_lock);
899
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300900 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
Jeff Laytonb401be222014-07-29 21:34:33 -0400901 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400902 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400903 }
Trond Myklebust60116952014-07-29 21:34:06 -0400904 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400905 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400906 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400907 if (fp)
908 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
Jeff Layton9767feb2015-10-01 09:05:50 -0400911void
912nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
913{
914 stateid_t *src = &stid->sc_stateid;
915
916 spin_lock(&stid->sc_lock);
917 if (unlikely(++src->si_generation == 0))
918 src->si_generation = 1;
919 memcpy(dst, src, sizeof(*dst));
920 spin_unlock(&stid->sc_lock);
921}
922
J. Bruce Fields353601e2018-02-16 14:29:42 -0500923static void put_deleg_file(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Jeff Layton6bcc0342014-08-09 10:22:40 -0400925 struct file *filp = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -0500926
927 spin_lock(&fp->fi_lock);
928 if (--fp->fi_delegees == 0)
929 swap(filp, fp->fi_deleg_file);
930 spin_unlock(&fp->fi_lock);
931
932 if (filp)
933 fput(filp);
934}
935
936static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
937{
J. Bruce Fieldscba7b3d2018-02-15 15:50:49 -0500938 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fields353601e2018-02-16 14:29:42 -0500939 struct file *filp = fp->fi_deleg_file;
Jeff Layton417c6622014-07-21 09:34:57 -0400940
J. Bruce Fieldsb8232d32018-02-15 15:29:15 -0500941 WARN_ON_ONCE(!fp->fi_delegees);
942
J. Bruce Fields353601e2018-02-16 14:29:42 -0500943 vfs_setlease(filp, F_UNLCK, NULL, (void **)&dp);
944 put_deleg_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
J. Bruce Fields0af6e692018-02-21 15:11:03 -0500947static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
948{
949 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -0500950 nfs4_unlock_deleg_lease(dp);
J. Bruce Fields0af6e692018-02-21 15:11:03 -0500951 nfs4_put_stid(&dp->dl_stid);
952}
953
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200954void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400955{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500956 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400957}
958
Andrew Elble34ed9872015-10-15 12:07:28 -0400959/**
J. Bruce Fields68b18f52018-02-19 11:38:33 -0500960 * nfs4_delegation_exists - Discover if this delegation already exists
Andrew Elble34ed9872015-10-15 12:07:28 -0400961 * @clp: a pointer to the nfs4_client we're granting a delegation to
962 * @fp: a pointer to the nfs4_file we're granting a delegation on
963 *
964 * Return:
J. Bruce Fields68b18f52018-02-19 11:38:33 -0500965 * On success: true iff an existing delegation is found
Andrew Elble34ed9872015-10-15 12:07:28 -0400966 */
967
J. Bruce Fields68b18f52018-02-19 11:38:33 -0500968static bool
969nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
Benny Halevy931ee562014-05-30 09:09:27 -0400970{
Andrew Elble34ed9872015-10-15 12:07:28 -0400971 struct nfs4_delegation *searchdp = NULL;
972 struct nfs4_client *searchclp = NULL;
973
Benny Halevycdc975052014-05-30 09:09:30 -0400974 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400975 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400976
Andrew Elble34ed9872015-10-15 12:07:28 -0400977 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
978 searchclp = searchdp->dl_stid.sc_client;
979 if (clp == searchclp) {
Fengguang Wu51d87bc2018-03-22 13:37:20 +0800980 return true;
Andrew Elble34ed9872015-10-15 12:07:28 -0400981 }
982 }
Fengguang Wu51d87bc2018-03-22 13:37:20 +0800983 return false;
Andrew Elble34ed9872015-10-15 12:07:28 -0400984}
985
986/**
987 * hash_delegation_locked - Add a delegation to the appropriate lists
988 * @dp: a pointer to the nfs4_delegation we are adding.
989 * @fp: a pointer to the nfs4_file we're granting a delegation on
990 *
991 * Return:
992 * On success: NULL if the delegation was successfully hashed.
993 *
994 * On error: -EAGAIN if one was previously granted to this
995 * nfs4_client for this nfs4_file. Delegation is not hashed.
996 *
997 */
998
999static int
1000hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1001{
Andrew Elble34ed9872015-10-15 12:07:28 -04001002 struct nfs4_client *clp = dp->dl_stid.sc_client;
1003
1004 lockdep_assert_held(&state_lock);
1005 lockdep_assert_held(&fp->fi_lock);
1006
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001007 if (nfs4_delegation_exists(clp, fp))
1008 return -EAGAIN;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001009 refcount_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -04001010 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -04001011 list_add(&dp->dl_perfile, &fp->fi_delegations);
Andrew Elble34ed9872015-10-15 12:07:28 -04001012 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1013 return 0;
Benny Halevy931ee562014-05-30 09:09:27 -04001014}
1015
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001016static bool
Jeff Layton42690672014-07-25 07:34:20 -04001017unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Trond Myklebust11b91642014-07-29 21:34:08 -04001019 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -04001020
Jeff Layton42690672014-07-25 07:34:20 -04001021 lockdep_assert_held(&state_lock);
1022
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001023 if (list_empty(&dp->dl_perfile))
1024 return false;
1025
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04001026 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -04001027 /* Ensure that deleg break won't try to requeue it */
1028 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -04001029 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001030 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -04001032 list_del_init(&dp->dl_perfile);
1033 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001034 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001035}
1036
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001037static void destroy_delegation(struct nfs4_delegation *dp)
1038{
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001039 bool unhashed;
1040
Jeff Layton42690672014-07-25 07:34:20 -04001041 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001042 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -04001043 spin_unlock(&state_lock);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001044 if (unhashed)
1045 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001046}
1047
1048static void revoke_delegation(struct nfs4_delegation *dp)
1049{
1050 struct nfs4_client *clp = dp->dl_stid.sc_client;
1051
Jeff Layton2d4a5322014-07-25 07:34:21 -04001052 WARN_ON(!list_empty(&dp->dl_recall_lru));
1053
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001054 if (clp->cl_minorversion) {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001055 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001056 refcount_inc(&dp->dl_stid.sc_count);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001057 spin_lock(&clp->cl_lock);
1058 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1059 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001060 }
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001061 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001062}
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064/*
1065 * SETCLIENTID state
1066 */
1067
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001068static unsigned int clientid_hashval(u32 id)
1069{
1070 return id & CLIENT_HASH_MASK;
1071}
1072
Scott Mayhew6b189102019-03-26 18:06:26 -04001073static unsigned int clientstr_hashval(struct xdr_netobj name)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001074{
Scott Mayhew6b189102019-03-26 18:06:26 -04001075 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001076}
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001079 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1080 * st_{access,deny}_bmap field of the stateid, in order to track not
1081 * only what share bits are currently in force, but also what
1082 * combinations of share bits previous opens have used. This allows us
1083 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1084 * return an error if the client attempt to downgrade to a combination
1085 * of share bits not explicable by closing some of its previous opens.
1086 *
1087 * XXX: This enforcement is actually incomplete, since we don't keep
1088 * track of access/deny bit combinations; so, e.g., we allow:
1089 *
1090 * OPEN allow read, deny write
1091 * OPEN allow both, deny none
1092 * DOWNGRADE allow read, deny none
1093 *
1094 * which we should reject.
1095 */
Jeff Layton5ae037e2012-05-11 09:45:11 -04001096static unsigned int
1097bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001098 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -04001099 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001100
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001101 for (i = 1; i < 4; i++) {
1102 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -04001103 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001104 }
Jeff Layton5ae037e2012-05-11 09:45:11 -04001105 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001106}
1107
Jeff Layton82c5ff12012-05-11 09:45:13 -04001108/* set share access for a given stateid */
1109static inline void
1110set_access(u32 access, struct nfs4_ol_stateid *stp)
1111{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001112 unsigned char mask = 1 << access;
1113
1114 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1115 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001116}
1117
1118/* clear share access for a given stateid */
1119static inline void
1120clear_access(u32 access, struct nfs4_ol_stateid *stp)
1121{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001122 unsigned char mask = 1 << access;
1123
1124 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1125 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001126}
1127
1128/* test whether a given stateid has access */
1129static inline bool
1130test_access(u32 access, struct nfs4_ol_stateid *stp)
1131{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001132 unsigned char mask = 1 << access;
1133
1134 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001135}
1136
Jeff Laytonce0fc432012-05-11 09:45:14 -04001137/* set share deny for a given stateid */
1138static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001139set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001140{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001141 unsigned char mask = 1 << deny;
1142
1143 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1144 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001145}
1146
1147/* clear share deny for a given stateid */
1148static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001149clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001150{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001151 unsigned char mask = 1 << deny;
1152
1153 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1154 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001155}
1156
1157/* test whether a given stateid is denying specific access */
1158static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -04001159test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001160{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001161 unsigned char mask = 1 << deny;
1162
1163 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001164}
1165
1166static int nfs4_access_to_omode(u32 access)
1167{
J. Bruce Fields8f34a432010-09-02 15:23:16 -04001168 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001169 case NFS4_SHARE_ACCESS_READ:
1170 return O_RDONLY;
1171 case NFS4_SHARE_ACCESS_WRITE:
1172 return O_WRONLY;
1173 case NFS4_SHARE_ACCESS_BOTH:
1174 return O_RDWR;
1175 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001176 WARN_ON_ONCE(1);
1177 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001178}
1179
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001180/*
1181 * A stateid that had a deny mode associated with it is being released
1182 * or downgraded. Recalculate the deny mode on the file.
1183 */
1184static void
1185recalculate_deny_mode(struct nfs4_file *fp)
1186{
1187 struct nfs4_ol_stateid *stp;
1188
1189 spin_lock(&fp->fi_lock);
1190 fp->fi_share_deny = 0;
1191 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1192 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1193 spin_unlock(&fp->fi_lock);
1194}
1195
1196static void
1197reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1198{
1199 int i;
1200 bool change = false;
1201
1202 for (i = 1; i < 4; i++) {
1203 if ((i & deny) != i) {
1204 change = true;
1205 clear_deny(i, stp);
1206 }
1207 }
1208
1209 /* Recalculate per-file deny mode if there was a change */
1210 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -04001211 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001212}
1213
Jeff Layton82c5ff12012-05-11 09:45:13 -04001214/* release all access and file references for a given stateid */
1215static void
1216release_all_access(struct nfs4_ol_stateid *stp)
1217{
1218 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -04001219 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001220
1221 if (fp && stp->st_deny_bmap != 0)
1222 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001223
1224 for (i = 1; i < 4; i++) {
1225 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -04001226 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001227 clear_access(i, stp);
1228 }
1229}
1230
Kinglong Meed50ffde2015-07-16 12:05:07 +08001231static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1232{
1233 kfree(sop->so_owner.data);
1234 sop->so_ops->so_free(sop);
1235}
1236
Jeff Layton6b180f02014-07-29 21:34:26 -04001237static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1238{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001239 struct nfs4_client *clp = sop->so_client;
1240
1241 might_lock(&clp->cl_lock);
1242
1243 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001244 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001245 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001246 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001247 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001248}
1249
Jeff Laytone8568732015-08-24 12:41:47 -04001250static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001251{
Trond Myklebust11b91642014-07-29 21:34:08 -04001252 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001253
Jeff Layton1c755dc2014-07-29 21:34:12 -04001254 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1255
Jeff Laytone8568732015-08-24 12:41:47 -04001256 if (list_empty(&stp->st_perfile))
1257 return false;
1258
Trond Myklebust1d31a252014-07-10 14:07:25 -04001259 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001260 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001261 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001262 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001263 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001264}
1265
Trond Myklebust60116952014-07-29 21:34:06 -04001266static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001267{
Trond Myklebust60116952014-07-29 21:34:06 -04001268 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001269
Sachin Bhamare8287f002015-04-27 14:50:14 +02001270 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001271 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001272 if (stp->st_stateowner)
1273 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -04001274 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001275}
1276
Jeff Laytonb49e0842014-07-29 21:34:11 -04001277static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001278{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001279 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1280 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001281 struct file *file;
1282
Jeff Laytonb49e0842014-07-29 21:34:11 -04001283 file = find_any_file(stp->st_stid.sc_file);
1284 if (file)
1285 filp_close(file, (fl_owner_t)lo);
1286 nfs4_free_ol_stateid(stid);
1287}
1288
Jeff Layton2c41beb2014-07-29 21:34:41 -04001289/*
1290 * Put the persistent reference to an already unhashed generic stateid, while
1291 * holding the cl_lock. If it's the last reference, then put it onto the
1292 * reaplist for later destruction.
1293 */
1294static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1295 struct list_head *reaplist)
1296{
1297 struct nfs4_stid *s = &stp->st_stid;
1298 struct nfs4_client *clp = s->sc_client;
1299
1300 lockdep_assert_held(&clp->cl_lock);
1301
1302 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1303
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001304 if (!refcount_dec_and_test(&s->sc_count)) {
Jeff Layton2c41beb2014-07-29 21:34:41 -04001305 wake_up_all(&close_wq);
1306 return;
1307 }
1308
1309 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1310 list_add(&stp->st_locks, reaplist);
1311}
1312
Jeff Laytone8568732015-08-24 12:41:47 -04001313static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001314{
Chuck Leverf46c4452016-10-29 18:19:03 -04001315 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001316
1317 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001318 nfs4_unhash_stid(&stp->st_stid);
Jeff Laytone8568732015-08-24 12:41:47 -04001319 return unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001320}
1321
Jeff Layton5adfd882014-07-29 21:34:31 -04001322static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001323{
Chuck Leverf46c4452016-10-29 18:19:03 -04001324 struct nfs4_client *clp = stp->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04001325 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001326
Chuck Leverf46c4452016-10-29 18:19:03 -04001327 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001328 unhashed = unhash_lock_stateid(stp);
Chuck Leverf46c4452016-10-29 18:19:03 -04001329 spin_unlock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001330 if (unhashed)
1331 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001332}
1333
Trond Myklebustc58c6612014-07-29 21:34:35 -04001334static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001335{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001336 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001337
Trond Myklebustd4f04892014-07-29 21:34:36 -04001338 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001339
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001340 list_del_init(&lo->lo_owner.so_strhash);
1341}
1342
Jeff Layton2c41beb2014-07-29 21:34:41 -04001343/*
1344 * Free a list of generic stateids that were collected earlier after being
1345 * fully unhashed.
1346 */
1347static void
1348free_ol_stateid_reaplist(struct list_head *reaplist)
1349{
1350 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001351 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001352
1353 might_sleep();
1354
1355 while (!list_empty(reaplist)) {
1356 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1357 st_locks);
1358 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001359 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001360 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001361 if (fp)
1362 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001363 }
1364}
1365
Jeff Laytond83017f2014-07-29 21:34:42 -04001366static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1367 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001368{
1369 struct nfs4_ol_stateid *stp;
1370
Jeff Laytone8568732015-08-24 12:41:47 -04001371 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1372
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001373 while (!list_empty(&open_stp->st_locks)) {
1374 stp = list_entry(open_stp->st_locks.next,
1375 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001376 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001377 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001378 }
1379}
1380
Jeff Laytone8568732015-08-24 12:41:47 -04001381static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001382 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001383{
Jeff Laytone8568732015-08-24 12:41:47 -04001384 bool unhashed;
1385
Jeff Layton2c41beb2014-07-29 21:34:41 -04001386 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1387
Jeff Laytone8568732015-08-24 12:41:47 -04001388 unhashed = unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001389 release_open_stateid_locks(stp, reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04001390 return unhashed;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001391}
1392
1393static void release_open_stateid(struct nfs4_ol_stateid *stp)
1394{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001395 LIST_HEAD(reaplist);
1396
1397 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001398 if (unhash_open_stateid(stp, &reaplist))
1399 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001400 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1401 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001402}
1403
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001404static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001405{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001406 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001407
Trond Myklebustd4f04892014-07-29 21:34:36 -04001408 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001409
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001410 list_del_init(&oo->oo_owner.so_strhash);
1411 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001412}
1413
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001414static void release_last_closed_stateid(struct nfs4_openowner *oo)
1415{
Jeff Layton217526e2014-07-30 08:27:11 -04001416 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1417 nfsd_net_id);
1418 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001419
Jeff Layton217526e2014-07-30 08:27:11 -04001420 spin_lock(&nn->client_lock);
1421 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001422 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001423 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001424 oo->oo_last_closed_stid = NULL;
1425 }
Jeff Layton217526e2014-07-30 08:27:11 -04001426 spin_unlock(&nn->client_lock);
1427 if (s)
1428 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001429}
1430
Jeff Layton2c41beb2014-07-29 21:34:41 -04001431static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001432{
1433 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001434 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001435 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001436
Jeff Layton2c41beb2014-07-29 21:34:41 -04001437 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001438
Trond Myklebustd4f04892014-07-29 21:34:36 -04001439 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001440 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001441 while (!list_empty(&oo->oo_owner.so_stateids)) {
1442 stp = list_first_entry(&oo->oo_owner.so_stateids,
1443 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001444 if (unhash_open_stateid(stp, &reaplist))
1445 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001446 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001447 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001448 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001449 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001450 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001451}
1452
Marc Eshel5282fd72009-04-03 08:27:52 +03001453static inline int
1454hash_sessionid(struct nfs4_sessionid *sessionid)
1455{
1456 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1457
1458 return sid->sequence % SESSION_HASH_SIZE;
1459}
1460
Mark Salter135dd002015-04-06 09:46:00 -04001461#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001462static inline void
1463dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1464{
1465 u32 *ptr = (u32 *)(&sessionid->data[0]);
1466 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1467}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001468#else
1469static inline void
1470dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1471{
1472}
1473#endif
1474
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001475/*
1476 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1477 * won't be used for replay.
1478 */
1479void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1480{
1481 struct nfs4_stateowner *so = cstate->replay_owner;
1482
1483 if (nfserr == nfserr_replay_me)
1484 return;
1485
1486 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001487 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001488 return;
1489 }
1490 if (!so)
1491 return;
1492 if (so->so_is_open_owner)
1493 release_last_closed_stateid(openowner(so));
1494 so->so_seqid++;
1495 return;
1496}
Marc Eshel5282fd72009-04-03 08:27:52 +03001497
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001498static void
1499gen_sessionid(struct nfsd4_session *ses)
1500{
1501 struct nfs4_client *clp = ses->se_client;
1502 struct nfsd4_sessionid *sid;
1503
1504 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1505 sid->clientid = clp->cl_clientid;
1506 sid->sequence = current_sessionid++;
1507 sid->reserved = 0;
1508}
1509
1510/*
Andy Adamsona649637c72009-08-28 08:45:01 -04001511 * The protocol defines ca_maxresponssize_cached to include the size of
1512 * the rpc header, but all we need to cache is the data starting after
1513 * the end of the initial SEQUENCE operation--the rest we regenerate
1514 * each time. Therefore we can advertise a ca_maxresponssize_cached
1515 * value that is the number of bytes in our cache plus a few additional
1516 * bytes. In order to stay on the safe side, and not promise more than
1517 * we can cache, those additional bytes must be the minimum possible: 24
1518 * bytes of rpc header (xid through accept state, with AUTH_NULL
1519 * verifier), 12 for the compound header (with zero-length tag), and 44
1520 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001521 */
Andy Adamsona649637c72009-08-28 08:45:01 -04001522#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1523
Andy Adamson557ce262009-08-28 08:45:04 -04001524static void
1525free_session_slots(struct nfsd4_session *ses)
1526{
1527 int i;
1528
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001529 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1530 free_svc_cred(&ses->se_slots[i]->sl_cred);
Andy Adamson557ce262009-08-28 08:45:04 -04001531 kfree(ses->se_slots[i]);
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001532 }
Andy Adamson557ce262009-08-28 08:45:04 -04001533}
1534
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001535/*
1536 * We don't actually need to cache the rpc and session headers, so we
1537 * can allocate a little less for each slot:
1538 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001539static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001540{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001541 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001542
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001543 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1544 size = 0;
1545 else
1546 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1547 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001548}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001549
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001550/*
1551 * XXX: If we run out of reserved DRC memory we could (up to a point)
1552 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001553 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001554 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001555static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001556{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001557 u32 slotsize = slot_bytes(ca);
1558 u32 num = ca->maxreqs;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001559 unsigned long avail, total_avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001560
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001561 spin_lock(&nfsd_drc_lock);
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001562 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1563 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001564 /*
1565 * Never use more than a third of the remaining memory,
1566 * unless it's the only way to give this client a slot:
1567 */
Paul Menzel3b2d4dc2019-07-03 13:28:15 +02001568 avail = clamp_t(unsigned long, avail, slotsize, total_avail/3);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001569 num = min_t(int, num, avail / slotsize);
1570 nfsd_drc_mem_used += num * slotsize;
1571 spin_unlock(&nfsd_drc_lock);
1572
1573 return num;
1574}
1575
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001576static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001577{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001578 int slotsize = slot_bytes(ca);
1579
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001580 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001581 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001582 spin_unlock(&nfsd_drc_lock);
1583}
1584
Kinglong Mee60810e52014-01-01 00:35:47 +08001585static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1586 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001587{
Kinglong Mee60810e52014-01-01 00:35:47 +08001588 int numslots = fattrs->maxreqs;
1589 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001590 struct nfsd4_session *new;
1591 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001592
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001593 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001594 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1595 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001596
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001597 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001598 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001599 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001600 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001601 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001602 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001603 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001604 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001605 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001606
1607 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1608 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1609
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001610 return new;
1611out_free:
1612 while (i--)
1613 kfree(new->se_slots[i]);
1614 kfree(new);
1615 return NULL;
1616}
1617
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001618static void free_conn(struct nfsd4_conn *c)
1619{
1620 svc_xprt_put(c->cn_xprt);
1621 kfree(c);
1622}
1623
1624static void nfsd4_conn_lost(struct svc_xpt_user *u)
1625{
1626 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1627 struct nfs4_client *clp = c->cn_session->se_client;
1628
1629 spin_lock(&clp->cl_lock);
1630 if (!list_empty(&c->cn_persession)) {
1631 list_del(&c->cn_persession);
1632 free_conn(c);
1633 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001634 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001635 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001636}
1637
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001638static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001639{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001640 struct nfsd4_conn *conn;
1641
1642 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1643 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001644 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001645 svc_xprt_get(rqstp->rq_xprt);
1646 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001647 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001648 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1649 return conn;
1650}
1651
J. Bruce Fields328ead22010-09-29 16:11:06 -04001652static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1653{
1654 conn->cn_session = ses;
1655 list_add(&conn->cn_persession, &ses->se_conns);
1656}
1657
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001658static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1659{
1660 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001661
1662 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001663 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001664 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001665}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001666
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001667static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001668{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001669 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001670 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001671}
1672
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001673static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001674{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001675 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001676
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001677 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001678 ret = nfsd4_register_conn(conn);
1679 if (ret)
1680 /* oops; xprt is already down: */
1681 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001682 /* We may have gained or lost a callback channel: */
1683 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001684}
1685
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001686static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001687{
1688 u32 dir = NFS4_CDFC4_FORE;
1689
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001690 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001691 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001692 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001693}
1694
1695/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001696static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001697{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001698 struct nfs4_client *clp = s->se_client;
1699 struct nfsd4_conn *c;
1700
1701 spin_lock(&clp->cl_lock);
1702 while (!list_empty(&s->se_conns)) {
1703 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1704 list_del_init(&c->cn_persession);
1705 spin_unlock(&clp->cl_lock);
1706
1707 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1708 free_conn(c);
1709
1710 spin_lock(&clp->cl_lock);
1711 }
1712 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001713}
1714
J. Bruce Fields1377b692012-09-11 21:42:40 -04001715static void __free_session(struct nfsd4_session *ses)
1716{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001717 free_session_slots(ses);
1718 kfree(ses);
1719}
1720
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001721static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001722{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001723 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001724 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001725 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001726}
Andy Adamson557ce262009-08-28 08:45:04 -04001727
Fengguang Wu135ae822012-11-10 07:20:25 -05001728static 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 -04001729{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001730 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001731 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001732
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001733 new->se_client = clp;
1734 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001735
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001736 INIT_LIST_HEAD(&new->se_conns);
1737
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001738 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001739 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001740 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001741 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001742 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001743 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001744 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001745 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001746 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001747 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001748
Chuck Leverb0d2e422014-08-22 15:10:59 -04001749 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001750 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001751 /*
1752 * This is a little silly; with sessions there's no real
1753 * use for the callback address. Use the peer address
1754 * as a reasonable default for now, but consider fixing
1755 * the rpc client not to require an address in the
1756 * future:
1757 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001758 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1759 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001760 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001761}
1762
Benny Halevy9089f1b2010-05-12 00:12:26 +03001763/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001764static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001765__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001766{
1767 struct nfsd4_session *elem;
1768 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001769 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001770
Trond Myklebust0a880a22014-07-30 08:27:10 -04001771 lockdep_assert_held(&nn->client_lock);
1772
Marc Eshel5282fd72009-04-03 08:27:52 +03001773 dump_sessionid(__func__, sessionid);
1774 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001775 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001776 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001777 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1778 NFS4_MAX_SESSIONID_LEN)) {
1779 return elem;
1780 }
1781 }
1782
1783 dprintk("%s: session not found\n", __func__);
1784 return NULL;
1785}
1786
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001787static struct nfsd4_session *
1788find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1789 __be32 *ret)
1790{
1791 struct nfsd4_session *session;
1792 __be32 status = nfserr_badsession;
1793
1794 session = __find_in_sessionid_hashtbl(sessionid, net);
1795 if (!session)
1796 goto out;
1797 status = nfsd4_get_session_locked(session);
1798 if (status)
1799 session = NULL;
1800out:
1801 *ret = status;
1802 return session;
1803}
1804
Benny Halevy9089f1b2010-05-12 00:12:26 +03001805/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001806static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001807unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001808{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001809 struct nfs4_client *clp = ses->se_client;
1810 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1811
1812 lockdep_assert_held(&nn->client_lock);
1813
Andy Adamson7116ed62009-04-03 08:27:43 +03001814 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001815 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001816 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001817 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001818}
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1821static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001822STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001824 /*
1825 * We're assuming the clid was not given out from a boot
1826 * precisely 2^32 (about 136 years) before this one. That seems
1827 * a safe assumption:
1828 */
1829 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001831 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001832 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return 1;
1834}
1835
1836/*
1837 * XXX Should we use a slab cache ?
1838 * This type of memory management is somewhat inefficient, but we use it
1839 * anyway since SETCLIENTID is not a common operation.
1840 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001841static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
1843 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001844 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Jeff Layton9258a2d2018-03-16 09:47:22 -04001846 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001847 if (clp == NULL)
1848 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001849 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001850 if (clp->cl_name.data == NULL)
1851 goto err_no_name;
Kees Cook6da2ec52018-06-12 13:55:00 -07001852 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1853 sizeof(struct list_head),
1854 GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001855 if (!clp->cl_ownerstr_hashtbl)
1856 goto err_no_hashtbl;
1857 for (i = 0; i < OWNER_HASH_SIZE; i++)
1858 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001859 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001860 INIT_LIST_HEAD(&clp->cl_sessions);
1861 idr_init(&clp->cl_stateids);
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04001862 atomic_set(&clp->cl_rpc_users, 0);
Trond Myklebust5694c932014-04-18 14:43:56 -04001863 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1864 INIT_LIST_HEAD(&clp->cl_idhash);
1865 INIT_LIST_HEAD(&clp->cl_openowners);
1866 INIT_LIST_HEAD(&clp->cl_delegations);
1867 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04001868 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001869#ifdef CONFIG_NFSD_PNFS
1870 INIT_LIST_HEAD(&clp->cl_lo_states);
1871#endif
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001872 INIT_LIST_HEAD(&clp->async_copies);
1873 spin_lock_init(&clp->async_lock);
Trond Myklebust5694c932014-04-18 14:43:56 -04001874 spin_lock_init(&clp->cl_lock);
1875 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001877err_no_hashtbl:
1878 kfree(clp->cl_name.data);
1879err_no_name:
Jeff Layton9258a2d2018-03-16 09:47:22 -04001880 kmem_cache_free(client_slab, clp);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001881 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
1883
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001884static void __free_client(struct kref *k)
1885{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04001886 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
1887 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001888
1889 free_svc_cred(&clp->cl_cred);
1890 kfree(clp->cl_ownerstr_hashtbl);
1891 kfree(clp->cl_name.data);
1892 idr_destroy(&clp->cl_stateids);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001893 kmem_cache_free(client_slab, clp);
1894}
1895
1896void drop_client(struct nfs4_client *clp)
1897{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04001898 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001899}
1900
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001901static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902free_client(struct nfs4_client *clp)
1903{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001904 while (!list_empty(&clp->cl_sessions)) {
1905 struct nfsd4_session *ses;
1906 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1907 se_perclnt);
1908 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001909 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1910 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001911 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001912 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04001913 if (clp->cl_nfsd_dentry)
1914 nfsd_client_rmdir(clp->cl_nfsd_dentry);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001915 drop_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916}
1917
Benny Halevy84d38ac2010-05-12 00:13:16 +03001918/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001919static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001920unhash_client_locked(struct nfs4_client *clp)
1921{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001922 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001923 struct nfsd4_session *ses;
1924
Trond Myklebust0a880a22014-07-30 08:27:10 -04001925 lockdep_assert_held(&nn->client_lock);
1926
Trond Myklebust4beb3452014-07-30 08:27:02 -04001927 /* Mark the client as expired! */
1928 clp->cl_time = 0;
1929 /* Make it invisible */
1930 if (!list_empty(&clp->cl_idhash)) {
1931 list_del_init(&clp->cl_idhash);
1932 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1933 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1934 else
1935 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1936 }
1937 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001938 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001939 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1940 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001941 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001942}
1943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001945unhash_client(struct nfs4_client *clp)
1946{
1947 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1948
1949 spin_lock(&nn->client_lock);
1950 unhash_client_locked(clp);
1951 spin_unlock(&nn->client_lock);
1952}
1953
Jeff Layton97403d92014-07-30 08:27:12 -04001954static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1955{
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04001956 if (atomic_read(&clp->cl_rpc_users))
Jeff Layton97403d92014-07-30 08:27:12 -04001957 return nfserr_jukebox;
1958 unhash_client_locked(clp);
1959 return nfs_ok;
1960}
1961
Trond Myklebust4beb3452014-07-30 08:27:02 -04001962static void
1963__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
Jeff Layton68ef3bc2018-03-16 11:32:02 -04001965 int i;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001966 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 struct list_head reaplist;
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04001971 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001972 while (!list_empty(&clp->cl_delegations)) {
1973 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001974 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04001975 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 }
Benny Halevycdc975052014-05-30 09:09:30 -04001977 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 while (!list_empty(&reaplist)) {
1979 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001980 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001981 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001983 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05001984 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001985 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001986 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001987 }
NeilBrownea1da632005-06-23 22:04:17 -07001988 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001989 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04001990 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001991 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
Jeff Layton68ef3bc2018-03-16 11:32:02 -04001993 for (i = 0; i < OWNER_HASH_SIZE; i++) {
1994 struct nfs4_stateowner *so, *tmp;
1995
1996 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
1997 so_strhash) {
1998 /* Should be no openowners at this point */
1999 WARN_ON_ONCE(so->so_is_open_owner);
2000 remove_blocked_locks(lockowner(so));
2001 }
2002 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002003 nfsd4_return_all_client_layouts(clp);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002004 nfsd4_shutdown_copy(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04002005 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002006 if (clp->cl_cb_conn.cb_xprt)
2007 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002008 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009}
2010
Trond Myklebust4beb3452014-07-30 08:27:02 -04002011static void
2012destroy_client(struct nfs4_client *clp)
2013{
2014 unhash_client(clp);
2015 __destroy_client(clp);
2016}
2017
Scott Mayhew362063a2019-03-26 18:06:28 -04002018static void inc_reclaim_complete(struct nfs4_client *clp)
2019{
2020 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2021
2022 if (!nn->track_reclaim_completes)
2023 return;
2024 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2025 return;
2026 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2027 nn->reclaim_str_hashtbl_size) {
2028 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2029 clp->net->ns.inum);
2030 nfsd4_end_grace(nn);
2031 }
2032}
2033
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002034static void expire_client(struct nfs4_client *clp)
2035{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002036 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002037 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04002038 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002039}
2040
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002041static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2042{
2043 memcpy(target->cl_verifier.data, source->data,
2044 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045}
2046
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002047static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2048{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2050 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2051}
2052
J. Bruce Fields50043852015-11-20 15:58:37 -05002053static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2054{
NeilBrown2f10fdc2017-03-23 16:57:36 +08002055 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2056 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2057 GFP_KERNEL);
Chuck Lever9abdda52018-08-16 12:05:59 -04002058 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2059 if ((source->cr_principal && !target->cr_principal) ||
2060 (source->cr_raw_principal && !target->cr_raw_principal) ||
2061 (source->cr_targ_princ && !target->cr_targ_princ))
NeilBrown2f10fdc2017-03-23 16:57:36 +08002062 return -ENOMEM;
J. Bruce Fields50043852015-11-20 15:58:37 -05002063
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04002064 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 target->cr_uid = source->cr_uid;
2066 target->cr_gid = source->cr_gid;
2067 target->cr_group_info = source->cr_group_info;
2068 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04002069 target->cr_gss_mech = source->cr_gss_mech;
2070 if (source->cr_gss_mech)
2071 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002072 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073}
2074
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002075static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002076compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2077{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002078 if (o1->len < o2->len)
2079 return -1;
2080 if (o1->len > o2->len)
2081 return 1;
2082 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002083}
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002086same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2087{
2088 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089}
2090
2091static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002092same_clid(clientid_t *cl1, clientid_t *cl2)
2093{
2094 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095}
2096
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002097static bool groups_equal(struct group_info *g1, struct group_info *g2)
2098{
2099 int i;
2100
2101 if (g1->ngroups != g2->ngroups)
2102 return false;
2103 for (i=0; i<g1->ngroups; i++)
Alexey Dobriyan81243ea2016-10-07 17:03:12 -07002104 if (!gid_eq(g1->gid[i], g2->gid[i]))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002105 return false;
2106 return true;
2107}
2108
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002109/*
2110 * RFC 3530 language requires clid_inuse be returned when the
2111 * "principal" associated with a requests differs from that previously
2112 * used. We use uid, gid's, and gss principal string as our best
2113 * approximation. We also don't want to allow non-gss use of a client
2114 * established using gss: in theory cr_principal should catch that
2115 * change, but in practice cr_principal can be null even in the gss case
2116 * since gssd doesn't always pass down a principal string.
2117 */
2118static bool is_gss_cred(struct svc_cred *cr)
2119{
2120 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2121 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2122}
2123
2124
Vivek Trivedi5559b502012-07-24 21:18:20 +05302125static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002126same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2127{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002128 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002129 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2130 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002131 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2132 return false;
Chuck Lever9abdda52018-08-16 12:05:59 -04002133 /* XXX: check that cr_targ_princ fields match ? */
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002134 if (cr1->cr_principal == cr2->cr_principal)
2135 return true;
2136 if (!cr1->cr_principal || !cr2->cr_principal)
2137 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05302138 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
2140
J. Bruce Fields57266a62013-04-13 14:27:29 -04002141static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2142{
2143 struct svc_cred *cr = &rqstp->rq_cred;
2144 u32 service;
2145
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04002146 if (!cr->cr_gss_mech)
2147 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002148 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2149 return service == RPC_GSS_SVC_INTEGRITY ||
2150 service == RPC_GSS_SVC_PRIVACY;
2151}
2152
Andrew Elblededeb132016-06-15 12:52:08 -04002153bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
J. Bruce Fields57266a62013-04-13 14:27:29 -04002154{
2155 struct svc_cred *cr = &rqstp->rq_cred;
2156
2157 if (!cl->cl_mach_cred)
2158 return true;
2159 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2160 return false;
2161 if (!svc_rqst_integrity_protected(rqstp))
2162 return false;
J. Bruce Fields414ca012015-11-20 10:48:02 -05002163 if (cl->cl_cred.cr_raw_principal)
2164 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2165 cr->cr_raw_principal);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002166 if (!cr->cr_principal)
2167 return false;
2168 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2169}
2170
Jeff Layton294ac322014-07-30 08:27:15 -04002171static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05002172{
Chuck Leverab4684d2012-03-02 17:13:50 -05002173 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Jeff Laytonf4199922014-06-17 07:44:11 -04002175 /*
2176 * This is opaque to client, so no need to byte-swap. Use
2177 * __force to keep sparse happy
2178 */
2179 verf[0] = (__force __be32)get_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08002180 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05002181 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
Jeff Layton294ac322014-07-30 08:27:15 -04002184static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2185{
2186 clp->cl_clientid.cl_boot = nn->boot_time;
2187 clp->cl_clientid.cl_id = nn->clientid_counter++;
2188 gen_confirm(clp, nn);
2189}
2190
Jeff Layton4770d722014-07-29 21:34:10 -04002191static struct nfs4_stid *
2192find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04002193{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002194 struct nfs4_stid *ret;
2195
2196 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2197 if (!ret || !ret->sc_type)
2198 return NULL;
2199 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04002200}
2201
Jeff Layton4770d722014-07-29 21:34:10 -04002202static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04002203find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002204{
2205 struct nfs4_stid *s;
2206
Jeff Layton4770d722014-07-29 21:34:10 -04002207 spin_lock(&cl->cl_lock);
2208 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002209 if (s != NULL) {
2210 if (typemask & s->sc_type)
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03002211 refcount_inc(&s->sc_count);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002212 else
2213 s = NULL;
2214 }
Jeff Layton4770d722014-07-29 21:34:10 -04002215 spin_unlock(&cl->cl_lock);
2216 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04002217}
2218
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002219static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2220{
2221 struct nfsdfs_client *nc;
2222 nc = get_nfsdfs_client(inode);
2223 if (!nc)
2224 return NULL;
2225 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2226}
2227
J. Bruce Fields169319f2019-06-19 12:39:46 -04002228static void seq_quote_mem(struct seq_file *m, char *data, int len)
2229{
2230 seq_printf(m, "\"");
2231 seq_escape_mem_ascii(m, data, len);
2232 seq_printf(m, "\"");
2233}
2234
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002235static int client_info_show(struct seq_file *m, void *v)
2236{
2237 struct inode *inode = m->private;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002238 struct nfs4_client *clp;
2239 u64 clid;
2240
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002241 clp = get_nfsdfs_clp(inode);
2242 if (!clp)
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002243 return -ENXIO;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002244 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2245 seq_printf(m, "clientid: 0x%llx\n", clid);
J. Bruce Fields169319f2019-06-19 12:39:46 -04002246 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2247 seq_printf(m, "name: ");
2248 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2249 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002250 drop_client(clp);
2251
2252 return 0;
2253}
2254
2255static int client_info_open(struct inode *inode, struct file *file)
2256{
2257 return single_open(file, client_info_show, inode);
2258}
2259
2260static const struct file_operations client_info_fops = {
2261 .open = client_info_open,
2262 .read = seq_read,
2263 .llseek = seq_lseek,
2264 .release = single_release,
2265};
2266
J. Bruce Fields78599c42019-04-22 15:26:23 -04002267static void *states_start(struct seq_file *s, loff_t *pos)
2268 __acquires(&clp->cl_lock)
2269{
2270 struct nfs4_client *clp = s->private;
2271 unsigned long id = *pos;
2272 void *ret;
2273
2274 spin_lock(&clp->cl_lock);
2275 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2276 *pos = id;
2277 return ret;
2278}
2279
2280static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2281{
2282 struct nfs4_client *clp = s->private;
2283 unsigned long id = *pos;
2284 void *ret;
2285
2286 id = *pos;
2287 id++;
2288 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2289 *pos = id;
2290 return ret;
2291}
2292
2293static void states_stop(struct seq_file *s, void *v)
2294 __releases(&clp->cl_lock)
2295{
2296 struct nfs4_client *clp = s->private;
2297
2298 spin_unlock(&clp->cl_lock);
2299}
2300
2301static void nfs4_show_superblock(struct seq_file *s, struct file *f)
2302{
2303 struct inode *inode = file_inode(f);
2304
2305 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2306 MAJOR(inode->i_sb->s_dev),
2307 MINOR(inode->i_sb->s_dev),
2308 inode->i_ino);
2309}
2310
2311static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2312{
2313 seq_printf(s, "owner: ");
2314 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2315}
2316
2317static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2318{
2319 struct nfs4_ol_stateid *ols;
2320 struct nfs4_file *nf;
2321 struct file *file;
2322 struct nfs4_stateowner *oo;
2323 unsigned int access, deny;
2324
2325 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2326 return 0; /* XXX: or SEQ_SKIP? */
2327 ols = openlockstateid(st);
2328 oo = ols->st_stateowner;
2329 nf = st->sc_file;
2330 file = find_any_file(nf);
2331
2332 seq_printf(s, "- 0x%16phN: { type: open, ", &st->sc_stateid);
2333
2334 access = bmap_to_share_mode(ols->st_access_bmap);
2335 deny = bmap_to_share_mode(ols->st_deny_bmap);
2336
2337 seq_printf(s, "access: \%s\%s, ",
2338 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2339 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2340 seq_printf(s, "deny: \%s\%s, ",
2341 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2342 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2343
2344 nfs4_show_superblock(s, file);
2345 seq_printf(s, ", ");
2346 nfs4_show_owner(s, oo);
2347 seq_printf(s, " }\n");
J. Bruce Fields78599c42019-04-22 15:26:23 -04002348 fput(file);
2349
2350 return 0;
2351}
2352
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002353static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2354{
2355 struct nfs4_ol_stateid *ols;
2356 struct nfs4_file *nf;
2357 struct file *file;
2358 struct nfs4_stateowner *oo;
2359
2360 ols = openlockstateid(st);
2361 oo = ols->st_stateowner;
2362 nf = st->sc_file;
2363 file = find_any_file(nf);
2364
2365 seq_printf(s, "- 0x%16phN: { type: lock, ", &st->sc_stateid);
2366
2367 /*
2368 * Note: a lock stateid isn't really the same thing as a lock,
2369 * it's the locking state held by one owner on a file, and there
2370 * may be multiple (or no) lock ranges associated with it.
2371 * (Same for the matter is true of open stateids.)
2372 */
2373
2374 nfs4_show_superblock(s, file);
2375 /* XXX: open stateid? */
2376 seq_printf(s, ", ");
2377 nfs4_show_owner(s, oo);
2378 seq_printf(s, " }\n");
2379 fput(file);
2380
2381 return 0;
2382}
2383
2384static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2385{
2386 struct nfs4_delegation *ds;
2387 struct nfs4_file *nf;
2388 struct file *file;
2389
2390 ds = delegstateid(st);
2391 nf = st->sc_file;
2392 file = nf->fi_deleg_file;
2393
2394 seq_printf(s, "- 0x%16phN: { type: deleg, ", &st->sc_stateid);
2395
2396 /* Kinda dead code as long as we only support read delegs: */
2397 seq_printf(s, "access: %s, ",
2398 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2399
2400 /* XXX: lease time, whether it's being recalled. */
2401
2402 nfs4_show_superblock(s, file);
2403 seq_printf(s, " }\n");
2404
2405 return 0;
2406}
2407
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002408static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2409{
2410 struct nfs4_layout_stateid *ls;
2411 struct file *file;
2412
2413 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2414 file = ls->ls_file;
2415
2416 seq_printf(s, "- 0x%16phN: { type: layout, ", &st->sc_stateid);
2417
2418 /* XXX: What else would be useful? */
2419
2420 nfs4_show_superblock(s, file);
2421 seq_printf(s, " }\n");
2422
2423 return 0;
2424}
2425
J. Bruce Fields78599c42019-04-22 15:26:23 -04002426static int states_show(struct seq_file *s, void *v)
2427{
2428 struct nfs4_stid *st = v;
2429
2430 switch (st->sc_type) {
2431 case NFS4_OPEN_STID:
2432 return nfs4_show_open(s, st);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002433 case NFS4_LOCK_STID:
2434 return nfs4_show_lock(s, st);
2435 case NFS4_DELEG_STID:
2436 return nfs4_show_deleg(s, st);
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002437 case NFS4_LAYOUT_STID:
2438 return nfs4_show_layout(s, st);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002439 default:
2440 return 0; /* XXX: or SEQ_SKIP? */
2441 }
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002442 /* XXX: copy stateids? */
J. Bruce Fields78599c42019-04-22 15:26:23 -04002443}
2444
2445static struct seq_operations states_seq_ops = {
2446 .start = states_start,
2447 .next = states_next,
2448 .stop = states_stop,
2449 .show = states_show
2450};
2451
2452static int client_states_open(struct inode *inode, struct file *file)
2453{
J. Bruce Fields78599c42019-04-22 15:26:23 -04002454 struct seq_file *s;
2455 struct nfs4_client *clp;
2456 int ret;
2457
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002458 clp = get_nfsdfs_clp(inode);
2459 if (!clp)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002460 return -ENXIO;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002461
2462 ret = seq_open(file, &states_seq_ops);
2463 if (ret)
2464 return ret;
2465 s = file->private_data;
2466 s->private = clp;
2467 return 0;
2468}
2469
2470static int client_opens_release(struct inode *inode, struct file *file)
2471{
2472 struct seq_file *m = file->private_data;
2473 struct nfs4_client *clp = m->private;
2474
2475 /* XXX: alternatively, we could get/drop in seq start/stop */
2476 drop_client(clp);
2477 return 0;
2478}
2479
2480static const struct file_operations client_states_fops = {
2481 .open = client_states_open,
2482 .read = seq_read,
2483 .llseek = seq_lseek,
2484 .release = client_opens_release,
2485};
2486
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002487static const struct tree_descr client_files[] = {
2488 [0] = {"info", &client_info_fops, S_IRUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002489 [1] = {"states", &client_states_fops, S_IRUSR},
2490 [3] = {""},
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002491};
2492
Jeff Layton2216d442012-11-12 15:00:57 -05002493static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002494 struct svc_rqst *rqstp, nfs4_verifier *verf)
2495{
2496 struct nfs4_client *clp;
2497 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002498 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002499 struct net *net = SVC_NET(rqstp);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002500 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002501
2502 clp = alloc_client(name);
2503 if (clp == NULL)
2504 return NULL;
2505
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002506 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2507 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002508 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002509 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002510 }
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002511 gen_clid(clp, nn);
2512 kref_init(&clp->cl_nfsdfs.cl_ref);
Christoph Hellwig0162ac22014-09-24 12:19:19 +02002513 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Benny Halevy07cd4902010-05-12 00:13:41 +03002514 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002515 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002516 copy_verf(clp, verf);
J. Bruce Fields3bade242019-05-14 21:38:11 -04002517 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04002518 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002519 clp->net = net;
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002520 clp->cl_nfsd_dentry = nfsd_client_mkdir(nn, &clp->cl_nfsdfs,
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002521 clp->cl_clientid.cl_id - nn->clientid_base,
2522 client_files);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002523 if (!clp->cl_nfsd_dentry) {
2524 free_client(clp);
2525 return NULL;
2526 }
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002527 return clp;
2528}
2529
NeilBrownfd39ca92005-06-23 22:04:03 -07002530static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002531add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2532{
2533 struct rb_node **new = &(root->rb_node), *parent = NULL;
2534 struct nfs4_client *clp;
2535
2536 while (*new) {
2537 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2538 parent = *new;
2539
2540 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2541 new = &((*new)->rb_left);
2542 else
2543 new = &((*new)->rb_right);
2544 }
2545
2546 rb_link_node(&new_clp->cl_namenode, parent, new);
2547 rb_insert_color(&new_clp->cl_namenode, root);
2548}
2549
2550static struct nfs4_client *
2551find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2552{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002553 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002554 struct rb_node *node = root->rb_node;
2555 struct nfs4_client *clp;
2556
2557 while (node) {
2558 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2559 cmp = compare_blob(&clp->cl_name, name);
2560 if (cmp > 0)
2561 node = node->rb_left;
2562 else if (cmp < 0)
2563 node = node->rb_right;
2564 else
2565 return clp;
2566 }
2567 return NULL;
2568}
2569
2570static void
2571add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572{
2573 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002574 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Trond Myklebust0a880a22014-07-30 08:27:10 -04002576 lockdep_assert_held(&nn->client_lock);
2577
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002578 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002579 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002581 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002582 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583}
2584
NeilBrownfd39ca92005-06-23 22:04:03 -07002585static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586move_to_confirmed(struct nfs4_client *clp)
2587{
2588 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002589 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Trond Myklebust0a880a22014-07-30 08:27:10 -04002591 lockdep_assert_held(&nn->client_lock);
2592
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002594 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002595 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002596 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002597 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002598 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599}
2600
2601static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002602find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
2604 struct nfs4_client *clp;
2605 unsigned int idhashval = clientid_hashval(clid->cl_id);
2606
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002607 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002608 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002609 if ((bool)clp->cl_minorversion != sessions)
2610 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002611 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 }
2615 return NULL;
2616}
2617
2618static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002619find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2620{
2621 struct list_head *tbl = nn->conf_id_hashtbl;
2622
Trond Myklebust0a880a22014-07-30 08:27:10 -04002623 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002624 return find_client_in_id_table(tbl, clid, sessions);
2625}
2626
2627static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002628find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002630 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Trond Myklebust0a880a22014-07-30 08:27:10 -04002632 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002633 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634}
2635
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002636static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002637{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002638 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002639}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002640
NeilBrown28ce6052005-06-23 22:03:56 -07002641static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002642find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002643{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002644 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002645 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002646}
2647
2648static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002649find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002650{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002651 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002652 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002653}
2654
NeilBrownfd39ca92005-06-23 22:04:03 -07002655static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002656gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002658 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002659 struct sockaddr *sa = svc_addr(rqstp);
2660 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002661 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
Jeff Layton7077ecb2009-08-14 12:57:58 -04002663 /* Currently, we only support tcp and tcp6 for the callback channel */
2664 if (se->se_callback_netid_len == 3 &&
2665 !memcmp(se->se_callback_netid_val, "tcp", 3))
2666 expected_family = AF_INET;
2667 else if (se->se_callback_netid_len == 4 &&
2668 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2669 expected_family = AF_INET6;
2670 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 goto out_err;
2672
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002673 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002674 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002675 (struct sockaddr *)&conn->cb_addr,
2676 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002677
J. Bruce Fields07263f12010-05-31 19:09:40 -04002678 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002680
J. Bruce Fields07263f12010-05-31 19:09:40 -04002681 if (conn->cb_addr.ss_family == AF_INET6)
2682 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002683
J. Bruce Fields07263f12010-05-31 19:09:40 -04002684 conn->cb_prog = se->se_callback_prog;
2685 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002686 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 return;
2688out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002689 conn->cb_addr.ss_family = AF_UNSPEC;
2690 conn->cb_addrlen = 0;
Rasmus Villemoes4ab495b2017-02-24 01:15:55 +01002691 dprintk("NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 "will not receive delegations\n",
2693 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2694
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 return;
2696}
2697
Andy Adamson074fe892009-04-03 08:28:15 +03002698/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002699 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002700 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002701static void
Andy Adamson074fe892009-04-03 08:28:15 +03002702nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2703{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002704 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002705 struct nfsd4_slot *slot = resp->cstate.slot;
2706 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002707
Andy Adamson557ce262009-08-28 08:45:04 -04002708 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002709
J. Bruce Fields085def32017-10-18 16:17:18 -04002710 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamson557ce262009-08-28 08:45:04 -04002711 slot->sl_opcnt = resp->opcnt;
2712 slot->sl_status = resp->cstate.status;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04002713 free_svc_cred(&slot->sl_cred);
2714 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
Andy Adamsonbf864a32009-04-03 08:28:35 +03002715
J. Bruce Fields085def32017-10-18 16:17:18 -04002716 if (!nfsd4_cache_this(resp)) {
2717 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002718 return;
2719 }
J. Bruce Fields085def32017-10-18 16:17:18 -04002720 slot->sl_flags |= NFSD4_SLOT_CACHED;
2721
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002722 base = resp->cstate.data_offset;
2723 slot->sl_datalen = buf->len - base;
2724 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Dan Carpenterd3f03402015-11-21 13:28:50 +03002725 WARN(1, "%s: sessions DRC could not cache compound\n",
2726 __func__);
Andy Adamson557ce262009-08-28 08:45:04 -04002727 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002728}
2729
2730/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002731 * Encode the replay sequence operation from the slot values.
2732 * If cachethis is FALSE encode the uncached rep error on the next
2733 * operation which sets resp->p and increments resp->opcnt for
2734 * nfs4svc_encode_compoundres.
2735 *
Andy Adamson074fe892009-04-03 08:28:15 +03002736 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002737static __be32
2738nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2739 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002740{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002741 struct nfsd4_op *op;
2742 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002743
Andy Adamsonabfabf82009-07-23 19:02:18 -04002744 /* Encode the replayed sequence operation */
2745 op = &args->ops[resp->opcnt - 1];
2746 nfsd4_encode_operation(resp, op);
2747
J. Bruce Fields085def32017-10-18 16:17:18 -04002748 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2749 return op->status;
2750 if (args->opcnt == 1) {
2751 /*
2752 * The original operation wasn't a solo sequence--we
2753 * always cache those--so this retry must not match the
2754 * original:
2755 */
2756 op->status = nfserr_seq_false_retry;
2757 } else {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002758 op = &args->ops[resp->opcnt++];
2759 op->status = nfserr_retry_uncached_rep;
2760 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002761 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002762 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002763}
2764
2765/*
Andy Adamson557ce262009-08-28 08:45:04 -04002766 * The sequence operation is not cached because we can use the slot and
2767 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002768 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002769static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002770nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2771 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002772{
Andy Adamson557ce262009-08-28 08:45:04 -04002773 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002774 struct xdr_stream *xdr = &resp->xdr;
2775 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002776 __be32 status;
2777
Andy Adamson557ce262009-08-28 08:45:04 -04002778 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002779
Andy Adamsonabfabf82009-07-23 19:02:18 -04002780 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002781 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002782 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002783
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002784 p = xdr_reserve_space(xdr, slot->sl_datalen);
2785 if (!p) {
2786 WARN_ON_ONCE(1);
2787 return nfserr_serverfault;
2788 }
2789 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2790 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002791
Andy Adamson557ce262009-08-28 08:45:04 -04002792 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002793 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002794}
2795
Andy Adamson0733d212009-04-03 08:28:01 +03002796/*
2797 * Set the exchange_id flags returned by the server.
2798 */
2799static void
2800nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2801{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002802#ifdef CONFIG_NFSD_PNFS
2803 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2804#else
Andy Adamson0733d212009-04-03 08:28:01 +03002805 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002806#endif
Andy Adamson0733d212009-04-03 08:28:01 +03002807
2808 /* Referrals are supported, Migration is not. */
2809 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2810
2811 /* set the wire flags to return to client. */
2812 clid->flags = new->cl_exchange_flags;
2813}
2814
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002815static bool client_has_openowners(struct nfs4_client *clp)
2816{
2817 struct nfs4_openowner *oo;
2818
2819 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2820 if (!list_empty(&oo->oo_owner.so_stateids))
2821 return true;
2822 }
2823 return false;
2824}
2825
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002826static bool client_has_state(struct nfs4_client *clp)
2827{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002828 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08002829#ifdef CONFIG_NFSD_PNFS
2830 || !list_empty(&clp->cl_lo_states)
2831#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002832 || !list_empty(&clp->cl_delegations)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002833 || !list_empty(&clp->cl_sessions)
2834 || !list_empty(&clp->async_copies);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002835}
2836
Al Virob37ad282006-10-19 23:28:59 -07002837__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02002838nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2839 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03002840{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002841 struct nfsd4_exchange_id *exid = &u->exchange_id;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002842 struct nfs4_client *conf, *new;
2843 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002844 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002845 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002846 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002847 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002848 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002849 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002850
Jeff Layton363168b2009-08-14 12:57:56 -04002851 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002852 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002853 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002854 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002855 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002856
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002857 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002858 return nfserr_inval;
2859
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002860 new = create_client(exid->clname, rqstp, &verf);
2861 if (new == NULL)
2862 return nfserr_jukebox;
2863
Andy Adamson0733d212009-04-03 08:28:01 +03002864 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002865 case SP4_MACH_CRED:
Andrew Elbleed941642016-06-15 12:52:09 -04002866 exid->spo_must_enforce[0] = 0;
2867 exid->spo_must_enforce[1] = (
2868 1 << (OP_BIND_CONN_TO_SESSION - 32) |
2869 1 << (OP_EXCHANGE_ID - 32) |
2870 1 << (OP_CREATE_SESSION - 32) |
2871 1 << (OP_DESTROY_SESSION - 32) |
2872 1 << (OP_DESTROY_CLIENTID - 32));
2873
2874 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
2875 1 << (OP_OPEN_DOWNGRADE) |
2876 1 << (OP_LOCKU) |
2877 1 << (OP_DELEGRETURN));
2878
2879 exid->spo_must_allow[1] &= (
2880 1 << (OP_TEST_STATEID - 32) |
2881 1 << (OP_FREE_STATEID - 32));
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002882 if (!svc_rqst_integrity_protected(rqstp)) {
2883 status = nfserr_inval;
2884 goto out_nolock;
2885 }
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002886 /*
2887 * Sometimes userspace doesn't give us a principal.
2888 * Which is a bug, really. Anyway, we can't enforce
2889 * MACH_CRED in that case, better to give up now:
2890 */
J. Bruce Fields414ca012015-11-20 10:48:02 -05002891 if (!new->cl_cred.cr_principal &&
2892 !new->cl_cred.cr_raw_principal) {
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002893 status = nfserr_serverfault;
2894 goto out_nolock;
2895 }
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002896 new->cl_mach_cred = true;
Andy Adamson0733d212009-04-03 08:28:01 +03002897 case SP4_NONE:
2898 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002899 default: /* checked by xdr code */
2900 WARN_ON_ONCE(1);
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -06002901 /* fall through */
Andy Adamson0733d212009-04-03 08:28:01 +03002902 case SP4_SSV:
Kinglong Mee8edf4b02016-02-26 22:36:42 +08002903 status = nfserr_encr_alg_unsupp;
2904 goto out_nolock;
Andy Adamson0733d212009-04-03 08:28:01 +03002905 }
2906
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002907 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002908 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002909 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002910 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002911 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2912 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2913
J. Bruce Fields136e6582012-05-12 20:37:23 -04002914 if (update) {
2915 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002916 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002917 goto out;
2918 }
Andrew Elblededeb132016-06-15 12:52:08 -04002919 if (!nfsd4_mach_creds_match(conf, rqstp)) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002920 status = nfserr_wrong_cred;
2921 goto out;
2922 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002923 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002924 status = nfserr_perm;
2925 goto out;
2926 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002927 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002928 status = nfserr_not_same;
2929 goto out;
2930 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002931 /* case 6 */
2932 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002933 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002934 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002935 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002936 if (client_has_state(conf)) {
2937 status = nfserr_clid_inuse;
2938 goto out;
2939 }
Andy Adamson0733d212009-04-03 08:28:01 +03002940 goto out_new;
2941 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002942 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002943 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002944 goto out_copy;
2945 }
2946 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002947 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002948 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002949 }
2950
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002951 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002952 status = nfserr_noent;
2953 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002954 }
2955
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002956 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002957 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002958 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002959
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002960 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002961out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002962 if (conf) {
2963 status = mark_client_expired_locked(conf);
2964 if (status)
2965 goto out;
2966 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002967 new->cl_minorversion = cstate->minorversion;
Andrew Elbleed941642016-06-15 12:52:09 -04002968 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
2969 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
Andy Adamson0733d212009-04-03 08:28:01 +03002970
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002971 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002972 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002973out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002974 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2975 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002976
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002977 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2978 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002979
2980 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002981 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002982 status = nfs_ok;
2983
2984out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002985 spin_unlock(&nn->client_lock);
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002986out_nolock:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002987 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002988 expire_client(new);
2989 if (unconf)
2990 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002991 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002992}
2993
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002994static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002995check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002996{
Andy Adamson88e588d2009-07-23 19:02:15 -04002997 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2998 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002999
3000 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04003001 if (slot_inuse) {
3002 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003003 return nfserr_jukebox;
3004 else
3005 return nfserr_seq_misordered;
3006 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05003007 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04003008 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03003009 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04003010 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003011 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003012 return nfserr_seq_misordered;
3013}
3014
Andy Adamson49557cc2009-07-23 19:02:16 -04003015/*
3016 * Cache the create session result into the create session single DRC
3017 * slot cache by saving the xdr structure. sl_seqid has been set.
3018 * Do this for solo or embedded create session operations.
3019 */
3020static void
3021nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003022 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04003023{
3024 slot->sl_status = nfserr;
3025 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3026}
3027
3028static __be32
3029nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3030 struct nfsd4_clid_slot *slot)
3031{
3032 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3033 return slot->sl_status;
3034}
3035
Mi Jinlong1b74c252011-07-14 14:50:17 +08003036#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3037 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3038 1 + /* MIN tag is length with zero, only length */ \
3039 3 + /* version, opcount, opcode */ \
3040 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3041 /* seqid, slotID, slotID, cache */ \
3042 4 ) * sizeof(__be32))
3043
3044#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3045 2 + /* verifier: AUTH_NULL, length 0 */\
3046 1 + /* status */ \
3047 1 + /* MIN tag is length with zero, only length */ \
3048 3 + /* opcount, opcode, opstatus*/ \
3049 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3050 /* seqid, slotID, slotID, slotID, status */ \
3051 5 ) * sizeof(__be32))
3052
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003053static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08003054{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003055 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3056
J. Bruce Fields373cd402013-04-08 15:42:12 -04003057 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3058 return nfserr_toosmall;
3059 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3060 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003061 ca->headerpadsz = 0;
3062 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3063 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3064 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3065 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3066 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3067 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3068 /*
3069 * Note decreasing slot size below client's request may make it
3070 * difficult for client to function correctly, whereas
3071 * decreasing the number of slots will (just?) affect
3072 * performance. When short on memory we therefore prefer to
3073 * decrease number of slots instead of their size. Clients that
3074 * request larger slots than they need will get poor results:
3075 */
3076 ca->maxreqs = nfsd4_get_drc_mem(ca);
3077 if (!ca->maxreqs)
3078 return nfserr_jukebox;
3079
J. Bruce Fields373cd402013-04-08 15:42:12 -04003080 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08003081}
3082
Chuck Lever45006322016-03-01 13:06:02 -05003083/*
3084 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3085 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3086 */
3087#define RPC_MAX_HEADER_WITH_AUTH_SYS \
3088 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3089
3090#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3091 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3092
Kinglong Mee8a891632013-12-23 18:11:02 +08003093#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003094 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003095#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003096 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3097 sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003098
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003099static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3100{
3101 ca->headerpadsz = 0;
3102
Kinglong Mee8a891632013-12-23 18:11:02 +08003103 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003104 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08003105 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003106 return nfserr_toosmall;
3107 ca->maxresp_cached = 0;
3108 if (ca->maxops < 2)
3109 return nfserr_toosmall;
3110
3111 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003112}
3113
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003114static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3115{
3116 switch (cbs->flavor) {
3117 case RPC_AUTH_NULL:
3118 case RPC_AUTH_UNIX:
3119 return nfs_ok;
3120 default:
3121 /*
3122 * GSS case: the spec doesn't allow us to return this
3123 * error. But it also doesn't allow us not to support
3124 * GSS.
3125 * I'd rather this fail hard than return some error the
3126 * client might think it can already handle:
3127 */
3128 return nfserr_encr_alg_unsupp;
3129 }
3130}
3131
Andy Adamson069b6ad2009-04-03 08:27:58 +03003132__be32
3133nfsd4_create_session(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003134 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003135{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003136 struct nfsd4_create_session *cr_ses = &u->create_session;
Jeff Layton363168b2009-08-14 12:57:56 -04003137 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003138 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003139 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003140 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003141 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003142 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003143 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003144 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003145
Mi Jinlonga62573d2011-03-23 17:57:07 +08003146 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3147 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003148 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3149 if (status)
3150 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003151 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04003152 if (status)
3153 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003154 status = check_backchannel_attrs(&cr_ses->back_channel);
3155 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08003156 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003157 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08003158 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003159 if (!new)
3160 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003161 conn = alloc_conn_from_crses(rqstp, cr_ses);
3162 if (!conn)
3163 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08003164
Jeff Laytond20c11d2014-07-30 08:27:07 -04003165 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003166 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003167 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003168 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003169
3170 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003171 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003172 if (!nfsd4_mach_creds_match(conf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003173 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003174 cs_slot = &conf->cl_cs_slot;
3175 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08003176 if (status) {
3177 if (status == nfserr_replay_cache)
3178 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003179 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003180 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003181 } else if (unconf) {
3182 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04003183 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003184 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003185 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003186 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04003187 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003188 if (!nfsd4_mach_creds_match(unconf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003189 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003190 cs_slot = &unconf->cl_cs_slot;
3191 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03003192 if (status) {
3193 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003194 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003195 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003196 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003197 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003198 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04003199 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003200 if (status) {
3201 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003202 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003203 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003204 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04003205 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003206 conf = unconf;
3207 } else {
3208 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003209 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003210 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003211 status = nfs_ok;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003212 /* Persistent sessions are not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003213 cr_ses->flags &= ~SESSION4_PERSIST;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003214 /* Upshifting from TCP to RDMA is not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003215 cr_ses->flags &= ~SESSION4_RDMA;
3216
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003217 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003218 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003219
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003220 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003221 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04003222 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04003223 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003224
Jeff Laytond20c11d2014-07-30 08:27:07 -04003225 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04003226 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003227 spin_unlock(&nn->client_lock);
3228 /* init connection and backchannel */
3229 nfsd4_init_conn(rqstp, conn, new);
3230 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003231 if (old)
3232 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003233 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003234out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003235 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003236 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003237 if (old)
3238 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003239out_free_session:
3240 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003241out_release_drc_mem:
3242 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04003243 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003244}
3245
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003246static __be32 nfsd4_map_bcts_dir(u32 *dir)
3247{
3248 switch (*dir) {
3249 case NFS4_CDFC4_FORE:
3250 case NFS4_CDFC4_BACK:
3251 return nfs_ok;
3252 case NFS4_CDFC4_FORE_OR_BOTH:
3253 case NFS4_CDFC4_BACK_OR_BOTH:
3254 *dir = NFS4_CDFC4_BOTH;
3255 return nfs_ok;
3256 };
3257 return nfserr_inval;
3258}
3259
Christoph Hellwigeb698532017-05-08 20:58:35 +02003260__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3261 struct nfsd4_compound_state *cstate,
3262 union nfsd4_op_u *u)
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003263{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003264 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003265 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003266 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003267 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003268
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003269 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3270 if (status)
3271 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003272 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003273 session->se_cb_prog = bc->bc_cb_program;
3274 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003275 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003276
3277 nfsd4_probe_callback(session->se_client);
3278
3279 return nfs_ok;
3280}
3281
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003282__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3283 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003284 union nfsd4_op_u *u)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003285{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003286 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003287 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003288 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003289 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003290 struct net *net = SVC_NET(rqstp);
3291 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003292
3293 if (!nfsd4_last_compound_op(rqstp))
3294 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003295 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003296 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003297 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003298 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003299 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003300 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003301 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003302 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003303 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003304 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003305 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003306 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003307 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003308 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003309 goto out;
3310 nfsd4_init_conn(rqstp, conn, session);
3311 status = nfs_ok;
3312out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003313 nfsd4_put_session(session);
3314out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003315 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003316}
3317
J. Bruce Fields665d5072018-04-11 16:53:36 -04003318static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003319{
J. Bruce Fields665d5072018-04-11 16:53:36 -04003320 if (!cstate->session)
Fengguang Wu51d87bc2018-03-22 13:37:20 +08003321 return false;
J. Bruce Fields665d5072018-04-11 16:53:36 -04003322 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003323}
3324
Andy Adamson069b6ad2009-04-03 08:27:58 +03003325__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003326nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3327 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003328{
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003329 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003330 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003331 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003332 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003333 struct net *net = SVC_NET(r);
3334 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003335
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003336 status = nfserr_not_only_op;
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003337 if (nfsd4_compound_in_session(cstate, sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04003338 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003339 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003340 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04003341 }
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003342 dump_sessionid(__func__, sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003343 spin_lock(&nn->client_lock);
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003344 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003345 if (!ses)
3346 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003347 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003348 if (!nfsd4_mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003349 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003350 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003351 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003352 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003353 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003354 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003355
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05003356 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04003357
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003358 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003359 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003360out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003361 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003362out_client_lock:
3363 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003364out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03003365 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003366}
3367
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003368static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003369{
3370 struct nfsd4_conn *c;
3371
3372 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003373 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04003374 return c;
3375 }
3376 }
3377 return NULL;
3378}
3379
J. Bruce Fields57266a62013-04-13 14:27:29 -04003380static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003381{
3382 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003383 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003384 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003385 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003386
3387 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003388 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003389 if (c)
3390 goto out_free;
3391 status = nfserr_conn_not_bound_to_session;
3392 if (clp->cl_mach_cred)
3393 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003394 __nfsd4_hash_conn(new, ses);
3395 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003396 ret = nfsd4_register_conn(new);
3397 if (ret)
3398 /* oops; xprt is already down: */
3399 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003400 return nfs_ok;
3401out_free:
3402 spin_unlock(&clp->cl_lock);
3403 free_conn(new);
3404 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003405}
3406
Mi Jinlong868b89c2011-04-27 09:09:58 +08003407static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3408{
3409 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3410
3411 return args->opcnt > session->se_fchannel.maxops;
3412}
3413
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003414static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3415 struct nfsd4_session *session)
3416{
3417 struct xdr_buf *xb = &rqstp->rq_arg;
3418
3419 return xb->len > session->se_fchannel.maxreq_sz;
3420}
3421
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003422static bool replay_matches_cache(struct svc_rqst *rqstp,
3423 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3424{
3425 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3426
3427 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3428 (bool)seq->cachethis)
3429 return false;
3430 /*
3431 * If there's an error than the reply can have fewer ops than
3432 * the call. But if we cached a reply with *more* ops than the
3433 * call you're sending us now, then this new call is clearly not
3434 * really a replay of the old one:
3435 */
3436 if (slot->sl_opcnt < argp->opcnt)
3437 return false;
3438 /* This is the only check explicitly called by spec: */
3439 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3440 return false;
3441 /*
3442 * There may be more comparisons we could actually do, but the
3443 * spec doesn't require us to catch every case where the calls
3444 * don't match (that would require caching the call as well as
3445 * the reply), so we don't bother.
3446 */
3447 return true;
3448}
3449
Andy Adamson069b6ad2009-04-03 08:27:58 +03003450__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003451nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3452 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003453{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003454 struct nfsd4_sequence *seq = &u->sequence;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003455 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003456 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003457 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003458 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003459 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003460 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003461 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003462 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003463 struct net *net = SVC_NET(rqstp);
3464 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003465
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003466 if (resp->opcnt != 1)
3467 return nfserr_sequence_pos;
3468
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003469 /*
3470 * Will be either used or freed by nfsd4_sequence_check_conn
3471 * below.
3472 */
3473 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3474 if (!conn)
3475 return nfserr_jukebox;
3476
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003477 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003478 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003479 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003480 goto out_no_session;
3481 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003482
Mi Jinlong868b89c2011-04-27 09:09:58 +08003483 status = nfserr_too_many_ops;
3484 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003485 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003486
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003487 status = nfserr_req_too_big;
3488 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003489 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003490
Benny Halevyb85d4c02009-04-03 08:28:08 +03003491 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003492 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003493 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003494
Andy Adamson557ce262009-08-28 08:45:04 -04003495 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003496 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3497
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003498 /* We do not negotiate the number of slots yet, so set the
3499 * maxslots to the session maxreqs which is used to encode
3500 * sr_highest_slotid and the sr_target_slot id to maxslots */
3501 seq->maxslots = session->se_fchannel.maxreqs;
3502
J. Bruce Fields73e79482012-02-13 16:39:00 -05003503 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3504 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003505 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003506 status = nfserr_seq_misordered;
3507 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003508 goto out_put_session;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003509 status = nfserr_seq_false_retry;
3510 if (!replay_matches_cache(rqstp, seq, slot))
3511 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003512 cstate->slot = slot;
3513 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003514 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003515 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003516 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003517 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003518 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003519 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003520 }
3521 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003522 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003523
J. Bruce Fields57266a62013-04-13 14:27:29 -04003524 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003525 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003526 if (status)
3527 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003528
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003529 buflen = (seq->cachethis) ?
3530 session->se_fchannel.maxresp_cached :
3531 session->se_fchannel.maxresp_sz;
3532 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3533 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003534 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003535 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003536 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003537
3538 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003539 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003540 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003541 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003542 if (seq->cachethis)
3543 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003544 else
3545 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003546
3547 cstate->slot = slot;
3548 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003549 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003550
Benny Halevyb85d4c02009-04-03 08:28:08 +03003551out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003552 switch (clp->cl_cb_state) {
3553 case NFSD4_CB_DOWN:
3554 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3555 break;
3556 case NFSD4_CB_FAULT:
3557 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3558 break;
3559 default:
3560 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003561 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003562 if (!list_empty(&clp->cl_revoked))
3563 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003564out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08003565 if (conn)
3566 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003567 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003568 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003569out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003570 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003571 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003572}
3573
Trond Myklebustb6076642014-06-30 11:48:35 -04003574void
3575nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3576{
3577 struct nfsd4_compound_state *cs = &resp->cstate;
3578
3579 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04003580 if (cs->status != nfserr_replay_cache) {
3581 nfsd4_store_cache_entry(resp);
3582 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3583 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003584 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04003585 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003586 } else if (cs->clp)
3587 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04003588}
3589
Mi Jinlong345c2842011-10-20 17:51:39 +08003590__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003591nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3592 struct nfsd4_compound_state *cstate,
3593 union nfsd4_op_u *u)
Mi Jinlong345c2842011-10-20 17:51:39 +08003594{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003595 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003596 struct nfs4_client *conf, *unconf;
3597 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003598 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003599 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08003600
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003601 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003602 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003603 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003604 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08003605
3606 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04003607 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08003608 status = nfserr_clientid_busy;
3609 goto out;
3610 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04003611 status = mark_client_expired_locked(conf);
3612 if (status)
3613 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003614 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08003615 } else if (unconf)
3616 clp = unconf;
3617 else {
3618 status = nfserr_stale_clientid;
3619 goto out;
3620 }
Andrew Elblededeb132016-06-15 12:52:08 -04003621 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003622 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003623 status = nfserr_wrong_cred;
3624 goto out;
3625 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003626 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003627out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003628 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003629 if (clp)
3630 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003631 return status;
3632}
3633
Andy Adamson069b6ad2009-04-03 08:27:58 +03003634__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003635nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3636 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003637{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003638 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003639 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003640
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003641 if (rc->rca_one_fs) {
3642 if (!cstate->current_fh.fh_dentry)
3643 return nfserr_nofilehandle;
3644 /*
3645 * We don't take advantage of the rca_one_fs case.
3646 * That's OK, it's optional, we can safely ignore it.
3647 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02003648 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003649 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003650
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003651 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04003652 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3653 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003654 goto out;
3655
3656 status = nfserr_stale_clientid;
3657 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003658 /*
3659 * The following error isn't really legal.
3660 * But we only get here if the client just explicitly
3661 * destroyed the client. Surely it no longer cares what
3662 * error it gets back on an operation for the dead
3663 * client.
3664 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003665 goto out;
3666
3667 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04003668 nfsd4_client_record_create(cstate->session->se_client);
Scott Mayhew362063a2019-03-26 18:06:28 -04003669 inc_reclaim_complete(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003670out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003671 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003672}
3673
3674__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003675nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003676 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003678 struct nfsd4_setclientid *setclid = &u->setclientid;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003679 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003681 struct nfs4_client *conf, *new;
3682 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003683 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003684 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3685
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003686 new = create_client(clname, rqstp, &clverifier);
3687 if (new == NULL)
3688 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003689 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003690 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003691 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04003692 if (conf && client_has_state(conf)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003693 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003695 if (clp_used_exchangeid(conf))
3696 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003697 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04003698 char addr_str[INET6_ADDRSTRLEN];
3699 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3700 sizeof(addr_str));
3701 dprintk("NFSD: setclientid: string in use by client "
3702 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 goto out;
3704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003706 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003707 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003708 unhash_client_locked(unconf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003709 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003710 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003712 gen_confirm(new, nn);
3713 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04003714 ;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003715 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003716 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003717 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3719 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3720 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003721 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 status = nfs_ok;
3723out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003724 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003725 if (new)
3726 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003727 if (unconf)
3728 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 return status;
3730}
3731
3732
Al Virob37ad282006-10-19 23:28:59 -07003733__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003734nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003735 struct nfsd4_compound_state *cstate,
3736 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003738 struct nfsd4_setclientid_confirm *setclientid_confirm =
3739 &u->setclientid_confirm;
NeilBrown21ab45a2005-06-23 22:04:14 -07003740 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003741 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3743 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003744 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003745 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003747 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003749
Jeff Laytond20c11d2014-07-30 08:27:07 -04003750 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003751 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003752 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003753 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003754 * We try hard to give out unique clientid's, so if we get an
3755 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003756 * the client may be buggy; this should never happen.
3757 *
3758 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003759 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003760 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04003761 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3762 goto out;
3763 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3764 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003765 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003766 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003767 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3768 /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 status = nfs_ok;
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003770 } else /* case 4: client hasn't noticed we rebooted yet? */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003771 status = nfserr_stale_clientid;
3772 goto out;
3773 }
3774 status = nfs_ok;
3775 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003776 old = unconf;
3777 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003778 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003779 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003780 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3781 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04003782 status = nfserr_clid_inuse;
3783 if (client_has_state(old)
3784 && !same_creds(&unconf->cl_cred,
3785 &old->cl_cred))
3786 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003787 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003788 if (status) {
3789 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003790 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003791 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003792 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003793 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003794 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003795 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003796 get_client_locked(conf);
3797 spin_unlock(&nn->client_lock);
3798 nfsd4_probe_callback(conf);
3799 spin_lock(&nn->client_lock);
3800 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003802 spin_unlock(&nn->client_lock);
3803 if (old)
3804 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 return status;
3806}
3807
J. Bruce Fields32513b42011-10-13 16:00:16 -04003808static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003810 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3811}
3812
3813/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003814static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3815 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003816{
Trond Myklebust950e0112014-06-30 11:48:31 -04003817 lockdep_assert_held(&state_lock);
3818
Elena Reshetova818a34e2017-10-20 12:53:30 +03003819 refcount_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003820 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003821 INIT_LIST_HEAD(&fp->fi_stateids);
3822 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02003823 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003824 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003825 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003826 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003827 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003828 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3829 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003830#ifdef CONFIG_NFSD_PNFS
3831 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02003832 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003833#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04003834 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835}
3836
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003837void
NeilBrowne60d4392005-06-23 22:03:01 -07003838nfsd4_free_slabs(void)
3839{
Jeff Layton9258a2d2018-03-16 09:47:22 -04003840 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003841 kmem_cache_destroy(openowner_slab);
3842 kmem_cache_destroy(lockowner_slab);
3843 kmem_cache_destroy(file_slab);
3844 kmem_cache_destroy(stateid_slab);
3845 kmem_cache_destroy(deleg_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04003846 kmem_cache_destroy(odstate_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003847}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848
Bryan Schumaker72083392011-11-01 15:24:59 -04003849int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850nfsd4_init_slabs(void)
3851{
Jeff Layton9258a2d2018-03-16 09:47:22 -04003852 client_slab = kmem_cache_create("nfsd4_clients",
3853 sizeof(struct nfs4_client), 0, 0, NULL);
3854 if (client_slab == NULL)
3855 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003856 openowner_slab = kmem_cache_create("nfsd4_openowners",
3857 sizeof(struct nfs4_openowner), 0, 0, NULL);
3858 if (openowner_slab == NULL)
Jeff Layton9258a2d2018-03-16 09:47:22 -04003859 goto out_free_client_slab;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003860 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003861 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003862 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003863 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003864 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003865 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003866 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003867 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003868 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003869 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003870 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003871 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003872 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003873 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003874 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003875 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003876 odstate_slab = kmem_cache_create("nfsd4_odstate",
3877 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3878 if (odstate_slab == NULL)
3879 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003881
Sachin Bhamare8287f002015-04-27 14:50:14 +02003882out_free_deleg_slab:
3883 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003884out_free_stateid_slab:
3885 kmem_cache_destroy(stateid_slab);
3886out_free_file_slab:
3887 kmem_cache_destroy(file_slab);
3888out_free_lockowner_slab:
3889 kmem_cache_destroy(lockowner_slab);
3890out_free_openowner_slab:
3891 kmem_cache_destroy(openowner_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04003892out_free_client_slab:
3893 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003894out:
NeilBrowne60d4392005-06-23 22:03:01 -07003895 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3896 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897}
3898
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003899static void init_nfs4_replay(struct nfs4_replay *rp)
3900{
3901 rp->rp_status = nfserr_serverfault;
3902 rp->rp_buflen = 0;
3903 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003904 mutex_init(&rp->rp_mutex);
3905}
3906
3907static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3908 struct nfs4_stateowner *so)
3909{
3910 if (!nfsd4_has_session(cstate)) {
3911 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003912 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04003913 }
3914}
3915
3916void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3917{
3918 struct nfs4_stateowner *so = cstate->replay_owner;
3919
3920 if (so != NULL) {
3921 cstate->replay_owner = NULL;
3922 mutex_unlock(&so->so_replay.rp_mutex);
3923 nfs4_put_stateowner(so);
3924 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003925}
3926
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003927static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928{
3929 struct nfs4_stateowner *sop;
3930
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003931 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003932 if (!sop)
3933 return NULL;
3934
3935 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3936 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003937 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003938 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003940 sop->so_owner.len = owner->len;
3941
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003942 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003943 sop->so_client = clp;
3944 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003945 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003946 return sop;
3947}
3948
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003949static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003950{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003951 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003952
Trond Myklebustd4f04892014-07-29 21:34:36 -04003953 list_add(&oo->oo_owner.so_strhash,
3954 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003955 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956}
3957
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003958static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3959{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003960 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003961}
3962
Jeff Layton6b180f02014-07-29 21:34:26 -04003963static void nfs4_free_openowner(struct nfs4_stateowner *so)
3964{
3965 struct nfs4_openowner *oo = openowner(so);
3966
3967 kmem_cache_free(openowner_slab, oo);
3968}
3969
3970static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003971 .so_unhash = nfs4_unhash_openowner,
3972 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003973};
3974
Andrew Elble7fc05642015-11-05 20:42:43 -05003975static struct nfs4_ol_stateid *
3976nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3977{
3978 struct nfs4_ol_stateid *local, *ret = NULL;
3979 struct nfs4_openowner *oo = open->op_openowner;
3980
3981 lockdep_assert_held(&fp->fi_lock);
3982
3983 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3984 /* ignore lock owners */
3985 if (local->st_stateowner->so_is_open_owner == 0)
3986 continue;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003987 if (local->st_stateowner != &oo->oo_owner)
3988 continue;
3989 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
Andrew Elble7fc05642015-11-05 20:42:43 -05003990 ret = local;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03003991 refcount_inc(&ret->st_stid.sc_count);
Andrew Elble7fc05642015-11-05 20:42:43 -05003992 break;
3993 }
3994 }
3995 return ret;
3996}
3997
Trond Myklebust15ca08d2017-11-03 08:00:10 -04003998static __be32
3999nfsd4_verify_open_stid(struct nfs4_stid *s)
4000{
4001 __be32 ret = nfs_ok;
4002
4003 switch (s->sc_type) {
4004 default:
4005 break;
Trond Myklebust4f176412018-01-12 17:42:30 -05004006 case 0:
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004007 case NFS4_CLOSED_STID:
4008 case NFS4_CLOSED_DELEG_STID:
4009 ret = nfserr_bad_stateid;
4010 break;
4011 case NFS4_REVOKED_DELEG_STID:
4012 ret = nfserr_deleg_revoked;
4013 }
4014 return ret;
4015}
4016
4017/* Lock the stateid st_mutex, and deal with races with CLOSE */
4018static __be32
4019nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4020{
4021 __be32 ret;
4022
Andrew Elble4f34bd02017-11-08 17:29:51 -05004023 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004024 ret = nfsd4_verify_open_stid(&stp->st_stid);
4025 if (ret != nfs_ok)
4026 mutex_unlock(&stp->st_mutex);
4027 return ret;
4028}
4029
4030static struct nfs4_ol_stateid *
4031nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4032{
4033 struct nfs4_ol_stateid *stp;
4034 for (;;) {
4035 spin_lock(&fp->fi_lock);
4036 stp = nfsd4_find_existing_open(fp, open);
4037 spin_unlock(&fp->fi_lock);
4038 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4039 break;
4040 nfs4_put_stid(&stp->st_stid);
4041 }
4042 return stp;
4043}
4044
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004045static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04004046alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004047 struct nfsd4_compound_state *cstate)
4048{
Trond Myklebust13d6f662014-06-30 11:48:45 -04004049 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004050 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004052 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4053 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04004055 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004056 oo->oo_owner.so_is_open_owner = 1;
4057 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004058 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004059 if (nfsd4_has_session(cstate))
4060 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004061 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004062 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004063 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04004064 spin_lock(&clp->cl_lock);
4065 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004066 if (ret == NULL) {
4067 hash_openowner(oo, clp, strhashval);
4068 ret = oo;
4069 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08004070 nfs4_free_stateowner(&oo->oo_owner);
4071
Trond Myklebustd4f04892014-07-29 21:34:36 -04004072 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04004073 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074}
4075
Andrew Elble7fc05642015-11-05 20:42:43 -05004076static struct nfs4_ol_stateid *
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004077init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
Andrew Elble7fc05642015-11-05 20:42:43 -05004078{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
Andrew Elble7fc05642015-11-05 20:42:43 -05004080 struct nfs4_openowner *oo = open->op_openowner;
4081 struct nfs4_ol_stateid *retstp = NULL;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004082 struct nfs4_ol_stateid *stp;
Andrew Elble7fc05642015-11-05 20:42:43 -05004083
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004084 stp = open->op_stp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004085 /* We are moving these outside of the spinlocks to avoid the warnings */
4086 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05004087 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004088
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004089retry:
Andrew Elble7fc05642015-11-05 20:42:43 -05004090 spin_lock(&oo->oo_owner.so_client->cl_lock);
4091 spin_lock(&fp->fi_lock);
4092
4093 retstp = nfsd4_find_existing_open(fp, open);
4094 if (retstp)
4095 goto out_unlock;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004096
4097 open->op_stp = NULL;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004098 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004099 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004100 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004101 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07004102 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004103 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 stp->st_access_bmap = 0;
4105 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07004106 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04004107 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004108 list_add(&stp->st_perfile, &fp->fi_stateids);
Andrew Elble7fc05642015-11-05 20:42:43 -05004109
4110out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04004111 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004112 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004113 if (retstp) {
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004114 /* Handle races with CLOSE */
4115 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4116 nfs4_put_stid(&retstp->st_stid);
4117 goto retry;
4118 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004119 /* To keep mutex tracking happy */
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004120 mutex_unlock(&stp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004121 stp = retstp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004122 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004123 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124}
4125
Jeff Laytond3134b12014-07-29 21:34:32 -04004126/*
4127 * In the 4.0 case we need to keep the owners around a little while to handle
4128 * CLOSE replay. We still do need to release any file access that is held by
4129 * them before returning however.
4130 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131static void
Jeff Laytond3134b12014-07-29 21:34:32 -04004132move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133{
Jeff Layton217526e2014-07-30 08:27:11 -04004134 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04004135 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4136 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4137 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004138
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004139 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140
Jeff Laytonb401be222014-07-29 21:34:33 -04004141 /*
4142 * We know that we hold one reference via nfsd4_close, and another
4143 * "persistent" reference for the client. If the refcount is higher
4144 * than 2, then there are still calls in progress that are using this
4145 * stateid. We can't put the sc_file reference until they are finished.
4146 * Wait for the refcount to drop to 2. Since it has been unhashed,
4147 * there should be no danger of the refcount going back up again at
4148 * this point.
4149 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004150 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
Jeff Laytonb401be222014-07-29 21:34:33 -04004151
Jeff Laytond3134b12014-07-29 21:34:32 -04004152 release_all_access(s);
4153 if (s->st_stid.sc_file) {
4154 put_nfs4_file(s->st_stid.sc_file);
4155 s->st_stid.sc_file = NULL;
4156 }
Jeff Layton217526e2014-07-30 08:27:11 -04004157
4158 spin_lock(&nn->client_lock);
4159 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004160 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004161 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004162 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04004163 spin_unlock(&nn->client_lock);
4164 if (last)
4165 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166}
4167
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168/* search file_hashtbl[] for file */
4169static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04004170find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 struct nfs4_file *fp;
4173
Jeff Layton5b095e92014-10-23 08:01:02 -04004174 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02004175 if (fh_match(&fp->fi_fhandle, fh)) {
Elena Reshetova818a34e2017-10-20 12:53:30 +03004176 if (refcount_inc_not_zero(&fp->fi_ref))
Jeff Layton5b095e92014-10-23 08:01:02 -04004177 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07004178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 }
4180 return NULL;
4181}
4182
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02004183struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04004184find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004185{
4186 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004187 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04004188
Jeff Layton5b095e92014-10-23 08:01:02 -04004189 rcu_read_lock();
4190 fp = find_file_locked(fh, hashval);
4191 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04004192 return fp;
4193}
4194
4195static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004196find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004197{
4198 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004199 unsigned int hashval = file_hashval(fh);
4200
4201 rcu_read_lock();
4202 fp = find_file_locked(fh, hashval);
4203 rcu_read_unlock();
4204 if (fp)
4205 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04004206
4207 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04004208 fp = find_file_locked(fh, hashval);
4209 if (likely(fp == NULL)) {
4210 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04004211 fp = new;
4212 }
4213 spin_unlock(&state_lock);
4214
4215 return fp;
4216}
4217
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04004218/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 * Called to check deny when READ with all zero stateid or
4220 * WRITE with all zero or all one stateid
4221 */
Al Virob37ad282006-10-19 23:28:59 -07004222static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4224{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004226 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
Trond Myklebustca943212014-07-23 16:17:39 -04004228 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07004229 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004230 return ret;
4231 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04004232 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004233 if (fp->fi_share_deny & deny_type)
4234 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04004235 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07004236 put_nfs4_file(fp);
4237 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238}
4239
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004240static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004242 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04004243 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4244 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004245
Trond Myklebust11b91642014-07-29 21:34:08 -04004246 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04004247
Jeff Layton02e12152014-07-16 10:31:57 -04004248 /*
4249 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04004250 * already holding inode->i_lock.
4251 *
Jeff Laytondff13992014-07-08 14:02:49 -04004252 * If the dl_time != 0, then we know that it has already been
4253 * queued for a lease break. Don't queue it again.
4254 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04004255 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04004256 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04004257 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04004258 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04004259 }
Jeff Layton02e12152014-07-16 10:31:57 -04004260 spin_unlock(&state_lock);
4261}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004263static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4264 struct rpc_task *task)
4265{
4266 struct nfs4_delegation *dp = cb_to_delegation(cb);
4267
Andrew Elblea4579742015-08-31 12:06:41 -04004268 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
4269 return 1;
4270
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004271 switch (task->tk_status) {
4272 case 0:
4273 return 1;
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004274 case -NFS4ERR_DELAY:
4275 rpc_delay(task, 2 * HZ);
4276 return 0;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004277 case -EBADHANDLE:
4278 case -NFS4ERR_BAD_STATEID:
4279 /*
4280 * Race: client probably got cb_recall before open reply
4281 * granting delegation.
4282 */
4283 if (dp->dl_retries--) {
4284 rpc_delay(task, 2 * HZ);
4285 return 0;
4286 }
4287 /*FALLTHRU*/
4288 default:
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004289 return 1;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004290 }
4291}
4292
4293static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4294{
4295 struct nfs4_delegation *dp = cb_to_delegation(cb);
4296
4297 nfs4_put_stid(&dp->dl_stid);
4298}
4299
Julia Lawallc4cb8972015-11-21 22:57:39 +01004300static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004301 .prepare = nfsd4_cb_recall_prepare,
4302 .done = nfsd4_cb_recall_done,
4303 .release = nfsd4_cb_recall_release,
4304};
4305
Jeff Layton02e12152014-07-16 10:31:57 -04004306static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4307{
4308 /*
4309 * We're assuming the state code never drops its reference
4310 * without first removing the lease. Since we're in this lease
J. Bruce Fields4a269efb2018-04-13 17:34:35 -04004311 * callback (and since the lease code is serialized by the
4312 * i_lock) we know the server hasn't removed the lease yet, and
4313 * we know it's safe to take a reference.
Jeff Layton02e12152014-07-16 10:31:57 -04004314 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004315 refcount_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02004316 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004317}
4318
Jeff Layton1c8c6012013-06-21 08:58:15 -04004319/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004320static bool
4321nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004322{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004323 bool ret = false;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004324 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4325 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004326
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004327 /*
4328 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004329 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004330 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004331 */
4332 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333
Jeff Layton02e12152014-07-16 10:31:57 -04004334 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004335 fp->fi_had_conflict = true;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004336 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04004337 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004338 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339}
4340
Jeff Laytonc45198e2014-09-01 07:12:07 -04004341static int
Jeff Layton7448cc32015-01-16 15:05:57 -05004342nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4343 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344{
4345 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04004346 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 else
4348 return -EAGAIN;
4349}
4350
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004351static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04004352 .lm_break = nfsd_break_deleg_cb,
4353 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354};
4355
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004356static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4357{
4358 if (nfsd4_has_session(cstate))
4359 return nfs_ok;
4360 if (seqid == so->so_seqid - 1)
4361 return nfserr_replay_me;
4362 if (seqid == so->so_seqid)
4363 return nfs_ok;
4364 return nfserr_bad_seqid;
4365}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366
Jeff Layton4b24ca72014-06-30 11:48:44 -04004367static __be32 lookup_clientid(clientid_t *clid,
4368 struct nfsd4_compound_state *cstate,
4369 struct nfsd_net *nn)
4370{
4371 struct nfs4_client *found;
4372
4373 if (cstate->clp) {
4374 found = cstate->clp;
4375 if (!same_clid(&found->cl_clientid, clid))
4376 return nfserr_stale_clientid;
4377 return nfs_ok;
4378 }
4379
4380 if (STALE_CLIENTID(clid, nn))
4381 return nfserr_stale_clientid;
4382
4383 /*
4384 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4385 * cached already then we know this is for is for v4.0 and "sessions"
4386 * will be false.
4387 */
4388 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004389 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004390 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004391 if (!found) {
4392 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004393 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04004394 }
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04004395 atomic_inc(&found->cl_rpc_users);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004396 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004397
4398 /* Cache the nfs4_client in cstate! */
4399 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004400 return nfs_ok;
4401}
4402
Al Virob37ad282006-10-19 23:28:59 -07004403__be32
Andy Adamson66689582009-04-03 08:28:45 +03004404nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004405 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 clientid_t *clientid = &open->op_clientid;
4408 struct nfs4_client *clp = NULL;
4409 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004410 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004411 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004413 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004415 /*
4416 * In case we need it later, after we've already created the
4417 * file and don't want to risk a further failure:
4418 */
4419 open->op_file = nfsd4_alloc_file();
4420 if (open->op_file == NULL)
4421 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
Trond Myklebust2d91e892014-06-30 11:48:46 -04004423 status = lookup_clientid(clientid, cstate, nn);
4424 if (status)
4425 return status;
4426 clp = cstate->clp;
4427
Trond Myklebustd4f04892014-07-29 21:34:36 -04004428 strhashval = ownerstr_hashval(&open->op_owner);
4429 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004430 open->op_openowner = oo;
4431 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004432 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004434 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004435 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004436 release_openowner(oo);
4437 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004438 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004439 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004440 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4441 if (status)
4442 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004443 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004444new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04004445 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004446 if (oo == NULL)
4447 return nfserr_jukebox;
4448 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004449alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04004450 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004451 if (!open->op_stp)
4452 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004453
4454 if (nfsd4_has_session(cstate) &&
4455 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4456 open->op_odstate = alloc_clnt_odstate(clp);
4457 if (!open->op_odstate)
4458 return nfserr_jukebox;
4459 }
4460
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004461 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462}
4463
Al Virob37ad282006-10-19 23:28:59 -07004464static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07004465nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4466{
4467 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4468 return nfserr_openmode;
4469 else
4470 return nfs_ok;
4471}
4472
Daniel Mackc47d8322011-05-16 16:38:14 +02004473static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04004474{
J. Bruce Fields24a01112010-05-18 20:01:35 -04004475 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4476}
4477
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004478static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004479{
4480 struct nfs4_stid *ret;
4481
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004482 ret = find_stateid_by_type(cl, s,
4483 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004484 if (!ret)
4485 return NULL;
4486 return delegstateid(ret);
4487}
4488
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004489static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4490{
4491 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4492 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4493}
4494
Al Virob37ad282006-10-19 23:28:59 -07004495static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04004496nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07004497 struct nfs4_delegation **dp)
4498{
4499 int flags;
Al Virob37ad282006-10-19 23:28:59 -07004500 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004501 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07004502
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004503 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4504 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07004505 goto out;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004506 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4507 nfs4_put_stid(&deleg->dl_stid);
4508 if (cl->cl_minorversion)
4509 status = nfserr_deleg_revoked;
4510 goto out;
4511 }
J. Bruce Fields24a01112010-05-18 20:01:35 -04004512 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004513 status = nfs4_check_delegmode(deleg, flags);
4514 if (status) {
4515 nfs4_put_stid(&deleg->dl_stid);
4516 goto out;
4517 }
4518 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004519out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004520 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004521 return nfs_ok;
4522 if (status)
4523 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004524 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004525 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07004526}
4527
J. Bruce Fields21fb4012010-07-28 12:21:23 -04004528static inline int nfs4_access_to_access(u32 nfs4_access)
4529{
4530 int flags = 0;
4531
4532 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4533 flags |= NFSD_MAY_READ;
4534 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4535 flags |= NFSD_MAY_WRITE;
4536 return flags;
4537}
4538
Al Virob37ad282006-10-19 23:28:59 -07004539static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4541 struct nfsd4_open *open)
4542{
4543 struct iattr iattr = {
4544 .ia_valid = ATTR_SIZE,
4545 .ia_size = 0,
4546 };
4547 if (!open->op_truncate)
4548 return 0;
4549 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08004550 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
4552}
4553
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004554static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004555 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4556 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004557{
Trond Myklebustde186432014-07-10 14:07:26 -04004558 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004559 __be32 status;
4560 int oflag = nfs4_access_to_omode(open->op_share_access);
4561 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004562 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004563
Trond Myklebustde186432014-07-10 14:07:26 -04004564 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004565
4566 /*
4567 * Are we trying to set a deny mode that would conflict with
4568 * current access?
4569 */
4570 status = nfs4_file_check_deny(fp, open->op_share_deny);
4571 if (status != nfs_ok) {
4572 spin_unlock(&fp->fi_lock);
4573 goto out;
4574 }
4575
4576 /* set access to the file */
4577 status = nfs4_file_get_access(fp, open->op_share_access);
4578 if (status != nfs_ok) {
4579 spin_unlock(&fp->fi_lock);
4580 goto out;
4581 }
4582
4583 /* Set access bits in stateid */
4584 old_access_bmap = stp->st_access_bmap;
4585 set_access(open->op_share_access, stp);
4586
4587 /* Set new deny mask */
4588 old_deny_bmap = stp->st_deny_bmap;
4589 set_deny(open->op_share_deny, stp);
4590 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4591
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004592 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04004593 spin_unlock(&fp->fi_lock);
4594 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004595 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004596 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04004597 spin_lock(&fp->fi_lock);
4598 if (!fp->fi_fds[oflag]) {
4599 fp->fi_fds[oflag] = filp;
4600 filp = NULL;
4601 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004602 }
Trond Myklebustde186432014-07-10 14:07:26 -04004603 spin_unlock(&fp->fi_lock);
4604 if (filp)
4605 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004606
4607 status = nfsd4_truncate(rqstp, cur_fh, open);
4608 if (status)
4609 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004610out:
4611 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004612out_put_access:
4613 stp->st_access_bmap = old_access_bmap;
4614 nfs4_file_put_access(fp, open->op_share_access);
4615 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4616 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004617}
4618
Al Virob37ad282006-10-19 23:28:59 -07004619static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004620nfs4_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 -07004621{
Al Virob37ad282006-10-19 23:28:59 -07004622 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02004623 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004625 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004626 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004627
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004628 /* test and set deny mode */
4629 spin_lock(&fp->fi_lock);
4630 status = nfs4_file_check_deny(fp, open->op_share_deny);
4631 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004632 set_deny(open->op_share_deny, stp);
4633 fp->fi_share_deny |=
4634 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4635 }
4636 spin_unlock(&fp->fi_lock);
4637
4638 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004641 status = nfsd4_truncate(rqstp, cur_fh, open);
4642 if (status != nfs_ok)
4643 reset_union_bmap_deny(old_deny_bmap, stp);
4644 return status;
4645}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004647/* Should we give out recallable state?: */
4648static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4649{
4650 if (clp->cl_cb_state == NFSD4_CB_UP)
4651 return true;
4652 /*
4653 * In the sessions case, since we don't have to establish a
4654 * separate connection for callbacks, we assume it's OK
4655 * until we hear otherwise:
4656 */
4657 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4658}
4659
J. Bruce Fields653e5142018-02-15 22:08:45 -05004660static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4661 int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004662{
4663 struct file_lock *fl;
4664
4665 fl = locks_alloc_lock();
4666 if (!fl)
4667 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004668 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04004669 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004670 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4671 fl->fl_end = OFFSET_MAX;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004672 fl->fl_owner = (fl_owner_t)dp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004673 fl->fl_pid = current->tgid;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004674 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004675 return fl;
4676}
4677
Jeff Layton0b266932014-07-25 07:34:25 -04004678static struct nfs4_delegation *
4679nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004680 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004681{
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004682 int status = 0;
Jeff Layton0b266932014-07-25 07:34:25 -04004683 struct nfs4_delegation *dp;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004684 struct file *filp;
4685 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04004686
J. Bruce Fields353601e2018-02-16 14:29:42 -05004687 /*
4688 * The fi_had_conflict and nfs_get_existing_delegation checks
4689 * here are just optimizations; we'll need to recheck them at
4690 * the end:
4691 */
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004692 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004693 return ERR_PTR(-EAGAIN);
4694
J. Bruce Fields353601e2018-02-16 14:29:42 -05004695 filp = find_readable_file(fp);
4696 if (!filp) {
4697 /* We should always have a readable file here */
4698 WARN_ON_ONCE(1);
4699 return ERR_PTR(-EBADF);
4700 }
Andrew Elble34ed9872015-10-15 12:07:28 -04004701 spin_lock(&state_lock);
4702 spin_lock(&fp->fi_lock);
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004703 if (nfs4_delegation_exists(clp, fp))
4704 status = -EAGAIN;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004705 else if (!fp->fi_deleg_file) {
4706 fp->fi_deleg_file = filp;
4707 /* increment early to prevent fi_deleg_file from being
4708 * cleared */
4709 fp->fi_delegees = 1;
4710 filp = NULL;
4711 } else
4712 fp->fi_delegees++;
4713 spin_unlock(&fp->fi_lock);
4714 spin_unlock(&state_lock);
4715 if (filp)
4716 fput(filp);
4717 if (status)
4718 return ERR_PTR(status);
4719
4720 status = -ENOMEM;
4721 dp = alloc_init_deleg(clp, fp, fh, odstate);
4722 if (!dp)
4723 goto out_delegees;
4724
4725 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
4726 if (!fl)
Andrew Elblebd8d7252018-10-05 09:32:08 -04004727 goto out_clnt_odstate;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004728
4729 status = vfs_setlease(fp->fi_deleg_file, fl->fl_type, &fl, NULL);
4730 if (fl)
4731 locks_free_lock(fl);
4732 if (status)
4733 goto out_clnt_odstate;
4734
4735 spin_lock(&state_lock);
4736 spin_lock(&fp->fi_lock);
4737 if (fp->fi_had_conflict)
4738 status = -EAGAIN;
4739 else
4740 status = hash_delegation_locked(dp, fp);
Andrew Elble34ed9872015-10-15 12:07:28 -04004741 spin_unlock(&fp->fi_lock);
4742 spin_unlock(&state_lock);
4743
4744 if (status)
Andrew Elble692ad282018-04-18 17:04:37 -04004745 goto out_unlock;
4746
Jeff Layton0b266932014-07-25 07:34:25 -04004747 return dp;
Andrew Elble692ad282018-04-18 17:04:37 -04004748out_unlock:
4749 vfs_setlease(fp->fi_deleg_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004750out_clnt_odstate:
4751 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004752 nfs4_put_stid(&dp->dl_stid);
4753out_delegees:
4754 put_deleg_file(fp);
4755 return ERR_PTR(status);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004756}
4757
Benny Halevy4aa89132012-02-21 14:16:44 -08004758static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4759{
4760 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4761 if (status == -EAGAIN)
4762 open->op_why_no_deleg = WND4_CONTENTION;
4763 else {
4764 open->op_why_no_deleg = WND4_RESOURCE;
4765 switch (open->op_deleg_want) {
4766 case NFS4_SHARE_WANT_READ_DELEG:
4767 case NFS4_SHARE_WANT_WRITE_DELEG:
4768 case NFS4_SHARE_WANT_ANY_DELEG:
4769 break;
4770 case NFS4_SHARE_WANT_CANCEL:
4771 open->op_why_no_deleg = WND4_CANCELLED;
4772 break;
4773 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004774 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08004775 }
4776 }
4777}
4778
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779/*
4780 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04004781 *
4782 * Note we don't support write delegations, and won't until the vfs has
4783 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 */
4785static void
Jeff Layton4cf59222014-07-25 07:34:24 -04004786nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4787 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788{
4789 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04004790 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4791 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004792 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004793 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004795 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07004796 open->op_recall = 0;
4797 switch (open->op_claim_type) {
4798 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05004799 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07004800 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004801 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4802 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004803 break;
4804 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00004805 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004806 /*
4807 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004808 * had the chance to reclaim theirs, *and* until
4809 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004810 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004811 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004812 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004813 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004814 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04004815 /*
4816 * Also, if the file was opened for write or
4817 * create, there's a good chance the client's
4818 * about to write to it, resulting in an
4819 * immediate recall (since we don't support
4820 * write delegations):
4821 */
NeilBrown7b190fe2005-06-23 22:03:23 -07004822 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04004823 goto out_no_deleg;
4824 if (open->op_create == NFS4_OPEN_CREATE)
4825 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004826 break;
4827 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004828 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004829 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004830 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004831 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004832 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004834 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004836 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004837 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04004838 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04004839 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004840 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004841out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004842 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4843 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004844 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04004845 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004846 open->op_recall = 1;
4847 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004848
4849 /* 4.1 client asking for a delegation? */
4850 if (open->op_deleg_want)
4851 nfsd4_open_deleg_none_ext(open, status);
4852 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853}
4854
Benny Halevye27f49c2012-02-21 14:16:54 -08004855static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4856 struct nfs4_delegation *dp)
4857{
4858 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4859 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4860 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4861 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4862 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4863 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4864 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4865 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4866 }
4867 /* Otherwise the client must be confused wanting a delegation
4868 * it already has, therefore we don't return
4869 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4870 */
4871}
4872
Al Virob37ad282006-10-19 23:28:59 -07004873__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4875{
Andy Adamson66689582009-04-03 08:28:45 +03004876 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004877 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004879 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004880 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004881 __be32 status;
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004882 bool new_stp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 /*
4885 * Lookup file; if found, lookup stateid and check open request,
4886 * and check for delegations in the process of being recalled.
4887 * If not found, create the nfs4_file struct
4888 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004889 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004890 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004891 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004892 if (status)
4893 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004894 stp = nfsd4_find_and_lock_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004896 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004897 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004898 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004899 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 }
4901
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004902 if (!stp) {
4903 stp = init_open_stateid(fp, open);
4904 if (!open->op_stp)
4905 new_stp = true;
4906 }
4907
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 /*
4909 * OPEN the file, or upgrade an existing OPEN.
4910 * If truncate fails, the OPEN fails.
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004911 *
4912 * stp is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 */
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004914 if (!new_stp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004916 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04004917 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004918 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04004920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 } else {
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004922 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4923 if (status) {
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004924 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004925 release_open_stateid(stp);
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004926 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004927 goto out;
4928 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004929
4930 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4931 open->op_odstate);
4932 if (stp->st_clnt_odstate == open->op_odstate)
4933 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 }
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004935
Jeff Layton9767feb2015-10-01 09:05:50 -04004936 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004937 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938
Benny Halevyd24433c2012-02-16 20:57:17 +02004939 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004940 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4941 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4942 open->op_why_no_deleg = WND4_NOT_WANTED;
4943 goto nodeleg;
4944 }
4945 }
4946
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 /*
4948 * Attempt to hand out a delegation. No error return, because the
4949 * OPEN succeeds even if we fail.
4950 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004951 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004952nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 status = nfs_ok;
4954
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004955 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004956 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004958 /* 4.1 client trying to upgrade/downgrade delegation? */
4959 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004960 open->op_deleg_want)
4961 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004962
NeilBrown13cd2182005-06-23 22:03:10 -07004963 if (fp)
4964 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004965 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08004966 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 /*
4968 * To finish the open response, we just need to set the rflags.
4969 */
4970 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Jeff Layton19e4c342016-09-16 16:28:27 -04004971 if (nfsd4_has_session(&resp->cstate))
4972 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
4973 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Jeff Layton19e4c342016-09-16 16:28:27 -04004975
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004976 if (dp)
4977 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004978 if (stp)
4979 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980
4981 return status;
4982}
4983
Jeff Layton58fb12e2014-07-29 21:34:27 -04004984void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04004985 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004986{
4987 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004988 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004989
Jeff Laytond3134b12014-07-29 21:34:32 -04004990 nfsd4_cstate_assign_replay(cstate, so);
4991 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004992 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004993 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04004994 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004995 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004996 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004997 if (open->op_odstate)
4998 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004999}
5000
Al Virob37ad282006-10-19 23:28:59 -07005001__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005002nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005003 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005005 clientid_t *clid = &u->renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07005007 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005008 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 dprintk("process_renew(%08x/%08x): starting\n",
5011 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04005012 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005013 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005015 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07005017 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04005018 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 goto out;
5020 status = nfs_ok;
5021out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 return status;
5023}
5024
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04005025void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005026nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07005027{
Jeff Layton33dcc482012-04-10 11:08:48 -04005028 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005029 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04005030 return;
5031
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005032 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04005033 /*
5034 * If the server goes down again right now, an NFSv4
5035 * client will still be allowed to reclaim after it comes back up,
5036 * even if it hasn't yet had a chance to reclaim state this time.
5037 *
5038 */
Jeff Layton919b8042014-09-12 16:40:20 -04005039 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005040 /*
5041 * At this point, NFSv4 clients can still reclaim. But if the
5042 * server crashes, any that have not yet reclaimed will be out
5043 * of luck on the next boot.
5044 *
5045 * (NFSv4.1+ clients are considered to have reclaimed once they
5046 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5047 * have reclaimed after their first OPEN.)
5048 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005049 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005050 /*
5051 * At this point, and once lockd and/or any other containers
5052 * exit their grace period, further reclaims will fail and
5053 * regular locking can resume.
5054 */
NeilBrowna76b4312005-06-23 22:04:01 -07005055}
5056
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005057/*
5058 * If we've waited a lease period but there are still clients trying to
5059 * reclaim, wait a little longer to give them a chance to finish.
5060 */
5061static bool clients_still_reclaiming(struct nfsd_net *nn)
5062{
5063 unsigned long now = get_seconds();
5064 unsigned long double_grace_period_end = nn->boot_time +
5065 2 * nn->nfsd4_lease;
5066
Scott Mayhew362063a2019-03-26 18:06:28 -04005067 if (nn->track_reclaim_completes &&
5068 atomic_read(&nn->nr_reclaim_complete) ==
5069 nn->reclaim_str_hashtbl_size)
5070 return false;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005071 if (!nn->somebody_reclaimed)
5072 return false;
5073 nn->somebody_reclaimed = false;
5074 /*
5075 * If we've given them *two* lease times to reclaim, and they're
5076 * still not done, give up:
5077 */
5078 if (time_after(now, double_grace_period_end))
5079 return false;
5080 return true;
5081}
5082
NeilBrownfd39ca92005-06-23 22:04:03 -07005083static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005084nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085{
5086 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005087 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04005089 struct nfs4_ol_stateid *stp;
Jeff Layton7919d0a2016-09-16 16:28:25 -04005090 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03005092 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04005093 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 dprintk("NFSD: laundromat service - starting\n");
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005096
5097 if (clients_still_reclaiming(nn)) {
5098 new_timeo = 0;
5099 goto out;
5100 }
Scott Mayhew362063a2019-03-26 18:06:28 -04005101 dprintk("NFSD: end of grace period\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005102 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03005103 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005104 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005105 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 clp = list_entry(pos, struct nfs4_client, cl_lru);
5107 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
5108 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04005109 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 break;
5111 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04005112 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03005113 dprintk("NFSD: client in use (clientid %08x)\n",
5114 clp->cl_clientid.cl_id);
5115 continue;
5116 }
Trond Myklebust4864af92014-07-30 08:27:03 -04005117 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03005118 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005119 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03005120 list_for_each_safe(pos, next, &reaplist) {
5121 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 dprintk("NFSD: purging unused client (clientid %08x)\n",
5123 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04005124 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 expire_client(clp);
5126 }
Benny Halevycdc975052014-05-30 09:09:30 -04005127 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005128 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5130 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04005131 t = dp->dl_time - cutoff;
5132 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 break;
5134 }
Jeff Layton3fcbbd22015-08-24 12:41:48 -04005135 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04005136 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 }
Benny Halevycdc975052014-05-30 09:09:30 -04005138 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005139 while (!list_empty(&reaplist)) {
5140 dp = list_first_entry(&reaplist, struct nfs4_delegation,
5141 dl_recall_lru);
5142 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005143 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 }
Jeff Layton217526e2014-07-30 08:27:11 -04005145
5146 spin_lock(&nn->client_lock);
5147 while (!list_empty(&nn->close_lru)) {
5148 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
5149 oo_close_lru);
5150 if (time_after((unsigned long)oo->oo_time,
5151 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04005152 t = oo->oo_time - cutoff;
5153 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 break;
5155 }
Jeff Layton217526e2014-07-30 08:27:11 -04005156 list_del_init(&oo->oo_close_lru);
5157 stp = oo->oo_last_closed_stid;
5158 oo->oo_last_closed_stid = NULL;
5159 spin_unlock(&nn->client_lock);
5160 nfs4_put_stid(&stp->st_stid);
5161 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 }
Jeff Layton217526e2014-07-30 08:27:11 -04005163 spin_unlock(&nn->client_lock);
5164
Jeff Layton7919d0a2016-09-16 16:28:25 -04005165 /*
5166 * It's possible for a client to try and acquire an already held lock
5167 * that is being held for a long time, and then lose interest in it.
5168 * So, we clean out any un-revisited request after a lease period
5169 * under the assumption that the client is no longer interested.
5170 *
5171 * RFC5661, sec. 9.6 states that the client must not rely on getting
5172 * notifications and must continue to poll for locks, even when the
5173 * server supports them. Thus this shouldn't lead to clients blocking
5174 * indefinitely once the lock does become free.
5175 */
5176 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04005177 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005178 while (!list_empty(&nn->blocked_locks_lru)) {
5179 nbl = list_first_entry(&nn->blocked_locks_lru,
5180 struct nfsd4_blocked_lock, nbl_lru);
5181 if (time_after((unsigned long)nbl->nbl_time,
5182 (unsigned long)cutoff)) {
5183 t = nbl->nbl_time - cutoff;
5184 new_timeo = min(new_timeo, t);
5185 break;
5186 }
5187 list_move(&nbl->nbl_lru, &reaplist);
5188 list_del_init(&nbl->nbl_list);
5189 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04005190 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005191
5192 while (!list_empty(&reaplist)) {
Naofumi Honda64ebe1242017-11-09 10:57:16 -05005193 nbl = list_first_entry(&reaplist,
Jeff Layton7919d0a2016-09-16 16:28:25 -04005194 struct nfsd4_blocked_lock, nbl_lru);
5195 list_del_init(&nbl->nbl_lru);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005196 free_blocked_lock(nbl);
5197 }
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005198out:
Jeff Laytona832e7a2014-05-30 09:09:26 -04005199 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04005200 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201}
5202
Harvey Harrisona254b242008-02-20 12:49:00 -08005203static struct workqueue_struct *laundry_wq;
5204static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08005205
5206static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005207laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208{
5209 time_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08005210 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005211 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
5212 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005214 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005216 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217}
5218
Jeff Layton8fcd4612015-07-30 06:57:46 -04005219static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07005220{
Jeff Layton8fcd4612015-07-30 06:57:46 -04005221 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005222 return nfserr_bad_stateid;
5223 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224}
5225
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04005227access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005229 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
5230 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
5231 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232}
5233
5234static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04005235access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005237 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
5238 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239}
5240
5241static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005242__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243{
Al Virob37ad282006-10-19 23:28:59 -07005244 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245
J. Bruce Fields02921912010-07-29 15:16:59 -04005246 /* For lock stateid's, we test the parent open, not the lock: */
5247 if (stp->st_openstp)
5248 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005249 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005251 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 goto out;
5253 status = nfs_ok;
5254out:
5255 return status;
5256}
5257
Al Virob37ad282006-10-19 23:28:59 -07005258static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005259check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005261 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005263 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005264 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 * conflicting state; so we must wait out the grace period. */
5266 return nfserr_grace;
5267 } else if (flags & WR_STATE)
5268 return nfs4_share_conflict(current_fh,
5269 NFS4_SHARE_DENY_WRITE);
5270 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
5271 return nfs4_share_conflict(current_fh,
5272 NFS4_SHARE_DENY_READ);
5273}
5274
5275/*
5276 * Allow READ/WRITE during grace period on recovered state only for files
5277 * that are not able to provide mandatory locking.
5278 */
5279static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005280grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005282 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283}
5284
J. Bruce Fields57b7b432012-04-25 17:58:50 -04005285static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05005286{
Andy Adamson66689582009-04-03 08:28:45 +03005287 /*
5288 * When sessions are used the stateid generation number is ignored
5289 * when it is zero.
5290 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04005291 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04005292 return nfs_ok;
5293
5294 if (in->si_generation == ref->si_generation)
5295 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03005296
J. Bruce Fields0836f582008-01-26 19:08:12 -05005297 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04005298 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05005299 return nfserr_bad_stateid;
5300 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04005301 * However, we could see a stateid from the past, even from a
5302 * non-buggy client. For example, if the client sends a lock
5303 * while some IO is outstanding, the lock may bump si_generation
5304 * while the IO is still in flight. The client could avoid that
5305 * situation by waiting for responses on all the IO requests,
5306 * but better performance may result in retrying IO that
5307 * receives an old_stateid error if requests are rarely
5308 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05005309 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04005310 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05005311}
5312
Trond Myklebust03da3162017-11-03 08:00:16 -04005313static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
5314{
5315 __be32 ret;
5316
5317 spin_lock(&s->sc_lock);
5318 ret = nfsd4_verify_open_stid(s);
5319 if (ret == nfs_ok)
5320 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
5321 spin_unlock(&s->sc_lock);
5322 return ret;
5323}
5324
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005325static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
5326{
5327 if (ols->st_stateowner->so_is_open_owner &&
5328 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5329 return nfserr_bad_stateid;
5330 return nfs_ok;
5331}
5332
Chuck Lever7df302f2012-05-29 13:56:37 -04005333static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04005334{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005335 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005336 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04005337
Andrew Elbleae254dac2017-11-09 13:41:10 -05005338 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5339 CLOSE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04005340 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04005341 /* Client debugging aid. */
5342 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
5343 char addr_str[INET6_ADDRSTRLEN];
5344 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
5345 sizeof(addr_str));
5346 pr_warn_ratelimited("NFSD: client %s testing state ID "
5347 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005348 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04005349 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005350 spin_lock(&cl->cl_lock);
5351 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005352 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005353 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005354 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04005355 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005356 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04005357 switch (s->sc_type) {
5358 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005359 status = nfs_ok;
5360 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005361 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005362 status = nfserr_deleg_revoked;
5363 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005364 case NFS4_OPEN_STID:
5365 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005366 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04005367 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005368 default:
5369 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005370 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04005371 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005372 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005373 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04005374 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005375out_unlock:
5376 spin_unlock(&cl->cl_lock);
5377 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04005378}
5379
Christoph Hellwigcd61c522014-08-14 08:44:57 +02005380__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005381nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5382 stateid_t *stateid, unsigned char typemask,
5383 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005384{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005385 __be32 status;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005386 bool return_revoked = false;
5387
5388 /*
5389 * only return revoked delegations if explicitly asked.
5390 * otherwise we report revoked or bad_stateid status.
5391 */
5392 if (typemask & NFS4_REVOKED_DELEG_STID)
5393 return_revoked = true;
5394 else if (typemask & NFS4_DELEG_STID)
5395 typemask |= NFS4_REVOKED_DELEG_STID;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005396
Andrew Elbleae254dac2017-11-09 13:41:10 -05005397 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5398 CLOSE_STATEID(stateid))
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005399 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005400 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005401 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005402 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005403 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005404 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005405 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005406 if (status)
5407 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005408 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005409 if (!*s)
5410 return nfserr_bad_stateid;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005411 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5412 nfs4_put_stid(*s);
5413 if (cstate->minorversion)
5414 return nfserr_deleg_revoked;
5415 return nfserr_bad_stateid;
5416 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005417 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005418}
5419
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005420static struct file *
5421nfs4_find_file(struct nfs4_stid *s, int flags)
5422{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005423 if (!s)
5424 return NULL;
5425
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005426 switch (s->sc_type) {
5427 case NFS4_DELEG_STID:
5428 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5429 return NULL;
5430 return get_file(s->sc_file->fi_deleg_file);
5431 case NFS4_OPEN_STID:
5432 case NFS4_LOCK_STID:
5433 if (flags & RD_STATE)
5434 return find_readable_file(s->sc_file);
5435 else
5436 return find_writeable_file(s->sc_file);
5437 break;
5438 }
5439
5440 return NULL;
5441}
5442
5443static __be32
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005444nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005445{
5446 __be32 status;
5447
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005448 status = nfsd4_check_openowner_confirmed(ols);
5449 if (status)
5450 return status;
5451 return nfs4_check_openmode(ols, flags);
5452}
5453
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005454static __be32
5455nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
5456 struct file **filpp, bool *tmp_file, int flags)
5457{
5458 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
5459 struct file *file;
5460 __be32 status;
5461
5462 file = nfs4_find_file(s, flags);
5463 if (file) {
5464 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5465 acc | NFSD_MAY_OWNER_OVERRIDE);
5466 if (status) {
5467 fput(file);
5468 return status;
5469 }
5470
5471 *filpp = file;
5472 } else {
5473 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
5474 if (status)
5475 return status;
5476
5477 if (tmp_file)
5478 *tmp_file = true;
5479 }
5480
5481 return 0;
5482}
5483
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005485 * Checks for stateid operations
5486 */
Al Virob37ad282006-10-19 23:28:59 -07005487__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005488nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01005489 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
5490 stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005492 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005493 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005494 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005495 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005496 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 if (filpp)
5499 *filpp = NULL;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005500 if (tmp_file)
5501 *tmp_file = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005503 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 return nfserr_grace;
5505
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005506 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5507 status = check_special_stateids(net, fhp, stateid, flags);
5508 goto done;
5509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005511 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04005512 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005513 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005514 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04005515 return status;
Trond Myklebust03da3162017-11-03 08:00:16 -04005516 status = nfsd4_stid_check_stateid_generation(stateid, s,
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005517 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04005518 if (status)
5519 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005520
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005521 switch (s->sc_type) {
5522 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005523 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005524 break;
5525 case NFS4_OPEN_STID:
5526 case NFS4_LOCK_STID:
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005527 status = nfs4_check_olstateid(openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005528 break;
5529 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04005530 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005531 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04005533 if (status)
5534 goto out;
5535 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005536
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005537done:
5538 if (!status && filpp)
5539 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540out:
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005541 if (s)
5542 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 return status;
5544}
5545
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005546/*
Bryan Schumaker17456802011-07-13 10:50:48 -04005547 * Test if the stateid is valid
5548 */
5549__be32
5550nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005551 union nfsd4_op_u *u)
Bryan Schumaker17456802011-07-13 10:50:48 -04005552{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005553 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005554 struct nfsd4_test_stateid_id *stateid;
5555 struct nfs4_client *cl = cstate->session->se_client;
5556
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005557 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04005558 stateid->ts_id_status =
5559 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005560
Bryan Schumaker17456802011-07-13 10:50:48 -04005561 return nfs_ok;
5562}
5563
Chuck Lever42691392016-08-11 10:37:30 -04005564static __be32
5565nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5566{
5567 struct nfs4_ol_stateid *stp = openlockstateid(s);
5568 __be32 ret;
5569
Trond Myklebust659aefb2017-11-03 08:00:13 -04005570 ret = nfsd4_lock_ol_stateid(stp);
5571 if (ret)
5572 goto out_put_stid;
Chuck Lever42691392016-08-11 10:37:30 -04005573
5574 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5575 if (ret)
5576 goto out;
5577
5578 ret = nfserr_locks_held;
5579 if (check_for_locks(stp->st_stid.sc_file,
5580 lockowner(stp->st_stateowner)))
5581 goto out;
5582
5583 release_lock_stateid(stp);
5584 ret = nfs_ok;
5585
5586out:
5587 mutex_unlock(&stp->st_mutex);
Trond Myklebust659aefb2017-11-03 08:00:13 -04005588out_put_stid:
Chuck Lever42691392016-08-11 10:37:30 -04005589 nfs4_put_stid(s);
5590 return ret;
5591}
5592
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005593__be32
5594nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005595 union nfsd4_op_u *u)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005596{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005597 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005598 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005599 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005600 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005601 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005602 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005603
Jeff Layton1af71cc2014-07-29 21:34:14 -04005604 spin_lock(&cl->cl_lock);
5605 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005606 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005607 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005608 spin_lock(&s->sc_lock);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005609 switch (s->sc_type) {
5610 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005611 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005612 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005613 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005614 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5615 if (ret)
5616 break;
5617 ret = nfserr_locks_held;
5618 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005619 case NFS4_LOCK_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005620 spin_unlock(&s->sc_lock);
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03005621 refcount_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005622 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04005623 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005624 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005625 case NFS4_REVOKED_DELEG_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005626 spin_unlock(&s->sc_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005627 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005628 list_del_init(&dp->dl_recall_lru);
5629 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04005630 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005631 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005632 goto out;
5633 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005634 }
Trond Myklebust03da3162017-11-03 08:00:16 -04005635 spin_unlock(&s->sc_lock);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005636out_unlock:
5637 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005638out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005639 return ret;
5640}
5641
NeilBrown4c4cd222005-07-07 17:59:27 -07005642static inline int
5643setlkflg (int type)
5644{
5645 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5646 RD_STATE : WR_STATE;
5647}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005649static __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 -04005650{
5651 struct svc_fh *current_fh = &cstate->current_fh;
5652 struct nfs4_stateowner *sop = stp->st_stateowner;
5653 __be32 status;
5654
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005655 status = nfsd4_check_seqid(cstate, sop, seqid);
5656 if (status)
5657 return status;
Trond Myklebust9271d7e2017-11-03 08:00:15 -04005658 status = nfsd4_lock_ol_stateid(stp);
5659 if (status != nfs_ok)
5660 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005661 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04005662 if (status == nfs_ok)
5663 status = nfs4_check_fh(current_fh, &stp->st_stid);
5664 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005665 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005666 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005667}
5668
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669/*
5670 * Checks for sequence id mutating operations.
5671 */
Al Virob37ad282006-10-19 23:28:59 -07005672static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03005673nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04005674 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005675 struct nfs4_ol_stateid **stpp,
5676 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677{
J. Bruce Fields0836f582008-01-26 19:08:12 -05005678 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005679 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005680 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005682 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5683 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07005684
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005686 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005687 if (status)
5688 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005689 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005690 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005692 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005693 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005694 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04005695 else
5696 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005697 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005698}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05005699
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005700static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5701 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005702{
5703 __be32 status;
5704 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005705 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005707 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005708 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04005709 if (status)
5710 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005711 oo = openowner(stp->st_stateowner);
5712 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005713 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005714 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07005715 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005716 }
5717 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07005718 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719}
5720
Al Virob37ad282006-10-19 23:28:59 -07005721__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005722nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005723 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005725 struct nfsd4_open_confirm *oc = &u->open_confirm;
Al Virob37ad282006-10-19 23:28:59 -07005726 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005727 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005728 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005729 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730
Al Viroa6a9f182013-09-16 10:57:01 -04005731 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5732 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005734 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07005735 if (status)
5736 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005738 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005739 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005740 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005741 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005742 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005743 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005744 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005745 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005746 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04005747 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005748 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04005749 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04005750 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005751 mutex_unlock(&stp->st_mutex);
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005752 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005753 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07005754
Jeff Layton2a4317c2012-03-21 16:42:43 -04005755 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005756 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04005757put_stateid:
5758 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005760 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 return status;
5762}
5763
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005764static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005766 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005767 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04005768 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005769 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005770}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04005771
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005772static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5773{
5774 switch (to_access) {
5775 case NFS4_SHARE_ACCESS_READ:
5776 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5777 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5778 break;
5779 case NFS4_SHARE_ACCESS_WRITE:
5780 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5781 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5782 break;
5783 case NFS4_SHARE_ACCESS_BOTH:
5784 break;
5785 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005786 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 }
5788}
5789
Al Virob37ad282006-10-19 23:28:59 -07005790__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005791nfsd4_open_downgrade(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005792 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005794 struct nfsd4_open_downgrade *od = &u->open_downgrade;
Al Virob37ad282006-10-19 23:28:59 -07005795 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005796 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005797 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798
Al Viroa6a9f182013-09-16 10:57:01 -04005799 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5800 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04005802 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02005803 if (od->od_deleg_want)
5804 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5805 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005807 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005808 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005809 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005812 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005813 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005815 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04005817 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005818 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005820 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005822 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04005823 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04005824 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005826put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005827 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005828 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005830 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 return status;
5832}
5833
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005834static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5835{
Trond Myklebustacf92952014-06-30 11:48:37 -04005836 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04005837 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04005838 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005839
Jeff Layton2c41beb2014-07-29 21:34:41 -04005840 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04005841 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005842
Jeff Laytond83017f2014-07-29 21:34:42 -04005843 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04005844 if (unhashed)
5845 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04005846 spin_unlock(&clp->cl_lock);
5847 free_ol_stateid_reaplist(&reaplist);
5848 } else {
5849 spin_unlock(&clp->cl_lock);
5850 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04005851 if (unhashed)
5852 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04005853 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04005854}
5855
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856/*
5857 * nfs4_unlock_state() called after encode
5858 */
Al Virob37ad282006-10-19 23:28:59 -07005859__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005860nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005861 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005863 struct nfsd4_close *close = &u->close;
Al Virob37ad282006-10-19 23:28:59 -07005864 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005865 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005866 struct net *net = SVC_NET(rqstp);
5867 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868
Al Viroa6a9f182013-09-16 10:57:01 -04005869 dprintk("NFSD: nfsd4_close on file %pd\n",
5870 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005872 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5873 &close->cl_stateid,
5874 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005875 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005876 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005877 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005879
5880 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Laytonbd2deca2018-02-15 15:16:18 -05005881
5882 /*
5883 * Technically we don't _really_ have to increment or copy it, since
5884 * it should just be gone after this operation and we clobber the
5885 * copied value below, but we continue to do so here just to ensure
5886 * that racing ops see that there was a state change.
5887 */
Jeff Layton9767feb2015-10-01 09:05:50 -04005888 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005890 nfsd4_close_open_stateid(stp);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005891 mutex_unlock(&stp->st_mutex);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005892
Jeff Laytonbd2deca2018-02-15 15:16:18 -05005893 /* v4.1+ suggests that we send a special stateid in here, since the
5894 * clients should just ignore this anyway. Since this is not useful
5895 * for v4.0 clients either, we set it to the special close_stateid
5896 * universally.
5897 *
5898 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
5899 */
5900 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
Trond Myklebustfb500a72017-11-03 08:00:12 -04005901
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005902 /* put reference from nfs4_preprocess_seqid_op */
5903 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 return status;
5906}
5907
Al Virob37ad282006-10-19 23:28:59 -07005908__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005909nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005910 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005912 struct nfsd4_delegreturn *dr = &u->delegreturn;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005913 struct nfs4_delegation *dp;
5914 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005915 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07005916 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005917 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005919 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005920 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005922 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005923 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005924 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005925 dp = delegstateid(s);
Trond Myklebust03da3162017-11-03 08:00:16 -04005926 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005927 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04005928 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005929
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005930 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005931put_stateid:
5932 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933out:
5934 return status;
5935}
5936
Benny Halevy87df4de2008-12-15 19:42:03 +02005937static inline u64
5938end_offset(u64 start, u64 len)
5939{
5940 u64 end;
5941
5942 end = start + len;
5943 return end >= start ? end: NFS4_MAX_UINT64;
5944}
5945
5946/* last octet in a range */
5947static inline u64
5948last_byte_offset(u64 start, u64 len)
5949{
5950 u64 end;
5951
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005952 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02005953 end = start + len;
5954 return end > start ? end - 1: NFS4_MAX_UINT64;
5955}
5956
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957/*
5958 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5959 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5960 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5961 * locking, this prevents us from being completely protocol-compliant. The
5962 * real solution to this problem is to start using unsigned file offsets in
5963 * the VFS, but this is a very deep change!
5964 */
5965static inline void
5966nfs4_transform_lock_offset(struct file_lock *lock)
5967{
5968 if (lock->fl_start < 0)
5969 lock->fl_start = OFFSET_MAX;
5970 if (lock->fl_end < 0)
5971 lock->fl_end = OFFSET_MAX;
5972}
5973
Jeff Laytoncae80b32015-04-03 09:04:04 -04005974static fl_owner_t
5975nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005976{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005977 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5978
5979 nfs4_get_stateowner(&lo->lo_owner);
5980 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005981}
5982
Jeff Laytoncae80b32015-04-03 09:04:04 -04005983static void
5984nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005985{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005986 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005987
Jeff Laytoncae80b32015-04-03 09:04:04 -04005988 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04005989 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04005990}
5991
Jeff Layton76d348f2016-09-16 16:28:24 -04005992static void
5993nfsd4_lm_notify(struct file_lock *fl)
5994{
5995 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
5996 struct net *net = lo->lo_owner.so_client->net;
5997 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5998 struct nfsd4_blocked_lock *nbl = container_of(fl,
5999 struct nfsd4_blocked_lock, nbl_lock);
6000 bool queue = false;
6001
Jeff Layton7919d0a2016-09-16 16:28:25 -04006002 /* An empty list means that something else is going to be using it */
Jeff Layton0cc11a62016-10-20 09:34:31 -04006003 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006004 if (!list_empty(&nbl->nbl_list)) {
6005 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006006 list_del_init(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -04006007 queue = true;
6008 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04006009 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006010
6011 if (queue)
6012 nfsd4_run_cb(&nbl->nbl_cb);
6013}
6014
Alexey Dobriyan7b021962009-09-21 17:01:12 -07006015static const struct lock_manager_operations nfsd_posix_mng_ops = {
Jeff Layton76d348f2016-09-16 16:28:24 -04006016 .lm_notify = nfsd4_lm_notify,
Kinglong Meeaef95832014-08-22 10:18:44 -04006017 .lm_get_owner = nfsd4_fl_get_owner,
6018 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07006019};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020
6021static inline void
6022nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
6023{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006024 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025
NeilBrownd5b90262006-04-10 22:55:22 -07006026 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006027 lo = (struct nfs4_lockowner *) fl->fl_owner;
6028 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
6029 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006030 if (!deny->ld_owner.data)
6031 /* We just don't care that much */
6032 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006033 deny->ld_owner.len = lo->lo_owner.so_owner.len;
6034 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07006035 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006036nevermind:
6037 deny->ld_owner.len = 0;
6038 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07006039 deny->ld_clientid.cl_boot = 0;
6040 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041 }
6042 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02006043 deny->ld_length = NFS4_MAX_UINT64;
6044 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 deny->ld_length = fl->fl_end - fl->fl_start + 1;
6046 deny->ld_type = NFS4_READ_LT;
6047 if (fl->fl_type != F_RDLCK)
6048 deny->ld_type = NFS4_WRITE_LT;
6049}
6050
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006051static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006052find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053{
Trond Myklebustd4f04892014-07-29 21:34:36 -04006054 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006055 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056
Trond Myklebust0a880a22014-07-30 08:27:10 -04006057 lockdep_assert_held(&clp->cl_lock);
6058
Trond Myklebustd4f04892014-07-29 21:34:36 -04006059 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
6060 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006061 if (so->so_is_open_owner)
6062 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006063 if (same_owner_str(so, owner))
6064 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 }
6066 return NULL;
6067}
6068
Trond Myklebustc58c6612014-07-29 21:34:35 -04006069static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006070find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04006071{
6072 struct nfs4_lockowner *lo;
6073
Trond Myklebustd4f04892014-07-29 21:34:36 -04006074 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006075 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04006076 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006077 return lo;
6078}
6079
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006080static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
6081{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006082 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006083}
6084
Jeff Layton6b180f02014-07-29 21:34:26 -04006085static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
6086{
6087 struct nfs4_lockowner *lo = lockowner(sop);
6088
6089 kmem_cache_free(lockowner_slab, lo);
6090}
6091
6092static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006093 .so_unhash = nfs4_unhash_lockowner,
6094 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04006095};
6096
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097/*
6098 * Alloc a lock owner structure.
6099 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006100 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006102 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006104static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04006105alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
6106 struct nfs4_ol_stateid *open_stp,
6107 struct nfsd4_lock *lock)
6108{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006109 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006111 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
6112 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006114 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006115 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
6116 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04006117 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04006118 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006119 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006120 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006121 if (ret == NULL) {
6122 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04006123 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006124 ret = lo;
6125 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08006126 nfs4_free_stateowner(&lo->lo_owner);
6127
Trond Myklebustd4f04892014-07-29 21:34:36 -04006128 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04006129 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130}
6131
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006132static struct nfs4_ol_stateid *
6133find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
6134{
6135 struct nfs4_ol_stateid *lst;
6136 struct nfs4_client *clp = lo->lo_owner.so_client;
6137
6138 lockdep_assert_held(&clp->cl_lock);
6139
6140 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
6141 if (lst->st_stid.sc_type != NFS4_LOCK_STID)
6142 continue;
6143 if (lst->st_stid.sc_file == fp) {
6144 refcount_inc(&lst->st_stid.sc_count);
6145 return lst;
6146 }
6147 }
6148 return NULL;
6149}
6150
Trond Myklebustbeeca192017-11-03 08:00:14 -04006151static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006152init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
6153 struct nfs4_file *fp, struct inode *inode,
6154 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04006156 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustbeeca192017-11-03 08:00:14 -04006157 struct nfs4_ol_stateid *retstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158
Trond Myklebustbeeca192017-11-03 08:00:14 -04006159 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05006160 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006161retry:
6162 spin_lock(&clp->cl_lock);
6163 spin_lock(&fp->fi_lock);
6164 retstp = find_lock_stateid(lo, fp);
6165 if (retstp)
6166 goto out_unlock;
Jeff Layton356a95e2014-07-29 21:34:13 -04006167
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03006168 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05006169 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006170 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07006171 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04006172 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006173 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07006175 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006176 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04006177 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04006178 list_add(&stp->st_perfile, &fp->fi_stateids);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006179out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04006180 spin_unlock(&fp->fi_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006181 spin_unlock(&clp->cl_lock);
6182 if (retstp) {
6183 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
6184 nfs4_put_stid(&retstp->st_stid);
6185 goto retry;
6186 }
6187 /* To keep mutex tracking happy */
6188 mutex_unlock(&stp->st_mutex);
6189 stp = retstp;
6190 }
6191 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192}
6193
Jeff Laytonc53530d2014-06-30 11:48:39 -04006194static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006195find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
6196 struct inode *inode, struct nfs4_ol_stateid *ost,
6197 bool *new)
6198{
6199 struct nfs4_stid *ns = NULL;
6200 struct nfs4_ol_stateid *lst;
6201 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6202 struct nfs4_client *clp = oo->oo_owner.so_client;
6203
Trond Myklebustbeeca192017-11-03 08:00:14 -04006204 *new = false;
Jeff Layton356a95e2014-07-29 21:34:13 -04006205 spin_lock(&clp->cl_lock);
6206 lst = find_lock_stateid(lo, fi);
Jeff Layton356a95e2014-07-29 21:34:13 -04006207 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006208 if (lst != NULL) {
6209 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
6210 goto out;
6211 nfs4_put_stid(&lst->st_stid);
6212 }
6213 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
6214 if (ns == NULL)
6215 return NULL;
6216
6217 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
6218 if (lst == openlockstateid(ns))
6219 *new = true;
6220 else
Jeff Layton356a95e2014-07-29 21:34:13 -04006221 nfs4_put_stid(ns);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006222out:
Jeff Layton356a95e2014-07-29 21:34:13 -04006223 return lst;
6224}
Jeff Laytonc53530d2014-06-30 11:48:39 -04006225
NeilBrownfd39ca92005-06-23 22:04:03 -07006226static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227check_lock_length(u64 offset, u64 length)
6228{
Kinglong Meee7969312015-07-13 17:34:19 +08006229 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
6230 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231}
6232
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006233static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006234{
Trond Myklebust11b91642014-07-29 21:34:08 -04006235 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006236
Jeff Layton7214e862014-07-10 14:07:33 -04006237 lockdep_assert_held(&fp->fi_lock);
6238
Jeff Layton82c5ff12012-05-11 09:45:13 -04006239 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05006240 return;
Jeff Layton12659652014-07-10 14:07:28 -04006241 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006242 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006243}
6244
Jeff Layton356a95e2014-07-29 21:34:13 -04006245static __be32
6246lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
6247 struct nfs4_ol_stateid *ost,
6248 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04006249 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006250{
Jeff Layton5db1c032014-07-29 21:34:28 -04006251 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04006252 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006253 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6254 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00006255 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006256 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04006257 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006258 unsigned int strhashval;
6259
Kinglong Meec8623992015-07-13 17:35:37 +08006260 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006261 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006262 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006263 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
6264 if (lo == NULL)
6265 return nfserr_jukebox;
6266 } else {
6267 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04006268 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04006269 if (!cstate->minorversion &&
6270 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04006271 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006272 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04006273
Jeff Laytondd257932016-08-11 10:37:39 -04006274 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
6275 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04006276 status = nfserr_jukebox;
6277 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006278 }
Jeff Laytondd257932016-08-11 10:37:39 -04006279
Jeff Layton5db1c032014-07-29 21:34:28 -04006280 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04006281 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04006282out:
6283 nfs4_put_stateowner(&lo->lo_owner);
6284 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006285}
6286
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287/*
6288 * LOCK operation
6289 */
Al Virob37ad282006-10-19 23:28:59 -07006290__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006291nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006292 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006294 struct nfsd4_lock *lock = &u->lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006295 struct nfs4_openowner *open_sop = NULL;
6296 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006297 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006298 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04006299 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04006300 struct file *filp = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006301 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006302 struct file_lock *file_lock = NULL;
6303 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006304 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006305 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07006306 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04006307 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04006308 unsigned char fl_type;
6309 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006310 struct net *net = SVC_NET(rqstp);
6311 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312
6313 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
6314 (long long) lock->lk_offset,
6315 (long long) lock->lk_length);
6316
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 if (check_lock_length(lock->lk_offset, lock->lk_length))
6318 return nfserr_inval;
6319
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006320 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02006321 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08006322 dprintk("NFSD: nfsd4_lock: permission denied!\n");
6323 return status;
6324 }
6325
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04006327 if (nfsd4_has_session(cstate))
6328 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006329 memcpy(&lock->lk_new_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04006330 &cstate->session->se_client->cl_clientid,
6331 sizeof(clientid_t));
6332
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006334 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006338 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339 lock->lk_new_open_seqid,
6340 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006341 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07006342 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006344 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006345 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006346 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04006347 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006348 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006349 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006350 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04006351 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006352 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03006353 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006354 lock->lk_old_lock_seqid,
6355 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006356 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006357 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04006358 if (status)
6359 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006360 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006362 lkflg = setlkflg(lock->lk_type);
6363 status = nfs4_check_openmode(lock_stp, lkflg);
6364 if (status)
6365 goto out;
6366
NeilBrown0dd395d2005-07-07 17:59:15 -07006367 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006368 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006369 goto out;
6370 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006371 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006372 goto out;
6373
Trond Myklebust11b91642014-07-29 21:34:08 -04006374 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376 case NFS4_READW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006377 if (nfsd4_has_session(cstate))
6378 fl_flags |= FL_SLEEP;
6379 /* Fallthrough */
6380 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006381 spin_lock(&fp->fi_lock);
6382 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006383 if (filp)
6384 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04006385 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006386 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006387 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388 case NFS4_WRITEW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006389 if (nfsd4_has_session(cstate))
6390 fl_flags |= FL_SLEEP;
6391 /* Fallthrough */
6392 case NFS4_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006393 spin_lock(&fp->fi_lock);
6394 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006395 if (filp)
6396 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04006397 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006398 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006399 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400 default:
6401 status = nfserr_inval;
6402 goto out;
6403 }
Jeff Layton76d348f2016-09-16 16:28:24 -04006404
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006405 if (!filp) {
6406 status = nfserr_openmode;
6407 goto out;
6408 }
Kinglong Meeaef95832014-08-22 10:18:44 -04006409
Jeff Layton76d348f2016-09-16 16:28:24 -04006410 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6411 if (!nbl) {
6412 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6413 status = nfserr_jukebox;
6414 goto out;
6415 }
6416
6417 file_lock = &nbl->nbl_lock;
6418 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04006419 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04006420 file_lock->fl_pid = current->tgid;
6421 file_lock->fl_file = filp;
Jeff Layton76d348f2016-09-16 16:28:24 -04006422 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04006423 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6424 file_lock->fl_start = lock->lk_offset;
6425 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6426 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427
Jeff Layton21179d82012-08-21 08:03:32 -04006428 conflock = locks_alloc_lock();
6429 if (!conflock) {
6430 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6431 status = nfserr_jukebox;
6432 goto out;
6433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434
Jeff Layton76d348f2016-09-16 16:28:24 -04006435 if (fl_flags & FL_SLEEP) {
Jeff Layton7919d0a2016-09-16 16:28:25 -04006436 nbl->nbl_time = jiffies;
Jeff Layton0cc11a62016-10-20 09:34:31 -04006437 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006438 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006439 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006440 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006441 }
6442
Jeff Layton21179d82012-08-21 08:03:32 -04006443 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006444 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04006446 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07006447 status = 0;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04006448 if (lock->lk_reclaim)
6449 nn->somebody_reclaimed = true;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006450 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006451 case FILE_LOCK_DEFERRED:
6452 nbl = NULL;
6453 /* Fallthrough */
6454 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006455 status = nfserr_denied;
6456 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04006457 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006458 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006459 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006461 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04006462 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05006463 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04006464 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006465 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467out:
Jeff Layton76d348f2016-09-16 16:28:24 -04006468 if (nbl) {
6469 /* dequeue it if we queued it before */
6470 if (fl_flags & FL_SLEEP) {
Jeff Layton0cc11a62016-10-20 09:34:31 -04006471 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006472 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006473 list_del_init(&nbl->nbl_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006474 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006475 }
6476 free_blocked_lock(nbl);
6477 }
Trond Myklebustde186432014-07-10 14:07:26 -04006478 if (filp)
6479 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04006480 if (lock_stp) {
6481 /* Bump seqid manually if the 4.0 replay owner is openowner */
6482 if (cstate->replay_owner &&
6483 cstate->replay_owner != &lock_sop->lo_owner &&
6484 seqid_mutating_err(ntohl(status)))
6485 lock_sop->lo_owner.so_seqid++;
6486
6487 /*
6488 * If this is a new, never-before-used stateid, and we are
6489 * returning an error, then just go ahead and release it.
6490 */
J. Bruce Fields25020722018-01-17 16:25:59 -05006491 if (status && new)
Jeff Layton5db1c032014-07-29 21:34:28 -04006492 release_lock_stateid(lock_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006493
6494 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton5db1c032014-07-29 21:34:28 -04006495
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006496 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04006497 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006498 if (open_stp)
6499 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006500 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006501 if (conflock)
6502 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503 return status;
6504}
6505
6506/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006507 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6508 * so we do a temporary open here just to get an open file to pass to
6509 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6510 * inode operation.)
6511 */
Al Viro04da6e92012-04-13 00:00:04 -04006512static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006513{
6514 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04006515 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
6516 if (!err) {
6517 err = nfserrno(vfs_test_lock(file, lock));
Christoph Hellwigfd891452015-04-28 15:41:16 +02006518 fput(file);
Al Viro04da6e92012-04-13 00:00:04 -04006519 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006520 return err;
6521}
6522
6523/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524 * LOCKT operation
6525 */
Al Virob37ad282006-10-19 23:28:59 -07006526__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006527nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006528 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006530 struct nfsd4_lockt *lockt = &u->lockt;
Jeff Layton21179d82012-08-21 08:03:32 -04006531 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04006532 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006533 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006534 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006536 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537 return nfserr_grace;
6538
6539 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6540 return nfserr_inval;
6541
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006542 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04006543 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006544 if (status)
6545 goto out;
6546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547
J. Bruce Fields75c096f2011-08-15 18:39:32 -04006548 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550
Jeff Layton21179d82012-08-21 08:03:32 -04006551 file_lock = locks_alloc_lock();
6552 if (!file_lock) {
6553 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6554 status = nfserr_jukebox;
6555 goto out;
6556 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006557
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558 switch (lockt->lt_type) {
6559 case NFS4_READ_LT:
6560 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006561 file_lock->fl_type = F_RDLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006562 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563 case NFS4_WRITE_LT:
6564 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006565 file_lock->fl_type = F_WRLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006566 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04006568 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006569 status = nfserr_inval;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006570 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571 }
6572
Kinglong Meec8623992015-07-13 17:35:37 +08006573 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006574 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04006575 file_lock->fl_owner = (fl_owner_t)lo;
6576 file_lock->fl_pid = current->tgid;
6577 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578
Jeff Layton21179d82012-08-21 08:03:32 -04006579 file_lock->fl_start = lockt->lt_offset;
6580 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581
Jeff Layton21179d82012-08-21 08:03:32 -04006582 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583
Jeff Layton21179d82012-08-21 08:03:32 -04006584 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04006585 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05006586 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04006587
Jeff Layton21179d82012-08-21 08:03:32 -04006588 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04006590 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591 }
6592out:
Jeff Layton5db1c032014-07-29 21:34:28 -04006593 if (lo)
6594 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04006595 if (file_lock)
6596 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597 return status;
6598}
6599
Al Virob37ad282006-10-19 23:28:59 -07006600__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006601nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006602 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006604 struct nfsd4_locku *locku = &u->locku;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006605 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006607 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006608 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07006609 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006610 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6611
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6613 (long long) locku->lu_offset,
6614 (long long) locku->lu_length);
6615
6616 if (check_lock_length(locku->lu_offset, locku->lu_length))
6617 return nfserr_inval;
6618
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006619 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006620 &locku->lu_stateid, NFS4_LOCK_STID,
6621 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006622 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04006624 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006625 if (!filp) {
6626 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04006627 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006628 }
Jeff Layton21179d82012-08-21 08:03:32 -04006629 file_lock = locks_alloc_lock();
6630 if (!file_lock) {
6631 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6632 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04006633 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04006634 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006635
Jeff Layton21179d82012-08-21 08:03:32 -04006636 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04006637 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04006638 file_lock->fl_pid = current->tgid;
6639 file_lock->fl_file = filp;
6640 file_lock->fl_flags = FL_POSIX;
6641 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6642 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643
Jeff Layton21179d82012-08-21 08:03:32 -04006644 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6645 locku->lu_length);
6646 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647
Jeff Layton21179d82012-08-21 08:03:32 -04006648 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07006649 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05006650 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 goto out_nfserr;
6652 }
Jeff Layton9767feb2015-10-01 09:05:50 -04006653 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Trond Myklebustde186432014-07-10 14:07:26 -04006654fput:
6655 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04006656put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006657 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04006658 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006660 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006661 if (file_lock)
6662 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663 return status;
6664
6665out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07006666 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04006667 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668}
6669
6670/*
6671 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006672 * true: locks held by lockowner
6673 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006675static bool
6676check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006678 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006679 int status = false;
6680 struct file *filp = find_any_file(fp);
6681 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006682 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006683
6684 if (!filp) {
6685 /* Any valid lock stateid should have some sort of access */
6686 WARN_ON_ONCE(1);
6687 return status;
6688 }
6689
Amir Goldstein64bed6c2018-07-13 17:22:24 +03006690 inode = locks_inode(filp);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006691 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006693 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05006694 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006695 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6696 if (fl->fl_owner == (fl_owner_t)lowner) {
6697 status = true;
6698 break;
6699 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08006700 }
Jeff Layton6109c852015-01-16 15:05:57 -05006701 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006702 }
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006703 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704 return status;
6705}
6706
Al Virob37ad282006-10-19 23:28:59 -07006707__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08006708nfsd4_release_lockowner(struct svc_rqst *rqstp,
6709 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006710 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006712 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04006714 struct nfs4_stateowner *sop;
6715 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006716 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006718 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07006719 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006720 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006721 struct nfs4_client *clp;
Chuck Lever88584812016-07-13 16:40:14 -04006722 LIST_HEAD (reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723
6724 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6725 clid->cl_boot, clid->cl_id);
6726
Jeff Layton4b24ca72014-06-30 11:48:44 -04006727 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006728 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04006729 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006730
Trond Myklebustd4f04892014-07-29 21:34:36 -04006731 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04006732 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04006733 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04006734 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04006735 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04006736
6737 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006738 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04006739
Jeff Layton882e9d22014-07-29 21:34:37 -04006740 /* see if there are still any locks associated with it */
6741 lo = lockowner(sop);
6742 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6743 if (check_for_locks(stp->st_stid.sc_file, lo)) {
6744 status = nfserr_locks_held;
6745 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04006746 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04006747 }
Jeff Layton5adfd882014-07-29 21:34:31 -04006748 }
Jeff Layton882e9d22014-07-29 21:34:37 -04006749
Kinglong Meeb5971af2014-08-22 10:18:43 -04006750 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04006751 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04006752 }
Chuck Lever88584812016-07-13 16:40:14 -04006753 if (!lo) {
6754 spin_unlock(&clp->cl_lock);
6755 return status;
6756 }
6757
6758 unhash_lockowner_locked(lo);
6759 while (!list_empty(&lo->lo_owner.so_stateids)) {
6760 stp = list_first_entry(&lo->lo_owner.so_stateids,
6761 struct nfs4_ol_stateid,
6762 st_perstateowner);
6763 WARN_ON(!unhash_lock_stateid(stp));
6764 put_ol_stateid_locked(stp, &reaplist);
6765 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04006766 spin_unlock(&clp->cl_lock);
Chuck Lever88584812016-07-13 16:40:14 -04006767 free_ol_stateid_reaplist(&reaplist);
Jeff Layton68ef3bc2018-03-16 11:32:02 -04006768 remove_blocked_locks(lo);
Chuck Lever88584812016-07-13 16:40:14 -04006769 nfs4_put_stateowner(&lo->lo_owner);
6770
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771 return status;
6772}
6773
6774static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07006775alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776{
NeilBrowna55370a2005-06-23 22:03:52 -07006777 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778}
6779
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006780bool
Scott Mayhew6b189102019-03-26 18:06:26 -04006781nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07006782{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006783 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07006784
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006785 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006786 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07006787}
6788
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789/*
6790 * failure => all reset bets are off, nfserr_no_grace...
Scott Mayhew6b189102019-03-26 18:06:26 -04006791 *
6792 * The caller is responsible for freeing name.data if NULL is returned (it
6793 * will be freed in nfs4_remove_reclaim_record in the normal case).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05006795struct nfs4_client_reclaim *
Scott Mayhew6b189102019-03-26 18:06:26 -04006796nfs4_client_to_reclaim(struct xdr_netobj name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797{
6798 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006799 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800
Scott Mayhew6b189102019-03-26 18:06:26 -04006801 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", name.len, name.data);
NeilBrowna55370a2005-06-23 22:03:52 -07006802 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05006803 if (crp) {
6804 strhashval = clientstr_hashval(name);
6805 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006806 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Scott Mayhew6b189102019-03-26 18:06:26 -04006807 crp->cr_name.data = name.data;
6808 crp->cr_name.len = name.len;
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006809 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006810 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006811 }
6812 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813}
6814
Jeff Layton2a4317c2012-03-21 16:42:43 -04006815void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006816nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05006817{
6818 list_del(&crp->cr_strhash);
Scott Mayhew6b189102019-03-26 18:06:26 -04006819 kfree(crp->cr_name.data);
Jeff Laytonce30e532012-11-12 15:00:53 -05006820 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006821 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05006822}
6823
6824void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006825nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826{
6827 struct nfs4_client_reclaim *crp = NULL;
6828 int i;
6829
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006831 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6832 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006834 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835 }
6836 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006837 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838}
6839
6840/*
6841 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04006842struct nfs4_client_reclaim *
Scott Mayhew6b189102019-03-26 18:06:26 -04006843nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844{
6845 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846 struct nfs4_client_reclaim *crp = NULL;
6847
Scott Mayhew6b189102019-03-26 18:06:26 -04006848 dprintk("NFSD: nfs4_find_reclaim_client for name %.*s\n", name.len, name.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849
Scott Mayhew6b189102019-03-26 18:06:26 -04006850 strhashval = clientstr_hashval(name);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006851 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Scott Mayhew6b189102019-03-26 18:06:26 -04006852 if (compare_blob(&crp->cr_name, &name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 return crp;
6854 }
6855 }
6856 return NULL;
6857}
6858
6859/*
6860* Called from OPEN. Look for clientid in reclaim list.
6861*/
Al Virob37ad282006-10-19 23:28:59 -07006862__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006863nfs4_check_open_reclaim(clientid_t *clid,
6864 struct nfsd4_compound_state *cstate,
6865 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006867 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04006868
6869 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006870 status = lookup_clientid(clid, cstate, nn);
6871 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04006872 return nfserr_reclaim_bad;
6873
Jeff Layton3b3e7b72014-09-12 16:40:22 -04006874 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6875 return nfserr_no_grace;
6876
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006877 if (nfsd4_client_record_check(cstate->clp))
6878 return nfserr_reclaim_bad;
6879
6880 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881}
6882
Bryan Schumaker65178db2011-11-01 13:35:21 -04006883#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04006884static inline void
6885put_client(struct nfs4_client *clp)
6886{
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04006887 atomic_dec(&clp->cl_rpc_users);
Jeff Layton016200c2014-07-30 08:27:21 -04006888}
6889
Jeff Layton285abde2014-07-30 08:27:24 -04006890static struct nfs4_client *
6891nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6892{
6893 struct nfs4_client *clp;
6894 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6895 nfsd_net_id);
6896
6897 if (!nfsd_netns_ready(nn))
6898 return NULL;
6899
6900 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6901 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
6902 return clp;
6903 }
6904 return NULL;
6905}
6906
Jeff Layton7ec0e362014-07-30 08:27:17 -04006907u64
Jeff Layton285abde2014-07-30 08:27:24 -04006908nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04006909{
6910 struct nfs4_client *clp;
6911 u64 count = 0;
6912 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6913 nfsd_net_id);
6914 char buf[INET6_ADDRSTRLEN];
6915
6916 if (!nfsd_netns_ready(nn))
6917 return 0;
6918
6919 spin_lock(&nn->client_lock);
6920 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6921 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6922 pr_info("NFS Client: %s\n", buf);
6923 ++count;
6924 }
6925 spin_unlock(&nn->client_lock);
6926
6927 return count;
6928}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006929
Jeff Laytona0926d12014-07-30 08:27:18 -04006930u64
Jeff Layton285abde2014-07-30 08:27:24 -04006931nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04006932{
6933 u64 count = 0;
6934 struct nfs4_client *clp;
6935 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6936 nfsd_net_id);
6937
6938 if (!nfsd_netns_ready(nn))
6939 return count;
6940
6941 spin_lock(&nn->client_lock);
6942 clp = nfsd_find_client(addr, addr_size);
6943 if (clp) {
6944 if (mark_client_expired_locked(clp) == nfs_ok)
6945 ++count;
6946 else
6947 clp = NULL;
6948 }
6949 spin_unlock(&nn->client_lock);
6950
6951 if (clp)
6952 expire_client(clp);
6953
6954 return count;
6955}
6956
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006957u64
Jeff Layton285abde2014-07-30 08:27:24 -04006958nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006959{
6960 u64 count = 0;
6961 struct nfs4_client *clp, *next;
6962 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6963 nfsd_net_id);
6964 LIST_HEAD(reaplist);
6965
6966 if (!nfsd_netns_ready(nn))
6967 return count;
6968
6969 spin_lock(&nn->client_lock);
6970 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6971 if (mark_client_expired_locked(clp) == nfs_ok) {
6972 list_add(&clp->cl_lru, &reaplist);
6973 if (max != 0 && ++count >= max)
6974 break;
6975 }
6976 }
6977 spin_unlock(&nn->client_lock);
6978
6979 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6980 expire_client(clp);
6981
6982 return count;
6983}
6984
Bryan Schumaker184c1842012-11-29 11:40:44 -05006985static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6986 const char *type)
6987{
6988 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05006989 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05006990 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6991}
6992
Jeff Layton016200c2014-07-30 08:27:21 -04006993static void
6994nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6995 struct list_head *collect)
6996{
6997 struct nfs4_client *clp = lst->st_stid.sc_client;
6998 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6999 nfsd_net_id);
7000
7001 if (!collect)
7002 return;
7003
7004 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007005 atomic_inc(&clp->cl_rpc_users);
Jeff Layton016200c2014-07-30 08:27:21 -04007006 list_add(&lst->st_locks, collect);
7007}
7008
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04007009static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04007010 struct list_head *collect,
Jeff Laytone8568732015-08-24 12:41:47 -04007011 bool (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05007012{
7013 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05007014 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04007015 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05007016 u64 count = 0;
7017
Jeff Layton016200c2014-07-30 08:27:21 -04007018 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05007019 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04007020 list_for_each_entry_safe(stp, st_next,
7021 &oop->oo_owner.so_stateids, st_perstateowner) {
7022 list_for_each_entry_safe(lst, lst_next,
7023 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04007024 if (func) {
Jeff Laytone8568732015-08-24 12:41:47 -04007025 if (func(lst))
7026 nfsd_inject_add_lock_to_list(lst,
7027 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04007028 }
Jeff Layton016200c2014-07-30 08:27:21 -04007029 ++count;
7030 /*
7031 * Despite the fact that these functions deal
7032 * with 64-bit integers for "count", we must
7033 * ensure that it doesn't blow up the
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007034 * clp->cl_rpc_users. Throw a warning if we
Jeff Layton016200c2014-07-30 08:27:21 -04007035 * start to approach INT_MAX here.
7036 */
7037 WARN_ON_ONCE(count == (INT_MAX / 2));
7038 if (count == max)
7039 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05007040 }
7041 }
7042 }
Jeff Layton016200c2014-07-30 08:27:21 -04007043out:
7044 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05007045
7046 return count;
7047}
7048
Jeff Layton016200c2014-07-30 08:27:21 -04007049static u64
7050nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
7051 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05007052{
Jeff Layton016200c2014-07-30 08:27:21 -04007053 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05007054}
7055
Jeff Layton016200c2014-07-30 08:27:21 -04007056static u64
7057nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05007058{
Jeff Layton016200c2014-07-30 08:27:21 -04007059 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05007060 nfsd_print_count(clp, count, "locked files");
7061 return count;
7062}
7063
Jeff Layton016200c2014-07-30 08:27:21 -04007064u64
Jeff Layton285abde2014-07-30 08:27:24 -04007065nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04007066{
7067 struct nfs4_client *clp;
7068 u64 count = 0;
7069 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7070 nfsd_net_id);
7071
7072 if (!nfsd_netns_ready(nn))
7073 return 0;
7074
7075 spin_lock(&nn->client_lock);
7076 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7077 count += nfsd_print_client_locks(clp);
7078 spin_unlock(&nn->client_lock);
7079
7080 return count;
7081}
7082
7083static void
7084nfsd_reap_locks(struct list_head *reaplist)
7085{
7086 struct nfs4_client *clp;
7087 struct nfs4_ol_stateid *stp, *next;
7088
7089 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
7090 list_del_init(&stp->st_locks);
7091 clp = stp->st_stid.sc_client;
7092 nfs4_put_stid(&stp->st_stid);
7093 put_client(clp);
7094 }
7095}
7096
7097u64
Jeff Layton285abde2014-07-30 08:27:24 -04007098nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04007099{
7100 unsigned int count = 0;
7101 struct nfs4_client *clp;
7102 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7103 nfsd_net_id);
7104 LIST_HEAD(reaplist);
7105
7106 if (!nfsd_netns_ready(nn))
7107 return count;
7108
7109 spin_lock(&nn->client_lock);
7110 clp = nfsd_find_client(addr, addr_size);
7111 if (clp)
7112 count = nfsd_collect_client_locks(clp, &reaplist, 0);
7113 spin_unlock(&nn->client_lock);
7114 nfsd_reap_locks(&reaplist);
7115 return count;
7116}
7117
7118u64
Jeff Layton285abde2014-07-30 08:27:24 -04007119nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04007120{
7121 u64 count = 0;
7122 struct nfs4_client *clp;
7123 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7124 nfsd_net_id);
7125 LIST_HEAD(reaplist);
7126
7127 if (!nfsd_netns_ready(nn))
7128 return count;
7129
7130 spin_lock(&nn->client_lock);
7131 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7132 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
7133 if (max != 0 && count >= max)
7134 break;
7135 }
7136 spin_unlock(&nn->client_lock);
7137 nfsd_reap_locks(&reaplist);
7138 return count;
7139}
7140
Jeff Layton82e05ef2014-07-30 08:27:22 -04007141static u64
7142nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
7143 struct list_head *collect,
7144 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007145{
7146 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04007147 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7148 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007149 u64 count = 0;
7150
Jeff Layton82e05ef2014-07-30 08:27:22 -04007151 lockdep_assert_held(&nn->client_lock);
7152
7153 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007154 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04007155 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007156 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04007157 if (collect) {
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007158 atomic_inc(&clp->cl_rpc_users);
Jeff Layton82e05ef2014-07-30 08:27:22 -04007159 list_add(&oop->oo_perclient, collect);
7160 }
7161 }
7162 ++count;
7163 /*
7164 * Despite the fact that these functions deal with
7165 * 64-bit integers for "count", we must ensure that
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007166 * it doesn't blow up the clp->cl_rpc_users. Throw a
Jeff Layton82e05ef2014-07-30 08:27:22 -04007167 * warning if we start to approach INT_MAX here.
7168 */
7169 WARN_ON_ONCE(count == (INT_MAX / 2));
7170 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007171 break;
7172 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04007173 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007174
7175 return count;
7176}
7177
Jeff Layton82e05ef2014-07-30 08:27:22 -04007178static u64
7179nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007180{
Jeff Layton82e05ef2014-07-30 08:27:22 -04007181 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
7182
7183 nfsd_print_count(clp, count, "openowners");
7184 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007185}
7186
Jeff Layton82e05ef2014-07-30 08:27:22 -04007187static u64
7188nfsd_collect_client_openowners(struct nfs4_client *clp,
7189 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05007190{
Jeff Layton82e05ef2014-07-30 08:27:22 -04007191 return nfsd_foreach_client_openowner(clp, max, collect,
7192 unhash_openowner_locked);
7193}
7194
7195u64
Jeff Layton285abde2014-07-30 08:27:24 -04007196nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04007197{
7198 struct nfs4_client *clp;
7199 u64 count = 0;
7200 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7201 nfsd_net_id);
7202
7203 if (!nfsd_netns_ready(nn))
7204 return 0;
7205
7206 spin_lock(&nn->client_lock);
7207 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7208 count += nfsd_print_client_openowners(clp);
7209 spin_unlock(&nn->client_lock);
7210
7211 return count;
7212}
7213
7214static void
7215nfsd_reap_openowners(struct list_head *reaplist)
7216{
7217 struct nfs4_client *clp;
7218 struct nfs4_openowner *oop, *next;
7219
7220 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
7221 list_del_init(&oop->oo_perclient);
7222 clp = oop->oo_owner.so_client;
7223 release_openowner(oop);
7224 put_client(clp);
7225 }
7226}
7227
7228u64
Jeff Layton285abde2014-07-30 08:27:24 -04007229nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
7230 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04007231{
7232 unsigned int count = 0;
7233 struct nfs4_client *clp;
7234 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7235 nfsd_net_id);
7236 LIST_HEAD(reaplist);
7237
7238 if (!nfsd_netns_ready(nn))
7239 return count;
7240
7241 spin_lock(&nn->client_lock);
7242 clp = nfsd_find_client(addr, addr_size);
7243 if (clp)
7244 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
7245 spin_unlock(&nn->client_lock);
7246 nfsd_reap_openowners(&reaplist);
7247 return count;
7248}
7249
7250u64
Jeff Layton285abde2014-07-30 08:27:24 -04007251nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04007252{
7253 u64 count = 0;
7254 struct nfs4_client *clp;
7255 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7256 nfsd_net_id);
7257 LIST_HEAD(reaplist);
7258
7259 if (!nfsd_netns_ready(nn))
7260 return count;
7261
7262 spin_lock(&nn->client_lock);
7263 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7264 count += nfsd_collect_client_openowners(clp, &reaplist,
7265 max - count);
7266 if (max != 0 && count >= max)
7267 break;
7268 }
7269 spin_unlock(&nn->client_lock);
7270 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05007271 return count;
7272}
7273
Bryan Schumaker269de302012-11-29 11:40:42 -05007274static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
7275 struct list_head *victims)
7276{
7277 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007278 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7279 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05007280 u64 count = 0;
7281
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007282 lockdep_assert_held(&nn->client_lock);
7283
7284 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05007285 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04007286 if (victims) {
7287 /*
7288 * It's not safe to mess with delegations that have a
7289 * non-zero dl_time. They might have already been broken
7290 * and could be processed by the laundromat outside of
7291 * the state_lock. Just leave them be.
7292 */
7293 if (dp->dl_time != 0)
7294 continue;
7295
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007296 atomic_inc(&clp->cl_rpc_users);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007297 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007298 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04007299 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007300 ++count;
7301 /*
7302 * Despite the fact that these functions deal with
7303 * 64-bit integers for "count", we must ensure that
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007304 * it doesn't blow up the clp->cl_rpc_users. Throw a
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007305 * warning if we start to approach INT_MAX here.
7306 */
7307 WARN_ON_ONCE(count == (INT_MAX / 2));
7308 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05007309 break;
7310 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007311 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05007312 return count;
7313}
7314
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007315static u64
7316nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05007317{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007318 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05007319
7320 nfsd_print_count(clp, count, "delegations");
7321 return count;
7322}
7323
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007324u64
Jeff Layton285abde2014-07-30 08:27:24 -04007325nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007326{
7327 struct nfs4_client *clp;
7328 u64 count = 0;
7329 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7330 nfsd_net_id);
7331
7332 if (!nfsd_netns_ready(nn))
7333 return 0;
7334
7335 spin_lock(&nn->client_lock);
7336 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7337 count += nfsd_print_client_delegations(clp);
7338 spin_unlock(&nn->client_lock);
7339
7340 return count;
7341}
7342
7343static void
7344nfsd_forget_delegations(struct list_head *reaplist)
7345{
7346 struct nfs4_client *clp;
7347 struct nfs4_delegation *dp, *next;
7348
7349 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7350 list_del_init(&dp->dl_recall_lru);
7351 clp = dp->dl_stid.sc_client;
7352 revoke_delegation(dp);
7353 put_client(clp);
7354 }
7355}
7356
7357u64
Jeff Layton285abde2014-07-30 08:27:24 -04007358nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
7359 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007360{
7361 u64 count = 0;
7362 struct nfs4_client *clp;
7363 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7364 nfsd_net_id);
7365 LIST_HEAD(reaplist);
7366
7367 if (!nfsd_netns_ready(nn))
7368 return count;
7369
7370 spin_lock(&nn->client_lock);
7371 clp = nfsd_find_client(addr, addr_size);
7372 if (clp)
7373 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7374 spin_unlock(&nn->client_lock);
7375
7376 nfsd_forget_delegations(&reaplist);
7377 return count;
7378}
7379
7380u64
Jeff Layton285abde2014-07-30 08:27:24 -04007381nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007382{
7383 u64 count = 0;
7384 struct nfs4_client *clp;
7385 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7386 nfsd_net_id);
7387 LIST_HEAD(reaplist);
7388
7389 if (!nfsd_netns_ready(nn))
7390 return count;
7391
7392 spin_lock(&nn->client_lock);
7393 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7394 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7395 if (max != 0 && count >= max)
7396 break;
7397 }
7398 spin_unlock(&nn->client_lock);
7399 nfsd_forget_delegations(&reaplist);
7400 return count;
7401}
7402
7403static void
7404nfsd_recall_delegations(struct list_head *reaplist)
7405{
7406 struct nfs4_client *clp;
7407 struct nfs4_delegation *dp, *next;
7408
7409 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7410 list_del_init(&dp->dl_recall_lru);
7411 clp = dp->dl_stid.sc_client;
7412 /*
7413 * We skipped all entries that had a zero dl_time before,
7414 * so we can now reset the dl_time back to 0. If a delegation
7415 * break comes in now, then it won't make any difference since
7416 * we're recalling it either way.
7417 */
7418 spin_lock(&state_lock);
7419 dp->dl_time = 0;
7420 spin_unlock(&state_lock);
7421 nfsd_break_one_deleg(dp);
7422 put_client(clp);
7423 }
7424}
7425
7426u64
Jeff Layton285abde2014-07-30 08:27:24 -04007427nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007428 size_t addr_size)
7429{
7430 u64 count = 0;
7431 struct nfs4_client *clp;
7432 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7433 nfsd_net_id);
7434 LIST_HEAD(reaplist);
7435
7436 if (!nfsd_netns_ready(nn))
7437 return count;
7438
7439 spin_lock(&nn->client_lock);
7440 clp = nfsd_find_client(addr, addr_size);
7441 if (clp)
7442 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7443 spin_unlock(&nn->client_lock);
7444
7445 nfsd_recall_delegations(&reaplist);
7446 return count;
7447}
7448
7449u64
Jeff Layton285abde2014-07-30 08:27:24 -04007450nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007451{
7452 u64 count = 0;
7453 struct nfs4_client *clp, *next;
7454 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7455 nfsd_net_id);
7456 LIST_HEAD(reaplist);
7457
7458 if (!nfsd_netns_ready(nn))
7459 return count;
7460
7461 spin_lock(&nn->client_lock);
7462 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7463 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7464 if (max != 0 && ++count >= max)
7465 break;
7466 }
7467 spin_unlock(&nn->client_lock);
7468 nfsd_recall_delegations(&reaplist);
7469 return count;
7470}
Bryan Schumaker65178db2011-11-01 13:35:21 -04007471#endif /* CONFIG_NFSD_FAULT_INJECTION */
7472
Meelap Shahc2f1a552007-07-17 04:04:39 -07007473/*
7474 * Since the lifetime of a delegation isn't limited to that of an open, a
7475 * client may quite reasonably hang on to a delegation as long as it has
7476 * the inode cached. This becomes an obvious problem the first time a
7477 * client's inode cache approaches the size of the server's total memory.
7478 *
7479 * For now we avoid this problem by imposing a hard limit on the number
7480 * of delegations, which varies according to the server's memory size.
7481 */
7482static void
7483set_max_delegations(void)
7484{
7485 /*
7486 * Allow at most 4 delegations per megabyte of RAM. Quick
7487 * estimates suggest that in the worst case (where every delegation
7488 * is for a different inode), a delegation could take about 1.5K,
7489 * giving a worst case usage of about 6% of memory.
7490 */
7491 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7492}
7493
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007494static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007495{
7496 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7497 int i;
7498
Kees Cook6da2ec52018-06-12 13:55:00 -07007499 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7500 sizeof(struct list_head),
7501 GFP_KERNEL);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007502 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007503 goto err;
Kees Cook6da2ec52018-06-12 13:55:00 -07007504 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7505 sizeof(struct list_head),
7506 GFP_KERNEL);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007507 if (!nn->unconf_id_hashtbl)
7508 goto err_unconf_id;
Kees Cook6da2ec52018-06-12 13:55:00 -07007509 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7510 sizeof(struct list_head),
7511 GFP_KERNEL);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007512 if (!nn->sessionid_hashtbl)
7513 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007514
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007515 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007516 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007517 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007518 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007519 for (i = 0; i < SESSION_HASH_SIZE; i++)
7520 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007521 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007522 nn->unconf_name_tree = RB_ROOT;
Vasily Averin81833de2017-11-13 07:25:40 +03007523 nn->boot_time = get_seconds();
7524 nn->grace_ended = false;
7525 nn->nfsd4_manager.block_opens = true;
7526 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03007527 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03007528 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007529 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03007530 spin_lock_init(&nn->client_lock);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04007531 spin_lock_init(&nn->s2s_cp_lock);
7532 idr_init(&nn->s2s_cp_stateids);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007533
Jeff Layton0cc11a62016-10-20 09:34:31 -04007534 spin_lock_init(&nn->blocked_locks_lock);
7535 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7536
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007537 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007538 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007539
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007540 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007541
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007542err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03007543 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007544err_unconf_id:
7545 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007546err:
7547 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007548}
7549
7550static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007551nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007552{
7553 int i;
7554 struct nfs4_client *clp = NULL;
7555 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7556
7557 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7558 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7559 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7560 destroy_client(clp);
7561 }
7562 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007563
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007564 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7565
Kinglong Mee2b905632014-03-26 22:09:30 +08007566 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7567 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7568 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7569 destroy_client(clp);
7570 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007571 }
7572
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007573 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007574 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007575 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007576 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007577}
7578
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007579int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007580nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07007581{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04007582 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007583 int ret;
7584
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007585 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007586 if (ret)
7587 return ret;
Jeff Laytond4318ac2014-09-12 16:40:21 -04007588 locks_start_grace(net, &nn->nfsd4_manager);
7589 nfsd4_client_tracking_init(net);
Scott Mayhew362063a2019-03-26 18:06:28 -04007590 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7591 goto skip_grace;
Vasily Averin7e981a82017-11-06 16:46:04 +03007592 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %x)\n",
7593 nn->nfsd4_grace, net->ns.inum);
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03007594 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007595 return 0;
Scott Mayhew362063a2019-03-26 18:06:28 -04007596
7597skip_grace:
7598 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7599 net->ns.inum);
7600 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7601 nfsd4_end_grace(nn);
7602 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007603}
7604
7605/* initialization to perform when the nfsd service is started: */
7606
7607int
7608nfs4_state_start(void)
7609{
7610 int ret;
7611
Jeff Layton51a54562015-08-20 07:17:01 -04007612 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05007613 if (laundry_wq == NULL) {
7614 ret = -ENOMEM;
Chuck Levera26dd642018-08-16 12:06:09 -04007615 goto out;
Jeff Laytona6d6b782012-03-05 11:42:36 -05007616 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007617 ret = nfsd4_create_callback_queue();
7618 if (ret)
7619 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007620
Meelap Shahc2f1a552007-07-17 04:04:39 -07007621 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007622 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007623
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007624out_free_laundry:
7625 destroy_workqueue(laundry_wq);
Chuck Levera26dd642018-08-16 12:06:09 -04007626out:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007627 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007628}
7629
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007630void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007631nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007634 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007635 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007636
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007637 cancel_delayed_work_sync(&nn->laundromat_work);
7638 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05007639
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04007641 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007642 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007643 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007644 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007645 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007646 }
Benny Halevycdc975052014-05-30 09:09:30 -04007647 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007648 list_for_each_safe(pos, next, &reaplist) {
7649 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04007650 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05007651 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007652 }
7653
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007654 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007655 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656}
7657
7658void
7659nfs4_state_shutdown(void)
7660{
NeilBrown5e8d5c22006-04-10 22:55:37 -07007661 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04007662 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007663}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007664
7665static void
7666get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7667{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007668 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
7669 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007670}
7671
7672static void
7673put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7674{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007675 if (cstate->minorversion) {
7676 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7677 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7678 }
7679}
7680
7681void
7682clear_current_stateid(struct nfsd4_compound_state *cstate)
7683{
7684 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007685}
7686
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007687/*
7688 * functions to set current state id
7689 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007690void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007691nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7692 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007693{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007694 put_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007695}
7696
7697void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007698nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7699 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007700{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007701 put_stateid(cstate, &u->open.op_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007702}
7703
7704void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007705nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7706 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007707{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007708 put_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007709}
7710
7711void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007712nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7713 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007714{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007715 put_stateid(cstate, &u->lock.lk_resp_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007716}
7717
7718/*
7719 * functions to consume current state id
7720 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007721
7722void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007723nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7724 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007725{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007726 get_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007727}
7728
7729void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007730nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7731 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007732{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007733 get_stateid(cstate, &u->delegreturn.dr_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007734}
7735
7736void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007737nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7738 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007739{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007740 get_stateid(cstate, &u->free_stateid.fr_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007741}
7742
7743void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007744nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7745 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007746{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007747 get_stateid(cstate, &u->setattr.sa_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007748}
7749
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007750void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007751nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7752 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007753{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007754 get_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007755}
7756
7757void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007758nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7759 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007760{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007761 get_stateid(cstate, &u->locku.lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007762}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007763
7764void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007765nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7766 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007767{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007768 get_stateid(cstate, &u->read.rd_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007769}
7770
7771void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007772nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7773 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007774{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007775 get_stateid(cstate, &u->write.wr_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007776}