blob: f9552357923e8785cef0b8ac5b0626222ba6d8b9 [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
Andy Adamson069b6ad2009-04-03 08:27:58 +03002405nfsd4_exchange_id(struct svc_rqst *rqstp,
2406 struct nfsd4_compound_state *cstate,
2407 struct nfsd4_exchange_id *exid)
2408{
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,
2701 struct nfsd4_compound_state *cstate,
2702 struct nfsd4_create_session *cr_ses)
2703{
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
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002827__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2828{
2829 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002830 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002831 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002832
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002833 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2834 if (status)
2835 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002836 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002837 session->se_cb_prog = bc->bc_cb_program;
2838 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002839 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002840
2841 nfsd4_probe_callback(session->se_client);
2842
2843 return nfs_ok;
2844}
2845
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002846__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2847 struct nfsd4_compound_state *cstate,
2848 struct nfsd4_bind_conn_to_session *bcts)
2849{
2850 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002851 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002852 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002853 struct net *net = SVC_NET(rqstp);
2854 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002855
2856 if (!nfsd4_last_compound_op(rqstp))
2857 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002858 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002859 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002860 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002861 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002862 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002863 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002864 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002865 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002866 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002867 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002868 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002869 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002870 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002871 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002872 goto out;
2873 nfsd4_init_conn(rqstp, conn, session);
2874 status = nfs_ok;
2875out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002876 nfsd4_put_session(session);
2877out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002878 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002879}
2880
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002881static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2882{
2883 if (!session)
2884 return 0;
2885 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2886}
2887
Andy Adamson069b6ad2009-04-03 08:27:58 +03002888__be32
2889nfsd4_destroy_session(struct svc_rqst *r,
2890 struct nfsd4_compound_state *cstate,
2891 struct nfsd4_destroy_session *sessionid)
2892{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002893 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002894 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002895 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002896 struct net *net = SVC_NET(r);
2897 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002898
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002899 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002900 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002901 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002902 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002903 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002904 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002905 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002906 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002907 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002908 if (!ses)
2909 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002910 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002911 if (!nfsd4_mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002912 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002913 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002914 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002915 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002916 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002917 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002918
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002919 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002920
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002921 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002922 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002923out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002924 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002925out_client_lock:
2926 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002927out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03002928 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002929}
2930
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002931static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002932{
2933 struct nfsd4_conn *c;
2934
2935 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002936 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002937 return c;
2938 }
2939 }
2940 return NULL;
2941}
2942
J. Bruce Fields57266a62013-04-13 14:27:29 -04002943static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002944{
2945 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002946 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002947 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002948 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002949
2950 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002951 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002952 if (c)
2953 goto out_free;
2954 status = nfserr_conn_not_bound_to_session;
2955 if (clp->cl_mach_cred)
2956 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002957 __nfsd4_hash_conn(new, ses);
2958 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002959 ret = nfsd4_register_conn(new);
2960 if (ret)
2961 /* oops; xprt is already down: */
2962 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002963 return nfs_ok;
2964out_free:
2965 spin_unlock(&clp->cl_lock);
2966 free_conn(new);
2967 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002968}
2969
Mi Jinlong868b89c2011-04-27 09:09:58 +08002970static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2971{
2972 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2973
2974 return args->opcnt > session->se_fchannel.maxops;
2975}
2976
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002977static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2978 struct nfsd4_session *session)
2979{
2980 struct xdr_buf *xb = &rqstp->rq_arg;
2981
2982 return xb->len > session->se_fchannel.maxreq_sz;
2983}
2984
Andy Adamson069b6ad2009-04-03 08:27:58 +03002985__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002986nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002987 struct nfsd4_compound_state *cstate,
2988 struct nfsd4_sequence *seq)
2989{
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002990 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002991 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002992 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002993 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002994 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002995 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002996 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002997 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002998 struct net *net = SVC_NET(rqstp);
2999 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003000
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003001 if (resp->opcnt != 1)
3002 return nfserr_sequence_pos;
3003
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003004 /*
3005 * Will be either used or freed by nfsd4_sequence_check_conn
3006 * below.
3007 */
3008 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3009 if (!conn)
3010 return nfserr_jukebox;
3011
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003012 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003013 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003014 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003015 goto out_no_session;
3016 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003017
Mi Jinlong868b89c2011-04-27 09:09:58 +08003018 status = nfserr_too_many_ops;
3019 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003020 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003021
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003022 status = nfserr_req_too_big;
3023 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003024 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003025
Benny Halevyb85d4c02009-04-03 08:28:08 +03003026 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003027 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003028 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003029
Andy Adamson557ce262009-08-28 08:45:04 -04003030 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003031 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3032
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003033 /* We do not negotiate the number of slots yet, so set the
3034 * maxslots to the session maxreqs which is used to encode
3035 * sr_highest_slotid and the sr_target_slot id to maxslots */
3036 seq->maxslots = session->se_fchannel.maxreqs;
3037
J. Bruce Fields73e79482012-02-13 16:39:00 -05003038 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3039 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003040 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003041 status = nfserr_seq_misordered;
3042 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003043 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003044 cstate->slot = slot;
3045 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003046 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003047 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003048 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003049 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003050 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003051 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003052 }
3053 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003054 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003055
J. Bruce Fields57266a62013-04-13 14:27:29 -04003056 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003057 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003058 if (status)
3059 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003060
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003061 buflen = (seq->cachethis) ?
3062 session->se_fchannel.maxresp_cached :
3063 session->se_fchannel.maxresp_sz;
3064 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3065 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003066 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003067 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003068 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003069
3070 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003071 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003072 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003073 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003074 if (seq->cachethis)
3075 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003076 else
3077 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003078
3079 cstate->slot = slot;
3080 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003081 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003082
Benny Halevyb85d4c02009-04-03 08:28:08 +03003083out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003084 switch (clp->cl_cb_state) {
3085 case NFSD4_CB_DOWN:
3086 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3087 break;
3088 case NFSD4_CB_FAULT:
3089 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3090 break;
3091 default:
3092 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003093 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003094 if (!list_empty(&clp->cl_revoked))
3095 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003096out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08003097 if (conn)
3098 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003099 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003100 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003101out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003102 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003103 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003104}
3105
Trond Myklebustb6076642014-06-30 11:48:35 -04003106void
3107nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3108{
3109 struct nfsd4_compound_state *cs = &resp->cstate;
3110
3111 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04003112 if (cs->status != nfserr_replay_cache) {
3113 nfsd4_store_cache_entry(resp);
3114 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3115 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003116 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04003117 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003118 } else if (cs->clp)
3119 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04003120}
3121
Mi Jinlong345c2842011-10-20 17:51:39 +08003122__be32
3123nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
3124{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003125 struct nfs4_client *conf, *unconf;
3126 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003127 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003128 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08003129
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003130 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003131 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003132 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003133 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08003134
3135 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04003136 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08003137 status = nfserr_clientid_busy;
3138 goto out;
3139 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04003140 status = mark_client_expired_locked(conf);
3141 if (status)
3142 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003143 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08003144 } else if (unconf)
3145 clp = unconf;
3146 else {
3147 status = nfserr_stale_clientid;
3148 goto out;
3149 }
Andrew Elblededeb132016-06-15 12:52:08 -04003150 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003151 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003152 status = nfserr_wrong_cred;
3153 goto out;
3154 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003155 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003156out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003157 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003158 if (clp)
3159 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003160 return status;
3161}
3162
Andy Adamson069b6ad2009-04-03 08:27:58 +03003163__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003164nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
3165{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003166 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003167
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003168 if (rc->rca_one_fs) {
3169 if (!cstate->current_fh.fh_dentry)
3170 return nfserr_nofilehandle;
3171 /*
3172 * We don't take advantage of the rca_one_fs case.
3173 * That's OK, it's optional, we can safely ignore it.
3174 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02003175 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003176 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003177
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003178 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04003179 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3180 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003181 goto out;
3182
3183 status = nfserr_stale_clientid;
3184 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003185 /*
3186 * The following error isn't really legal.
3187 * But we only get here if the client just explicitly
3188 * destroyed the client. Surely it no longer cares what
3189 * error it gets back on an operation for the dead
3190 * client.
3191 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003192 goto out;
3193
3194 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04003195 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003196out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003197 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003198}
3199
3200__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003201nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3202 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003204 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003206 struct nfs4_client *conf, *new;
3207 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003208 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003209 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3210
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003211 new = create_client(clname, rqstp, &clverifier);
3212 if (new == NULL)
3213 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003214 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003215 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003216 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04003217 if (conf && client_has_state(conf)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003218 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003220 if (clp_used_exchangeid(conf))
3221 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003222 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04003223 char addr_str[INET6_ADDRSTRLEN];
3224 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3225 sizeof(addr_str));
3226 dprintk("NFSD: setclientid: string in use by client "
3227 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 goto out;
3229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003231 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003232 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003233 unhash_client_locked(unconf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003234 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003235 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003237 gen_confirm(new, nn);
3238 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003239 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003240 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003241 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003242 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3244 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3245 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003246 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 status = nfs_ok;
3248out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003249 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003250 if (new)
3251 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003252 if (unconf)
3253 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 return status;
3255}
3256
3257
Al Virob37ad282006-10-19 23:28:59 -07003258__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003259nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3260 struct nfsd4_compound_state *cstate,
3261 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262{
NeilBrown21ab45a2005-06-23 22:04:14 -07003263 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003264 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3266 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003267 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003268 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003270 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003272
Jeff Laytond20c11d2014-07-30 08:27:07 -04003273 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003274 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003275 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003276 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003277 * We try hard to give out unique clientid's, so if we get an
3278 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003279 * the client may be buggy; this should never happen.
3280 *
3281 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003282 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003283 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04003284 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3285 goto out;
3286 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3287 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003288 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003289 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003290 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3291 /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 status = nfs_ok;
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003293 } else /* case 4: client hasn't noticed we rebooted yet? */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003294 status = nfserr_stale_clientid;
3295 goto out;
3296 }
3297 status = nfs_ok;
3298 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003299 old = unconf;
3300 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003301 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003302 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003303 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3304 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04003305 status = nfserr_clid_inuse;
3306 if (client_has_state(old)
3307 && !same_creds(&unconf->cl_cred,
3308 &old->cl_cred))
3309 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003310 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003311 if (status) {
3312 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003313 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003314 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003315 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003316 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003317 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003318 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003319 get_client_locked(conf);
3320 spin_unlock(&nn->client_lock);
3321 nfsd4_probe_callback(conf);
3322 spin_lock(&nn->client_lock);
3323 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003325 spin_unlock(&nn->client_lock);
3326 if (old)
3327 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 return status;
3329}
3330
J. Bruce Fields32513b42011-10-13 16:00:16 -04003331static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003333 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3334}
3335
3336/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003337static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3338 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003339{
Trond Myklebust950e0112014-06-30 11:48:31 -04003340 lockdep_assert_held(&state_lock);
3341
J. Bruce Fields32513b42011-10-13 16:00:16 -04003342 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003343 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003344 INIT_LIST_HEAD(&fp->fi_stateids);
3345 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02003346 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003347 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003348 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003349 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003350 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003351 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3352 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003353#ifdef CONFIG_NFSD_PNFS
3354 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02003355 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003356#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04003357 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358}
3359
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003360void
NeilBrowne60d4392005-06-23 22:03:01 -07003361nfsd4_free_slabs(void)
3362{
Sachin Bhamare8287f002015-04-27 14:50:14 +02003363 kmem_cache_destroy(odstate_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003364 kmem_cache_destroy(openowner_slab);
3365 kmem_cache_destroy(lockowner_slab);
3366 kmem_cache_destroy(file_slab);
3367 kmem_cache_destroy(stateid_slab);
3368 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003369}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370
Bryan Schumaker72083392011-11-01 15:24:59 -04003371int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372nfsd4_init_slabs(void)
3373{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003374 openowner_slab = kmem_cache_create("nfsd4_openowners",
3375 sizeof(struct nfs4_openowner), 0, 0, NULL);
3376 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003377 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003378 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003379 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003380 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003381 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003382 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003383 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003384 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003385 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003386 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003387 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003388 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003389 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003390 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003391 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003392 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003393 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003394 odstate_slab = kmem_cache_create("nfsd4_odstate",
3395 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3396 if (odstate_slab == NULL)
3397 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003399
Sachin Bhamare8287f002015-04-27 14:50:14 +02003400out_free_deleg_slab:
3401 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003402out_free_stateid_slab:
3403 kmem_cache_destroy(stateid_slab);
3404out_free_file_slab:
3405 kmem_cache_destroy(file_slab);
3406out_free_lockowner_slab:
3407 kmem_cache_destroy(lockowner_slab);
3408out_free_openowner_slab:
3409 kmem_cache_destroy(openowner_slab);
3410out:
NeilBrowne60d4392005-06-23 22:03:01 -07003411 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3412 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413}
3414
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003415static void init_nfs4_replay(struct nfs4_replay *rp)
3416{
3417 rp->rp_status = nfserr_serverfault;
3418 rp->rp_buflen = 0;
3419 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003420 mutex_init(&rp->rp_mutex);
3421}
3422
3423static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3424 struct nfs4_stateowner *so)
3425{
3426 if (!nfsd4_has_session(cstate)) {
3427 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003428 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04003429 }
3430}
3431
3432void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3433{
3434 struct nfs4_stateowner *so = cstate->replay_owner;
3435
3436 if (so != NULL) {
3437 cstate->replay_owner = NULL;
3438 mutex_unlock(&so->so_replay.rp_mutex);
3439 nfs4_put_stateowner(so);
3440 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003441}
3442
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003443static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444{
3445 struct nfs4_stateowner *sop;
3446
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003447 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003448 if (!sop)
3449 return NULL;
3450
3451 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3452 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003453 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003454 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003456 sop->so_owner.len = owner->len;
3457
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003458 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003459 sop->so_client = clp;
3460 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003461 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003462 return sop;
3463}
3464
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003465static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003466{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003467 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003468
Trond Myklebustd4f04892014-07-29 21:34:36 -04003469 list_add(&oo->oo_owner.so_strhash,
3470 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003471 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472}
3473
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003474static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3475{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003476 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003477}
3478
Jeff Layton6b180f02014-07-29 21:34:26 -04003479static void nfs4_free_openowner(struct nfs4_stateowner *so)
3480{
3481 struct nfs4_openowner *oo = openowner(so);
3482
3483 kmem_cache_free(openowner_slab, oo);
3484}
3485
3486static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003487 .so_unhash = nfs4_unhash_openowner,
3488 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003489};
3490
Andrew Elble7fc05642015-11-05 20:42:43 -05003491static struct nfs4_ol_stateid *
3492nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3493{
3494 struct nfs4_ol_stateid *local, *ret = NULL;
3495 struct nfs4_openowner *oo = open->op_openowner;
3496
3497 lockdep_assert_held(&fp->fi_lock);
3498
3499 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3500 /* ignore lock owners */
3501 if (local->st_stateowner->so_is_open_owner == 0)
3502 continue;
3503 if (local->st_stateowner == &oo->oo_owner) {
3504 ret = local;
3505 atomic_inc(&ret->st_stid.sc_count);
3506 break;
3507 }
3508 }
3509 return ret;
3510}
3511
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003512static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003513alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003514 struct nfsd4_compound_state *cstate)
3515{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003516 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003517 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003519 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3520 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003522 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003523 oo->oo_owner.so_is_open_owner = 1;
3524 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003525 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003526 if (nfsd4_has_session(cstate))
3527 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003528 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003529 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003530 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003531 spin_lock(&clp->cl_lock);
3532 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003533 if (ret == NULL) {
3534 hash_openowner(oo, clp, strhashval);
3535 ret = oo;
3536 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08003537 nfs4_free_stateowner(&oo->oo_owner);
3538
Trond Myklebustd4f04892014-07-29 21:34:36 -04003539 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04003540 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541}
3542
Andrew Elble7fc05642015-11-05 20:42:43 -05003543static struct nfs4_ol_stateid *
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003544init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
Andrew Elble7fc05642015-11-05 20:42:43 -05003545{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
Andrew Elble7fc05642015-11-05 20:42:43 -05003547 struct nfs4_openowner *oo = open->op_openowner;
3548 struct nfs4_ol_stateid *retstp = NULL;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003549 struct nfs4_ol_stateid *stp;
Andrew Elble7fc05642015-11-05 20:42:43 -05003550
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003551 stp = open->op_stp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003552 /* We are moving these outside of the spinlocks to avoid the warnings */
3553 mutex_init(&stp->st_mutex);
3554 mutex_lock(&stp->st_mutex);
3555
Andrew Elble7fc05642015-11-05 20:42:43 -05003556 spin_lock(&oo->oo_owner.so_client->cl_lock);
3557 spin_lock(&fp->fi_lock);
3558
3559 retstp = nfsd4_find_existing_open(fp, open);
3560 if (retstp)
3561 goto out_unlock;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003562
3563 open->op_stp = NULL;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003564 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003565 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003566 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003567 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07003568 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003569 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 stp->st_access_bmap = 0;
3571 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003572 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003573 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003574 list_add(&stp->st_perfile, &fp->fi_stateids);
Andrew Elble7fc05642015-11-05 20:42:43 -05003575
3576out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04003577 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003578 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003579 if (retstp) {
3580 mutex_lock(&retstp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003581 /* To keep mutex tracking happy */
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003582 mutex_unlock(&stp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003583 stp = retstp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003584 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003585 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586}
3587
Jeff Laytond3134b12014-07-29 21:34:32 -04003588/*
3589 * In the 4.0 case we need to keep the owners around a little while to handle
3590 * CLOSE replay. We still do need to release any file access that is held by
3591 * them before returning however.
3592 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003594move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595{
Jeff Layton217526e2014-07-30 08:27:11 -04003596 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003597 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3598 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3599 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003600
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003601 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602
Jeff Laytonb401be222014-07-29 21:34:33 -04003603 /*
3604 * We know that we hold one reference via nfsd4_close, and another
3605 * "persistent" reference for the client. If the refcount is higher
3606 * than 2, then there are still calls in progress that are using this
3607 * stateid. We can't put the sc_file reference until they are finished.
3608 * Wait for the refcount to drop to 2. Since it has been unhashed,
3609 * there should be no danger of the refcount going back up again at
3610 * this point.
3611 */
3612 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3613
Jeff Laytond3134b12014-07-29 21:34:32 -04003614 release_all_access(s);
3615 if (s->st_stid.sc_file) {
3616 put_nfs4_file(s->st_stid.sc_file);
3617 s->st_stid.sc_file = NULL;
3618 }
Jeff Layton217526e2014-07-30 08:27:11 -04003619
3620 spin_lock(&nn->client_lock);
3621 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003622 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003623 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003624 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003625 spin_unlock(&nn->client_lock);
3626 if (last)
3627 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628}
3629
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630/* search file_hashtbl[] for file */
3631static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04003632find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 struct nfs4_file *fp;
3635
Jeff Layton5b095e92014-10-23 08:01:02 -04003636 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02003637 if (fh_match(&fp->fi_fhandle, fh)) {
Jeff Layton5b095e92014-10-23 08:01:02 -04003638 if (atomic_inc_not_zero(&fp->fi_ref))
3639 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 }
3642 return NULL;
3643}
3644
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02003645struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003646find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003647{
3648 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003649 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003650
Jeff Layton5b095e92014-10-23 08:01:02 -04003651 rcu_read_lock();
3652 fp = find_file_locked(fh, hashval);
3653 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04003654 return fp;
3655}
3656
3657static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003658find_or_add_file(struct nfs4_file *new, 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);
3662
3663 rcu_read_lock();
3664 fp = find_file_locked(fh, hashval);
3665 rcu_read_unlock();
3666 if (fp)
3667 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04003668
3669 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04003670 fp = find_file_locked(fh, hashval);
3671 if (likely(fp == NULL)) {
3672 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04003673 fp = new;
3674 }
3675 spin_unlock(&state_lock);
3676
3677 return fp;
3678}
3679
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003680/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 * Called to check deny when READ with all zero stateid or
3682 * WRITE with all zero or all one stateid
3683 */
Al Virob37ad282006-10-19 23:28:59 -07003684static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3686{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003688 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689
Trond Myklebustca943212014-07-23 16:17:39 -04003690 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003691 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003692 return ret;
3693 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003694 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003695 if (fp->fi_share_deny & deny_type)
3696 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003697 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003698 put_nfs4_file(fp);
3699 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700}
3701
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003702static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003704 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04003705 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3706 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003707
Trond Myklebust11b91642014-07-29 21:34:08 -04003708 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003709
Jeff Layton02e12152014-07-16 10:31:57 -04003710 /*
3711 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003712 * already holding inode->i_lock.
3713 *
Jeff Laytondff13992014-07-08 14:02:49 -04003714 * If the dl_time != 0, then we know that it has already been
3715 * queued for a lease break. Don't queue it again.
3716 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003717 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003718 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003719 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003720 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003721 }
Jeff Layton02e12152014-07-16 10:31:57 -04003722 spin_unlock(&state_lock);
3723}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003725static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3726 struct rpc_task *task)
3727{
3728 struct nfs4_delegation *dp = cb_to_delegation(cb);
3729
Andrew Elblea4579742015-08-31 12:06:41 -04003730 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3731 return 1;
3732
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003733 switch (task->tk_status) {
3734 case 0:
3735 return 1;
3736 case -EBADHANDLE:
3737 case -NFS4ERR_BAD_STATEID:
3738 /*
3739 * Race: client probably got cb_recall before open reply
3740 * granting delegation.
3741 */
3742 if (dp->dl_retries--) {
3743 rpc_delay(task, 2 * HZ);
3744 return 0;
3745 }
3746 /*FALLTHRU*/
3747 default:
3748 return -1;
3749 }
3750}
3751
3752static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3753{
3754 struct nfs4_delegation *dp = cb_to_delegation(cb);
3755
3756 nfs4_put_stid(&dp->dl_stid);
3757}
3758
Julia Lawallc4cb8972015-11-21 22:57:39 +01003759static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003760 .prepare = nfsd4_cb_recall_prepare,
3761 .done = nfsd4_cb_recall_done,
3762 .release = nfsd4_cb_recall_release,
3763};
3764
Jeff Layton02e12152014-07-16 10:31:57 -04003765static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3766{
3767 /*
3768 * We're assuming the state code never drops its reference
3769 * without first removing the lease. Since we're in this lease
3770 * callback (and since the lease code is serialized by the kernel
3771 * lock) we know the server hasn't removed the lease yet, we know
3772 * it's safe to take a reference.
3773 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003774 atomic_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02003775 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003776}
3777
Jeff Layton1c8c6012013-06-21 08:58:15 -04003778/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003779static bool
3780nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003781{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003782 bool ret = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003783 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3784 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003785
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003786 if (!fp) {
3787 WARN(1, "(%p)->fl_owner NULL\n", fl);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003788 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003789 }
3790 if (fp->fi_had_conflict) {
3791 WARN(1, "duplicate break on %p\n", fp);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003792 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003793 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003794 /*
3795 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003796 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003797 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003798 */
3799 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800
Jeff Layton02e12152014-07-16 10:31:57 -04003801 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003802 fp->fi_had_conflict = true;
3803 /*
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003804 * If there are no delegations on the list, then return true
3805 * so that the lease code will go ahead and delete it.
Jeff Layton417c6622014-07-21 09:34:57 -04003806 */
3807 if (list_empty(&fp->fi_delegations))
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003808 ret = true;
Jeff Layton417c6622014-07-21 09:34:57 -04003809 else
3810 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3811 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003812 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003813 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814}
3815
Jeff Laytonc45198e2014-09-01 07:12:07 -04003816static int
Jeff Layton7448cc32015-01-16 15:05:57 -05003817nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3818 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819{
3820 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04003821 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 else
3823 return -EAGAIN;
3824}
3825
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003826static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003827 .lm_break = nfsd_break_deleg_cb,
3828 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829};
3830
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003831static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3832{
3833 if (nfsd4_has_session(cstate))
3834 return nfs_ok;
3835 if (seqid == so->so_seqid - 1)
3836 return nfserr_replay_me;
3837 if (seqid == so->so_seqid)
3838 return nfs_ok;
3839 return nfserr_bad_seqid;
3840}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841
Jeff Layton4b24ca72014-06-30 11:48:44 -04003842static __be32 lookup_clientid(clientid_t *clid,
3843 struct nfsd4_compound_state *cstate,
3844 struct nfsd_net *nn)
3845{
3846 struct nfs4_client *found;
3847
3848 if (cstate->clp) {
3849 found = cstate->clp;
3850 if (!same_clid(&found->cl_clientid, clid))
3851 return nfserr_stale_clientid;
3852 return nfs_ok;
3853 }
3854
3855 if (STALE_CLIENTID(clid, nn))
3856 return nfserr_stale_clientid;
3857
3858 /*
3859 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3860 * cached already then we know this is for is for v4.0 and "sessions"
3861 * will be false.
3862 */
3863 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003864 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003865 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003866 if (!found) {
3867 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003868 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003869 }
3870 atomic_inc(&found->cl_refcount);
3871 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003872
3873 /* Cache the nfs4_client in cstate! */
3874 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003875 return nfs_ok;
3876}
3877
Al Virob37ad282006-10-19 23:28:59 -07003878__be32
Andy Adamson66689582009-04-03 08:28:45 +03003879nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003880 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 clientid_t *clientid = &open->op_clientid;
3883 struct nfs4_client *clp = NULL;
3884 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003885 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003886 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003888 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003890 /*
3891 * In case we need it later, after we've already created the
3892 * file and don't want to risk a further failure:
3893 */
3894 open->op_file = nfsd4_alloc_file();
3895 if (open->op_file == NULL)
3896 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897
Trond Myklebust2d91e892014-06-30 11:48:46 -04003898 status = lookup_clientid(clientid, cstate, nn);
3899 if (status)
3900 return status;
3901 clp = cstate->clp;
3902
Trond Myklebustd4f04892014-07-29 21:34:36 -04003903 strhashval = ownerstr_hashval(&open->op_owner);
3904 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003905 open->op_openowner = oo;
3906 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003907 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003909 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003910 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003911 release_openowner(oo);
3912 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003913 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003914 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003915 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3916 if (status)
3917 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003918 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003919new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003920 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003921 if (oo == NULL)
3922 return nfserr_jukebox;
3923 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003924alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003925 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003926 if (!open->op_stp)
3927 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003928
3929 if (nfsd4_has_session(cstate) &&
3930 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
3931 open->op_odstate = alloc_clnt_odstate(clp);
3932 if (!open->op_odstate)
3933 return nfserr_jukebox;
3934 }
3935
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003936 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937}
3938
Al Virob37ad282006-10-19 23:28:59 -07003939static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003940nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3941{
3942 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3943 return nfserr_openmode;
3944 else
3945 return nfs_ok;
3946}
3947
Daniel Mackc47d8322011-05-16 16:38:14 +02003948static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003949{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003950 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3951}
3952
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003953static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003954{
3955 struct nfs4_stid *ret;
3956
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003957 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003958 if (!ret)
3959 return NULL;
3960 return delegstateid(ret);
3961}
3962
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003963static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3964{
3965 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3966 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3967}
3968
Al Virob37ad282006-10-19 23:28:59 -07003969static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003970nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003971 struct nfs4_delegation **dp)
3972{
3973 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003974 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003975 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003976
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003977 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3978 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003979 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003980 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003981 status = nfs4_check_delegmode(deleg, flags);
3982 if (status) {
3983 nfs4_put_stid(&deleg->dl_stid);
3984 goto out;
3985 }
3986 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003987out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003988 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003989 return nfs_ok;
3990 if (status)
3991 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003992 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003993 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003994}
3995
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003996static inline int nfs4_access_to_access(u32 nfs4_access)
3997{
3998 int flags = 0;
3999
4000 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4001 flags |= NFSD_MAY_READ;
4002 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4003 flags |= NFSD_MAY_WRITE;
4004 return flags;
4005}
4006
Al Virob37ad282006-10-19 23:28:59 -07004007static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4009 struct nfsd4_open *open)
4010{
4011 struct iattr iattr = {
4012 .ia_valid = ATTR_SIZE,
4013 .ia_size = 0,
4014 };
4015 if (!open->op_truncate)
4016 return 0;
4017 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08004018 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
4020}
4021
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004022static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004023 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4024 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004025{
Trond Myklebustde186432014-07-10 14:07:26 -04004026 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004027 __be32 status;
4028 int oflag = nfs4_access_to_omode(open->op_share_access);
4029 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004030 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004031
Trond Myklebustde186432014-07-10 14:07:26 -04004032 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004033
4034 /*
4035 * Are we trying to set a deny mode that would conflict with
4036 * current access?
4037 */
4038 status = nfs4_file_check_deny(fp, open->op_share_deny);
4039 if (status != nfs_ok) {
4040 spin_unlock(&fp->fi_lock);
4041 goto out;
4042 }
4043
4044 /* set access to the file */
4045 status = nfs4_file_get_access(fp, open->op_share_access);
4046 if (status != nfs_ok) {
4047 spin_unlock(&fp->fi_lock);
4048 goto out;
4049 }
4050
4051 /* Set access bits in stateid */
4052 old_access_bmap = stp->st_access_bmap;
4053 set_access(open->op_share_access, stp);
4054
4055 /* Set new deny mask */
4056 old_deny_bmap = stp->st_deny_bmap;
4057 set_deny(open->op_share_deny, stp);
4058 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4059
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004060 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04004061 spin_unlock(&fp->fi_lock);
4062 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004063 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004064 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04004065 spin_lock(&fp->fi_lock);
4066 if (!fp->fi_fds[oflag]) {
4067 fp->fi_fds[oflag] = filp;
4068 filp = NULL;
4069 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004070 }
Trond Myklebustde186432014-07-10 14:07:26 -04004071 spin_unlock(&fp->fi_lock);
4072 if (filp)
4073 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004074
4075 status = nfsd4_truncate(rqstp, cur_fh, open);
4076 if (status)
4077 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004078out:
4079 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004080out_put_access:
4081 stp->st_access_bmap = old_access_bmap;
4082 nfs4_file_put_access(fp, open->op_share_access);
4083 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4084 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004085}
4086
Al Virob37ad282006-10-19 23:28:59 -07004087static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004088nfs4_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 -07004089{
Al Virob37ad282006-10-19 23:28:59 -07004090 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02004091 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004093 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004094 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004095
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004096 /* test and set deny mode */
4097 spin_lock(&fp->fi_lock);
4098 status = nfs4_file_check_deny(fp, open->op_share_deny);
4099 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004100 set_deny(open->op_share_deny, stp);
4101 fp->fi_share_deny |=
4102 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4103 }
4104 spin_unlock(&fp->fi_lock);
4105
4106 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004109 status = nfsd4_truncate(rqstp, cur_fh, open);
4110 if (status != nfs_ok)
4111 reset_union_bmap_deny(old_deny_bmap, stp);
4112 return status;
4113}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004115/* Should we give out recallable state?: */
4116static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4117{
4118 if (clp->cl_cb_state == NFSD4_CB_UP)
4119 return true;
4120 /*
4121 * In the sessions case, since we don't have to establish a
4122 * separate connection for callbacks, we assume it's OK
4123 * until we hear otherwise:
4124 */
4125 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4126}
4127
Jeff Laytond564fbe2014-07-16 10:31:58 -04004128static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004129{
4130 struct file_lock *fl;
4131
4132 fl = locks_alloc_lock();
4133 if (!fl)
4134 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004135 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04004136 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004137 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4138 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04004139 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004140 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004141 return fl;
4142}
4143
Andrew Elble34ed9872015-10-15 12:07:28 -04004144/**
4145 * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer
4146 * @dp: a pointer to the nfs4_delegation we're adding.
4147 *
4148 * Return:
4149 * On success: Return code will be 0 on success.
4150 *
4151 * On error: -EAGAIN if there was an existing delegation.
4152 * nonzero if there is an error in other cases.
4153 *
4154 */
4155
J. Bruce Fields99c41512013-05-21 16:21:25 -04004156static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004157{
Trond Myklebust11b91642014-07-29 21:34:08 -04004158 struct nfs4_file *fp = dp->dl_stid.sc_file;
Kinglong Meeefde6b42015-07-13 17:33:24 +08004159 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04004160 struct file *filp;
4161 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004162
Jeff Laytond564fbe2014-07-16 10:31:58 -04004163 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004164 if (!fl)
4165 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04004166 filp = find_readable_file(fp);
4167 if (!filp) {
4168 /* We should always have a readable file here */
4169 WARN_ON_ONCE(1);
Kinglong Meeaf9dbaf2015-07-13 17:30:51 +08004170 locks_free_lock(fl);
Jeff Layton417c6622014-07-21 09:34:57 -04004171 return -EBADF;
4172 }
4173 fl->fl_file = filp;
Jeff Laytone6f5c782014-08-22 10:40:25 -04004174 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04004175 if (fl)
Jeff Layton417c6622014-07-21 09:34:57 -04004176 locks_free_lock(fl);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04004177 if (status)
Jeff Layton417c6622014-07-21 09:34:57 -04004178 goto out_fput;
Benny Halevycdc975052014-05-30 09:09:30 -04004179 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004180 spin_lock(&fp->fi_lock);
4181 /* Did the lease get broken before we took the lock? */
4182 status = -EAGAIN;
4183 if (fp->fi_had_conflict)
4184 goto out_unlock;
4185 /* Race breaker */
Jeff Layton0c637be2014-08-22 12:05:43 -04004186 if (fp->fi_deleg_file) {
Andrew Elble34ed9872015-10-15 12:07:28 -04004187 status = hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04004188 goto out_unlock;
4189 }
Jeff Layton417c6622014-07-21 09:34:57 -04004190 fp->fi_deleg_file = filp;
Andrew Elble34ed9872015-10-15 12:07:28 -04004191 fp->fi_delegees = 0;
4192 status = hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04004193 spin_unlock(&fp->fi_lock);
Benny Halevycdc975052014-05-30 09:09:30 -04004194 spin_unlock(&state_lock);
Andrew Elble34ed9872015-10-15 12:07:28 -04004195 if (status) {
4196 /* Should never happen, this is a new fi_deleg_file */
4197 WARN_ON_ONCE(1);
4198 goto out_fput;
4199 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004200 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04004201out_unlock:
4202 spin_unlock(&fp->fi_lock);
4203 spin_unlock(&state_lock);
4204out_fput:
4205 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05004206 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004207}
4208
Jeff Layton0b266932014-07-25 07:34:25 -04004209static struct nfs4_delegation *
4210nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004211 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004212{
Jeff Layton0b266932014-07-25 07:34:25 -04004213 int status;
4214 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04004215
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004216 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004217 return ERR_PTR(-EAGAIN);
4218
Andrew Elble34ed9872015-10-15 12:07:28 -04004219 spin_lock(&state_lock);
4220 spin_lock(&fp->fi_lock);
4221 status = nfs4_get_existing_delegation(clp, fp);
4222 spin_unlock(&fp->fi_lock);
4223 spin_unlock(&state_lock);
4224
4225 if (status)
4226 return ERR_PTR(status);
4227
Sachin Bhamare8287f002015-04-27 14:50:14 +02004228 dp = alloc_init_deleg(clp, fh, odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004229 if (!dp)
4230 return ERR_PTR(-ENOMEM);
4231
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004232 get_nfs4_file(fp);
Benny Halevycdc975052014-05-30 09:09:30 -04004233 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004234 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04004235 dp->dl_stid.sc_file = fp;
Jeff Layton0c637be2014-08-22 12:05:43 -04004236 if (!fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04004237 spin_unlock(&fp->fi_lock);
4238 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004239 status = nfs4_setlease(dp);
4240 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04004241 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004242 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04004243 status = -EAGAIN;
4244 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004245 }
Andrew Elble34ed9872015-10-15 12:07:28 -04004246 status = hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04004247out_unlock:
4248 spin_unlock(&fp->fi_lock);
Benny Halevycdc975052014-05-30 09:09:30 -04004249 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004250out:
4251 if (status) {
Sachin Bhamare8287f002015-04-27 14:50:14 +02004252 put_clnt_odstate(dp->dl_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04004253 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04004254 return ERR_PTR(status);
4255 }
4256 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004257}
4258
Benny Halevy4aa89132012-02-21 14:16:44 -08004259static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4260{
4261 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4262 if (status == -EAGAIN)
4263 open->op_why_no_deleg = WND4_CONTENTION;
4264 else {
4265 open->op_why_no_deleg = WND4_RESOURCE;
4266 switch (open->op_deleg_want) {
4267 case NFS4_SHARE_WANT_READ_DELEG:
4268 case NFS4_SHARE_WANT_WRITE_DELEG:
4269 case NFS4_SHARE_WANT_ANY_DELEG:
4270 break;
4271 case NFS4_SHARE_WANT_CANCEL:
4272 open->op_why_no_deleg = WND4_CANCELLED;
4273 break;
4274 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004275 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08004276 }
4277 }
4278}
4279
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280/*
4281 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04004282 *
4283 * Note we don't support write delegations, and won't until the vfs has
4284 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 */
4286static void
Jeff Layton4cf59222014-07-25 07:34:24 -04004287nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4288 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289{
4290 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04004291 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4292 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004293 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004294 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004296 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07004297 open->op_recall = 0;
4298 switch (open->op_claim_type) {
4299 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05004300 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07004301 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004302 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4303 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004304 break;
4305 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00004306 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004307 /*
4308 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004309 * had the chance to reclaim theirs, *and* until
4310 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004311 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004312 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004313 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004314 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004315 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04004316 /*
4317 * Also, if the file was opened for write or
4318 * create, there's a good chance the client's
4319 * about to write to it, resulting in an
4320 * immediate recall (since we don't support
4321 * write delegations):
4322 */
NeilBrown7b190fe2005-06-23 22:03:23 -07004323 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04004324 goto out_no_deleg;
4325 if (open->op_create == NFS4_OPEN_CREATE)
4326 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004327 break;
4328 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004329 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004330 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004331 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004332 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004333 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004335 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004337 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004338 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04004339 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04004340 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004341 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004342out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004343 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4344 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004345 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04004346 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004347 open->op_recall = 1;
4348 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004349
4350 /* 4.1 client asking for a delegation? */
4351 if (open->op_deleg_want)
4352 nfsd4_open_deleg_none_ext(open, status);
4353 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354}
4355
Benny Halevye27f49c2012-02-21 14:16:54 -08004356static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4357 struct nfs4_delegation *dp)
4358{
4359 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4360 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4361 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4362 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4363 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4364 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4365 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4366 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4367 }
4368 /* Otherwise the client must be confused wanting a delegation
4369 * it already has, therefore we don't return
4370 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4371 */
4372}
4373
Al Virob37ad282006-10-19 23:28:59 -07004374__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4376{
Andy Adamson66689582009-04-03 08:28:45 +03004377 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004378 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004380 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004381 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004382 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 /*
4385 * Lookup file; if found, lookup stateid and check open request,
4386 * and check for delegations in the process of being recalled.
4387 * If not found, create the nfs4_file struct
4388 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004389 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004390 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004391 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004392 if (status)
4393 goto out;
Andrew Elble7fc05642015-11-05 20:42:43 -05004394 spin_lock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04004395 stp = nfsd4_find_existing_open(fp, open);
Andrew Elble7fc05642015-11-05 20:42:43 -05004396 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004398 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004399 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004400 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004401 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 }
4403
4404 /*
4405 * OPEN the file, or upgrade an existing OPEN.
4406 * If truncate fails, the OPEN fails.
4407 */
4408 if (stp) {
4409 /* Stateid was found, this is an OPEN upgrade */
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004410 mutex_lock(&stp->st_mutex);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004411 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04004412 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004413 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04004415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 } else {
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004417 /* stp is returned locked. */
4418 stp = init_open_stateid(fp, open);
4419 /* See if we lost the race to some other thread */
4420 if (stp->st_access_bmap != 0) {
Andrew Elble7fc05642015-11-05 20:42:43 -05004421 status = nfs4_upgrade_open(rqstp, fp, current_fh,
4422 stp, open);
4423 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004424 mutex_unlock(&stp->st_mutex);
Andrew Elble7fc05642015-11-05 20:42:43 -05004425 goto out;
4426 }
4427 goto upgrade_out;
4428 }
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004429 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4430 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004431 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004432 release_open_stateid(stp);
4433 goto out;
4434 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004435
4436 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4437 open->op_odstate);
4438 if (stp->st_clnt_odstate == open->op_odstate)
4439 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 }
Andrew Elble7fc05642015-11-05 20:42:43 -05004441upgrade_out:
Jeff Layton9767feb2015-10-01 09:05:50 -04004442 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004443 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444
Benny Halevyd24433c2012-02-16 20:57:17 +02004445 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004446 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4447 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4448 open->op_why_no_deleg = WND4_NOT_WANTED;
4449 goto nodeleg;
4450 }
4451 }
4452
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 /*
4454 * Attempt to hand out a delegation. No error return, because the
4455 * OPEN succeeds even if we fail.
4456 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004457 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004458nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 status = nfs_ok;
4460
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004461 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004462 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004464 /* 4.1 client trying to upgrade/downgrade delegation? */
4465 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004466 open->op_deleg_want)
4467 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004468
NeilBrown13cd2182005-06-23 22:03:10 -07004469 if (fp)
4470 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004471 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08004472 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 /*
4474 * To finish the open response, we just need to set the rflags.
4475 */
4476 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Jeff Layton19e4c342016-09-16 16:28:27 -04004477 if (nfsd4_has_session(&resp->cstate))
4478 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
4479 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Jeff Layton19e4c342016-09-16 16:28:27 -04004481
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004482 if (dp)
4483 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004484 if (stp)
4485 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486
4487 return status;
4488}
4489
Jeff Layton58fb12e2014-07-29 21:34:27 -04004490void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04004491 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004492{
4493 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004494 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004495
Jeff Laytond3134b12014-07-29 21:34:32 -04004496 nfsd4_cstate_assign_replay(cstate, so);
4497 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004498 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004499 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04004500 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004501 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004502 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004503 if (open->op_odstate)
4504 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004505}
4506
Al Virob37ad282006-10-19 23:28:59 -07004507__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004508nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4509 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510{
4511 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004512 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004513 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 dprintk("process_renew(%08x/%08x): starting\n",
4516 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004517 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004518 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004520 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004522 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004523 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 goto out;
4525 status = nfs_ok;
4526out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 return status;
4528}
4529
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004530void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004531nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004532{
Jeff Layton33dcc482012-04-10 11:08:48 -04004533 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004534 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004535 return;
4536
NeilBrowna76b4312005-06-23 22:04:01 -07004537 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004538 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004539 /*
4540 * If the server goes down again right now, an NFSv4
4541 * client will still be allowed to reclaim after it comes back up,
4542 * even if it hasn't yet had a chance to reclaim state this time.
4543 *
4544 */
Jeff Layton919b8042014-09-12 16:40:20 -04004545 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004546 /*
4547 * At this point, NFSv4 clients can still reclaim. But if the
4548 * server crashes, any that have not yet reclaimed will be out
4549 * of luck on the next boot.
4550 *
4551 * (NFSv4.1+ clients are considered to have reclaimed once they
4552 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4553 * have reclaimed after their first OPEN.)
4554 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004555 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004556 /*
4557 * At this point, and once lockd and/or any other containers
4558 * exit their grace period, further reclaims will fail and
4559 * regular locking can resume.
4560 */
NeilBrowna76b4312005-06-23 22:04:01 -07004561}
4562
NeilBrownfd39ca92005-06-23 22:04:03 -07004563static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004564nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565{
4566 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004567 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004569 struct nfs4_ol_stateid *stp;
Jeff Layton7919d0a2016-09-16 16:28:25 -04004570 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004572 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004573 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004576 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004577 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004578 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004579 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 clp = list_entry(pos, struct nfs4_client, cl_lru);
4581 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4582 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004583 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 break;
4585 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004586 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004587 dprintk("NFSD: client in use (clientid %08x)\n",
4588 clp->cl_clientid.cl_id);
4589 continue;
4590 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004591 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004592 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004593 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004594 list_for_each_safe(pos, next, &reaplist) {
4595 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 dprintk("NFSD: purging unused client (clientid %08x)\n",
4597 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004598 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 expire_client(clp);
4600 }
Benny Halevycdc975052014-05-30 09:09:30 -04004601 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004602 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4604 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004605 t = dp->dl_time - cutoff;
4606 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 break;
4608 }
Jeff Layton3fcbbd22015-08-24 12:41:48 -04004609 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04004610 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 }
Benny Halevycdc975052014-05-30 09:09:30 -04004612 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004613 while (!list_empty(&reaplist)) {
4614 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4615 dl_recall_lru);
4616 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004617 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 }
Jeff Layton217526e2014-07-30 08:27:11 -04004619
4620 spin_lock(&nn->client_lock);
4621 while (!list_empty(&nn->close_lru)) {
4622 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4623 oo_close_lru);
4624 if (time_after((unsigned long)oo->oo_time,
4625 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004626 t = oo->oo_time - cutoff;
4627 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 break;
4629 }
Jeff Layton217526e2014-07-30 08:27:11 -04004630 list_del_init(&oo->oo_close_lru);
4631 stp = oo->oo_last_closed_stid;
4632 oo->oo_last_closed_stid = NULL;
4633 spin_unlock(&nn->client_lock);
4634 nfs4_put_stid(&stp->st_stid);
4635 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 }
Jeff Layton217526e2014-07-30 08:27:11 -04004637 spin_unlock(&nn->client_lock);
4638
Jeff Layton7919d0a2016-09-16 16:28:25 -04004639 /*
4640 * It's possible for a client to try and acquire an already held lock
4641 * that is being held for a long time, and then lose interest in it.
4642 * So, we clean out any un-revisited request after a lease period
4643 * under the assumption that the client is no longer interested.
4644 *
4645 * RFC5661, sec. 9.6 states that the client must not rely on getting
4646 * notifications and must continue to poll for locks, even when the
4647 * server supports them. Thus this shouldn't lead to clients blocking
4648 * indefinitely once the lock does become free.
4649 */
4650 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04004651 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04004652 while (!list_empty(&nn->blocked_locks_lru)) {
4653 nbl = list_first_entry(&nn->blocked_locks_lru,
4654 struct nfsd4_blocked_lock, nbl_lru);
4655 if (time_after((unsigned long)nbl->nbl_time,
4656 (unsigned long)cutoff)) {
4657 t = nbl->nbl_time - cutoff;
4658 new_timeo = min(new_timeo, t);
4659 break;
4660 }
4661 list_move(&nbl->nbl_lru, &reaplist);
4662 list_del_init(&nbl->nbl_list);
4663 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04004664 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04004665
4666 while (!list_empty(&reaplist)) {
4667 nbl = list_first_entry(&nn->blocked_locks_lru,
4668 struct nfsd4_blocked_lock, nbl_lru);
4669 list_del_init(&nbl->nbl_lru);
4670 posix_unblock_lock(&nbl->nbl_lock);
4671 free_blocked_lock(nbl);
4672 }
4673
Jeff Laytona832e7a2014-05-30 09:09:26 -04004674 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004675 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676}
4677
Harvey Harrisona254b242008-02-20 12:49:00 -08004678static struct workqueue_struct *laundry_wq;
4679static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004680
4681static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004682laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683{
4684 time_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08004685 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004686 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4687 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004689 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004691 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692}
4693
Jeff Layton8fcd4612015-07-30 06:57:46 -04004694static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004695{
Jeff Layton8fcd4612015-07-30 06:57:46 -04004696 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004697 return nfserr_bad_stateid;
4698 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699}
4700
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004702access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004704 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4705 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4706 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707}
4708
4709static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004710access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004712 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4713 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714}
4715
4716static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004717__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718{
Al Virob37ad282006-10-19 23:28:59 -07004719 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720
J. Bruce Fields02921912010-07-29 15:16:59 -04004721 /* For lock stateid's, we test the parent open, not the lock: */
4722 if (stp->st_openstp)
4723 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004724 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004726 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 goto out;
4728 status = nfs_ok;
4729out:
4730 return status;
4731}
4732
Al Virob37ad282006-10-19 23:28:59 -07004733static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004734check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004736 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004738 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004739 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 * conflicting state; so we must wait out the grace period. */
4741 return nfserr_grace;
4742 } else if (flags & WR_STATE)
4743 return nfs4_share_conflict(current_fh,
4744 NFS4_SHARE_DENY_WRITE);
4745 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4746 return nfs4_share_conflict(current_fh,
4747 NFS4_SHARE_DENY_READ);
4748}
4749
4750/*
4751 * Allow READ/WRITE during grace period on recovered state only for files
4752 * that are not able to provide mandatory locking.
4753 */
4754static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004755grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004757 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758}
4759
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004760static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004761{
Andy Adamson66689582009-04-03 08:28:45 +03004762 /*
4763 * When sessions are used the stateid generation number is ignored
4764 * when it is zero.
4765 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004766 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004767 return nfs_ok;
4768
4769 if (in->si_generation == ref->si_generation)
4770 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004771
J. Bruce Fields0836f582008-01-26 19:08:12 -05004772 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04004773 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004774 return nfserr_bad_stateid;
4775 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004776 * However, we could see a stateid from the past, even from a
4777 * non-buggy client. For example, if the client sends a lock
4778 * while some IO is outstanding, the lock may bump si_generation
4779 * while the IO is still in flight. The client could avoid that
4780 * situation by waiting for responses on all the IO requests,
4781 * but better performance may result in retrying IO that
4782 * receives an old_stateid error if requests are rarely
4783 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004784 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004785 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004786}
4787
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004788static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4789{
4790 if (ols->st_stateowner->so_is_open_owner &&
4791 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4792 return nfserr_bad_stateid;
4793 return nfs_ok;
4794}
4795
Chuck Lever7df302f2012-05-29 13:56:37 -04004796static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004797{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004798 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004799 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004800
Chuck Lever7df302f2012-05-29 13:56:37 -04004801 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004802 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004803 /* Client debugging aid. */
4804 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4805 char addr_str[INET6_ADDRSTRLEN];
4806 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4807 sizeof(addr_str));
4808 pr_warn_ratelimited("NFSD: client %s testing state ID "
4809 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004810 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004811 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004812 spin_lock(&cl->cl_lock);
4813 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004814 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004815 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004816 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004817 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004818 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004819 switch (s->sc_type) {
4820 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004821 status = nfs_ok;
4822 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004823 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004824 status = nfserr_deleg_revoked;
4825 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004826 case NFS4_OPEN_STID:
4827 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004828 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04004829 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004830 default:
4831 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004832 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004833 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004834 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004835 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004836 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004837out_unlock:
4838 spin_unlock(&cl->cl_lock);
4839 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004840}
4841
Christoph Hellwigcd61c522014-08-14 08:44:57 +02004842__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004843nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4844 stateid_t *stateid, unsigned char typemask,
4845 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004846{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004847 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004848
4849 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4850 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004851 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004852 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004853 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004854 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004855 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004856 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004857 if (status)
4858 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004859 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004860 if (!*s)
4861 return nfserr_bad_stateid;
4862 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004863}
4864
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004865static struct file *
4866nfs4_find_file(struct nfs4_stid *s, int flags)
4867{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004868 if (!s)
4869 return NULL;
4870
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004871 switch (s->sc_type) {
4872 case NFS4_DELEG_STID:
4873 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
4874 return NULL;
4875 return get_file(s->sc_file->fi_deleg_file);
4876 case NFS4_OPEN_STID:
4877 case NFS4_LOCK_STID:
4878 if (flags & RD_STATE)
4879 return find_readable_file(s->sc_file);
4880 else
4881 return find_writeable_file(s->sc_file);
4882 break;
4883 }
4884
4885 return NULL;
4886}
4887
4888static __be32
4889nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
4890{
4891 __be32 status;
4892
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004893 status = nfsd4_check_openowner_confirmed(ols);
4894 if (status)
4895 return status;
4896 return nfs4_check_openmode(ols, flags);
4897}
4898
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004899static __be32
4900nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
4901 struct file **filpp, bool *tmp_file, int flags)
4902{
4903 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
4904 struct file *file;
4905 __be32 status;
4906
4907 file = nfs4_find_file(s, flags);
4908 if (file) {
4909 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
4910 acc | NFSD_MAY_OWNER_OVERRIDE);
4911 if (status) {
4912 fput(file);
4913 return status;
4914 }
4915
4916 *filpp = file;
4917 } else {
4918 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
4919 if (status)
4920 return status;
4921
4922 if (tmp_file)
4923 *tmp_file = true;
4924 }
4925
4926 return 0;
4927}
4928
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004930 * Checks for stateid operations
4931 */
Al Virob37ad282006-10-19 23:28:59 -07004932__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004933nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01004934 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
4935 stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004937 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004938 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004939 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004940 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004941 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 if (filpp)
4944 *filpp = NULL;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004945 if (tmp_file)
4946 *tmp_file = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004948 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 return nfserr_grace;
4950
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004951 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
4952 status = check_special_stateids(net, fhp, stateid, flags);
4953 goto done;
4954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004956 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004957 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004958 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004959 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04004960 return status;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004961 status = check_stateid_generation(stateid, &s->sc_stateid,
4962 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04004963 if (status)
4964 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004965
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004966 switch (s->sc_type) {
4967 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004968 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004969 break;
4970 case NFS4_OPEN_STID:
4971 case NFS4_LOCK_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004972 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004973 break;
4974 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004975 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004976 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04004978 if (status)
4979 goto out;
4980 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004981
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004982done:
4983 if (!status && filpp)
4984 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985out:
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004986 if (s)
4987 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 return status;
4989}
4990
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004991/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004992 * Test if the stateid is valid
4993 */
4994__be32
4995nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4996 struct nfsd4_test_stateid *test_stateid)
4997{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004998 struct nfsd4_test_stateid_id *stateid;
4999 struct nfs4_client *cl = cstate->session->se_client;
5000
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005001 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04005002 stateid->ts_id_status =
5003 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005004
Bryan Schumaker17456802011-07-13 10:50:48 -04005005 return nfs_ok;
5006}
5007
Chuck Lever42691392016-08-11 10:37:30 -04005008static __be32
5009nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5010{
5011 struct nfs4_ol_stateid *stp = openlockstateid(s);
5012 __be32 ret;
5013
5014 mutex_lock(&stp->st_mutex);
5015
5016 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5017 if (ret)
5018 goto out;
5019
5020 ret = nfserr_locks_held;
5021 if (check_for_locks(stp->st_stid.sc_file,
5022 lockowner(stp->st_stateowner)))
5023 goto out;
5024
5025 release_lock_stateid(stp);
5026 ret = nfs_ok;
5027
5028out:
5029 mutex_unlock(&stp->st_mutex);
5030 nfs4_put_stid(s);
5031 return ret;
5032}
5033
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005034__be32
5035nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5036 struct nfsd4_free_stateid *free_stateid)
5037{
5038 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005039 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005040 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005041 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005042 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005043
Jeff Layton1af71cc2014-07-29 21:34:14 -04005044 spin_lock(&cl->cl_lock);
5045 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005046 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005047 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005048 switch (s->sc_type) {
5049 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005050 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005051 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005052 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005053 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5054 if (ret)
5055 break;
5056 ret = nfserr_locks_held;
5057 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005058 case NFS4_LOCK_STID:
Chuck Lever42691392016-08-11 10:37:30 -04005059 atomic_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005060 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04005061 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005062 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005063 case NFS4_REVOKED_DELEG_STID:
5064 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005065 list_del_init(&dp->dl_recall_lru);
5066 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04005067 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005068 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005069 goto out;
5070 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005071 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005072out_unlock:
5073 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005074out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005075 return ret;
5076}
5077
NeilBrown4c4cd222005-07-07 17:59:27 -07005078static inline int
5079setlkflg (int type)
5080{
5081 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5082 RD_STATE : WR_STATE;
5083}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005085static __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 -04005086{
5087 struct svc_fh *current_fh = &cstate->current_fh;
5088 struct nfs4_stateowner *sop = stp->st_stateowner;
5089 __be32 status;
5090
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005091 status = nfsd4_check_seqid(cstate, sop, seqid);
5092 if (status)
5093 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005094 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
5095 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005096 /*
5097 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005098 * nfserr_replay_me from the previous step, and
5099 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005100 */
5101 return nfserr_bad_stateid;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005102 mutex_lock(&stp->st_mutex);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005103 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04005104 if (status == nfs_ok)
5105 status = nfs4_check_fh(current_fh, &stp->st_stid);
5106 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005107 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005108 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005109}
5110
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111/*
5112 * Checks for sequence id mutating operations.
5113 */
Al Virob37ad282006-10-19 23:28:59 -07005114static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03005115nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04005116 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005117 struct nfs4_ol_stateid **stpp,
5118 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119{
J. Bruce Fields0836f582008-01-26 19:08:12 -05005120 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005121 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005122 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005124 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5125 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07005126
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005128 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005129 if (status)
5130 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005131 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005132 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005134 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005135 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005136 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04005137 else
5138 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005139 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005140}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05005141
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005142static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5143 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005144{
5145 __be32 status;
5146 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005147 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005149 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005150 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04005151 if (status)
5152 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005153 oo = openowner(stp->st_stateowner);
5154 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005155 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005156 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07005157 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005158 }
5159 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07005160 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161}
5162
Al Virob37ad282006-10-19 23:28:59 -07005163__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005164nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005165 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166{
Al Virob37ad282006-10-19 23:28:59 -07005167 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005168 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005169 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005170 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171
Al Viroa6a9f182013-09-16 10:57:01 -04005172 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5173 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005175 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07005176 if (status)
5177 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005179 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005180 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005181 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005182 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005183 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005184 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005185 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005186 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005187 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04005188 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005189 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04005190 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04005191 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005192 mutex_unlock(&stp->st_mutex);
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005193 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005194 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07005195
Jeff Layton2a4317c2012-03-21 16:42:43 -04005196 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005197 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04005198put_stateid:
5199 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005201 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 return status;
5203}
5204
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005205static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005207 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005208 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04005209 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005210 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005211}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04005212
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005213static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5214{
5215 switch (to_access) {
5216 case NFS4_SHARE_ACCESS_READ:
5217 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5218 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5219 break;
5220 case NFS4_SHARE_ACCESS_WRITE:
5221 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5222 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5223 break;
5224 case NFS4_SHARE_ACCESS_BOTH:
5225 break;
5226 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005227 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 }
5229}
5230
Al Virob37ad282006-10-19 23:28:59 -07005231__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005232nfsd4_open_downgrade(struct svc_rqst *rqstp,
5233 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005234 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235{
Al Virob37ad282006-10-19 23:28:59 -07005236 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005237 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005238 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239
Al Viroa6a9f182013-09-16 10:57:01 -04005240 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5241 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04005243 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02005244 if (od->od_deleg_want)
5245 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5246 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005248 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005249 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005250 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005253 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005254 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005256 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04005258 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005259 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005261 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005263 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04005264 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04005265 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005267put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005268 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005269 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005271 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 return status;
5273}
5274
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005275static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5276{
Trond Myklebustacf92952014-06-30 11:48:37 -04005277 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04005278 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04005279 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005280
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005281 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04005282 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04005283 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005284
Jeff Laytond83017f2014-07-29 21:34:42 -04005285 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04005286 if (unhashed)
5287 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04005288 spin_unlock(&clp->cl_lock);
5289 free_ol_stateid_reaplist(&reaplist);
5290 } else {
5291 spin_unlock(&clp->cl_lock);
5292 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04005293 if (unhashed)
5294 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04005295 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04005296}
5297
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298/*
5299 * nfs4_unlock_state() called after encode
5300 */
Al Virob37ad282006-10-19 23:28:59 -07005301__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005302nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005303 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304{
Al Virob37ad282006-10-19 23:28:59 -07005305 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005306 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005307 struct net *net = SVC_NET(rqstp);
5308 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309
Al Viroa6a9f182013-09-16 10:57:01 -04005310 dprintk("NFSD: nfsd4_close on file %pd\n",
5311 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005313 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5314 &close->cl_stateid,
5315 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005316 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005317 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005318 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 goto out;
Jeff Layton9767feb2015-10-01 09:05:50 -04005320 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005321 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005323 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005324
5325 /* put reference from nfs4_preprocess_seqid_op */
5326 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 return status;
5329}
5330
Al Virob37ad282006-10-19 23:28:59 -07005331__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005332nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5333 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005335 struct nfs4_delegation *dp;
5336 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005337 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07005338 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005339 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005341 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005342 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005344 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005345 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005346 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005347 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04005348 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005349 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04005350 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005351
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005352 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005353put_stateid:
5354 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355out:
5356 return status;
5357}
5358
Benny Halevy87df4de2008-12-15 19:42:03 +02005359static inline u64
5360end_offset(u64 start, u64 len)
5361{
5362 u64 end;
5363
5364 end = start + len;
5365 return end >= start ? end: NFS4_MAX_UINT64;
5366}
5367
5368/* last octet in a range */
5369static inline u64
5370last_byte_offset(u64 start, u64 len)
5371{
5372 u64 end;
5373
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005374 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02005375 end = start + len;
5376 return end > start ? end - 1: NFS4_MAX_UINT64;
5377}
5378
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379/*
5380 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5381 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5382 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5383 * locking, this prevents us from being completely protocol-compliant. The
5384 * real solution to this problem is to start using unsigned file offsets in
5385 * the VFS, but this is a very deep change!
5386 */
5387static inline void
5388nfs4_transform_lock_offset(struct file_lock *lock)
5389{
5390 if (lock->fl_start < 0)
5391 lock->fl_start = OFFSET_MAX;
5392 if (lock->fl_end < 0)
5393 lock->fl_end = OFFSET_MAX;
5394}
5395
Jeff Laytoncae80b32015-04-03 09:04:04 -04005396static fl_owner_t
5397nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005398{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005399 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5400
5401 nfs4_get_stateowner(&lo->lo_owner);
5402 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005403}
5404
Jeff Laytoncae80b32015-04-03 09:04:04 -04005405static void
5406nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005407{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005408 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005409
Jeff Laytoncae80b32015-04-03 09:04:04 -04005410 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04005411 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04005412}
5413
Jeff Layton76d348f2016-09-16 16:28:24 -04005414static void
5415nfsd4_lm_notify(struct file_lock *fl)
5416{
5417 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
5418 struct net *net = lo->lo_owner.so_client->net;
5419 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5420 struct nfsd4_blocked_lock *nbl = container_of(fl,
5421 struct nfsd4_blocked_lock, nbl_lock);
5422 bool queue = false;
5423
Jeff Layton7919d0a2016-09-16 16:28:25 -04005424 /* An empty list means that something else is going to be using it */
Jeff Layton0cc11a62016-10-20 09:34:31 -04005425 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005426 if (!list_empty(&nbl->nbl_list)) {
5427 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005428 list_del_init(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -04005429 queue = true;
5430 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04005431 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005432
5433 if (queue)
5434 nfsd4_run_cb(&nbl->nbl_cb);
5435}
5436
Alexey Dobriyan7b021962009-09-21 17:01:12 -07005437static const struct lock_manager_operations nfsd_posix_mng_ops = {
Jeff Layton76d348f2016-09-16 16:28:24 -04005438 .lm_notify = nfsd4_lm_notify,
Kinglong Meeaef95832014-08-22 10:18:44 -04005439 .lm_get_owner = nfsd4_fl_get_owner,
5440 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07005441};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442
5443static inline void
5444nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5445{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005446 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447
NeilBrownd5b90262006-04-10 22:55:22 -07005448 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005449 lo = (struct nfs4_lockowner *) fl->fl_owner;
5450 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5451 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005452 if (!deny->ld_owner.data)
5453 /* We just don't care that much */
5454 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005455 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5456 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07005457 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005458nevermind:
5459 deny->ld_owner.len = 0;
5460 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07005461 deny->ld_clientid.cl_boot = 0;
5462 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 }
5464 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02005465 deny->ld_length = NFS4_MAX_UINT64;
5466 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5468 deny->ld_type = NFS4_READ_LT;
5469 if (fl->fl_type != F_RDLCK)
5470 deny->ld_type = NFS4_WRITE_LT;
5471}
5472
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005473static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005474find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475{
Trond Myklebustd4f04892014-07-29 21:34:36 -04005476 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005477 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478
Trond Myklebust0a880a22014-07-30 08:27:10 -04005479 lockdep_assert_held(&clp->cl_lock);
5480
Trond Myklebustd4f04892014-07-29 21:34:36 -04005481 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5482 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005483 if (so->so_is_open_owner)
5484 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005485 if (same_owner_str(so, owner))
5486 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 }
5488 return NULL;
5489}
5490
Trond Myklebustc58c6612014-07-29 21:34:35 -04005491static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005492find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04005493{
5494 struct nfs4_lockowner *lo;
5495
Trond Myklebustd4f04892014-07-29 21:34:36 -04005496 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005497 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005498 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005499 return lo;
5500}
5501
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005502static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5503{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005504 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005505}
5506
Jeff Layton6b180f02014-07-29 21:34:26 -04005507static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5508{
5509 struct nfs4_lockowner *lo = lockowner(sop);
5510
5511 kmem_cache_free(lockowner_slab, lo);
5512}
5513
5514static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005515 .so_unhash = nfs4_unhash_lockowner,
5516 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005517};
5518
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519/*
5520 * Alloc a lock owner structure.
5521 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005522 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005524 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005526static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005527alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5528 struct nfs4_ol_stateid *open_stp,
5529 struct nfsd4_lock *lock)
5530{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005531 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005533 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5534 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04005536 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005537 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5538 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005539 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005540 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005541 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005542 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005543 if (ret == NULL) {
5544 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005545 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005546 ret = lo;
5547 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08005548 nfs4_free_stateowner(&lo->lo_owner);
5549
Trond Myklebustd4f04892014-07-29 21:34:36 -04005550 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04005551 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552}
5553
Jeff Layton356a95e2014-07-29 21:34:13 -04005554static void
5555init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5556 struct nfs4_file *fp, struct inode *inode,
5557 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005559 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560
Jeff Layton356a95e2014-07-29 21:34:13 -04005561 lockdep_assert_held(&clp->cl_lock);
5562
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005563 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005564 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005565 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07005566 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005567 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005568 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005570 stp->st_openstp = open_stp;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005571 mutex_init(&stp->st_mutex);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005572 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005573 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005574 spin_lock(&fp->fi_lock);
5575 list_add(&stp->st_perfile, &fp->fi_stateids);
5576 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577}
5578
Jeff Laytonc53530d2014-06-30 11:48:39 -04005579static struct nfs4_ol_stateid *
5580find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5581{
5582 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005583 struct nfs4_client *clp = lo->lo_owner.so_client;
5584
5585 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005586
5587 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005588 if (lst->st_stid.sc_file == fp) {
5589 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005590 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005591 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005592 }
5593 return NULL;
5594}
5595
Jeff Layton356a95e2014-07-29 21:34:13 -04005596static struct nfs4_ol_stateid *
5597find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5598 struct inode *inode, struct nfs4_ol_stateid *ost,
5599 bool *new)
5600{
5601 struct nfs4_stid *ns = NULL;
5602 struct nfs4_ol_stateid *lst;
5603 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5604 struct nfs4_client *clp = oo->oo_owner.so_client;
5605
5606 spin_lock(&clp->cl_lock);
5607 lst = find_lock_stateid(lo, fi);
5608 if (lst == NULL) {
5609 spin_unlock(&clp->cl_lock);
Kinglong Meed19fb702017-01-18 19:04:42 +08005610 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
Jeff Layton356a95e2014-07-29 21:34:13 -04005611 if (ns == NULL)
5612 return NULL;
5613
5614 spin_lock(&clp->cl_lock);
5615 lst = find_lock_stateid(lo, fi);
5616 if (likely(!lst)) {
5617 lst = openlockstateid(ns);
5618 init_lock_stateid(lst, lo, fi, inode, ost);
5619 ns = NULL;
5620 *new = true;
5621 }
5622 }
5623 spin_unlock(&clp->cl_lock);
5624 if (ns)
5625 nfs4_put_stid(ns);
5626 return lst;
5627}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005628
NeilBrownfd39ca92005-06-23 22:04:03 -07005629static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630check_lock_length(u64 offset, u64 length)
5631{
Kinglong Meee7969312015-07-13 17:34:19 +08005632 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5633 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634}
5635
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005636static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005637{
Trond Myklebust11b91642014-07-29 21:34:08 -04005638 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005639
Jeff Layton7214e862014-07-10 14:07:33 -04005640 lockdep_assert_held(&fp->fi_lock);
5641
Jeff Layton82c5ff12012-05-11 09:45:13 -04005642 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005643 return;
Jeff Layton12659652014-07-10 14:07:28 -04005644 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005645 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005646}
5647
Jeff Layton356a95e2014-07-29 21:34:13 -04005648static __be32
5649lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5650 struct nfs4_ol_stateid *ost,
5651 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04005652 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005653{
Jeff Layton5db1c032014-07-29 21:34:28 -04005654 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005655 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005656 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5657 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00005658 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005659 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04005660 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005661 unsigned int strhashval;
Jeff Laytondd257932016-08-11 10:37:39 -04005662 bool hashed;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005663
Kinglong Meec8623992015-07-13 17:35:37 +08005664 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005665 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005666 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005667 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5668 if (lo == NULL)
5669 return nfserr_jukebox;
5670 } else {
5671 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005672 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005673 if (!cstate->minorversion &&
5674 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005675 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005676 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005677
Jeff Laytondd257932016-08-11 10:37:39 -04005678retry:
5679 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5680 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005681 status = nfserr_jukebox;
5682 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005683 }
Jeff Laytondd257932016-08-11 10:37:39 -04005684
5685 mutex_lock(&lst->st_mutex);
5686
5687 /* See if it's still hashed to avoid race with FREE_STATEID */
5688 spin_lock(&cl->cl_lock);
5689 hashed = !list_empty(&lst->st_perfile);
5690 spin_unlock(&cl->cl_lock);
5691
5692 if (!hashed) {
5693 mutex_unlock(&lst->st_mutex);
5694 nfs4_put_stid(&lst->st_stid);
5695 goto retry;
5696 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005697 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04005698 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04005699out:
5700 nfs4_put_stateowner(&lo->lo_owner);
5701 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005702}
5703
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704/*
5705 * LOCK operation
5706 */
Al Virob37ad282006-10-19 23:28:59 -07005707__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005708nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005709 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005711 struct nfs4_openowner *open_sop = NULL;
5712 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005713 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005714 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005715 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005716 struct file *filp = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04005717 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005718 struct file_lock *file_lock = NULL;
5719 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005720 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005721 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005722 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005723 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04005724 unsigned char fl_type;
5725 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005726 struct net *net = SVC_NET(rqstp);
5727 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728
5729 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5730 (long long) lock->lk_offset,
5731 (long long) lock->lk_length);
5732
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 if (check_lock_length(lock->lk_offset, lock->lk_length))
5734 return nfserr_inval;
5735
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005736 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005737 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005738 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5739 return status;
5740 }
5741
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005743 if (nfsd4_has_session(cstate))
5744 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005745 memcpy(&lock->lk_new_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04005746 &cstate->session->se_client->cl_clientid,
5747 sizeof(clientid_t));
5748
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005750 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005754 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 lock->lk_new_open_seqid,
5756 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005757 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005758 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005760 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005761 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005762 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005763 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005764 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005765 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005766 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005767 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005768 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005769 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005770 lock->lk_old_lock_seqid,
5771 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005772 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005773 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005774 if (status)
5775 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005776 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005778 lkflg = setlkflg(lock->lk_type);
5779 status = nfs4_check_openmode(lock_stp, lkflg);
5780 if (status)
5781 goto out;
5782
NeilBrown0dd395d2005-07-07 17:59:15 -07005783 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005784 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005785 goto out;
5786 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005787 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005788 goto out;
5789
Trond Myklebust11b91642014-07-29 21:34:08 -04005790 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 case NFS4_READW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04005793 if (nfsd4_has_session(cstate))
5794 fl_flags |= FL_SLEEP;
5795 /* Fallthrough */
5796 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005797 spin_lock(&fp->fi_lock);
5798 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005799 if (filp)
5800 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005801 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005802 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005803 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 case NFS4_WRITEW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04005805 if (nfsd4_has_session(cstate))
5806 fl_flags |= FL_SLEEP;
5807 /* Fallthrough */
5808 case NFS4_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005809 spin_lock(&fp->fi_lock);
5810 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005811 if (filp)
5812 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005813 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005814 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005815 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 default:
5817 status = nfserr_inval;
5818 goto out;
5819 }
Jeff Layton76d348f2016-09-16 16:28:24 -04005820
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005821 if (!filp) {
5822 status = nfserr_openmode;
5823 goto out;
5824 }
Kinglong Meeaef95832014-08-22 10:18:44 -04005825
Jeff Layton76d348f2016-09-16 16:28:24 -04005826 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
5827 if (!nbl) {
5828 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
5829 status = nfserr_jukebox;
5830 goto out;
5831 }
5832
5833 file_lock = &nbl->nbl_lock;
5834 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04005835 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04005836 file_lock->fl_pid = current->tgid;
5837 file_lock->fl_file = filp;
Jeff Layton76d348f2016-09-16 16:28:24 -04005838 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04005839 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5840 file_lock->fl_start = lock->lk_offset;
5841 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5842 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843
Jeff Layton21179d82012-08-21 08:03:32 -04005844 conflock = locks_alloc_lock();
5845 if (!conflock) {
5846 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5847 status = nfserr_jukebox;
5848 goto out;
5849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850
Jeff Layton76d348f2016-09-16 16:28:24 -04005851 if (fl_flags & FL_SLEEP) {
Jeff Layton7919d0a2016-09-16 16:28:25 -04005852 nbl->nbl_time = jiffies;
Jeff Layton0cc11a62016-10-20 09:34:31 -04005853 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005854 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005855 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04005856 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005857 }
5858
Jeff Layton21179d82012-08-21 08:03:32 -04005859 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005860 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04005862 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07005863 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005864 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04005865 case FILE_LOCK_DEFERRED:
5866 nbl = NULL;
5867 /* Fallthrough */
5868 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005869 status = nfserr_denied;
5870 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005871 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005872 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04005873 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005875 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005876 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005877 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005878 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005879 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881out:
Jeff Layton76d348f2016-09-16 16:28:24 -04005882 if (nbl) {
5883 /* dequeue it if we queued it before */
5884 if (fl_flags & FL_SLEEP) {
Jeff Layton0cc11a62016-10-20 09:34:31 -04005885 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005886 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005887 list_del_init(&nbl->nbl_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04005888 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005889 }
5890 free_blocked_lock(nbl);
5891 }
Trond Myklebustde186432014-07-10 14:07:26 -04005892 if (filp)
5893 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005894 if (lock_stp) {
5895 /* Bump seqid manually if the 4.0 replay owner is openowner */
5896 if (cstate->replay_owner &&
5897 cstate->replay_owner != &lock_sop->lo_owner &&
5898 seqid_mutating_err(ntohl(status)))
5899 lock_sop->lo_owner.so_seqid++;
5900
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005901 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005902
Jeff Layton5db1c032014-07-29 21:34:28 -04005903 /*
5904 * If this is a new, never-before-used stateid, and we are
5905 * returning an error, then just go ahead and release it.
5906 */
5907 if (status && new)
5908 release_lock_stateid(lock_stp);
5909
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005910 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005911 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005912 if (open_stp)
5913 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005914 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005915 if (conflock)
5916 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 return status;
5918}
5919
5920/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005921 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5922 * so we do a temporary open here just to get an open file to pass to
5923 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5924 * inode operation.)
5925 */
Al Viro04da6e92012-04-13 00:00:04 -04005926static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005927{
5928 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005929 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5930 if (!err) {
5931 err = nfserrno(vfs_test_lock(file, lock));
Christoph Hellwigfd891452015-04-28 15:41:16 +02005932 fput(file);
Al Viro04da6e92012-04-13 00:00:04 -04005933 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005934 return err;
5935}
5936
5937/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 * LOCKT operation
5939 */
Al Virob37ad282006-10-19 23:28:59 -07005940__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005941nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5942 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943{
Jeff Layton21179d82012-08-21 08:03:32 -04005944 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005945 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005946 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005947 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005949 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 return nfserr_grace;
5951
5952 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5953 return nfserr_inval;
5954
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005955 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005956 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005957 if (status)
5958 goto out;
5959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005961 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963
Jeff Layton21179d82012-08-21 08:03:32 -04005964 file_lock = locks_alloc_lock();
5965 if (!file_lock) {
5966 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5967 status = nfserr_jukebox;
5968 goto out;
5969 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005970
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971 switch (lockt->lt_type) {
5972 case NFS4_READ_LT:
5973 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005974 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 break;
5976 case NFS4_WRITE_LT:
5977 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005978 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 break;
5980 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005981 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 status = nfserr_inval;
5983 goto out;
5984 }
5985
Kinglong Meec8623992015-07-13 17:35:37 +08005986 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005987 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005988 file_lock->fl_owner = (fl_owner_t)lo;
5989 file_lock->fl_pid = current->tgid;
5990 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991
Jeff Layton21179d82012-08-21 08:03:32 -04005992 file_lock->fl_start = lockt->lt_offset;
5993 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994
Jeff Layton21179d82012-08-21 08:03:32 -04005995 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996
Jeff Layton21179d82012-08-21 08:03:32 -04005997 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005998 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005999 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04006000
Jeff Layton21179d82012-08-21 08:03:32 -04006001 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04006003 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 }
6005out:
Jeff Layton5db1c032014-07-29 21:34:28 -04006006 if (lo)
6007 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04006008 if (file_lock)
6009 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 return status;
6011}
6012
Al Virob37ad282006-10-19 23:28:59 -07006013__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006014nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08006015 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006017 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006019 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006020 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07006021 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006022 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6023
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6025 (long long) locku->lu_offset,
6026 (long long) locku->lu_length);
6027
6028 if (check_lock_length(locku->lu_offset, locku->lu_length))
6029 return nfserr_inval;
6030
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006031 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006032 &locku->lu_stateid, NFS4_LOCK_STID,
6033 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006034 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04006036 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006037 if (!filp) {
6038 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04006039 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006040 }
Jeff Layton21179d82012-08-21 08:03:32 -04006041 file_lock = locks_alloc_lock();
6042 if (!file_lock) {
6043 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6044 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04006045 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04006046 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006047
Jeff Layton21179d82012-08-21 08:03:32 -04006048 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04006049 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04006050 file_lock->fl_pid = current->tgid;
6051 file_lock->fl_file = filp;
6052 file_lock->fl_flags = FL_POSIX;
6053 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6054 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055
Jeff Layton21179d82012-08-21 08:03:32 -04006056 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6057 locku->lu_length);
6058 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059
Jeff Layton21179d82012-08-21 08:03:32 -04006060 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07006061 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05006062 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 goto out_nfserr;
6064 }
Jeff Layton9767feb2015-10-01 09:05:50 -04006065 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Trond Myklebustde186432014-07-10 14:07:26 -04006066fput:
6067 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04006068put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006069 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04006070 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006072 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006073 if (file_lock)
6074 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 return status;
6076
6077out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07006078 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04006079 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080}
6081
6082/*
6083 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006084 * true: locks held by lockowner
6085 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006087static bool
6088check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006090 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006091 int status = false;
6092 struct file *filp = find_any_file(fp);
6093 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006094 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006095
6096 if (!filp) {
6097 /* Any valid lock stateid should have some sort of access */
6098 WARN_ON_ONCE(1);
6099 return status;
6100 }
6101
6102 inode = file_inode(filp);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006103 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006105 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05006106 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006107 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6108 if (fl->fl_owner == (fl_owner_t)lowner) {
6109 status = true;
6110 break;
6111 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08006112 }
Jeff Layton6109c852015-01-16 15:05:57 -05006113 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114 }
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006115 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 return status;
6117}
6118
Al Virob37ad282006-10-19 23:28:59 -07006119__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08006120nfsd4_release_lockowner(struct svc_rqst *rqstp,
6121 struct nfsd4_compound_state *cstate,
6122 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123{
6124 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04006125 struct nfs4_stateowner *sop;
6126 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006127 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006129 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07006130 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006131 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006132 struct nfs4_client *clp;
Chuck Lever88584812016-07-13 16:40:14 -04006133 LIST_HEAD (reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134
6135 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6136 clid->cl_boot, clid->cl_id);
6137
Jeff Layton4b24ca72014-06-30 11:48:44 -04006138 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006139 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04006140 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006141
Trond Myklebustd4f04892014-07-29 21:34:36 -04006142 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04006143 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04006144 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04006145 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04006146 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04006147
6148 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006149 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04006150
Jeff Layton882e9d22014-07-29 21:34:37 -04006151 /* see if there are still any locks associated with it */
6152 lo = lockowner(sop);
6153 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6154 if (check_for_locks(stp->st_stid.sc_file, lo)) {
6155 status = nfserr_locks_held;
6156 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04006157 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04006158 }
Jeff Layton5adfd882014-07-29 21:34:31 -04006159 }
Jeff Layton882e9d22014-07-29 21:34:37 -04006160
Kinglong Meeb5971af2014-08-22 10:18:43 -04006161 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04006162 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04006163 }
Chuck Lever88584812016-07-13 16:40:14 -04006164 if (!lo) {
6165 spin_unlock(&clp->cl_lock);
6166 return status;
6167 }
6168
6169 unhash_lockowner_locked(lo);
6170 while (!list_empty(&lo->lo_owner.so_stateids)) {
6171 stp = list_first_entry(&lo->lo_owner.so_stateids,
6172 struct nfs4_ol_stateid,
6173 st_perstateowner);
6174 WARN_ON(!unhash_lock_stateid(stp));
6175 put_ol_stateid_locked(stp, &reaplist);
6176 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04006177 spin_unlock(&clp->cl_lock);
Chuck Lever88584812016-07-13 16:40:14 -04006178 free_ol_stateid_reaplist(&reaplist);
6179 nfs4_put_stateowner(&lo->lo_owner);
6180
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 return status;
6182}
6183
6184static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07006185alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186{
NeilBrowna55370a2005-06-23 22:03:52 -07006187 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188}
6189
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006190bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006191nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07006192{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006193 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07006194
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006195 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006196 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07006197}
6198
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199/*
6200 * failure => all reset bets are off, nfserr_no_grace...
6201 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05006202struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006203nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204{
6205 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006206 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207
NeilBrowna55370a2005-06-23 22:03:52 -07006208 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
6209 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05006210 if (crp) {
6211 strhashval = clientstr_hashval(name);
6212 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006213 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05006214 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006215 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006216 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006217 }
6218 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219}
6220
Jeff Layton2a4317c2012-03-21 16:42:43 -04006221void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006222nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05006223{
6224 list_del(&crp->cr_strhash);
6225 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006226 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05006227}
6228
6229void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006230nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231{
6232 struct nfs4_client_reclaim *crp = NULL;
6233 int i;
6234
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006236 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6237 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006239 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240 }
6241 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006242 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243}
6244
6245/*
6246 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04006247struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006248nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249{
6250 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251 struct nfs4_client_reclaim *crp = NULL;
6252
Jeff Layton278c9312012-11-12 15:00:52 -05006253 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254
Jeff Layton278c9312012-11-12 15:00:52 -05006255 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006256 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05006257 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258 return crp;
6259 }
6260 }
6261 return NULL;
6262}
6263
6264/*
6265* Called from OPEN. Look for clientid in reclaim list.
6266*/
Al Virob37ad282006-10-19 23:28:59 -07006267__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006268nfs4_check_open_reclaim(clientid_t *clid,
6269 struct nfsd4_compound_state *cstate,
6270 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006272 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04006273
6274 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006275 status = lookup_clientid(clid, cstate, nn);
6276 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04006277 return nfserr_reclaim_bad;
6278
Jeff Layton3b3e7b72014-09-12 16:40:22 -04006279 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6280 return nfserr_no_grace;
6281
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006282 if (nfsd4_client_record_check(cstate->clp))
6283 return nfserr_reclaim_bad;
6284
6285 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286}
6287
Bryan Schumaker65178db2011-11-01 13:35:21 -04006288#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04006289static inline void
6290put_client(struct nfs4_client *clp)
6291{
6292 atomic_dec(&clp->cl_refcount);
6293}
6294
Jeff Layton285abde2014-07-30 08:27:24 -04006295static struct nfs4_client *
6296nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6297{
6298 struct nfs4_client *clp;
6299 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6300 nfsd_net_id);
6301
6302 if (!nfsd_netns_ready(nn))
6303 return NULL;
6304
6305 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6306 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
6307 return clp;
6308 }
6309 return NULL;
6310}
6311
Jeff Layton7ec0e362014-07-30 08:27:17 -04006312u64
Jeff Layton285abde2014-07-30 08:27:24 -04006313nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04006314{
6315 struct nfs4_client *clp;
6316 u64 count = 0;
6317 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6318 nfsd_net_id);
6319 char buf[INET6_ADDRSTRLEN];
6320
6321 if (!nfsd_netns_ready(nn))
6322 return 0;
6323
6324 spin_lock(&nn->client_lock);
6325 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6326 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6327 pr_info("NFS Client: %s\n", buf);
6328 ++count;
6329 }
6330 spin_unlock(&nn->client_lock);
6331
6332 return count;
6333}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006334
Jeff Laytona0926d12014-07-30 08:27:18 -04006335u64
Jeff Layton285abde2014-07-30 08:27:24 -04006336nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04006337{
6338 u64 count = 0;
6339 struct nfs4_client *clp;
6340 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6341 nfsd_net_id);
6342
6343 if (!nfsd_netns_ready(nn))
6344 return count;
6345
6346 spin_lock(&nn->client_lock);
6347 clp = nfsd_find_client(addr, addr_size);
6348 if (clp) {
6349 if (mark_client_expired_locked(clp) == nfs_ok)
6350 ++count;
6351 else
6352 clp = NULL;
6353 }
6354 spin_unlock(&nn->client_lock);
6355
6356 if (clp)
6357 expire_client(clp);
6358
6359 return count;
6360}
6361
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006362u64
Jeff Layton285abde2014-07-30 08:27:24 -04006363nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006364{
6365 u64 count = 0;
6366 struct nfs4_client *clp, *next;
6367 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6368 nfsd_net_id);
6369 LIST_HEAD(reaplist);
6370
6371 if (!nfsd_netns_ready(nn))
6372 return count;
6373
6374 spin_lock(&nn->client_lock);
6375 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6376 if (mark_client_expired_locked(clp) == nfs_ok) {
6377 list_add(&clp->cl_lru, &reaplist);
6378 if (max != 0 && ++count >= max)
6379 break;
6380 }
6381 }
6382 spin_unlock(&nn->client_lock);
6383
6384 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6385 expire_client(clp);
6386
6387 return count;
6388}
6389
Bryan Schumaker184c1842012-11-29 11:40:44 -05006390static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6391 const char *type)
6392{
6393 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05006394 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05006395 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6396}
6397
Jeff Layton016200c2014-07-30 08:27:21 -04006398static void
6399nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6400 struct list_head *collect)
6401{
6402 struct nfs4_client *clp = lst->st_stid.sc_client;
6403 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6404 nfsd_net_id);
6405
6406 if (!collect)
6407 return;
6408
6409 lockdep_assert_held(&nn->client_lock);
6410 atomic_inc(&clp->cl_refcount);
6411 list_add(&lst->st_locks, collect);
6412}
6413
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006414static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04006415 struct list_head *collect,
Jeff Laytone8568732015-08-24 12:41:47 -04006416 bool (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05006417{
6418 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006419 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006420 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006421 u64 count = 0;
6422
Jeff Layton016200c2014-07-30 08:27:21 -04006423 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006424 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006425 list_for_each_entry_safe(stp, st_next,
6426 &oop->oo_owner.so_stateids, st_perstateowner) {
6427 list_for_each_entry_safe(lst, lst_next,
6428 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04006429 if (func) {
Jeff Laytone8568732015-08-24 12:41:47 -04006430 if (func(lst))
6431 nfsd_inject_add_lock_to_list(lst,
6432 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04006433 }
Jeff Layton016200c2014-07-30 08:27:21 -04006434 ++count;
6435 /*
6436 * Despite the fact that these functions deal
6437 * with 64-bit integers for "count", we must
6438 * ensure that it doesn't blow up the
6439 * clp->cl_refcount. Throw a warning if we
6440 * start to approach INT_MAX here.
6441 */
6442 WARN_ON_ONCE(count == (INT_MAX / 2));
6443 if (count == max)
6444 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006445 }
6446 }
6447 }
Jeff Layton016200c2014-07-30 08:27:21 -04006448out:
6449 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006450
6451 return count;
6452}
6453
Jeff Layton016200c2014-07-30 08:27:21 -04006454static u64
6455nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6456 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05006457{
Jeff Layton016200c2014-07-30 08:27:21 -04006458 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006459}
6460
Jeff Layton016200c2014-07-30 08:27:21 -04006461static u64
6462nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006463{
Jeff Layton016200c2014-07-30 08:27:21 -04006464 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006465 nfsd_print_count(clp, count, "locked files");
6466 return count;
6467}
6468
Jeff Layton016200c2014-07-30 08:27:21 -04006469u64
Jeff Layton285abde2014-07-30 08:27:24 -04006470nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04006471{
6472 struct nfs4_client *clp;
6473 u64 count = 0;
6474 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6475 nfsd_net_id);
6476
6477 if (!nfsd_netns_ready(nn))
6478 return 0;
6479
6480 spin_lock(&nn->client_lock);
6481 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6482 count += nfsd_print_client_locks(clp);
6483 spin_unlock(&nn->client_lock);
6484
6485 return count;
6486}
6487
6488static void
6489nfsd_reap_locks(struct list_head *reaplist)
6490{
6491 struct nfs4_client *clp;
6492 struct nfs4_ol_stateid *stp, *next;
6493
6494 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6495 list_del_init(&stp->st_locks);
6496 clp = stp->st_stid.sc_client;
6497 nfs4_put_stid(&stp->st_stid);
6498 put_client(clp);
6499 }
6500}
6501
6502u64
Jeff Layton285abde2014-07-30 08:27:24 -04006503nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04006504{
6505 unsigned int count = 0;
6506 struct nfs4_client *clp;
6507 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6508 nfsd_net_id);
6509 LIST_HEAD(reaplist);
6510
6511 if (!nfsd_netns_ready(nn))
6512 return count;
6513
6514 spin_lock(&nn->client_lock);
6515 clp = nfsd_find_client(addr, addr_size);
6516 if (clp)
6517 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6518 spin_unlock(&nn->client_lock);
6519 nfsd_reap_locks(&reaplist);
6520 return count;
6521}
6522
6523u64
Jeff Layton285abde2014-07-30 08:27:24 -04006524nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04006525{
6526 u64 count = 0;
6527 struct nfs4_client *clp;
6528 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6529 nfsd_net_id);
6530 LIST_HEAD(reaplist);
6531
6532 if (!nfsd_netns_ready(nn))
6533 return count;
6534
6535 spin_lock(&nn->client_lock);
6536 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6537 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6538 if (max != 0 && count >= max)
6539 break;
6540 }
6541 spin_unlock(&nn->client_lock);
6542 nfsd_reap_locks(&reaplist);
6543 return count;
6544}
6545
Jeff Layton82e05ef2014-07-30 08:27:22 -04006546static u64
6547nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6548 struct list_head *collect,
6549 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006550{
6551 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04006552 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6553 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006554 u64 count = 0;
6555
Jeff Layton82e05ef2014-07-30 08:27:22 -04006556 lockdep_assert_held(&nn->client_lock);
6557
6558 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006559 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04006560 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006561 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04006562 if (collect) {
6563 atomic_inc(&clp->cl_refcount);
6564 list_add(&oop->oo_perclient, collect);
6565 }
6566 }
6567 ++count;
6568 /*
6569 * Despite the fact that these functions deal with
6570 * 64-bit integers for "count", we must ensure that
6571 * it doesn't blow up the clp->cl_refcount. Throw a
6572 * warning if we start to approach INT_MAX here.
6573 */
6574 WARN_ON_ONCE(count == (INT_MAX / 2));
6575 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006576 break;
6577 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04006578 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006579
6580 return count;
6581}
6582
Jeff Layton82e05ef2014-07-30 08:27:22 -04006583static u64
6584nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006585{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006586 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6587
6588 nfsd_print_count(clp, count, "openowners");
6589 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006590}
6591
Jeff Layton82e05ef2014-07-30 08:27:22 -04006592static u64
6593nfsd_collect_client_openowners(struct nfs4_client *clp,
6594 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006595{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006596 return nfsd_foreach_client_openowner(clp, max, collect,
6597 unhash_openowner_locked);
6598}
6599
6600u64
Jeff Layton285abde2014-07-30 08:27:24 -04006601nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006602{
6603 struct nfs4_client *clp;
6604 u64 count = 0;
6605 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6606 nfsd_net_id);
6607
6608 if (!nfsd_netns_ready(nn))
6609 return 0;
6610
6611 spin_lock(&nn->client_lock);
6612 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6613 count += nfsd_print_client_openowners(clp);
6614 spin_unlock(&nn->client_lock);
6615
6616 return count;
6617}
6618
6619static void
6620nfsd_reap_openowners(struct list_head *reaplist)
6621{
6622 struct nfs4_client *clp;
6623 struct nfs4_openowner *oop, *next;
6624
6625 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6626 list_del_init(&oop->oo_perclient);
6627 clp = oop->oo_owner.so_client;
6628 release_openowner(oop);
6629 put_client(clp);
6630 }
6631}
6632
6633u64
Jeff Layton285abde2014-07-30 08:27:24 -04006634nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6635 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006636{
6637 unsigned int count = 0;
6638 struct nfs4_client *clp;
6639 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6640 nfsd_net_id);
6641 LIST_HEAD(reaplist);
6642
6643 if (!nfsd_netns_ready(nn))
6644 return count;
6645
6646 spin_lock(&nn->client_lock);
6647 clp = nfsd_find_client(addr, addr_size);
6648 if (clp)
6649 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6650 spin_unlock(&nn->client_lock);
6651 nfsd_reap_openowners(&reaplist);
6652 return count;
6653}
6654
6655u64
Jeff Layton285abde2014-07-30 08:27:24 -04006656nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006657{
6658 u64 count = 0;
6659 struct nfs4_client *clp;
6660 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6661 nfsd_net_id);
6662 LIST_HEAD(reaplist);
6663
6664 if (!nfsd_netns_ready(nn))
6665 return count;
6666
6667 spin_lock(&nn->client_lock);
6668 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6669 count += nfsd_collect_client_openowners(clp, &reaplist,
6670 max - count);
6671 if (max != 0 && count >= max)
6672 break;
6673 }
6674 spin_unlock(&nn->client_lock);
6675 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006676 return count;
6677}
6678
Bryan Schumaker269de302012-11-29 11:40:42 -05006679static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6680 struct list_head *victims)
6681{
6682 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006683 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6684 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006685 u64 count = 0;
6686
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006687 lockdep_assert_held(&nn->client_lock);
6688
6689 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006690 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006691 if (victims) {
6692 /*
6693 * It's not safe to mess with delegations that have a
6694 * non-zero dl_time. They might have already been broken
6695 * and could be processed by the laundromat outside of
6696 * the state_lock. Just leave them be.
6697 */
6698 if (dp->dl_time != 0)
6699 continue;
6700
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006701 atomic_inc(&clp->cl_refcount);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04006702 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04006703 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006704 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006705 ++count;
6706 /*
6707 * Despite the fact that these functions deal with
6708 * 64-bit integers for "count", we must ensure that
6709 * it doesn't blow up the clp->cl_refcount. Throw a
6710 * warning if we start to approach INT_MAX here.
6711 */
6712 WARN_ON_ONCE(count == (INT_MAX / 2));
6713 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006714 break;
6715 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006716 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006717 return count;
6718}
6719
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006720static u64
6721nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006722{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006723 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006724
6725 nfsd_print_count(clp, count, "delegations");
6726 return count;
6727}
6728
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006729u64
Jeff Layton285abde2014-07-30 08:27:24 -04006730nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006731{
6732 struct nfs4_client *clp;
6733 u64 count = 0;
6734 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6735 nfsd_net_id);
6736
6737 if (!nfsd_netns_ready(nn))
6738 return 0;
6739
6740 spin_lock(&nn->client_lock);
6741 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6742 count += nfsd_print_client_delegations(clp);
6743 spin_unlock(&nn->client_lock);
6744
6745 return count;
6746}
6747
6748static void
6749nfsd_forget_delegations(struct list_head *reaplist)
6750{
6751 struct nfs4_client *clp;
6752 struct nfs4_delegation *dp, *next;
6753
6754 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6755 list_del_init(&dp->dl_recall_lru);
6756 clp = dp->dl_stid.sc_client;
6757 revoke_delegation(dp);
6758 put_client(clp);
6759 }
6760}
6761
6762u64
Jeff Layton285abde2014-07-30 08:27:24 -04006763nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6764 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006765{
6766 u64 count = 0;
6767 struct nfs4_client *clp;
6768 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6769 nfsd_net_id);
6770 LIST_HEAD(reaplist);
6771
6772 if (!nfsd_netns_ready(nn))
6773 return count;
6774
6775 spin_lock(&nn->client_lock);
6776 clp = nfsd_find_client(addr, addr_size);
6777 if (clp)
6778 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6779 spin_unlock(&nn->client_lock);
6780
6781 nfsd_forget_delegations(&reaplist);
6782 return count;
6783}
6784
6785u64
Jeff Layton285abde2014-07-30 08:27:24 -04006786nfsd_inject_forget_delegations(u64 max)
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 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6799 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6800 if (max != 0 && count >= max)
6801 break;
6802 }
6803 spin_unlock(&nn->client_lock);
6804 nfsd_forget_delegations(&reaplist);
6805 return count;
6806}
6807
6808static void
6809nfsd_recall_delegations(struct list_head *reaplist)
6810{
6811 struct nfs4_client *clp;
6812 struct nfs4_delegation *dp, *next;
6813
6814 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6815 list_del_init(&dp->dl_recall_lru);
6816 clp = dp->dl_stid.sc_client;
6817 /*
6818 * We skipped all entries that had a zero dl_time before,
6819 * so we can now reset the dl_time back to 0. If a delegation
6820 * break comes in now, then it won't make any difference since
6821 * we're recalling it either way.
6822 */
6823 spin_lock(&state_lock);
6824 dp->dl_time = 0;
6825 spin_unlock(&state_lock);
6826 nfsd_break_one_deleg(dp);
6827 put_client(clp);
6828 }
6829}
6830
6831u64
Jeff Layton285abde2014-07-30 08:27:24 -04006832nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006833 size_t addr_size)
6834{
6835 u64 count = 0;
6836 struct nfs4_client *clp;
6837 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6838 nfsd_net_id);
6839 LIST_HEAD(reaplist);
6840
6841 if (!nfsd_netns_ready(nn))
6842 return count;
6843
6844 spin_lock(&nn->client_lock);
6845 clp = nfsd_find_client(addr, addr_size);
6846 if (clp)
6847 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6848 spin_unlock(&nn->client_lock);
6849
6850 nfsd_recall_delegations(&reaplist);
6851 return count;
6852}
6853
6854u64
Jeff Layton285abde2014-07-30 08:27:24 -04006855nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006856{
6857 u64 count = 0;
6858 struct nfs4_client *clp, *next;
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 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6868 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6869 if (max != 0 && ++count >= max)
6870 break;
6871 }
6872 spin_unlock(&nn->client_lock);
6873 nfsd_recall_delegations(&reaplist);
6874 return count;
6875}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006876#endif /* CONFIG_NFSD_FAULT_INJECTION */
6877
Meelap Shahc2f1a552007-07-17 04:04:39 -07006878/*
6879 * Since the lifetime of a delegation isn't limited to that of an open, a
6880 * client may quite reasonably hang on to a delegation as long as it has
6881 * the inode cached. This becomes an obvious problem the first time a
6882 * client's inode cache approaches the size of the server's total memory.
6883 *
6884 * For now we avoid this problem by imposing a hard limit on the number
6885 * of delegations, which varies according to the server's memory size.
6886 */
6887static void
6888set_max_delegations(void)
6889{
6890 /*
6891 * Allow at most 4 delegations per megabyte of RAM. Quick
6892 * estimates suggest that in the worst case (where every delegation
6893 * is for a different inode), a delegation could take about 1.5K,
6894 * giving a worst case usage of about 6% of memory.
6895 */
6896 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6897}
6898
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006899static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006900{
6901 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6902 int i;
6903
6904 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6905 CLIENT_HASH_SIZE, GFP_KERNEL);
6906 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006907 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006908 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6909 CLIENT_HASH_SIZE, GFP_KERNEL);
6910 if (!nn->unconf_id_hashtbl)
6911 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006912 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6913 SESSION_HASH_SIZE, GFP_KERNEL);
6914 if (!nn->sessionid_hashtbl)
6915 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006916
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006917 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006918 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006919 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006920 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006921 for (i = 0; i < SESSION_HASH_SIZE; i++)
6922 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006923 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006924 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03006925 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03006926 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006927 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03006928 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006929
Jeff Layton0cc11a62016-10-20 09:34:31 -04006930 spin_lock_init(&nn->blocked_locks_lock);
6931 INIT_LIST_HEAD(&nn->blocked_locks_lru);
6932
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006933 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006934 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006935
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006936 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006937
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006938err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03006939 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006940err_unconf_id:
6941 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006942err:
6943 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006944}
6945
6946static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006947nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006948{
6949 int i;
6950 struct nfs4_client *clp = NULL;
6951 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6952
6953 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6954 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6955 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6956 destroy_client(clp);
6957 }
6958 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006959
Kinglong Mee2b905632014-03-26 22:09:30 +08006960 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6961 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6962 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6963 destroy_client(clp);
6964 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006965 }
6966
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006967 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006968 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006969 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006970 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006971}
6972
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006973int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006974nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006975{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006976 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006977 int ret;
6978
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006979 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006980 if (ret)
6981 return ret;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006982 nn->boot_time = get_seconds();
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006983 nn->grace_ended = false;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04006984 nn->nfsd4_manager.block_opens = true;
Jeff Laytond4318ac2014-09-12 16:40:21 -04006985 locks_start_grace(net, &nn->nfsd4_manager);
6986 nfsd4_client_tracking_init(net);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006987 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006988 nn->nfsd4_grace, net);
6989 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006990 return 0;
6991}
6992
6993/* initialization to perform when the nfsd service is started: */
6994
6995int
6996nfs4_state_start(void)
6997{
6998 int ret;
6999
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007000 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007001 if (ret)
Kinglong Meef7d1ddb2017-02-05 09:57:07 +08007002 return ret;
7003
Jeff Layton51a54562015-08-20 07:17:01 -04007004 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05007005 if (laundry_wq == NULL) {
7006 ret = -ENOMEM;
Kinglong Meef7d1ddb2017-02-05 09:57:07 +08007007 goto out_cleanup_cred;
Jeff Laytona6d6b782012-03-05 11:42:36 -05007008 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007009 ret = nfsd4_create_callback_queue();
7010 if (ret)
7011 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007012
Meelap Shahc2f1a552007-07-17 04:04:39 -07007013 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007014 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007015
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007016out_free_laundry:
7017 destroy_workqueue(laundry_wq);
Kinglong Meef7d1ddb2017-02-05 09:57:07 +08007018out_cleanup_cred:
7019 cleanup_callback_cred();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007020 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021}
7022
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007023void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007024nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007025{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007028 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Jeff Layton7919d0a2016-09-16 16:28:25 -04007029 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007031 cancel_delayed_work_sync(&nn->laundromat_work);
7032 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05007033
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04007035 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007036 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007038 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007039 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040 }
Benny Halevycdc975052014-05-30 09:09:30 -04007041 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042 list_for_each_safe(pos, next, &reaplist) {
7043 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04007044 list_del_init(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +02007045 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -04007046 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04007047 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048 }
7049
Jeff Layton7919d0a2016-09-16 16:28:25 -04007050 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04007051 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04007052 while (!list_empty(&nn->blocked_locks_lru)) {
7053 nbl = list_first_entry(&nn->blocked_locks_lru,
7054 struct nfsd4_blocked_lock, nbl_lru);
7055 list_move(&nbl->nbl_lru, &reaplist);
7056 list_del_init(&nbl->nbl_list);
7057 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04007058 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04007059
7060 while (!list_empty(&reaplist)) {
7061 nbl = list_first_entry(&nn->blocked_locks_lru,
7062 struct nfsd4_blocked_lock, nbl_lru);
7063 list_del_init(&nbl->nbl_lru);
7064 posix_unblock_lock(&nbl->nbl_lock);
7065 free_blocked_lock(nbl);
7066 }
7067
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007068 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007069 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070}
7071
7072void
7073nfs4_state_shutdown(void)
7074{
NeilBrown5e8d5c22006-04-10 22:55:37 -07007075 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04007076 nfsd4_destroy_callback_queue();
Kinglong Meef7d1ddb2017-02-05 09:57:07 +08007077 cleanup_callback_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007078}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007079
7080static void
7081get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7082{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007083 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
7084 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007085}
7086
7087static void
7088put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7089{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007090 if (cstate->minorversion) {
7091 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7092 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7093 }
7094}
7095
7096void
7097clear_current_stateid(struct nfsd4_compound_state *cstate)
7098{
7099 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007100}
7101
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007102/*
7103 * functions to set current state id
7104 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007105void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007106nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7107 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007108{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007109 put_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007110}
7111
7112void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007113nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7114 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007115{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007116 put_stateid(cstate, &u->open.op_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007117}
7118
7119void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007120nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7121 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007122{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007123 put_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007124}
7125
7126void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007127nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7128 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007129{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007130 put_stateid(cstate, &u->lock.lk_resp_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007131}
7132
7133/*
7134 * functions to consume current state id
7135 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007136
7137void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007138nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
7139{
7140 get_stateid(cstate, &odp->od_stateid);
7141}
7142
7143void
7144nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
7145{
7146 get_stateid(cstate, &drp->dr_stateid);
7147}
7148
7149void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007150nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
7151{
7152 get_stateid(cstate, &fsp->fr_stateid);
7153}
7154
7155void
7156nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
7157{
7158 get_stateid(cstate, &setattr->sa_stateid);
7159}
7160
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007161void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007162nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
7163{
7164 get_stateid(cstate, &close->cl_stateid);
7165}
7166
7167void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007168nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007169{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007170 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007171}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007172
7173void
7174nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
7175{
7176 get_stateid(cstate, &read->rd_stateid);
7177}
7178
7179void
7180nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
7181{
7182 get_stateid(cstate, &write->wr_stateid);
7183}