blob: 0c04f81aa63b225b2207b226b1113e1973ec1e1b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Chuck Lever7df302f2012-05-29 13:56:37 -040041#include <linux/ratelimit.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050042#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton59766872013-02-04 12:50:00 -050043#include <linux/sunrpc/addr.h>
Daniel Borkmann87545892014-12-10 16:33:11 +010044#include <linux/jhash.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020045#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040046#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050047#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040048#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040050#include "netns.h"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020051#include "pnfs.h"
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define NFSDDBG_FACILITY NFSDDBG_PROC
54
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050055#define all_ones {{~0,~0},~0}
56static const stateid_t one_stateid = {
57 .si_generation = ~0,
58 .si_opaque = all_ones,
59};
60static const stateid_t zero_stateid = {
61 /* all fields zero */
62};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010063static const stateid_t currentstateid = {
64 .si_generation = 1,
65};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050066
Andy Adamsonec6b5d72009-04-03 08:28:28 +030067static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070068
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050069#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
70#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010071#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040074static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040075static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
J. Bruce Fields8b671b82009-02-22 14:51:34 -080077/* Locking: */
78
J. Bruce Fields8b671b82009-02-22 14:51:34 -080079/*
80 * Currently used for the del_recall_lru and file hash table. In an
81 * effort to decrease the scope of the client_mutex, this spinlock may
82 * eventually cover more:
83 */
Benny Halevycdc975052014-05-30 09:09:30 -040084static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080085
Jeff Laytonb401be222014-07-29 21:34:33 -040086/*
87 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
88 * the refcount on the open stateid to drop.
89 */
90static DECLARE_WAIT_QUEUE_HEAD(close_wq);
91
Christoph Hellwigabf11352014-05-21 07:43:03 -070092static struct kmem_cache *openowner_slab;
93static struct kmem_cache *lockowner_slab;
94static struct kmem_cache *file_slab;
95static struct kmem_cache *stateid_slab;
96static struct kmem_cache *deleg_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +020097static struct kmem_cache *odstate_slab;
NeilBrowne60d4392005-06-23 22:03:01 -070098
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -040099static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800100
Julia Lawallc4cb8972015-11-21 22:57:39 +0100101static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
Jeff Layton76d348f2016-09-16 16:28:24 -0400102static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200103
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400104static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800105{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400106 return ses->se_flags & NFS4_SESSION_DEAD;
107}
108
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400109static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
110{
111 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400112 return nfserr_jukebox;
113 ses->se_flags |= NFS4_SESSION_DEAD;
114 return nfs_ok;
115}
116
J. Bruce Fields221a6872013-04-01 22:23:49 -0400117static bool is_client_expired(struct nfs4_client *clp)
118{
119 return clp->cl_time == 0;
120}
121
J. Bruce Fields221a6872013-04-01 22:23:49 -0400122static __be32 get_client_locked(struct nfs4_client *clp)
123{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400124 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
125
126 lockdep_assert_held(&nn->client_lock);
127
J. Bruce Fields221a6872013-04-01 22:23:49 -0400128 if (is_client_expired(clp))
129 return nfserr_expired;
130 atomic_inc(&clp->cl_refcount);
131 return nfs_ok;
132}
133
134/* must be called under the client_lock */
135static inline void
136renew_client_locked(struct nfs4_client *clp)
137{
138 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
139
140 if (is_client_expired(clp)) {
141 WARN_ON(1);
142 printk("%s: client (clientid %08x/%08x) already expired\n",
143 __func__,
144 clp->cl_clientid.cl_boot,
145 clp->cl_clientid.cl_id);
146 return;
147 }
148
149 dprintk("renewing client (clientid %08x/%08x)\n",
150 clp->cl_clientid.cl_boot,
151 clp->cl_clientid.cl_id);
152 list_move_tail(&clp->cl_lru, &nn->client_lru);
153 clp->cl_time = get_seconds();
154}
155
Fengguang Wuba138432013-04-16 22:14:15 -0400156static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400157{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400158 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
159
160 lockdep_assert_held(&nn->client_lock);
161
J. Bruce Fields221a6872013-04-01 22:23:49 -0400162 if (!atomic_dec_and_test(&clp->cl_refcount))
163 return;
164 if (!is_client_expired(clp))
165 renew_client_locked(clp);
166}
167
Jeff Layton4b24ca72014-06-30 11:48:44 -0400168static void put_client_renew(struct nfs4_client *clp)
169{
170 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
171
Jeff Laytond6c249b2014-07-08 14:02:50 -0400172 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
173 return;
174 if (!is_client_expired(clp))
175 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400176 spin_unlock(&nn->client_lock);
177}
178
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400179static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
180{
181 __be32 status;
182
183 if (is_session_dead(ses))
184 return nfserr_badsession;
185 status = get_client_locked(ses->se_client);
186 if (status)
187 return status;
188 atomic_inc(&ses->se_ref);
189 return nfs_ok;
190}
191
192static void nfsd4_put_session_locked(struct nfsd4_session *ses)
193{
194 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400195 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
196
197 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400198
199 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
200 free_session(ses);
201 put_client_renew_locked(clp);
202}
203
204static void nfsd4_put_session(struct nfsd4_session *ses)
205{
206 struct nfs4_client *clp = ses->se_client;
207 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
208
209 spin_lock(&nn->client_lock);
210 nfsd4_put_session_locked(ses);
211 spin_unlock(&nn->client_lock);
212}
213
Jeff Layton76d348f2016-09-16 16:28:24 -0400214static struct nfsd4_blocked_lock *
215find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
216 struct nfsd_net *nn)
217{
218 struct nfsd4_blocked_lock *cur, *found = NULL;
219
Jeff Layton0cc11a62016-10-20 09:34:31 -0400220 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400221 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
222 if (fh_match(fh, &cur->nbl_fh)) {
223 list_del_init(&cur->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -0400224 list_del_init(&cur->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400225 found = cur;
226 break;
227 }
228 }
Jeff Layton0cc11a62016-10-20 09:34:31 -0400229 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400230 if (found)
231 posix_unblock_lock(&found->nbl_lock);
232 return found;
233}
234
235static struct nfsd4_blocked_lock *
236find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
237 struct nfsd_net *nn)
238{
239 struct nfsd4_blocked_lock *nbl;
240
241 nbl = find_blocked_lock(lo, fh, nn);
242 if (!nbl) {
243 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
244 if (nbl) {
245 fh_copy_shallow(&nbl->nbl_fh, fh);
246 locks_init_lock(&nbl->nbl_lock);
247 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
248 &nfsd4_cb_notify_lock_ops,
249 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
250 }
251 }
252 return nbl;
253}
254
255static void
256free_blocked_lock(struct nfsd4_blocked_lock *nbl)
257{
258 locks_release_private(&nbl->nbl_lock);
259 kfree(nbl);
260}
261
262static int
263nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
264{
265 /*
266 * Since this is just an optimization, we don't try very hard if it
267 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
268 * just quit trying on anything else.
269 */
270 switch (task->tk_status) {
271 case -NFS4ERR_DELAY:
272 rpc_delay(task, 1 * HZ);
273 return 0;
274 default:
275 return 1;
276 }
277}
278
279static void
280nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
281{
282 struct nfsd4_blocked_lock *nbl = container_of(cb,
283 struct nfsd4_blocked_lock, nbl_cb);
284
285 free_blocked_lock(nbl);
286}
287
288static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
289 .done = nfsd4_cb_notify_lock_done,
290 .release = nfsd4_cb_notify_lock_release,
291};
292
Kinglong Meeb5971af2014-08-22 10:18:43 -0400293static inline struct nfs4_stateowner *
294nfs4_get_stateowner(struct nfs4_stateowner *sop)
295{
296 atomic_inc(&sop->so_count);
297 return sop;
298}
299
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400300static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400301same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400302{
303 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400304 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400305}
306
307static struct nfs4_openowner *
308find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400309 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400310{
311 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400312
Trond Myklebustd4f04892014-07-29 21:34:36 -0400313 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400314
Trond Myklebustd4f04892014-07-29 21:34:36 -0400315 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
316 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400317 if (!so->so_is_open_owner)
318 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400319 if (same_owner_str(so, &open->op_owner))
320 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400321 }
322 return NULL;
323}
324
325static struct nfs4_openowner *
326find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400327 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400328{
329 struct nfs4_openowner *oo;
330
Trond Myklebustd4f04892014-07-29 21:34:36 -0400331 spin_lock(&clp->cl_lock);
332 oo = find_openstateowner_str_locked(hashval, open, clp);
333 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400334 return oo;
335}
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337static inline u32
338opaque_hashval(const void *ptr, int nbytes)
339{
340 unsigned char *cptr = (unsigned char *) ptr;
341
342 u32 x = 0;
343 while (nbytes--) {
344 x *= 37;
345 x += *cptr++;
346 }
347 return x;
348}
349
Jeff Layton5b095e92014-10-23 08:01:02 -0400350static void nfsd4_free_file_rcu(struct rcu_head *rcu)
J. Bruce Fields32513b42011-10-13 16:00:16 -0400351{
Jeff Layton5b095e92014-10-23 08:01:02 -0400352 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
353
354 kmem_cache_free(file_slab, fp);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400355}
356
Christoph Hellwige6ba76e2014-08-14 08:50:16 +0200357void
NeilBrown13cd2182005-06-23 22:03:10 -0700358put_nfs4_file(struct nfs4_file *fi)
359{
Jeff Layton02e12152014-07-16 10:31:57 -0400360 might_lock(&state_lock);
361
Benny Halevycdc975052014-05-30 09:09:30 -0400362 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton5b095e92014-10-23 08:01:02 -0400363 hlist_del_rcu(&fi->fi_hash);
Benny Halevycdc975052014-05-30 09:09:30 -0400364 spin_unlock(&state_lock);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200365 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
Jeff Layton5b095e92014-10-23 08:01:02 -0400366 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
367 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800368 }
NeilBrown13cd2182005-06-23 22:03:10 -0700369}
370
Trond Myklebustde186432014-07-10 14:07:26 -0400371static struct file *
372__nfs4_get_fd(struct nfs4_file *f, int oflag)
373{
374 if (f->fi_fds[oflag])
375 return get_file(f->fi_fds[oflag]);
376 return NULL;
377}
378
379static struct file *
380find_writeable_file_locked(struct nfs4_file *f)
381{
382 struct file *ret;
383
384 lockdep_assert_held(&f->fi_lock);
385
386 ret = __nfs4_get_fd(f, O_WRONLY);
387 if (!ret)
388 ret = __nfs4_get_fd(f, O_RDWR);
389 return ret;
390}
391
392static struct file *
393find_writeable_file(struct nfs4_file *f)
394{
395 struct file *ret;
396
397 spin_lock(&f->fi_lock);
398 ret = find_writeable_file_locked(f);
399 spin_unlock(&f->fi_lock);
400
401 return ret;
402}
403
404static struct file *find_readable_file_locked(struct nfs4_file *f)
405{
406 struct file *ret;
407
408 lockdep_assert_held(&f->fi_lock);
409
410 ret = __nfs4_get_fd(f, O_RDONLY);
411 if (!ret)
412 ret = __nfs4_get_fd(f, O_RDWR);
413 return ret;
414}
415
416static struct file *
417find_readable_file(struct nfs4_file *f)
418{
419 struct file *ret;
420
421 spin_lock(&f->fi_lock);
422 ret = find_readable_file_locked(f);
423 spin_unlock(&f->fi_lock);
424
425 return ret;
426}
427
Christoph Hellwig4d227fc2014-08-17 07:40:00 -0500428struct file *
Trond Myklebustde186432014-07-10 14:07:26 -0400429find_any_file(struct nfs4_file *f)
430{
431 struct file *ret;
432
433 spin_lock(&f->fi_lock);
434 ret = __nfs4_get_fd(f, O_RDWR);
435 if (!ret) {
436 ret = __nfs4_get_fd(f, O_WRONLY);
437 if (!ret)
438 ret = __nfs4_get_fd(f, O_RDONLY);
439 }
440 spin_unlock(&f->fi_lock);
441 return ret;
442}
443
Trond Myklebust02a35082014-07-25 07:34:22 -0400444static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800445unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700446
447/*
448 * Open owner state (share locks)
449 */
450
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500451/* hash tables for lock and open owners */
452#define OWNER_HASH_BITS 8
453#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
454#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700455
Trond Myklebustd4f04892014-07-29 21:34:36 -0400456static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400457{
458 unsigned int ret;
459
460 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500461 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400462}
NeilBrownef0f3392006-04-10 22:55:41 -0700463
NeilBrownef0f3392006-04-10 22:55:41 -0700464/* hash table for nfs4_file */
465#define FILE_HASH_BITS 8
466#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800467
Trond Myklebustca943212014-07-23 16:17:39 -0400468static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400469{
Trond Myklebustca943212014-07-23 16:17:39 -0400470 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
471}
472
473static unsigned int file_hashval(struct knfsd_fh *fh)
474{
475 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
476}
477
Jeff Layton89876f82013-04-02 09:01:59 -0400478static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700479
Jeff Layton12659652014-07-10 14:07:28 -0400480static void
481__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400482{
Jeff Layton7214e862014-07-10 14:07:33 -0400483 lockdep_assert_held(&fp->fi_lock);
484
Jeff Layton12659652014-07-10 14:07:28 -0400485 if (access & NFS4_SHARE_ACCESS_WRITE)
486 atomic_inc(&fp->fi_access[O_WRONLY]);
487 if (access & NFS4_SHARE_ACCESS_READ)
488 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400489}
490
Jeff Layton12659652014-07-10 14:07:28 -0400491static __be32
492nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400493{
Jeff Layton7214e862014-07-10 14:07:33 -0400494 lockdep_assert_held(&fp->fi_lock);
495
Jeff Layton12659652014-07-10 14:07:28 -0400496 /* Does this access mode make sense? */
497 if (access & ~NFS4_SHARE_ACCESS_BOTH)
498 return nfserr_inval;
499
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400500 /* Does it conflict with a deny mode already set? */
501 if ((access & fp->fi_share_deny) != 0)
502 return nfserr_share_denied;
503
Jeff Layton12659652014-07-10 14:07:28 -0400504 __nfs4_file_get_access(fp, access);
505 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400506}
507
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400508static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
509{
510 /* Common case is that there is no deny mode. */
511 if (deny) {
512 /* Does this deny mode make sense? */
513 if (deny & ~NFS4_SHARE_DENY_BOTH)
514 return nfserr_inval;
515
516 if ((deny & NFS4_SHARE_DENY_READ) &&
517 atomic_read(&fp->fi_access[O_RDONLY]))
518 return nfserr_share_denied;
519
520 if ((deny & NFS4_SHARE_DENY_WRITE) &&
521 atomic_read(&fp->fi_access[O_WRONLY]))
522 return nfserr_share_denied;
523 }
524 return nfs_ok;
525}
526
J. Bruce Fields998db522010-08-07 09:21:41 -0400527static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400528{
Trond Myklebustde186432014-07-10 14:07:26 -0400529 might_lock(&fp->fi_lock);
530
531 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
532 struct file *f1 = NULL;
533 struct file *f2 = NULL;
534
Jeff Layton6d338b52014-07-10 14:07:29 -0400535 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400536 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400537 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400538 spin_unlock(&fp->fi_lock);
539 if (f1)
540 fput(f1);
541 if (f2)
542 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400543 }
544}
545
Jeff Layton12659652014-07-10 14:07:28 -0400546static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400547{
Jeff Layton12659652014-07-10 14:07:28 -0400548 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
549
550 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400551 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400552 if (access & NFS4_SHARE_ACCESS_READ)
553 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400554}
555
Sachin Bhamare8287f002015-04-27 14:50:14 +0200556/*
557 * Allocate a new open/delegation state counter. This is needed for
558 * pNFS for proper return on close semantics.
559 *
560 * Note that we only allocate it for pNFS-enabled exports, otherwise
561 * all pointers to struct nfs4_clnt_odstate are always NULL.
562 */
563static struct nfs4_clnt_odstate *
564alloc_clnt_odstate(struct nfs4_client *clp)
565{
566 struct nfs4_clnt_odstate *co;
567
568 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
569 if (co) {
570 co->co_client = clp;
571 atomic_set(&co->co_odcount, 1);
572 }
573 return co;
574}
575
576static void
577hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
578{
579 struct nfs4_file *fp = co->co_file;
580
581 lockdep_assert_held(&fp->fi_lock);
582 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
583}
584
585static inline void
586get_clnt_odstate(struct nfs4_clnt_odstate *co)
587{
588 if (co)
589 atomic_inc(&co->co_odcount);
590}
591
592static void
593put_clnt_odstate(struct nfs4_clnt_odstate *co)
594{
595 struct nfs4_file *fp;
596
597 if (!co)
598 return;
599
600 fp = co->co_file;
601 if (atomic_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
602 list_del(&co->co_perfile);
603 spin_unlock(&fp->fi_lock);
604
605 nfsd4_return_all_file_layouts(co->co_client, fp);
606 kmem_cache_free(odstate_slab, co);
607 }
608}
609
610static struct nfs4_clnt_odstate *
611find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
612{
613 struct nfs4_clnt_odstate *co;
614 struct nfs4_client *cl;
615
616 if (!new)
617 return NULL;
618
619 cl = new->co_client;
620
621 spin_lock(&fp->fi_lock);
622 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
623 if (co->co_client == cl) {
624 get_clnt_odstate(co);
625 goto out;
626 }
627 }
628 co = new;
629 co->co_file = fp;
630 hash_clnt_odstate_locked(new);
631out:
632 spin_unlock(&fp->fi_lock);
633 return co;
634}
635
Kinglong Meed19fb702017-01-18 19:04:42 +0800636struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
637 void (*sc_free)(struct nfs4_stid *))
J. Bruce Fields996e0932011-10-17 11:14:48 -0400638{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500639 struct nfs4_stid *stid;
640 int new_id;
641
Trond Myklebustf8338832014-07-25 07:34:19 -0400642 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500643 if (!stid)
644 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400645
Jeff Layton4770d722014-07-29 21:34:10 -0400646 idr_preload(GFP_KERNEL);
647 spin_lock(&cl->cl_lock);
648 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
649 spin_unlock(&cl->cl_lock);
650 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700651 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500652 goto out_free;
Kinglong Meed19fb702017-01-18 19:04:42 +0800653
654 stid->sc_free = sc_free;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500655 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500656 stid->sc_stateid.si_opaque.so_id = new_id;
657 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
658 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400659 atomic_set(&stid->sc_count, 1);
Jeff Layton9767feb2015-10-01 09:05:50 -0400660 spin_lock_init(&stid->sc_lock);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500661
J. Bruce Fields996e0932011-10-17 11:14:48 -0400662 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500663 * It shouldn't be a problem to reuse an opaque stateid value.
664 * I don't think it is for 4.1. But with 4.0 I worry that, for
665 * example, a stray write retransmission could be accepted by
666 * the server when it should have been rejected. Therefore,
667 * adopt a trick from the sctp code to attempt to maximize the
668 * amount of time until an id is reused, by ensuring they always
669 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400670 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500671 return stid;
672out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800673 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500674 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400675}
676
Jeff Laytonb49e0842014-07-29 21:34:11 -0400677static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400678{
Trond Myklebust60116952014-07-29 21:34:06 -0400679 struct nfs4_stid *stid;
Trond Myklebust60116952014-07-29 21:34:06 -0400680
Kinglong Meed19fb702017-01-18 19:04:42 +0800681 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
Trond Myklebust60116952014-07-29 21:34:06 -0400682 if (!stid)
683 return NULL;
684
Kinglong Meed19fb702017-01-18 19:04:42 +0800685 return openlockstateid(stid);
Trond Myklebust60116952014-07-29 21:34:06 -0400686}
687
688static void nfs4_free_deleg(struct nfs4_stid *stid)
689{
Trond Myklebust60116952014-07-29 21:34:06 -0400690 kmem_cache_free(deleg_slab, stid);
691 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400692}
693
NeilBrown6282cd52014-06-04 17:39:26 +1000694/*
695 * When we recall a delegation, we should be careful not to hand it
696 * out again straight away.
697 * To ensure this we keep a pair of bloom filters ('new' and 'old')
698 * in which the filehandles of recalled delegations are "stored".
699 * If a filehandle appear in either filter, a delegation is blocked.
700 * When a delegation is recalled, the filehandle is stored in the "new"
701 * filter.
702 * Every 30 seconds we swap the filters and clear the "new" one,
703 * unless both are empty of course.
704 *
705 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
706 * low 3 bytes as hash-table indices.
707 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400708 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000709 * is used to manage concurrent access. Testing does not need the lock
710 * except when swapping the two filters.
711 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400712static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000713static struct bloom_pair {
714 int entries, old_entries;
715 time_t swap_time;
716 int new; /* index into 'set' */
717 DECLARE_BITMAP(set[2], 256);
718} blocked_delegations;
719
720static int delegation_blocked(struct knfsd_fh *fh)
721{
722 u32 hash;
723 struct bloom_pair *bd = &blocked_delegations;
724
725 if (bd->entries == 0)
726 return 0;
727 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400728 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000729 if (seconds_since_boot() - bd->swap_time > 30) {
730 bd->entries -= bd->old_entries;
731 bd->old_entries = bd->entries;
732 memset(bd->set[bd->new], 0,
733 sizeof(bd->set[0]));
734 bd->new = 1-bd->new;
735 bd->swap_time = seconds_since_boot();
736 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400737 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000738 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100739 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000740 if (test_bit(hash&255, bd->set[0]) &&
741 test_bit((hash>>8)&255, bd->set[0]) &&
742 test_bit((hash>>16)&255, bd->set[0]))
743 return 1;
744
745 if (test_bit(hash&255, bd->set[1]) &&
746 test_bit((hash>>8)&255, bd->set[1]) &&
747 test_bit((hash>>16)&255, bd->set[1]))
748 return 1;
749
750 return 0;
751}
752
753static void block_delegations(struct knfsd_fh *fh)
754{
755 u32 hash;
756 struct bloom_pair *bd = &blocked_delegations;
757
Daniel Borkmann87545892014-12-10 16:33:11 +0100758 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000759
Jeff Laytonf54fe962014-07-25 07:34:26 -0400760 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000761 __set_bit(hash&255, bd->set[bd->new]);
762 __set_bit((hash>>8)&255, bd->set[bd->new]);
763 __set_bit((hash>>16)&255, bd->set[bd->new]);
764 if (bd->entries == 0)
765 bd->swap_time = seconds_since_boot();
766 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400767 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770static struct nfs4_delegation *
Sachin Bhamare8287f002015-04-27 14:50:14 +0200771alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh,
772 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400775 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400778 n = atomic_long_inc_return(&num_delegations);
779 if (n < 0 || n > max_delegations)
780 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000781 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400782 goto out_dec;
Kinglong Meed19fb702017-01-18 19:04:42 +0800783 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700784 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400785 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400786
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400787 /*
788 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400789 * meaning of seqid 0 isn't meaningful, really, but let's avoid
790 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400791 */
792 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700793 INIT_LIST_HEAD(&dp->dl_perfile);
794 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200796 dp->dl_clnt_odstate = odstate;
797 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400798 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200799 dp->dl_retries = 1;
800 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200801 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400803out_dec:
804 atomic_long_dec(&num_delegations);
805 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
808void
Trond Myklebust60116952014-07-29 21:34:06 -0400809nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Trond Myklebust11b91642014-07-29 21:34:08 -0400811 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400812 struct nfs4_client *clp = s->sc_client;
813
Jeff Layton4770d722014-07-29 21:34:10 -0400814 might_lock(&clp->cl_lock);
815
Jeff Laytonb401be222014-07-29 21:34:33 -0400816 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
817 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400818 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400819 }
Trond Myklebust60116952014-07-29 21:34:06 -0400820 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400821 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400822 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400823 if (fp)
824 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
Jeff Layton9767feb2015-10-01 09:05:50 -0400827void
828nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
829{
830 stateid_t *src = &stid->sc_stateid;
831
832 spin_lock(&stid->sc_lock);
833 if (unlikely(++src->si_generation == 0))
834 src->si_generation = 1;
835 memcpy(dst, src, sizeof(*dst));
836 spin_unlock(&stid->sc_lock);
837}
838
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500839static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Jeff Layton6bcc0342014-08-09 10:22:40 -0400841 struct file *filp = NULL;
Jeff Layton417c6622014-07-21 09:34:57 -0400842
Jeff Layton6bcc0342014-08-09 10:22:40 -0400843 spin_lock(&fp->fi_lock);
Jeff Layton67db1032014-12-13 09:11:40 -0500844 if (fp->fi_deleg_file && --fp->fi_delegees == 0)
Jeff Layton6bcc0342014-08-09 10:22:40 -0400845 swap(filp, fp->fi_deleg_file);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400846 spin_unlock(&fp->fi_lock);
847
848 if (filp) {
Christoph Hellwig2ab99ee2015-01-21 19:14:02 +0100849 vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400850 fput(filp);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200854void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400855{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500856 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400857}
858
Andrew Elble34ed9872015-10-15 12:07:28 -0400859/**
860 * nfs4_get_existing_delegation - Discover if this delegation already exists
861 * @clp: a pointer to the nfs4_client we're granting a delegation to
862 * @fp: a pointer to the nfs4_file we're granting a delegation on
863 *
864 * Return:
865 * On success: NULL if an existing delegation was not found.
866 *
867 * On error: -EAGAIN if one was previously granted to this nfs4_client
868 * for this nfs4_file.
869 *
870 */
871
872static int
873nfs4_get_existing_delegation(struct nfs4_client *clp, struct nfs4_file *fp)
Benny Halevy931ee562014-05-30 09:09:27 -0400874{
Andrew Elble34ed9872015-10-15 12:07:28 -0400875 struct nfs4_delegation *searchdp = NULL;
876 struct nfs4_client *searchclp = NULL;
877
Benny Halevycdc975052014-05-30 09:09:30 -0400878 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400879 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400880
Andrew Elble34ed9872015-10-15 12:07:28 -0400881 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
882 searchclp = searchdp->dl_stid.sc_client;
883 if (clp == searchclp) {
884 return -EAGAIN;
885 }
886 }
887 return 0;
888}
889
890/**
891 * hash_delegation_locked - Add a delegation to the appropriate lists
892 * @dp: a pointer to the nfs4_delegation we are adding.
893 * @fp: a pointer to the nfs4_file we're granting a delegation on
894 *
895 * Return:
896 * On success: NULL if the delegation was successfully hashed.
897 *
898 * On error: -EAGAIN if one was previously granted to this
899 * nfs4_client for this nfs4_file. Delegation is not hashed.
900 *
901 */
902
903static int
904hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
905{
906 int status;
907 struct nfs4_client *clp = dp->dl_stid.sc_client;
908
909 lockdep_assert_held(&state_lock);
910 lockdep_assert_held(&fp->fi_lock);
911
912 status = nfs4_get_existing_delegation(clp, fp);
913 if (status)
914 return status;
915 ++fp->fi_delegees;
Trond Myklebust67cb1272014-07-29 21:34:17 -0400916 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400917 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400918 list_add(&dp->dl_perfile, &fp->fi_delegations);
Andrew Elble34ed9872015-10-15 12:07:28 -0400919 list_add(&dp->dl_perclnt, &clp->cl_delegations);
920 return 0;
Benny Halevy931ee562014-05-30 09:09:27 -0400921}
922
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400923static bool
Jeff Layton42690672014-07-25 07:34:20 -0400924unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Trond Myklebust11b91642014-07-29 21:34:08 -0400926 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400927
Jeff Layton42690672014-07-25 07:34:20 -0400928 lockdep_assert_held(&state_lock);
929
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400930 if (list_empty(&dp->dl_perfile))
931 return false;
932
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400933 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400934 /* Ensure that deleg break won't try to requeue it */
935 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400936 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400937 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400939 list_del_init(&dp->dl_perfile);
940 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400941 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400942}
943
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400944static void destroy_delegation(struct nfs4_delegation *dp)
945{
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400946 bool unhashed;
947
Jeff Layton42690672014-07-25 07:34:20 -0400948 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400949 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -0400950 spin_unlock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400951 if (unhashed) {
952 put_clnt_odstate(dp->dl_clnt_odstate);
953 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
954 nfs4_put_stid(&dp->dl_stid);
955 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400956}
957
958static void revoke_delegation(struct nfs4_delegation *dp)
959{
960 struct nfs4_client *clp = dp->dl_stid.sc_client;
961
Jeff Layton2d4a5322014-07-25 07:34:21 -0400962 WARN_ON(!list_empty(&dp->dl_recall_lru));
963
Sachin Bhamare8287f002015-04-27 14:50:14 +0200964 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -0400965 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
966
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400967 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400968 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400969 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400970 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400971 spin_lock(&clp->cl_lock);
972 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
973 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400974 }
975}
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977/*
978 * SETCLIENTID state
979 */
980
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400981static unsigned int clientid_hashval(u32 id)
982{
983 return id & CLIENT_HASH_MASK;
984}
985
986static unsigned int clientstr_hashval(const char *name)
987{
988 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
989}
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400992 * We store the NONE, READ, WRITE, and BOTH bits separately in the
993 * st_{access,deny}_bmap field of the stateid, in order to track not
994 * only what share bits are currently in force, but also what
995 * combinations of share bits previous opens have used. This allows us
996 * to enforce the recommendation of rfc 3530 14.2.19 that the server
997 * return an error if the client attempt to downgrade to a combination
998 * of share bits not explicable by closing some of its previous opens.
999 *
1000 * XXX: This enforcement is actually incomplete, since we don't keep
1001 * track of access/deny bit combinations; so, e.g., we allow:
1002 *
1003 * OPEN allow read, deny write
1004 * OPEN allow both, deny none
1005 * DOWNGRADE allow read, deny none
1006 *
1007 * which we should reject.
1008 */
Jeff Layton5ae037e2012-05-11 09:45:11 -04001009static unsigned int
1010bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001011 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -04001012 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001013
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001014 for (i = 1; i < 4; i++) {
1015 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -04001016 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001017 }
Jeff Layton5ae037e2012-05-11 09:45:11 -04001018 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001019}
1020
Jeff Layton82c5ff12012-05-11 09:45:13 -04001021/* set share access for a given stateid */
1022static inline void
1023set_access(u32 access, struct nfs4_ol_stateid *stp)
1024{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001025 unsigned char mask = 1 << access;
1026
1027 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1028 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001029}
1030
1031/* clear share access for a given stateid */
1032static inline void
1033clear_access(u32 access, struct nfs4_ol_stateid *stp)
1034{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001035 unsigned char mask = 1 << access;
1036
1037 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1038 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001039}
1040
1041/* test whether a given stateid has access */
1042static inline bool
1043test_access(u32 access, struct nfs4_ol_stateid *stp)
1044{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001045 unsigned char mask = 1 << access;
1046
1047 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001048}
1049
Jeff Laytonce0fc432012-05-11 09:45:14 -04001050/* set share deny for a given stateid */
1051static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001052set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001053{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001054 unsigned char mask = 1 << deny;
1055
1056 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1057 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001058}
1059
1060/* clear share deny for a given stateid */
1061static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001062clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001063{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001064 unsigned char mask = 1 << deny;
1065
1066 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1067 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001068}
1069
1070/* test whether a given stateid is denying specific access */
1071static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -04001072test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001073{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001074 unsigned char mask = 1 << deny;
1075
1076 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001077}
1078
1079static int nfs4_access_to_omode(u32 access)
1080{
J. Bruce Fields8f34a432010-09-02 15:23:16 -04001081 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001082 case NFS4_SHARE_ACCESS_READ:
1083 return O_RDONLY;
1084 case NFS4_SHARE_ACCESS_WRITE:
1085 return O_WRONLY;
1086 case NFS4_SHARE_ACCESS_BOTH:
1087 return O_RDWR;
1088 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001089 WARN_ON_ONCE(1);
1090 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001091}
1092
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001093/*
1094 * A stateid that had a deny mode associated with it is being released
1095 * or downgraded. Recalculate the deny mode on the file.
1096 */
1097static void
1098recalculate_deny_mode(struct nfs4_file *fp)
1099{
1100 struct nfs4_ol_stateid *stp;
1101
1102 spin_lock(&fp->fi_lock);
1103 fp->fi_share_deny = 0;
1104 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1105 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1106 spin_unlock(&fp->fi_lock);
1107}
1108
1109static void
1110reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1111{
1112 int i;
1113 bool change = false;
1114
1115 for (i = 1; i < 4; i++) {
1116 if ((i & deny) != i) {
1117 change = true;
1118 clear_deny(i, stp);
1119 }
1120 }
1121
1122 /* Recalculate per-file deny mode if there was a change */
1123 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -04001124 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001125}
1126
Jeff Layton82c5ff12012-05-11 09:45:13 -04001127/* release all access and file references for a given stateid */
1128static void
1129release_all_access(struct nfs4_ol_stateid *stp)
1130{
1131 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -04001132 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001133
1134 if (fp && stp->st_deny_bmap != 0)
1135 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001136
1137 for (i = 1; i < 4; i++) {
1138 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -04001139 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001140 clear_access(i, stp);
1141 }
1142}
1143
Kinglong Meed50ffde2015-07-16 12:05:07 +08001144static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1145{
1146 kfree(sop->so_owner.data);
1147 sop->so_ops->so_free(sop);
1148}
1149
Jeff Layton6b180f02014-07-29 21:34:26 -04001150static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1151{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001152 struct nfs4_client *clp = sop->so_client;
1153
1154 might_lock(&clp->cl_lock);
1155
1156 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001157 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001158 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001159 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001160 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001161}
1162
Jeff Laytone8568732015-08-24 12:41:47 -04001163static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001164{
Trond Myklebust11b91642014-07-29 21:34:08 -04001165 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001166
Jeff Layton1c755dc2014-07-29 21:34:12 -04001167 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1168
Jeff Laytone8568732015-08-24 12:41:47 -04001169 if (list_empty(&stp->st_perfile))
1170 return false;
1171
Trond Myklebust1d31a252014-07-10 14:07:25 -04001172 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001173 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001174 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001175 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001176 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001177}
1178
Trond Myklebust60116952014-07-29 21:34:06 -04001179static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001180{
Trond Myklebust60116952014-07-29 21:34:06 -04001181 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001182
Sachin Bhamare8287f002015-04-27 14:50:14 +02001183 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001184 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001185 if (stp->st_stateowner)
1186 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -04001187 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001188}
1189
Jeff Laytonb49e0842014-07-29 21:34:11 -04001190static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001191{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001192 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1193 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001194 struct file *file;
1195
Jeff Laytonb49e0842014-07-29 21:34:11 -04001196 file = find_any_file(stp->st_stid.sc_file);
1197 if (file)
1198 filp_close(file, (fl_owner_t)lo);
1199 nfs4_free_ol_stateid(stid);
1200}
1201
Jeff Layton2c41beb2014-07-29 21:34:41 -04001202/*
1203 * Put the persistent reference to an already unhashed generic stateid, while
1204 * holding the cl_lock. If it's the last reference, then put it onto the
1205 * reaplist for later destruction.
1206 */
1207static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1208 struct list_head *reaplist)
1209{
1210 struct nfs4_stid *s = &stp->st_stid;
1211 struct nfs4_client *clp = s->sc_client;
1212
1213 lockdep_assert_held(&clp->cl_lock);
1214
1215 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1216
1217 if (!atomic_dec_and_test(&s->sc_count)) {
1218 wake_up_all(&close_wq);
1219 return;
1220 }
1221
1222 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1223 list_add(&stp->st_locks, reaplist);
1224}
1225
Jeff Laytone8568732015-08-24 12:41:47 -04001226static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001227{
Chuck Leverf46c4452016-10-29 18:19:03 -04001228 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001229
1230 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001231 nfs4_unhash_stid(&stp->st_stid);
Jeff Laytone8568732015-08-24 12:41:47 -04001232 return unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001233}
1234
Jeff Layton5adfd882014-07-29 21:34:31 -04001235static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001236{
Chuck Leverf46c4452016-10-29 18:19:03 -04001237 struct nfs4_client *clp = stp->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04001238 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001239
Chuck Leverf46c4452016-10-29 18:19:03 -04001240 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001241 unhashed = unhash_lock_stateid(stp);
Chuck Leverf46c4452016-10-29 18:19:03 -04001242 spin_unlock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001243 if (unhashed)
1244 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001245}
1246
Trond Myklebustc58c6612014-07-29 21:34:35 -04001247static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001248{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001249 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001250
Trond Myklebustd4f04892014-07-29 21:34:36 -04001251 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001252
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001253 list_del_init(&lo->lo_owner.so_strhash);
1254}
1255
Jeff Layton2c41beb2014-07-29 21:34:41 -04001256/*
1257 * Free a list of generic stateids that were collected earlier after being
1258 * fully unhashed.
1259 */
1260static void
1261free_ol_stateid_reaplist(struct list_head *reaplist)
1262{
1263 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001264 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001265
1266 might_sleep();
1267
1268 while (!list_empty(reaplist)) {
1269 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1270 st_locks);
1271 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001272 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001273 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001274 if (fp)
1275 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001276 }
1277}
1278
Jeff Laytond83017f2014-07-29 21:34:42 -04001279static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1280 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001281{
1282 struct nfs4_ol_stateid *stp;
1283
Jeff Laytone8568732015-08-24 12:41:47 -04001284 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1285
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001286 while (!list_empty(&open_stp->st_locks)) {
1287 stp = list_entry(open_stp->st_locks.next,
1288 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001289 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001290 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001291 }
1292}
1293
Jeff Laytone8568732015-08-24 12:41:47 -04001294static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001295 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001296{
Jeff Laytone8568732015-08-24 12:41:47 -04001297 bool unhashed;
1298
Jeff Layton2c41beb2014-07-29 21:34:41 -04001299 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1300
Jeff Laytone8568732015-08-24 12:41:47 -04001301 unhashed = unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001302 release_open_stateid_locks(stp, reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04001303 return unhashed;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001304}
1305
1306static void release_open_stateid(struct nfs4_ol_stateid *stp)
1307{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001308 LIST_HEAD(reaplist);
1309
1310 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001311 if (unhash_open_stateid(stp, &reaplist))
1312 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001313 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1314 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001315}
1316
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001317static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001318{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001319 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001320
Trond Myklebustd4f04892014-07-29 21:34:36 -04001321 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001322
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001323 list_del_init(&oo->oo_owner.so_strhash);
1324 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001325}
1326
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001327static void release_last_closed_stateid(struct nfs4_openowner *oo)
1328{
Jeff Layton217526e2014-07-30 08:27:11 -04001329 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1330 nfsd_net_id);
1331 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001332
Jeff Layton217526e2014-07-30 08:27:11 -04001333 spin_lock(&nn->client_lock);
1334 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001335 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001336 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001337 oo->oo_last_closed_stid = NULL;
1338 }
Jeff Layton217526e2014-07-30 08:27:11 -04001339 spin_unlock(&nn->client_lock);
1340 if (s)
1341 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001342}
1343
Jeff Layton2c41beb2014-07-29 21:34:41 -04001344static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001345{
1346 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001347 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001348 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001349
Jeff Layton2c41beb2014-07-29 21:34:41 -04001350 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001351
Trond Myklebustd4f04892014-07-29 21:34:36 -04001352 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001353 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001354 while (!list_empty(&oo->oo_owner.so_stateids)) {
1355 stp = list_first_entry(&oo->oo_owner.so_stateids,
1356 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001357 if (unhash_open_stateid(stp, &reaplist))
1358 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001359 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001360 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001361 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001362 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001363 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001364}
1365
Marc Eshel5282fd72009-04-03 08:27:52 +03001366static inline int
1367hash_sessionid(struct nfs4_sessionid *sessionid)
1368{
1369 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1370
1371 return sid->sequence % SESSION_HASH_SIZE;
1372}
1373
Mark Salter135dd002015-04-06 09:46:00 -04001374#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001375static inline void
1376dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1377{
1378 u32 *ptr = (u32 *)(&sessionid->data[0]);
1379 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1380}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001381#else
1382static inline void
1383dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1384{
1385}
1386#endif
1387
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001388/*
1389 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1390 * won't be used for replay.
1391 */
1392void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1393{
1394 struct nfs4_stateowner *so = cstate->replay_owner;
1395
1396 if (nfserr == nfserr_replay_me)
1397 return;
1398
1399 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001400 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001401 return;
1402 }
1403 if (!so)
1404 return;
1405 if (so->so_is_open_owner)
1406 release_last_closed_stateid(openowner(so));
1407 so->so_seqid++;
1408 return;
1409}
Marc Eshel5282fd72009-04-03 08:27:52 +03001410
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001411static void
1412gen_sessionid(struct nfsd4_session *ses)
1413{
1414 struct nfs4_client *clp = ses->se_client;
1415 struct nfsd4_sessionid *sid;
1416
1417 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1418 sid->clientid = clp->cl_clientid;
1419 sid->sequence = current_sessionid++;
1420 sid->reserved = 0;
1421}
1422
1423/*
Andy Adamsona649637c72009-08-28 08:45:01 -04001424 * The protocol defines ca_maxresponssize_cached to include the size of
1425 * the rpc header, but all we need to cache is the data starting after
1426 * the end of the initial SEQUENCE operation--the rest we regenerate
1427 * each time. Therefore we can advertise a ca_maxresponssize_cached
1428 * value that is the number of bytes in our cache plus a few additional
1429 * bytes. In order to stay on the safe side, and not promise more than
1430 * we can cache, those additional bytes must be the minimum possible: 24
1431 * bytes of rpc header (xid through accept state, with AUTH_NULL
1432 * verifier), 12 for the compound header (with zero-length tag), and 44
1433 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001434 */
Andy Adamsona649637c72009-08-28 08:45:01 -04001435#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1436
Andy Adamson557ce262009-08-28 08:45:04 -04001437static void
1438free_session_slots(struct nfsd4_session *ses)
1439{
1440 int i;
1441
1442 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1443 kfree(ses->se_slots[i]);
1444}
1445
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001446/*
1447 * We don't actually need to cache the rpc and session headers, so we
1448 * can allocate a little less for each slot:
1449 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001450static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001451{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001452 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001453
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001454 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1455 size = 0;
1456 else
1457 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1458 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001459}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001460
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001461/*
1462 * XXX: If we run out of reserved DRC memory we could (up to a point)
1463 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001464 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001465 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001466static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001467{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001468 u32 slotsize = slot_bytes(ca);
1469 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001470 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001471
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001472 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001473 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1474 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001475 num = min_t(int, num, avail / slotsize);
1476 nfsd_drc_mem_used += num * slotsize;
1477 spin_unlock(&nfsd_drc_lock);
1478
1479 return num;
1480}
1481
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001482static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001483{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001484 int slotsize = slot_bytes(ca);
1485
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001486 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001487 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001488 spin_unlock(&nfsd_drc_lock);
1489}
1490
Kinglong Mee60810e52014-01-01 00:35:47 +08001491static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1492 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001493{
Kinglong Mee60810e52014-01-01 00:35:47 +08001494 int numslots = fattrs->maxreqs;
1495 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001496 struct nfsd4_session *new;
1497 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001498
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001499 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001500 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1501 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001502
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001503 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001504 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001505 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001506 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001507 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001508 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001509 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001510 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001511 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001512
1513 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1514 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1515
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001516 return new;
1517out_free:
1518 while (i--)
1519 kfree(new->se_slots[i]);
1520 kfree(new);
1521 return NULL;
1522}
1523
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001524static void free_conn(struct nfsd4_conn *c)
1525{
1526 svc_xprt_put(c->cn_xprt);
1527 kfree(c);
1528}
1529
1530static void nfsd4_conn_lost(struct svc_xpt_user *u)
1531{
1532 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1533 struct nfs4_client *clp = c->cn_session->se_client;
1534
1535 spin_lock(&clp->cl_lock);
1536 if (!list_empty(&c->cn_persession)) {
1537 list_del(&c->cn_persession);
1538 free_conn(c);
1539 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001540 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001541 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001542}
1543
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001544static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001545{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001546 struct nfsd4_conn *conn;
1547
1548 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1549 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001550 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001551 svc_xprt_get(rqstp->rq_xprt);
1552 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001553 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001554 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1555 return conn;
1556}
1557
J. Bruce Fields328ead22010-09-29 16:11:06 -04001558static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1559{
1560 conn->cn_session = ses;
1561 list_add(&conn->cn_persession, &ses->se_conns);
1562}
1563
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001564static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1565{
1566 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001567
1568 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001569 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001570 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001571}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001572
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001573static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001574{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001575 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001576 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001577}
1578
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001579static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001580{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001581 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001582
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001583 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001584 ret = nfsd4_register_conn(conn);
1585 if (ret)
1586 /* oops; xprt is already down: */
1587 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001588 /* We may have gained or lost a callback channel: */
1589 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001590}
1591
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001592static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001593{
1594 u32 dir = NFS4_CDFC4_FORE;
1595
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001596 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001597 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001598 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001599}
1600
1601/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001602static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001603{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001604 struct nfs4_client *clp = s->se_client;
1605 struct nfsd4_conn *c;
1606
1607 spin_lock(&clp->cl_lock);
1608 while (!list_empty(&s->se_conns)) {
1609 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1610 list_del_init(&c->cn_persession);
1611 spin_unlock(&clp->cl_lock);
1612
1613 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1614 free_conn(c);
1615
1616 spin_lock(&clp->cl_lock);
1617 }
1618 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001619}
1620
J. Bruce Fields1377b692012-09-11 21:42:40 -04001621static void __free_session(struct nfsd4_session *ses)
1622{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001623 free_session_slots(ses);
1624 kfree(ses);
1625}
1626
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001627static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001628{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001629 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001630 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001631 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001632}
Andy Adamson557ce262009-08-28 08:45:04 -04001633
Fengguang Wu135ae822012-11-10 07:20:25 -05001634static 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 -04001635{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001636 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001637 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001638
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001639 new->se_client = clp;
1640 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001641
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001642 INIT_LIST_HEAD(&new->se_conns);
1643
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001644 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001645 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001646 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001647 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001648 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001649 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001650 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001651 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001652 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001653 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001654
Chuck Leverb0d2e422014-08-22 15:10:59 -04001655 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001656 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001657 /*
1658 * This is a little silly; with sessions there's no real
1659 * use for the callback address. Use the peer address
1660 * as a reasonable default for now, but consider fixing
1661 * the rpc client not to require an address in the
1662 * future:
1663 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001664 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1665 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001666 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001667}
1668
Benny Halevy9089f1b2010-05-12 00:12:26 +03001669/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001670static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001671__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001672{
1673 struct nfsd4_session *elem;
1674 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001675 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001676
Trond Myklebust0a880a22014-07-30 08:27:10 -04001677 lockdep_assert_held(&nn->client_lock);
1678
Marc Eshel5282fd72009-04-03 08:27:52 +03001679 dump_sessionid(__func__, sessionid);
1680 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001681 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001682 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001683 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1684 NFS4_MAX_SESSIONID_LEN)) {
1685 return elem;
1686 }
1687 }
1688
1689 dprintk("%s: session not found\n", __func__);
1690 return NULL;
1691}
1692
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001693static struct nfsd4_session *
1694find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1695 __be32 *ret)
1696{
1697 struct nfsd4_session *session;
1698 __be32 status = nfserr_badsession;
1699
1700 session = __find_in_sessionid_hashtbl(sessionid, net);
1701 if (!session)
1702 goto out;
1703 status = nfsd4_get_session_locked(session);
1704 if (status)
1705 session = NULL;
1706out:
1707 *ret = status;
1708 return session;
1709}
1710
Benny Halevy9089f1b2010-05-12 00:12:26 +03001711/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001712static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001713unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001714{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001715 struct nfs4_client *clp = ses->se_client;
1716 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1717
1718 lockdep_assert_held(&nn->client_lock);
1719
Andy Adamson7116ed62009-04-03 08:27:43 +03001720 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001721 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001722 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001723 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001724}
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1727static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001728STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001730 /*
1731 * We're assuming the clid was not given out from a boot
1732 * precisely 2^32 (about 136 years) before this one. That seems
1733 * a safe assumption:
1734 */
1735 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001737 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001738 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return 1;
1740}
1741
1742/*
1743 * XXX Should we use a slab cache ?
1744 * This type of memory management is somewhat inefficient, but we use it
1745 * anyway since SETCLIENTID is not a common operation.
1746 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001747static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
1749 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001750 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001752 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1753 if (clp == NULL)
1754 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001755 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001756 if (clp->cl_name.data == NULL)
1757 goto err_no_name;
1758 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1759 OWNER_HASH_SIZE, GFP_KERNEL);
1760 if (!clp->cl_ownerstr_hashtbl)
1761 goto err_no_hashtbl;
1762 for (i = 0; i < OWNER_HASH_SIZE; i++)
1763 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001764 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001765 INIT_LIST_HEAD(&clp->cl_sessions);
1766 idr_init(&clp->cl_stateids);
1767 atomic_set(&clp->cl_refcount, 0);
1768 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1769 INIT_LIST_HEAD(&clp->cl_idhash);
1770 INIT_LIST_HEAD(&clp->cl_openowners);
1771 INIT_LIST_HEAD(&clp->cl_delegations);
1772 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04001773 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001774#ifdef CONFIG_NFSD_PNFS
1775 INIT_LIST_HEAD(&clp->cl_lo_states);
1776#endif
Trond Myklebust5694c932014-04-18 14:43:56 -04001777 spin_lock_init(&clp->cl_lock);
1778 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001780err_no_hashtbl:
1781 kfree(clp->cl_name.data);
1782err_no_name:
1783 kfree(clp);
1784 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001787static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788free_client(struct nfs4_client *clp)
1789{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001790 while (!list_empty(&clp->cl_sessions)) {
1791 struct nfsd4_session *ses;
1792 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1793 se_perclnt);
1794 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001795 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1796 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001797 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001798 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001799 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001800 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001802 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 kfree(clp);
1804}
1805
Benny Halevy84d38ac2010-05-12 00:13:16 +03001806/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001807static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001808unhash_client_locked(struct nfs4_client *clp)
1809{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001810 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001811 struct nfsd4_session *ses;
1812
Trond Myklebust0a880a22014-07-30 08:27:10 -04001813 lockdep_assert_held(&nn->client_lock);
1814
Trond Myklebust4beb3452014-07-30 08:27:02 -04001815 /* Mark the client as expired! */
1816 clp->cl_time = 0;
1817 /* Make it invisible */
1818 if (!list_empty(&clp->cl_idhash)) {
1819 list_del_init(&clp->cl_idhash);
1820 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1821 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1822 else
1823 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1824 }
1825 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001826 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001827 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1828 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001829 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001830}
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001833unhash_client(struct nfs4_client *clp)
1834{
1835 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1836
1837 spin_lock(&nn->client_lock);
1838 unhash_client_locked(clp);
1839 spin_unlock(&nn->client_lock);
1840}
1841
Jeff Layton97403d92014-07-30 08:27:12 -04001842static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1843{
1844 if (atomic_read(&clp->cl_refcount))
1845 return nfserr_jukebox;
1846 unhash_client_locked(clp);
1847 return nfs_ok;
1848}
1849
Trond Myklebust4beb3452014-07-30 08:27:02 -04001850static void
1851__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001853 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 struct list_head reaplist;
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04001858 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001859 while (!list_empty(&clp->cl_delegations)) {
1860 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001861 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04001862 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 }
Benny Halevycdc975052014-05-30 09:09:30 -04001864 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 while (!list_empty(&reaplist)) {
1866 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001867 list_del_init(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +02001868 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -04001869 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04001870 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001872 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05001873 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001874 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001875 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001876 }
NeilBrownea1da632005-06-23 22:04:17 -07001877 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001878 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04001879 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001880 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001882 nfsd4_return_all_client_layouts(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001883 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001884 if (clp->cl_cb_conn.cb_xprt)
1885 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001886 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887}
1888
Trond Myklebust4beb3452014-07-30 08:27:02 -04001889static void
1890destroy_client(struct nfs4_client *clp)
1891{
1892 unhash_client(clp);
1893 __destroy_client(clp);
1894}
1895
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001896static void expire_client(struct nfs4_client *clp)
1897{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001898 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001899 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001900 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001901}
1902
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001903static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1904{
1905 memcpy(target->cl_verifier.data, source->data,
1906 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907}
1908
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001909static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1910{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1912 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1913}
1914
J. Bruce Fields50043852015-11-20 15:58:37 -05001915static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1916{
NeilBrown2f10fdc2017-03-23 16:57:36 +08001917 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
1918 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
1919 GFP_KERNEL);
1920 if ((source->cr_principal && ! target->cr_principal) ||
1921 (source->cr_raw_principal && ! target->cr_raw_principal))
1922 return -ENOMEM;
J. Bruce Fields50043852015-11-20 15:58:37 -05001923
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001924 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 target->cr_uid = source->cr_uid;
1926 target->cr_gid = source->cr_gid;
1927 target->cr_group_info = source->cr_group_info;
1928 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001929 target->cr_gss_mech = source->cr_gss_mech;
1930 if (source->cr_gss_mech)
1931 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001932 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
1934
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001935static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001936compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1937{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001938 if (o1->len < o2->len)
1939 return -1;
1940 if (o1->len > o2->len)
1941 return 1;
1942 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001943}
1944
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001945static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001946{
NeilBrowna55370a2005-06-23 22:03:52 -07001947 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
1949
1950static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001951same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1952{
1953 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955
1956static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001957same_clid(clientid_t *cl1, clientid_t *cl2)
1958{
1959 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
1961
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001962static bool groups_equal(struct group_info *g1, struct group_info *g2)
1963{
1964 int i;
1965
1966 if (g1->ngroups != g2->ngroups)
1967 return false;
1968 for (i=0; i<g1->ngroups; i++)
Alexey Dobriyan81243ea2016-10-07 17:03:12 -07001969 if (!gid_eq(g1->gid[i], g2->gid[i]))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001970 return false;
1971 return true;
1972}
1973
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001974/*
1975 * RFC 3530 language requires clid_inuse be returned when the
1976 * "principal" associated with a requests differs from that previously
1977 * used. We use uid, gid's, and gss principal string as our best
1978 * approximation. We also don't want to allow non-gss use of a client
1979 * established using gss: in theory cr_principal should catch that
1980 * change, but in practice cr_principal can be null even in the gss case
1981 * since gssd doesn't always pass down a principal string.
1982 */
1983static bool is_gss_cred(struct svc_cred *cr)
1984{
1985 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1986 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1987}
1988
1989
Vivek Trivedi5559b502012-07-24 21:18:20 +05301990static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001991same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1992{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001993 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001994 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1995 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001996 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1997 return false;
1998 if (cr1->cr_principal == cr2->cr_principal)
1999 return true;
2000 if (!cr1->cr_principal || !cr2->cr_principal)
2001 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05302002 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
2004
J. Bruce Fields57266a62013-04-13 14:27:29 -04002005static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2006{
2007 struct svc_cred *cr = &rqstp->rq_cred;
2008 u32 service;
2009
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04002010 if (!cr->cr_gss_mech)
2011 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002012 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2013 return service == RPC_GSS_SVC_INTEGRITY ||
2014 service == RPC_GSS_SVC_PRIVACY;
2015}
2016
Andrew Elblededeb132016-06-15 12:52:08 -04002017bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
J. Bruce Fields57266a62013-04-13 14:27:29 -04002018{
2019 struct svc_cred *cr = &rqstp->rq_cred;
2020
2021 if (!cl->cl_mach_cred)
2022 return true;
2023 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2024 return false;
2025 if (!svc_rqst_integrity_protected(rqstp))
2026 return false;
J. Bruce Fields414ca012015-11-20 10:48:02 -05002027 if (cl->cl_cred.cr_raw_principal)
2028 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2029 cr->cr_raw_principal);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002030 if (!cr->cr_principal)
2031 return false;
2032 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2033}
2034
Jeff Layton294ac322014-07-30 08:27:15 -04002035static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05002036{
Chuck Leverab4684d2012-03-02 17:13:50 -05002037 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Jeff Laytonf4199922014-06-17 07:44:11 -04002039 /*
2040 * This is opaque to client, so no need to byte-swap. Use
2041 * __force to keep sparse happy
2042 */
2043 verf[0] = (__force __be32)get_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08002044 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05002045 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046}
2047
Jeff Layton294ac322014-07-30 08:27:15 -04002048static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2049{
2050 clp->cl_clientid.cl_boot = nn->boot_time;
2051 clp->cl_clientid.cl_id = nn->clientid_counter++;
2052 gen_confirm(clp, nn);
2053}
2054
Jeff Layton4770d722014-07-29 21:34:10 -04002055static struct nfs4_stid *
2056find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04002057{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002058 struct nfs4_stid *ret;
2059
2060 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2061 if (!ret || !ret->sc_type)
2062 return NULL;
2063 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04002064}
2065
Jeff Layton4770d722014-07-29 21:34:10 -04002066static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04002067find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002068{
2069 struct nfs4_stid *s;
2070
Jeff Layton4770d722014-07-29 21:34:10 -04002071 spin_lock(&cl->cl_lock);
2072 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002073 if (s != NULL) {
2074 if (typemask & s->sc_type)
2075 atomic_inc(&s->sc_count);
2076 else
2077 s = NULL;
2078 }
Jeff Layton4770d722014-07-29 21:34:10 -04002079 spin_unlock(&cl->cl_lock);
2080 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04002081}
2082
Jeff Layton2216d442012-11-12 15:00:57 -05002083static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002084 struct svc_rqst *rqstp, nfs4_verifier *verf)
2085{
2086 struct nfs4_client *clp;
2087 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002088 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002089 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002090
2091 clp = alloc_client(name);
2092 if (clp == NULL)
2093 return NULL;
2094
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002095 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2096 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002097 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002098 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002099 }
Christoph Hellwig0162ac22014-09-24 12:19:19 +02002100 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Benny Halevy07cd4902010-05-12 00:13:41 +03002101 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002102 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002103 copy_verf(clp, verf);
2104 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04002105 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002106 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002107 return clp;
2108}
2109
NeilBrownfd39ca92005-06-23 22:04:03 -07002110static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002111add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2112{
2113 struct rb_node **new = &(root->rb_node), *parent = NULL;
2114 struct nfs4_client *clp;
2115
2116 while (*new) {
2117 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2118 parent = *new;
2119
2120 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2121 new = &((*new)->rb_left);
2122 else
2123 new = &((*new)->rb_right);
2124 }
2125
2126 rb_link_node(&new_clp->cl_namenode, parent, new);
2127 rb_insert_color(&new_clp->cl_namenode, root);
2128}
2129
2130static struct nfs4_client *
2131find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2132{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002133 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002134 struct rb_node *node = root->rb_node;
2135 struct nfs4_client *clp;
2136
2137 while (node) {
2138 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2139 cmp = compare_blob(&clp->cl_name, name);
2140 if (cmp > 0)
2141 node = node->rb_left;
2142 else if (cmp < 0)
2143 node = node->rb_right;
2144 else
2145 return clp;
2146 }
2147 return NULL;
2148}
2149
2150static void
2151add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
2153 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002154 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Trond Myklebust0a880a22014-07-30 08:27:10 -04002156 lockdep_assert_held(&nn->client_lock);
2157
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002158 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002159 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002161 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002162 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163}
2164
NeilBrownfd39ca92005-06-23 22:04:03 -07002165static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166move_to_confirmed(struct nfs4_client *clp)
2167{
2168 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002169 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Trond Myklebust0a880a22014-07-30 08:27:10 -04002171 lockdep_assert_held(&nn->client_lock);
2172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002174 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002175 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002176 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002177 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002178 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180
2181static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002182find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
2184 struct nfs4_client *clp;
2185 unsigned int idhashval = clientid_hashval(clid->cl_id);
2186
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002187 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002188 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002189 if ((bool)clp->cl_minorversion != sessions)
2190 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002191 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 }
2195 return NULL;
2196}
2197
2198static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002199find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2200{
2201 struct list_head *tbl = nn->conf_id_hashtbl;
2202
Trond Myklebust0a880a22014-07-30 08:27:10 -04002203 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002204 return find_client_in_id_table(tbl, clid, sessions);
2205}
2206
2207static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002208find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002210 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Trond Myklebust0a880a22014-07-30 08:27:10 -04002212 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002213 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214}
2215
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002216static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002217{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002218 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002219}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002220
NeilBrown28ce6052005-06-23 22:03:56 -07002221static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002222find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002223{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002224 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002225 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002226}
2227
2228static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002229find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002230{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002231 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002232 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002233}
2234
NeilBrownfd39ca92005-06-23 22:04:03 -07002235static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002236gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002238 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002239 struct sockaddr *sa = svc_addr(rqstp);
2240 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002241 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Jeff Layton7077ecb2009-08-14 12:57:58 -04002243 /* Currently, we only support tcp and tcp6 for the callback channel */
2244 if (se->se_callback_netid_len == 3 &&
2245 !memcmp(se->se_callback_netid_val, "tcp", 3))
2246 expected_family = AF_INET;
2247 else if (se->se_callback_netid_len == 4 &&
2248 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2249 expected_family = AF_INET6;
2250 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 goto out_err;
2252
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002253 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002254 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002255 (struct sockaddr *)&conn->cb_addr,
2256 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002257
J. Bruce Fields07263f12010-05-31 19:09:40 -04002258 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002260
J. Bruce Fields07263f12010-05-31 19:09:40 -04002261 if (conn->cb_addr.ss_family == AF_INET6)
2262 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002263
J. Bruce Fields07263f12010-05-31 19:09:40 -04002264 conn->cb_prog = se->se_callback_prog;
2265 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002266 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 return;
2268out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002269 conn->cb_addr.ss_family = AF_UNSPEC;
2270 conn->cb_addrlen = 0;
Rasmus Villemoes4ab495b2017-02-24 01:15:55 +01002271 dprintk("NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 "will not receive delegations\n",
2273 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2274
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 return;
2276}
2277
Andy Adamson074fe892009-04-03 08:28:15 +03002278/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002279 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002280 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002281static void
Andy Adamson074fe892009-04-03 08:28:15 +03002282nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2283{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002284 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002285 struct nfsd4_slot *slot = resp->cstate.slot;
2286 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002287
Andy Adamson557ce262009-08-28 08:45:04 -04002288 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002289
Andy Adamson557ce262009-08-28 08:45:04 -04002290 slot->sl_opcnt = resp->opcnt;
2291 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002292
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002293 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002294 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002295 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002296 return;
2297 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002298 base = resp->cstate.data_offset;
2299 slot->sl_datalen = buf->len - base;
2300 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Dan Carpenterd3f03402015-11-21 13:28:50 +03002301 WARN(1, "%s: sessions DRC could not cache compound\n",
2302 __func__);
Andy Adamson557ce262009-08-28 08:45:04 -04002303 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002304}
2305
2306/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002307 * Encode the replay sequence operation from the slot values.
2308 * If cachethis is FALSE encode the uncached rep error on the next
2309 * operation which sets resp->p and increments resp->opcnt for
2310 * nfs4svc_encode_compoundres.
2311 *
Andy Adamson074fe892009-04-03 08:28:15 +03002312 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002313static __be32
2314nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2315 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002316{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002317 struct nfsd4_op *op;
2318 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002319
Andy Adamsonabfabf82009-07-23 19:02:18 -04002320 /* Encode the replayed sequence operation */
2321 op = &args->ops[resp->opcnt - 1];
2322 nfsd4_encode_operation(resp, op);
2323
2324 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002325 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002326 op = &args->ops[resp->opcnt++];
2327 op->status = nfserr_retry_uncached_rep;
2328 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002329 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002330 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002331}
2332
2333/*
Andy Adamson557ce262009-08-28 08:45:04 -04002334 * The sequence operation is not cached because we can use the slot and
2335 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002336 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002337static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002338nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2339 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002340{
Andy Adamson557ce262009-08-28 08:45:04 -04002341 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002342 struct xdr_stream *xdr = &resp->xdr;
2343 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002344 __be32 status;
2345
Andy Adamson557ce262009-08-28 08:45:04 -04002346 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002347
Andy Adamsonabfabf82009-07-23 19:02:18 -04002348 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002349 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002350 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002351
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002352 p = xdr_reserve_space(xdr, slot->sl_datalen);
2353 if (!p) {
2354 WARN_ON_ONCE(1);
2355 return nfserr_serverfault;
2356 }
2357 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2358 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002359
Andy Adamson557ce262009-08-28 08:45:04 -04002360 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002361 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002362}
2363
Andy Adamson0733d212009-04-03 08:28:01 +03002364/*
2365 * Set the exchange_id flags returned by the server.
2366 */
2367static void
2368nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2369{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002370#ifdef CONFIG_NFSD_PNFS
2371 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2372#else
Andy Adamson0733d212009-04-03 08:28:01 +03002373 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002374#endif
Andy Adamson0733d212009-04-03 08:28:01 +03002375
2376 /* Referrals are supported, Migration is not. */
2377 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2378
2379 /* set the wire flags to return to client. */
2380 clid->flags = new->cl_exchange_flags;
2381}
2382
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002383static bool client_has_openowners(struct nfs4_client *clp)
2384{
2385 struct nfs4_openowner *oo;
2386
2387 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2388 if (!list_empty(&oo->oo_owner.so_stateids))
2389 return true;
2390 }
2391 return false;
2392}
2393
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002394static bool client_has_state(struct nfs4_client *clp)
2395{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002396 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08002397#ifdef CONFIG_NFSD_PNFS
2398 || !list_empty(&clp->cl_lo_states)
2399#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002400 || !list_empty(&clp->cl_delegations)
2401 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002402}
2403
Al Virob37ad282006-10-19 23:28:59 -07002404__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02002405nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2406 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03002407{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002408 struct nfsd4_exchange_id *exid = &u->exchange_id;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002409 struct nfs4_client *conf, *new;
2410 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002411 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002412 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002413 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002414 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002415 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002416 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002417
Jeff Layton363168b2009-08-14 12:57:56 -04002418 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002419 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002420 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002421 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002422 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002423
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002424 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002425 return nfserr_inval;
2426
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002427 new = create_client(exid->clname, rqstp, &verf);
2428 if (new == NULL)
2429 return nfserr_jukebox;
2430
Andy Adamson0733d212009-04-03 08:28:01 +03002431 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002432 case SP4_MACH_CRED:
Andrew Elbleed941642016-06-15 12:52:09 -04002433 exid->spo_must_enforce[0] = 0;
2434 exid->spo_must_enforce[1] = (
2435 1 << (OP_BIND_CONN_TO_SESSION - 32) |
2436 1 << (OP_EXCHANGE_ID - 32) |
2437 1 << (OP_CREATE_SESSION - 32) |
2438 1 << (OP_DESTROY_SESSION - 32) |
2439 1 << (OP_DESTROY_CLIENTID - 32));
2440
2441 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
2442 1 << (OP_OPEN_DOWNGRADE) |
2443 1 << (OP_LOCKU) |
2444 1 << (OP_DELEGRETURN));
2445
2446 exid->spo_must_allow[1] &= (
2447 1 << (OP_TEST_STATEID - 32) |
2448 1 << (OP_FREE_STATEID - 32));
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002449 if (!svc_rqst_integrity_protected(rqstp)) {
2450 status = nfserr_inval;
2451 goto out_nolock;
2452 }
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002453 /*
2454 * Sometimes userspace doesn't give us a principal.
2455 * Which is a bug, really. Anyway, we can't enforce
2456 * MACH_CRED in that case, better to give up now:
2457 */
J. Bruce Fields414ca012015-11-20 10:48:02 -05002458 if (!new->cl_cred.cr_principal &&
2459 !new->cl_cred.cr_raw_principal) {
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002460 status = nfserr_serverfault;
2461 goto out_nolock;
2462 }
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002463 new->cl_mach_cred = true;
Andy Adamson0733d212009-04-03 08:28:01 +03002464 case SP4_NONE:
2465 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002466 default: /* checked by xdr code */
2467 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002468 case SP4_SSV:
Kinglong Mee8edf4b02016-02-26 22:36:42 +08002469 status = nfserr_encr_alg_unsupp;
2470 goto out_nolock;
Andy Adamson0733d212009-04-03 08:28:01 +03002471 }
2472
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002473 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002474 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002475 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002476 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002477 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2478 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2479
J. Bruce Fields136e6582012-05-12 20:37:23 -04002480 if (update) {
2481 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002482 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002483 goto out;
2484 }
Andrew Elblededeb132016-06-15 12:52:08 -04002485 if (!nfsd4_mach_creds_match(conf, rqstp)) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002486 status = nfserr_wrong_cred;
2487 goto out;
2488 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002489 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002490 status = nfserr_perm;
2491 goto out;
2492 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002493 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002494 status = nfserr_not_same;
2495 goto out;
2496 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002497 /* case 6 */
2498 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002499 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002500 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002501 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002502 if (client_has_state(conf)) {
2503 status = nfserr_clid_inuse;
2504 goto out;
2505 }
Andy Adamson0733d212009-04-03 08:28:01 +03002506 goto out_new;
2507 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002508 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002509 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002510 goto out_copy;
2511 }
2512 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002513 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002514 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002515 }
2516
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002517 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002518 status = nfserr_noent;
2519 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002520 }
2521
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002522 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002523 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002524 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002525
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002526 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002527out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002528 if (conf) {
2529 status = mark_client_expired_locked(conf);
2530 if (status)
2531 goto out;
2532 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002533 new->cl_minorversion = cstate->minorversion;
Andrew Elbleed941642016-06-15 12:52:09 -04002534 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
2535 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
Andy Adamson0733d212009-04-03 08:28:01 +03002536
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002537 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002538 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002539 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002540out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002541 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2542 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002543
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002544 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2545 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002546
2547 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002548 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002549 status = nfs_ok;
2550
2551out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002552 spin_unlock(&nn->client_lock);
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002553out_nolock:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002554 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002555 expire_client(new);
2556 if (unconf)
2557 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002558 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002559}
2560
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002561static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002562check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002563{
Andy Adamson88e588d2009-07-23 19:02:15 -04002564 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2565 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002566
2567 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002568 if (slot_inuse) {
2569 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002570 return nfserr_jukebox;
2571 else
2572 return nfserr_seq_misordered;
2573 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002574 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002575 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002576 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002577 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002578 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002579 return nfserr_seq_misordered;
2580}
2581
Andy Adamson49557cc2009-07-23 19:02:16 -04002582/*
2583 * Cache the create session result into the create session single DRC
2584 * slot cache by saving the xdr structure. sl_seqid has been set.
2585 * Do this for solo or embedded create session operations.
2586 */
2587static void
2588nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002589 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002590{
2591 slot->sl_status = nfserr;
2592 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2593}
2594
2595static __be32
2596nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2597 struct nfsd4_clid_slot *slot)
2598{
2599 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2600 return slot->sl_status;
2601}
2602
Mi Jinlong1b74c252011-07-14 14:50:17 +08002603#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2604 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2605 1 + /* MIN tag is length with zero, only length */ \
2606 3 + /* version, opcount, opcode */ \
2607 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2608 /* seqid, slotID, slotID, cache */ \
2609 4 ) * sizeof(__be32))
2610
2611#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2612 2 + /* verifier: AUTH_NULL, length 0 */\
2613 1 + /* status */ \
2614 1 + /* MIN tag is length with zero, only length */ \
2615 3 + /* opcount, opcode, opstatus*/ \
2616 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2617 /* seqid, slotID, slotID, slotID, status */ \
2618 5 ) * sizeof(__be32))
2619
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002620static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002621{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002622 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2623
J. Bruce Fields373cd402013-04-08 15:42:12 -04002624 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2625 return nfserr_toosmall;
2626 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2627 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002628 ca->headerpadsz = 0;
2629 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2630 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2631 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2632 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2633 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2634 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2635 /*
2636 * Note decreasing slot size below client's request may make it
2637 * difficult for client to function correctly, whereas
2638 * decreasing the number of slots will (just?) affect
2639 * performance. When short on memory we therefore prefer to
2640 * decrease number of slots instead of their size. Clients that
2641 * request larger slots than they need will get poor results:
2642 */
2643 ca->maxreqs = nfsd4_get_drc_mem(ca);
2644 if (!ca->maxreqs)
2645 return nfserr_jukebox;
2646
J. Bruce Fields373cd402013-04-08 15:42:12 -04002647 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002648}
2649
Chuck Lever45006322016-03-01 13:06:02 -05002650/*
2651 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
2652 * These are based on similar macros in linux/sunrpc/msg_prot.h .
2653 */
2654#define RPC_MAX_HEADER_WITH_AUTH_SYS \
2655 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
2656
2657#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
2658 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
2659
Kinglong Mee8a891632013-12-23 18:11:02 +08002660#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05002661 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08002662#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05002663 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
2664 sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08002665
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002666static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2667{
2668 ca->headerpadsz = 0;
2669
Kinglong Mee8a891632013-12-23 18:11:02 +08002670 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002671 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002672 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002673 return nfserr_toosmall;
2674 ca->maxresp_cached = 0;
2675 if (ca->maxops < 2)
2676 return nfserr_toosmall;
2677
2678 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002679}
2680
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002681static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2682{
2683 switch (cbs->flavor) {
2684 case RPC_AUTH_NULL:
2685 case RPC_AUTH_UNIX:
2686 return nfs_ok;
2687 default:
2688 /*
2689 * GSS case: the spec doesn't allow us to return this
2690 * error. But it also doesn't allow us not to support
2691 * GSS.
2692 * I'd rather this fail hard than return some error the
2693 * client might think it can already handle:
2694 */
2695 return nfserr_encr_alg_unsupp;
2696 }
2697}
2698
Andy Adamson069b6ad2009-04-03 08:27:58 +03002699__be32
2700nfsd4_create_session(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02002701 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03002702{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002703 struct nfsd4_create_session *cr_ses = &u->create_session;
Jeff Layton363168b2009-08-14 12:57:56 -04002704 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002705 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002706 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04002707 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002708 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002709 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002710 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002711 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002712
Mi Jinlonga62573d2011-03-23 17:57:07 +08002713 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2714 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002715 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2716 if (status)
2717 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002718 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002719 if (status)
2720 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002721 status = check_backchannel_attrs(&cr_ses->back_channel);
2722 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002723 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002724 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002725 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002726 if (!new)
2727 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002728 conn = alloc_conn_from_crses(rqstp, cr_ses);
2729 if (!conn)
2730 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002731
Jeff Laytond20c11d2014-07-30 08:27:07 -04002732 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002733 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002734 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002735 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002736
2737 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002738 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002739 if (!nfsd4_mach_creds_match(conf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002740 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002741 cs_slot = &conf->cl_cs_slot;
2742 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08002743 if (status) {
2744 if (status == nfserr_replay_cache)
2745 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002746 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002747 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002748 } else if (unconf) {
2749 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002750 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002751 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002752 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002753 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002754 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002755 if (!nfsd4_mach_creds_match(unconf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002756 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002757 cs_slot = &unconf->cl_cs_slot;
2758 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002759 if (status) {
2760 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002761 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002762 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002763 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002764 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002765 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002766 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002767 if (status) {
2768 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002769 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002770 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002771 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002772 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002773 conf = unconf;
2774 } else {
2775 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002776 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002777 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002778 status = nfs_ok;
Chuck Lever4ce85c82016-03-01 13:05:27 -05002779 /* Persistent sessions are not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002780 cr_ses->flags &= ~SESSION4_PERSIST;
Chuck Lever4ce85c82016-03-01 13:05:27 -05002781 /* Upshifting from TCP to RDMA is not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002782 cr_ses->flags &= ~SESSION4_RDMA;
2783
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002784 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002785 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002786
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04002787 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002788 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002789 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002790 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002791
Jeff Laytond20c11d2014-07-30 08:27:07 -04002792 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002793 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002794 spin_unlock(&nn->client_lock);
2795 /* init connection and backchannel */
2796 nfsd4_init_conn(rqstp, conn, new);
2797 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002798 if (old)
2799 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002800 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002801out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002802 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002803 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002804 if (old)
2805 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002806out_free_session:
2807 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002808out_release_drc_mem:
2809 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002810 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002811}
2812
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002813static __be32 nfsd4_map_bcts_dir(u32 *dir)
2814{
2815 switch (*dir) {
2816 case NFS4_CDFC4_FORE:
2817 case NFS4_CDFC4_BACK:
2818 return nfs_ok;
2819 case NFS4_CDFC4_FORE_OR_BOTH:
2820 case NFS4_CDFC4_BACK_OR_BOTH:
2821 *dir = NFS4_CDFC4_BOTH;
2822 return nfs_ok;
2823 };
2824 return nfserr_inval;
2825}
2826
Christoph Hellwigeb698532017-05-08 20:58:35 +02002827__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
2828 struct nfsd4_compound_state *cstate,
2829 union nfsd4_op_u *u)
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002830{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002831 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002832 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002833 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002834 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002835
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002836 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2837 if (status)
2838 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002839 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002840 session->se_cb_prog = bc->bc_cb_program;
2841 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002842 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002843
2844 nfsd4_probe_callback(session->se_client);
2845
2846 return nfs_ok;
2847}
2848
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002849__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2850 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02002851 union nfsd4_op_u *u)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002852{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002853 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002854 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002855 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002856 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002857 struct net *net = SVC_NET(rqstp);
2858 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002859
2860 if (!nfsd4_last_compound_op(rqstp))
2861 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002862 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002863 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002864 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002865 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002866 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002867 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002868 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002869 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002870 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002871 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002872 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002873 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002874 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002875 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002876 goto out;
2877 nfsd4_init_conn(rqstp, conn, session);
2878 status = nfs_ok;
2879out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002880 nfsd4_put_session(session);
2881out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002882 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002883}
2884
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002885static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2886{
2887 if (!session)
2888 return 0;
2889 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2890}
2891
Andy Adamson069b6ad2009-04-03 08:27:58 +03002892__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02002893nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
2894 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03002895{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002896 struct nfsd4_destroy_session *sessionid = &u->destroy_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002897 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002898 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002899 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002900 struct net *net = SVC_NET(r);
2901 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002902
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002903 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002904 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002905 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002906 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002907 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002908 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002909 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002910 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002911 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002912 if (!ses)
2913 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002914 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002915 if (!nfsd4_mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002916 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002917 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002918 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002919 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002920 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002921 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002922
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002923 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002924
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002925 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002926 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002927out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002928 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002929out_client_lock:
2930 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002931out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03002932 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002933}
2934
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002935static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002936{
2937 struct nfsd4_conn *c;
2938
2939 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002940 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002941 return c;
2942 }
2943 }
2944 return NULL;
2945}
2946
J. Bruce Fields57266a62013-04-13 14:27:29 -04002947static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002948{
2949 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002950 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002951 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002952 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002953
2954 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002955 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002956 if (c)
2957 goto out_free;
2958 status = nfserr_conn_not_bound_to_session;
2959 if (clp->cl_mach_cred)
2960 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002961 __nfsd4_hash_conn(new, ses);
2962 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002963 ret = nfsd4_register_conn(new);
2964 if (ret)
2965 /* oops; xprt is already down: */
2966 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002967 return nfs_ok;
2968out_free:
2969 spin_unlock(&clp->cl_lock);
2970 free_conn(new);
2971 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002972}
2973
Mi Jinlong868b89c2011-04-27 09:09:58 +08002974static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2975{
2976 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2977
2978 return args->opcnt > session->se_fchannel.maxops;
2979}
2980
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002981static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2982 struct nfsd4_session *session)
2983{
2984 struct xdr_buf *xb = &rqstp->rq_arg;
2985
2986 return xb->len > session->se_fchannel.maxreq_sz;
2987}
2988
Andy Adamson069b6ad2009-04-03 08:27:58 +03002989__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02002990nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2991 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03002992{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002993 struct nfsd4_sequence *seq = &u->sequence;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002994 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002995 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002996 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002997 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002998 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002999 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003000 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003001 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003002 struct net *net = SVC_NET(rqstp);
3003 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003004
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003005 if (resp->opcnt != 1)
3006 return nfserr_sequence_pos;
3007
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003008 /*
3009 * Will be either used or freed by nfsd4_sequence_check_conn
3010 * below.
3011 */
3012 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3013 if (!conn)
3014 return nfserr_jukebox;
3015
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003016 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003017 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003018 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003019 goto out_no_session;
3020 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003021
Mi Jinlong868b89c2011-04-27 09:09:58 +08003022 status = nfserr_too_many_ops;
3023 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003024 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003025
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003026 status = nfserr_req_too_big;
3027 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003028 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003029
Benny Halevyb85d4c02009-04-03 08:28:08 +03003030 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003031 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003032 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003033
Andy Adamson557ce262009-08-28 08:45:04 -04003034 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003035 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3036
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003037 /* We do not negotiate the number of slots yet, so set the
3038 * maxslots to the session maxreqs which is used to encode
3039 * sr_highest_slotid and the sr_target_slot id to maxslots */
3040 seq->maxslots = session->se_fchannel.maxreqs;
3041
J. Bruce Fields73e79482012-02-13 16:39:00 -05003042 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3043 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003044 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003045 status = nfserr_seq_misordered;
3046 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003047 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003048 cstate->slot = slot;
3049 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003050 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003051 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003052 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003053 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003054 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003055 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003056 }
3057 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003058 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003059
J. Bruce Fields57266a62013-04-13 14:27:29 -04003060 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003061 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003062 if (status)
3063 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003064
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003065 buflen = (seq->cachethis) ?
3066 session->se_fchannel.maxresp_cached :
3067 session->se_fchannel.maxresp_sz;
3068 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3069 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003070 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003071 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003072 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003073
3074 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003075 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003076 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003077 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003078 if (seq->cachethis)
3079 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003080 else
3081 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003082
3083 cstate->slot = slot;
3084 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003085 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003086
Benny Halevyb85d4c02009-04-03 08:28:08 +03003087out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003088 switch (clp->cl_cb_state) {
3089 case NFSD4_CB_DOWN:
3090 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3091 break;
3092 case NFSD4_CB_FAULT:
3093 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3094 break;
3095 default:
3096 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003097 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003098 if (!list_empty(&clp->cl_revoked))
3099 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003100out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08003101 if (conn)
3102 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003103 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003104 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003105out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003106 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003107 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003108}
3109
Trond Myklebustb6076642014-06-30 11:48:35 -04003110void
3111nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3112{
3113 struct nfsd4_compound_state *cs = &resp->cstate;
3114
3115 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04003116 if (cs->status != nfserr_replay_cache) {
3117 nfsd4_store_cache_entry(resp);
3118 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3119 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003120 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04003121 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003122 } else if (cs->clp)
3123 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04003124}
3125
Mi Jinlong345c2842011-10-20 17:51:39 +08003126__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003127nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3128 struct nfsd4_compound_state *cstate,
3129 union nfsd4_op_u *u)
Mi Jinlong345c2842011-10-20 17:51:39 +08003130{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003131 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003132 struct nfs4_client *conf, *unconf;
3133 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003134 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003135 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08003136
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003137 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003138 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003139 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003140 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08003141
3142 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04003143 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08003144 status = nfserr_clientid_busy;
3145 goto out;
3146 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04003147 status = mark_client_expired_locked(conf);
3148 if (status)
3149 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003150 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08003151 } else if (unconf)
3152 clp = unconf;
3153 else {
3154 status = nfserr_stale_clientid;
3155 goto out;
3156 }
Andrew Elblededeb132016-06-15 12:52:08 -04003157 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003158 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003159 status = nfserr_wrong_cred;
3160 goto out;
3161 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003162 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003163out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003164 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003165 if (clp)
3166 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003167 return status;
3168}
3169
Andy Adamson069b6ad2009-04-03 08:27:58 +03003170__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003171nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3172 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003173{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003174 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003175 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003176
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003177 if (rc->rca_one_fs) {
3178 if (!cstate->current_fh.fh_dentry)
3179 return nfserr_nofilehandle;
3180 /*
3181 * We don't take advantage of the rca_one_fs case.
3182 * That's OK, it's optional, we can safely ignore it.
3183 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02003184 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003185 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003186
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003187 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04003188 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3189 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003190 goto out;
3191
3192 status = nfserr_stale_clientid;
3193 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003194 /*
3195 * The following error isn't really legal.
3196 * But we only get here if the client just explicitly
3197 * destroyed the client. Surely it no longer cares what
3198 * error it gets back on an operation for the dead
3199 * client.
3200 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003201 goto out;
3202
3203 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04003204 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003205out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003206 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003207}
3208
3209__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003210nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003211 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003213 struct nfsd4_setclientid *setclid = &u->setclientid;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003214 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003216 struct nfs4_client *conf, *new;
3217 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003218 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003219 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3220
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003221 new = create_client(clname, rqstp, &clverifier);
3222 if (new == NULL)
3223 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003224 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003225 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003226 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04003227 if (conf && client_has_state(conf)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003228 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003230 if (clp_used_exchangeid(conf))
3231 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003232 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04003233 char addr_str[INET6_ADDRSTRLEN];
3234 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3235 sizeof(addr_str));
3236 dprintk("NFSD: setclientid: string in use by client "
3237 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 goto out;
3239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003241 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003242 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003243 unhash_client_locked(unconf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003244 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003245 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003247 gen_confirm(new, nn);
3248 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003249 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003250 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003251 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003252 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3254 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3255 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003256 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 status = nfs_ok;
3258out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003259 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003260 if (new)
3261 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003262 if (unconf)
3263 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 return status;
3265}
3266
3267
Al Virob37ad282006-10-19 23:28:59 -07003268__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003269nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003270 struct nfsd4_compound_state *cstate,
3271 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003273 struct nfsd4_setclientid_confirm *setclientid_confirm =
3274 &u->setclientid_confirm;
NeilBrown21ab45a2005-06-23 22:04:14 -07003275 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003276 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3278 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003279 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003280 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003282 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003284
Jeff Laytond20c11d2014-07-30 08:27:07 -04003285 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003286 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003287 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003288 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003289 * We try hard to give out unique clientid's, so if we get an
3290 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003291 * the client may be buggy; this should never happen.
3292 *
3293 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003294 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003295 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04003296 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3297 goto out;
3298 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3299 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003300 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003301 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003302 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3303 /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 status = nfs_ok;
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003305 } else /* case 4: client hasn't noticed we rebooted yet? */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003306 status = nfserr_stale_clientid;
3307 goto out;
3308 }
3309 status = nfs_ok;
3310 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003311 old = unconf;
3312 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003313 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003314 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003315 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3316 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04003317 status = nfserr_clid_inuse;
3318 if (client_has_state(old)
3319 && !same_creds(&unconf->cl_cred,
3320 &old->cl_cred))
3321 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003322 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003323 if (status) {
3324 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003325 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003326 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003327 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003328 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003329 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003330 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003331 get_client_locked(conf);
3332 spin_unlock(&nn->client_lock);
3333 nfsd4_probe_callback(conf);
3334 spin_lock(&nn->client_lock);
3335 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003337 spin_unlock(&nn->client_lock);
3338 if (old)
3339 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 return status;
3341}
3342
J. Bruce Fields32513b42011-10-13 16:00:16 -04003343static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003345 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3346}
3347
3348/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003349static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3350 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003351{
Trond Myklebust950e0112014-06-30 11:48:31 -04003352 lockdep_assert_held(&state_lock);
3353
J. Bruce Fields32513b42011-10-13 16:00:16 -04003354 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003355 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003356 INIT_LIST_HEAD(&fp->fi_stateids);
3357 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02003358 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003359 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003360 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003361 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003362 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003363 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3364 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003365#ifdef CONFIG_NFSD_PNFS
3366 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02003367 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003368#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04003369 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370}
3371
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003372void
NeilBrowne60d4392005-06-23 22:03:01 -07003373nfsd4_free_slabs(void)
3374{
Sachin Bhamare8287f002015-04-27 14:50:14 +02003375 kmem_cache_destroy(odstate_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003376 kmem_cache_destroy(openowner_slab);
3377 kmem_cache_destroy(lockowner_slab);
3378 kmem_cache_destroy(file_slab);
3379 kmem_cache_destroy(stateid_slab);
3380 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003381}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
Bryan Schumaker72083392011-11-01 15:24:59 -04003383int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384nfsd4_init_slabs(void)
3385{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003386 openowner_slab = kmem_cache_create("nfsd4_openowners",
3387 sizeof(struct nfs4_openowner), 0, 0, NULL);
3388 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003389 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003390 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003391 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003392 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003393 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003394 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003395 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003396 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003397 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003398 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003399 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003400 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003401 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003402 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003403 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003404 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003405 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003406 odstate_slab = kmem_cache_create("nfsd4_odstate",
3407 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3408 if (odstate_slab == NULL)
3409 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003411
Sachin Bhamare8287f002015-04-27 14:50:14 +02003412out_free_deleg_slab:
3413 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003414out_free_stateid_slab:
3415 kmem_cache_destroy(stateid_slab);
3416out_free_file_slab:
3417 kmem_cache_destroy(file_slab);
3418out_free_lockowner_slab:
3419 kmem_cache_destroy(lockowner_slab);
3420out_free_openowner_slab:
3421 kmem_cache_destroy(openowner_slab);
3422out:
NeilBrowne60d4392005-06-23 22:03:01 -07003423 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3424 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425}
3426
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003427static void init_nfs4_replay(struct nfs4_replay *rp)
3428{
3429 rp->rp_status = nfserr_serverfault;
3430 rp->rp_buflen = 0;
3431 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003432 mutex_init(&rp->rp_mutex);
3433}
3434
3435static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3436 struct nfs4_stateowner *so)
3437{
3438 if (!nfsd4_has_session(cstate)) {
3439 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003440 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04003441 }
3442}
3443
3444void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3445{
3446 struct nfs4_stateowner *so = cstate->replay_owner;
3447
3448 if (so != NULL) {
3449 cstate->replay_owner = NULL;
3450 mutex_unlock(&so->so_replay.rp_mutex);
3451 nfs4_put_stateowner(so);
3452 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003453}
3454
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003455static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456{
3457 struct nfs4_stateowner *sop;
3458
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003459 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003460 if (!sop)
3461 return NULL;
3462
3463 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3464 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003465 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003466 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003468 sop->so_owner.len = owner->len;
3469
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003470 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003471 sop->so_client = clp;
3472 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003473 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003474 return sop;
3475}
3476
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003477static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003478{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003479 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003480
Trond Myklebustd4f04892014-07-29 21:34:36 -04003481 list_add(&oo->oo_owner.so_strhash,
3482 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003483 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484}
3485
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003486static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3487{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003488 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003489}
3490
Jeff Layton6b180f02014-07-29 21:34:26 -04003491static void nfs4_free_openowner(struct nfs4_stateowner *so)
3492{
3493 struct nfs4_openowner *oo = openowner(so);
3494
3495 kmem_cache_free(openowner_slab, oo);
3496}
3497
3498static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003499 .so_unhash = nfs4_unhash_openowner,
3500 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003501};
3502
Andrew Elble7fc05642015-11-05 20:42:43 -05003503static struct nfs4_ol_stateid *
3504nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3505{
3506 struct nfs4_ol_stateid *local, *ret = NULL;
3507 struct nfs4_openowner *oo = open->op_openowner;
3508
3509 lockdep_assert_held(&fp->fi_lock);
3510
3511 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3512 /* ignore lock owners */
3513 if (local->st_stateowner->so_is_open_owner == 0)
3514 continue;
3515 if (local->st_stateowner == &oo->oo_owner) {
3516 ret = local;
3517 atomic_inc(&ret->st_stid.sc_count);
3518 break;
3519 }
3520 }
3521 return ret;
3522}
3523
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003524static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003525alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003526 struct nfsd4_compound_state *cstate)
3527{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003528 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003529 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003531 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3532 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003534 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003535 oo->oo_owner.so_is_open_owner = 1;
3536 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003537 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003538 if (nfsd4_has_session(cstate))
3539 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003540 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003541 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003542 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003543 spin_lock(&clp->cl_lock);
3544 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003545 if (ret == NULL) {
3546 hash_openowner(oo, clp, strhashval);
3547 ret = oo;
3548 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08003549 nfs4_free_stateowner(&oo->oo_owner);
3550
Trond Myklebustd4f04892014-07-29 21:34:36 -04003551 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04003552 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553}
3554
Andrew Elble7fc05642015-11-05 20:42:43 -05003555static struct nfs4_ol_stateid *
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003556init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
Andrew Elble7fc05642015-11-05 20:42:43 -05003557{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
Andrew Elble7fc05642015-11-05 20:42:43 -05003559 struct nfs4_openowner *oo = open->op_openowner;
3560 struct nfs4_ol_stateid *retstp = NULL;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003561 struct nfs4_ol_stateid *stp;
Andrew Elble7fc05642015-11-05 20:42:43 -05003562
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003563 stp = open->op_stp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003564 /* We are moving these outside of the spinlocks to avoid the warnings */
3565 mutex_init(&stp->st_mutex);
3566 mutex_lock(&stp->st_mutex);
3567
Andrew Elble7fc05642015-11-05 20:42:43 -05003568 spin_lock(&oo->oo_owner.so_client->cl_lock);
3569 spin_lock(&fp->fi_lock);
3570
3571 retstp = nfsd4_find_existing_open(fp, open);
3572 if (retstp)
3573 goto out_unlock;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003574
3575 open->op_stp = NULL;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003576 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003577 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003578 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003579 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07003580 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003581 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 stp->st_access_bmap = 0;
3583 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003584 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003585 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003586 list_add(&stp->st_perfile, &fp->fi_stateids);
Andrew Elble7fc05642015-11-05 20:42:43 -05003587
3588out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04003589 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003590 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003591 if (retstp) {
3592 mutex_lock(&retstp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003593 /* To keep mutex tracking happy */
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003594 mutex_unlock(&stp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003595 stp = retstp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003596 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003597 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598}
3599
Jeff Laytond3134b12014-07-29 21:34:32 -04003600/*
3601 * In the 4.0 case we need to keep the owners around a little while to handle
3602 * CLOSE replay. We still do need to release any file access that is held by
3603 * them before returning however.
3604 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003606move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607{
Jeff Layton217526e2014-07-30 08:27:11 -04003608 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003609 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3610 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3611 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003612
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003613 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614
Jeff Laytonb401be222014-07-29 21:34:33 -04003615 /*
3616 * We know that we hold one reference via nfsd4_close, and another
3617 * "persistent" reference for the client. If the refcount is higher
3618 * than 2, then there are still calls in progress that are using this
3619 * stateid. We can't put the sc_file reference until they are finished.
3620 * Wait for the refcount to drop to 2. Since it has been unhashed,
3621 * there should be no danger of the refcount going back up again at
3622 * this point.
3623 */
3624 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3625
Jeff Laytond3134b12014-07-29 21:34:32 -04003626 release_all_access(s);
3627 if (s->st_stid.sc_file) {
3628 put_nfs4_file(s->st_stid.sc_file);
3629 s->st_stid.sc_file = NULL;
3630 }
Jeff Layton217526e2014-07-30 08:27:11 -04003631
3632 spin_lock(&nn->client_lock);
3633 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003634 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003635 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003636 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003637 spin_unlock(&nn->client_lock);
3638 if (last)
3639 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640}
3641
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642/* search file_hashtbl[] for file */
3643static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04003644find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 struct nfs4_file *fp;
3647
Jeff Layton5b095e92014-10-23 08:01:02 -04003648 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02003649 if (fh_match(&fp->fi_fhandle, fh)) {
Jeff Layton5b095e92014-10-23 08:01:02 -04003650 if (atomic_inc_not_zero(&fp->fi_ref))
3651 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 }
3654 return NULL;
3655}
3656
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02003657struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003658find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003659{
3660 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003661 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003662
Jeff Layton5b095e92014-10-23 08:01:02 -04003663 rcu_read_lock();
3664 fp = find_file_locked(fh, hashval);
3665 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04003666 return fp;
3667}
3668
3669static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003670find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003671{
3672 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003673 unsigned int hashval = file_hashval(fh);
3674
3675 rcu_read_lock();
3676 fp = find_file_locked(fh, hashval);
3677 rcu_read_unlock();
3678 if (fp)
3679 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04003680
3681 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04003682 fp = find_file_locked(fh, hashval);
3683 if (likely(fp == NULL)) {
3684 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04003685 fp = new;
3686 }
3687 spin_unlock(&state_lock);
3688
3689 return fp;
3690}
3691
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003692/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 * Called to check deny when READ with all zero stateid or
3694 * WRITE with all zero or all one stateid
3695 */
Al Virob37ad282006-10-19 23:28:59 -07003696static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3698{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003700 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701
Trond Myklebustca943212014-07-23 16:17:39 -04003702 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003703 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003704 return ret;
3705 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003706 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003707 if (fp->fi_share_deny & deny_type)
3708 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003709 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003710 put_nfs4_file(fp);
3711 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712}
3713
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003714static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003716 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04003717 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3718 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003719
Trond Myklebust11b91642014-07-29 21:34:08 -04003720 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003721
Jeff Layton02e12152014-07-16 10:31:57 -04003722 /*
3723 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003724 * already holding inode->i_lock.
3725 *
Jeff Laytondff13992014-07-08 14:02:49 -04003726 * If the dl_time != 0, then we know that it has already been
3727 * queued for a lease break. Don't queue it again.
3728 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003729 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003730 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003731 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003732 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003733 }
Jeff Layton02e12152014-07-16 10:31:57 -04003734 spin_unlock(&state_lock);
3735}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003737static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3738 struct rpc_task *task)
3739{
3740 struct nfs4_delegation *dp = cb_to_delegation(cb);
3741
Andrew Elblea4579742015-08-31 12:06:41 -04003742 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3743 return 1;
3744
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003745 switch (task->tk_status) {
3746 case 0:
3747 return 1;
3748 case -EBADHANDLE:
3749 case -NFS4ERR_BAD_STATEID:
3750 /*
3751 * Race: client probably got cb_recall before open reply
3752 * granting delegation.
3753 */
3754 if (dp->dl_retries--) {
3755 rpc_delay(task, 2 * HZ);
3756 return 0;
3757 }
3758 /*FALLTHRU*/
3759 default:
3760 return -1;
3761 }
3762}
3763
3764static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3765{
3766 struct nfs4_delegation *dp = cb_to_delegation(cb);
3767
3768 nfs4_put_stid(&dp->dl_stid);
3769}
3770
Julia Lawallc4cb8972015-11-21 22:57:39 +01003771static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003772 .prepare = nfsd4_cb_recall_prepare,
3773 .done = nfsd4_cb_recall_done,
3774 .release = nfsd4_cb_recall_release,
3775};
3776
Jeff Layton02e12152014-07-16 10:31:57 -04003777static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3778{
3779 /*
3780 * We're assuming the state code never drops its reference
3781 * without first removing the lease. Since we're in this lease
3782 * callback (and since the lease code is serialized by the kernel
3783 * lock) we know the server hasn't removed the lease yet, we know
3784 * it's safe to take a reference.
3785 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003786 atomic_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02003787 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003788}
3789
Jeff Layton1c8c6012013-06-21 08:58:15 -04003790/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003791static bool
3792nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003793{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003794 bool ret = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003795 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3796 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003797
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003798 if (!fp) {
3799 WARN(1, "(%p)->fl_owner NULL\n", fl);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003800 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003801 }
3802 if (fp->fi_had_conflict) {
3803 WARN(1, "duplicate break on %p\n", fp);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003804 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003805 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003806 /*
3807 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003808 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003809 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003810 */
3811 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812
Jeff Layton02e12152014-07-16 10:31:57 -04003813 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003814 fp->fi_had_conflict = true;
3815 /*
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003816 * If there are no delegations on the list, then return true
3817 * so that the lease code will go ahead and delete it.
Jeff Layton417c6622014-07-21 09:34:57 -04003818 */
3819 if (list_empty(&fp->fi_delegations))
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003820 ret = true;
Jeff Layton417c6622014-07-21 09:34:57 -04003821 else
3822 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3823 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003824 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003825 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826}
3827
Jeff Laytonc45198e2014-09-01 07:12:07 -04003828static int
Jeff Layton7448cc32015-01-16 15:05:57 -05003829nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3830 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831{
3832 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04003833 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 else
3835 return -EAGAIN;
3836}
3837
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003838static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003839 .lm_break = nfsd_break_deleg_cb,
3840 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841};
3842
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003843static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3844{
3845 if (nfsd4_has_session(cstate))
3846 return nfs_ok;
3847 if (seqid == so->so_seqid - 1)
3848 return nfserr_replay_me;
3849 if (seqid == so->so_seqid)
3850 return nfs_ok;
3851 return nfserr_bad_seqid;
3852}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853
Jeff Layton4b24ca72014-06-30 11:48:44 -04003854static __be32 lookup_clientid(clientid_t *clid,
3855 struct nfsd4_compound_state *cstate,
3856 struct nfsd_net *nn)
3857{
3858 struct nfs4_client *found;
3859
3860 if (cstate->clp) {
3861 found = cstate->clp;
3862 if (!same_clid(&found->cl_clientid, clid))
3863 return nfserr_stale_clientid;
3864 return nfs_ok;
3865 }
3866
3867 if (STALE_CLIENTID(clid, nn))
3868 return nfserr_stale_clientid;
3869
3870 /*
3871 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3872 * cached already then we know this is for is for v4.0 and "sessions"
3873 * will be false.
3874 */
3875 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003876 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003877 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003878 if (!found) {
3879 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003880 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003881 }
3882 atomic_inc(&found->cl_refcount);
3883 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003884
3885 /* Cache the nfs4_client in cstate! */
3886 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003887 return nfs_ok;
3888}
3889
Al Virob37ad282006-10-19 23:28:59 -07003890__be32
Andy Adamson66689582009-04-03 08:28:45 +03003891nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003892 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 clientid_t *clientid = &open->op_clientid;
3895 struct nfs4_client *clp = NULL;
3896 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003897 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003898 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003900 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003902 /*
3903 * In case we need it later, after we've already created the
3904 * file and don't want to risk a further failure:
3905 */
3906 open->op_file = nfsd4_alloc_file();
3907 if (open->op_file == NULL)
3908 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909
Trond Myklebust2d91e892014-06-30 11:48:46 -04003910 status = lookup_clientid(clientid, cstate, nn);
3911 if (status)
3912 return status;
3913 clp = cstate->clp;
3914
Trond Myklebustd4f04892014-07-29 21:34:36 -04003915 strhashval = ownerstr_hashval(&open->op_owner);
3916 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003917 open->op_openowner = oo;
3918 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003919 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003921 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003922 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003923 release_openowner(oo);
3924 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003925 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003926 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003927 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3928 if (status)
3929 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003930 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003931new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003932 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003933 if (oo == NULL)
3934 return nfserr_jukebox;
3935 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003936alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003937 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003938 if (!open->op_stp)
3939 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003940
3941 if (nfsd4_has_session(cstate) &&
3942 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
3943 open->op_odstate = alloc_clnt_odstate(clp);
3944 if (!open->op_odstate)
3945 return nfserr_jukebox;
3946 }
3947
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003948 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949}
3950
Al Virob37ad282006-10-19 23:28:59 -07003951static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003952nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3953{
3954 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3955 return nfserr_openmode;
3956 else
3957 return nfs_ok;
3958}
3959
Daniel Mackc47d8322011-05-16 16:38:14 +02003960static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003961{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003962 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3963}
3964
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003965static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003966{
3967 struct nfs4_stid *ret;
3968
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003969 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003970 if (!ret)
3971 return NULL;
3972 return delegstateid(ret);
3973}
3974
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003975static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3976{
3977 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3978 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3979}
3980
Al Virob37ad282006-10-19 23:28:59 -07003981static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003982nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003983 struct nfs4_delegation **dp)
3984{
3985 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003986 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003987 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003988
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003989 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3990 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003991 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003992 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003993 status = nfs4_check_delegmode(deleg, flags);
3994 if (status) {
3995 nfs4_put_stid(&deleg->dl_stid);
3996 goto out;
3997 }
3998 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003999out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004000 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004001 return nfs_ok;
4002 if (status)
4003 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004004 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004005 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07004006}
4007
J. Bruce Fields21fb4012010-07-28 12:21:23 -04004008static inline int nfs4_access_to_access(u32 nfs4_access)
4009{
4010 int flags = 0;
4011
4012 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4013 flags |= NFSD_MAY_READ;
4014 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4015 flags |= NFSD_MAY_WRITE;
4016 return flags;
4017}
4018
Al Virob37ad282006-10-19 23:28:59 -07004019static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4021 struct nfsd4_open *open)
4022{
4023 struct iattr iattr = {
4024 .ia_valid = ATTR_SIZE,
4025 .ia_size = 0,
4026 };
4027 if (!open->op_truncate)
4028 return 0;
4029 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08004030 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
4032}
4033
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004034static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004035 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4036 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004037{
Trond Myklebustde186432014-07-10 14:07:26 -04004038 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004039 __be32 status;
4040 int oflag = nfs4_access_to_omode(open->op_share_access);
4041 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004042 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004043
Trond Myklebustde186432014-07-10 14:07:26 -04004044 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004045
4046 /*
4047 * Are we trying to set a deny mode that would conflict with
4048 * current access?
4049 */
4050 status = nfs4_file_check_deny(fp, open->op_share_deny);
4051 if (status != nfs_ok) {
4052 spin_unlock(&fp->fi_lock);
4053 goto out;
4054 }
4055
4056 /* set access to the file */
4057 status = nfs4_file_get_access(fp, open->op_share_access);
4058 if (status != nfs_ok) {
4059 spin_unlock(&fp->fi_lock);
4060 goto out;
4061 }
4062
4063 /* Set access bits in stateid */
4064 old_access_bmap = stp->st_access_bmap;
4065 set_access(open->op_share_access, stp);
4066
4067 /* Set new deny mask */
4068 old_deny_bmap = stp->st_deny_bmap;
4069 set_deny(open->op_share_deny, stp);
4070 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4071
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004072 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04004073 spin_unlock(&fp->fi_lock);
4074 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004075 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004076 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04004077 spin_lock(&fp->fi_lock);
4078 if (!fp->fi_fds[oflag]) {
4079 fp->fi_fds[oflag] = filp;
4080 filp = NULL;
4081 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004082 }
Trond Myklebustde186432014-07-10 14:07:26 -04004083 spin_unlock(&fp->fi_lock);
4084 if (filp)
4085 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004086
4087 status = nfsd4_truncate(rqstp, cur_fh, open);
4088 if (status)
4089 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004090out:
4091 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004092out_put_access:
4093 stp->st_access_bmap = old_access_bmap;
4094 nfs4_file_put_access(fp, open->op_share_access);
4095 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4096 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004097}
4098
Al Virob37ad282006-10-19 23:28:59 -07004099static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004100nfs4_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 -07004101{
Al Virob37ad282006-10-19 23:28:59 -07004102 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02004103 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004105 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004106 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004107
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004108 /* test and set deny mode */
4109 spin_lock(&fp->fi_lock);
4110 status = nfs4_file_check_deny(fp, open->op_share_deny);
4111 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004112 set_deny(open->op_share_deny, stp);
4113 fp->fi_share_deny |=
4114 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4115 }
4116 spin_unlock(&fp->fi_lock);
4117
4118 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004121 status = nfsd4_truncate(rqstp, cur_fh, open);
4122 if (status != nfs_ok)
4123 reset_union_bmap_deny(old_deny_bmap, stp);
4124 return status;
4125}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004127/* Should we give out recallable state?: */
4128static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4129{
4130 if (clp->cl_cb_state == NFSD4_CB_UP)
4131 return true;
4132 /*
4133 * In the sessions case, since we don't have to establish a
4134 * separate connection for callbacks, we assume it's OK
4135 * until we hear otherwise:
4136 */
4137 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4138}
4139
Jeff Laytond564fbe2014-07-16 10:31:58 -04004140static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004141{
4142 struct file_lock *fl;
4143
4144 fl = locks_alloc_lock();
4145 if (!fl)
4146 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004147 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04004148 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004149 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4150 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04004151 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004152 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004153 return fl;
4154}
4155
Andrew Elble34ed9872015-10-15 12:07:28 -04004156/**
4157 * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer
4158 * @dp: a pointer to the nfs4_delegation we're adding.
4159 *
4160 * Return:
4161 * On success: Return code will be 0 on success.
4162 *
4163 * On error: -EAGAIN if there was an existing delegation.
4164 * nonzero if there is an error in other cases.
4165 *
4166 */
4167
J. Bruce Fields99c41512013-05-21 16:21:25 -04004168static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004169{
Trond Myklebust11b91642014-07-29 21:34:08 -04004170 struct nfs4_file *fp = dp->dl_stid.sc_file;
Kinglong Meeefde6b42015-07-13 17:33:24 +08004171 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04004172 struct file *filp;
4173 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004174
Jeff Laytond564fbe2014-07-16 10:31:58 -04004175 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004176 if (!fl)
4177 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04004178 filp = find_readable_file(fp);
4179 if (!filp) {
4180 /* We should always have a readable file here */
4181 WARN_ON_ONCE(1);
Kinglong Meeaf9dbaf2015-07-13 17:30:51 +08004182 locks_free_lock(fl);
Jeff Layton417c6622014-07-21 09:34:57 -04004183 return -EBADF;
4184 }
4185 fl->fl_file = filp;
Jeff Laytone6f5c782014-08-22 10:40:25 -04004186 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04004187 if (fl)
Jeff Layton417c6622014-07-21 09:34:57 -04004188 locks_free_lock(fl);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04004189 if (status)
Jeff Layton417c6622014-07-21 09:34:57 -04004190 goto out_fput;
Benny Halevycdc975052014-05-30 09:09:30 -04004191 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004192 spin_lock(&fp->fi_lock);
4193 /* Did the lease get broken before we took the lock? */
4194 status = -EAGAIN;
4195 if (fp->fi_had_conflict)
4196 goto out_unlock;
4197 /* Race breaker */
Jeff Layton0c637be2014-08-22 12:05:43 -04004198 if (fp->fi_deleg_file) {
Andrew Elble34ed9872015-10-15 12:07:28 -04004199 status = hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04004200 goto out_unlock;
4201 }
Jeff Layton417c6622014-07-21 09:34:57 -04004202 fp->fi_deleg_file = filp;
Andrew Elble34ed9872015-10-15 12:07:28 -04004203 fp->fi_delegees = 0;
4204 status = hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04004205 spin_unlock(&fp->fi_lock);
Benny Halevycdc975052014-05-30 09:09:30 -04004206 spin_unlock(&state_lock);
Andrew Elble34ed9872015-10-15 12:07:28 -04004207 if (status) {
4208 /* Should never happen, this is a new fi_deleg_file */
4209 WARN_ON_ONCE(1);
4210 goto out_fput;
4211 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004212 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04004213out_unlock:
4214 spin_unlock(&fp->fi_lock);
4215 spin_unlock(&state_lock);
4216out_fput:
4217 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05004218 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004219}
4220
Jeff Layton0b266932014-07-25 07:34:25 -04004221static struct nfs4_delegation *
4222nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004223 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004224{
Jeff Layton0b266932014-07-25 07:34:25 -04004225 int status;
4226 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04004227
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004228 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004229 return ERR_PTR(-EAGAIN);
4230
Andrew Elble34ed9872015-10-15 12:07:28 -04004231 spin_lock(&state_lock);
4232 spin_lock(&fp->fi_lock);
4233 status = nfs4_get_existing_delegation(clp, fp);
4234 spin_unlock(&fp->fi_lock);
4235 spin_unlock(&state_lock);
4236
4237 if (status)
4238 return ERR_PTR(status);
4239
Sachin Bhamare8287f002015-04-27 14:50:14 +02004240 dp = alloc_init_deleg(clp, fh, odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004241 if (!dp)
4242 return ERR_PTR(-ENOMEM);
4243
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004244 get_nfs4_file(fp);
Benny Halevycdc975052014-05-30 09:09:30 -04004245 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004246 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04004247 dp->dl_stid.sc_file = fp;
Jeff Layton0c637be2014-08-22 12:05:43 -04004248 if (!fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04004249 spin_unlock(&fp->fi_lock);
4250 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004251 status = nfs4_setlease(dp);
4252 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04004253 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004254 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04004255 status = -EAGAIN;
4256 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004257 }
Andrew Elble34ed9872015-10-15 12:07:28 -04004258 status = hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04004259out_unlock:
4260 spin_unlock(&fp->fi_lock);
Benny Halevycdc975052014-05-30 09:09:30 -04004261 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004262out:
4263 if (status) {
Sachin Bhamare8287f002015-04-27 14:50:14 +02004264 put_clnt_odstate(dp->dl_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04004265 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04004266 return ERR_PTR(status);
4267 }
4268 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004269}
4270
Benny Halevy4aa89132012-02-21 14:16:44 -08004271static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4272{
4273 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4274 if (status == -EAGAIN)
4275 open->op_why_no_deleg = WND4_CONTENTION;
4276 else {
4277 open->op_why_no_deleg = WND4_RESOURCE;
4278 switch (open->op_deleg_want) {
4279 case NFS4_SHARE_WANT_READ_DELEG:
4280 case NFS4_SHARE_WANT_WRITE_DELEG:
4281 case NFS4_SHARE_WANT_ANY_DELEG:
4282 break;
4283 case NFS4_SHARE_WANT_CANCEL:
4284 open->op_why_no_deleg = WND4_CANCELLED;
4285 break;
4286 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004287 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08004288 }
4289 }
4290}
4291
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292/*
4293 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04004294 *
4295 * Note we don't support write delegations, and won't until the vfs has
4296 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 */
4298static void
Jeff Layton4cf59222014-07-25 07:34:24 -04004299nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4300 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301{
4302 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04004303 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4304 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004305 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004306 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004308 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07004309 open->op_recall = 0;
4310 switch (open->op_claim_type) {
4311 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05004312 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07004313 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004314 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4315 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004316 break;
4317 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00004318 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004319 /*
4320 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004321 * had the chance to reclaim theirs, *and* until
4322 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004323 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004324 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004325 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004326 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004327 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04004328 /*
4329 * Also, if the file was opened for write or
4330 * create, there's a good chance the client's
4331 * about to write to it, resulting in an
4332 * immediate recall (since we don't support
4333 * write delegations):
4334 */
NeilBrown7b190fe2005-06-23 22:03:23 -07004335 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04004336 goto out_no_deleg;
4337 if (open->op_create == NFS4_OPEN_CREATE)
4338 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004339 break;
4340 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004341 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004342 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004343 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004344 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004345 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004347 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004349 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004350 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04004351 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04004352 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004353 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004354out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004355 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4356 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004357 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04004358 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004359 open->op_recall = 1;
4360 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004361
4362 /* 4.1 client asking for a delegation? */
4363 if (open->op_deleg_want)
4364 nfsd4_open_deleg_none_ext(open, status);
4365 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366}
4367
Benny Halevye27f49c2012-02-21 14:16:54 -08004368static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4369 struct nfs4_delegation *dp)
4370{
4371 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4372 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4373 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4374 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4375 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4376 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4377 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4378 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4379 }
4380 /* Otherwise the client must be confused wanting a delegation
4381 * it already has, therefore we don't return
4382 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4383 */
4384}
4385
Al Virob37ad282006-10-19 23:28:59 -07004386__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4388{
Andy Adamson66689582009-04-03 08:28:45 +03004389 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004390 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004392 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004393 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004394 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 /*
4397 * Lookup file; if found, lookup stateid and check open request,
4398 * and check for delegations in the process of being recalled.
4399 * If not found, create the nfs4_file struct
4400 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004401 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004402 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004403 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004404 if (status)
4405 goto out;
Andrew Elble7fc05642015-11-05 20:42:43 -05004406 spin_lock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04004407 stp = nfsd4_find_existing_open(fp, open);
Andrew Elble7fc05642015-11-05 20:42:43 -05004408 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004410 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004411 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004412 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004413 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 }
4415
4416 /*
4417 * OPEN the file, or upgrade an existing OPEN.
4418 * If truncate fails, the OPEN fails.
4419 */
4420 if (stp) {
4421 /* Stateid was found, this is an OPEN upgrade */
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004422 mutex_lock(&stp->st_mutex);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004423 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04004424 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004425 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04004427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 } else {
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004429 /* stp is returned locked. */
4430 stp = init_open_stateid(fp, open);
4431 /* See if we lost the race to some other thread */
4432 if (stp->st_access_bmap != 0) {
Andrew Elble7fc05642015-11-05 20:42:43 -05004433 status = nfs4_upgrade_open(rqstp, fp, current_fh,
4434 stp, open);
4435 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004436 mutex_unlock(&stp->st_mutex);
Andrew Elble7fc05642015-11-05 20:42:43 -05004437 goto out;
4438 }
4439 goto upgrade_out;
4440 }
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004441 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4442 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004443 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004444 release_open_stateid(stp);
4445 goto out;
4446 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004447
4448 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4449 open->op_odstate);
4450 if (stp->st_clnt_odstate == open->op_odstate)
4451 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 }
Andrew Elble7fc05642015-11-05 20:42:43 -05004453upgrade_out:
Jeff Layton9767feb2015-10-01 09:05:50 -04004454 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004455 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456
Benny Halevyd24433c2012-02-16 20:57:17 +02004457 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004458 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4459 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4460 open->op_why_no_deleg = WND4_NOT_WANTED;
4461 goto nodeleg;
4462 }
4463 }
4464
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 /*
4466 * Attempt to hand out a delegation. No error return, because the
4467 * OPEN succeeds even if we fail.
4468 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004469 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004470nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 status = nfs_ok;
4472
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004473 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004474 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004476 /* 4.1 client trying to upgrade/downgrade delegation? */
4477 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004478 open->op_deleg_want)
4479 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004480
NeilBrown13cd2182005-06-23 22:03:10 -07004481 if (fp)
4482 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004483 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08004484 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 /*
4486 * To finish the open response, we just need to set the rflags.
4487 */
4488 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Jeff Layton19e4c342016-09-16 16:28:27 -04004489 if (nfsd4_has_session(&resp->cstate))
4490 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
4491 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Jeff Layton19e4c342016-09-16 16:28:27 -04004493
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004494 if (dp)
4495 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004496 if (stp)
4497 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498
4499 return status;
4500}
4501
Jeff Layton58fb12e2014-07-29 21:34:27 -04004502void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04004503 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004504{
4505 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004506 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004507
Jeff Laytond3134b12014-07-29 21:34:32 -04004508 nfsd4_cstate_assign_replay(cstate, so);
4509 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004510 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004511 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04004512 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004513 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004514 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004515 if (open->op_odstate)
4516 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004517}
4518
Al Virob37ad282006-10-19 23:28:59 -07004519__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004520nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02004521 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522{
Christoph Hellwigeb698532017-05-08 20:58:35 +02004523 clientid_t *clid = &u->renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004525 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004526 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 dprintk("process_renew(%08x/%08x): starting\n",
4529 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004530 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004531 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004533 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004535 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004536 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 goto out;
4538 status = nfs_ok;
4539out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 return status;
4541}
4542
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004543void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004544nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004545{
Jeff Layton33dcc482012-04-10 11:08:48 -04004546 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004547 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004548 return;
4549
NeilBrowna76b4312005-06-23 22:04:01 -07004550 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004551 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004552 /*
4553 * If the server goes down again right now, an NFSv4
4554 * client will still be allowed to reclaim after it comes back up,
4555 * even if it hasn't yet had a chance to reclaim state this time.
4556 *
4557 */
Jeff Layton919b8042014-09-12 16:40:20 -04004558 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004559 /*
4560 * At this point, NFSv4 clients can still reclaim. But if the
4561 * server crashes, any that have not yet reclaimed will be out
4562 * of luck on the next boot.
4563 *
4564 * (NFSv4.1+ clients are considered to have reclaimed once they
4565 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4566 * have reclaimed after their first OPEN.)
4567 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004568 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004569 /*
4570 * At this point, and once lockd and/or any other containers
4571 * exit their grace period, further reclaims will fail and
4572 * regular locking can resume.
4573 */
NeilBrowna76b4312005-06-23 22:04:01 -07004574}
4575
NeilBrownfd39ca92005-06-23 22:04:03 -07004576static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004577nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578{
4579 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004580 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004582 struct nfs4_ol_stateid *stp;
Jeff Layton7919d0a2016-09-16 16:28:25 -04004583 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004585 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004586 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004589 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004590 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004591 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004592 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 clp = list_entry(pos, struct nfs4_client, cl_lru);
4594 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4595 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004596 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 break;
4598 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004599 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004600 dprintk("NFSD: client in use (clientid %08x)\n",
4601 clp->cl_clientid.cl_id);
4602 continue;
4603 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004604 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004605 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004606 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004607 list_for_each_safe(pos, next, &reaplist) {
4608 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 dprintk("NFSD: purging unused client (clientid %08x)\n",
4610 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004611 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 expire_client(clp);
4613 }
Benny Halevycdc975052014-05-30 09:09:30 -04004614 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004615 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4617 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004618 t = dp->dl_time - cutoff;
4619 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 break;
4621 }
Jeff Layton3fcbbd22015-08-24 12:41:48 -04004622 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04004623 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 }
Benny Halevycdc975052014-05-30 09:09:30 -04004625 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004626 while (!list_empty(&reaplist)) {
4627 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4628 dl_recall_lru);
4629 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004630 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 }
Jeff Layton217526e2014-07-30 08:27:11 -04004632
4633 spin_lock(&nn->client_lock);
4634 while (!list_empty(&nn->close_lru)) {
4635 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4636 oo_close_lru);
4637 if (time_after((unsigned long)oo->oo_time,
4638 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004639 t = oo->oo_time - cutoff;
4640 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 break;
4642 }
Jeff Layton217526e2014-07-30 08:27:11 -04004643 list_del_init(&oo->oo_close_lru);
4644 stp = oo->oo_last_closed_stid;
4645 oo->oo_last_closed_stid = NULL;
4646 spin_unlock(&nn->client_lock);
4647 nfs4_put_stid(&stp->st_stid);
4648 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 }
Jeff Layton217526e2014-07-30 08:27:11 -04004650 spin_unlock(&nn->client_lock);
4651
Jeff Layton7919d0a2016-09-16 16:28:25 -04004652 /*
4653 * It's possible for a client to try and acquire an already held lock
4654 * that is being held for a long time, and then lose interest in it.
4655 * So, we clean out any un-revisited request after a lease period
4656 * under the assumption that the client is no longer interested.
4657 *
4658 * RFC5661, sec. 9.6 states that the client must not rely on getting
4659 * notifications and must continue to poll for locks, even when the
4660 * server supports them. Thus this shouldn't lead to clients blocking
4661 * indefinitely once the lock does become free.
4662 */
4663 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04004664 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04004665 while (!list_empty(&nn->blocked_locks_lru)) {
4666 nbl = list_first_entry(&nn->blocked_locks_lru,
4667 struct nfsd4_blocked_lock, nbl_lru);
4668 if (time_after((unsigned long)nbl->nbl_time,
4669 (unsigned long)cutoff)) {
4670 t = nbl->nbl_time - cutoff;
4671 new_timeo = min(new_timeo, t);
4672 break;
4673 }
4674 list_move(&nbl->nbl_lru, &reaplist);
4675 list_del_init(&nbl->nbl_list);
4676 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04004677 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04004678
4679 while (!list_empty(&reaplist)) {
4680 nbl = list_first_entry(&nn->blocked_locks_lru,
4681 struct nfsd4_blocked_lock, nbl_lru);
4682 list_del_init(&nbl->nbl_lru);
4683 posix_unblock_lock(&nbl->nbl_lock);
4684 free_blocked_lock(nbl);
4685 }
4686
Jeff Laytona832e7a2014-05-30 09:09:26 -04004687 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004688 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689}
4690
Harvey Harrisona254b242008-02-20 12:49:00 -08004691static struct workqueue_struct *laundry_wq;
4692static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004693
4694static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004695laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696{
4697 time_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08004698 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004699 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4700 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004702 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004704 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705}
4706
Jeff Layton8fcd4612015-07-30 06:57:46 -04004707static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004708{
Jeff Layton8fcd4612015-07-30 06:57:46 -04004709 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004710 return nfserr_bad_stateid;
4711 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712}
4713
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004715access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004717 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4718 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4719 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720}
4721
4722static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004723access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004725 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4726 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727}
4728
4729static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004730__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731{
Al Virob37ad282006-10-19 23:28:59 -07004732 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
J. Bruce Fields02921912010-07-29 15:16:59 -04004734 /* For lock stateid's, we test the parent open, not the lock: */
4735 if (stp->st_openstp)
4736 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004737 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004739 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 goto out;
4741 status = nfs_ok;
4742out:
4743 return status;
4744}
4745
Al Virob37ad282006-10-19 23:28:59 -07004746static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004747check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004749 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004751 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004752 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 * conflicting state; so we must wait out the grace period. */
4754 return nfserr_grace;
4755 } else if (flags & WR_STATE)
4756 return nfs4_share_conflict(current_fh,
4757 NFS4_SHARE_DENY_WRITE);
4758 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4759 return nfs4_share_conflict(current_fh,
4760 NFS4_SHARE_DENY_READ);
4761}
4762
4763/*
4764 * Allow READ/WRITE during grace period on recovered state only for files
4765 * that are not able to provide mandatory locking.
4766 */
4767static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004768grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004770 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771}
4772
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004773static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004774{
Andy Adamson66689582009-04-03 08:28:45 +03004775 /*
4776 * When sessions are used the stateid generation number is ignored
4777 * when it is zero.
4778 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004779 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004780 return nfs_ok;
4781
4782 if (in->si_generation == ref->si_generation)
4783 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004784
J. Bruce Fields0836f582008-01-26 19:08:12 -05004785 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04004786 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004787 return nfserr_bad_stateid;
4788 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004789 * However, we could see a stateid from the past, even from a
4790 * non-buggy client. For example, if the client sends a lock
4791 * while some IO is outstanding, the lock may bump si_generation
4792 * while the IO is still in flight. The client could avoid that
4793 * situation by waiting for responses on all the IO requests,
4794 * but better performance may result in retrying IO that
4795 * receives an old_stateid error if requests are rarely
4796 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004797 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004798 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004799}
4800
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004801static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4802{
4803 if (ols->st_stateowner->so_is_open_owner &&
4804 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4805 return nfserr_bad_stateid;
4806 return nfs_ok;
4807}
4808
Chuck Lever7df302f2012-05-29 13:56:37 -04004809static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004810{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004811 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004812 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004813
Chuck Lever7df302f2012-05-29 13:56:37 -04004814 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004815 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004816 /* Client debugging aid. */
4817 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4818 char addr_str[INET6_ADDRSTRLEN];
4819 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4820 sizeof(addr_str));
4821 pr_warn_ratelimited("NFSD: client %s testing state ID "
4822 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004823 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004824 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004825 spin_lock(&cl->cl_lock);
4826 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004827 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004828 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004829 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004830 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004831 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004832 switch (s->sc_type) {
4833 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004834 status = nfs_ok;
4835 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004836 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004837 status = nfserr_deleg_revoked;
4838 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004839 case NFS4_OPEN_STID:
4840 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004841 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04004842 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004843 default:
4844 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004845 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004846 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004847 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004848 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004849 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004850out_unlock:
4851 spin_unlock(&cl->cl_lock);
4852 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004853}
4854
Christoph Hellwigcd61c522014-08-14 08:44:57 +02004855__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004856nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4857 stateid_t *stateid, unsigned char typemask,
4858 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004859{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004860 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004861
4862 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4863 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004864 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004865 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004866 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004867 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004868 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004869 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004870 if (status)
4871 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004872 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004873 if (!*s)
4874 return nfserr_bad_stateid;
4875 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004876}
4877
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004878static struct file *
4879nfs4_find_file(struct nfs4_stid *s, int flags)
4880{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004881 if (!s)
4882 return NULL;
4883
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004884 switch (s->sc_type) {
4885 case NFS4_DELEG_STID:
4886 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
4887 return NULL;
4888 return get_file(s->sc_file->fi_deleg_file);
4889 case NFS4_OPEN_STID:
4890 case NFS4_LOCK_STID:
4891 if (flags & RD_STATE)
4892 return find_readable_file(s->sc_file);
4893 else
4894 return find_writeable_file(s->sc_file);
4895 break;
4896 }
4897
4898 return NULL;
4899}
4900
4901static __be32
4902nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
4903{
4904 __be32 status;
4905
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004906 status = nfsd4_check_openowner_confirmed(ols);
4907 if (status)
4908 return status;
4909 return nfs4_check_openmode(ols, flags);
4910}
4911
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004912static __be32
4913nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
4914 struct file **filpp, bool *tmp_file, int flags)
4915{
4916 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
4917 struct file *file;
4918 __be32 status;
4919
4920 file = nfs4_find_file(s, flags);
4921 if (file) {
4922 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
4923 acc | NFSD_MAY_OWNER_OVERRIDE);
4924 if (status) {
4925 fput(file);
4926 return status;
4927 }
4928
4929 *filpp = file;
4930 } else {
4931 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
4932 if (status)
4933 return status;
4934
4935 if (tmp_file)
4936 *tmp_file = true;
4937 }
4938
4939 return 0;
4940}
4941
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004943 * Checks for stateid operations
4944 */
Al Virob37ad282006-10-19 23:28:59 -07004945__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004946nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01004947 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
4948 stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004950 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004951 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004952 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004953 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004954 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 if (filpp)
4957 *filpp = NULL;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004958 if (tmp_file)
4959 *tmp_file = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004961 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 return nfserr_grace;
4963
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004964 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
4965 status = check_special_stateids(net, fhp, stateid, flags);
4966 goto done;
4967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004969 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004970 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004971 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004972 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04004973 return status;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004974 status = check_stateid_generation(stateid, &s->sc_stateid,
4975 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04004976 if (status)
4977 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004978
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004979 switch (s->sc_type) {
4980 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004981 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004982 break;
4983 case NFS4_OPEN_STID:
4984 case NFS4_LOCK_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004985 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004986 break;
4987 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004988 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004989 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04004991 if (status)
4992 goto out;
4993 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004994
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004995done:
4996 if (!status && filpp)
4997 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998out:
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004999 if (s)
5000 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 return status;
5002}
5003
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005004/*
Bryan Schumaker17456802011-07-13 10:50:48 -04005005 * Test if the stateid is valid
5006 */
5007__be32
5008nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005009 union nfsd4_op_u *u)
Bryan Schumaker17456802011-07-13 10:50:48 -04005010{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005011 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005012 struct nfsd4_test_stateid_id *stateid;
5013 struct nfs4_client *cl = cstate->session->se_client;
5014
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005015 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04005016 stateid->ts_id_status =
5017 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005018
Bryan Schumaker17456802011-07-13 10:50:48 -04005019 return nfs_ok;
5020}
5021
Chuck Lever42691392016-08-11 10:37:30 -04005022static __be32
5023nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5024{
5025 struct nfs4_ol_stateid *stp = openlockstateid(s);
5026 __be32 ret;
5027
5028 mutex_lock(&stp->st_mutex);
5029
5030 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5031 if (ret)
5032 goto out;
5033
5034 ret = nfserr_locks_held;
5035 if (check_for_locks(stp->st_stid.sc_file,
5036 lockowner(stp->st_stateowner)))
5037 goto out;
5038
5039 release_lock_stateid(stp);
5040 ret = nfs_ok;
5041
5042out:
5043 mutex_unlock(&stp->st_mutex);
5044 nfs4_put_stid(s);
5045 return ret;
5046}
5047
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005048__be32
5049nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005050 union nfsd4_op_u *u)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005051{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005052 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005053 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005054 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005055 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005056 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005057 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005058
Jeff Layton1af71cc2014-07-29 21:34:14 -04005059 spin_lock(&cl->cl_lock);
5060 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005061 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005062 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005063 switch (s->sc_type) {
5064 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005065 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005066 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005067 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005068 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5069 if (ret)
5070 break;
5071 ret = nfserr_locks_held;
5072 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005073 case NFS4_LOCK_STID:
Chuck Lever42691392016-08-11 10:37:30 -04005074 atomic_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005075 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04005076 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005077 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005078 case NFS4_REVOKED_DELEG_STID:
5079 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005080 list_del_init(&dp->dl_recall_lru);
5081 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04005082 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005083 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005084 goto out;
5085 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005086 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005087out_unlock:
5088 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005089out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005090 return ret;
5091}
5092
NeilBrown4c4cd222005-07-07 17:59:27 -07005093static inline int
5094setlkflg (int type)
5095{
5096 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5097 RD_STATE : WR_STATE;
5098}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005100static __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 -04005101{
5102 struct svc_fh *current_fh = &cstate->current_fh;
5103 struct nfs4_stateowner *sop = stp->st_stateowner;
5104 __be32 status;
5105
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005106 status = nfsd4_check_seqid(cstate, sop, seqid);
5107 if (status)
5108 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005109 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
5110 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005111 /*
5112 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005113 * nfserr_replay_me from the previous step, and
5114 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005115 */
5116 return nfserr_bad_stateid;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005117 mutex_lock(&stp->st_mutex);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005118 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04005119 if (status == nfs_ok)
5120 status = nfs4_check_fh(current_fh, &stp->st_stid);
5121 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005122 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005123 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005124}
5125
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126/*
5127 * Checks for sequence id mutating operations.
5128 */
Al Virob37ad282006-10-19 23:28:59 -07005129static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03005130nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04005131 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005132 struct nfs4_ol_stateid **stpp,
5133 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134{
J. Bruce Fields0836f582008-01-26 19:08:12 -05005135 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005136 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005137 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005139 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5140 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07005141
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005143 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005144 if (status)
5145 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005146 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005147 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005149 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005150 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005151 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04005152 else
5153 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005154 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005155}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05005156
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005157static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5158 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005159{
5160 __be32 status;
5161 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005162 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005164 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005165 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04005166 if (status)
5167 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005168 oo = openowner(stp->st_stateowner);
5169 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005170 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005171 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07005172 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005173 }
5174 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07005175 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176}
5177
Al Virob37ad282006-10-19 23:28:59 -07005178__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005179nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005180 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005182 struct nfsd4_open_confirm *oc = &u->open_confirm;
Al Virob37ad282006-10-19 23:28:59 -07005183 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005184 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005185 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005186 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187
Al Viroa6a9f182013-09-16 10:57:01 -04005188 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5189 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005191 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07005192 if (status)
5193 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005195 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005196 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005197 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005198 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005199 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005200 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005201 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005202 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005203 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04005204 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005205 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04005206 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04005207 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005208 mutex_unlock(&stp->st_mutex);
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005209 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005210 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07005211
Jeff Layton2a4317c2012-03-21 16:42:43 -04005212 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005213 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04005214put_stateid:
5215 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005217 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 return status;
5219}
5220
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005221static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005223 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005224 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04005225 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005226 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005227}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04005228
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005229static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5230{
5231 switch (to_access) {
5232 case NFS4_SHARE_ACCESS_READ:
5233 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5234 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5235 break;
5236 case NFS4_SHARE_ACCESS_WRITE:
5237 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5238 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5239 break;
5240 case NFS4_SHARE_ACCESS_BOTH:
5241 break;
5242 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005243 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 }
5245}
5246
Al Virob37ad282006-10-19 23:28:59 -07005247__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005248nfsd4_open_downgrade(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005249 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005251 struct nfsd4_open_downgrade *od = &u->open_downgrade;
Al Virob37ad282006-10-19 23:28:59 -07005252 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005253 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005254 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255
Al Viroa6a9f182013-09-16 10:57:01 -04005256 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5257 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04005259 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02005260 if (od->od_deleg_want)
5261 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5262 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005264 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005265 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005266 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005269 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005270 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005272 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04005274 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005275 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005277 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005279 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04005280 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04005281 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005283put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005284 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005285 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005287 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288 return status;
5289}
5290
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005291static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5292{
Trond Myklebustacf92952014-06-30 11:48:37 -04005293 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04005294 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04005295 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005296
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005297 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04005298 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04005299 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005300
Jeff Laytond83017f2014-07-29 21:34:42 -04005301 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04005302 if (unhashed)
5303 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04005304 spin_unlock(&clp->cl_lock);
5305 free_ol_stateid_reaplist(&reaplist);
5306 } else {
5307 spin_unlock(&clp->cl_lock);
5308 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04005309 if (unhashed)
5310 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04005311 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04005312}
5313
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314/*
5315 * nfs4_unlock_state() called after encode
5316 */
Al Virob37ad282006-10-19 23:28:59 -07005317__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005318nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005319 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005321 struct nfsd4_close *close = &u->close;
Al Virob37ad282006-10-19 23:28:59 -07005322 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005323 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005324 struct net *net = SVC_NET(rqstp);
5325 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326
Al Viroa6a9f182013-09-16 10:57:01 -04005327 dprintk("NFSD: nfsd4_close on file %pd\n",
5328 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005330 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5331 &close->cl_stateid,
5332 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005333 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005334 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005335 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 goto out;
Jeff Layton9767feb2015-10-01 09:05:50 -04005337 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005338 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005340 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005341
5342 /* put reference from nfs4_preprocess_seqid_op */
5343 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 return status;
5346}
5347
Al Virob37ad282006-10-19 23:28:59 -07005348__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005349nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005350 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005352 struct nfsd4_delegreturn *dr = &u->delegreturn;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005353 struct nfs4_delegation *dp;
5354 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005355 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07005356 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005357 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005359 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005360 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005362 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005363 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005364 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005365 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04005366 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005367 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04005368 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005369
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005370 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005371put_stateid:
5372 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373out:
5374 return status;
5375}
5376
Benny Halevy87df4de2008-12-15 19:42:03 +02005377static inline u64
5378end_offset(u64 start, u64 len)
5379{
5380 u64 end;
5381
5382 end = start + len;
5383 return end >= start ? end: NFS4_MAX_UINT64;
5384}
5385
5386/* last octet in a range */
5387static inline u64
5388last_byte_offset(u64 start, u64 len)
5389{
5390 u64 end;
5391
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005392 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02005393 end = start + len;
5394 return end > start ? end - 1: NFS4_MAX_UINT64;
5395}
5396
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397/*
5398 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5399 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5400 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5401 * locking, this prevents us from being completely protocol-compliant. The
5402 * real solution to this problem is to start using unsigned file offsets in
5403 * the VFS, but this is a very deep change!
5404 */
5405static inline void
5406nfs4_transform_lock_offset(struct file_lock *lock)
5407{
5408 if (lock->fl_start < 0)
5409 lock->fl_start = OFFSET_MAX;
5410 if (lock->fl_end < 0)
5411 lock->fl_end = OFFSET_MAX;
5412}
5413
Jeff Laytoncae80b32015-04-03 09:04:04 -04005414static fl_owner_t
5415nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005416{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005417 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5418
5419 nfs4_get_stateowner(&lo->lo_owner);
5420 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005421}
5422
Jeff Laytoncae80b32015-04-03 09:04:04 -04005423static void
5424nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005425{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005426 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005427
Jeff Laytoncae80b32015-04-03 09:04:04 -04005428 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04005429 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04005430}
5431
Jeff Layton76d348f2016-09-16 16:28:24 -04005432static void
5433nfsd4_lm_notify(struct file_lock *fl)
5434{
5435 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
5436 struct net *net = lo->lo_owner.so_client->net;
5437 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5438 struct nfsd4_blocked_lock *nbl = container_of(fl,
5439 struct nfsd4_blocked_lock, nbl_lock);
5440 bool queue = false;
5441
Jeff Layton7919d0a2016-09-16 16:28:25 -04005442 /* An empty list means that something else is going to be using it */
Jeff Layton0cc11a62016-10-20 09:34:31 -04005443 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005444 if (!list_empty(&nbl->nbl_list)) {
5445 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005446 list_del_init(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -04005447 queue = true;
5448 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04005449 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005450
5451 if (queue)
5452 nfsd4_run_cb(&nbl->nbl_cb);
5453}
5454
Alexey Dobriyan7b021962009-09-21 17:01:12 -07005455static const struct lock_manager_operations nfsd_posix_mng_ops = {
Jeff Layton76d348f2016-09-16 16:28:24 -04005456 .lm_notify = nfsd4_lm_notify,
Kinglong Meeaef95832014-08-22 10:18:44 -04005457 .lm_get_owner = nfsd4_fl_get_owner,
5458 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07005459};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460
5461static inline void
5462nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5463{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005464 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465
NeilBrownd5b90262006-04-10 22:55:22 -07005466 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005467 lo = (struct nfs4_lockowner *) fl->fl_owner;
5468 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5469 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005470 if (!deny->ld_owner.data)
5471 /* We just don't care that much */
5472 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005473 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5474 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07005475 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005476nevermind:
5477 deny->ld_owner.len = 0;
5478 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07005479 deny->ld_clientid.cl_boot = 0;
5480 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 }
5482 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02005483 deny->ld_length = NFS4_MAX_UINT64;
5484 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5486 deny->ld_type = NFS4_READ_LT;
5487 if (fl->fl_type != F_RDLCK)
5488 deny->ld_type = NFS4_WRITE_LT;
5489}
5490
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005491static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005492find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493{
Trond Myklebustd4f04892014-07-29 21:34:36 -04005494 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005495 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496
Trond Myklebust0a880a22014-07-30 08:27:10 -04005497 lockdep_assert_held(&clp->cl_lock);
5498
Trond Myklebustd4f04892014-07-29 21:34:36 -04005499 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5500 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005501 if (so->so_is_open_owner)
5502 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005503 if (same_owner_str(so, owner))
5504 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 }
5506 return NULL;
5507}
5508
Trond Myklebustc58c6612014-07-29 21:34:35 -04005509static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005510find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04005511{
5512 struct nfs4_lockowner *lo;
5513
Trond Myklebustd4f04892014-07-29 21:34:36 -04005514 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005515 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005516 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005517 return lo;
5518}
5519
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005520static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5521{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005522 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005523}
5524
Jeff Layton6b180f02014-07-29 21:34:26 -04005525static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5526{
5527 struct nfs4_lockowner *lo = lockowner(sop);
5528
5529 kmem_cache_free(lockowner_slab, lo);
5530}
5531
5532static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005533 .so_unhash = nfs4_unhash_lockowner,
5534 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005535};
5536
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537/*
5538 * Alloc a lock owner structure.
5539 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005540 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005542 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005544static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005545alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5546 struct nfs4_ol_stateid *open_stp,
5547 struct nfsd4_lock *lock)
5548{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005549 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005551 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5552 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04005554 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005555 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5556 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005557 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005558 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005559 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005560 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005561 if (ret == NULL) {
5562 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005563 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005564 ret = lo;
5565 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08005566 nfs4_free_stateowner(&lo->lo_owner);
5567
Trond Myklebustd4f04892014-07-29 21:34:36 -04005568 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04005569 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570}
5571
Jeff Layton356a95e2014-07-29 21:34:13 -04005572static void
5573init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5574 struct nfs4_file *fp, struct inode *inode,
5575 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005577 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578
Jeff Layton356a95e2014-07-29 21:34:13 -04005579 lockdep_assert_held(&clp->cl_lock);
5580
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005581 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005582 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005583 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07005584 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005585 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005586 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005588 stp->st_openstp = open_stp;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005589 mutex_init(&stp->st_mutex);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005590 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005591 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005592 spin_lock(&fp->fi_lock);
5593 list_add(&stp->st_perfile, &fp->fi_stateids);
5594 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595}
5596
Jeff Laytonc53530d2014-06-30 11:48:39 -04005597static struct nfs4_ol_stateid *
5598find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5599{
5600 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005601 struct nfs4_client *clp = lo->lo_owner.so_client;
5602
5603 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005604
5605 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005606 if (lst->st_stid.sc_file == fp) {
5607 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005608 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005609 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005610 }
5611 return NULL;
5612}
5613
Jeff Layton356a95e2014-07-29 21:34:13 -04005614static struct nfs4_ol_stateid *
5615find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5616 struct inode *inode, struct nfs4_ol_stateid *ost,
5617 bool *new)
5618{
5619 struct nfs4_stid *ns = NULL;
5620 struct nfs4_ol_stateid *lst;
5621 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5622 struct nfs4_client *clp = oo->oo_owner.so_client;
5623
5624 spin_lock(&clp->cl_lock);
5625 lst = find_lock_stateid(lo, fi);
5626 if (lst == NULL) {
5627 spin_unlock(&clp->cl_lock);
Kinglong Meed19fb702017-01-18 19:04:42 +08005628 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
Jeff Layton356a95e2014-07-29 21:34:13 -04005629 if (ns == NULL)
5630 return NULL;
5631
5632 spin_lock(&clp->cl_lock);
5633 lst = find_lock_stateid(lo, fi);
5634 if (likely(!lst)) {
5635 lst = openlockstateid(ns);
5636 init_lock_stateid(lst, lo, fi, inode, ost);
5637 ns = NULL;
5638 *new = true;
5639 }
5640 }
5641 spin_unlock(&clp->cl_lock);
5642 if (ns)
5643 nfs4_put_stid(ns);
5644 return lst;
5645}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005646
NeilBrownfd39ca92005-06-23 22:04:03 -07005647static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648check_lock_length(u64 offset, u64 length)
5649{
Kinglong Meee7969312015-07-13 17:34:19 +08005650 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5651 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652}
5653
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005654static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005655{
Trond Myklebust11b91642014-07-29 21:34:08 -04005656 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005657
Jeff Layton7214e862014-07-10 14:07:33 -04005658 lockdep_assert_held(&fp->fi_lock);
5659
Jeff Layton82c5ff12012-05-11 09:45:13 -04005660 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005661 return;
Jeff Layton12659652014-07-10 14:07:28 -04005662 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005663 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005664}
5665
Jeff Layton356a95e2014-07-29 21:34:13 -04005666static __be32
5667lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5668 struct nfs4_ol_stateid *ost,
5669 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04005670 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005671{
Jeff Layton5db1c032014-07-29 21:34:28 -04005672 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005673 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005674 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5675 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00005676 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005677 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04005678 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005679 unsigned int strhashval;
Jeff Laytondd257932016-08-11 10:37:39 -04005680 bool hashed;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005681
Kinglong Meec8623992015-07-13 17:35:37 +08005682 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005683 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005684 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005685 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5686 if (lo == NULL)
5687 return nfserr_jukebox;
5688 } else {
5689 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005690 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005691 if (!cstate->minorversion &&
5692 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005693 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005694 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005695
Jeff Laytondd257932016-08-11 10:37:39 -04005696retry:
5697 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5698 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005699 status = nfserr_jukebox;
5700 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005701 }
Jeff Laytondd257932016-08-11 10:37:39 -04005702
5703 mutex_lock(&lst->st_mutex);
5704
5705 /* See if it's still hashed to avoid race with FREE_STATEID */
5706 spin_lock(&cl->cl_lock);
5707 hashed = !list_empty(&lst->st_perfile);
5708 spin_unlock(&cl->cl_lock);
5709
5710 if (!hashed) {
5711 mutex_unlock(&lst->st_mutex);
5712 nfs4_put_stid(&lst->st_stid);
5713 goto retry;
5714 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005715 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04005716 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04005717out:
5718 nfs4_put_stateowner(&lo->lo_owner);
5719 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005720}
5721
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722/*
5723 * LOCK operation
5724 */
Al Virob37ad282006-10-19 23:28:59 -07005725__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005726nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005727 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005729 struct nfsd4_lock *lock = &u->lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005730 struct nfs4_openowner *open_sop = NULL;
5731 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005732 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005733 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005734 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005735 struct file *filp = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04005736 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005737 struct file_lock *file_lock = NULL;
5738 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005739 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005740 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005741 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005742 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04005743 unsigned char fl_type;
5744 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005745 struct net *net = SVC_NET(rqstp);
5746 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747
5748 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5749 (long long) lock->lk_offset,
5750 (long long) lock->lk_length);
5751
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 if (check_lock_length(lock->lk_offset, lock->lk_length))
5753 return nfserr_inval;
5754
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005755 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005756 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005757 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5758 return status;
5759 }
5760
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005762 if (nfsd4_has_session(cstate))
5763 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005764 memcpy(&lock->lk_new_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04005765 &cstate->session->se_client->cl_clientid,
5766 sizeof(clientid_t));
5767
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005769 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005773 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 lock->lk_new_open_seqid,
5775 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005776 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005777 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005779 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005780 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005781 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005782 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005783 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005784 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005785 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005786 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005787 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005788 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005789 lock->lk_old_lock_seqid,
5790 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005791 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005792 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005793 if (status)
5794 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005795 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005797 lkflg = setlkflg(lock->lk_type);
5798 status = nfs4_check_openmode(lock_stp, lkflg);
5799 if (status)
5800 goto out;
5801
NeilBrown0dd395d2005-07-07 17:59:15 -07005802 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005803 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005804 goto out;
5805 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005806 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005807 goto out;
5808
Trond Myklebust11b91642014-07-29 21:34:08 -04005809 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 case NFS4_READW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04005812 if (nfsd4_has_session(cstate))
5813 fl_flags |= FL_SLEEP;
5814 /* Fallthrough */
5815 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005816 spin_lock(&fp->fi_lock);
5817 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005818 if (filp)
5819 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005820 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005821 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005822 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 case NFS4_WRITEW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04005824 if (nfsd4_has_session(cstate))
5825 fl_flags |= FL_SLEEP;
5826 /* Fallthrough */
5827 case NFS4_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005828 spin_lock(&fp->fi_lock);
5829 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005830 if (filp)
5831 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005832 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005833 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005834 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 default:
5836 status = nfserr_inval;
5837 goto out;
5838 }
Jeff Layton76d348f2016-09-16 16:28:24 -04005839
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005840 if (!filp) {
5841 status = nfserr_openmode;
5842 goto out;
5843 }
Kinglong Meeaef95832014-08-22 10:18:44 -04005844
Jeff Layton76d348f2016-09-16 16:28:24 -04005845 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
5846 if (!nbl) {
5847 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
5848 status = nfserr_jukebox;
5849 goto out;
5850 }
5851
5852 file_lock = &nbl->nbl_lock;
5853 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04005854 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04005855 file_lock->fl_pid = current->tgid;
5856 file_lock->fl_file = filp;
Jeff Layton76d348f2016-09-16 16:28:24 -04005857 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04005858 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5859 file_lock->fl_start = lock->lk_offset;
5860 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5861 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862
Jeff Layton21179d82012-08-21 08:03:32 -04005863 conflock = locks_alloc_lock();
5864 if (!conflock) {
5865 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5866 status = nfserr_jukebox;
5867 goto out;
5868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869
Jeff Layton76d348f2016-09-16 16:28:24 -04005870 if (fl_flags & FL_SLEEP) {
Jeff Layton7919d0a2016-09-16 16:28:25 -04005871 nbl->nbl_time = jiffies;
Jeff Layton0cc11a62016-10-20 09:34:31 -04005872 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005873 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005874 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04005875 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005876 }
5877
Jeff Layton21179d82012-08-21 08:03:32 -04005878 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005879 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04005881 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07005882 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005883 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04005884 case FILE_LOCK_DEFERRED:
5885 nbl = NULL;
5886 /* Fallthrough */
5887 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005888 status = nfserr_denied;
5889 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005890 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005891 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04005892 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005894 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005895 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005896 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005897 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005898 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900out:
Jeff Layton76d348f2016-09-16 16:28:24 -04005901 if (nbl) {
5902 /* dequeue it if we queued it before */
5903 if (fl_flags & FL_SLEEP) {
Jeff Layton0cc11a62016-10-20 09:34:31 -04005904 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005905 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005906 list_del_init(&nbl->nbl_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04005907 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005908 }
5909 free_blocked_lock(nbl);
5910 }
Trond Myklebustde186432014-07-10 14:07:26 -04005911 if (filp)
5912 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005913 if (lock_stp) {
5914 /* Bump seqid manually if the 4.0 replay owner is openowner */
5915 if (cstate->replay_owner &&
5916 cstate->replay_owner != &lock_sop->lo_owner &&
5917 seqid_mutating_err(ntohl(status)))
5918 lock_sop->lo_owner.so_seqid++;
5919
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005920 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005921
Jeff Layton5db1c032014-07-29 21:34:28 -04005922 /*
5923 * If this is a new, never-before-used stateid, and we are
5924 * returning an error, then just go ahead and release it.
5925 */
5926 if (status && new)
5927 release_lock_stateid(lock_stp);
5928
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005929 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005930 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005931 if (open_stp)
5932 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005933 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005934 if (conflock)
5935 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 return status;
5937}
5938
5939/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005940 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5941 * so we do a temporary open here just to get an open file to pass to
5942 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5943 * inode operation.)
5944 */
Al Viro04da6e92012-04-13 00:00:04 -04005945static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005946{
5947 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005948 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5949 if (!err) {
5950 err = nfserrno(vfs_test_lock(file, lock));
Christoph Hellwigfd891452015-04-28 15:41:16 +02005951 fput(file);
Al Viro04da6e92012-04-13 00:00:04 -04005952 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005953 return err;
5954}
5955
5956/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 * LOCKT operation
5958 */
Al Virob37ad282006-10-19 23:28:59 -07005959__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005960nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005961 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005963 struct nfsd4_lockt *lockt = &u->lockt;
Jeff Layton21179d82012-08-21 08:03:32 -04005964 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005965 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005966 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005967 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005969 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 return nfserr_grace;
5971
5972 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5973 return nfserr_inval;
5974
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005975 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005976 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005977 if (status)
5978 goto out;
5979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005981 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983
Jeff Layton21179d82012-08-21 08:03:32 -04005984 file_lock = locks_alloc_lock();
5985 if (!file_lock) {
5986 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5987 status = nfserr_jukebox;
5988 goto out;
5989 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005990
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 switch (lockt->lt_type) {
5992 case NFS4_READ_LT:
5993 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005994 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 break;
5996 case NFS4_WRITE_LT:
5997 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005998 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 break;
6000 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04006001 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 status = nfserr_inval;
6003 goto out;
6004 }
6005
Kinglong Meec8623992015-07-13 17:35:37 +08006006 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006007 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04006008 file_lock->fl_owner = (fl_owner_t)lo;
6009 file_lock->fl_pid = current->tgid;
6010 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011
Jeff Layton21179d82012-08-21 08:03:32 -04006012 file_lock->fl_start = lockt->lt_offset;
6013 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014
Jeff Layton21179d82012-08-21 08:03:32 -04006015 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016
Jeff Layton21179d82012-08-21 08:03:32 -04006017 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04006018 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05006019 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04006020
Jeff Layton21179d82012-08-21 08:03:32 -04006021 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04006023 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 }
6025out:
Jeff Layton5db1c032014-07-29 21:34:28 -04006026 if (lo)
6027 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04006028 if (file_lock)
6029 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030 return status;
6031}
6032
Al Virob37ad282006-10-19 23:28:59 -07006033__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006034nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006035 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006037 struct nfsd4_locku *locku = &u->locku;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006038 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006040 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006041 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07006042 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006043 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6044
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6046 (long long) locku->lu_offset,
6047 (long long) locku->lu_length);
6048
6049 if (check_lock_length(locku->lu_offset, locku->lu_length))
6050 return nfserr_inval;
6051
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006052 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006053 &locku->lu_stateid, NFS4_LOCK_STID,
6054 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006055 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04006057 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006058 if (!filp) {
6059 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04006060 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006061 }
Jeff Layton21179d82012-08-21 08:03:32 -04006062 file_lock = locks_alloc_lock();
6063 if (!file_lock) {
6064 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6065 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04006066 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04006067 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006068
Jeff Layton21179d82012-08-21 08:03:32 -04006069 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04006070 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04006071 file_lock->fl_pid = current->tgid;
6072 file_lock->fl_file = filp;
6073 file_lock->fl_flags = FL_POSIX;
6074 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6075 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076
Jeff Layton21179d82012-08-21 08:03:32 -04006077 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6078 locku->lu_length);
6079 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080
Jeff Layton21179d82012-08-21 08:03:32 -04006081 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07006082 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05006083 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 goto out_nfserr;
6085 }
Jeff Layton9767feb2015-10-01 09:05:50 -04006086 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Trond Myklebustde186432014-07-10 14:07:26 -04006087fput:
6088 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04006089put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006090 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04006091 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006093 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006094 if (file_lock)
6095 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 return status;
6097
6098out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07006099 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04006100 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101}
6102
6103/*
6104 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006105 * true: locks held by lockowner
6106 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006108static bool
6109check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006111 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006112 int status = false;
6113 struct file *filp = find_any_file(fp);
6114 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006115 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006116
6117 if (!filp) {
6118 /* Any valid lock stateid should have some sort of access */
6119 WARN_ON_ONCE(1);
6120 return status;
6121 }
6122
6123 inode = file_inode(filp);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006124 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006126 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05006127 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006128 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6129 if (fl->fl_owner == (fl_owner_t)lowner) {
6130 status = true;
6131 break;
6132 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08006133 }
Jeff Layton6109c852015-01-16 15:05:57 -05006134 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 }
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006136 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 return status;
6138}
6139
Al Virob37ad282006-10-19 23:28:59 -07006140__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08006141nfsd4_release_lockowner(struct svc_rqst *rqstp,
6142 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006143 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006145 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04006147 struct nfs4_stateowner *sop;
6148 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006149 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006151 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07006152 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006153 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006154 struct nfs4_client *clp;
Chuck Lever88584812016-07-13 16:40:14 -04006155 LIST_HEAD (reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156
6157 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6158 clid->cl_boot, clid->cl_id);
6159
Jeff Layton4b24ca72014-06-30 11:48:44 -04006160 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006161 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04006162 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006163
Trond Myklebustd4f04892014-07-29 21:34:36 -04006164 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04006165 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04006166 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04006167 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04006168 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04006169
6170 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006171 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04006172
Jeff Layton882e9d22014-07-29 21:34:37 -04006173 /* see if there are still any locks associated with it */
6174 lo = lockowner(sop);
6175 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6176 if (check_for_locks(stp->st_stid.sc_file, lo)) {
6177 status = nfserr_locks_held;
6178 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04006179 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04006180 }
Jeff Layton5adfd882014-07-29 21:34:31 -04006181 }
Jeff Layton882e9d22014-07-29 21:34:37 -04006182
Kinglong Meeb5971af2014-08-22 10:18:43 -04006183 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04006184 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04006185 }
Chuck Lever88584812016-07-13 16:40:14 -04006186 if (!lo) {
6187 spin_unlock(&clp->cl_lock);
6188 return status;
6189 }
6190
6191 unhash_lockowner_locked(lo);
6192 while (!list_empty(&lo->lo_owner.so_stateids)) {
6193 stp = list_first_entry(&lo->lo_owner.so_stateids,
6194 struct nfs4_ol_stateid,
6195 st_perstateowner);
6196 WARN_ON(!unhash_lock_stateid(stp));
6197 put_ol_stateid_locked(stp, &reaplist);
6198 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04006199 spin_unlock(&clp->cl_lock);
Chuck Lever88584812016-07-13 16:40:14 -04006200 free_ol_stateid_reaplist(&reaplist);
6201 nfs4_put_stateowner(&lo->lo_owner);
6202
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203 return status;
6204}
6205
6206static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07006207alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208{
NeilBrowna55370a2005-06-23 22:03:52 -07006209 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210}
6211
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006212bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006213nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07006214{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006215 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07006216
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006217 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006218 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07006219}
6220
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221/*
6222 * failure => all reset bets are off, nfserr_no_grace...
6223 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05006224struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006225nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226{
6227 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006228 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229
NeilBrowna55370a2005-06-23 22:03:52 -07006230 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
6231 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05006232 if (crp) {
6233 strhashval = clientstr_hashval(name);
6234 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006235 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05006236 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006237 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006238 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006239 }
6240 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241}
6242
Jeff Layton2a4317c2012-03-21 16:42:43 -04006243void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006244nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05006245{
6246 list_del(&crp->cr_strhash);
6247 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006248 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05006249}
6250
6251void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006252nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253{
6254 struct nfs4_client_reclaim *crp = NULL;
6255 int i;
6256
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006258 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6259 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006261 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 }
6263 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006264 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265}
6266
6267/*
6268 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04006269struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006270nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271{
6272 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 struct nfs4_client_reclaim *crp = NULL;
6274
Jeff Layton278c9312012-11-12 15:00:52 -05006275 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276
Jeff Layton278c9312012-11-12 15:00:52 -05006277 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006278 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05006279 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 return crp;
6281 }
6282 }
6283 return NULL;
6284}
6285
6286/*
6287* Called from OPEN. Look for clientid in reclaim list.
6288*/
Al Virob37ad282006-10-19 23:28:59 -07006289__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006290nfs4_check_open_reclaim(clientid_t *clid,
6291 struct nfsd4_compound_state *cstate,
6292 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006294 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04006295
6296 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006297 status = lookup_clientid(clid, cstate, nn);
6298 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04006299 return nfserr_reclaim_bad;
6300
Jeff Layton3b3e7b72014-09-12 16:40:22 -04006301 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6302 return nfserr_no_grace;
6303
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006304 if (nfsd4_client_record_check(cstate->clp))
6305 return nfserr_reclaim_bad;
6306
6307 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308}
6309
Bryan Schumaker65178db2011-11-01 13:35:21 -04006310#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04006311static inline void
6312put_client(struct nfs4_client *clp)
6313{
6314 atomic_dec(&clp->cl_refcount);
6315}
6316
Jeff Layton285abde2014-07-30 08:27:24 -04006317static struct nfs4_client *
6318nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6319{
6320 struct nfs4_client *clp;
6321 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6322 nfsd_net_id);
6323
6324 if (!nfsd_netns_ready(nn))
6325 return NULL;
6326
6327 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6328 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
6329 return clp;
6330 }
6331 return NULL;
6332}
6333
Jeff Layton7ec0e362014-07-30 08:27:17 -04006334u64
Jeff Layton285abde2014-07-30 08:27:24 -04006335nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04006336{
6337 struct nfs4_client *clp;
6338 u64 count = 0;
6339 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6340 nfsd_net_id);
6341 char buf[INET6_ADDRSTRLEN];
6342
6343 if (!nfsd_netns_ready(nn))
6344 return 0;
6345
6346 spin_lock(&nn->client_lock);
6347 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6348 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6349 pr_info("NFS Client: %s\n", buf);
6350 ++count;
6351 }
6352 spin_unlock(&nn->client_lock);
6353
6354 return count;
6355}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006356
Jeff Laytona0926d12014-07-30 08:27:18 -04006357u64
Jeff Layton285abde2014-07-30 08:27:24 -04006358nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04006359{
6360 u64 count = 0;
6361 struct nfs4_client *clp;
6362 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6363 nfsd_net_id);
6364
6365 if (!nfsd_netns_ready(nn))
6366 return count;
6367
6368 spin_lock(&nn->client_lock);
6369 clp = nfsd_find_client(addr, addr_size);
6370 if (clp) {
6371 if (mark_client_expired_locked(clp) == nfs_ok)
6372 ++count;
6373 else
6374 clp = NULL;
6375 }
6376 spin_unlock(&nn->client_lock);
6377
6378 if (clp)
6379 expire_client(clp);
6380
6381 return count;
6382}
6383
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006384u64
Jeff Layton285abde2014-07-30 08:27:24 -04006385nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006386{
6387 u64 count = 0;
6388 struct nfs4_client *clp, *next;
6389 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6390 nfsd_net_id);
6391 LIST_HEAD(reaplist);
6392
6393 if (!nfsd_netns_ready(nn))
6394 return count;
6395
6396 spin_lock(&nn->client_lock);
6397 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6398 if (mark_client_expired_locked(clp) == nfs_ok) {
6399 list_add(&clp->cl_lru, &reaplist);
6400 if (max != 0 && ++count >= max)
6401 break;
6402 }
6403 }
6404 spin_unlock(&nn->client_lock);
6405
6406 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6407 expire_client(clp);
6408
6409 return count;
6410}
6411
Bryan Schumaker184c1842012-11-29 11:40:44 -05006412static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6413 const char *type)
6414{
6415 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05006416 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05006417 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6418}
6419
Jeff Layton016200c2014-07-30 08:27:21 -04006420static void
6421nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6422 struct list_head *collect)
6423{
6424 struct nfs4_client *clp = lst->st_stid.sc_client;
6425 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6426 nfsd_net_id);
6427
6428 if (!collect)
6429 return;
6430
6431 lockdep_assert_held(&nn->client_lock);
6432 atomic_inc(&clp->cl_refcount);
6433 list_add(&lst->st_locks, collect);
6434}
6435
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006436static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04006437 struct list_head *collect,
Jeff Laytone8568732015-08-24 12:41:47 -04006438 bool (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05006439{
6440 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006441 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006442 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006443 u64 count = 0;
6444
Jeff Layton016200c2014-07-30 08:27:21 -04006445 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006446 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006447 list_for_each_entry_safe(stp, st_next,
6448 &oop->oo_owner.so_stateids, st_perstateowner) {
6449 list_for_each_entry_safe(lst, lst_next,
6450 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04006451 if (func) {
Jeff Laytone8568732015-08-24 12:41:47 -04006452 if (func(lst))
6453 nfsd_inject_add_lock_to_list(lst,
6454 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04006455 }
Jeff Layton016200c2014-07-30 08:27:21 -04006456 ++count;
6457 /*
6458 * Despite the fact that these functions deal
6459 * with 64-bit integers for "count", we must
6460 * ensure that it doesn't blow up the
6461 * clp->cl_refcount. Throw a warning if we
6462 * start to approach INT_MAX here.
6463 */
6464 WARN_ON_ONCE(count == (INT_MAX / 2));
6465 if (count == max)
6466 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006467 }
6468 }
6469 }
Jeff Layton016200c2014-07-30 08:27:21 -04006470out:
6471 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006472
6473 return count;
6474}
6475
Jeff Layton016200c2014-07-30 08:27:21 -04006476static u64
6477nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6478 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05006479{
Jeff Layton016200c2014-07-30 08:27:21 -04006480 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006481}
6482
Jeff Layton016200c2014-07-30 08:27:21 -04006483static u64
6484nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006485{
Jeff Layton016200c2014-07-30 08:27:21 -04006486 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006487 nfsd_print_count(clp, count, "locked files");
6488 return count;
6489}
6490
Jeff Layton016200c2014-07-30 08:27:21 -04006491u64
Jeff Layton285abde2014-07-30 08:27:24 -04006492nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04006493{
6494 struct nfs4_client *clp;
6495 u64 count = 0;
6496 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6497 nfsd_net_id);
6498
6499 if (!nfsd_netns_ready(nn))
6500 return 0;
6501
6502 spin_lock(&nn->client_lock);
6503 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6504 count += nfsd_print_client_locks(clp);
6505 spin_unlock(&nn->client_lock);
6506
6507 return count;
6508}
6509
6510static void
6511nfsd_reap_locks(struct list_head *reaplist)
6512{
6513 struct nfs4_client *clp;
6514 struct nfs4_ol_stateid *stp, *next;
6515
6516 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6517 list_del_init(&stp->st_locks);
6518 clp = stp->st_stid.sc_client;
6519 nfs4_put_stid(&stp->st_stid);
6520 put_client(clp);
6521 }
6522}
6523
6524u64
Jeff Layton285abde2014-07-30 08:27:24 -04006525nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04006526{
6527 unsigned int count = 0;
6528 struct nfs4_client *clp;
6529 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6530 nfsd_net_id);
6531 LIST_HEAD(reaplist);
6532
6533 if (!nfsd_netns_ready(nn))
6534 return count;
6535
6536 spin_lock(&nn->client_lock);
6537 clp = nfsd_find_client(addr, addr_size);
6538 if (clp)
6539 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6540 spin_unlock(&nn->client_lock);
6541 nfsd_reap_locks(&reaplist);
6542 return count;
6543}
6544
6545u64
Jeff Layton285abde2014-07-30 08:27:24 -04006546nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04006547{
6548 u64 count = 0;
6549 struct nfs4_client *clp;
6550 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6551 nfsd_net_id);
6552 LIST_HEAD(reaplist);
6553
6554 if (!nfsd_netns_ready(nn))
6555 return count;
6556
6557 spin_lock(&nn->client_lock);
6558 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6559 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6560 if (max != 0 && count >= max)
6561 break;
6562 }
6563 spin_unlock(&nn->client_lock);
6564 nfsd_reap_locks(&reaplist);
6565 return count;
6566}
6567
Jeff Layton82e05ef2014-07-30 08:27:22 -04006568static u64
6569nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6570 struct list_head *collect,
6571 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006572{
6573 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04006574 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6575 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006576 u64 count = 0;
6577
Jeff Layton82e05ef2014-07-30 08:27:22 -04006578 lockdep_assert_held(&nn->client_lock);
6579
6580 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006581 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04006582 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006583 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04006584 if (collect) {
6585 atomic_inc(&clp->cl_refcount);
6586 list_add(&oop->oo_perclient, collect);
6587 }
6588 }
6589 ++count;
6590 /*
6591 * Despite the fact that these functions deal with
6592 * 64-bit integers for "count", we must ensure that
6593 * it doesn't blow up the clp->cl_refcount. Throw a
6594 * warning if we start to approach INT_MAX here.
6595 */
6596 WARN_ON_ONCE(count == (INT_MAX / 2));
6597 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006598 break;
6599 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04006600 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006601
6602 return count;
6603}
6604
Jeff Layton82e05ef2014-07-30 08:27:22 -04006605static u64
6606nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006607{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006608 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6609
6610 nfsd_print_count(clp, count, "openowners");
6611 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006612}
6613
Jeff Layton82e05ef2014-07-30 08:27:22 -04006614static u64
6615nfsd_collect_client_openowners(struct nfs4_client *clp,
6616 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006617{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006618 return nfsd_foreach_client_openowner(clp, max, collect,
6619 unhash_openowner_locked);
6620}
6621
6622u64
Jeff Layton285abde2014-07-30 08:27:24 -04006623nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006624{
6625 struct nfs4_client *clp;
6626 u64 count = 0;
6627 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6628 nfsd_net_id);
6629
6630 if (!nfsd_netns_ready(nn))
6631 return 0;
6632
6633 spin_lock(&nn->client_lock);
6634 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6635 count += nfsd_print_client_openowners(clp);
6636 spin_unlock(&nn->client_lock);
6637
6638 return count;
6639}
6640
6641static void
6642nfsd_reap_openowners(struct list_head *reaplist)
6643{
6644 struct nfs4_client *clp;
6645 struct nfs4_openowner *oop, *next;
6646
6647 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6648 list_del_init(&oop->oo_perclient);
6649 clp = oop->oo_owner.so_client;
6650 release_openowner(oop);
6651 put_client(clp);
6652 }
6653}
6654
6655u64
Jeff Layton285abde2014-07-30 08:27:24 -04006656nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6657 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006658{
6659 unsigned int count = 0;
6660 struct nfs4_client *clp;
6661 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6662 nfsd_net_id);
6663 LIST_HEAD(reaplist);
6664
6665 if (!nfsd_netns_ready(nn))
6666 return count;
6667
6668 spin_lock(&nn->client_lock);
6669 clp = nfsd_find_client(addr, addr_size);
6670 if (clp)
6671 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6672 spin_unlock(&nn->client_lock);
6673 nfsd_reap_openowners(&reaplist);
6674 return count;
6675}
6676
6677u64
Jeff Layton285abde2014-07-30 08:27:24 -04006678nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006679{
6680 u64 count = 0;
6681 struct nfs4_client *clp;
6682 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6683 nfsd_net_id);
6684 LIST_HEAD(reaplist);
6685
6686 if (!nfsd_netns_ready(nn))
6687 return count;
6688
6689 spin_lock(&nn->client_lock);
6690 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6691 count += nfsd_collect_client_openowners(clp, &reaplist,
6692 max - count);
6693 if (max != 0 && count >= max)
6694 break;
6695 }
6696 spin_unlock(&nn->client_lock);
6697 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006698 return count;
6699}
6700
Bryan Schumaker269de302012-11-29 11:40:42 -05006701static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6702 struct list_head *victims)
6703{
6704 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006705 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6706 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006707 u64 count = 0;
6708
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006709 lockdep_assert_held(&nn->client_lock);
6710
6711 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006712 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006713 if (victims) {
6714 /*
6715 * It's not safe to mess with delegations that have a
6716 * non-zero dl_time. They might have already been broken
6717 * and could be processed by the laundromat outside of
6718 * the state_lock. Just leave them be.
6719 */
6720 if (dp->dl_time != 0)
6721 continue;
6722
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006723 atomic_inc(&clp->cl_refcount);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04006724 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04006725 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006726 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006727 ++count;
6728 /*
6729 * Despite the fact that these functions deal with
6730 * 64-bit integers for "count", we must ensure that
6731 * it doesn't blow up the clp->cl_refcount. Throw a
6732 * warning if we start to approach INT_MAX here.
6733 */
6734 WARN_ON_ONCE(count == (INT_MAX / 2));
6735 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006736 break;
6737 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006738 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006739 return count;
6740}
6741
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006742static u64
6743nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006744{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006745 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006746
6747 nfsd_print_count(clp, count, "delegations");
6748 return count;
6749}
6750
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006751u64
Jeff Layton285abde2014-07-30 08:27:24 -04006752nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006753{
6754 struct nfs4_client *clp;
6755 u64 count = 0;
6756 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6757 nfsd_net_id);
6758
6759 if (!nfsd_netns_ready(nn))
6760 return 0;
6761
6762 spin_lock(&nn->client_lock);
6763 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6764 count += nfsd_print_client_delegations(clp);
6765 spin_unlock(&nn->client_lock);
6766
6767 return count;
6768}
6769
6770static void
6771nfsd_forget_delegations(struct list_head *reaplist)
6772{
6773 struct nfs4_client *clp;
6774 struct nfs4_delegation *dp, *next;
6775
6776 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6777 list_del_init(&dp->dl_recall_lru);
6778 clp = dp->dl_stid.sc_client;
6779 revoke_delegation(dp);
6780 put_client(clp);
6781 }
6782}
6783
6784u64
Jeff Layton285abde2014-07-30 08:27:24 -04006785nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6786 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006787{
6788 u64 count = 0;
6789 struct nfs4_client *clp;
6790 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6791 nfsd_net_id);
6792 LIST_HEAD(reaplist);
6793
6794 if (!nfsd_netns_ready(nn))
6795 return count;
6796
6797 spin_lock(&nn->client_lock);
6798 clp = nfsd_find_client(addr, addr_size);
6799 if (clp)
6800 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6801 spin_unlock(&nn->client_lock);
6802
6803 nfsd_forget_delegations(&reaplist);
6804 return count;
6805}
6806
6807u64
Jeff Layton285abde2014-07-30 08:27:24 -04006808nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006809{
6810 u64 count = 0;
6811 struct nfs4_client *clp;
6812 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6813 nfsd_net_id);
6814 LIST_HEAD(reaplist);
6815
6816 if (!nfsd_netns_ready(nn))
6817 return count;
6818
6819 spin_lock(&nn->client_lock);
6820 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6821 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6822 if (max != 0 && count >= max)
6823 break;
6824 }
6825 spin_unlock(&nn->client_lock);
6826 nfsd_forget_delegations(&reaplist);
6827 return count;
6828}
6829
6830static void
6831nfsd_recall_delegations(struct list_head *reaplist)
6832{
6833 struct nfs4_client *clp;
6834 struct nfs4_delegation *dp, *next;
6835
6836 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6837 list_del_init(&dp->dl_recall_lru);
6838 clp = dp->dl_stid.sc_client;
6839 /*
6840 * We skipped all entries that had a zero dl_time before,
6841 * so we can now reset the dl_time back to 0. If a delegation
6842 * break comes in now, then it won't make any difference since
6843 * we're recalling it either way.
6844 */
6845 spin_lock(&state_lock);
6846 dp->dl_time = 0;
6847 spin_unlock(&state_lock);
6848 nfsd_break_one_deleg(dp);
6849 put_client(clp);
6850 }
6851}
6852
6853u64
Jeff Layton285abde2014-07-30 08:27:24 -04006854nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006855 size_t addr_size)
6856{
6857 u64 count = 0;
6858 struct nfs4_client *clp;
6859 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6860 nfsd_net_id);
6861 LIST_HEAD(reaplist);
6862
6863 if (!nfsd_netns_ready(nn))
6864 return count;
6865
6866 spin_lock(&nn->client_lock);
6867 clp = nfsd_find_client(addr, addr_size);
6868 if (clp)
6869 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6870 spin_unlock(&nn->client_lock);
6871
6872 nfsd_recall_delegations(&reaplist);
6873 return count;
6874}
6875
6876u64
Jeff Layton285abde2014-07-30 08:27:24 -04006877nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006878{
6879 u64 count = 0;
6880 struct nfs4_client *clp, *next;
6881 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6882 nfsd_net_id);
6883 LIST_HEAD(reaplist);
6884
6885 if (!nfsd_netns_ready(nn))
6886 return count;
6887
6888 spin_lock(&nn->client_lock);
6889 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6890 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6891 if (max != 0 && ++count >= max)
6892 break;
6893 }
6894 spin_unlock(&nn->client_lock);
6895 nfsd_recall_delegations(&reaplist);
6896 return count;
6897}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006898#endif /* CONFIG_NFSD_FAULT_INJECTION */
6899
Meelap Shahc2f1a552007-07-17 04:04:39 -07006900/*
6901 * Since the lifetime of a delegation isn't limited to that of an open, a
6902 * client may quite reasonably hang on to a delegation as long as it has
6903 * the inode cached. This becomes an obvious problem the first time a
6904 * client's inode cache approaches the size of the server's total memory.
6905 *
6906 * For now we avoid this problem by imposing a hard limit on the number
6907 * of delegations, which varies according to the server's memory size.
6908 */
6909static void
6910set_max_delegations(void)
6911{
6912 /*
6913 * Allow at most 4 delegations per megabyte of RAM. Quick
6914 * estimates suggest that in the worst case (where every delegation
6915 * is for a different inode), a delegation could take about 1.5K,
6916 * giving a worst case usage of about 6% of memory.
6917 */
6918 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6919}
6920
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006921static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006922{
6923 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6924 int i;
6925
6926 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6927 CLIENT_HASH_SIZE, GFP_KERNEL);
6928 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006929 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006930 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6931 CLIENT_HASH_SIZE, GFP_KERNEL);
6932 if (!nn->unconf_id_hashtbl)
6933 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006934 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6935 SESSION_HASH_SIZE, GFP_KERNEL);
6936 if (!nn->sessionid_hashtbl)
6937 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006938
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006939 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006940 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006941 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006942 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006943 for (i = 0; i < SESSION_HASH_SIZE; i++)
6944 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006945 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006946 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03006947 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03006948 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006949 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03006950 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006951
Jeff Layton0cc11a62016-10-20 09:34:31 -04006952 spin_lock_init(&nn->blocked_locks_lock);
6953 INIT_LIST_HEAD(&nn->blocked_locks_lru);
6954
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006955 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006956 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006957
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006958 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006959
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006960err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03006961 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006962err_unconf_id:
6963 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006964err:
6965 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006966}
6967
6968static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006969nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006970{
6971 int i;
6972 struct nfs4_client *clp = NULL;
6973 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6974
6975 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6976 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6977 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6978 destroy_client(clp);
6979 }
6980 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006981
Kinglong Mee2b905632014-03-26 22:09:30 +08006982 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6983 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6984 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6985 destroy_client(clp);
6986 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006987 }
6988
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006989 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006990 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006991 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006992 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006993}
6994
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006995int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006996nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006997{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006998 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006999 int ret;
7000
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007001 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007002 if (ret)
7003 return ret;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04007004 nn->boot_time = get_seconds();
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04007005 nn->grace_ended = false;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04007006 nn->nfsd4_manager.block_opens = true;
Jeff Laytond4318ac2014-09-12 16:40:21 -04007007 locks_start_grace(net, &nn->nfsd4_manager);
7008 nfsd4_client_tracking_init(net);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007009 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03007010 nn->nfsd4_grace, net);
7011 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007012 return 0;
7013}
7014
7015/* initialization to perform when the nfsd service is started: */
7016
7017int
7018nfs4_state_start(void)
7019{
7020 int ret;
7021
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007022 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007023 if (ret)
Kinglong Meef7d1ddb2017-02-05 09:57:07 +08007024 return ret;
7025
Jeff Layton51a54562015-08-20 07:17:01 -04007026 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05007027 if (laundry_wq == NULL) {
7028 ret = -ENOMEM;
Kinglong Meef7d1ddb2017-02-05 09:57:07 +08007029 goto out_cleanup_cred;
Jeff Laytona6d6b782012-03-05 11:42:36 -05007030 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007031 ret = nfsd4_create_callback_queue();
7032 if (ret)
7033 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007034
Meelap Shahc2f1a552007-07-17 04:04:39 -07007035 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007036 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007037
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007038out_free_laundry:
7039 destroy_workqueue(laundry_wq);
Kinglong Meef7d1ddb2017-02-05 09:57:07 +08007040out_cleanup_cred:
7041 cleanup_callback_cred();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007042 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043}
7044
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007045void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007046nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007050 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Jeff Layton7919d0a2016-09-16 16:28:25 -04007051 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007053 cancel_delayed_work_sync(&nn->laundromat_work);
7054 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05007055
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04007057 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007058 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007060 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007061 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007062 }
Benny Halevycdc975052014-05-30 09:09:30 -04007063 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064 list_for_each_safe(pos, next, &reaplist) {
7065 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04007066 list_del_init(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +02007067 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -04007068 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04007069 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070 }
7071
Jeff Layton7919d0a2016-09-16 16:28:25 -04007072 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04007073 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04007074 while (!list_empty(&nn->blocked_locks_lru)) {
7075 nbl = list_first_entry(&nn->blocked_locks_lru,
7076 struct nfsd4_blocked_lock, nbl_lru);
7077 list_move(&nbl->nbl_lru, &reaplist);
7078 list_del_init(&nbl->nbl_list);
7079 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04007080 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04007081
7082 while (!list_empty(&reaplist)) {
7083 nbl = list_first_entry(&nn->blocked_locks_lru,
7084 struct nfsd4_blocked_lock, nbl_lru);
7085 list_del_init(&nbl->nbl_lru);
7086 posix_unblock_lock(&nbl->nbl_lock);
7087 free_blocked_lock(nbl);
7088 }
7089
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007090 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007091 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092}
7093
7094void
7095nfs4_state_shutdown(void)
7096{
NeilBrown5e8d5c22006-04-10 22:55:37 -07007097 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04007098 nfsd4_destroy_callback_queue();
Kinglong Meef7d1ddb2017-02-05 09:57:07 +08007099 cleanup_callback_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007101
7102static void
7103get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7104{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007105 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
7106 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007107}
7108
7109static void
7110put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7111{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007112 if (cstate->minorversion) {
7113 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7114 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7115 }
7116}
7117
7118void
7119clear_current_stateid(struct nfsd4_compound_state *cstate)
7120{
7121 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007122}
7123
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007124/*
7125 * functions to set current state id
7126 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007127void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007128nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7129 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007130{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007131 put_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007132}
7133
7134void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007135nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7136 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007137{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007138 put_stateid(cstate, &u->open.op_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007139}
7140
7141void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007142nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7143 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007144{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007145 put_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007146}
7147
7148void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007149nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7150 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007151{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007152 put_stateid(cstate, &u->lock.lk_resp_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007153}
7154
7155/*
7156 * functions to consume current state id
7157 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007158
7159void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007160nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7161 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007162{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007163 get_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007164}
7165
7166void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007167nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7168 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007169{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007170 get_stateid(cstate, &u->delegreturn.dr_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007171}
7172
7173void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007174nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7175 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007176{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007177 get_stateid(cstate, &u->free_stateid.fr_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007178}
7179
7180void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007181nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7182 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007183{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007184 get_stateid(cstate, &u->setattr.sa_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007185}
7186
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007187void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007188nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7189 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007190{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007191 get_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007192}
7193
7194void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007195nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7196 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007197{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007198 get_stateid(cstate, &u->locku.lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007199}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007200
7201void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007202nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7203 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007204{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007205 get_stateid(cstate, &u->read.rd_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007206}
7207
7208void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007209nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7210 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007211{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007212 get_stateid(cstate, &u->write.wr_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007213}