blob: 799c18cba6a5db43b1ca3d6b046b2cb13d1dec90 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Chuck Lever7df302f2012-05-29 13:56:37 -040041#include <linux/ratelimit.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050042#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton59766872013-02-04 12:50:00 -050043#include <linux/sunrpc/addr.h>
Daniel Borkmann87545892014-12-10 16:33:11 +010044#include <linux/jhash.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020045#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040046#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050047#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040048#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040050#include "netns.h"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020051#include "pnfs.h"
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define NFSDDBG_FACILITY NFSDDBG_PROC
54
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050055#define all_ones {{~0,~0},~0}
56static const stateid_t one_stateid = {
57 .si_generation = ~0,
58 .si_opaque = all_ones,
59};
60static const stateid_t zero_stateid = {
61 /* all fields zero */
62};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010063static const stateid_t currentstateid = {
64 .si_generation = 1,
65};
Trond Myklebust5cd35862017-11-03 08:00:12 -040066static const stateid_t close_stateid = {
67 .si_generation = 0xffffffffU,
68};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050069
Andy Adamsonec6b5d72009-04-03 08:28:28 +030070static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070071
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050072#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
73#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010074#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040077static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040078static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
J. Bruce Fields8b671b82009-02-22 14:51:34 -080080/* Locking: */
81
J. Bruce Fields8b671b82009-02-22 14:51:34 -080082/*
83 * Currently used for the del_recall_lru and file hash table. In an
84 * effort to decrease the scope of the client_mutex, this spinlock may
85 * eventually cover more:
86 */
Benny Halevycdc97502014-05-30 09:09:30 -040087static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080088
Jeff Laytonb401be222014-07-29 21:34:33 -040089/*
90 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
91 * the refcount on the open stateid to drop.
92 */
93static DECLARE_WAIT_QUEUE_HEAD(close_wq);
94
Christoph Hellwigabf11352014-05-21 07:43:03 -070095static struct kmem_cache *openowner_slab;
96static struct kmem_cache *lockowner_slab;
97static struct kmem_cache *file_slab;
98static struct kmem_cache *stateid_slab;
99static struct kmem_cache *deleg_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +0200100static struct kmem_cache *odstate_slab;
NeilBrowne60d4392005-06-23 22:03:01 -0700101
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400102static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800103
Julia Lawallc4cb8972015-11-21 22:57:39 +0100104static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
Jeff Layton76d348f2016-09-16 16:28:24 -0400105static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200106
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400107static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800108{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400109 return ses->se_flags & NFS4_SESSION_DEAD;
110}
111
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400112static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
113{
114 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400115 return nfserr_jukebox;
116 ses->se_flags |= NFS4_SESSION_DEAD;
117 return nfs_ok;
118}
119
J. Bruce Fields221a6872013-04-01 22:23:49 -0400120static bool is_client_expired(struct nfs4_client *clp)
121{
122 return clp->cl_time == 0;
123}
124
J. Bruce Fields221a6872013-04-01 22:23:49 -0400125static __be32 get_client_locked(struct nfs4_client *clp)
126{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400127 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
128
129 lockdep_assert_held(&nn->client_lock);
130
J. Bruce Fields221a6872013-04-01 22:23:49 -0400131 if (is_client_expired(clp))
132 return nfserr_expired;
133 atomic_inc(&clp->cl_refcount);
134 return nfs_ok;
135}
136
137/* must be called under the client_lock */
138static inline void
139renew_client_locked(struct nfs4_client *clp)
140{
141 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
142
143 if (is_client_expired(clp)) {
144 WARN_ON(1);
145 printk("%s: client (clientid %08x/%08x) already expired\n",
146 __func__,
147 clp->cl_clientid.cl_boot,
148 clp->cl_clientid.cl_id);
149 return;
150 }
151
152 dprintk("renewing client (clientid %08x/%08x)\n",
153 clp->cl_clientid.cl_boot,
154 clp->cl_clientid.cl_id);
155 list_move_tail(&clp->cl_lru, &nn->client_lru);
156 clp->cl_time = get_seconds();
157}
158
Fengguang Wuba138432013-04-16 22:14:15 -0400159static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400160{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400161 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
162
163 lockdep_assert_held(&nn->client_lock);
164
J. Bruce Fields221a6872013-04-01 22:23:49 -0400165 if (!atomic_dec_and_test(&clp->cl_refcount))
166 return;
167 if (!is_client_expired(clp))
168 renew_client_locked(clp);
169}
170
Jeff Layton4b24ca72014-06-30 11:48:44 -0400171static void put_client_renew(struct nfs4_client *clp)
172{
173 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
174
Jeff Laytond6c249b2014-07-08 14:02:50 -0400175 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
176 return;
177 if (!is_client_expired(clp))
178 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400179 spin_unlock(&nn->client_lock);
180}
181
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400182static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
183{
184 __be32 status;
185
186 if (is_session_dead(ses))
187 return nfserr_badsession;
188 status = get_client_locked(ses->se_client);
189 if (status)
190 return status;
191 atomic_inc(&ses->se_ref);
192 return nfs_ok;
193}
194
195static void nfsd4_put_session_locked(struct nfsd4_session *ses)
196{
197 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400198 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
199
200 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400201
202 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
203 free_session(ses);
204 put_client_renew_locked(clp);
205}
206
207static void nfsd4_put_session(struct nfsd4_session *ses)
208{
209 struct nfs4_client *clp = ses->se_client;
210 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
211
212 spin_lock(&nn->client_lock);
213 nfsd4_put_session_locked(ses);
214 spin_unlock(&nn->client_lock);
215}
216
Jeff Layton76d348f2016-09-16 16:28:24 -0400217static struct nfsd4_blocked_lock *
218find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
219 struct nfsd_net *nn)
220{
221 struct nfsd4_blocked_lock *cur, *found = NULL;
222
Jeff Layton0cc11a62016-10-20 09:34:31 -0400223 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400224 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
225 if (fh_match(fh, &cur->nbl_fh)) {
226 list_del_init(&cur->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -0400227 list_del_init(&cur->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400228 found = cur;
229 break;
230 }
231 }
Jeff Layton0cc11a62016-10-20 09:34:31 -0400232 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400233 if (found)
234 posix_unblock_lock(&found->nbl_lock);
235 return found;
236}
237
238static struct nfsd4_blocked_lock *
239find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
240 struct nfsd_net *nn)
241{
242 struct nfsd4_blocked_lock *nbl;
243
244 nbl = find_blocked_lock(lo, fh, nn);
245 if (!nbl) {
246 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
247 if (nbl) {
248 fh_copy_shallow(&nbl->nbl_fh, fh);
249 locks_init_lock(&nbl->nbl_lock);
250 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
251 &nfsd4_cb_notify_lock_ops,
252 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
253 }
254 }
255 return nbl;
256}
257
258static void
259free_blocked_lock(struct nfsd4_blocked_lock *nbl)
260{
261 locks_release_private(&nbl->nbl_lock);
262 kfree(nbl);
263}
264
265static int
266nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
267{
268 /*
269 * Since this is just an optimization, we don't try very hard if it
270 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
271 * just quit trying on anything else.
272 */
273 switch (task->tk_status) {
274 case -NFS4ERR_DELAY:
275 rpc_delay(task, 1 * HZ);
276 return 0;
277 default:
278 return 1;
279 }
280}
281
282static void
283nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
284{
285 struct nfsd4_blocked_lock *nbl = container_of(cb,
286 struct nfsd4_blocked_lock, nbl_cb);
287
288 free_blocked_lock(nbl);
289}
290
291static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
292 .done = nfsd4_cb_notify_lock_done,
293 .release = nfsd4_cb_notify_lock_release,
294};
295
Kinglong Meeb5971af2014-08-22 10:18:43 -0400296static inline struct nfs4_stateowner *
297nfs4_get_stateowner(struct nfs4_stateowner *sop)
298{
299 atomic_inc(&sop->so_count);
300 return sop;
301}
302
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400303static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400304same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400305{
306 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400307 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400308}
309
310static struct nfs4_openowner *
311find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400312 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400313{
314 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400315
Trond Myklebustd4f04892014-07-29 21:34:36 -0400316 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400317
Trond Myklebustd4f04892014-07-29 21:34:36 -0400318 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
319 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400320 if (!so->so_is_open_owner)
321 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400322 if (same_owner_str(so, &open->op_owner))
323 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400324 }
325 return NULL;
326}
327
328static struct nfs4_openowner *
329find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400330 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400331{
332 struct nfs4_openowner *oo;
333
Trond Myklebustd4f04892014-07-29 21:34:36 -0400334 spin_lock(&clp->cl_lock);
335 oo = find_openstateowner_str_locked(hashval, open, clp);
336 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400337 return oo;
338}
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340static inline u32
341opaque_hashval(const void *ptr, int nbytes)
342{
343 unsigned char *cptr = (unsigned char *) ptr;
344
345 u32 x = 0;
346 while (nbytes--) {
347 x *= 37;
348 x += *cptr++;
349 }
350 return x;
351}
352
Jeff Layton5b095e92014-10-23 08:01:02 -0400353static void nfsd4_free_file_rcu(struct rcu_head *rcu)
J. Bruce Fields32513b42011-10-13 16:00:16 -0400354{
Jeff Layton5b095e92014-10-23 08:01:02 -0400355 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
356
357 kmem_cache_free(file_slab, fp);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400358}
359
Christoph Hellwige6ba76e2014-08-14 08:50:16 +0200360void
NeilBrown13cd2182005-06-23 22:03:10 -0700361put_nfs4_file(struct nfs4_file *fi)
362{
Jeff Layton02e12152014-07-16 10:31:57 -0400363 might_lock(&state_lock);
364
Benny Halevycdc97502014-05-30 09:09:30 -0400365 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton5b095e92014-10-23 08:01:02 -0400366 hlist_del_rcu(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400367 spin_unlock(&state_lock);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200368 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
Jeff Layton5b095e92014-10-23 08:01:02 -0400369 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
370 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800371 }
NeilBrown13cd2182005-06-23 22:03:10 -0700372}
373
Trond Myklebustde186432014-07-10 14:07:26 -0400374static struct file *
375__nfs4_get_fd(struct nfs4_file *f, int oflag)
376{
377 if (f->fi_fds[oflag])
378 return get_file(f->fi_fds[oflag]);
379 return NULL;
380}
381
382static struct file *
383find_writeable_file_locked(struct nfs4_file *f)
384{
385 struct file *ret;
386
387 lockdep_assert_held(&f->fi_lock);
388
389 ret = __nfs4_get_fd(f, O_WRONLY);
390 if (!ret)
391 ret = __nfs4_get_fd(f, O_RDWR);
392 return ret;
393}
394
395static struct file *
396find_writeable_file(struct nfs4_file *f)
397{
398 struct file *ret;
399
400 spin_lock(&f->fi_lock);
401 ret = find_writeable_file_locked(f);
402 spin_unlock(&f->fi_lock);
403
404 return ret;
405}
406
407static struct file *find_readable_file_locked(struct nfs4_file *f)
408{
409 struct file *ret;
410
411 lockdep_assert_held(&f->fi_lock);
412
413 ret = __nfs4_get_fd(f, O_RDONLY);
414 if (!ret)
415 ret = __nfs4_get_fd(f, O_RDWR);
416 return ret;
417}
418
419static struct file *
420find_readable_file(struct nfs4_file *f)
421{
422 struct file *ret;
423
424 spin_lock(&f->fi_lock);
425 ret = find_readable_file_locked(f);
426 spin_unlock(&f->fi_lock);
427
428 return ret;
429}
430
Christoph Hellwig4d227fc2014-08-17 07:40:00 -0500431struct file *
Trond Myklebustde186432014-07-10 14:07:26 -0400432find_any_file(struct nfs4_file *f)
433{
434 struct file *ret;
435
436 spin_lock(&f->fi_lock);
437 ret = __nfs4_get_fd(f, O_RDWR);
438 if (!ret) {
439 ret = __nfs4_get_fd(f, O_WRONLY);
440 if (!ret)
441 ret = __nfs4_get_fd(f, O_RDONLY);
442 }
443 spin_unlock(&f->fi_lock);
444 return ret;
445}
446
Trond Myklebust02a35082014-07-25 07:34:22 -0400447static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800448unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700449
450/*
451 * Open owner state (share locks)
452 */
453
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500454/* hash tables for lock and open owners */
455#define OWNER_HASH_BITS 8
456#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
457#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700458
Trond Myklebustd4f04892014-07-29 21:34:36 -0400459static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400460{
461 unsigned int ret;
462
463 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500464 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400465}
NeilBrownef0f3392006-04-10 22:55:41 -0700466
NeilBrownef0f3392006-04-10 22:55:41 -0700467/* hash table for nfs4_file */
468#define FILE_HASH_BITS 8
469#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800470
Trond Myklebustca943212014-07-23 16:17:39 -0400471static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400472{
Trond Myklebustca943212014-07-23 16:17:39 -0400473 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
474}
475
476static unsigned int file_hashval(struct knfsd_fh *fh)
477{
478 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
479}
480
Jeff Layton89876f82013-04-02 09:01:59 -0400481static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700482
Jeff Layton12659652014-07-10 14:07:28 -0400483static void
484__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400485{
Jeff Layton7214e862014-07-10 14:07:33 -0400486 lockdep_assert_held(&fp->fi_lock);
487
Jeff Layton12659652014-07-10 14:07:28 -0400488 if (access & NFS4_SHARE_ACCESS_WRITE)
489 atomic_inc(&fp->fi_access[O_WRONLY]);
490 if (access & NFS4_SHARE_ACCESS_READ)
491 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400492}
493
Jeff Layton12659652014-07-10 14:07:28 -0400494static __be32
495nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400496{
Jeff Layton7214e862014-07-10 14:07:33 -0400497 lockdep_assert_held(&fp->fi_lock);
498
Jeff Layton12659652014-07-10 14:07:28 -0400499 /* Does this access mode make sense? */
500 if (access & ~NFS4_SHARE_ACCESS_BOTH)
501 return nfserr_inval;
502
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400503 /* Does it conflict with a deny mode already set? */
504 if ((access & fp->fi_share_deny) != 0)
505 return nfserr_share_denied;
506
Jeff Layton12659652014-07-10 14:07:28 -0400507 __nfs4_file_get_access(fp, access);
508 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400509}
510
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400511static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
512{
513 /* Common case is that there is no deny mode. */
514 if (deny) {
515 /* Does this deny mode make sense? */
516 if (deny & ~NFS4_SHARE_DENY_BOTH)
517 return nfserr_inval;
518
519 if ((deny & NFS4_SHARE_DENY_READ) &&
520 atomic_read(&fp->fi_access[O_RDONLY]))
521 return nfserr_share_denied;
522
523 if ((deny & NFS4_SHARE_DENY_WRITE) &&
524 atomic_read(&fp->fi_access[O_WRONLY]))
525 return nfserr_share_denied;
526 }
527 return nfs_ok;
528}
529
J. Bruce Fields998db522010-08-07 09:21:41 -0400530static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400531{
Trond Myklebustde186432014-07-10 14:07:26 -0400532 might_lock(&fp->fi_lock);
533
534 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
535 struct file *f1 = NULL;
536 struct file *f2 = NULL;
537
Jeff Layton6d338b52014-07-10 14:07:29 -0400538 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400539 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400540 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400541 spin_unlock(&fp->fi_lock);
542 if (f1)
543 fput(f1);
544 if (f2)
545 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400546 }
547}
548
Jeff Layton12659652014-07-10 14:07:28 -0400549static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400550{
Jeff Layton12659652014-07-10 14:07:28 -0400551 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
552
553 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400554 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400555 if (access & NFS4_SHARE_ACCESS_READ)
556 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400557}
558
Sachin Bhamare8287f002015-04-27 14:50:14 +0200559/*
560 * Allocate a new open/delegation state counter. This is needed for
561 * pNFS for proper return on close semantics.
562 *
563 * Note that we only allocate it for pNFS-enabled exports, otherwise
564 * all pointers to struct nfs4_clnt_odstate are always NULL.
565 */
566static struct nfs4_clnt_odstate *
567alloc_clnt_odstate(struct nfs4_client *clp)
568{
569 struct nfs4_clnt_odstate *co;
570
571 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
572 if (co) {
573 co->co_client = clp;
574 atomic_set(&co->co_odcount, 1);
575 }
576 return co;
577}
578
579static void
580hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
581{
582 struct nfs4_file *fp = co->co_file;
583
584 lockdep_assert_held(&fp->fi_lock);
585 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
586}
587
588static inline void
589get_clnt_odstate(struct nfs4_clnt_odstate *co)
590{
591 if (co)
592 atomic_inc(&co->co_odcount);
593}
594
595static void
596put_clnt_odstate(struct nfs4_clnt_odstate *co)
597{
598 struct nfs4_file *fp;
599
600 if (!co)
601 return;
602
603 fp = co->co_file;
604 if (atomic_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
605 list_del(&co->co_perfile);
606 spin_unlock(&fp->fi_lock);
607
608 nfsd4_return_all_file_layouts(co->co_client, fp);
609 kmem_cache_free(odstate_slab, co);
610 }
611}
612
613static struct nfs4_clnt_odstate *
614find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
615{
616 struct nfs4_clnt_odstate *co;
617 struct nfs4_client *cl;
618
619 if (!new)
620 return NULL;
621
622 cl = new->co_client;
623
624 spin_lock(&fp->fi_lock);
625 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
626 if (co->co_client == cl) {
627 get_clnt_odstate(co);
628 goto out;
629 }
630 }
631 co = new;
632 co->co_file = fp;
633 hash_clnt_odstate_locked(new);
634out:
635 spin_unlock(&fp->fi_lock);
636 return co;
637}
638
Kinglong Mee743146d32017-01-18 19:04:42 +0800639struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
640 void (*sc_free)(struct nfs4_stid *))
J. Bruce Fields996e0932011-10-17 11:14:48 -0400641{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500642 struct nfs4_stid *stid;
643 int new_id;
644
Trond Myklebustf8338832014-07-25 07:34:19 -0400645 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500646 if (!stid)
647 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400648
Jeff Layton4770d722014-07-29 21:34:10 -0400649 idr_preload(GFP_KERNEL);
650 spin_lock(&cl->cl_lock);
651 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
652 spin_unlock(&cl->cl_lock);
653 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700654 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500655 goto out_free;
Kinglong Mee743146d32017-01-18 19:04:42 +0800656
657 stid->sc_free = sc_free;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500658 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500659 stid->sc_stateid.si_opaque.so_id = new_id;
660 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
661 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400662 atomic_set(&stid->sc_count, 1);
Jeff Layton9767feb2015-10-01 09:05:50 -0400663 spin_lock_init(&stid->sc_lock);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500664
J. Bruce Fields996e0932011-10-17 11:14:48 -0400665 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500666 * It shouldn't be a problem to reuse an opaque stateid value.
667 * I don't think it is for 4.1. But with 4.0 I worry that, for
668 * example, a stray write retransmission could be accepted by
669 * the server when it should have been rejected. Therefore,
670 * adopt a trick from the sctp code to attempt to maximize the
671 * amount of time until an id is reused, by ensuring they always
672 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400673 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500674 return stid;
675out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800676 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500677 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400678}
679
Jeff Laytonb49e0842014-07-29 21:34:11 -0400680static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400681{
Trond Myklebust60116952014-07-29 21:34:06 -0400682 struct nfs4_stid *stid;
Trond Myklebust60116952014-07-29 21:34:06 -0400683
Kinglong Mee743146d32017-01-18 19:04:42 +0800684 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
Trond Myklebust60116952014-07-29 21:34:06 -0400685 if (!stid)
686 return NULL;
687
Kinglong Mee743146d32017-01-18 19:04:42 +0800688 return openlockstateid(stid);
Trond Myklebust60116952014-07-29 21:34:06 -0400689}
690
691static void nfs4_free_deleg(struct nfs4_stid *stid)
692{
Trond Myklebust60116952014-07-29 21:34:06 -0400693 kmem_cache_free(deleg_slab, stid);
694 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400695}
696
NeilBrown6282cd52014-06-04 17:39:26 +1000697/*
698 * When we recall a delegation, we should be careful not to hand it
699 * out again straight away.
700 * To ensure this we keep a pair of bloom filters ('new' and 'old')
701 * in which the filehandles of recalled delegations are "stored".
702 * If a filehandle appear in either filter, a delegation is blocked.
703 * When a delegation is recalled, the filehandle is stored in the "new"
704 * filter.
705 * Every 30 seconds we swap the filters and clear the "new" one,
706 * unless both are empty of course.
707 *
708 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
709 * low 3 bytes as hash-table indices.
710 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400711 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000712 * is used to manage concurrent access. Testing does not need the lock
713 * except when swapping the two filters.
714 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400715static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000716static struct bloom_pair {
717 int entries, old_entries;
718 time_t swap_time;
719 int new; /* index into 'set' */
720 DECLARE_BITMAP(set[2], 256);
721} blocked_delegations;
722
723static int delegation_blocked(struct knfsd_fh *fh)
724{
725 u32 hash;
726 struct bloom_pair *bd = &blocked_delegations;
727
728 if (bd->entries == 0)
729 return 0;
730 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400731 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000732 if (seconds_since_boot() - bd->swap_time > 30) {
733 bd->entries -= bd->old_entries;
734 bd->old_entries = bd->entries;
735 memset(bd->set[bd->new], 0,
736 sizeof(bd->set[0]));
737 bd->new = 1-bd->new;
738 bd->swap_time = seconds_since_boot();
739 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400740 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000741 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100742 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000743 if (test_bit(hash&255, bd->set[0]) &&
744 test_bit((hash>>8)&255, bd->set[0]) &&
745 test_bit((hash>>16)&255, bd->set[0]))
746 return 1;
747
748 if (test_bit(hash&255, bd->set[1]) &&
749 test_bit((hash>>8)&255, bd->set[1]) &&
750 test_bit((hash>>16)&255, bd->set[1]))
751 return 1;
752
753 return 0;
754}
755
756static void block_delegations(struct knfsd_fh *fh)
757{
758 u32 hash;
759 struct bloom_pair *bd = &blocked_delegations;
760
Daniel Borkmann87545892014-12-10 16:33:11 +0100761 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000762
Jeff Laytonf54fe962014-07-25 07:34:26 -0400763 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000764 __set_bit(hash&255, bd->set[bd->new]);
765 __set_bit((hash>>8)&255, bd->set[bd->new]);
766 __set_bit((hash>>16)&255, bd->set[bd->new]);
767 if (bd->entries == 0)
768 bd->swap_time = seconds_since_boot();
769 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400770 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000771}
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773static struct nfs4_delegation *
Sachin Bhamare8287f002015-04-27 14:50:14 +0200774alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh,
775 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400778 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400781 n = atomic_long_inc_return(&num_delegations);
782 if (n < 0 || n > max_delegations)
783 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000784 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400785 goto out_dec;
Kinglong Mee743146d32017-01-18 19:04:42 +0800786 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700787 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400788 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400789
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400790 /*
791 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400792 * meaning of seqid 0 isn't meaningful, really, but let's avoid
793 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400794 */
795 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700796 INIT_LIST_HEAD(&dp->dl_perfile);
797 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200799 dp->dl_clnt_odstate = odstate;
800 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400801 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200802 dp->dl_retries = 1;
803 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200804 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400806out_dec:
807 atomic_long_dec(&num_delegations);
808 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
811void
Trond Myklebust60116952014-07-29 21:34:06 -0400812nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Trond Myklebust11b91642014-07-29 21:34:08 -0400814 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400815 struct nfs4_client *clp = s->sc_client;
816
Jeff Layton4770d722014-07-29 21:34:10 -0400817 might_lock(&clp->cl_lock);
818
Jeff Laytonb401be222014-07-29 21:34:33 -0400819 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
820 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400821 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400822 }
Trond Myklebust60116952014-07-29 21:34:06 -0400823 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400824 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400825 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400826 if (fp)
827 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
Jeff Layton9767feb2015-10-01 09:05:50 -0400830void
831nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
832{
833 stateid_t *src = &stid->sc_stateid;
834
835 spin_lock(&stid->sc_lock);
836 if (unlikely(++src->si_generation == 0))
837 src->si_generation = 1;
838 memcpy(dst, src, sizeof(*dst));
839 spin_unlock(&stid->sc_lock);
840}
841
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500842static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Jeff Layton6bcc0342014-08-09 10:22:40 -0400844 struct file *filp = NULL;
Jeff Layton417c6622014-07-21 09:34:57 -0400845
Jeff Layton6bcc0342014-08-09 10:22:40 -0400846 spin_lock(&fp->fi_lock);
Jeff Layton67db1032014-12-13 09:11:40 -0500847 if (fp->fi_deleg_file && --fp->fi_delegees == 0)
Jeff Layton6bcc0342014-08-09 10:22:40 -0400848 swap(filp, fp->fi_deleg_file);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400849 spin_unlock(&fp->fi_lock);
850
851 if (filp) {
Christoph Hellwig2ab99ee2015-01-21 19:14:02 +0100852 vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400853 fput(filp);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200857void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400858{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500859 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400860}
861
Andrew Elble34ed9872015-10-15 12:07:28 -0400862/**
863 * nfs4_get_existing_delegation - Discover if this delegation already exists
864 * @clp: a pointer to the nfs4_client we're granting a delegation to
865 * @fp: a pointer to the nfs4_file we're granting a delegation on
866 *
867 * Return:
868 * On success: NULL if an existing delegation was not found.
869 *
870 * On error: -EAGAIN if one was previously granted to this nfs4_client
871 * for this nfs4_file.
872 *
873 */
874
875static int
876nfs4_get_existing_delegation(struct nfs4_client *clp, struct nfs4_file *fp)
Benny Halevy931ee562014-05-30 09:09:27 -0400877{
Andrew Elble34ed9872015-10-15 12:07:28 -0400878 struct nfs4_delegation *searchdp = NULL;
879 struct nfs4_client *searchclp = NULL;
880
Benny Halevycdc97502014-05-30 09:09:30 -0400881 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400882 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400883
Andrew Elble34ed9872015-10-15 12:07:28 -0400884 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
885 searchclp = searchdp->dl_stid.sc_client;
886 if (clp == searchclp) {
887 return -EAGAIN;
888 }
889 }
890 return 0;
891}
892
893/**
894 * hash_delegation_locked - Add a delegation to the appropriate lists
895 * @dp: a pointer to the nfs4_delegation we are adding.
896 * @fp: a pointer to the nfs4_file we're granting a delegation on
897 *
898 * Return:
899 * On success: NULL if the delegation was successfully hashed.
900 *
901 * On error: -EAGAIN if one was previously granted to this
902 * nfs4_client for this nfs4_file. Delegation is not hashed.
903 *
904 */
905
906static int
907hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
908{
909 int status;
910 struct nfs4_client *clp = dp->dl_stid.sc_client;
911
912 lockdep_assert_held(&state_lock);
913 lockdep_assert_held(&fp->fi_lock);
914
915 status = nfs4_get_existing_delegation(clp, fp);
916 if (status)
917 return status;
918 ++fp->fi_delegees;
Trond Myklebust67cb1272014-07-29 21:34:17 -0400919 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400920 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400921 list_add(&dp->dl_perfile, &fp->fi_delegations);
Andrew Elble34ed9872015-10-15 12:07:28 -0400922 list_add(&dp->dl_perclnt, &clp->cl_delegations);
923 return 0;
Benny Halevy931ee562014-05-30 09:09:27 -0400924}
925
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400926static bool
Jeff Layton42690672014-07-25 07:34:20 -0400927unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Trond Myklebust11b91642014-07-29 21:34:08 -0400929 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400930
Jeff Layton42690672014-07-25 07:34:20 -0400931 lockdep_assert_held(&state_lock);
932
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400933 if (list_empty(&dp->dl_perfile))
934 return false;
935
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400936 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400937 /* Ensure that deleg break won't try to requeue it */
938 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400939 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400940 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400942 list_del_init(&dp->dl_perfile);
943 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400944 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400945}
946
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400947static void destroy_delegation(struct nfs4_delegation *dp)
948{
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400949 bool unhashed;
950
Jeff Layton42690672014-07-25 07:34:20 -0400951 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400952 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -0400953 spin_unlock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400954 if (unhashed) {
955 put_clnt_odstate(dp->dl_clnt_odstate);
956 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
957 nfs4_put_stid(&dp->dl_stid);
958 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400959}
960
961static void revoke_delegation(struct nfs4_delegation *dp)
962{
963 struct nfs4_client *clp = dp->dl_stid.sc_client;
964
Jeff Layton2d4a5322014-07-25 07:34:21 -0400965 WARN_ON(!list_empty(&dp->dl_recall_lru));
966
Sachin Bhamare8287f002015-04-27 14:50:14 +0200967 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -0400968 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
969
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400970 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400971 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400972 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400973 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400974 spin_lock(&clp->cl_lock);
975 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
976 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400977 }
978}
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980/*
981 * SETCLIENTID state
982 */
983
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400984static unsigned int clientid_hashval(u32 id)
985{
986 return id & CLIENT_HASH_MASK;
987}
988
989static unsigned int clientstr_hashval(const char *name)
990{
991 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
992}
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400995 * We store the NONE, READ, WRITE, and BOTH bits separately in the
996 * st_{access,deny}_bmap field of the stateid, in order to track not
997 * only what share bits are currently in force, but also what
998 * combinations of share bits previous opens have used. This allows us
999 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1000 * return an error if the client attempt to downgrade to a combination
1001 * of share bits not explicable by closing some of its previous opens.
1002 *
1003 * XXX: This enforcement is actually incomplete, since we don't keep
1004 * track of access/deny bit combinations; so, e.g., we allow:
1005 *
1006 * OPEN allow read, deny write
1007 * OPEN allow both, deny none
1008 * DOWNGRADE allow read, deny none
1009 *
1010 * which we should reject.
1011 */
Jeff Layton5ae037e2012-05-11 09:45:11 -04001012static unsigned int
1013bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001014 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -04001015 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001016
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001017 for (i = 1; i < 4; i++) {
1018 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -04001019 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001020 }
Jeff Layton5ae037e2012-05-11 09:45:11 -04001021 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001022}
1023
Jeff Layton82c5ff12012-05-11 09:45:13 -04001024/* set share access for a given stateid */
1025static inline void
1026set_access(u32 access, struct nfs4_ol_stateid *stp)
1027{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001028 unsigned char mask = 1 << access;
1029
1030 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1031 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001032}
1033
1034/* clear share access for a given stateid */
1035static inline void
1036clear_access(u32 access, struct nfs4_ol_stateid *stp)
1037{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001038 unsigned char mask = 1 << access;
1039
1040 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1041 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001042}
1043
1044/* test whether a given stateid has access */
1045static inline bool
1046test_access(u32 access, struct nfs4_ol_stateid *stp)
1047{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001048 unsigned char mask = 1 << access;
1049
1050 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001051}
1052
Jeff Laytonce0fc432012-05-11 09:45:14 -04001053/* set share deny for a given stateid */
1054static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001055set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001056{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001057 unsigned char mask = 1 << deny;
1058
1059 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1060 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001061}
1062
1063/* clear share deny for a given stateid */
1064static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001065clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001066{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001067 unsigned char mask = 1 << deny;
1068
1069 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1070 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001071}
1072
1073/* test whether a given stateid is denying specific access */
1074static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -04001075test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001076{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001077 unsigned char mask = 1 << deny;
1078
1079 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001080}
1081
1082static int nfs4_access_to_omode(u32 access)
1083{
J. Bruce Fields8f34a432010-09-02 15:23:16 -04001084 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001085 case NFS4_SHARE_ACCESS_READ:
1086 return O_RDONLY;
1087 case NFS4_SHARE_ACCESS_WRITE:
1088 return O_WRONLY;
1089 case NFS4_SHARE_ACCESS_BOTH:
1090 return O_RDWR;
1091 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001092 WARN_ON_ONCE(1);
1093 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001094}
1095
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001096/*
1097 * A stateid that had a deny mode associated with it is being released
1098 * or downgraded. Recalculate the deny mode on the file.
1099 */
1100static void
1101recalculate_deny_mode(struct nfs4_file *fp)
1102{
1103 struct nfs4_ol_stateid *stp;
1104
1105 spin_lock(&fp->fi_lock);
1106 fp->fi_share_deny = 0;
1107 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1108 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1109 spin_unlock(&fp->fi_lock);
1110}
1111
1112static void
1113reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1114{
1115 int i;
1116 bool change = false;
1117
1118 for (i = 1; i < 4; i++) {
1119 if ((i & deny) != i) {
1120 change = true;
1121 clear_deny(i, stp);
1122 }
1123 }
1124
1125 /* Recalculate per-file deny mode if there was a change */
1126 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -04001127 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001128}
1129
Jeff Layton82c5ff12012-05-11 09:45:13 -04001130/* release all access and file references for a given stateid */
1131static void
1132release_all_access(struct nfs4_ol_stateid *stp)
1133{
1134 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -04001135 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001136
1137 if (fp && stp->st_deny_bmap != 0)
1138 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001139
1140 for (i = 1; i < 4; i++) {
1141 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -04001142 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001143 clear_access(i, stp);
1144 }
1145}
1146
Kinglong Meed50ffde2015-07-16 12:05:07 +08001147static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1148{
1149 kfree(sop->so_owner.data);
1150 sop->so_ops->so_free(sop);
1151}
1152
Jeff Layton6b180f02014-07-29 21:34:26 -04001153static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1154{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001155 struct nfs4_client *clp = sop->so_client;
1156
1157 might_lock(&clp->cl_lock);
1158
1159 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001160 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001161 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001162 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001163 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001164}
1165
Jeff Laytone8568732015-08-24 12:41:47 -04001166static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001167{
Trond Myklebust11b91642014-07-29 21:34:08 -04001168 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001169
Jeff Layton1c755dc2014-07-29 21:34:12 -04001170 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1171
Jeff Laytone8568732015-08-24 12:41:47 -04001172 if (list_empty(&stp->st_perfile))
1173 return false;
1174
Trond Myklebust1d31a252014-07-10 14:07:25 -04001175 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001176 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001177 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001178 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001179 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001180}
1181
Trond Myklebust60116952014-07-29 21:34:06 -04001182static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001183{
Trond Myklebust60116952014-07-29 21:34:06 -04001184 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001185
Sachin Bhamare8287f002015-04-27 14:50:14 +02001186 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001187 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001188 if (stp->st_stateowner)
1189 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -04001190 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001191}
1192
Jeff Laytonb49e0842014-07-29 21:34:11 -04001193static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001194{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001195 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1196 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001197 struct file *file;
1198
Jeff Laytonb49e0842014-07-29 21:34:11 -04001199 file = find_any_file(stp->st_stid.sc_file);
1200 if (file)
1201 filp_close(file, (fl_owner_t)lo);
1202 nfs4_free_ol_stateid(stid);
1203}
1204
Jeff Layton2c41beb2014-07-29 21:34:41 -04001205/*
1206 * Put the persistent reference to an already unhashed generic stateid, while
1207 * holding the cl_lock. If it's the last reference, then put it onto the
1208 * reaplist for later destruction.
1209 */
1210static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1211 struct list_head *reaplist)
1212{
1213 struct nfs4_stid *s = &stp->st_stid;
1214 struct nfs4_client *clp = s->sc_client;
1215
1216 lockdep_assert_held(&clp->cl_lock);
1217
1218 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1219
1220 if (!atomic_dec_and_test(&s->sc_count)) {
1221 wake_up_all(&close_wq);
1222 return;
1223 }
1224
1225 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1226 list_add(&stp->st_locks, reaplist);
1227}
1228
Jeff Laytone8568732015-08-24 12:41:47 -04001229static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001230{
Chuck Leverf46c4452016-10-29 18:19:03 -04001231 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001232
1233 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001234 nfs4_unhash_stid(&stp->st_stid);
Jeff Laytone8568732015-08-24 12:41:47 -04001235 return unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001236}
1237
Jeff Layton5adfd882014-07-29 21:34:31 -04001238static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001239{
Chuck Leverf46c4452016-10-29 18:19:03 -04001240 struct nfs4_client *clp = stp->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04001241 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001242
Chuck Leverf46c4452016-10-29 18:19:03 -04001243 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001244 unhashed = unhash_lock_stateid(stp);
Chuck Leverf46c4452016-10-29 18:19:03 -04001245 spin_unlock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001246 if (unhashed)
1247 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001248}
1249
Trond Myklebustc58c6612014-07-29 21:34:35 -04001250static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001251{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001252 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001253
Trond Myklebustd4f04892014-07-29 21:34:36 -04001254 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001255
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001256 list_del_init(&lo->lo_owner.so_strhash);
1257}
1258
Jeff Layton2c41beb2014-07-29 21:34:41 -04001259/*
1260 * Free a list of generic stateids that were collected earlier after being
1261 * fully unhashed.
1262 */
1263static void
1264free_ol_stateid_reaplist(struct list_head *reaplist)
1265{
1266 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001267 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001268
1269 might_sleep();
1270
1271 while (!list_empty(reaplist)) {
1272 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1273 st_locks);
1274 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001275 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001276 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001277 if (fp)
1278 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001279 }
1280}
1281
Jeff Laytond83017f2014-07-29 21:34:42 -04001282static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1283 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001284{
1285 struct nfs4_ol_stateid *stp;
1286
Jeff Laytone8568732015-08-24 12:41:47 -04001287 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1288
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001289 while (!list_empty(&open_stp->st_locks)) {
1290 stp = list_entry(open_stp->st_locks.next,
1291 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001292 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001293 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001294 }
1295}
1296
Jeff Laytone8568732015-08-24 12:41:47 -04001297static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001298 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001299{
Jeff Laytone8568732015-08-24 12:41:47 -04001300 bool unhashed;
1301
Jeff Layton2c41beb2014-07-29 21:34:41 -04001302 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1303
Jeff Laytone8568732015-08-24 12:41:47 -04001304 unhashed = unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001305 release_open_stateid_locks(stp, reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04001306 return unhashed;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001307}
1308
1309static void release_open_stateid(struct nfs4_ol_stateid *stp)
1310{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001311 LIST_HEAD(reaplist);
1312
1313 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001314 if (unhash_open_stateid(stp, &reaplist))
1315 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001316 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1317 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001318}
1319
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001320static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001321{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001322 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001323
Trond Myklebustd4f04892014-07-29 21:34:36 -04001324 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001325
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001326 list_del_init(&oo->oo_owner.so_strhash);
1327 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001328}
1329
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001330static void release_last_closed_stateid(struct nfs4_openowner *oo)
1331{
Jeff Layton217526e2014-07-30 08:27:11 -04001332 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1333 nfsd_net_id);
1334 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001335
Jeff Layton217526e2014-07-30 08:27:11 -04001336 spin_lock(&nn->client_lock);
1337 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001338 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001339 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001340 oo->oo_last_closed_stid = NULL;
1341 }
Jeff Layton217526e2014-07-30 08:27:11 -04001342 spin_unlock(&nn->client_lock);
1343 if (s)
1344 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001345}
1346
Jeff Layton2c41beb2014-07-29 21:34:41 -04001347static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001348{
1349 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001350 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001351 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001352
Jeff Layton2c41beb2014-07-29 21:34:41 -04001353 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001354
Trond Myklebustd4f04892014-07-29 21:34:36 -04001355 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001356 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001357 while (!list_empty(&oo->oo_owner.so_stateids)) {
1358 stp = list_first_entry(&oo->oo_owner.so_stateids,
1359 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001360 if (unhash_open_stateid(stp, &reaplist))
1361 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001362 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001363 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001364 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001365 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001366 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001367}
1368
Marc Eshel5282fd72009-04-03 08:27:52 +03001369static inline int
1370hash_sessionid(struct nfs4_sessionid *sessionid)
1371{
1372 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1373
1374 return sid->sequence % SESSION_HASH_SIZE;
1375}
1376
Mark Salter135dd002015-04-06 09:46:00 -04001377#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001378static inline void
1379dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1380{
1381 u32 *ptr = (u32 *)(&sessionid->data[0]);
1382 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1383}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001384#else
1385static inline void
1386dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1387{
1388}
1389#endif
1390
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001391/*
1392 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1393 * won't be used for replay.
1394 */
1395void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1396{
1397 struct nfs4_stateowner *so = cstate->replay_owner;
1398
1399 if (nfserr == nfserr_replay_me)
1400 return;
1401
1402 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001403 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001404 return;
1405 }
1406 if (!so)
1407 return;
1408 if (so->so_is_open_owner)
1409 release_last_closed_stateid(openowner(so));
1410 so->so_seqid++;
1411 return;
1412}
Marc Eshel5282fd72009-04-03 08:27:52 +03001413
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001414static void
1415gen_sessionid(struct nfsd4_session *ses)
1416{
1417 struct nfs4_client *clp = ses->se_client;
1418 struct nfsd4_sessionid *sid;
1419
1420 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1421 sid->clientid = clp->cl_clientid;
1422 sid->sequence = current_sessionid++;
1423 sid->reserved = 0;
1424}
1425
1426/*
Andy Adamsona6496372009-08-28 08:45:01 -04001427 * The protocol defines ca_maxresponssize_cached to include the size of
1428 * the rpc header, but all we need to cache is the data starting after
1429 * the end of the initial SEQUENCE operation--the rest we regenerate
1430 * each time. Therefore we can advertise a ca_maxresponssize_cached
1431 * value that is the number of bytes in our cache plus a few additional
1432 * bytes. In order to stay on the safe side, and not promise more than
1433 * we can cache, those additional bytes must be the minimum possible: 24
1434 * bytes of rpc header (xid through accept state, with AUTH_NULL
1435 * verifier), 12 for the compound header (with zero-length tag), and 44
1436 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001437 */
Andy Adamsona6496372009-08-28 08:45:01 -04001438#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1439
Andy Adamson557ce262009-08-28 08:45:04 -04001440static void
1441free_session_slots(struct nfsd4_session *ses)
1442{
1443 int i;
1444
1445 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1446 kfree(ses->se_slots[i]);
1447}
1448
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001449/*
1450 * We don't actually need to cache the rpc and session headers, so we
1451 * can allocate a little less for each slot:
1452 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001453static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001454{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001455 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001456
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001457 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1458 size = 0;
1459 else
1460 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1461 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001462}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001463
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001464/*
1465 * XXX: If we run out of reserved DRC memory we could (up to a point)
1466 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001467 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001468 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001469static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001470{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001471 u32 slotsize = slot_bytes(ca);
1472 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001473 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001474
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001475 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001476 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1477 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001478 num = min_t(int, num, avail / slotsize);
1479 nfsd_drc_mem_used += num * slotsize;
1480 spin_unlock(&nfsd_drc_lock);
1481
1482 return num;
1483}
1484
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001485static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001486{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001487 int slotsize = slot_bytes(ca);
1488
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001489 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001490 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001491 spin_unlock(&nfsd_drc_lock);
1492}
1493
Kinglong Mee60810e52014-01-01 00:35:47 +08001494static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1495 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001496{
Kinglong Mee60810e52014-01-01 00:35:47 +08001497 int numslots = fattrs->maxreqs;
1498 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001499 struct nfsd4_session *new;
1500 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001501
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001502 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001503 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1504 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001505
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001506 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001507 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001508 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001509 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001510 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001511 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001512 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001513 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001514 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001515
1516 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1517 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1518
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001519 return new;
1520out_free:
1521 while (i--)
1522 kfree(new->se_slots[i]);
1523 kfree(new);
1524 return NULL;
1525}
1526
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001527static void free_conn(struct nfsd4_conn *c)
1528{
1529 svc_xprt_put(c->cn_xprt);
1530 kfree(c);
1531}
1532
1533static void nfsd4_conn_lost(struct svc_xpt_user *u)
1534{
1535 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1536 struct nfs4_client *clp = c->cn_session->se_client;
1537
1538 spin_lock(&clp->cl_lock);
1539 if (!list_empty(&c->cn_persession)) {
1540 list_del(&c->cn_persession);
1541 free_conn(c);
1542 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001543 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001544 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001545}
1546
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001547static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001548{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001549 struct nfsd4_conn *conn;
1550
1551 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1552 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001553 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001554 svc_xprt_get(rqstp->rq_xprt);
1555 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001556 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001557 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1558 return conn;
1559}
1560
J. Bruce Fields328ead22010-09-29 16:11:06 -04001561static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1562{
1563 conn->cn_session = ses;
1564 list_add(&conn->cn_persession, &ses->se_conns);
1565}
1566
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001567static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1568{
1569 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001570
1571 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001572 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001573 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001574}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001575
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001576static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001577{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001578 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001579 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001580}
1581
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001582static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001583{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001584 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001585
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001586 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001587 ret = nfsd4_register_conn(conn);
1588 if (ret)
1589 /* oops; xprt is already down: */
1590 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001591 /* We may have gained or lost a callback channel: */
1592 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001593}
1594
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001595static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001596{
1597 u32 dir = NFS4_CDFC4_FORE;
1598
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001599 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001600 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001601 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001602}
1603
1604/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001605static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001606{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001607 struct nfs4_client *clp = s->se_client;
1608 struct nfsd4_conn *c;
1609
1610 spin_lock(&clp->cl_lock);
1611 while (!list_empty(&s->se_conns)) {
1612 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1613 list_del_init(&c->cn_persession);
1614 spin_unlock(&clp->cl_lock);
1615
1616 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1617 free_conn(c);
1618
1619 spin_lock(&clp->cl_lock);
1620 }
1621 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001622}
1623
J. Bruce Fields1377b692012-09-11 21:42:40 -04001624static void __free_session(struct nfsd4_session *ses)
1625{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001626 free_session_slots(ses);
1627 kfree(ses);
1628}
1629
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001630static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001631{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001632 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001633 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001634 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001635}
Andy Adamson557ce262009-08-28 08:45:04 -04001636
Fengguang Wu135ae822012-11-10 07:20:25 -05001637static 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 -04001638{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001639 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001640 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001641
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001642 new->se_client = clp;
1643 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001644
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001645 INIT_LIST_HEAD(&new->se_conns);
1646
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001647 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001648 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001649 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001650 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001651 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001652 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001653 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001654 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001655 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001656 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001657
Chuck Leverb0d2e422014-08-22 15:10:59 -04001658 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001659 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001660 /*
1661 * This is a little silly; with sessions there's no real
1662 * use for the callback address. Use the peer address
1663 * as a reasonable default for now, but consider fixing
1664 * the rpc client not to require an address in the
1665 * future:
1666 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001667 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1668 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001669 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001670}
1671
Benny Halevy9089f1b2010-05-12 00:12:26 +03001672/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001673static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001674__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001675{
1676 struct nfsd4_session *elem;
1677 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001678 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001679
Trond Myklebust0a880a22014-07-30 08:27:10 -04001680 lockdep_assert_held(&nn->client_lock);
1681
Marc Eshel5282fd72009-04-03 08:27:52 +03001682 dump_sessionid(__func__, sessionid);
1683 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001684 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001685 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001686 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1687 NFS4_MAX_SESSIONID_LEN)) {
1688 return elem;
1689 }
1690 }
1691
1692 dprintk("%s: session not found\n", __func__);
1693 return NULL;
1694}
1695
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001696static struct nfsd4_session *
1697find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1698 __be32 *ret)
1699{
1700 struct nfsd4_session *session;
1701 __be32 status = nfserr_badsession;
1702
1703 session = __find_in_sessionid_hashtbl(sessionid, net);
1704 if (!session)
1705 goto out;
1706 status = nfsd4_get_session_locked(session);
1707 if (status)
1708 session = NULL;
1709out:
1710 *ret = status;
1711 return session;
1712}
1713
Benny Halevy9089f1b2010-05-12 00:12:26 +03001714/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001715static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001716unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001717{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001718 struct nfs4_client *clp = ses->se_client;
1719 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1720
1721 lockdep_assert_held(&nn->client_lock);
1722
Andy Adamson7116ed62009-04-03 08:27:43 +03001723 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001724 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001725 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001726 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001727}
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1730static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001731STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001733 /*
1734 * We're assuming the clid was not given out from a boot
1735 * precisely 2^32 (about 136 years) before this one. That seems
1736 * a safe assumption:
1737 */
1738 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001740 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001741 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return 1;
1743}
1744
1745/*
1746 * XXX Should we use a slab cache ?
1747 * This type of memory management is somewhat inefficient, but we use it
1748 * anyway since SETCLIENTID is not a common operation.
1749 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001750static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
1752 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001753 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001755 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1756 if (clp == NULL)
1757 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001758 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001759 if (clp->cl_name.data == NULL)
1760 goto err_no_name;
1761 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1762 OWNER_HASH_SIZE, GFP_KERNEL);
1763 if (!clp->cl_ownerstr_hashtbl)
1764 goto err_no_hashtbl;
1765 for (i = 0; i < OWNER_HASH_SIZE; i++)
1766 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001767 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001768 INIT_LIST_HEAD(&clp->cl_sessions);
1769 idr_init(&clp->cl_stateids);
1770 atomic_set(&clp->cl_refcount, 0);
1771 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1772 INIT_LIST_HEAD(&clp->cl_idhash);
1773 INIT_LIST_HEAD(&clp->cl_openowners);
1774 INIT_LIST_HEAD(&clp->cl_delegations);
1775 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04001776 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001777#ifdef CONFIG_NFSD_PNFS
1778 INIT_LIST_HEAD(&clp->cl_lo_states);
1779#endif
Trond Myklebust5694c932014-04-18 14:43:56 -04001780 spin_lock_init(&clp->cl_lock);
1781 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001783err_no_hashtbl:
1784 kfree(clp->cl_name.data);
1785err_no_name:
1786 kfree(clp);
1787 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001790static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791free_client(struct nfs4_client *clp)
1792{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001793 while (!list_empty(&clp->cl_sessions)) {
1794 struct nfsd4_session *ses;
1795 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1796 se_perclnt);
1797 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001798 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1799 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001800 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001801 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001802 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001803 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001805 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 kfree(clp);
1807}
1808
Benny Halevy84d38ac2010-05-12 00:13:16 +03001809/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001810static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001811unhash_client_locked(struct nfs4_client *clp)
1812{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001813 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001814 struct nfsd4_session *ses;
1815
Trond Myklebust0a880a22014-07-30 08:27:10 -04001816 lockdep_assert_held(&nn->client_lock);
1817
Trond Myklebust4beb3452014-07-30 08:27:02 -04001818 /* Mark the client as expired! */
1819 clp->cl_time = 0;
1820 /* Make it invisible */
1821 if (!list_empty(&clp->cl_idhash)) {
1822 list_del_init(&clp->cl_idhash);
1823 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1824 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1825 else
1826 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1827 }
1828 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001829 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001830 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1831 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001832 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001833}
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001836unhash_client(struct nfs4_client *clp)
1837{
1838 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1839
1840 spin_lock(&nn->client_lock);
1841 unhash_client_locked(clp);
1842 spin_unlock(&nn->client_lock);
1843}
1844
Jeff Layton97403d92014-07-30 08:27:12 -04001845static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1846{
1847 if (atomic_read(&clp->cl_refcount))
1848 return nfserr_jukebox;
1849 unhash_client_locked(clp);
1850 return nfs_ok;
1851}
1852
Trond Myklebust4beb3452014-07-30 08:27:02 -04001853static void
1854__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001856 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 struct list_head reaplist;
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001861 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001862 while (!list_empty(&clp->cl_delegations)) {
1863 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001864 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04001865 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
Benny Halevycdc97502014-05-30 09:09:30 -04001867 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 while (!list_empty(&reaplist)) {
1869 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001870 list_del_init(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +02001871 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -04001872 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04001873 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001875 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05001876 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001877 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001878 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001879 }
NeilBrownea1da632005-06-23 22:04:17 -07001880 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001881 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04001882 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001883 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001885 nfsd4_return_all_client_layouts(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001886 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001887 if (clp->cl_cb_conn.cb_xprt)
1888 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001889 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890}
1891
Trond Myklebust4beb3452014-07-30 08:27:02 -04001892static void
1893destroy_client(struct nfs4_client *clp)
1894{
1895 unhash_client(clp);
1896 __destroy_client(clp);
1897}
1898
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001899static void expire_client(struct nfs4_client *clp)
1900{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001901 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001902 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001903 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001904}
1905
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001906static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1907{
1908 memcpy(target->cl_verifier.data, source->data,
1909 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}
1911
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001912static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1913{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1915 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1916}
1917
J. Bruce Fields50043852015-11-20 15:58:37 -05001918int strdup_if_nonnull(char **target, char *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001919{
J. Bruce Fields50043852015-11-20 15:58:37 -05001920 if (source) {
1921 *target = kstrdup(source, GFP_KERNEL);
1922 if (!*target)
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001923 return -ENOMEM;
1924 } else
J. Bruce Fields50043852015-11-20 15:58:37 -05001925 *target = NULL;
1926 return 0;
1927}
1928
1929static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1930{
1931 int ret;
1932
1933 ret = strdup_if_nonnull(&target->cr_principal, source->cr_principal);
1934 if (ret)
1935 return ret;
J. Bruce Fields414ca012015-11-20 10:48:02 -05001936 ret = strdup_if_nonnull(&target->cr_raw_principal,
1937 source->cr_raw_principal);
1938 if (ret)
1939 return ret;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001940 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 target->cr_uid = source->cr_uid;
1942 target->cr_gid = source->cr_gid;
1943 target->cr_group_info = source->cr_group_info;
1944 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001945 target->cr_gss_mech = source->cr_gss_mech;
1946 if (source->cr_gss_mech)
1947 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001948 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949}
1950
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001951static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001952compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1953{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001954 if (o1->len < o2->len)
1955 return -1;
1956 if (o1->len > o2->len)
1957 return 1;
1958 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001959}
1960
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001961static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001962{
NeilBrowna55370a2005-06-23 22:03:52 -07001963 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
1965
1966static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001967same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1968{
1969 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970}
1971
1972static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001973same_clid(clientid_t *cl1, clientid_t *cl2)
1974{
1975 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
1977
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001978static bool groups_equal(struct group_info *g1, struct group_info *g2)
1979{
1980 int i;
1981
1982 if (g1->ngroups != g2->ngroups)
1983 return false;
1984 for (i=0; i<g1->ngroups; i++)
Alexey Dobriyan81243ea2016-10-07 17:03:12 -07001985 if (!gid_eq(g1->gid[i], g2->gid[i]))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001986 return false;
1987 return true;
1988}
1989
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001990/*
1991 * RFC 3530 language requires clid_inuse be returned when the
1992 * "principal" associated with a requests differs from that previously
1993 * used. We use uid, gid's, and gss principal string as our best
1994 * approximation. We also don't want to allow non-gss use of a client
1995 * established using gss: in theory cr_principal should catch that
1996 * change, but in practice cr_principal can be null even in the gss case
1997 * since gssd doesn't always pass down a principal string.
1998 */
1999static bool is_gss_cred(struct svc_cred *cr)
2000{
2001 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2002 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2003}
2004
2005
Vivek Trivedi5559b502012-07-24 21:18:20 +05302006static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002007same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2008{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002009 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002010 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2011 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002012 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2013 return false;
2014 if (cr1->cr_principal == cr2->cr_principal)
2015 return true;
2016 if (!cr1->cr_principal || !cr2->cr_principal)
2017 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05302018 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019}
2020
J. Bruce Fields57266a62013-04-13 14:27:29 -04002021static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2022{
2023 struct svc_cred *cr = &rqstp->rq_cred;
2024 u32 service;
2025
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04002026 if (!cr->cr_gss_mech)
2027 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002028 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2029 return service == RPC_GSS_SVC_INTEGRITY ||
2030 service == RPC_GSS_SVC_PRIVACY;
2031}
2032
Andrew Elblededeb132016-06-15 12:52:08 -04002033bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
J. Bruce Fields57266a62013-04-13 14:27:29 -04002034{
2035 struct svc_cred *cr = &rqstp->rq_cred;
2036
2037 if (!cl->cl_mach_cred)
2038 return true;
2039 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2040 return false;
2041 if (!svc_rqst_integrity_protected(rqstp))
2042 return false;
J. Bruce Fields414ca012015-11-20 10:48:02 -05002043 if (cl->cl_cred.cr_raw_principal)
2044 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2045 cr->cr_raw_principal);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002046 if (!cr->cr_principal)
2047 return false;
2048 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2049}
2050
Jeff Layton294ac322014-07-30 08:27:15 -04002051static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05002052{
Chuck Leverab4684d2012-03-02 17:13:50 -05002053 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Jeff Laytonf4199922014-06-17 07:44:11 -04002055 /*
2056 * This is opaque to client, so no need to byte-swap. Use
2057 * __force to keep sparse happy
2058 */
2059 verf[0] = (__force __be32)get_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08002060 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05002061 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062}
2063
Jeff Layton294ac322014-07-30 08:27:15 -04002064static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2065{
2066 clp->cl_clientid.cl_boot = nn->boot_time;
2067 clp->cl_clientid.cl_id = nn->clientid_counter++;
2068 gen_confirm(clp, nn);
2069}
2070
Jeff Layton4770d722014-07-29 21:34:10 -04002071static struct nfs4_stid *
2072find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04002073{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002074 struct nfs4_stid *ret;
2075
2076 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2077 if (!ret || !ret->sc_type)
2078 return NULL;
2079 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04002080}
2081
Jeff Layton4770d722014-07-29 21:34:10 -04002082static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04002083find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002084{
2085 struct nfs4_stid *s;
2086
Jeff Layton4770d722014-07-29 21:34:10 -04002087 spin_lock(&cl->cl_lock);
2088 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002089 if (s != NULL) {
2090 if (typemask & s->sc_type)
2091 atomic_inc(&s->sc_count);
2092 else
2093 s = NULL;
2094 }
Jeff Layton4770d722014-07-29 21:34:10 -04002095 spin_unlock(&cl->cl_lock);
2096 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04002097}
2098
Jeff Layton2216d442012-11-12 15:00:57 -05002099static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002100 struct svc_rqst *rqstp, nfs4_verifier *verf)
2101{
2102 struct nfs4_client *clp;
2103 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002104 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002105 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002106
2107 clp = alloc_client(name);
2108 if (clp == NULL)
2109 return NULL;
2110
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002111 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2112 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002113 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002114 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002115 }
Christoph Hellwig0162ac22014-09-24 12:19:19 +02002116 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Benny Halevy07cd4902010-05-12 00:13:41 +03002117 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002118 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002119 copy_verf(clp, verf);
2120 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04002121 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002122 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002123 return clp;
2124}
2125
NeilBrownfd39ca92005-06-23 22:04:03 -07002126static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002127add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2128{
2129 struct rb_node **new = &(root->rb_node), *parent = NULL;
2130 struct nfs4_client *clp;
2131
2132 while (*new) {
2133 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2134 parent = *new;
2135
2136 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2137 new = &((*new)->rb_left);
2138 else
2139 new = &((*new)->rb_right);
2140 }
2141
2142 rb_link_node(&new_clp->cl_namenode, parent, new);
2143 rb_insert_color(&new_clp->cl_namenode, root);
2144}
2145
2146static struct nfs4_client *
2147find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2148{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002149 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002150 struct rb_node *node = root->rb_node;
2151 struct nfs4_client *clp;
2152
2153 while (node) {
2154 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2155 cmp = compare_blob(&clp->cl_name, name);
2156 if (cmp > 0)
2157 node = node->rb_left;
2158 else if (cmp < 0)
2159 node = node->rb_right;
2160 else
2161 return clp;
2162 }
2163 return NULL;
2164}
2165
2166static void
2167add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
2169 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002170 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Trond Myklebust0a880a22014-07-30 08:27:10 -04002172 lockdep_assert_held(&nn->client_lock);
2173
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002174 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002175 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002177 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002178 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180
NeilBrownfd39ca92005-06-23 22:04:03 -07002181static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182move_to_confirmed(struct nfs4_client *clp)
2183{
2184 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002185 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Trond Myklebust0a880a22014-07-30 08:27:10 -04002187 lockdep_assert_held(&nn->client_lock);
2188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002190 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002191 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002192 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002193 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002194 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195}
2196
2197static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002198find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
2200 struct nfs4_client *clp;
2201 unsigned int idhashval = clientid_hashval(clid->cl_id);
2202
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002203 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002204 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002205 if ((bool)clp->cl_minorversion != sessions)
2206 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002207 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 }
2211 return NULL;
2212}
2213
2214static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002215find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2216{
2217 struct list_head *tbl = nn->conf_id_hashtbl;
2218
Trond Myklebust0a880a22014-07-30 08:27:10 -04002219 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002220 return find_client_in_id_table(tbl, clid, sessions);
2221}
2222
2223static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002224find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002226 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Trond Myklebust0a880a22014-07-30 08:27:10 -04002228 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002229 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230}
2231
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002232static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002233{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002234 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002235}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002236
NeilBrown28ce6052005-06-23 22:03:56 -07002237static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002238find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002239{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002240 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002241 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002242}
2243
2244static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002245find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002246{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002247 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002248 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002249}
2250
NeilBrownfd39ca92005-06-23 22:04:03 -07002251static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002252gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002254 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002255 struct sockaddr *sa = svc_addr(rqstp);
2256 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002257 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Jeff Layton7077ecb2009-08-14 12:57:58 -04002259 /* Currently, we only support tcp and tcp6 for the callback channel */
2260 if (se->se_callback_netid_len == 3 &&
2261 !memcmp(se->se_callback_netid_val, "tcp", 3))
2262 expected_family = AF_INET;
2263 else if (se->se_callback_netid_len == 4 &&
2264 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2265 expected_family = AF_INET6;
2266 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 goto out_err;
2268
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002269 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002270 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002271 (struct sockaddr *)&conn->cb_addr,
2272 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002273
J. Bruce Fields07263f12010-05-31 19:09:40 -04002274 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002276
J. Bruce Fields07263f12010-05-31 19:09:40 -04002277 if (conn->cb_addr.ss_family == AF_INET6)
2278 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002279
J. Bruce Fields07263f12010-05-31 19:09:40 -04002280 conn->cb_prog = se->se_callback_prog;
2281 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002282 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 return;
2284out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002285 conn->cb_addr.ss_family = AF_UNSPEC;
2286 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002287 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 "will not receive delegations\n",
2289 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 return;
2292}
2293
Andy Adamson074fe892009-04-03 08:28:15 +03002294/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002295 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002296 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002297static void
Andy Adamson074fe892009-04-03 08:28:15 +03002298nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2299{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002300 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002301 struct nfsd4_slot *slot = resp->cstate.slot;
2302 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002303
Andy Adamson557ce262009-08-28 08:45:04 -04002304 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002305
Andy Adamson557ce262009-08-28 08:45:04 -04002306 slot->sl_opcnt = resp->opcnt;
2307 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002308
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002309 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002310 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002311 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002312 return;
2313 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002314 base = resp->cstate.data_offset;
2315 slot->sl_datalen = buf->len - base;
2316 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Dan Carpenterd3f03402015-11-21 13:28:50 +03002317 WARN(1, "%s: sessions DRC could not cache compound\n",
2318 __func__);
Andy Adamson557ce262009-08-28 08:45:04 -04002319 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002320}
2321
2322/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002323 * Encode the replay sequence operation from the slot values.
2324 * If cachethis is FALSE encode the uncached rep error on the next
2325 * operation which sets resp->p and increments resp->opcnt for
2326 * nfs4svc_encode_compoundres.
2327 *
Andy Adamson074fe892009-04-03 08:28:15 +03002328 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002329static __be32
2330nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2331 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002332{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002333 struct nfsd4_op *op;
2334 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002335
Andy Adamsonabfabf82009-07-23 19:02:18 -04002336 /* Encode the replayed sequence operation */
2337 op = &args->ops[resp->opcnt - 1];
2338 nfsd4_encode_operation(resp, op);
2339
2340 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002341 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002342 op = &args->ops[resp->opcnt++];
2343 op->status = nfserr_retry_uncached_rep;
2344 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002345 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002346 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002347}
2348
2349/*
Andy Adamson557ce262009-08-28 08:45:04 -04002350 * The sequence operation is not cached because we can use the slot and
2351 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002352 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002353static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002354nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2355 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002356{
Andy Adamson557ce262009-08-28 08:45:04 -04002357 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002358 struct xdr_stream *xdr = &resp->xdr;
2359 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002360 __be32 status;
2361
Andy Adamson557ce262009-08-28 08:45:04 -04002362 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002363
Andy Adamsonabfabf82009-07-23 19:02:18 -04002364 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002365 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002366 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002367
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002368 p = xdr_reserve_space(xdr, slot->sl_datalen);
2369 if (!p) {
2370 WARN_ON_ONCE(1);
2371 return nfserr_serverfault;
2372 }
2373 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2374 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002375
Andy Adamson557ce262009-08-28 08:45:04 -04002376 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002377 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002378}
2379
Andy Adamson0733d212009-04-03 08:28:01 +03002380/*
2381 * Set the exchange_id flags returned by the server.
2382 */
2383static void
2384nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2385{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002386#ifdef CONFIG_NFSD_PNFS
2387 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2388#else
Andy Adamson0733d212009-04-03 08:28:01 +03002389 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002390#endif
Andy Adamson0733d212009-04-03 08:28:01 +03002391
2392 /* Referrals are supported, Migration is not. */
2393 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2394
2395 /* set the wire flags to return to client. */
2396 clid->flags = new->cl_exchange_flags;
2397}
2398
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002399static bool client_has_openowners(struct nfs4_client *clp)
2400{
2401 struct nfs4_openowner *oo;
2402
2403 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2404 if (!list_empty(&oo->oo_owner.so_stateids))
2405 return true;
2406 }
2407 return false;
2408}
2409
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002410static bool client_has_state(struct nfs4_client *clp)
2411{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002412 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08002413#ifdef CONFIG_NFSD_PNFS
2414 || !list_empty(&clp->cl_lo_states)
2415#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002416 || !list_empty(&clp->cl_delegations)
2417 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002418}
2419
Al Virob37ad282006-10-19 23:28:59 -07002420__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002421nfsd4_exchange_id(struct svc_rqst *rqstp,
2422 struct nfsd4_compound_state *cstate,
2423 struct nfsd4_exchange_id *exid)
2424{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002425 struct nfs4_client *conf, *new;
2426 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002427 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002428 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002429 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002430 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002431 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002432 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002433
Jeff Layton363168b2009-08-14 12:57:56 -04002434 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002435 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002436 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002437 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002438 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002439
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002440 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002441 return nfserr_inval;
2442
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002443 new = create_client(exid->clname, rqstp, &verf);
2444 if (new == NULL)
2445 return nfserr_jukebox;
2446
Andy Adamson0733d212009-04-03 08:28:01 +03002447 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002448 case SP4_MACH_CRED:
Andrew Elbleed941642016-06-15 12:52:09 -04002449 exid->spo_must_enforce[0] = 0;
2450 exid->spo_must_enforce[1] = (
2451 1 << (OP_BIND_CONN_TO_SESSION - 32) |
2452 1 << (OP_EXCHANGE_ID - 32) |
2453 1 << (OP_CREATE_SESSION - 32) |
2454 1 << (OP_DESTROY_SESSION - 32) |
2455 1 << (OP_DESTROY_CLIENTID - 32));
2456
2457 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
2458 1 << (OP_OPEN_DOWNGRADE) |
2459 1 << (OP_LOCKU) |
2460 1 << (OP_DELEGRETURN));
2461
2462 exid->spo_must_allow[1] &= (
2463 1 << (OP_TEST_STATEID - 32) |
2464 1 << (OP_FREE_STATEID - 32));
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002465 if (!svc_rqst_integrity_protected(rqstp)) {
2466 status = nfserr_inval;
2467 goto out_nolock;
2468 }
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002469 /*
2470 * Sometimes userspace doesn't give us a principal.
2471 * Which is a bug, really. Anyway, we can't enforce
2472 * MACH_CRED in that case, better to give up now:
2473 */
J. Bruce Fields414ca012015-11-20 10:48:02 -05002474 if (!new->cl_cred.cr_principal &&
2475 !new->cl_cred.cr_raw_principal) {
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002476 status = nfserr_serverfault;
2477 goto out_nolock;
2478 }
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002479 new->cl_mach_cred = true;
Andy Adamson0733d212009-04-03 08:28:01 +03002480 case SP4_NONE:
2481 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002482 default: /* checked by xdr code */
2483 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002484 case SP4_SSV:
Kinglong Mee8edf4b02016-02-26 22:36:42 +08002485 status = nfserr_encr_alg_unsupp;
2486 goto out_nolock;
Andy Adamson0733d212009-04-03 08:28:01 +03002487 }
2488
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002489 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002490 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002491 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002492 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002493 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2494 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2495
J. Bruce Fields136e6582012-05-12 20:37:23 -04002496 if (update) {
2497 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002498 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002499 goto out;
2500 }
Andrew Elblededeb132016-06-15 12:52:08 -04002501 if (!nfsd4_mach_creds_match(conf, rqstp)) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002502 status = nfserr_wrong_cred;
2503 goto out;
2504 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002505 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002506 status = nfserr_perm;
2507 goto out;
2508 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002509 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002510 status = nfserr_not_same;
2511 goto out;
2512 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002513 /* case 6 */
2514 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002515 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002516 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002517 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002518 if (client_has_state(conf)) {
2519 status = nfserr_clid_inuse;
2520 goto out;
2521 }
Andy Adamson0733d212009-04-03 08:28:01 +03002522 goto out_new;
2523 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002524 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002525 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002526 goto out_copy;
2527 }
2528 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002529 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002530 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002531 }
2532
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002533 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002534 status = nfserr_noent;
2535 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002536 }
2537
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002538 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002539 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002540 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002541
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002542 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002543out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002544 if (conf) {
2545 status = mark_client_expired_locked(conf);
2546 if (status)
2547 goto out;
2548 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002549 new->cl_minorversion = cstate->minorversion;
Andrew Elbleed941642016-06-15 12:52:09 -04002550 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
2551 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
Andy Adamson0733d212009-04-03 08:28:01 +03002552
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002553 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002554 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002555 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002556out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002557 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2558 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002559
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002560 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2561 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002562
2563 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002564 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002565 status = nfs_ok;
2566
2567out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002568 spin_unlock(&nn->client_lock);
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002569out_nolock:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002570 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002571 expire_client(new);
2572 if (unconf)
2573 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002574 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002575}
2576
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002577static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002578check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002579{
Andy Adamson88e588d2009-07-23 19:02:15 -04002580 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2581 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002582
2583 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002584 if (slot_inuse) {
2585 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002586 return nfserr_jukebox;
2587 else
2588 return nfserr_seq_misordered;
2589 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002590 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002591 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002592 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002593 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002594 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002595 return nfserr_seq_misordered;
2596}
2597
Andy Adamson49557cc2009-07-23 19:02:16 -04002598/*
2599 * Cache the create session result into the create session single DRC
2600 * slot cache by saving the xdr structure. sl_seqid has been set.
2601 * Do this for solo or embedded create session operations.
2602 */
2603static void
2604nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002605 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002606{
2607 slot->sl_status = nfserr;
2608 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2609}
2610
2611static __be32
2612nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2613 struct nfsd4_clid_slot *slot)
2614{
2615 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2616 return slot->sl_status;
2617}
2618
Mi Jinlong1b74c252011-07-14 14:50:17 +08002619#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2620 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2621 1 + /* MIN tag is length with zero, only length */ \
2622 3 + /* version, opcount, opcode */ \
2623 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2624 /* seqid, slotID, slotID, cache */ \
2625 4 ) * sizeof(__be32))
2626
2627#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2628 2 + /* verifier: AUTH_NULL, length 0 */\
2629 1 + /* status */ \
2630 1 + /* MIN tag is length with zero, only length */ \
2631 3 + /* opcount, opcode, opstatus*/ \
2632 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2633 /* seqid, slotID, slotID, slotID, status */ \
2634 5 ) * sizeof(__be32))
2635
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002636static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002637{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002638 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2639
J. Bruce Fields373cd402013-04-08 15:42:12 -04002640 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2641 return nfserr_toosmall;
2642 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2643 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002644 ca->headerpadsz = 0;
2645 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2646 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2647 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2648 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2649 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2650 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2651 /*
2652 * Note decreasing slot size below client's request may make it
2653 * difficult for client to function correctly, whereas
2654 * decreasing the number of slots will (just?) affect
2655 * performance. When short on memory we therefore prefer to
2656 * decrease number of slots instead of their size. Clients that
2657 * request larger slots than they need will get poor results:
2658 */
2659 ca->maxreqs = nfsd4_get_drc_mem(ca);
2660 if (!ca->maxreqs)
2661 return nfserr_jukebox;
2662
J. Bruce Fields373cd402013-04-08 15:42:12 -04002663 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002664}
2665
Chuck Lever45006322016-03-01 13:06:02 -05002666/*
2667 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
2668 * These are based on similar macros in linux/sunrpc/msg_prot.h .
2669 */
2670#define RPC_MAX_HEADER_WITH_AUTH_SYS \
2671 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
2672
2673#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
2674 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
2675
Kinglong Mee8a891632013-12-23 18:11:02 +08002676#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05002677 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08002678#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05002679 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
2680 sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08002681
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002682static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2683{
2684 ca->headerpadsz = 0;
2685
Kinglong Mee8a891632013-12-23 18:11:02 +08002686 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002687 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002688 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002689 return nfserr_toosmall;
2690 ca->maxresp_cached = 0;
2691 if (ca->maxops < 2)
2692 return nfserr_toosmall;
2693
2694 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002695}
2696
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002697static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2698{
2699 switch (cbs->flavor) {
2700 case RPC_AUTH_NULL:
2701 case RPC_AUTH_UNIX:
2702 return nfs_ok;
2703 default:
2704 /*
2705 * GSS case: the spec doesn't allow us to return this
2706 * error. But it also doesn't allow us not to support
2707 * GSS.
2708 * I'd rather this fail hard than return some error the
2709 * client might think it can already handle:
2710 */
2711 return nfserr_encr_alg_unsupp;
2712 }
2713}
2714
Andy Adamson069b6ad2009-04-03 08:27:58 +03002715__be32
2716nfsd4_create_session(struct svc_rqst *rqstp,
2717 struct nfsd4_compound_state *cstate,
2718 struct nfsd4_create_session *cr_ses)
2719{
Jeff Layton363168b2009-08-14 12:57:56 -04002720 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002721 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002722 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002723 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002724 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002725 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002726 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002727 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002728
Mi Jinlonga62573d2011-03-23 17:57:07 +08002729 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2730 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002731 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2732 if (status)
2733 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002734 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002735 if (status)
2736 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002737 status = check_backchannel_attrs(&cr_ses->back_channel);
2738 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002739 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002740 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002741 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002742 if (!new)
2743 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002744 conn = alloc_conn_from_crses(rqstp, cr_ses);
2745 if (!conn)
2746 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002747
Jeff Laytond20c11d2014-07-30 08:27:07 -04002748 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002749 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002750 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002751 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002752
2753 if (conf) {
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(conf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002756 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002757 cs_slot = &conf->cl_cs_slot;
2758 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08002759 if (status) {
2760 if (status == nfserr_replay_cache)
2761 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002762 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002763 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002764 } else if (unconf) {
2765 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002766 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002767 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002768 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002769 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002770 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002771 if (!nfsd4_mach_creds_match(unconf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002772 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002773 cs_slot = &unconf->cl_cs_slot;
2774 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002775 if (status) {
2776 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002777 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002778 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002779 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002780 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002781 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002782 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002783 if (status) {
2784 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002785 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002786 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002787 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002788 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002789 conf = unconf;
2790 } else {
2791 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002792 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002793 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002794 status = nfs_ok;
Chuck Lever4ce85c82016-03-01 13:05:27 -05002795 /* Persistent sessions are not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002796 cr_ses->flags &= ~SESSION4_PERSIST;
Chuck Lever4ce85c82016-03-01 13:05:27 -05002797 /* Upshifting from TCP to RDMA is not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002798 cr_ses->flags &= ~SESSION4_RDMA;
2799
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002800 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002801 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002802
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002803 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002804 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002805 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002806 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002807
Jeff Laytond20c11d2014-07-30 08:27:07 -04002808 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002809 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002810 spin_unlock(&nn->client_lock);
2811 /* init connection and backchannel */
2812 nfsd4_init_conn(rqstp, conn, new);
2813 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002814 if (old)
2815 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002816 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002817out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002818 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002819 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002820 if (old)
2821 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002822out_free_session:
2823 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002824out_release_drc_mem:
2825 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002826 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002827}
2828
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002829static __be32 nfsd4_map_bcts_dir(u32 *dir)
2830{
2831 switch (*dir) {
2832 case NFS4_CDFC4_FORE:
2833 case NFS4_CDFC4_BACK:
2834 return nfs_ok;
2835 case NFS4_CDFC4_FORE_OR_BOTH:
2836 case NFS4_CDFC4_BACK_OR_BOTH:
2837 *dir = NFS4_CDFC4_BOTH;
2838 return nfs_ok;
2839 };
2840 return nfserr_inval;
2841}
2842
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002843__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2844{
2845 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002846 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002847 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002848
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002849 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2850 if (status)
2851 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002852 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002853 session->se_cb_prog = bc->bc_cb_program;
2854 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002855 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002856
2857 nfsd4_probe_callback(session->se_client);
2858
2859 return nfs_ok;
2860}
2861
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002862__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2863 struct nfsd4_compound_state *cstate,
2864 struct nfsd4_bind_conn_to_session *bcts)
2865{
2866 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002867 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002868 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002869 struct net *net = SVC_NET(rqstp);
2870 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002871
2872 if (!nfsd4_last_compound_op(rqstp))
2873 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002874 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002875 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002876 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002877 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002878 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002879 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002880 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002881 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002882 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002883 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002884 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002885 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002886 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002887 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002888 goto out;
2889 nfsd4_init_conn(rqstp, conn, session);
2890 status = nfs_ok;
2891out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002892 nfsd4_put_session(session);
2893out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002894 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002895}
2896
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002897static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2898{
2899 if (!session)
2900 return 0;
2901 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2902}
2903
Andy Adamson069b6ad2009-04-03 08:27:58 +03002904__be32
2905nfsd4_destroy_session(struct svc_rqst *r,
2906 struct nfsd4_compound_state *cstate,
2907 struct nfsd4_destroy_session *sessionid)
2908{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002909 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002910 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002911 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002912 struct net *net = SVC_NET(r);
2913 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002914
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002915 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002916 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002917 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002918 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002919 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002920 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002921 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002922 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002923 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002924 if (!ses)
2925 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002926 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002927 if (!nfsd4_mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002928 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002929 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002930 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002931 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002932 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002933 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002934
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002935 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002936
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002937 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002938 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002939out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002940 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002941out_client_lock:
2942 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002943out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03002944 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002945}
2946
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002947static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002948{
2949 struct nfsd4_conn *c;
2950
2951 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002952 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002953 return c;
2954 }
2955 }
2956 return NULL;
2957}
2958
J. Bruce Fields57266a62013-04-13 14:27:29 -04002959static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002960{
2961 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002962 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002963 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002964 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002965
2966 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002967 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002968 if (c)
2969 goto out_free;
2970 status = nfserr_conn_not_bound_to_session;
2971 if (clp->cl_mach_cred)
2972 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002973 __nfsd4_hash_conn(new, ses);
2974 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002975 ret = nfsd4_register_conn(new);
2976 if (ret)
2977 /* oops; xprt is already down: */
2978 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002979 return nfs_ok;
2980out_free:
2981 spin_unlock(&clp->cl_lock);
2982 free_conn(new);
2983 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002984}
2985
Mi Jinlong868b89c2011-04-27 09:09:58 +08002986static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2987{
2988 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2989
2990 return args->opcnt > session->se_fchannel.maxops;
2991}
2992
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002993static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2994 struct nfsd4_session *session)
2995{
2996 struct xdr_buf *xb = &rqstp->rq_arg;
2997
2998 return xb->len > session->se_fchannel.maxreq_sz;
2999}
3000
Andy Adamson069b6ad2009-04-03 08:27:58 +03003001__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03003002nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03003003 struct nfsd4_compound_state *cstate,
3004 struct nfsd4_sequence *seq)
3005{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03003006 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003007 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003008 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003009 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003010 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003011 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003012 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003013 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003014 struct net *net = SVC_NET(rqstp);
3015 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003016
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03003017 if (resp->opcnt != 1)
3018 return nfserr_sequence_pos;
3019
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003020 /*
3021 * Will be either used or freed by nfsd4_sequence_check_conn
3022 * below.
3023 */
3024 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3025 if (!conn)
3026 return nfserr_jukebox;
3027
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003028 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003029 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003030 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003031 goto out_no_session;
3032 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003033
Mi Jinlong868b89c2011-04-27 09:09:58 +08003034 status = nfserr_too_many_ops;
3035 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003036 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003037
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003038 status = nfserr_req_too_big;
3039 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003040 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003041
Benny Halevyb85d4c02009-04-03 08:28:08 +03003042 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003043 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003044 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003045
Andy Adamson557ce262009-08-28 08:45:04 -04003046 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003047 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3048
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003049 /* We do not negotiate the number of slots yet, so set the
3050 * maxslots to the session maxreqs which is used to encode
3051 * sr_highest_slotid and the sr_target_slot id to maxslots */
3052 seq->maxslots = session->se_fchannel.maxreqs;
3053
J. Bruce Fields73e79482012-02-13 16:39:00 -05003054 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3055 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003056 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003057 status = nfserr_seq_misordered;
3058 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003059 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003060 cstate->slot = slot;
3061 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003062 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003063 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003064 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003065 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003066 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003067 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003068 }
3069 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003070 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003071
J. Bruce Fields57266a62013-04-13 14:27:29 -04003072 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003073 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003074 if (status)
3075 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003076
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003077 buflen = (seq->cachethis) ?
3078 session->se_fchannel.maxresp_cached :
3079 session->se_fchannel.maxresp_sz;
3080 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3081 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003082 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003083 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003084 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003085
3086 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003087 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003088 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003089 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003090 if (seq->cachethis)
3091 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003092 else
3093 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003094
3095 cstate->slot = slot;
3096 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003097 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003098
Benny Halevyb85d4c02009-04-03 08:28:08 +03003099out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003100 switch (clp->cl_cb_state) {
3101 case NFSD4_CB_DOWN:
3102 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3103 break;
3104 case NFSD4_CB_FAULT:
3105 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3106 break;
3107 default:
3108 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003109 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003110 if (!list_empty(&clp->cl_revoked))
3111 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003112out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08003113 if (conn)
3114 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003115 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003116 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003117out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003118 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003119 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003120}
3121
Trond Myklebustb6076642014-06-30 11:48:35 -04003122void
3123nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3124{
3125 struct nfsd4_compound_state *cs = &resp->cstate;
3126
3127 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04003128 if (cs->status != nfserr_replay_cache) {
3129 nfsd4_store_cache_entry(resp);
3130 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3131 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003132 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04003133 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003134 } else if (cs->clp)
3135 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04003136}
3137
Mi Jinlong345c2842011-10-20 17:51:39 +08003138__be32
3139nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
3140{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003141 struct nfs4_client *conf, *unconf;
3142 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003143 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003144 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08003145
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003146 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003147 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003148 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003149 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08003150
3151 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04003152 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08003153 status = nfserr_clientid_busy;
3154 goto out;
3155 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04003156 status = mark_client_expired_locked(conf);
3157 if (status)
3158 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003159 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08003160 } else if (unconf)
3161 clp = unconf;
3162 else {
3163 status = nfserr_stale_clientid;
3164 goto out;
3165 }
Andrew Elblededeb132016-06-15 12:52:08 -04003166 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003167 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003168 status = nfserr_wrong_cred;
3169 goto out;
3170 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003171 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003172out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003173 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003174 if (clp)
3175 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003176 return status;
3177}
3178
Andy Adamson069b6ad2009-04-03 08:27:58 +03003179__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003180nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
3181{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003182 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003183
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003184 if (rc->rca_one_fs) {
3185 if (!cstate->current_fh.fh_dentry)
3186 return nfserr_nofilehandle;
3187 /*
3188 * We don't take advantage of the rca_one_fs case.
3189 * That's OK, it's optional, we can safely ignore it.
3190 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02003191 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003192 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003193
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003194 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04003195 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3196 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003197 goto out;
3198
3199 status = nfserr_stale_clientid;
3200 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003201 /*
3202 * The following error isn't really legal.
3203 * But we only get here if the client just explicitly
3204 * destroyed the client. Surely it no longer cares what
3205 * error it gets back on an operation for the dead
3206 * client.
3207 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003208 goto out;
3209
3210 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04003211 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003212out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003213 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003214}
3215
3216__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003217nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3218 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003220 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003222 struct nfs4_client *conf, *new;
3223 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003224 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003225 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3226
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003227 new = create_client(clname, rqstp, &clverifier);
3228 if (new == NULL)
3229 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003230 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003231 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003232 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04003233 if (conf && client_has_state(conf)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003234 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003236 if (clp_used_exchangeid(conf))
3237 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003238 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04003239 char addr_str[INET6_ADDRSTRLEN];
3240 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3241 sizeof(addr_str));
3242 dprintk("NFSD: setclientid: string in use by client "
3243 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 goto out;
3245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003247 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003248 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003249 unhash_client_locked(unconf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003250 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003251 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003253 gen_confirm(new, nn);
3254 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003255 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003256 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003257 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003258 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3260 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3261 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003262 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 status = nfs_ok;
3264out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003265 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003266 if (new)
3267 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003268 if (unconf)
3269 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 return status;
3271}
3272
3273
Al Virob37ad282006-10-19 23:28:59 -07003274__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003275nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3276 struct nfsd4_compound_state *cstate,
3277 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278{
NeilBrown21ab45a2005-06-23 22:04:14 -07003279 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003280 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3282 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003283 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003284 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003286 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003288
Jeff Laytond20c11d2014-07-30 08:27:07 -04003289 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003290 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003291 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003292 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003293 * We try hard to give out unique clientid's, so if we get an
3294 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003295 * the client may be buggy; this should never happen.
3296 *
3297 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003298 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003299 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04003300 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3301 goto out;
3302 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3303 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003304 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003305 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003306 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3307 /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 status = nfs_ok;
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003309 } else /* case 4: client hasn't noticed we rebooted yet? */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003310 status = nfserr_stale_clientid;
3311 goto out;
3312 }
3313 status = nfs_ok;
3314 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003315 old = unconf;
3316 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003317 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003318 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003319 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3320 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04003321 status = nfserr_clid_inuse;
3322 if (client_has_state(old)
3323 && !same_creds(&unconf->cl_cred,
3324 &old->cl_cred))
3325 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003326 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003327 if (status) {
3328 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003329 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003330 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003331 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003332 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003333 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003334 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003335 get_client_locked(conf);
3336 spin_unlock(&nn->client_lock);
3337 nfsd4_probe_callback(conf);
3338 spin_lock(&nn->client_lock);
3339 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003341 spin_unlock(&nn->client_lock);
3342 if (old)
3343 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 return status;
3345}
3346
J. Bruce Fields32513b42011-10-13 16:00:16 -04003347static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003349 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3350}
3351
3352/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003353static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3354 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003355{
Trond Myklebust950e0112014-06-30 11:48:31 -04003356 lockdep_assert_held(&state_lock);
3357
J. Bruce Fields32513b42011-10-13 16:00:16 -04003358 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003359 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003360 INIT_LIST_HEAD(&fp->fi_stateids);
3361 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02003362 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003363 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003364 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003365 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003366 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003367 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3368 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003369#ifdef CONFIG_NFSD_PNFS
3370 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02003371 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003372#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04003373 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374}
3375
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003376void
NeilBrowne60d4392005-06-23 22:03:01 -07003377nfsd4_free_slabs(void)
3378{
Sachin Bhamare8287f002015-04-27 14:50:14 +02003379 kmem_cache_destroy(odstate_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003380 kmem_cache_destroy(openowner_slab);
3381 kmem_cache_destroy(lockowner_slab);
3382 kmem_cache_destroy(file_slab);
3383 kmem_cache_destroy(stateid_slab);
3384 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003385}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386
Bryan Schumaker72083392011-11-01 15:24:59 -04003387int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388nfsd4_init_slabs(void)
3389{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003390 openowner_slab = kmem_cache_create("nfsd4_openowners",
3391 sizeof(struct nfs4_openowner), 0, 0, NULL);
3392 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003393 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003394 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003395 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003396 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003397 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003398 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003399 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003400 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003401 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003402 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003403 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003404 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003405 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003406 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003407 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003408 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003409 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003410 odstate_slab = kmem_cache_create("nfsd4_odstate",
3411 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3412 if (odstate_slab == NULL)
3413 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003415
Sachin Bhamare8287f002015-04-27 14:50:14 +02003416out_free_deleg_slab:
3417 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003418out_free_stateid_slab:
3419 kmem_cache_destroy(stateid_slab);
3420out_free_file_slab:
3421 kmem_cache_destroy(file_slab);
3422out_free_lockowner_slab:
3423 kmem_cache_destroy(lockowner_slab);
3424out_free_openowner_slab:
3425 kmem_cache_destroy(openowner_slab);
3426out:
NeilBrowne60d4392005-06-23 22:03:01 -07003427 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3428 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429}
3430
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003431static void init_nfs4_replay(struct nfs4_replay *rp)
3432{
3433 rp->rp_status = nfserr_serverfault;
3434 rp->rp_buflen = 0;
3435 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003436 mutex_init(&rp->rp_mutex);
3437}
3438
3439static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3440 struct nfs4_stateowner *so)
3441{
3442 if (!nfsd4_has_session(cstate)) {
3443 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003444 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04003445 }
3446}
3447
3448void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3449{
3450 struct nfs4_stateowner *so = cstate->replay_owner;
3451
3452 if (so != NULL) {
3453 cstate->replay_owner = NULL;
3454 mutex_unlock(&so->so_replay.rp_mutex);
3455 nfs4_put_stateowner(so);
3456 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003457}
3458
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003459static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460{
3461 struct nfs4_stateowner *sop;
3462
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003463 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003464 if (!sop)
3465 return NULL;
3466
3467 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3468 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003469 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003470 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003472 sop->so_owner.len = owner->len;
3473
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003474 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003475 sop->so_client = clp;
3476 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003477 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003478 return sop;
3479}
3480
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003481static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003482{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003483 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003484
Trond Myklebustd4f04892014-07-29 21:34:36 -04003485 list_add(&oo->oo_owner.so_strhash,
3486 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003487 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488}
3489
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003490static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3491{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003492 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003493}
3494
Jeff Layton6b180f02014-07-29 21:34:26 -04003495static void nfs4_free_openowner(struct nfs4_stateowner *so)
3496{
3497 struct nfs4_openowner *oo = openowner(so);
3498
3499 kmem_cache_free(openowner_slab, oo);
3500}
3501
3502static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003503 .so_unhash = nfs4_unhash_openowner,
3504 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003505};
3506
Andrew Elble7fc05642015-11-05 20:42:43 -05003507static struct nfs4_ol_stateid *
3508nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3509{
3510 struct nfs4_ol_stateid *local, *ret = NULL;
3511 struct nfs4_openowner *oo = open->op_openowner;
3512
3513 lockdep_assert_held(&fp->fi_lock);
3514
3515 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3516 /* ignore lock owners */
3517 if (local->st_stateowner->so_is_open_owner == 0)
3518 continue;
Trond Myklebust1c8ea412017-11-03 08:00:10 -04003519 if (local->st_stateowner != &oo->oo_owner)
3520 continue;
3521 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
Andrew Elble7fc05642015-11-05 20:42:43 -05003522 ret = local;
3523 atomic_inc(&ret->st_stid.sc_count);
3524 break;
3525 }
3526 }
3527 return ret;
3528}
3529
Trond Myklebust1c8ea412017-11-03 08:00:10 -04003530static __be32
3531nfsd4_verify_open_stid(struct nfs4_stid *s)
3532{
3533 __be32 ret = nfs_ok;
3534
3535 switch (s->sc_type) {
3536 default:
3537 break;
3538 case NFS4_CLOSED_STID:
3539 case NFS4_CLOSED_DELEG_STID:
3540 ret = nfserr_bad_stateid;
3541 break;
3542 case NFS4_REVOKED_DELEG_STID:
3543 ret = nfserr_deleg_revoked;
3544 }
3545 return ret;
3546}
3547
3548/* Lock the stateid st_mutex, and deal with races with CLOSE */
3549static __be32
3550nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
3551{
3552 __be32 ret;
3553
3554 mutex_lock(&stp->st_mutex);
3555 ret = nfsd4_verify_open_stid(&stp->st_stid);
3556 if (ret != nfs_ok)
3557 mutex_unlock(&stp->st_mutex);
3558 return ret;
3559}
3560
3561static struct nfs4_ol_stateid *
3562nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3563{
3564 struct nfs4_ol_stateid *stp;
3565 for (;;) {
3566 spin_lock(&fp->fi_lock);
3567 stp = nfsd4_find_existing_open(fp, open);
3568 spin_unlock(&fp->fi_lock);
3569 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
3570 break;
3571 nfs4_put_stid(&stp->st_stid);
3572 }
3573 return stp;
3574}
3575
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003576static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003577alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003578 struct nfsd4_compound_state *cstate)
3579{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003580 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003581 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003583 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3584 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003586 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003587 oo->oo_owner.so_is_open_owner = 1;
3588 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003589 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003590 if (nfsd4_has_session(cstate))
3591 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003592 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003593 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003594 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003595 spin_lock(&clp->cl_lock);
3596 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003597 if (ret == NULL) {
3598 hash_openowner(oo, clp, strhashval);
3599 ret = oo;
3600 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08003601 nfs4_free_stateowner(&oo->oo_owner);
3602
Trond Myklebustd4f04892014-07-29 21:34:36 -04003603 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04003604 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605}
3606
Andrew Elble7fc05642015-11-05 20:42:43 -05003607static struct nfs4_ol_stateid *
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003608init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
Andrew Elble7fc05642015-11-05 20:42:43 -05003609{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610
Andrew Elble7fc05642015-11-05 20:42:43 -05003611 struct nfs4_openowner *oo = open->op_openowner;
3612 struct nfs4_ol_stateid *retstp = NULL;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003613 struct nfs4_ol_stateid *stp;
Andrew Elble7fc05642015-11-05 20:42:43 -05003614
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003615 stp = open->op_stp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003616 /* We are moving these outside of the spinlocks to avoid the warnings */
3617 mutex_init(&stp->st_mutex);
3618 mutex_lock(&stp->st_mutex);
3619
Trond Myklebust1c8ea412017-11-03 08:00:10 -04003620retry:
Andrew Elble7fc05642015-11-05 20:42:43 -05003621 spin_lock(&oo->oo_owner.so_client->cl_lock);
3622 spin_lock(&fp->fi_lock);
3623
3624 retstp = nfsd4_find_existing_open(fp, open);
3625 if (retstp)
3626 goto out_unlock;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003627
3628 open->op_stp = NULL;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003629 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003630 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003631 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003632 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07003633 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003634 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 stp->st_access_bmap = 0;
3636 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003637 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003638 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003639 list_add(&stp->st_perfile, &fp->fi_stateids);
Andrew Elble7fc05642015-11-05 20:42:43 -05003640
3641out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04003642 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003643 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003644 if (retstp) {
Trond Myklebust1c8ea412017-11-03 08:00:10 -04003645 /* Handle races with CLOSE */
3646 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
3647 nfs4_put_stid(&retstp->st_stid);
3648 goto retry;
3649 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003650 /* To keep mutex tracking happy */
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003651 mutex_unlock(&stp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003652 stp = retstp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003653 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003654 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655}
3656
Jeff Laytond3134b12014-07-29 21:34:32 -04003657/*
3658 * In the 4.0 case we need to keep the owners around a little while to handle
3659 * CLOSE replay. We still do need to release any file access that is held by
3660 * them before returning however.
3661 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003663move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664{
Jeff Layton217526e2014-07-30 08:27:11 -04003665 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003666 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3667 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3668 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003669
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003670 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
Jeff Laytonb401be222014-07-29 21:34:33 -04003672 /*
3673 * We know that we hold one reference via nfsd4_close, and another
3674 * "persistent" reference for the client. If the refcount is higher
3675 * than 2, then there are still calls in progress that are using this
3676 * stateid. We can't put the sc_file reference until they are finished.
3677 * Wait for the refcount to drop to 2. Since it has been unhashed,
3678 * there should be no danger of the refcount going back up again at
3679 * this point.
3680 */
3681 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3682
Jeff Laytond3134b12014-07-29 21:34:32 -04003683 release_all_access(s);
3684 if (s->st_stid.sc_file) {
3685 put_nfs4_file(s->st_stid.sc_file);
3686 s->st_stid.sc_file = NULL;
3687 }
Jeff Layton217526e2014-07-30 08:27:11 -04003688
3689 spin_lock(&nn->client_lock);
3690 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003691 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003692 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003693 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003694 spin_unlock(&nn->client_lock);
3695 if (last)
3696 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697}
3698
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699/* search file_hashtbl[] for file */
3700static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04003701find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 struct nfs4_file *fp;
3704
Jeff Layton5b095e92014-10-23 08:01:02 -04003705 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02003706 if (fh_match(&fp->fi_fhandle, fh)) {
Jeff Layton5b095e92014-10-23 08:01:02 -04003707 if (atomic_inc_not_zero(&fp->fi_ref))
3708 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 }
3711 return NULL;
3712}
3713
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02003714struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003715find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003716{
3717 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003718 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003719
Jeff Layton5b095e92014-10-23 08:01:02 -04003720 rcu_read_lock();
3721 fp = find_file_locked(fh, hashval);
3722 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04003723 return fp;
3724}
3725
3726static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003727find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003728{
3729 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003730 unsigned int hashval = file_hashval(fh);
3731
3732 rcu_read_lock();
3733 fp = find_file_locked(fh, hashval);
3734 rcu_read_unlock();
3735 if (fp)
3736 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04003737
3738 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04003739 fp = find_file_locked(fh, hashval);
3740 if (likely(fp == NULL)) {
3741 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04003742 fp = new;
3743 }
3744 spin_unlock(&state_lock);
3745
3746 return fp;
3747}
3748
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003749/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 * Called to check deny when READ with all zero stateid or
3751 * WRITE with all zero or all one stateid
3752 */
Al Virob37ad282006-10-19 23:28:59 -07003753static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3755{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003757 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
Trond Myklebustca943212014-07-23 16:17:39 -04003759 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003760 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003761 return ret;
3762 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003763 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003764 if (fp->fi_share_deny & deny_type)
3765 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003766 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003767 put_nfs4_file(fp);
3768 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769}
3770
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003771static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003773 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04003774 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3775 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003776
Trond Myklebust11b91642014-07-29 21:34:08 -04003777 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003778
Jeff Layton02e12152014-07-16 10:31:57 -04003779 /*
3780 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003781 * already holding inode->i_lock.
3782 *
Jeff Laytondff13992014-07-08 14:02:49 -04003783 * If the dl_time != 0, then we know that it has already been
3784 * queued for a lease break. Don't queue it again.
3785 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003786 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003787 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003788 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003789 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003790 }
Jeff Layton02e12152014-07-16 10:31:57 -04003791 spin_unlock(&state_lock);
3792}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003794static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3795 struct rpc_task *task)
3796{
3797 struct nfs4_delegation *dp = cb_to_delegation(cb);
3798
Andrew Elblea4579742015-08-31 12:06:41 -04003799 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3800 return 1;
3801
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003802 switch (task->tk_status) {
3803 case 0:
3804 return 1;
3805 case -EBADHANDLE:
3806 case -NFS4ERR_BAD_STATEID:
3807 /*
3808 * Race: client probably got cb_recall before open reply
3809 * granting delegation.
3810 */
3811 if (dp->dl_retries--) {
3812 rpc_delay(task, 2 * HZ);
3813 return 0;
3814 }
3815 /*FALLTHRU*/
3816 default:
3817 return -1;
3818 }
3819}
3820
3821static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3822{
3823 struct nfs4_delegation *dp = cb_to_delegation(cb);
3824
3825 nfs4_put_stid(&dp->dl_stid);
3826}
3827
Julia Lawallc4cb8972015-11-21 22:57:39 +01003828static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003829 .prepare = nfsd4_cb_recall_prepare,
3830 .done = nfsd4_cb_recall_done,
3831 .release = nfsd4_cb_recall_release,
3832};
3833
Jeff Layton02e12152014-07-16 10:31:57 -04003834static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3835{
3836 /*
3837 * We're assuming the state code never drops its reference
3838 * without first removing the lease. Since we're in this lease
3839 * callback (and since the lease code is serialized by the kernel
3840 * lock) we know the server hasn't removed the lease yet, we know
3841 * it's safe to take a reference.
3842 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003843 atomic_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02003844 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003845}
3846
Jeff Layton1c8c6012013-06-21 08:58:15 -04003847/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003848static bool
3849nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003850{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003851 bool ret = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003852 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3853 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003854
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003855 if (!fp) {
3856 WARN(1, "(%p)->fl_owner NULL\n", fl);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003857 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003858 }
3859 if (fp->fi_had_conflict) {
3860 WARN(1, "duplicate break on %p\n", fp);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003861 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003862 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003863 /*
3864 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003865 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003866 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003867 */
3868 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869
Jeff Layton02e12152014-07-16 10:31:57 -04003870 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003871 fp->fi_had_conflict = true;
3872 /*
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003873 * If there are no delegations on the list, then return true
3874 * so that the lease code will go ahead and delete it.
Jeff Layton417c6622014-07-21 09:34:57 -04003875 */
3876 if (list_empty(&fp->fi_delegations))
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003877 ret = true;
Jeff Layton417c6622014-07-21 09:34:57 -04003878 else
3879 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3880 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003881 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003882 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883}
3884
Jeff Laytonc45198e2014-09-01 07:12:07 -04003885static int
Jeff Layton7448cc32015-01-16 15:05:57 -05003886nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3887 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888{
3889 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04003890 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 else
3892 return -EAGAIN;
3893}
3894
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003895static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003896 .lm_break = nfsd_break_deleg_cb,
3897 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898};
3899
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003900static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3901{
3902 if (nfsd4_has_session(cstate))
3903 return nfs_ok;
3904 if (seqid == so->so_seqid - 1)
3905 return nfserr_replay_me;
3906 if (seqid == so->so_seqid)
3907 return nfs_ok;
3908 return nfserr_bad_seqid;
3909}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910
Jeff Layton4b24ca72014-06-30 11:48:44 -04003911static __be32 lookup_clientid(clientid_t *clid,
3912 struct nfsd4_compound_state *cstate,
3913 struct nfsd_net *nn)
3914{
3915 struct nfs4_client *found;
3916
3917 if (cstate->clp) {
3918 found = cstate->clp;
3919 if (!same_clid(&found->cl_clientid, clid))
3920 return nfserr_stale_clientid;
3921 return nfs_ok;
3922 }
3923
3924 if (STALE_CLIENTID(clid, nn))
3925 return nfserr_stale_clientid;
3926
3927 /*
3928 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3929 * cached already then we know this is for is for v4.0 and "sessions"
3930 * will be false.
3931 */
3932 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003933 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003934 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003935 if (!found) {
3936 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003937 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003938 }
3939 atomic_inc(&found->cl_refcount);
3940 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003941
3942 /* Cache the nfs4_client in cstate! */
3943 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003944 return nfs_ok;
3945}
3946
Al Virob37ad282006-10-19 23:28:59 -07003947__be32
Andy Adamson66689582009-04-03 08:28:45 +03003948nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003949 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 clientid_t *clientid = &open->op_clientid;
3952 struct nfs4_client *clp = NULL;
3953 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003954 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003955 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003957 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003959 /*
3960 * In case we need it later, after we've already created the
3961 * file and don't want to risk a further failure:
3962 */
3963 open->op_file = nfsd4_alloc_file();
3964 if (open->op_file == NULL)
3965 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966
Trond Myklebust2d91e892014-06-30 11:48:46 -04003967 status = lookup_clientid(clientid, cstate, nn);
3968 if (status)
3969 return status;
3970 clp = cstate->clp;
3971
Trond Myklebustd4f04892014-07-29 21:34:36 -04003972 strhashval = ownerstr_hashval(&open->op_owner);
3973 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003974 open->op_openowner = oo;
3975 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003976 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003978 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003979 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003980 release_openowner(oo);
3981 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003982 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003983 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003984 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3985 if (status)
3986 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003987 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003988new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003989 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003990 if (oo == NULL)
3991 return nfserr_jukebox;
3992 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003993alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003994 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003995 if (!open->op_stp)
3996 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003997
3998 if (nfsd4_has_session(cstate) &&
3999 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4000 open->op_odstate = alloc_clnt_odstate(clp);
4001 if (!open->op_odstate)
4002 return nfserr_jukebox;
4003 }
4004
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004005 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006}
4007
Al Virob37ad282006-10-19 23:28:59 -07004008static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07004009nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4010{
4011 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4012 return nfserr_openmode;
4013 else
4014 return nfs_ok;
4015}
4016
Daniel Mackc47d8322011-05-16 16:38:14 +02004017static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04004018{
J. Bruce Fields24a01112010-05-18 20:01:35 -04004019 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4020}
4021
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004022static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004023{
4024 struct nfs4_stid *ret;
4025
Andrew Elble62f0b692017-11-03 14:06:31 -04004026 ret = find_stateid_by_type(cl, s,
4027 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004028 if (!ret)
4029 return NULL;
4030 return delegstateid(ret);
4031}
4032
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004033static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4034{
4035 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4036 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4037}
4038
Al Virob37ad282006-10-19 23:28:59 -07004039static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04004040nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07004041 struct nfs4_delegation **dp)
4042{
4043 int flags;
Al Virob37ad282006-10-19 23:28:59 -07004044 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004045 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07004046
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004047 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4048 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07004049 goto out;
Andrew Elble62f0b692017-11-03 14:06:31 -04004050 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4051 nfs4_put_stid(&deleg->dl_stid);
4052 if (cl->cl_minorversion)
4053 status = nfserr_deleg_revoked;
4054 goto out;
4055 }
J. Bruce Fields24a01112010-05-18 20:01:35 -04004056 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004057 status = nfs4_check_delegmode(deleg, flags);
4058 if (status) {
4059 nfs4_put_stid(&deleg->dl_stid);
4060 goto out;
4061 }
4062 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004063out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004064 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004065 return nfs_ok;
4066 if (status)
4067 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004068 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004069 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07004070}
4071
J. Bruce Fields21fb4012010-07-28 12:21:23 -04004072static inline int nfs4_access_to_access(u32 nfs4_access)
4073{
4074 int flags = 0;
4075
4076 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4077 flags |= NFSD_MAY_READ;
4078 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4079 flags |= NFSD_MAY_WRITE;
4080 return flags;
4081}
4082
Al Virob37ad282006-10-19 23:28:59 -07004083static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4085 struct nfsd4_open *open)
4086{
4087 struct iattr iattr = {
4088 .ia_valid = ATTR_SIZE,
4089 .ia_size = 0,
4090 };
4091 if (!open->op_truncate)
4092 return 0;
4093 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08004094 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
4096}
4097
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004098static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004099 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4100 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004101{
Trond Myklebustde186432014-07-10 14:07:26 -04004102 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004103 __be32 status;
4104 int oflag = nfs4_access_to_omode(open->op_share_access);
4105 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004106 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004107
Trond Myklebustde186432014-07-10 14:07:26 -04004108 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004109
4110 /*
4111 * Are we trying to set a deny mode that would conflict with
4112 * current access?
4113 */
4114 status = nfs4_file_check_deny(fp, open->op_share_deny);
4115 if (status != nfs_ok) {
4116 spin_unlock(&fp->fi_lock);
4117 goto out;
4118 }
4119
4120 /* set access to the file */
4121 status = nfs4_file_get_access(fp, open->op_share_access);
4122 if (status != nfs_ok) {
4123 spin_unlock(&fp->fi_lock);
4124 goto out;
4125 }
4126
4127 /* Set access bits in stateid */
4128 old_access_bmap = stp->st_access_bmap;
4129 set_access(open->op_share_access, stp);
4130
4131 /* Set new deny mask */
4132 old_deny_bmap = stp->st_deny_bmap;
4133 set_deny(open->op_share_deny, stp);
4134 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4135
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004136 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04004137 spin_unlock(&fp->fi_lock);
4138 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004139 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004140 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04004141 spin_lock(&fp->fi_lock);
4142 if (!fp->fi_fds[oflag]) {
4143 fp->fi_fds[oflag] = filp;
4144 filp = NULL;
4145 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004146 }
Trond Myklebustde186432014-07-10 14:07:26 -04004147 spin_unlock(&fp->fi_lock);
4148 if (filp)
4149 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004150
4151 status = nfsd4_truncate(rqstp, cur_fh, open);
4152 if (status)
4153 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004154out:
4155 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004156out_put_access:
4157 stp->st_access_bmap = old_access_bmap;
4158 nfs4_file_put_access(fp, open->op_share_access);
4159 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4160 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004161}
4162
Al Virob37ad282006-10-19 23:28:59 -07004163static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004164nfs4_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 -07004165{
Al Virob37ad282006-10-19 23:28:59 -07004166 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02004167 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004169 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004170 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004171
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004172 /* test and set deny mode */
4173 spin_lock(&fp->fi_lock);
4174 status = nfs4_file_check_deny(fp, open->op_share_deny);
4175 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004176 set_deny(open->op_share_deny, stp);
4177 fp->fi_share_deny |=
4178 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4179 }
4180 spin_unlock(&fp->fi_lock);
4181
4182 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004185 status = nfsd4_truncate(rqstp, cur_fh, open);
4186 if (status != nfs_ok)
4187 reset_union_bmap_deny(old_deny_bmap, stp);
4188 return status;
4189}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004191/* Should we give out recallable state?: */
4192static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4193{
4194 if (clp->cl_cb_state == NFSD4_CB_UP)
4195 return true;
4196 /*
4197 * In the sessions case, since we don't have to establish a
4198 * separate connection for callbacks, we assume it's OK
4199 * until we hear otherwise:
4200 */
4201 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4202}
4203
Jeff Laytond564fbe2014-07-16 10:31:58 -04004204static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004205{
4206 struct file_lock *fl;
4207
4208 fl = locks_alloc_lock();
4209 if (!fl)
4210 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004211 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04004212 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004213 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4214 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04004215 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004216 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004217 return fl;
4218}
4219
Andrew Elble34ed9872015-10-15 12:07:28 -04004220/**
4221 * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer
4222 * @dp: a pointer to the nfs4_delegation we're adding.
4223 *
4224 * Return:
4225 * On success: Return code will be 0 on success.
4226 *
4227 * On error: -EAGAIN if there was an existing delegation.
4228 * nonzero if there is an error in other cases.
4229 *
4230 */
4231
J. Bruce Fields99c41512013-05-21 16:21:25 -04004232static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004233{
Trond Myklebust11b91642014-07-29 21:34:08 -04004234 struct nfs4_file *fp = dp->dl_stid.sc_file;
Kinglong Meeefde6b42015-07-13 17:33:24 +08004235 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04004236 struct file *filp;
4237 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004238
Jeff Laytond564fbe2014-07-16 10:31:58 -04004239 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004240 if (!fl)
4241 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04004242 filp = find_readable_file(fp);
4243 if (!filp) {
4244 /* We should always have a readable file here */
4245 WARN_ON_ONCE(1);
Kinglong Meeaf9dbaf2015-07-13 17:30:51 +08004246 locks_free_lock(fl);
Jeff Layton417c6622014-07-21 09:34:57 -04004247 return -EBADF;
4248 }
4249 fl->fl_file = filp;
Jeff Laytone6f5c782014-08-22 10:40:25 -04004250 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04004251 if (fl)
Jeff Layton417c6622014-07-21 09:34:57 -04004252 locks_free_lock(fl);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04004253 if (status)
Jeff Layton417c6622014-07-21 09:34:57 -04004254 goto out_fput;
Benny Halevycdc97502014-05-30 09:09:30 -04004255 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004256 spin_lock(&fp->fi_lock);
4257 /* Did the lease get broken before we took the lock? */
4258 status = -EAGAIN;
4259 if (fp->fi_had_conflict)
4260 goto out_unlock;
4261 /* Race breaker */
Jeff Layton0c637be2014-08-22 12:05:43 -04004262 if (fp->fi_deleg_file) {
Andrew Elble34ed9872015-10-15 12:07:28 -04004263 status = hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04004264 goto out_unlock;
4265 }
Jeff Layton417c6622014-07-21 09:34:57 -04004266 fp->fi_deleg_file = filp;
Andrew Elble34ed9872015-10-15 12:07:28 -04004267 fp->fi_delegees = 0;
4268 status = hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04004269 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04004270 spin_unlock(&state_lock);
Andrew Elble34ed9872015-10-15 12:07:28 -04004271 if (status) {
4272 /* Should never happen, this is a new fi_deleg_file */
4273 WARN_ON_ONCE(1);
4274 goto out_fput;
4275 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004276 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04004277out_unlock:
4278 spin_unlock(&fp->fi_lock);
4279 spin_unlock(&state_lock);
4280out_fput:
4281 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05004282 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004283}
4284
Jeff Layton0b266932014-07-25 07:34:25 -04004285static struct nfs4_delegation *
4286nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004287 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004288{
Jeff Layton0b266932014-07-25 07:34:25 -04004289 int status;
4290 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04004291
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004292 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004293 return ERR_PTR(-EAGAIN);
4294
Andrew Elble34ed9872015-10-15 12:07:28 -04004295 spin_lock(&state_lock);
4296 spin_lock(&fp->fi_lock);
4297 status = nfs4_get_existing_delegation(clp, fp);
4298 spin_unlock(&fp->fi_lock);
4299 spin_unlock(&state_lock);
4300
4301 if (status)
4302 return ERR_PTR(status);
4303
Sachin Bhamare8287f002015-04-27 14:50:14 +02004304 dp = alloc_init_deleg(clp, fh, odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004305 if (!dp)
4306 return ERR_PTR(-ENOMEM);
4307
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004308 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04004309 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004310 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04004311 dp->dl_stid.sc_file = fp;
Jeff Layton0c637be2014-08-22 12:05:43 -04004312 if (!fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04004313 spin_unlock(&fp->fi_lock);
4314 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004315 status = nfs4_setlease(dp);
4316 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04004317 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004318 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04004319 status = -EAGAIN;
4320 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004321 }
Andrew Elble34ed9872015-10-15 12:07:28 -04004322 status = hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04004323out_unlock:
4324 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04004325 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004326out:
4327 if (status) {
Sachin Bhamare8287f002015-04-27 14:50:14 +02004328 put_clnt_odstate(dp->dl_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04004329 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04004330 return ERR_PTR(status);
4331 }
4332 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004333}
4334
Benny Halevy4aa89132012-02-21 14:16:44 -08004335static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4336{
4337 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4338 if (status == -EAGAIN)
4339 open->op_why_no_deleg = WND4_CONTENTION;
4340 else {
4341 open->op_why_no_deleg = WND4_RESOURCE;
4342 switch (open->op_deleg_want) {
4343 case NFS4_SHARE_WANT_READ_DELEG:
4344 case NFS4_SHARE_WANT_WRITE_DELEG:
4345 case NFS4_SHARE_WANT_ANY_DELEG:
4346 break;
4347 case NFS4_SHARE_WANT_CANCEL:
4348 open->op_why_no_deleg = WND4_CANCELLED;
4349 break;
4350 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004351 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08004352 }
4353 }
4354}
4355
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356/*
4357 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04004358 *
4359 * Note we don't support write delegations, and won't until the vfs has
4360 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 */
4362static void
Jeff Layton4cf59222014-07-25 07:34:24 -04004363nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4364 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365{
4366 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04004367 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4368 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004369 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004370 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004372 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07004373 open->op_recall = 0;
4374 switch (open->op_claim_type) {
4375 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05004376 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07004377 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004378 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4379 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004380 break;
4381 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00004382 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004383 /*
4384 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004385 * had the chance to reclaim theirs, *and* until
4386 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004387 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004388 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004389 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004390 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004391 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04004392 /*
4393 * Also, if the file was opened for write or
4394 * create, there's a good chance the client's
4395 * about to write to it, resulting in an
4396 * immediate recall (since we don't support
4397 * write delegations):
4398 */
NeilBrown7b190fe2005-06-23 22:03:23 -07004399 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04004400 goto out_no_deleg;
4401 if (open->op_create == NFS4_OPEN_CREATE)
4402 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004403 break;
4404 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004405 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004406 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004407 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004408 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004409 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004411 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004413 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004414 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04004415 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04004416 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004417 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004418out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004419 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4420 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004421 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04004422 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004423 open->op_recall = 1;
4424 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004425
4426 /* 4.1 client asking for a delegation? */
4427 if (open->op_deleg_want)
4428 nfsd4_open_deleg_none_ext(open, status);
4429 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430}
4431
Benny Halevye27f49c2012-02-21 14:16:54 -08004432static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4433 struct nfs4_delegation *dp)
4434{
4435 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4436 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4437 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4438 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4439 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4440 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4441 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4442 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4443 }
4444 /* Otherwise the client must be confused wanting a delegation
4445 * it already has, therefore we don't return
4446 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4447 */
4448}
4449
Al Virob37ad282006-10-19 23:28:59 -07004450__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4452{
Andy Adamson66689582009-04-03 08:28:45 +03004453 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004454 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004456 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004457 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004458 __be32 status;
Trond Myklebust1c404252017-11-03 08:00:11 -04004459 bool new_stp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 /*
4462 * Lookup file; if found, lookup stateid and check open request,
4463 * and check for delegations in the process of being recalled.
4464 * If not found, create the nfs4_file struct
4465 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004466 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004467 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004468 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004469 if (status)
4470 goto out;
Trond Myklebust1c8ea412017-11-03 08:00:10 -04004471 stp = nfsd4_find_and_lock_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004473 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004474 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004475 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004476 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 }
4478
Trond Myklebust1c404252017-11-03 08:00:11 -04004479 if (!stp) {
4480 stp = init_open_stateid(fp, open);
4481 if (!open->op_stp)
4482 new_stp = true;
4483 }
4484
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 /*
4486 * OPEN the file, or upgrade an existing OPEN.
4487 * If truncate fails, the OPEN fails.
Trond Myklebust1c404252017-11-03 08:00:11 -04004488 *
4489 * stp is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 */
Trond Myklebust1c404252017-11-03 08:00:11 -04004491 if (!new_stp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004493 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04004494 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004495 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04004497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 } else {
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004499 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4500 if (status) {
Trond Myklebust1c404252017-11-03 08:00:11 -04004501 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004502 release_open_stateid(stp);
Trond Myklebust1c404252017-11-03 08:00:11 -04004503 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004504 goto out;
4505 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004506
4507 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4508 open->op_odstate);
4509 if (stp->st_clnt_odstate == open->op_odstate)
4510 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 }
Trond Myklebust1c404252017-11-03 08:00:11 -04004512
Jeff Layton9767feb2015-10-01 09:05:50 -04004513 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004514 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
Benny Halevyd24433c2012-02-16 20:57:17 +02004516 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004517 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4518 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4519 open->op_why_no_deleg = WND4_NOT_WANTED;
4520 goto nodeleg;
4521 }
4522 }
4523
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 /*
4525 * Attempt to hand out a delegation. No error return, because the
4526 * OPEN succeeds even if we fail.
4527 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004528 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004529nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 status = nfs_ok;
4531
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004532 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004533 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004535 /* 4.1 client trying to upgrade/downgrade delegation? */
4536 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004537 open->op_deleg_want)
4538 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004539
NeilBrown13cd2182005-06-23 22:03:10 -07004540 if (fp)
4541 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004542 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08004543 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 /*
4545 * To finish the open response, we just need to set the rflags.
4546 */
4547 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Jeff Layton19e4c342016-09-16 16:28:27 -04004548 if (nfsd4_has_session(&resp->cstate))
4549 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
4550 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Jeff Layton19e4c342016-09-16 16:28:27 -04004552
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004553 if (dp)
4554 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004555 if (stp)
4556 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
4558 return status;
4559}
4560
Jeff Layton58fb12e2014-07-29 21:34:27 -04004561void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04004562 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004563{
4564 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004565 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004566
Jeff Laytond3134b12014-07-29 21:34:32 -04004567 nfsd4_cstate_assign_replay(cstate, so);
4568 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004569 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004570 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04004571 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004572 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004573 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004574 if (open->op_odstate)
4575 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004576}
4577
Al Virob37ad282006-10-19 23:28:59 -07004578__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004579nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4580 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581{
4582 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004583 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004584 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 dprintk("process_renew(%08x/%08x): starting\n",
4587 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004588 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004589 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004591 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004593 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004594 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 goto out;
4596 status = nfs_ok;
4597out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 return status;
4599}
4600
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004601void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004602nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004603{
Jeff Layton33dcc482012-04-10 11:08:48 -04004604 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004605 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004606 return;
4607
NeilBrowna76b4312005-06-23 22:04:01 -07004608 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004609 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004610 /*
4611 * If the server goes down again right now, an NFSv4
4612 * client will still be allowed to reclaim after it comes back up,
4613 * even if it hasn't yet had a chance to reclaim state this time.
4614 *
4615 */
Jeff Layton919b8042014-09-12 16:40:20 -04004616 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004617 /*
4618 * At this point, NFSv4 clients can still reclaim. But if the
4619 * server crashes, any that have not yet reclaimed will be out
4620 * of luck on the next boot.
4621 *
4622 * (NFSv4.1+ clients are considered to have reclaimed once they
4623 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4624 * have reclaimed after their first OPEN.)
4625 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004626 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004627 /*
4628 * At this point, and once lockd and/or any other containers
4629 * exit their grace period, further reclaims will fail and
4630 * regular locking can resume.
4631 */
NeilBrowna76b4312005-06-23 22:04:01 -07004632}
4633
NeilBrownfd39ca92005-06-23 22:04:03 -07004634static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004635nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636{
4637 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004638 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004640 struct nfs4_ol_stateid *stp;
Jeff Layton7919d0a2016-09-16 16:28:25 -04004641 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004643 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004644 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004647 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004648 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004649 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004650 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 clp = list_entry(pos, struct nfs4_client, cl_lru);
4652 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4653 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004654 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 break;
4656 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004657 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004658 dprintk("NFSD: client in use (clientid %08x)\n",
4659 clp->cl_clientid.cl_id);
4660 continue;
4661 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004662 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004663 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004664 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004665 list_for_each_safe(pos, next, &reaplist) {
4666 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 dprintk("NFSD: purging unused client (clientid %08x)\n",
4668 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004669 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 expire_client(clp);
4671 }
Benny Halevycdc97502014-05-30 09:09:30 -04004672 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004673 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4675 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004676 t = dp->dl_time - cutoff;
4677 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 break;
4679 }
Jeff Layton3fcbbd22015-08-24 12:41:48 -04004680 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04004681 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 }
Benny Halevycdc97502014-05-30 09:09:30 -04004683 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004684 while (!list_empty(&reaplist)) {
4685 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4686 dl_recall_lru);
4687 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004688 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 }
Jeff Layton217526e2014-07-30 08:27:11 -04004690
4691 spin_lock(&nn->client_lock);
4692 while (!list_empty(&nn->close_lru)) {
4693 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4694 oo_close_lru);
4695 if (time_after((unsigned long)oo->oo_time,
4696 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004697 t = oo->oo_time - cutoff;
4698 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 break;
4700 }
Jeff Layton217526e2014-07-30 08:27:11 -04004701 list_del_init(&oo->oo_close_lru);
4702 stp = oo->oo_last_closed_stid;
4703 oo->oo_last_closed_stid = NULL;
4704 spin_unlock(&nn->client_lock);
4705 nfs4_put_stid(&stp->st_stid);
4706 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 }
Jeff Layton217526e2014-07-30 08:27:11 -04004708 spin_unlock(&nn->client_lock);
4709
Jeff Layton7919d0a2016-09-16 16:28:25 -04004710 /*
4711 * It's possible for a client to try and acquire an already held lock
4712 * that is being held for a long time, and then lose interest in it.
4713 * So, we clean out any un-revisited request after a lease period
4714 * under the assumption that the client is no longer interested.
4715 *
4716 * RFC5661, sec. 9.6 states that the client must not rely on getting
4717 * notifications and must continue to poll for locks, even when the
4718 * server supports them. Thus this shouldn't lead to clients blocking
4719 * indefinitely once the lock does become free.
4720 */
4721 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04004722 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04004723 while (!list_empty(&nn->blocked_locks_lru)) {
4724 nbl = list_first_entry(&nn->blocked_locks_lru,
4725 struct nfsd4_blocked_lock, nbl_lru);
4726 if (time_after((unsigned long)nbl->nbl_time,
4727 (unsigned long)cutoff)) {
4728 t = nbl->nbl_time - cutoff;
4729 new_timeo = min(new_timeo, t);
4730 break;
4731 }
4732 list_move(&nbl->nbl_lru, &reaplist);
4733 list_del_init(&nbl->nbl_list);
4734 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04004735 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04004736
4737 while (!list_empty(&reaplist)) {
Naofumi Hondaaa1a6cf2017-11-09 10:57:16 -05004738 nbl = list_first_entry(&reaplist,
Jeff Layton7919d0a2016-09-16 16:28:25 -04004739 struct nfsd4_blocked_lock, nbl_lru);
4740 list_del_init(&nbl->nbl_lru);
4741 posix_unblock_lock(&nbl->nbl_lock);
4742 free_blocked_lock(nbl);
4743 }
4744
Jeff Laytona832e7a2014-05-30 09:09:26 -04004745 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004746 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747}
4748
Harvey Harrisona254b242008-02-20 12:49:00 -08004749static struct workqueue_struct *laundry_wq;
4750static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004751
4752static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004753laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754{
4755 time_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08004756 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004757 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4758 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004760 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004762 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763}
4764
Jeff Layton8fcd4612015-07-30 06:57:46 -04004765static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004766{
Jeff Layton8fcd4612015-07-30 06:57:46 -04004767 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004768 return nfserr_bad_stateid;
4769 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770}
4771
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004773access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004775 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4776 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4777 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778}
4779
4780static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004781access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004783 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4784 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785}
4786
4787static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004788__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789{
Al Virob37ad282006-10-19 23:28:59 -07004790 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791
J. Bruce Fields02921912010-07-29 15:16:59 -04004792 /* For lock stateid's, we test the parent open, not the lock: */
4793 if (stp->st_openstp)
4794 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004795 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004797 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 goto out;
4799 status = nfs_ok;
4800out:
4801 return status;
4802}
4803
Al Virob37ad282006-10-19 23:28:59 -07004804static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004805check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004807 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004809 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004810 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 * conflicting state; so we must wait out the grace period. */
4812 return nfserr_grace;
4813 } else if (flags & WR_STATE)
4814 return nfs4_share_conflict(current_fh,
4815 NFS4_SHARE_DENY_WRITE);
4816 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4817 return nfs4_share_conflict(current_fh,
4818 NFS4_SHARE_DENY_READ);
4819}
4820
4821/*
4822 * Allow READ/WRITE during grace period on recovered state only for files
4823 * that are not able to provide mandatory locking.
4824 */
4825static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004826grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004828 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829}
4830
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004831static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004832{
Andy Adamson66689582009-04-03 08:28:45 +03004833 /*
4834 * When sessions are used the stateid generation number is ignored
4835 * when it is zero.
4836 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004837 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004838 return nfs_ok;
4839
4840 if (in->si_generation == ref->si_generation)
4841 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004842
J. Bruce Fields0836f582008-01-26 19:08:12 -05004843 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04004844 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004845 return nfserr_bad_stateid;
4846 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004847 * However, we could see a stateid from the past, even from a
4848 * non-buggy client. For example, if the client sends a lock
4849 * while some IO is outstanding, the lock may bump si_generation
4850 * while the IO is still in flight. The client could avoid that
4851 * situation by waiting for responses on all the IO requests,
4852 * but better performance may result in retrying IO that
4853 * receives an old_stateid error if requests are rarely
4854 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004855 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004856 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004857}
4858
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004859static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4860{
4861 if (ols->st_stateowner->so_is_open_owner &&
4862 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4863 return nfserr_bad_stateid;
4864 return nfs_ok;
4865}
4866
Chuck Lever7df302f2012-05-29 13:56:37 -04004867static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004868{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004869 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004870 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004871
Chuck Lever7df302f2012-05-29 13:56:37 -04004872 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004873 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004874 /* Client debugging aid. */
4875 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4876 char addr_str[INET6_ADDRSTRLEN];
4877 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4878 sizeof(addr_str));
4879 pr_warn_ratelimited("NFSD: client %s testing state ID "
4880 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004881 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004882 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004883 spin_lock(&cl->cl_lock);
4884 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004885 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004886 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004887 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004888 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004889 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004890 switch (s->sc_type) {
4891 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004892 status = nfs_ok;
4893 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004894 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004895 status = nfserr_deleg_revoked;
4896 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004897 case NFS4_OPEN_STID:
4898 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004899 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04004900 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004901 default:
4902 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004903 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004904 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004905 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004906 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004907 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004908out_unlock:
4909 spin_unlock(&cl->cl_lock);
4910 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004911}
4912
Christoph Hellwigcd61c522014-08-14 08:44:57 +02004913__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004914nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4915 stateid_t *stateid, unsigned char typemask,
4916 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004917{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004918 __be32 status;
Andrew Elble62f0b692017-11-03 14:06:31 -04004919 bool return_revoked = false;
4920
4921 /*
4922 * only return revoked delegations if explicitly asked.
4923 * otherwise we report revoked or bad_stateid status.
4924 */
4925 if (typemask & NFS4_REVOKED_DELEG_STID)
4926 return_revoked = true;
4927 else if (typemask & NFS4_DELEG_STID)
4928 typemask |= NFS4_REVOKED_DELEG_STID;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004929
4930 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4931 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004932 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004933 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004934 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004935 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004936 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004937 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004938 if (status)
4939 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004940 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004941 if (!*s)
4942 return nfserr_bad_stateid;
Andrew Elble62f0b692017-11-03 14:06:31 -04004943 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
4944 nfs4_put_stid(*s);
4945 if (cstate->minorversion)
4946 return nfserr_deleg_revoked;
4947 return nfserr_bad_stateid;
4948 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004949 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004950}
4951
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004952static struct file *
4953nfs4_find_file(struct nfs4_stid *s, int flags)
4954{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004955 if (!s)
4956 return NULL;
4957
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004958 switch (s->sc_type) {
4959 case NFS4_DELEG_STID:
4960 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
4961 return NULL;
4962 return get_file(s->sc_file->fi_deleg_file);
4963 case NFS4_OPEN_STID:
4964 case NFS4_LOCK_STID:
4965 if (flags & RD_STATE)
4966 return find_readable_file(s->sc_file);
4967 else
4968 return find_writeable_file(s->sc_file);
4969 break;
4970 }
4971
4972 return NULL;
4973}
4974
4975static __be32
4976nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
4977{
4978 __be32 status;
4979
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004980 status = nfsd4_check_openowner_confirmed(ols);
4981 if (status)
4982 return status;
4983 return nfs4_check_openmode(ols, flags);
4984}
4985
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004986static __be32
4987nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
4988 struct file **filpp, bool *tmp_file, int flags)
4989{
4990 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
4991 struct file *file;
4992 __be32 status;
4993
4994 file = nfs4_find_file(s, flags);
4995 if (file) {
4996 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
4997 acc | NFSD_MAY_OWNER_OVERRIDE);
4998 if (status) {
4999 fput(file);
5000 return status;
5001 }
5002
5003 *filpp = file;
5004 } else {
5005 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
5006 if (status)
5007 return status;
5008
5009 if (tmp_file)
5010 *tmp_file = true;
5011 }
5012
5013 return 0;
5014}
5015
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005017 * Checks for stateid operations
5018 */
Al Virob37ad282006-10-19 23:28:59 -07005019__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005020nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01005021 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
5022 stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005024 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005025 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005026 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005027 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005028 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 if (filpp)
5031 *filpp = NULL;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005032 if (tmp_file)
5033 *tmp_file = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005035 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 return nfserr_grace;
5037
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005038 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5039 status = check_special_stateids(net, fhp, stateid, flags);
5040 goto done;
5041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005043 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04005044 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005045 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005046 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04005047 return status;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005048 status = check_stateid_generation(stateid, &s->sc_stateid,
5049 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04005050 if (status)
5051 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005052
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005053 switch (s->sc_type) {
5054 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005055 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005056 break;
5057 case NFS4_OPEN_STID:
5058 case NFS4_LOCK_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005059 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005060 break;
5061 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04005062 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005063 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04005065 if (status)
5066 goto out;
5067 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005068
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005069done:
5070 if (!status && filpp)
5071 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072out:
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005073 if (s)
5074 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 return status;
5076}
5077
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005078/*
Bryan Schumaker17456802011-07-13 10:50:48 -04005079 * Test if the stateid is valid
5080 */
5081__be32
5082nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5083 struct nfsd4_test_stateid *test_stateid)
5084{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005085 struct nfsd4_test_stateid_id *stateid;
5086 struct nfs4_client *cl = cstate->session->se_client;
5087
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005088 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04005089 stateid->ts_id_status =
5090 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005091
Bryan Schumaker17456802011-07-13 10:50:48 -04005092 return nfs_ok;
5093}
5094
Chuck Lever42691392016-08-11 10:37:30 -04005095static __be32
5096nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5097{
5098 struct nfs4_ol_stateid *stp = openlockstateid(s);
5099 __be32 ret;
5100
5101 mutex_lock(&stp->st_mutex);
5102
5103 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5104 if (ret)
5105 goto out;
5106
5107 ret = nfserr_locks_held;
5108 if (check_for_locks(stp->st_stid.sc_file,
5109 lockowner(stp->st_stateowner)))
5110 goto out;
5111
5112 release_lock_stateid(stp);
5113 ret = nfs_ok;
5114
5115out:
5116 mutex_unlock(&stp->st_mutex);
5117 nfs4_put_stid(s);
5118 return ret;
5119}
5120
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005121__be32
5122nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5123 struct nfsd4_free_stateid *free_stateid)
5124{
5125 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005126 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005127 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005128 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005129 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005130
Jeff Layton1af71cc2014-07-29 21:34:14 -04005131 spin_lock(&cl->cl_lock);
5132 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005133 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005134 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005135 switch (s->sc_type) {
5136 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005137 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005138 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005139 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005140 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5141 if (ret)
5142 break;
5143 ret = nfserr_locks_held;
5144 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005145 case NFS4_LOCK_STID:
Chuck Lever42691392016-08-11 10:37:30 -04005146 atomic_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005147 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04005148 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005149 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005150 case NFS4_REVOKED_DELEG_STID:
5151 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005152 list_del_init(&dp->dl_recall_lru);
5153 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04005154 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005155 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005156 goto out;
5157 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005158 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005159out_unlock:
5160 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005161out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005162 return ret;
5163}
5164
NeilBrown4c4cd222005-07-07 17:59:27 -07005165static inline int
5166setlkflg (int type)
5167{
5168 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5169 RD_STATE : WR_STATE;
5170}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005172static __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 -04005173{
5174 struct svc_fh *current_fh = &cstate->current_fh;
5175 struct nfs4_stateowner *sop = stp->st_stateowner;
5176 __be32 status;
5177
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005178 status = nfsd4_check_seqid(cstate, sop, seqid);
5179 if (status)
5180 return status;
Trond Myklebust2a7d4a72017-11-03 08:00:15 -04005181 status = nfsd4_lock_ol_stateid(stp);
5182 if (status != nfs_ok)
5183 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005184 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04005185 if (status == nfs_ok)
5186 status = nfs4_check_fh(current_fh, &stp->st_stid);
5187 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005188 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005189 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005190}
5191
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192/*
5193 * Checks for sequence id mutating operations.
5194 */
Al Virob37ad282006-10-19 23:28:59 -07005195static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03005196nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04005197 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005198 struct nfs4_ol_stateid **stpp,
5199 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200{
J. Bruce Fields0836f582008-01-26 19:08:12 -05005201 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005202 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005203 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005205 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5206 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07005207
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005209 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005210 if (status)
5211 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005212 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005213 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005215 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005216 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005217 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04005218 else
5219 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005220 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005221}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05005222
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005223static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5224 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005225{
5226 __be32 status;
5227 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005228 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005230 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005231 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04005232 if (status)
5233 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005234 oo = openowner(stp->st_stateowner);
5235 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005236 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005237 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07005238 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005239 }
5240 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07005241 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242}
5243
Al Virob37ad282006-10-19 23:28:59 -07005244__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005245nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005246 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247{
Al Virob37ad282006-10-19 23:28:59 -07005248 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005249 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005250 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005251 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252
Al Viroa6a9f182013-09-16 10:57:01 -04005253 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5254 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005256 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07005257 if (status)
5258 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005260 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005261 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005262 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005263 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005264 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005265 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005266 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005267 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005268 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04005269 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005270 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04005271 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04005272 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005273 mutex_unlock(&stp->st_mutex);
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005274 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005275 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07005276
Jeff Layton2a4317c2012-03-21 16:42:43 -04005277 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005278 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04005279put_stateid:
5280 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005282 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 return status;
5284}
5285
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005286static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005288 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005289 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04005290 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005291 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005292}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04005293
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005294static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5295{
5296 switch (to_access) {
5297 case NFS4_SHARE_ACCESS_READ:
5298 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5299 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5300 break;
5301 case NFS4_SHARE_ACCESS_WRITE:
5302 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5303 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5304 break;
5305 case NFS4_SHARE_ACCESS_BOTH:
5306 break;
5307 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005308 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 }
5310}
5311
Al Virob37ad282006-10-19 23:28:59 -07005312__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005313nfsd4_open_downgrade(struct svc_rqst *rqstp,
5314 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005315 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316{
Al Virob37ad282006-10-19 23:28:59 -07005317 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005318 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005319 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320
Al Viroa6a9f182013-09-16 10:57:01 -04005321 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5322 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04005324 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02005325 if (od->od_deleg_want)
5326 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5327 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005329 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005330 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005331 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005334 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005335 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005337 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04005339 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005340 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005342 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005344 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04005345 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04005346 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005348put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005349 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005350 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005352 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 return status;
5354}
5355
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005356static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5357{
Trond Myklebustacf92952014-06-30 11:48:37 -04005358 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04005359 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04005360 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005361
Jeff Layton2c41beb2014-07-29 21:34:41 -04005362 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04005363 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005364
Jeff Laytond83017f2014-07-29 21:34:42 -04005365 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04005366 if (unhashed)
5367 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04005368 spin_unlock(&clp->cl_lock);
5369 free_ol_stateid_reaplist(&reaplist);
5370 } else {
5371 spin_unlock(&clp->cl_lock);
5372 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04005373 if (unhashed)
5374 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04005375 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04005376}
5377
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378/*
5379 * nfs4_unlock_state() called after encode
5380 */
Al Virob37ad282006-10-19 23:28:59 -07005381__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005382nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005383 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384{
Al Virob37ad282006-10-19 23:28:59 -07005385 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005386 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005387 struct net *net = SVC_NET(rqstp);
5388 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389
Al Viroa6a9f182013-09-16 10:57:01 -04005390 dprintk("NFSD: nfsd4_close on file %pd\n",
5391 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005393 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5394 &close->cl_stateid,
5395 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005396 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005397 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005398 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 goto out;
Trond Myklebust1c8ea412017-11-03 08:00:10 -04005400
5401 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton9767feb2015-10-01 09:05:50 -04005402 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005404 nfsd4_close_open_stateid(stp);
Trond Myklebust1c8ea412017-11-03 08:00:10 -04005405 mutex_unlock(&stp->st_mutex);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005406
Trond Myklebust5cd35862017-11-03 08:00:12 -04005407 /* See RFC5661 sectionm 18.2.4 */
5408 if (stp->st_stid.sc_client->cl_minorversion)
5409 memcpy(&close->cl_stateid, &close_stateid,
5410 sizeof(close->cl_stateid));
5411
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005412 /* put reference from nfs4_preprocess_seqid_op */
5413 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 return status;
5416}
5417
Al Virob37ad282006-10-19 23:28:59 -07005418__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005419nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5420 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005422 struct nfs4_delegation *dp;
5423 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005424 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07005425 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005426 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005428 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005429 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005431 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005432 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005433 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005434 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04005435 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005436 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04005437 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005438
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005439 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005440put_stateid:
5441 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442out:
5443 return status;
5444}
5445
Benny Halevy87df4de2008-12-15 19:42:03 +02005446static inline u64
5447end_offset(u64 start, u64 len)
5448{
5449 u64 end;
5450
5451 end = start + len;
5452 return end >= start ? end: NFS4_MAX_UINT64;
5453}
5454
5455/* last octet in a range */
5456static inline u64
5457last_byte_offset(u64 start, u64 len)
5458{
5459 u64 end;
5460
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005461 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02005462 end = start + len;
5463 return end > start ? end - 1: NFS4_MAX_UINT64;
5464}
5465
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466/*
5467 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5468 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5469 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5470 * locking, this prevents us from being completely protocol-compliant. The
5471 * real solution to this problem is to start using unsigned file offsets in
5472 * the VFS, but this is a very deep change!
5473 */
5474static inline void
5475nfs4_transform_lock_offset(struct file_lock *lock)
5476{
5477 if (lock->fl_start < 0)
5478 lock->fl_start = OFFSET_MAX;
5479 if (lock->fl_end < 0)
5480 lock->fl_end = OFFSET_MAX;
5481}
5482
Jeff Laytoncae80b32015-04-03 09:04:04 -04005483static fl_owner_t
5484nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005485{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005486 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5487
5488 nfs4_get_stateowner(&lo->lo_owner);
5489 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005490}
5491
Jeff Laytoncae80b32015-04-03 09:04:04 -04005492static void
5493nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005494{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005495 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005496
Jeff Laytoncae80b32015-04-03 09:04:04 -04005497 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04005498 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04005499}
5500
Jeff Layton76d348f2016-09-16 16:28:24 -04005501static void
5502nfsd4_lm_notify(struct file_lock *fl)
5503{
5504 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
5505 struct net *net = lo->lo_owner.so_client->net;
5506 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5507 struct nfsd4_blocked_lock *nbl = container_of(fl,
5508 struct nfsd4_blocked_lock, nbl_lock);
5509 bool queue = false;
5510
Jeff Layton7919d0a2016-09-16 16:28:25 -04005511 /* An empty list means that something else is going to be using it */
Jeff Layton0cc11a62016-10-20 09:34:31 -04005512 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005513 if (!list_empty(&nbl->nbl_list)) {
5514 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005515 list_del_init(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -04005516 queue = true;
5517 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04005518 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005519
5520 if (queue)
5521 nfsd4_run_cb(&nbl->nbl_cb);
5522}
5523
Alexey Dobriyan7b021962009-09-21 17:01:12 -07005524static const struct lock_manager_operations nfsd_posix_mng_ops = {
Jeff Layton76d348f2016-09-16 16:28:24 -04005525 .lm_notify = nfsd4_lm_notify,
Kinglong Meeaef95832014-08-22 10:18:44 -04005526 .lm_get_owner = nfsd4_fl_get_owner,
5527 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07005528};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
5530static inline void
5531nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5532{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005533 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534
NeilBrownd5b90262006-04-10 22:55:22 -07005535 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005536 lo = (struct nfs4_lockowner *) fl->fl_owner;
5537 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5538 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005539 if (!deny->ld_owner.data)
5540 /* We just don't care that much */
5541 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005542 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5543 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07005544 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005545nevermind:
5546 deny->ld_owner.len = 0;
5547 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07005548 deny->ld_clientid.cl_boot = 0;
5549 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 }
5551 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02005552 deny->ld_length = NFS4_MAX_UINT64;
5553 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5555 deny->ld_type = NFS4_READ_LT;
5556 if (fl->fl_type != F_RDLCK)
5557 deny->ld_type = NFS4_WRITE_LT;
5558}
5559
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005560static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005561find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562{
Trond Myklebustd4f04892014-07-29 21:34:36 -04005563 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005564 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565
Trond Myklebust0a880a22014-07-30 08:27:10 -04005566 lockdep_assert_held(&clp->cl_lock);
5567
Trond Myklebustd4f04892014-07-29 21:34:36 -04005568 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5569 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005570 if (so->so_is_open_owner)
5571 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005572 if (same_owner_str(so, owner))
5573 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 }
5575 return NULL;
5576}
5577
Trond Myklebustc58c6612014-07-29 21:34:35 -04005578static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005579find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04005580{
5581 struct nfs4_lockowner *lo;
5582
Trond Myklebustd4f04892014-07-29 21:34:36 -04005583 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005584 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005585 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005586 return lo;
5587}
5588
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005589static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5590{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005591 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005592}
5593
Jeff Layton6b180f02014-07-29 21:34:26 -04005594static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5595{
5596 struct nfs4_lockowner *lo = lockowner(sop);
5597
5598 kmem_cache_free(lockowner_slab, lo);
5599}
5600
5601static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005602 .so_unhash = nfs4_unhash_lockowner,
5603 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005604};
5605
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606/*
5607 * Alloc a lock owner structure.
5608 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005609 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005611 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005613static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005614alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5615 struct nfs4_ol_stateid *open_stp,
5616 struct nfsd4_lock *lock)
5617{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005618 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005620 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5621 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04005623 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005624 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5625 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005626 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005627 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005628 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005629 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005630 if (ret == NULL) {
5631 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005632 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005633 ret = lo;
5634 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08005635 nfs4_free_stateowner(&lo->lo_owner);
5636
Trond Myklebustd4f04892014-07-29 21:34:36 -04005637 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04005638 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639}
5640
Jeff Layton356a95e2014-07-29 21:34:13 -04005641static void
5642init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5643 struct nfs4_file *fp, struct inode *inode,
5644 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005646 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647
Jeff Layton356a95e2014-07-29 21:34:13 -04005648 lockdep_assert_held(&clp->cl_lock);
5649
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005650 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005651 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005652 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07005653 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005654 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005655 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005657 stp->st_openstp = open_stp;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005658 mutex_init(&stp->st_mutex);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005659 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005660 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005661 spin_lock(&fp->fi_lock);
5662 list_add(&stp->st_perfile, &fp->fi_stateids);
5663 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664}
5665
Jeff Laytonc53530d2014-06-30 11:48:39 -04005666static struct nfs4_ol_stateid *
5667find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5668{
5669 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005670 struct nfs4_client *clp = lo->lo_owner.so_client;
5671
5672 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005673
5674 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005675 if (lst->st_stid.sc_file == fp) {
5676 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005677 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005678 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005679 }
5680 return NULL;
5681}
5682
Jeff Layton356a95e2014-07-29 21:34:13 -04005683static struct nfs4_ol_stateid *
5684find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5685 struct inode *inode, struct nfs4_ol_stateid *ost,
5686 bool *new)
5687{
5688 struct nfs4_stid *ns = NULL;
5689 struct nfs4_ol_stateid *lst;
5690 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5691 struct nfs4_client *clp = oo->oo_owner.so_client;
5692
5693 spin_lock(&clp->cl_lock);
5694 lst = find_lock_stateid(lo, fi);
5695 if (lst == NULL) {
5696 spin_unlock(&clp->cl_lock);
Kinglong Mee743146d32017-01-18 19:04:42 +08005697 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
Jeff Layton356a95e2014-07-29 21:34:13 -04005698 if (ns == NULL)
5699 return NULL;
5700
5701 spin_lock(&clp->cl_lock);
5702 lst = find_lock_stateid(lo, fi);
5703 if (likely(!lst)) {
5704 lst = openlockstateid(ns);
5705 init_lock_stateid(lst, lo, fi, inode, ost);
5706 ns = NULL;
5707 *new = true;
5708 }
5709 }
5710 spin_unlock(&clp->cl_lock);
5711 if (ns)
5712 nfs4_put_stid(ns);
5713 return lst;
5714}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005715
NeilBrownfd39ca92005-06-23 22:04:03 -07005716static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717check_lock_length(u64 offset, u64 length)
5718{
Kinglong Meee7969312015-07-13 17:34:19 +08005719 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5720 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721}
5722
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005723static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005724{
Trond Myklebust11b91642014-07-29 21:34:08 -04005725 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005726
Jeff Layton7214e862014-07-10 14:07:33 -04005727 lockdep_assert_held(&fp->fi_lock);
5728
Jeff Layton82c5ff12012-05-11 09:45:13 -04005729 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005730 return;
Jeff Layton12659652014-07-10 14:07:28 -04005731 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005732 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005733}
5734
Jeff Layton356a95e2014-07-29 21:34:13 -04005735static __be32
5736lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5737 struct nfs4_ol_stateid *ost,
5738 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04005739 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005740{
Jeff Layton5db1c032014-07-29 21:34:28 -04005741 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005742 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005743 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5744 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00005745 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005746 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04005747 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005748 unsigned int strhashval;
Jeff Laytondd257932016-08-11 10:37:39 -04005749 bool hashed;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005750
Kinglong Meec8623992015-07-13 17:35:37 +08005751 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005752 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005753 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005754 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5755 if (lo == NULL)
5756 return nfserr_jukebox;
5757 } else {
5758 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005759 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005760 if (!cstate->minorversion &&
5761 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005762 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005763 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005764
Jeff Laytondd257932016-08-11 10:37:39 -04005765retry:
5766 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5767 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005768 status = nfserr_jukebox;
5769 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005770 }
Jeff Laytondd257932016-08-11 10:37:39 -04005771
5772 mutex_lock(&lst->st_mutex);
5773
5774 /* See if it's still hashed to avoid race with FREE_STATEID */
5775 spin_lock(&cl->cl_lock);
5776 hashed = !list_empty(&lst->st_perfile);
5777 spin_unlock(&cl->cl_lock);
5778
5779 if (!hashed) {
5780 mutex_unlock(&lst->st_mutex);
5781 nfs4_put_stid(&lst->st_stid);
5782 goto retry;
5783 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005784 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04005785 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04005786out:
5787 nfs4_put_stateowner(&lo->lo_owner);
5788 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005789}
5790
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791/*
5792 * LOCK operation
5793 */
Al Virob37ad282006-10-19 23:28:59 -07005794__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005795nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005796 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005798 struct nfs4_openowner *open_sop = NULL;
5799 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005800 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005801 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005802 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005803 struct file *filp = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04005804 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005805 struct file_lock *file_lock = NULL;
5806 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005807 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005808 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005809 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005810 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04005811 unsigned char fl_type;
5812 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005813 struct net *net = SVC_NET(rqstp);
5814 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815
5816 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5817 (long long) lock->lk_offset,
5818 (long long) lock->lk_length);
5819
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 if (check_lock_length(lock->lk_offset, lock->lk_length))
5821 return nfserr_inval;
5822
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005823 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005824 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005825 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5826 return status;
5827 }
5828
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005830 if (nfsd4_has_session(cstate))
5831 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005832 memcpy(&lock->lk_new_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04005833 &cstate->session->se_client->cl_clientid,
5834 sizeof(clientid_t));
5835
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005837 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005841 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 lock->lk_new_open_seqid,
5843 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005844 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005845 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005847 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005848 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005849 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005850 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005851 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005852 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005853 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005854 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005855 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005856 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005857 lock->lk_old_lock_seqid,
5858 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005859 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005860 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005861 if (status)
5862 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005863 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005865 lkflg = setlkflg(lock->lk_type);
5866 status = nfs4_check_openmode(lock_stp, lkflg);
5867 if (status)
5868 goto out;
5869
NeilBrown0dd395d2005-07-07 17:59:15 -07005870 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005871 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005872 goto out;
5873 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005874 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005875 goto out;
5876
Trond Myklebust11b91642014-07-29 21:34:08 -04005877 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 case NFS4_READW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04005880 if (nfsd4_has_session(cstate))
5881 fl_flags |= FL_SLEEP;
5882 /* Fallthrough */
5883 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005884 spin_lock(&fp->fi_lock);
5885 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005886 if (filp)
5887 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005888 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005889 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005890 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 case NFS4_WRITEW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04005892 if (nfsd4_has_session(cstate))
5893 fl_flags |= FL_SLEEP;
5894 /* Fallthrough */
5895 case NFS4_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005896 spin_lock(&fp->fi_lock);
5897 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005898 if (filp)
5899 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005900 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005901 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005902 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 default:
5904 status = nfserr_inval;
5905 goto out;
5906 }
Jeff Layton76d348f2016-09-16 16:28:24 -04005907
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005908 if (!filp) {
5909 status = nfserr_openmode;
5910 goto out;
5911 }
Kinglong Meeaef95832014-08-22 10:18:44 -04005912
Jeff Layton76d348f2016-09-16 16:28:24 -04005913 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
5914 if (!nbl) {
5915 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
5916 status = nfserr_jukebox;
5917 goto out;
5918 }
5919
5920 file_lock = &nbl->nbl_lock;
5921 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04005922 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04005923 file_lock->fl_pid = current->tgid;
5924 file_lock->fl_file = filp;
Jeff Layton76d348f2016-09-16 16:28:24 -04005925 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04005926 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5927 file_lock->fl_start = lock->lk_offset;
5928 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5929 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930
Jeff Layton21179d82012-08-21 08:03:32 -04005931 conflock = locks_alloc_lock();
5932 if (!conflock) {
5933 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5934 status = nfserr_jukebox;
5935 goto out;
5936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937
Jeff Layton76d348f2016-09-16 16:28:24 -04005938 if (fl_flags & FL_SLEEP) {
Jeff Layton7919d0a2016-09-16 16:28:25 -04005939 nbl->nbl_time = jiffies;
Jeff Layton0cc11a62016-10-20 09:34:31 -04005940 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005941 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005942 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04005943 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005944 }
5945
Jeff Layton21179d82012-08-21 08:03:32 -04005946 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005947 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04005949 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07005950 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005951 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04005952 case FILE_LOCK_DEFERRED:
5953 nbl = NULL;
5954 /* Fallthrough */
5955 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005956 status = nfserr_denied;
5957 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005958 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005959 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04005960 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005962 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005963 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005964 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005965 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005966 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968out:
Jeff Layton76d348f2016-09-16 16:28:24 -04005969 if (nbl) {
5970 /* dequeue it if we queued it before */
5971 if (fl_flags & FL_SLEEP) {
Jeff Layton0cc11a62016-10-20 09:34:31 -04005972 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005973 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005974 list_del_init(&nbl->nbl_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04005975 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005976 }
5977 free_blocked_lock(nbl);
5978 }
Trond Myklebustde186432014-07-10 14:07:26 -04005979 if (filp)
5980 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005981 if (lock_stp) {
5982 /* Bump seqid manually if the 4.0 replay owner is openowner */
5983 if (cstate->replay_owner &&
5984 cstate->replay_owner != &lock_sop->lo_owner &&
5985 seqid_mutating_err(ntohl(status)))
5986 lock_sop->lo_owner.so_seqid++;
5987
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005988 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005989
Jeff Layton5db1c032014-07-29 21:34:28 -04005990 /*
5991 * If this is a new, never-before-used stateid, and we are
5992 * returning an error, then just go ahead and release it.
5993 */
5994 if (status && new)
5995 release_lock_stateid(lock_stp);
5996
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005997 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005998 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005999 if (open_stp)
6000 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006001 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006002 if (conflock)
6003 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 return status;
6005}
6006
6007/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006008 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6009 * so we do a temporary open here just to get an open file to pass to
6010 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6011 * inode operation.)
6012 */
Al Viro04da6e92012-04-13 00:00:04 -04006013static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006014{
6015 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04006016 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
6017 if (!err) {
6018 err = nfserrno(vfs_test_lock(file, lock));
Christoph Hellwigfd891452015-04-28 15:41:16 +02006019 fput(file);
Al Viro04da6e92012-04-13 00:00:04 -04006020 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006021 return err;
6022}
6023
6024/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 * LOCKT operation
6026 */
Al Virob37ad282006-10-19 23:28:59 -07006027__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006028nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6029 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030{
Jeff Layton21179d82012-08-21 08:03:32 -04006031 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04006032 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006033 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006034 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006036 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037 return nfserr_grace;
6038
6039 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6040 return nfserr_inval;
6041
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006042 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04006043 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006044 if (status)
6045 goto out;
6046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047
J. Bruce Fields75c096f2011-08-15 18:39:32 -04006048 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050
Jeff Layton21179d82012-08-21 08:03:32 -04006051 file_lock = locks_alloc_lock();
6052 if (!file_lock) {
6053 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6054 status = nfserr_jukebox;
6055 goto out;
6056 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006057
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 switch (lockt->lt_type) {
6059 case NFS4_READ_LT:
6060 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006061 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 break;
6063 case NFS4_WRITE_LT:
6064 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006065 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 break;
6067 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04006068 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 status = nfserr_inval;
6070 goto out;
6071 }
6072
Kinglong Meec8623992015-07-13 17:35:37 +08006073 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006074 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04006075 file_lock->fl_owner = (fl_owner_t)lo;
6076 file_lock->fl_pid = current->tgid;
6077 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078
Jeff Layton21179d82012-08-21 08:03:32 -04006079 file_lock->fl_start = lockt->lt_offset;
6080 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081
Jeff Layton21179d82012-08-21 08:03:32 -04006082 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083
Jeff Layton21179d82012-08-21 08:03:32 -04006084 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04006085 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05006086 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04006087
Jeff Layton21179d82012-08-21 08:03:32 -04006088 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04006090 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 }
6092out:
Jeff Layton5db1c032014-07-29 21:34:28 -04006093 if (lo)
6094 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04006095 if (file_lock)
6096 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 return status;
6098}
6099
Al Virob37ad282006-10-19 23:28:59 -07006100__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006101nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08006102 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006104 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006106 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006107 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07006108 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006109 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6110
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6112 (long long) locku->lu_offset,
6113 (long long) locku->lu_length);
6114
6115 if (check_lock_length(locku->lu_offset, locku->lu_length))
6116 return nfserr_inval;
6117
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006118 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006119 &locku->lu_stateid, NFS4_LOCK_STID,
6120 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006121 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04006123 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006124 if (!filp) {
6125 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04006126 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006127 }
Jeff Layton21179d82012-08-21 08:03:32 -04006128 file_lock = locks_alloc_lock();
6129 if (!file_lock) {
6130 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6131 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04006132 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04006133 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006134
Jeff Layton21179d82012-08-21 08:03:32 -04006135 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04006136 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04006137 file_lock->fl_pid = current->tgid;
6138 file_lock->fl_file = filp;
6139 file_lock->fl_flags = FL_POSIX;
6140 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6141 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142
Jeff Layton21179d82012-08-21 08:03:32 -04006143 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6144 locku->lu_length);
6145 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146
Jeff Layton21179d82012-08-21 08:03:32 -04006147 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07006148 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05006149 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 goto out_nfserr;
6151 }
Jeff Layton9767feb2015-10-01 09:05:50 -04006152 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Trond Myklebustde186432014-07-10 14:07:26 -04006153fput:
6154 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04006155put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006156 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04006157 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006159 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006160 if (file_lock)
6161 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162 return status;
6163
6164out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07006165 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04006166 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167}
6168
6169/*
6170 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006171 * true: locks held by lockowner
6172 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006174static bool
6175check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006177 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006178 int status = false;
6179 struct file *filp = find_any_file(fp);
6180 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006181 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006182
6183 if (!filp) {
6184 /* Any valid lock stateid should have some sort of access */
6185 WARN_ON_ONCE(1);
6186 return status;
6187 }
6188
6189 inode = file_inode(filp);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006190 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006192 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05006193 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006194 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6195 if (fl->fl_owner == (fl_owner_t)lowner) {
6196 status = true;
6197 break;
6198 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08006199 }
Jeff Layton6109c852015-01-16 15:05:57 -05006200 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201 }
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006202 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203 return status;
6204}
6205
Al Virob37ad282006-10-19 23:28:59 -07006206__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08006207nfsd4_release_lockowner(struct svc_rqst *rqstp,
6208 struct nfsd4_compound_state *cstate,
6209 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210{
6211 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04006212 struct nfs4_stateowner *sop;
6213 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006214 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006216 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07006217 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006218 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006219 struct nfs4_client *clp;
Chuck Lever88584812016-07-13 16:40:14 -04006220 LIST_HEAD (reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221
6222 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6223 clid->cl_boot, clid->cl_id);
6224
Jeff Layton4b24ca72014-06-30 11:48:44 -04006225 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006226 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04006227 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006228
Trond Myklebustd4f04892014-07-29 21:34:36 -04006229 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04006230 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04006231 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04006232 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04006233 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04006234
6235 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006236 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04006237
Jeff Layton882e9d22014-07-29 21:34:37 -04006238 /* see if there are still any locks associated with it */
6239 lo = lockowner(sop);
6240 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6241 if (check_for_locks(stp->st_stid.sc_file, lo)) {
6242 status = nfserr_locks_held;
6243 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04006244 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04006245 }
Jeff Layton5adfd882014-07-29 21:34:31 -04006246 }
Jeff Layton882e9d22014-07-29 21:34:37 -04006247
Kinglong Meeb5971af2014-08-22 10:18:43 -04006248 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04006249 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04006250 }
Chuck Lever88584812016-07-13 16:40:14 -04006251 if (!lo) {
6252 spin_unlock(&clp->cl_lock);
6253 return status;
6254 }
6255
6256 unhash_lockowner_locked(lo);
6257 while (!list_empty(&lo->lo_owner.so_stateids)) {
6258 stp = list_first_entry(&lo->lo_owner.so_stateids,
6259 struct nfs4_ol_stateid,
6260 st_perstateowner);
6261 WARN_ON(!unhash_lock_stateid(stp));
6262 put_ol_stateid_locked(stp, &reaplist);
6263 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04006264 spin_unlock(&clp->cl_lock);
Chuck Lever88584812016-07-13 16:40:14 -04006265 free_ol_stateid_reaplist(&reaplist);
6266 nfs4_put_stateowner(&lo->lo_owner);
6267
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268 return status;
6269}
6270
6271static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07006272alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273{
NeilBrowna55370a2005-06-23 22:03:52 -07006274 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275}
6276
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006277bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006278nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07006279{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006280 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07006281
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006282 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006283 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07006284}
6285
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286/*
6287 * failure => all reset bets are off, nfserr_no_grace...
6288 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05006289struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006290nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291{
6292 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006293 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294
NeilBrowna55370a2005-06-23 22:03:52 -07006295 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
6296 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05006297 if (crp) {
6298 strhashval = clientstr_hashval(name);
6299 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006300 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05006301 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006302 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006303 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006304 }
6305 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306}
6307
Jeff Layton2a4317c2012-03-21 16:42:43 -04006308void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006309nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05006310{
6311 list_del(&crp->cr_strhash);
6312 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006313 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05006314}
6315
6316void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006317nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318{
6319 struct nfs4_client_reclaim *crp = NULL;
6320 int i;
6321
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006323 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6324 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006326 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327 }
6328 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006329 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330}
6331
6332/*
6333 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04006334struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006335nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336{
6337 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338 struct nfs4_client_reclaim *crp = NULL;
6339
Jeff Layton278c9312012-11-12 15:00:52 -05006340 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341
Jeff Layton278c9312012-11-12 15:00:52 -05006342 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006343 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05006344 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345 return crp;
6346 }
6347 }
6348 return NULL;
6349}
6350
6351/*
6352* Called from OPEN. Look for clientid in reclaim list.
6353*/
Al Virob37ad282006-10-19 23:28:59 -07006354__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006355nfs4_check_open_reclaim(clientid_t *clid,
6356 struct nfsd4_compound_state *cstate,
6357 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006359 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04006360
6361 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006362 status = lookup_clientid(clid, cstate, nn);
6363 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04006364 return nfserr_reclaim_bad;
6365
Jeff Layton3b3e7b72014-09-12 16:40:22 -04006366 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6367 return nfserr_no_grace;
6368
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006369 if (nfsd4_client_record_check(cstate->clp))
6370 return nfserr_reclaim_bad;
6371
6372 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373}
6374
Bryan Schumaker65178db2011-11-01 13:35:21 -04006375#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04006376static inline void
6377put_client(struct nfs4_client *clp)
6378{
6379 atomic_dec(&clp->cl_refcount);
6380}
6381
Jeff Layton285abde2014-07-30 08:27:24 -04006382static struct nfs4_client *
6383nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6384{
6385 struct nfs4_client *clp;
6386 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6387 nfsd_net_id);
6388
6389 if (!nfsd_netns_ready(nn))
6390 return NULL;
6391
6392 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6393 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
6394 return clp;
6395 }
6396 return NULL;
6397}
6398
Jeff Layton7ec0e362014-07-30 08:27:17 -04006399u64
Jeff Layton285abde2014-07-30 08:27:24 -04006400nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04006401{
6402 struct nfs4_client *clp;
6403 u64 count = 0;
6404 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6405 nfsd_net_id);
6406 char buf[INET6_ADDRSTRLEN];
6407
6408 if (!nfsd_netns_ready(nn))
6409 return 0;
6410
6411 spin_lock(&nn->client_lock);
6412 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6413 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6414 pr_info("NFS Client: %s\n", buf);
6415 ++count;
6416 }
6417 spin_unlock(&nn->client_lock);
6418
6419 return count;
6420}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006421
Jeff Laytona0926d12014-07-30 08:27:18 -04006422u64
Jeff Layton285abde2014-07-30 08:27:24 -04006423nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04006424{
6425 u64 count = 0;
6426 struct nfs4_client *clp;
6427 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6428 nfsd_net_id);
6429
6430 if (!nfsd_netns_ready(nn))
6431 return count;
6432
6433 spin_lock(&nn->client_lock);
6434 clp = nfsd_find_client(addr, addr_size);
6435 if (clp) {
6436 if (mark_client_expired_locked(clp) == nfs_ok)
6437 ++count;
6438 else
6439 clp = NULL;
6440 }
6441 spin_unlock(&nn->client_lock);
6442
6443 if (clp)
6444 expire_client(clp);
6445
6446 return count;
6447}
6448
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006449u64
Jeff Layton285abde2014-07-30 08:27:24 -04006450nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006451{
6452 u64 count = 0;
6453 struct nfs4_client *clp, *next;
6454 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6455 nfsd_net_id);
6456 LIST_HEAD(reaplist);
6457
6458 if (!nfsd_netns_ready(nn))
6459 return count;
6460
6461 spin_lock(&nn->client_lock);
6462 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6463 if (mark_client_expired_locked(clp) == nfs_ok) {
6464 list_add(&clp->cl_lru, &reaplist);
6465 if (max != 0 && ++count >= max)
6466 break;
6467 }
6468 }
6469 spin_unlock(&nn->client_lock);
6470
6471 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6472 expire_client(clp);
6473
6474 return count;
6475}
6476
Bryan Schumaker184c1842012-11-29 11:40:44 -05006477static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6478 const char *type)
6479{
6480 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05006481 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05006482 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6483}
6484
Jeff Layton016200c2014-07-30 08:27:21 -04006485static void
6486nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6487 struct list_head *collect)
6488{
6489 struct nfs4_client *clp = lst->st_stid.sc_client;
6490 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6491 nfsd_net_id);
6492
6493 if (!collect)
6494 return;
6495
6496 lockdep_assert_held(&nn->client_lock);
6497 atomic_inc(&clp->cl_refcount);
6498 list_add(&lst->st_locks, collect);
6499}
6500
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006501static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04006502 struct list_head *collect,
Jeff Laytone8568732015-08-24 12:41:47 -04006503 bool (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05006504{
6505 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006506 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006507 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006508 u64 count = 0;
6509
Jeff Layton016200c2014-07-30 08:27:21 -04006510 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006511 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006512 list_for_each_entry_safe(stp, st_next,
6513 &oop->oo_owner.so_stateids, st_perstateowner) {
6514 list_for_each_entry_safe(lst, lst_next,
6515 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04006516 if (func) {
Jeff Laytone8568732015-08-24 12:41:47 -04006517 if (func(lst))
6518 nfsd_inject_add_lock_to_list(lst,
6519 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04006520 }
Jeff Layton016200c2014-07-30 08:27:21 -04006521 ++count;
6522 /*
6523 * Despite the fact that these functions deal
6524 * with 64-bit integers for "count", we must
6525 * ensure that it doesn't blow up the
6526 * clp->cl_refcount. Throw a warning if we
6527 * start to approach INT_MAX here.
6528 */
6529 WARN_ON_ONCE(count == (INT_MAX / 2));
6530 if (count == max)
6531 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006532 }
6533 }
6534 }
Jeff Layton016200c2014-07-30 08:27:21 -04006535out:
6536 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006537
6538 return count;
6539}
6540
Jeff Layton016200c2014-07-30 08:27:21 -04006541static u64
6542nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6543 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05006544{
Jeff Layton016200c2014-07-30 08:27:21 -04006545 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006546}
6547
Jeff Layton016200c2014-07-30 08:27:21 -04006548static u64
6549nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006550{
Jeff Layton016200c2014-07-30 08:27:21 -04006551 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006552 nfsd_print_count(clp, count, "locked files");
6553 return count;
6554}
6555
Jeff Layton016200c2014-07-30 08:27:21 -04006556u64
Jeff Layton285abde2014-07-30 08:27:24 -04006557nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04006558{
6559 struct nfs4_client *clp;
6560 u64 count = 0;
6561 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6562 nfsd_net_id);
6563
6564 if (!nfsd_netns_ready(nn))
6565 return 0;
6566
6567 spin_lock(&nn->client_lock);
6568 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6569 count += nfsd_print_client_locks(clp);
6570 spin_unlock(&nn->client_lock);
6571
6572 return count;
6573}
6574
6575static void
6576nfsd_reap_locks(struct list_head *reaplist)
6577{
6578 struct nfs4_client *clp;
6579 struct nfs4_ol_stateid *stp, *next;
6580
6581 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6582 list_del_init(&stp->st_locks);
6583 clp = stp->st_stid.sc_client;
6584 nfs4_put_stid(&stp->st_stid);
6585 put_client(clp);
6586 }
6587}
6588
6589u64
Jeff Layton285abde2014-07-30 08:27:24 -04006590nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04006591{
6592 unsigned int count = 0;
6593 struct nfs4_client *clp;
6594 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6595 nfsd_net_id);
6596 LIST_HEAD(reaplist);
6597
6598 if (!nfsd_netns_ready(nn))
6599 return count;
6600
6601 spin_lock(&nn->client_lock);
6602 clp = nfsd_find_client(addr, addr_size);
6603 if (clp)
6604 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6605 spin_unlock(&nn->client_lock);
6606 nfsd_reap_locks(&reaplist);
6607 return count;
6608}
6609
6610u64
Jeff Layton285abde2014-07-30 08:27:24 -04006611nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04006612{
6613 u64 count = 0;
6614 struct nfs4_client *clp;
6615 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6616 nfsd_net_id);
6617 LIST_HEAD(reaplist);
6618
6619 if (!nfsd_netns_ready(nn))
6620 return count;
6621
6622 spin_lock(&nn->client_lock);
6623 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6624 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6625 if (max != 0 && count >= max)
6626 break;
6627 }
6628 spin_unlock(&nn->client_lock);
6629 nfsd_reap_locks(&reaplist);
6630 return count;
6631}
6632
Jeff Layton82e05ef2014-07-30 08:27:22 -04006633static u64
6634nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6635 struct list_head *collect,
6636 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006637{
6638 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04006639 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6640 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006641 u64 count = 0;
6642
Jeff Layton82e05ef2014-07-30 08:27:22 -04006643 lockdep_assert_held(&nn->client_lock);
6644
6645 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006646 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04006647 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006648 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04006649 if (collect) {
6650 atomic_inc(&clp->cl_refcount);
6651 list_add(&oop->oo_perclient, collect);
6652 }
6653 }
6654 ++count;
6655 /*
6656 * Despite the fact that these functions deal with
6657 * 64-bit integers for "count", we must ensure that
6658 * it doesn't blow up the clp->cl_refcount. Throw a
6659 * warning if we start to approach INT_MAX here.
6660 */
6661 WARN_ON_ONCE(count == (INT_MAX / 2));
6662 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006663 break;
6664 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04006665 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006666
6667 return count;
6668}
6669
Jeff Layton82e05ef2014-07-30 08:27:22 -04006670static u64
6671nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006672{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006673 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6674
6675 nfsd_print_count(clp, count, "openowners");
6676 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006677}
6678
Jeff Layton82e05ef2014-07-30 08:27:22 -04006679static u64
6680nfsd_collect_client_openowners(struct nfs4_client *clp,
6681 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006682{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006683 return nfsd_foreach_client_openowner(clp, max, collect,
6684 unhash_openowner_locked);
6685}
6686
6687u64
Jeff Layton285abde2014-07-30 08:27:24 -04006688nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006689{
6690 struct nfs4_client *clp;
6691 u64 count = 0;
6692 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6693 nfsd_net_id);
6694
6695 if (!nfsd_netns_ready(nn))
6696 return 0;
6697
6698 spin_lock(&nn->client_lock);
6699 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6700 count += nfsd_print_client_openowners(clp);
6701 spin_unlock(&nn->client_lock);
6702
6703 return count;
6704}
6705
6706static void
6707nfsd_reap_openowners(struct list_head *reaplist)
6708{
6709 struct nfs4_client *clp;
6710 struct nfs4_openowner *oop, *next;
6711
6712 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6713 list_del_init(&oop->oo_perclient);
6714 clp = oop->oo_owner.so_client;
6715 release_openowner(oop);
6716 put_client(clp);
6717 }
6718}
6719
6720u64
Jeff Layton285abde2014-07-30 08:27:24 -04006721nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6722 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006723{
6724 unsigned int count = 0;
6725 struct nfs4_client *clp;
6726 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6727 nfsd_net_id);
6728 LIST_HEAD(reaplist);
6729
6730 if (!nfsd_netns_ready(nn))
6731 return count;
6732
6733 spin_lock(&nn->client_lock);
6734 clp = nfsd_find_client(addr, addr_size);
6735 if (clp)
6736 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6737 spin_unlock(&nn->client_lock);
6738 nfsd_reap_openowners(&reaplist);
6739 return count;
6740}
6741
6742u64
Jeff Layton285abde2014-07-30 08:27:24 -04006743nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006744{
6745 u64 count = 0;
6746 struct nfs4_client *clp;
6747 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6748 nfsd_net_id);
6749 LIST_HEAD(reaplist);
6750
6751 if (!nfsd_netns_ready(nn))
6752 return count;
6753
6754 spin_lock(&nn->client_lock);
6755 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6756 count += nfsd_collect_client_openowners(clp, &reaplist,
6757 max - count);
6758 if (max != 0 && count >= max)
6759 break;
6760 }
6761 spin_unlock(&nn->client_lock);
6762 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006763 return count;
6764}
6765
Bryan Schumaker269de302012-11-29 11:40:42 -05006766static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6767 struct list_head *victims)
6768{
6769 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006770 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6771 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006772 u64 count = 0;
6773
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006774 lockdep_assert_held(&nn->client_lock);
6775
6776 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006777 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006778 if (victims) {
6779 /*
6780 * It's not safe to mess with delegations that have a
6781 * non-zero dl_time. They might have already been broken
6782 * and could be processed by the laundromat outside of
6783 * the state_lock. Just leave them be.
6784 */
6785 if (dp->dl_time != 0)
6786 continue;
6787
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006788 atomic_inc(&clp->cl_refcount);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04006789 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04006790 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006791 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006792 ++count;
6793 /*
6794 * Despite the fact that these functions deal with
6795 * 64-bit integers for "count", we must ensure that
6796 * it doesn't blow up the clp->cl_refcount. Throw a
6797 * warning if we start to approach INT_MAX here.
6798 */
6799 WARN_ON_ONCE(count == (INT_MAX / 2));
6800 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006801 break;
6802 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006803 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006804 return count;
6805}
6806
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006807static u64
6808nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006809{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006810 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006811
6812 nfsd_print_count(clp, count, "delegations");
6813 return count;
6814}
6815
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006816u64
Jeff Layton285abde2014-07-30 08:27:24 -04006817nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006818{
6819 struct nfs4_client *clp;
6820 u64 count = 0;
6821 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6822 nfsd_net_id);
6823
6824 if (!nfsd_netns_ready(nn))
6825 return 0;
6826
6827 spin_lock(&nn->client_lock);
6828 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6829 count += nfsd_print_client_delegations(clp);
6830 spin_unlock(&nn->client_lock);
6831
6832 return count;
6833}
6834
6835static void
6836nfsd_forget_delegations(struct list_head *reaplist)
6837{
6838 struct nfs4_client *clp;
6839 struct nfs4_delegation *dp, *next;
6840
6841 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6842 list_del_init(&dp->dl_recall_lru);
6843 clp = dp->dl_stid.sc_client;
6844 revoke_delegation(dp);
6845 put_client(clp);
6846 }
6847}
6848
6849u64
Jeff Layton285abde2014-07-30 08:27:24 -04006850nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6851 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006852{
6853 u64 count = 0;
6854 struct nfs4_client *clp;
6855 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6856 nfsd_net_id);
6857 LIST_HEAD(reaplist);
6858
6859 if (!nfsd_netns_ready(nn))
6860 return count;
6861
6862 spin_lock(&nn->client_lock);
6863 clp = nfsd_find_client(addr, addr_size);
6864 if (clp)
6865 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6866 spin_unlock(&nn->client_lock);
6867
6868 nfsd_forget_delegations(&reaplist);
6869 return count;
6870}
6871
6872u64
Jeff Layton285abde2014-07-30 08:27:24 -04006873nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006874{
6875 u64 count = 0;
6876 struct nfs4_client *clp;
6877 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6878 nfsd_net_id);
6879 LIST_HEAD(reaplist);
6880
6881 if (!nfsd_netns_ready(nn))
6882 return count;
6883
6884 spin_lock(&nn->client_lock);
6885 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6886 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6887 if (max != 0 && count >= max)
6888 break;
6889 }
6890 spin_unlock(&nn->client_lock);
6891 nfsd_forget_delegations(&reaplist);
6892 return count;
6893}
6894
6895static void
6896nfsd_recall_delegations(struct list_head *reaplist)
6897{
6898 struct nfs4_client *clp;
6899 struct nfs4_delegation *dp, *next;
6900
6901 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6902 list_del_init(&dp->dl_recall_lru);
6903 clp = dp->dl_stid.sc_client;
6904 /*
6905 * We skipped all entries that had a zero dl_time before,
6906 * so we can now reset the dl_time back to 0. If a delegation
6907 * break comes in now, then it won't make any difference since
6908 * we're recalling it either way.
6909 */
6910 spin_lock(&state_lock);
6911 dp->dl_time = 0;
6912 spin_unlock(&state_lock);
6913 nfsd_break_one_deleg(dp);
6914 put_client(clp);
6915 }
6916}
6917
6918u64
Jeff Layton285abde2014-07-30 08:27:24 -04006919nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006920 size_t addr_size)
6921{
6922 u64 count = 0;
6923 struct nfs4_client *clp;
6924 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6925 nfsd_net_id);
6926 LIST_HEAD(reaplist);
6927
6928 if (!nfsd_netns_ready(nn))
6929 return count;
6930
6931 spin_lock(&nn->client_lock);
6932 clp = nfsd_find_client(addr, addr_size);
6933 if (clp)
6934 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6935 spin_unlock(&nn->client_lock);
6936
6937 nfsd_recall_delegations(&reaplist);
6938 return count;
6939}
6940
6941u64
Jeff Layton285abde2014-07-30 08:27:24 -04006942nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006943{
6944 u64 count = 0;
6945 struct nfs4_client *clp, *next;
6946 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6947 nfsd_net_id);
6948 LIST_HEAD(reaplist);
6949
6950 if (!nfsd_netns_ready(nn))
6951 return count;
6952
6953 spin_lock(&nn->client_lock);
6954 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6955 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6956 if (max != 0 && ++count >= max)
6957 break;
6958 }
6959 spin_unlock(&nn->client_lock);
6960 nfsd_recall_delegations(&reaplist);
6961 return count;
6962}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006963#endif /* CONFIG_NFSD_FAULT_INJECTION */
6964
Meelap Shahc2f1a552007-07-17 04:04:39 -07006965/*
6966 * Since the lifetime of a delegation isn't limited to that of an open, a
6967 * client may quite reasonably hang on to a delegation as long as it has
6968 * the inode cached. This becomes an obvious problem the first time a
6969 * client's inode cache approaches the size of the server's total memory.
6970 *
6971 * For now we avoid this problem by imposing a hard limit on the number
6972 * of delegations, which varies according to the server's memory size.
6973 */
6974static void
6975set_max_delegations(void)
6976{
6977 /*
6978 * Allow at most 4 delegations per megabyte of RAM. Quick
6979 * estimates suggest that in the worst case (where every delegation
6980 * is for a different inode), a delegation could take about 1.5K,
6981 * giving a worst case usage of about 6% of memory.
6982 */
6983 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6984}
6985
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006986static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006987{
6988 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6989 int i;
6990
6991 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6992 CLIENT_HASH_SIZE, GFP_KERNEL);
6993 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006994 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006995 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6996 CLIENT_HASH_SIZE, GFP_KERNEL);
6997 if (!nn->unconf_id_hashtbl)
6998 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006999 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
7000 SESSION_HASH_SIZE, GFP_KERNEL);
7001 if (!nn->sessionid_hashtbl)
7002 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007003
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007004 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007005 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007006 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007007 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007008 for (i = 0; i < SESSION_HASH_SIZE; i++)
7009 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007010 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007011 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03007012 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03007013 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007014 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03007015 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007016
Jeff Layton0cc11a62016-10-20 09:34:31 -04007017 spin_lock_init(&nn->blocked_locks_lock);
7018 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7019
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007020 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007021 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007022
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007023 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007024
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007025err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03007026 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007027err_unconf_id:
7028 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007029err:
7030 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007031}
7032
7033static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007034nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007035{
7036 int i;
7037 struct nfs4_client *clp = NULL;
7038 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7039
7040 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7041 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7042 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7043 destroy_client(clp);
7044 }
7045 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007046
Kinglong Mee2b905632014-03-26 22:09:30 +08007047 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7048 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7049 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7050 destroy_client(clp);
7051 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007052 }
7053
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007054 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007055 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007056 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007057 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007058}
7059
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007060int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007061nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07007062{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04007063 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007064 int ret;
7065
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007066 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007067 if (ret)
7068 return ret;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04007069 nn->boot_time = get_seconds();
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04007070 nn->grace_ended = false;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04007071 nn->nfsd4_manager.block_opens = true;
Jeff Laytond4318ac2014-09-12 16:40:21 -04007072 locks_start_grace(net, &nn->nfsd4_manager);
7073 nfsd4_client_tracking_init(net);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007074 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03007075 nn->nfsd4_grace, net);
7076 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007077 return 0;
7078}
7079
7080/* initialization to perform when the nfsd service is started: */
7081
7082int
7083nfs4_state_start(void)
7084{
7085 int ret;
7086
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007087 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007088 if (ret)
Kinglong Meed4bda742017-02-05 09:57:07 +08007089 return ret;
7090
Jeff Layton51a54562015-08-20 07:17:01 -04007091 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05007092 if (laundry_wq == NULL) {
7093 ret = -ENOMEM;
Kinglong Meed4bda742017-02-05 09:57:07 +08007094 goto out_cleanup_cred;
Jeff Laytona6d6b782012-03-05 11:42:36 -05007095 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007096 ret = nfsd4_create_callback_queue();
7097 if (ret)
7098 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007099
Meelap Shahc2f1a552007-07-17 04:04:39 -07007100 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007101 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007102
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007103out_free_laundry:
7104 destroy_workqueue(laundry_wq);
Kinglong Meed4bda742017-02-05 09:57:07 +08007105out_cleanup_cred:
7106 cleanup_callback_cred();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007107 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108}
7109
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007110void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007111nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007115 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Jeff Layton7919d0a2016-09-16 16:28:25 -04007116 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007117
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007118 cancel_delayed_work_sync(&nn->laundromat_work);
7119 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05007120
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04007122 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007123 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007125 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007126 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127 }
Benny Halevycdc97502014-05-30 09:09:30 -04007128 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129 list_for_each_safe(pos, next, &reaplist) {
7130 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04007131 list_del_init(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +02007132 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -04007133 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04007134 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 }
7136
Jeff Layton7919d0a2016-09-16 16:28:25 -04007137 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04007138 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04007139 while (!list_empty(&nn->blocked_locks_lru)) {
7140 nbl = list_first_entry(&nn->blocked_locks_lru,
7141 struct nfsd4_blocked_lock, nbl_lru);
7142 list_move(&nbl->nbl_lru, &reaplist);
7143 list_del_init(&nbl->nbl_list);
7144 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04007145 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04007146
7147 while (!list_empty(&reaplist)) {
Naofumi Hondaaa1a6cf2017-11-09 10:57:16 -05007148 nbl = list_first_entry(&reaplist,
Jeff Layton7919d0a2016-09-16 16:28:25 -04007149 struct nfsd4_blocked_lock, nbl_lru);
7150 list_del_init(&nbl->nbl_lru);
7151 posix_unblock_lock(&nbl->nbl_lock);
7152 free_blocked_lock(nbl);
7153 }
7154
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007155 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007156 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157}
7158
7159void
7160nfs4_state_shutdown(void)
7161{
NeilBrown5e8d5c22006-04-10 22:55:37 -07007162 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04007163 nfsd4_destroy_callback_queue();
Kinglong Meed4bda742017-02-05 09:57:07 +08007164 cleanup_callback_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007166
7167static void
7168get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7169{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007170 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
7171 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007172}
7173
7174static void
7175put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7176{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007177 if (cstate->minorversion) {
7178 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7179 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7180 }
7181}
7182
7183void
7184clear_current_stateid(struct nfsd4_compound_state *cstate)
7185{
7186 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007187}
7188
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007189/*
7190 * functions to set current state id
7191 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007192void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007193nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
7194{
7195 put_stateid(cstate, &odp->od_stateid);
7196}
7197
7198void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007199nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
7200{
7201 put_stateid(cstate, &open->op_stateid);
7202}
7203
7204void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007205nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
7206{
7207 put_stateid(cstate, &close->cl_stateid);
7208}
7209
7210void
7211nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
7212{
7213 put_stateid(cstate, &lock->lk_resp_stateid);
7214}
7215
7216/*
7217 * functions to consume current state id
7218 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007219
7220void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007221nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
7222{
7223 get_stateid(cstate, &odp->od_stateid);
7224}
7225
7226void
7227nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
7228{
7229 get_stateid(cstate, &drp->dr_stateid);
7230}
7231
7232void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007233nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
7234{
7235 get_stateid(cstate, &fsp->fr_stateid);
7236}
7237
7238void
7239nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
7240{
7241 get_stateid(cstate, &setattr->sa_stateid);
7242}
7243
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007244void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007245nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
7246{
7247 get_stateid(cstate, &close->cl_stateid);
7248}
7249
7250void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007251nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007252{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007253 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007254}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007255
7256void
7257nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
7258{
7259 get_stateid(cstate, &read->rd_stateid);
7260}
7261
7262void
7263nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
7264{
7265 get_stateid(cstate, &write->wr_stateid);
7266}