blob: 532a60cca2fbd62f423ed9384db1e39a690389e0 [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"
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define NFSDDBG_FACILITY NFSDDBG_PROC
53
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050054#define all_ones {{~0,~0},~0}
55static const stateid_t one_stateid = {
56 .si_generation = ~0,
57 .si_opaque = all_ones,
58};
59static const stateid_t zero_stateid = {
60 /* all fields zero */
61};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010062static const stateid_t currentstateid = {
63 .si_generation = 1,
64};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050065
Andy Adamsonec6b5d72009-04-03 08:28:28 +030066static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070067
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050068#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
69#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010070#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040073static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040074static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
J. Bruce Fields8b671b82009-02-22 14:51:34 -080076/* Locking: */
77
J. Bruce Fields8b671b82009-02-22 14:51:34 -080078/*
79 * Currently used for the del_recall_lru and file hash table. In an
80 * effort to decrease the scope of the client_mutex, this spinlock may
81 * eventually cover more:
82 */
Benny Halevycdc97502014-05-30 09:09:30 -040083static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080084
Jeff Laytonb401be222014-07-29 21:34:33 -040085/*
86 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
87 * the refcount on the open stateid to drop.
88 */
89static DECLARE_WAIT_QUEUE_HEAD(close_wq);
90
Christoph Hellwigabf11352014-05-21 07:43:03 -070091static struct kmem_cache *openowner_slab;
92static struct kmem_cache *lockowner_slab;
93static struct kmem_cache *file_slab;
94static struct kmem_cache *stateid_slab;
95static struct kmem_cache *deleg_slab;
NeilBrowne60d4392005-06-23 22:03:01 -070096
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -040097static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -080098
Christoph Hellwig0162ac22014-09-24 12:19:19 +020099static struct nfsd4_callback_ops nfsd4_cb_recall_ops;
100
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400101static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800102{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400103 return ses->se_flags & NFS4_SESSION_DEAD;
104}
105
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400106static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
107{
108 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400109 return nfserr_jukebox;
110 ses->se_flags |= NFS4_SESSION_DEAD;
111 return nfs_ok;
112}
113
J. Bruce Fields221a6872013-04-01 22:23:49 -0400114static bool is_client_expired(struct nfs4_client *clp)
115{
116 return clp->cl_time == 0;
117}
118
J. Bruce Fields221a6872013-04-01 22:23:49 -0400119static __be32 get_client_locked(struct nfs4_client *clp)
120{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400121 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
122
123 lockdep_assert_held(&nn->client_lock);
124
J. Bruce Fields221a6872013-04-01 22:23:49 -0400125 if (is_client_expired(clp))
126 return nfserr_expired;
127 atomic_inc(&clp->cl_refcount);
128 return nfs_ok;
129}
130
131/* must be called under the client_lock */
132static inline void
133renew_client_locked(struct nfs4_client *clp)
134{
135 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
136
137 if (is_client_expired(clp)) {
138 WARN_ON(1);
139 printk("%s: client (clientid %08x/%08x) already expired\n",
140 __func__,
141 clp->cl_clientid.cl_boot,
142 clp->cl_clientid.cl_id);
143 return;
144 }
145
146 dprintk("renewing client (clientid %08x/%08x)\n",
147 clp->cl_clientid.cl_boot,
148 clp->cl_clientid.cl_id);
149 list_move_tail(&clp->cl_lru, &nn->client_lru);
150 clp->cl_time = get_seconds();
151}
152
153static inline void
154renew_client(struct nfs4_client *clp)
155{
156 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
157
158 spin_lock(&nn->client_lock);
159 renew_client_locked(clp);
160 spin_unlock(&nn->client_lock);
161}
162
Fengguang Wuba138432013-04-16 22:14:15 -0400163static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400164{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400165 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
166
167 lockdep_assert_held(&nn->client_lock);
168
J. Bruce Fields221a6872013-04-01 22:23:49 -0400169 if (!atomic_dec_and_test(&clp->cl_refcount))
170 return;
171 if (!is_client_expired(clp))
172 renew_client_locked(clp);
173}
174
Jeff Layton4b24ca72014-06-30 11:48:44 -0400175static void put_client_renew(struct nfs4_client *clp)
176{
177 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
178
Jeff Laytond6c249b2014-07-08 14:02:50 -0400179 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
180 return;
181 if (!is_client_expired(clp))
182 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400183 spin_unlock(&nn->client_lock);
184}
185
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400186static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
187{
188 __be32 status;
189
190 if (is_session_dead(ses))
191 return nfserr_badsession;
192 status = get_client_locked(ses->se_client);
193 if (status)
194 return status;
195 atomic_inc(&ses->se_ref);
196 return nfs_ok;
197}
198
199static void nfsd4_put_session_locked(struct nfsd4_session *ses)
200{
201 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400202 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
203
204 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400205
206 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
207 free_session(ses);
208 put_client_renew_locked(clp);
209}
210
211static void nfsd4_put_session(struct nfsd4_session *ses)
212{
213 struct nfs4_client *clp = ses->se_client;
214 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
215
216 spin_lock(&nn->client_lock);
217 nfsd4_put_session_locked(ses);
218 spin_unlock(&nn->client_lock);
219}
220
Kinglong Meeb5971af2014-08-22 10:18:43 -0400221static inline struct nfs4_stateowner *
222nfs4_get_stateowner(struct nfs4_stateowner *sop)
223{
224 atomic_inc(&sop->so_count);
225 return sop;
226}
227
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400228static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400229same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400230{
231 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400232 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400233}
234
235static struct nfs4_openowner *
236find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400237 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400238{
239 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400240
Trond Myklebustd4f04892014-07-29 21:34:36 -0400241 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400242
Trond Myklebustd4f04892014-07-29 21:34:36 -0400243 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
244 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400245 if (!so->so_is_open_owner)
246 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400247 if (same_owner_str(so, &open->op_owner))
248 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400249 }
250 return NULL;
251}
252
253static struct nfs4_openowner *
254find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400255 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400256{
257 struct nfs4_openowner *oo;
258
Trond Myklebustd4f04892014-07-29 21:34:36 -0400259 spin_lock(&clp->cl_lock);
260 oo = find_openstateowner_str_locked(hashval, open, clp);
261 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400262 return oo;
263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265static inline u32
266opaque_hashval(const void *ptr, int nbytes)
267{
268 unsigned char *cptr = (unsigned char *) ptr;
269
270 u32 x = 0;
271 while (nbytes--) {
272 x *= 37;
273 x += *cptr++;
274 }
275 return x;
276}
277
Jeff Layton5b095e92014-10-23 08:01:02 -0400278static void nfsd4_free_file_rcu(struct rcu_head *rcu)
J. Bruce Fields32513b42011-10-13 16:00:16 -0400279{
Jeff Layton5b095e92014-10-23 08:01:02 -0400280 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
281
282 kmem_cache_free(file_slab, fp);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400283}
284
NeilBrown13cd2182005-06-23 22:03:10 -0700285static inline void
286put_nfs4_file(struct nfs4_file *fi)
287{
Jeff Layton02e12152014-07-16 10:31:57 -0400288 might_lock(&state_lock);
289
Benny Halevycdc97502014-05-30 09:09:30 -0400290 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton5b095e92014-10-23 08:01:02 -0400291 hlist_del_rcu(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400292 spin_unlock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -0400293 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
294 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800295 }
NeilBrown13cd2182005-06-23 22:03:10 -0700296}
297
298static inline void
299get_nfs4_file(struct nfs4_file *fi)
300{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800301 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700302}
303
Trond Myklebustde186432014-07-10 14:07:26 -0400304static struct file *
305__nfs4_get_fd(struct nfs4_file *f, int oflag)
306{
307 if (f->fi_fds[oflag])
308 return get_file(f->fi_fds[oflag]);
309 return NULL;
310}
311
312static struct file *
313find_writeable_file_locked(struct nfs4_file *f)
314{
315 struct file *ret;
316
317 lockdep_assert_held(&f->fi_lock);
318
319 ret = __nfs4_get_fd(f, O_WRONLY);
320 if (!ret)
321 ret = __nfs4_get_fd(f, O_RDWR);
322 return ret;
323}
324
325static struct file *
326find_writeable_file(struct nfs4_file *f)
327{
328 struct file *ret;
329
330 spin_lock(&f->fi_lock);
331 ret = find_writeable_file_locked(f);
332 spin_unlock(&f->fi_lock);
333
334 return ret;
335}
336
337static struct file *find_readable_file_locked(struct nfs4_file *f)
338{
339 struct file *ret;
340
341 lockdep_assert_held(&f->fi_lock);
342
343 ret = __nfs4_get_fd(f, O_RDONLY);
344 if (!ret)
345 ret = __nfs4_get_fd(f, O_RDWR);
346 return ret;
347}
348
349static struct file *
350find_readable_file(struct nfs4_file *f)
351{
352 struct file *ret;
353
354 spin_lock(&f->fi_lock);
355 ret = find_readable_file_locked(f);
356 spin_unlock(&f->fi_lock);
357
358 return ret;
359}
360
361static struct file *
362find_any_file(struct nfs4_file *f)
363{
364 struct file *ret;
365
366 spin_lock(&f->fi_lock);
367 ret = __nfs4_get_fd(f, O_RDWR);
368 if (!ret) {
369 ret = __nfs4_get_fd(f, O_WRONLY);
370 if (!ret)
371 ret = __nfs4_get_fd(f, O_RDONLY);
372 }
373 spin_unlock(&f->fi_lock);
374 return ret;
375}
376
Trond Myklebust02a35082014-07-25 07:34:22 -0400377static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800378unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700379
380/*
381 * Open owner state (share locks)
382 */
383
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500384/* hash tables for lock and open owners */
385#define OWNER_HASH_BITS 8
386#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
387#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700388
Trond Myklebustd4f04892014-07-29 21:34:36 -0400389static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400390{
391 unsigned int ret;
392
393 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500394 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400395}
NeilBrownef0f3392006-04-10 22:55:41 -0700396
NeilBrownef0f3392006-04-10 22:55:41 -0700397/* hash table for nfs4_file */
398#define FILE_HASH_BITS 8
399#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800400
Trond Myklebustca943212014-07-23 16:17:39 -0400401static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400402{
Trond Myklebustca943212014-07-23 16:17:39 -0400403 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
404}
405
406static unsigned int file_hashval(struct knfsd_fh *fh)
407{
408 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
409}
410
411static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
412{
413 return fh1->fh_size == fh2->fh_size &&
414 !memcmp(fh1->fh_base.fh_pad,
415 fh2->fh_base.fh_pad,
416 fh1->fh_size);
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400417}
418
Jeff Layton89876f82013-04-02 09:01:59 -0400419static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700420
Jeff Layton12659652014-07-10 14:07:28 -0400421static void
422__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400423{
Jeff Layton7214e862014-07-10 14:07:33 -0400424 lockdep_assert_held(&fp->fi_lock);
425
Jeff Layton12659652014-07-10 14:07:28 -0400426 if (access & NFS4_SHARE_ACCESS_WRITE)
427 atomic_inc(&fp->fi_access[O_WRONLY]);
428 if (access & NFS4_SHARE_ACCESS_READ)
429 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400430}
431
Jeff Layton12659652014-07-10 14:07:28 -0400432static __be32
433nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400434{
Jeff Layton7214e862014-07-10 14:07:33 -0400435 lockdep_assert_held(&fp->fi_lock);
436
Jeff Layton12659652014-07-10 14:07:28 -0400437 /* Does this access mode make sense? */
438 if (access & ~NFS4_SHARE_ACCESS_BOTH)
439 return nfserr_inval;
440
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400441 /* Does it conflict with a deny mode already set? */
442 if ((access & fp->fi_share_deny) != 0)
443 return nfserr_share_denied;
444
Jeff Layton12659652014-07-10 14:07:28 -0400445 __nfs4_file_get_access(fp, access);
446 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400447}
448
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400449static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
450{
451 /* Common case is that there is no deny mode. */
452 if (deny) {
453 /* Does this deny mode make sense? */
454 if (deny & ~NFS4_SHARE_DENY_BOTH)
455 return nfserr_inval;
456
457 if ((deny & NFS4_SHARE_DENY_READ) &&
458 atomic_read(&fp->fi_access[O_RDONLY]))
459 return nfserr_share_denied;
460
461 if ((deny & NFS4_SHARE_DENY_WRITE) &&
462 atomic_read(&fp->fi_access[O_WRONLY]))
463 return nfserr_share_denied;
464 }
465 return nfs_ok;
466}
467
J. Bruce Fields998db522010-08-07 09:21:41 -0400468static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400469{
Trond Myklebustde186432014-07-10 14:07:26 -0400470 might_lock(&fp->fi_lock);
471
472 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
473 struct file *f1 = NULL;
474 struct file *f2 = NULL;
475
Jeff Layton6d338b52014-07-10 14:07:29 -0400476 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400477 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400478 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400479 spin_unlock(&fp->fi_lock);
480 if (f1)
481 fput(f1);
482 if (f2)
483 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400484 }
485}
486
Jeff Layton12659652014-07-10 14:07:28 -0400487static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400488{
Jeff Layton12659652014-07-10 14:07:28 -0400489 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
490
491 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400492 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400493 if (access & NFS4_SHARE_ACCESS_READ)
494 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400495}
496
Trond Myklebust60116952014-07-29 21:34:06 -0400497static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
498 struct kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400499{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500500 struct nfs4_stid *stid;
501 int new_id;
502
Trond Myklebustf8338832014-07-25 07:34:19 -0400503 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500504 if (!stid)
505 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400506
Jeff Layton4770d722014-07-29 21:34:10 -0400507 idr_preload(GFP_KERNEL);
508 spin_lock(&cl->cl_lock);
509 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
510 spin_unlock(&cl->cl_lock);
511 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700512 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500513 goto out_free;
514 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500515 stid->sc_stateid.si_opaque.so_id = new_id;
516 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
517 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400518 atomic_set(&stid->sc_count, 1);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500519
J. Bruce Fields996e0932011-10-17 11:14:48 -0400520 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500521 * It shouldn't be a problem to reuse an opaque stateid value.
522 * I don't think it is for 4.1. But with 4.0 I worry that, for
523 * example, a stray write retransmission could be accepted by
524 * the server when it should have been rejected. Therefore,
525 * adopt a trick from the sctp code to attempt to maximize the
526 * amount of time until an id is reused, by ensuring they always
527 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400528 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500529 return stid;
530out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800531 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500532 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400533}
534
Jeff Laytonb49e0842014-07-29 21:34:11 -0400535static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400536{
Trond Myklebust60116952014-07-29 21:34:06 -0400537 struct nfs4_stid *stid;
538 struct nfs4_ol_stateid *stp;
539
540 stid = nfs4_alloc_stid(clp, stateid_slab);
541 if (!stid)
542 return NULL;
543
544 stp = openlockstateid(stid);
545 stp->st_stid.sc_free = nfs4_free_ol_stateid;
546 return stp;
547}
548
549static void nfs4_free_deleg(struct nfs4_stid *stid)
550{
Trond Myklebust60116952014-07-29 21:34:06 -0400551 kmem_cache_free(deleg_slab, stid);
552 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400553}
554
NeilBrown6282cd52014-06-04 17:39:26 +1000555/*
556 * When we recall a delegation, we should be careful not to hand it
557 * out again straight away.
558 * To ensure this we keep a pair of bloom filters ('new' and 'old')
559 * in which the filehandles of recalled delegations are "stored".
560 * If a filehandle appear in either filter, a delegation is blocked.
561 * When a delegation is recalled, the filehandle is stored in the "new"
562 * filter.
563 * Every 30 seconds we swap the filters and clear the "new" one,
564 * unless both are empty of course.
565 *
566 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
567 * low 3 bytes as hash-table indices.
568 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400569 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000570 * is used to manage concurrent access. Testing does not need the lock
571 * except when swapping the two filters.
572 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400573static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000574static struct bloom_pair {
575 int entries, old_entries;
576 time_t swap_time;
577 int new; /* index into 'set' */
578 DECLARE_BITMAP(set[2], 256);
579} blocked_delegations;
580
581static int delegation_blocked(struct knfsd_fh *fh)
582{
583 u32 hash;
584 struct bloom_pair *bd = &blocked_delegations;
585
586 if (bd->entries == 0)
587 return 0;
588 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400589 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000590 if (seconds_since_boot() - bd->swap_time > 30) {
591 bd->entries -= bd->old_entries;
592 bd->old_entries = bd->entries;
593 memset(bd->set[bd->new], 0,
594 sizeof(bd->set[0]));
595 bd->new = 1-bd->new;
596 bd->swap_time = seconds_since_boot();
597 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400598 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000599 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100600 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000601 if (test_bit(hash&255, bd->set[0]) &&
602 test_bit((hash>>8)&255, bd->set[0]) &&
603 test_bit((hash>>16)&255, bd->set[0]))
604 return 1;
605
606 if (test_bit(hash&255, bd->set[1]) &&
607 test_bit((hash>>8)&255, bd->set[1]) &&
608 test_bit((hash>>16)&255, bd->set[1]))
609 return 1;
610
611 return 0;
612}
613
614static void block_delegations(struct knfsd_fh *fh)
615{
616 u32 hash;
617 struct bloom_pair *bd = &blocked_delegations;
618
Daniel Borkmann87545892014-12-10 16:33:11 +0100619 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000620
Jeff Laytonf54fe962014-07-25 07:34:26 -0400621 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000622 __set_bit(hash&255, bd->set[bd->new]);
623 __set_bit((hash>>8)&255, bd->set[bd->new]);
624 __set_bit((hash>>16)&255, bd->set[bd->new]);
625 if (bd->entries == 0)
626 bd->swap_time = seconds_since_boot();
627 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400628 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000629}
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631static struct nfs4_delegation *
Jeff Laytonf9416e22014-07-25 07:34:23 -0400632alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400635 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400638 n = atomic_long_inc_return(&num_delegations);
639 if (n < 0 || n > max_delegations)
640 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000641 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400642 goto out_dec;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400643 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700644 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400645 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400646
647 dp->dl_stid.sc_free = nfs4_free_deleg;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400648 /*
649 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400650 * meaning of seqid 0 isn't meaningful, really, but let's avoid
651 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400652 */
653 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700654 INIT_LIST_HEAD(&dp->dl_perfile);
655 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400657 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200658 dp->dl_retries = 1;
659 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200660 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400662out_dec:
663 atomic_long_dec(&num_delegations);
664 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
667void
Trond Myklebust60116952014-07-29 21:34:06 -0400668nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Trond Myklebust11b91642014-07-29 21:34:08 -0400670 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400671 struct nfs4_client *clp = s->sc_client;
672
Jeff Layton4770d722014-07-29 21:34:10 -0400673 might_lock(&clp->cl_lock);
674
Jeff Laytonb401be222014-07-29 21:34:33 -0400675 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
676 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400677 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400678 }
Trond Myklebust60116952014-07-29 21:34:06 -0400679 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400680 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400681 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400682 if (fp)
683 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500686static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Jeff Layton6bcc0342014-08-09 10:22:40 -0400688 struct file *filp = NULL;
Jeff Layton417c6622014-07-21 09:34:57 -0400689
Jeff Layton6bcc0342014-08-09 10:22:40 -0400690 spin_lock(&fp->fi_lock);
Jeff Layton0c637be2014-08-22 12:05:43 -0400691 if (fp->fi_deleg_file && atomic_dec_and_test(&fp->fi_delegees))
Jeff Layton6bcc0342014-08-09 10:22:40 -0400692 swap(filp, fp->fi_deleg_file);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400693 spin_unlock(&fp->fi_lock);
694
695 if (filp) {
Jeff Laytone6f5c782014-08-22 10:40:25 -0400696 vfs_setlease(filp, F_UNLCK, NULL, NULL);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400697 fput(filp);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400701static void unhash_stid(struct nfs4_stid *s)
702{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500703 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400704}
705
Benny Halevy931ee562014-05-30 09:09:27 -0400706static void
707hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
708{
Benny Halevycdc97502014-05-30 09:09:30 -0400709 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400710 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400711
Trond Myklebust67cb1272014-07-29 21:34:17 -0400712 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400713 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400714 list_add(&dp->dl_perfile, &fp->fi_delegations);
715 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
716}
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718static void
Jeff Layton42690672014-07-25 07:34:20 -0400719unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Trond Myklebust11b91642014-07-29 21:34:08 -0400721 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400722
Jeff Layton42690672014-07-25 07:34:20 -0400723 lockdep_assert_held(&state_lock);
724
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400725 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400726 /* Ensure that deleg break won't try to requeue it */
727 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400728 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400729 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400731 list_del_init(&dp->dl_perfile);
732 spin_unlock(&fp->fi_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400733}
734
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400735static void destroy_delegation(struct nfs4_delegation *dp)
736{
Jeff Layton42690672014-07-25 07:34:20 -0400737 spin_lock(&state_lock);
738 unhash_delegation_locked(dp);
739 spin_unlock(&state_lock);
Jeff Laytonafbda402014-08-09 10:22:41 -0400740 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -0400741 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400742}
743
744static void revoke_delegation(struct nfs4_delegation *dp)
745{
746 struct nfs4_client *clp = dp->dl_stid.sc_client;
747
Jeff Layton2d4a5322014-07-25 07:34:21 -0400748 WARN_ON(!list_empty(&dp->dl_recall_lru));
749
Jeff Laytonafbda402014-08-09 10:22:41 -0400750 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
751
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400752 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400753 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400754 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400755 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400756 spin_lock(&clp->cl_lock);
757 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
758 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400759 }
760}
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762/*
763 * SETCLIENTID state
764 */
765
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400766static unsigned int clientid_hashval(u32 id)
767{
768 return id & CLIENT_HASH_MASK;
769}
770
771static unsigned int clientstr_hashval(const char *name)
772{
773 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
774}
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400777 * We store the NONE, READ, WRITE, and BOTH bits separately in the
778 * st_{access,deny}_bmap field of the stateid, in order to track not
779 * only what share bits are currently in force, but also what
780 * combinations of share bits previous opens have used. This allows us
781 * to enforce the recommendation of rfc 3530 14.2.19 that the server
782 * return an error if the client attempt to downgrade to a combination
783 * of share bits not explicable by closing some of its previous opens.
784 *
785 * XXX: This enforcement is actually incomplete, since we don't keep
786 * track of access/deny bit combinations; so, e.g., we allow:
787 *
788 * OPEN allow read, deny write
789 * OPEN allow both, deny none
790 * DOWNGRADE allow read, deny none
791 *
792 * which we should reject.
793 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400794static unsigned int
795bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400796 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400797 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400798
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400799 for (i = 1; i < 4; i++) {
800 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400801 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400802 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400803 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400804}
805
Jeff Layton82c5ff12012-05-11 09:45:13 -0400806/* set share access for a given stateid */
807static inline void
808set_access(u32 access, struct nfs4_ol_stateid *stp)
809{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400810 unsigned char mask = 1 << access;
811
812 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
813 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400814}
815
816/* clear share access for a given stateid */
817static inline void
818clear_access(u32 access, struct nfs4_ol_stateid *stp)
819{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400820 unsigned char mask = 1 << access;
821
822 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
823 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400824}
825
826/* test whether a given stateid has access */
827static inline bool
828test_access(u32 access, struct nfs4_ol_stateid *stp)
829{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400830 unsigned char mask = 1 << access;
831
832 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400833}
834
Jeff Laytonce0fc432012-05-11 09:45:14 -0400835/* set share deny for a given stateid */
836static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400837set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400838{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400839 unsigned char mask = 1 << deny;
840
841 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
842 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400843}
844
845/* clear share deny for a given stateid */
846static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400847clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400848{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400849 unsigned char mask = 1 << deny;
850
851 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
852 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400853}
854
855/* test whether a given stateid is denying specific access */
856static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400857test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400858{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400859 unsigned char mask = 1 << deny;
860
861 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400862}
863
864static int nfs4_access_to_omode(u32 access)
865{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400866 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400867 case NFS4_SHARE_ACCESS_READ:
868 return O_RDONLY;
869 case NFS4_SHARE_ACCESS_WRITE:
870 return O_WRONLY;
871 case NFS4_SHARE_ACCESS_BOTH:
872 return O_RDWR;
873 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500874 WARN_ON_ONCE(1);
875 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400876}
877
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400878/*
879 * A stateid that had a deny mode associated with it is being released
880 * or downgraded. Recalculate the deny mode on the file.
881 */
882static void
883recalculate_deny_mode(struct nfs4_file *fp)
884{
885 struct nfs4_ol_stateid *stp;
886
887 spin_lock(&fp->fi_lock);
888 fp->fi_share_deny = 0;
889 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
890 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
891 spin_unlock(&fp->fi_lock);
892}
893
894static void
895reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
896{
897 int i;
898 bool change = false;
899
900 for (i = 1; i < 4; i++) {
901 if ((i & deny) != i) {
902 change = true;
903 clear_deny(i, stp);
904 }
905 }
906
907 /* Recalculate per-file deny mode if there was a change */
908 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -0400909 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400910}
911
Jeff Layton82c5ff12012-05-11 09:45:13 -0400912/* release all access and file references for a given stateid */
913static void
914release_all_access(struct nfs4_ol_stateid *stp)
915{
916 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -0400917 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400918
919 if (fp && stp->st_deny_bmap != 0)
920 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400921
922 for (i = 1; i < 4; i++) {
923 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -0400924 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400925 clear_access(i, stp);
926 }
927}
928
Jeff Layton6b180f02014-07-29 21:34:26 -0400929static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
930{
Jeff Laytona819ecc2014-07-29 21:34:38 -0400931 struct nfs4_client *clp = sop->so_client;
932
933 might_lock(&clp->cl_lock);
934
935 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -0400936 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -0400937 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -0400938 spin_unlock(&clp->cl_lock);
Jeff Layton6b180f02014-07-29 21:34:26 -0400939 kfree(sop->so_owner.data);
940 sop->so_ops->so_free(sop);
941}
942
Jeff Layton4ae098d2014-07-29 21:34:43 -0400943static void unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500944{
Trond Myklebust11b91642014-07-29 21:34:08 -0400945 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -0400946
Jeff Layton1c755dc2014-07-29 21:34:12 -0400947 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
948
Trond Myklebust1d31a252014-07-10 14:07:25 -0400949 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500950 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400951 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500952 list_del(&stp->st_perstateowner);
953}
954
Trond Myklebust60116952014-07-29 21:34:06 -0400955static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500956{
Trond Myklebust60116952014-07-29 21:34:06 -0400957 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400958
Trond Myklebust60116952014-07-29 21:34:06 -0400959 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -0400960 if (stp->st_stateowner)
961 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -0400962 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500963}
964
Jeff Laytonb49e0842014-07-29 21:34:11 -0400965static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500966{
Jeff Laytonb49e0842014-07-29 21:34:11 -0400967 struct nfs4_ol_stateid *stp = openlockstateid(stid);
968 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500969 struct file *file;
970
Jeff Laytonb49e0842014-07-29 21:34:11 -0400971 file = find_any_file(stp->st_stid.sc_file);
972 if (file)
973 filp_close(file, (fl_owner_t)lo);
974 nfs4_free_ol_stateid(stid);
975}
976
Jeff Layton2c41beb2014-07-29 21:34:41 -0400977/*
978 * Put the persistent reference to an already unhashed generic stateid, while
979 * holding the cl_lock. If it's the last reference, then put it onto the
980 * reaplist for later destruction.
981 */
982static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
983 struct list_head *reaplist)
984{
985 struct nfs4_stid *s = &stp->st_stid;
986 struct nfs4_client *clp = s->sc_client;
987
988 lockdep_assert_held(&clp->cl_lock);
989
990 WARN_ON_ONCE(!list_empty(&stp->st_locks));
991
992 if (!atomic_dec_and_test(&s->sc_count)) {
993 wake_up_all(&close_wq);
994 return;
995 }
996
997 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
998 list_add(&stp->st_locks, reaplist);
999}
1000
Jeff Layton3c1c9952014-07-29 21:34:39 -04001001static void unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1002{
1003 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1004
1005 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
1006
1007 list_del_init(&stp->st_locks);
Jeff Layton4ae098d2014-07-29 21:34:43 -04001008 unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001009 unhash_stid(&stp->st_stid);
1010}
1011
Jeff Layton5adfd882014-07-29 21:34:31 -04001012static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001013{
Jeff Layton1c755dc2014-07-29 21:34:12 -04001014 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1015
1016 spin_lock(&oo->oo_owner.so_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001017 unhash_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001018 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04001019 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001020}
1021
Trond Myklebustc58c6612014-07-29 21:34:35 -04001022static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001023{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001024 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001025
Trond Myklebustd4f04892014-07-29 21:34:36 -04001026 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001027
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001028 list_del_init(&lo->lo_owner.so_strhash);
1029}
1030
Jeff Layton2c41beb2014-07-29 21:34:41 -04001031/*
1032 * Free a list of generic stateids that were collected earlier after being
1033 * fully unhashed.
1034 */
1035static void
1036free_ol_stateid_reaplist(struct list_head *reaplist)
1037{
1038 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001039 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001040
1041 might_sleep();
1042
1043 while (!list_empty(reaplist)) {
1044 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1045 st_locks);
1046 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001047 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001048 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001049 if (fp)
1050 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001051 }
1052}
1053
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001054static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001055{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001056 struct nfs4_client *clp = lo->lo_owner.so_client;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001057 struct nfs4_ol_stateid *stp;
1058 struct list_head reaplist;
1059
1060 INIT_LIST_HEAD(&reaplist);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001061
Trond Myklebustd4f04892014-07-29 21:34:36 -04001062 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001063 unhash_lockowner_locked(lo);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001064 while (!list_empty(&lo->lo_owner.so_stateids)) {
1065 stp = list_first_entry(&lo->lo_owner.so_stateids,
1066 struct nfs4_ol_stateid, st_perstateowner);
1067 unhash_lock_stateid(stp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001068 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001069 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001070 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001071 free_ol_stateid_reaplist(&reaplist);
Jeff Layton6b180f02014-07-29 21:34:26 -04001072 nfs4_put_stateowner(&lo->lo_owner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001073}
1074
Jeff Laytond83017f2014-07-29 21:34:42 -04001075static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1076 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001077{
1078 struct nfs4_ol_stateid *stp;
1079
1080 while (!list_empty(&open_stp->st_locks)) {
1081 stp = list_entry(open_stp->st_locks.next,
1082 struct nfs4_ol_stateid, st_locks);
Jeff Laytond83017f2014-07-29 21:34:42 -04001083 unhash_lock_stateid(stp);
1084 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001085 }
1086}
1087
Jeff Laytond83017f2014-07-29 21:34:42 -04001088static void unhash_open_stateid(struct nfs4_ol_stateid *stp,
1089 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001090{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001091 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1092
Jeff Layton4ae098d2014-07-29 21:34:43 -04001093 unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001094 release_open_stateid_locks(stp, reaplist);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001095}
1096
1097static void release_open_stateid(struct nfs4_ol_stateid *stp)
1098{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001099 LIST_HEAD(reaplist);
1100
1101 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04001102 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001103 put_ol_stateid_locked(stp, &reaplist);
1104 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1105 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001106}
1107
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001108static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001109{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001110 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001111
Trond Myklebustd4f04892014-07-29 21:34:36 -04001112 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001113
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001114 list_del_init(&oo->oo_owner.so_strhash);
1115 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001116}
1117
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001118static void release_last_closed_stateid(struct nfs4_openowner *oo)
1119{
Jeff Layton217526e2014-07-30 08:27:11 -04001120 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1121 nfsd_net_id);
1122 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001123
Jeff Layton217526e2014-07-30 08:27:11 -04001124 spin_lock(&nn->client_lock);
1125 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001126 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001127 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001128 oo->oo_last_closed_stid = NULL;
1129 }
Jeff Layton217526e2014-07-30 08:27:11 -04001130 spin_unlock(&nn->client_lock);
1131 if (s)
1132 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001133}
1134
Jeff Layton2c41beb2014-07-29 21:34:41 -04001135static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001136{
1137 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001138 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001139 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001140
Jeff Layton2c41beb2014-07-29 21:34:41 -04001141 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001142
Trond Myklebustd4f04892014-07-29 21:34:36 -04001143 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001144 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001145 while (!list_empty(&oo->oo_owner.so_stateids)) {
1146 stp = list_first_entry(&oo->oo_owner.so_stateids,
1147 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytond83017f2014-07-29 21:34:42 -04001148 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001149 put_ol_stateid_locked(stp, &reaplist);
1150 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001151 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001152 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001153 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001154 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001155}
1156
Marc Eshel5282fd72009-04-03 08:27:52 +03001157static inline int
1158hash_sessionid(struct nfs4_sessionid *sessionid)
1159{
1160 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1161
1162 return sid->sequence % SESSION_HASH_SIZE;
1163}
1164
Trond Myklebust8f199b82012-03-20 15:11:17 -04001165#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001166static inline void
1167dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1168{
1169 u32 *ptr = (u32 *)(&sessionid->data[0]);
1170 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1171}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001172#else
1173static inline void
1174dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1175{
1176}
1177#endif
1178
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001179/*
1180 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1181 * won't be used for replay.
1182 */
1183void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1184{
1185 struct nfs4_stateowner *so = cstate->replay_owner;
1186
1187 if (nfserr == nfserr_replay_me)
1188 return;
1189
1190 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001191 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001192 return;
1193 }
1194 if (!so)
1195 return;
1196 if (so->so_is_open_owner)
1197 release_last_closed_stateid(openowner(so));
1198 so->so_seqid++;
1199 return;
1200}
Marc Eshel5282fd72009-04-03 08:27:52 +03001201
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001202static void
1203gen_sessionid(struct nfsd4_session *ses)
1204{
1205 struct nfs4_client *clp = ses->se_client;
1206 struct nfsd4_sessionid *sid;
1207
1208 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1209 sid->clientid = clp->cl_clientid;
1210 sid->sequence = current_sessionid++;
1211 sid->reserved = 0;
1212}
1213
1214/*
Andy Adamsona6496372009-08-28 08:45:01 -04001215 * The protocol defines ca_maxresponssize_cached to include the size of
1216 * the rpc header, but all we need to cache is the data starting after
1217 * the end of the initial SEQUENCE operation--the rest we regenerate
1218 * each time. Therefore we can advertise a ca_maxresponssize_cached
1219 * value that is the number of bytes in our cache plus a few additional
1220 * bytes. In order to stay on the safe side, and not promise more than
1221 * we can cache, those additional bytes must be the minimum possible: 24
1222 * bytes of rpc header (xid through accept state, with AUTH_NULL
1223 * verifier), 12 for the compound header (with zero-length tag), and 44
1224 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001225 */
Andy Adamsona6496372009-08-28 08:45:01 -04001226#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1227
Andy Adamson557ce262009-08-28 08:45:04 -04001228static void
1229free_session_slots(struct nfsd4_session *ses)
1230{
1231 int i;
1232
1233 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1234 kfree(ses->se_slots[i]);
1235}
1236
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001237/*
1238 * We don't actually need to cache the rpc and session headers, so we
1239 * can allocate a little less for each slot:
1240 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001241static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001242{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001243 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001244
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001245 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1246 size = 0;
1247 else
1248 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1249 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001250}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001251
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001252/*
1253 * XXX: If we run out of reserved DRC memory we could (up to a point)
1254 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001255 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001256 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001257static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001258{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001259 u32 slotsize = slot_bytes(ca);
1260 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001261 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001262
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001263 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001264 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1265 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001266 num = min_t(int, num, avail / slotsize);
1267 nfsd_drc_mem_used += num * slotsize;
1268 spin_unlock(&nfsd_drc_lock);
1269
1270 return num;
1271}
1272
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001273static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001274{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001275 int slotsize = slot_bytes(ca);
1276
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001277 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001278 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001279 spin_unlock(&nfsd_drc_lock);
1280}
1281
Kinglong Mee60810e52014-01-01 00:35:47 +08001282static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1283 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001284{
Kinglong Mee60810e52014-01-01 00:35:47 +08001285 int numslots = fattrs->maxreqs;
1286 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001287 struct nfsd4_session *new;
1288 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001289
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001290 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001291 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1292 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001293
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001294 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001295 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001296 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001297 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001298 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001299 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001300 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001301 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001302 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001303
1304 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1305 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1306
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001307 return new;
1308out_free:
1309 while (i--)
1310 kfree(new->se_slots[i]);
1311 kfree(new);
1312 return NULL;
1313}
1314
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001315static void free_conn(struct nfsd4_conn *c)
1316{
1317 svc_xprt_put(c->cn_xprt);
1318 kfree(c);
1319}
1320
1321static void nfsd4_conn_lost(struct svc_xpt_user *u)
1322{
1323 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1324 struct nfs4_client *clp = c->cn_session->se_client;
1325
1326 spin_lock(&clp->cl_lock);
1327 if (!list_empty(&c->cn_persession)) {
1328 list_del(&c->cn_persession);
1329 free_conn(c);
1330 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001331 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001332 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001333}
1334
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001335static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001336{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001337 struct nfsd4_conn *conn;
1338
1339 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1340 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001341 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001342 svc_xprt_get(rqstp->rq_xprt);
1343 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001344 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001345 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1346 return conn;
1347}
1348
J. Bruce Fields328ead22010-09-29 16:11:06 -04001349static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1350{
1351 conn->cn_session = ses;
1352 list_add(&conn->cn_persession, &ses->se_conns);
1353}
1354
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001355static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1356{
1357 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001358
1359 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001360 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001361 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001362}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001363
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001364static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001365{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001366 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001367 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001368}
1369
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001370static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001371{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001372 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001373
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001374 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001375 ret = nfsd4_register_conn(conn);
1376 if (ret)
1377 /* oops; xprt is already down: */
1378 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001379 /* We may have gained or lost a callback channel: */
1380 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001381}
1382
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001383static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001384{
1385 u32 dir = NFS4_CDFC4_FORE;
1386
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001387 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001388 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001389 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001390}
1391
1392/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001393static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001394{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001395 struct nfs4_client *clp = s->se_client;
1396 struct nfsd4_conn *c;
1397
1398 spin_lock(&clp->cl_lock);
1399 while (!list_empty(&s->se_conns)) {
1400 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1401 list_del_init(&c->cn_persession);
1402 spin_unlock(&clp->cl_lock);
1403
1404 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1405 free_conn(c);
1406
1407 spin_lock(&clp->cl_lock);
1408 }
1409 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001410}
1411
J. Bruce Fields1377b692012-09-11 21:42:40 -04001412static void __free_session(struct nfsd4_session *ses)
1413{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001414 free_session_slots(ses);
1415 kfree(ses);
1416}
1417
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001418static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001419{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001420 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001421 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001422 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001423}
Andy Adamson557ce262009-08-28 08:45:04 -04001424
Fengguang Wu135ae822012-11-10 07:20:25 -05001425static 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 -04001426{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001427 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001428 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001429
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001430 new->se_client = clp;
1431 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001432
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001433 INIT_LIST_HEAD(&new->se_conns);
1434
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001435 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001436 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001437 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001438 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001439 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001440 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001441 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001442 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001443 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001444 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001445
Chuck Leverb0d2e422014-08-22 15:10:59 -04001446 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001447 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001448 /*
1449 * This is a little silly; with sessions there's no real
1450 * use for the callback address. Use the peer address
1451 * as a reasonable default for now, but consider fixing
1452 * the rpc client not to require an address in the
1453 * future:
1454 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001455 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1456 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001457 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001458}
1459
Benny Halevy9089f1b2010-05-12 00:12:26 +03001460/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001461static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001462__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001463{
1464 struct nfsd4_session *elem;
1465 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001466 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001467
Trond Myklebust0a880a22014-07-30 08:27:10 -04001468 lockdep_assert_held(&nn->client_lock);
1469
Marc Eshel5282fd72009-04-03 08:27:52 +03001470 dump_sessionid(__func__, sessionid);
1471 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001472 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001473 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001474 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1475 NFS4_MAX_SESSIONID_LEN)) {
1476 return elem;
1477 }
1478 }
1479
1480 dprintk("%s: session not found\n", __func__);
1481 return NULL;
1482}
1483
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001484static struct nfsd4_session *
1485find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1486 __be32 *ret)
1487{
1488 struct nfsd4_session *session;
1489 __be32 status = nfserr_badsession;
1490
1491 session = __find_in_sessionid_hashtbl(sessionid, net);
1492 if (!session)
1493 goto out;
1494 status = nfsd4_get_session_locked(session);
1495 if (status)
1496 session = NULL;
1497out:
1498 *ret = status;
1499 return session;
1500}
1501
Benny Halevy9089f1b2010-05-12 00:12:26 +03001502/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001503static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001504unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001505{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001506 struct nfs4_client *clp = ses->se_client;
1507 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1508
1509 lockdep_assert_held(&nn->client_lock);
1510
Andy Adamson7116ed62009-04-03 08:27:43 +03001511 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001512 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001513 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001514 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001515}
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1518static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001519STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001521 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001523 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001524 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 return 1;
1526}
1527
1528/*
1529 * XXX Should we use a slab cache ?
1530 * This type of memory management is somewhat inefficient, but we use it
1531 * anyway since SETCLIENTID is not a common operation.
1532 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001533static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
1535 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001536 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001538 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1539 if (clp == NULL)
1540 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001541 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001542 if (clp->cl_name.data == NULL)
1543 goto err_no_name;
1544 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1545 OWNER_HASH_SIZE, GFP_KERNEL);
1546 if (!clp->cl_ownerstr_hashtbl)
1547 goto err_no_hashtbl;
1548 for (i = 0; i < OWNER_HASH_SIZE; i++)
1549 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001550 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001551 INIT_LIST_HEAD(&clp->cl_sessions);
1552 idr_init(&clp->cl_stateids);
1553 atomic_set(&clp->cl_refcount, 0);
1554 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1555 INIT_LIST_HEAD(&clp->cl_idhash);
1556 INIT_LIST_HEAD(&clp->cl_openowners);
1557 INIT_LIST_HEAD(&clp->cl_delegations);
1558 INIT_LIST_HEAD(&clp->cl_lru);
1559 INIT_LIST_HEAD(&clp->cl_callbacks);
1560 INIT_LIST_HEAD(&clp->cl_revoked);
1561 spin_lock_init(&clp->cl_lock);
1562 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001564err_no_hashtbl:
1565 kfree(clp->cl_name.data);
1566err_no_name:
1567 kfree(clp);
1568 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001571static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572free_client(struct nfs4_client *clp)
1573{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001574 while (!list_empty(&clp->cl_sessions)) {
1575 struct nfsd4_session *ses;
1576 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1577 se_perclnt);
1578 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001579 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1580 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001581 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001582 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001583 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001584 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001586 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 kfree(clp);
1588}
1589
Benny Halevy84d38ac2010-05-12 00:13:16 +03001590/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001591static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001592unhash_client_locked(struct nfs4_client *clp)
1593{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001594 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001595 struct nfsd4_session *ses;
1596
Trond Myklebust0a880a22014-07-30 08:27:10 -04001597 lockdep_assert_held(&nn->client_lock);
1598
Trond Myklebust4beb3452014-07-30 08:27:02 -04001599 /* Mark the client as expired! */
1600 clp->cl_time = 0;
1601 /* Make it invisible */
1602 if (!list_empty(&clp->cl_idhash)) {
1603 list_del_init(&clp->cl_idhash);
1604 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1605 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1606 else
1607 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1608 }
1609 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001610 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001611 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1612 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001613 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001614}
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001617unhash_client(struct nfs4_client *clp)
1618{
1619 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1620
1621 spin_lock(&nn->client_lock);
1622 unhash_client_locked(clp);
1623 spin_unlock(&nn->client_lock);
1624}
1625
Jeff Layton97403d92014-07-30 08:27:12 -04001626static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1627{
1628 if (atomic_read(&clp->cl_refcount))
1629 return nfserr_jukebox;
1630 unhash_client_locked(clp);
1631 return nfs_ok;
1632}
1633
Trond Myklebust4beb3452014-07-30 08:27:02 -04001634static void
1635__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001637 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 struct list_head reaplist;
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001642 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001643 while (!list_empty(&clp->cl_delegations)) {
1644 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001645 unhash_delegation_locked(dp);
1646 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
Benny Halevycdc97502014-05-30 09:09:30 -04001648 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 while (!list_empty(&reaplist)) {
1650 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001651 list_del_init(&dp->dl_recall_lru);
Jeff Laytonafbda402014-08-09 10:22:41 -04001652 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04001653 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001655 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001656 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001657 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001658 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001659 }
NeilBrownea1da632005-06-23 22:04:17 -07001660 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001661 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04001662 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001663 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001665 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001666 if (clp->cl_cb_conn.cb_xprt)
1667 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001668 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
1670
Trond Myklebust4beb3452014-07-30 08:27:02 -04001671static void
1672destroy_client(struct nfs4_client *clp)
1673{
1674 unhash_client(clp);
1675 __destroy_client(clp);
1676}
1677
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001678static void expire_client(struct nfs4_client *clp)
1679{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001680 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001681 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001682 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001683}
1684
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001685static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1686{
1687 memcpy(target->cl_verifier.data, source->data,
1688 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001691static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1692{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1694 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1695}
1696
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001697static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001698{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001699 if (source->cr_principal) {
1700 target->cr_principal =
1701 kstrdup(source->cr_principal, GFP_KERNEL);
1702 if (target->cr_principal == NULL)
1703 return -ENOMEM;
1704 } else
1705 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001706 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 target->cr_uid = source->cr_uid;
1708 target->cr_gid = source->cr_gid;
1709 target->cr_group_info = source->cr_group_info;
1710 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001711 target->cr_gss_mech = source->cr_gss_mech;
1712 if (source->cr_gss_mech)
1713 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001714 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715}
1716
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001717static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001718compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1719{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001720 if (o1->len < o2->len)
1721 return -1;
1722 if (o1->len > o2->len)
1723 return 1;
1724 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001725}
1726
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001727static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001728{
NeilBrowna55370a2005-06-23 22:03:52 -07001729 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}
1731
1732static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001733same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1734{
1735 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736}
1737
1738static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001739same_clid(clientid_t *cl1, clientid_t *cl2)
1740{
1741 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742}
1743
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001744static bool groups_equal(struct group_info *g1, struct group_info *g2)
1745{
1746 int i;
1747
1748 if (g1->ngroups != g2->ngroups)
1749 return false;
1750 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001751 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001752 return false;
1753 return true;
1754}
1755
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001756/*
1757 * RFC 3530 language requires clid_inuse be returned when the
1758 * "principal" associated with a requests differs from that previously
1759 * used. We use uid, gid's, and gss principal string as our best
1760 * approximation. We also don't want to allow non-gss use of a client
1761 * established using gss: in theory cr_principal should catch that
1762 * change, but in practice cr_principal can be null even in the gss case
1763 * since gssd doesn't always pass down a principal string.
1764 */
1765static bool is_gss_cred(struct svc_cred *cr)
1766{
1767 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1768 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1769}
1770
1771
Vivek Trivedi5559b502012-07-24 21:18:20 +05301772static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001773same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1774{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001775 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001776 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1777 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001778 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1779 return false;
1780 if (cr1->cr_principal == cr2->cr_principal)
1781 return true;
1782 if (!cr1->cr_principal || !cr2->cr_principal)
1783 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301784 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
J. Bruce Fields57266a62013-04-13 14:27:29 -04001787static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1788{
1789 struct svc_cred *cr = &rqstp->rq_cred;
1790 u32 service;
1791
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001792 if (!cr->cr_gss_mech)
1793 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001794 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1795 return service == RPC_GSS_SVC_INTEGRITY ||
1796 service == RPC_GSS_SVC_PRIVACY;
1797}
1798
1799static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1800{
1801 struct svc_cred *cr = &rqstp->rq_cred;
1802
1803 if (!cl->cl_mach_cred)
1804 return true;
1805 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1806 return false;
1807 if (!svc_rqst_integrity_protected(rqstp))
1808 return false;
1809 if (!cr->cr_principal)
1810 return false;
1811 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1812}
1813
Jeff Layton294ac322014-07-30 08:27:15 -04001814static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001815{
Chuck Leverab4684d2012-03-02 17:13:50 -05001816 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Jeff Laytonf4199922014-06-17 07:44:11 -04001818 /*
1819 * This is opaque to client, so no need to byte-swap. Use
1820 * __force to keep sparse happy
1821 */
1822 verf[0] = (__force __be32)get_seconds();
Jeff Layton294ac322014-07-30 08:27:15 -04001823 verf[1] = (__force __be32)nn->clientid_counter;
Chuck Leverab4684d2012-03-02 17:13:50 -05001824 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
Jeff Layton294ac322014-07-30 08:27:15 -04001827static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1828{
1829 clp->cl_clientid.cl_boot = nn->boot_time;
1830 clp->cl_clientid.cl_id = nn->clientid_counter++;
1831 gen_confirm(clp, nn);
1832}
1833
Jeff Layton4770d722014-07-29 21:34:10 -04001834static struct nfs4_stid *
1835find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001836{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001837 struct nfs4_stid *ret;
1838
1839 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1840 if (!ret || !ret->sc_type)
1841 return NULL;
1842 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001843}
1844
Jeff Layton4770d722014-07-29 21:34:10 -04001845static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001846find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001847{
1848 struct nfs4_stid *s;
1849
Jeff Layton4770d722014-07-29 21:34:10 -04001850 spin_lock(&cl->cl_lock);
1851 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001852 if (s != NULL) {
1853 if (typemask & s->sc_type)
1854 atomic_inc(&s->sc_count);
1855 else
1856 s = NULL;
1857 }
Jeff Layton4770d722014-07-29 21:34:10 -04001858 spin_unlock(&cl->cl_lock);
1859 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001860}
1861
Jeff Layton2216d442012-11-12 15:00:57 -05001862static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001863 struct svc_rqst *rqstp, nfs4_verifier *verf)
1864{
1865 struct nfs4_client *clp;
1866 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001867 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001868 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001869
1870 clp = alloc_client(name);
1871 if (clp == NULL)
1872 return NULL;
1873
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001874 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1875 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001876 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001877 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001878 }
Christoph Hellwig0162ac22014-09-24 12:19:19 +02001879 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Benny Halevy07cd4902010-05-12 00:13:41 +03001880 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001881 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001882 copy_verf(clp, verf);
1883 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001884 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001885 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001886 return clp;
1887}
1888
NeilBrownfd39ca92005-06-23 22:04:03 -07001889static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001890add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1891{
1892 struct rb_node **new = &(root->rb_node), *parent = NULL;
1893 struct nfs4_client *clp;
1894
1895 while (*new) {
1896 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1897 parent = *new;
1898
1899 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1900 new = &((*new)->rb_left);
1901 else
1902 new = &((*new)->rb_right);
1903 }
1904
1905 rb_link_node(&new_clp->cl_namenode, parent, new);
1906 rb_insert_color(&new_clp->cl_namenode, root);
1907}
1908
1909static struct nfs4_client *
1910find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1911{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001912 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001913 struct rb_node *node = root->rb_node;
1914 struct nfs4_client *clp;
1915
1916 while (node) {
1917 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1918 cmp = compare_blob(&clp->cl_name, name);
1919 if (cmp > 0)
1920 node = node->rb_left;
1921 else if (cmp < 0)
1922 node = node->rb_right;
1923 else
1924 return clp;
1925 }
1926 return NULL;
1927}
1928
1929static void
1930add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
1932 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001933 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Trond Myklebust0a880a22014-07-30 08:27:10 -04001935 lockdep_assert_held(&nn->client_lock);
1936
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001937 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001938 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001940 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001941 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
1943
NeilBrownfd39ca92005-06-23 22:04:03 -07001944static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945move_to_confirmed(struct nfs4_client *clp)
1946{
1947 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001948 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Trond Myklebust0a880a22014-07-30 08:27:10 -04001950 lockdep_assert_held(&nn->client_lock);
1951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001953 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001954 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001955 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001956 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001957 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958}
1959
1960static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001961find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 struct nfs4_client *clp;
1964 unsigned int idhashval = clientid_hashval(clid->cl_id);
1965
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001966 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001967 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001968 if ((bool)clp->cl_minorversion != sessions)
1969 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001970 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
1974 return NULL;
1975}
1976
1977static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001978find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1979{
1980 struct list_head *tbl = nn->conf_id_hashtbl;
1981
Trond Myklebust0a880a22014-07-30 08:27:10 -04001982 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001983 return find_client_in_id_table(tbl, clid, sessions);
1984}
1985
1986static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001987find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001989 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Trond Myklebust0a880a22014-07-30 08:27:10 -04001991 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001992 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001995static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001996{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001997 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001998}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001999
NeilBrown28ce6052005-06-23 22:03:56 -07002000static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002001find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002002{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002003 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002004 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002005}
2006
2007static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002008find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002009{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002010 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002011 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002012}
2013
NeilBrownfd39ca92005-06-23 22:04:03 -07002014static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002015gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002017 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002018 struct sockaddr *sa = svc_addr(rqstp);
2019 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002020 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Jeff Layton7077ecb2009-08-14 12:57:58 -04002022 /* Currently, we only support tcp and tcp6 for the callback channel */
2023 if (se->se_callback_netid_len == 3 &&
2024 !memcmp(se->se_callback_netid_val, "tcp", 3))
2025 expected_family = AF_INET;
2026 else if (se->se_callback_netid_len == 4 &&
2027 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2028 expected_family = AF_INET6;
2029 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 goto out_err;
2031
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002032 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002033 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002034 (struct sockaddr *)&conn->cb_addr,
2035 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002036
J. Bruce Fields07263f12010-05-31 19:09:40 -04002037 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002039
J. Bruce Fields07263f12010-05-31 19:09:40 -04002040 if (conn->cb_addr.ss_family == AF_INET6)
2041 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002042
J. Bruce Fields07263f12010-05-31 19:09:40 -04002043 conn->cb_prog = se->se_callback_prog;
2044 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002045 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 return;
2047out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002048 conn->cb_addr.ss_family = AF_UNSPEC;
2049 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002050 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 "will not receive delegations\n",
2052 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 return;
2055}
2056
Andy Adamson074fe892009-04-03 08:28:15 +03002057/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002058 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002059 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002060static void
Andy Adamson074fe892009-04-03 08:28:15 +03002061nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2062{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002063 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002064 struct nfsd4_slot *slot = resp->cstate.slot;
2065 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002066
Andy Adamson557ce262009-08-28 08:45:04 -04002067 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002068
Andy Adamson557ce262009-08-28 08:45:04 -04002069 slot->sl_opcnt = resp->opcnt;
2070 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002071
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002072 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002073 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002074 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002075 return;
2076 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002077 base = resp->cstate.data_offset;
2078 slot->sl_datalen = buf->len - base;
2079 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04002080 WARN("%s: sessions DRC could not cache compound\n", __func__);
2081 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002082}
2083
2084/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002085 * Encode the replay sequence operation from the slot values.
2086 * If cachethis is FALSE encode the uncached rep error on the next
2087 * operation which sets resp->p and increments resp->opcnt for
2088 * nfs4svc_encode_compoundres.
2089 *
Andy Adamson074fe892009-04-03 08:28:15 +03002090 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002091static __be32
2092nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2093 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002094{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002095 struct nfsd4_op *op;
2096 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002097
Andy Adamsonabfabf82009-07-23 19:02:18 -04002098 /* Encode the replayed sequence operation */
2099 op = &args->ops[resp->opcnt - 1];
2100 nfsd4_encode_operation(resp, op);
2101
2102 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002103 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002104 op = &args->ops[resp->opcnt++];
2105 op->status = nfserr_retry_uncached_rep;
2106 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002107 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002108 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002109}
2110
2111/*
Andy Adamson557ce262009-08-28 08:45:04 -04002112 * The sequence operation is not cached because we can use the slot and
2113 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002114 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002115static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002116nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2117 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002118{
Andy Adamson557ce262009-08-28 08:45:04 -04002119 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002120 struct xdr_stream *xdr = &resp->xdr;
2121 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002122 __be32 status;
2123
Andy Adamson557ce262009-08-28 08:45:04 -04002124 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002125
Andy Adamsonabfabf82009-07-23 19:02:18 -04002126 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002127 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002128 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002129
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002130 p = xdr_reserve_space(xdr, slot->sl_datalen);
2131 if (!p) {
2132 WARN_ON_ONCE(1);
2133 return nfserr_serverfault;
2134 }
2135 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2136 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002137
Andy Adamson557ce262009-08-28 08:45:04 -04002138 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002139 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002140}
2141
Andy Adamson0733d212009-04-03 08:28:01 +03002142/*
2143 * Set the exchange_id flags returned by the server.
2144 */
2145static void
2146nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2147{
2148 /* pNFS is not supported */
2149 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2150
2151 /* Referrals are supported, Migration is not. */
2152 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2153
2154 /* set the wire flags to return to client. */
2155 clid->flags = new->cl_exchange_flags;
2156}
2157
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002158static bool client_has_state(struct nfs4_client *clp)
2159{
2160 /*
2161 * Note clp->cl_openowners check isn't quite right: there's no
2162 * need to count owners without stateid's.
2163 *
2164 * Also note we should probably be using this in 4.0 case too.
2165 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002166 return !list_empty(&clp->cl_openowners)
2167 || !list_empty(&clp->cl_delegations)
2168 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002169}
2170
Al Virob37ad282006-10-19 23:28:59 -07002171__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002172nfsd4_exchange_id(struct svc_rqst *rqstp,
2173 struct nfsd4_compound_state *cstate,
2174 struct nfsd4_exchange_id *exid)
2175{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002176 struct nfs4_client *conf, *new;
2177 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002178 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002179 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002180 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002181 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002182 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002183 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002184
Jeff Layton363168b2009-08-14 12:57:56 -04002185 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002186 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002187 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002188 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002189 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002190
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002191 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002192 return nfserr_inval;
2193
Andy Adamson0733d212009-04-03 08:28:01 +03002194 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002195 case SP4_MACH_CRED:
2196 if (!svc_rqst_integrity_protected(rqstp))
2197 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002198 case SP4_NONE:
2199 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002200 default: /* checked by xdr code */
2201 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002202 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002203 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002204 }
2205
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002206 new = create_client(exid->clname, rqstp, &verf);
2207 if (new == NULL)
2208 return nfserr_jukebox;
2209
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002210 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002211 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002212 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002213 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002214 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2215 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2216
J. Bruce Fields136e6582012-05-12 20:37:23 -04002217 if (update) {
2218 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002219 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002220 goto out;
2221 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002222 if (!mach_creds_match(conf, rqstp)) {
2223 status = nfserr_wrong_cred;
2224 goto out;
2225 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002226 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002227 status = nfserr_perm;
2228 goto out;
2229 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002230 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002231 status = nfserr_not_same;
2232 goto out;
2233 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002234 /* case 6 */
2235 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002236 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002237 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002238 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002239 if (client_has_state(conf)) {
2240 status = nfserr_clid_inuse;
2241 goto out;
2242 }
Andy Adamson0733d212009-04-03 08:28:01 +03002243 goto out_new;
2244 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002245 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002246 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002247 goto out_copy;
2248 }
2249 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002250 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002251 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002252 }
2253
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002254 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002255 status = nfserr_noent;
2256 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002257 }
2258
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002259 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002260 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002261 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002262
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002263 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002264out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002265 if (conf) {
2266 status = mark_client_expired_locked(conf);
2267 if (status)
2268 goto out;
2269 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002270 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002271 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002272
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002273 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002274 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002275 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002276out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002277 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2278 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002279
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002280 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2281 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002282
2283 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002284 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002285 status = nfs_ok;
2286
2287out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002288 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002289 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002290 expire_client(new);
2291 if (unconf)
2292 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002293 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002294}
2295
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002296static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002297check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002298{
Andy Adamson88e588d2009-07-23 19:02:15 -04002299 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2300 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002301
2302 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002303 if (slot_inuse) {
2304 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002305 return nfserr_jukebox;
2306 else
2307 return nfserr_seq_misordered;
2308 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002309 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002310 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002311 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002312 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002313 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002314 return nfserr_seq_misordered;
2315}
2316
Andy Adamson49557cc2009-07-23 19:02:16 -04002317/*
2318 * Cache the create session result into the create session single DRC
2319 * slot cache by saving the xdr structure. sl_seqid has been set.
2320 * Do this for solo or embedded create session operations.
2321 */
2322static void
2323nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002324 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002325{
2326 slot->sl_status = nfserr;
2327 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2328}
2329
2330static __be32
2331nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2332 struct nfsd4_clid_slot *slot)
2333{
2334 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2335 return slot->sl_status;
2336}
2337
Mi Jinlong1b74c252011-07-14 14:50:17 +08002338#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2339 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2340 1 + /* MIN tag is length with zero, only length */ \
2341 3 + /* version, opcount, opcode */ \
2342 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2343 /* seqid, slotID, slotID, cache */ \
2344 4 ) * sizeof(__be32))
2345
2346#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2347 2 + /* verifier: AUTH_NULL, length 0 */\
2348 1 + /* status */ \
2349 1 + /* MIN tag is length with zero, only length */ \
2350 3 + /* opcount, opcode, opstatus*/ \
2351 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2352 /* seqid, slotID, slotID, slotID, status */ \
2353 5 ) * sizeof(__be32))
2354
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002355static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002356{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002357 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2358
J. Bruce Fields373cd402013-04-08 15:42:12 -04002359 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2360 return nfserr_toosmall;
2361 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2362 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002363 ca->headerpadsz = 0;
2364 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2365 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2366 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2367 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2368 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2369 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2370 /*
2371 * Note decreasing slot size below client's request may make it
2372 * difficult for client to function correctly, whereas
2373 * decreasing the number of slots will (just?) affect
2374 * performance. When short on memory we therefore prefer to
2375 * decrease number of slots instead of their size. Clients that
2376 * request larger slots than they need will get poor results:
2377 */
2378 ca->maxreqs = nfsd4_get_drc_mem(ca);
2379 if (!ca->maxreqs)
2380 return nfserr_jukebox;
2381
J. Bruce Fields373cd402013-04-08 15:42:12 -04002382 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002383}
2384
Kinglong Mee8a891632013-12-23 18:11:02 +08002385#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2386 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2387#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2388 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2389
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002390static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2391{
2392 ca->headerpadsz = 0;
2393
2394 /*
2395 * These RPC_MAX_HEADER macros are overkill, especially since we
2396 * don't even do gss on the backchannel yet. But this is still
2397 * less than 1k. Tighten up this estimate in the unlikely event
2398 * it turns out to be a problem for some client:
2399 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002400 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002401 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002402 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002403 return nfserr_toosmall;
2404 ca->maxresp_cached = 0;
2405 if (ca->maxops < 2)
2406 return nfserr_toosmall;
2407
2408 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002409}
2410
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002411static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2412{
2413 switch (cbs->flavor) {
2414 case RPC_AUTH_NULL:
2415 case RPC_AUTH_UNIX:
2416 return nfs_ok;
2417 default:
2418 /*
2419 * GSS case: the spec doesn't allow us to return this
2420 * error. But it also doesn't allow us not to support
2421 * GSS.
2422 * I'd rather this fail hard than return some error the
2423 * client might think it can already handle:
2424 */
2425 return nfserr_encr_alg_unsupp;
2426 }
2427}
2428
Andy Adamson069b6ad2009-04-03 08:27:58 +03002429__be32
2430nfsd4_create_session(struct svc_rqst *rqstp,
2431 struct nfsd4_compound_state *cstate,
2432 struct nfsd4_create_session *cr_ses)
2433{
Jeff Layton363168b2009-08-14 12:57:56 -04002434 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002435 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002436 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002437 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002438 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002439 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002440 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002441 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002442
Mi Jinlonga62573d2011-03-23 17:57:07 +08002443 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2444 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002445 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2446 if (status)
2447 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002448 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002449 if (status)
2450 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002451 status = check_backchannel_attrs(&cr_ses->back_channel);
2452 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002453 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002454 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002455 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002456 if (!new)
2457 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002458 conn = alloc_conn_from_crses(rqstp, cr_ses);
2459 if (!conn)
2460 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002461
Jeff Laytond20c11d2014-07-30 08:27:07 -04002462 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002463 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002464 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002465 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002466
2467 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002468 status = nfserr_wrong_cred;
2469 if (!mach_creds_match(conf, rqstp))
2470 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002471 cs_slot = &conf->cl_cs_slot;
2472 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002473 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002474 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002475 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002476 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002477 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002478 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002479 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002480 } else if (unconf) {
2481 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002482 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002483 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002484 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002485 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002486 status = nfserr_wrong_cred;
2487 if (!mach_creds_match(unconf, rqstp))
2488 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002489 cs_slot = &unconf->cl_cs_slot;
2490 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002491 if (status) {
2492 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002493 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002494 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002495 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002496 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002497 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002498 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002499 if (status) {
2500 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002501 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002502 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002503 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002504 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002505 conf = unconf;
2506 } else {
2507 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002508 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002509 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002510 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002511 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002512 * We do not support RDMA or persistent sessions
2513 */
2514 cr_ses->flags &= ~SESSION4_PERSIST;
2515 cr_ses->flags &= ~SESSION4_RDMA;
2516
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002517 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002518 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002519
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002520 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002521 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002522 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002523 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002524
Jeff Laytond20c11d2014-07-30 08:27:07 -04002525 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002526 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002527 spin_unlock(&nn->client_lock);
2528 /* init connection and backchannel */
2529 nfsd4_init_conn(rqstp, conn, new);
2530 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002531 if (old)
2532 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002533 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002534out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002535 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002536 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002537 if (old)
2538 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002539out_free_session:
2540 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002541out_release_drc_mem:
2542 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002543 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002544}
2545
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002546static __be32 nfsd4_map_bcts_dir(u32 *dir)
2547{
2548 switch (*dir) {
2549 case NFS4_CDFC4_FORE:
2550 case NFS4_CDFC4_BACK:
2551 return nfs_ok;
2552 case NFS4_CDFC4_FORE_OR_BOTH:
2553 case NFS4_CDFC4_BACK_OR_BOTH:
2554 *dir = NFS4_CDFC4_BOTH;
2555 return nfs_ok;
2556 };
2557 return nfserr_inval;
2558}
2559
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002560__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2561{
2562 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002563 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002564 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002565
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002566 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2567 if (status)
2568 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002569 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002570 session->se_cb_prog = bc->bc_cb_program;
2571 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002572 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002573
2574 nfsd4_probe_callback(session->se_client);
2575
2576 return nfs_ok;
2577}
2578
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002579__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2580 struct nfsd4_compound_state *cstate,
2581 struct nfsd4_bind_conn_to_session *bcts)
2582{
2583 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002584 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002585 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002586 struct net *net = SVC_NET(rqstp);
2587 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002588
2589 if (!nfsd4_last_compound_op(rqstp))
2590 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002591 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002592 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002593 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002594 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002595 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002596 status = nfserr_wrong_cred;
2597 if (!mach_creds_match(session->se_client, rqstp))
2598 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002599 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002600 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002601 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002602 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002603 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002604 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002605 goto out;
2606 nfsd4_init_conn(rqstp, conn, session);
2607 status = nfs_ok;
2608out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002609 nfsd4_put_session(session);
2610out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002611 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002612}
2613
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002614static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2615{
2616 if (!session)
2617 return 0;
2618 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2619}
2620
Andy Adamson069b6ad2009-04-03 08:27:58 +03002621__be32
2622nfsd4_destroy_session(struct svc_rqst *r,
2623 struct nfsd4_compound_state *cstate,
2624 struct nfsd4_destroy_session *sessionid)
2625{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002626 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002627 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002628 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002629 struct net *net = SVC_NET(r);
2630 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002631
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002632 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002633 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002634 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002635 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002636 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002637 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002638 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002639 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002640 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002641 if (!ses)
2642 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002643 status = nfserr_wrong_cred;
2644 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002645 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002646 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002647 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002648 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002649 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002650 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002651
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002652 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002653
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002654 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002655 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002656out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002657 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002658out_client_lock:
2659 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002660out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03002661 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002662}
2663
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002664static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002665{
2666 struct nfsd4_conn *c;
2667
2668 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002669 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002670 return c;
2671 }
2672 }
2673 return NULL;
2674}
2675
J. Bruce Fields57266a62013-04-13 14:27:29 -04002676static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002677{
2678 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002679 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002680 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002681 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002682
2683 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002684 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002685 if (c)
2686 goto out_free;
2687 status = nfserr_conn_not_bound_to_session;
2688 if (clp->cl_mach_cred)
2689 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002690 __nfsd4_hash_conn(new, ses);
2691 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002692 ret = nfsd4_register_conn(new);
2693 if (ret)
2694 /* oops; xprt is already down: */
2695 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002696 return nfs_ok;
2697out_free:
2698 spin_unlock(&clp->cl_lock);
2699 free_conn(new);
2700 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002701}
2702
Mi Jinlong868b89c2011-04-27 09:09:58 +08002703static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2704{
2705 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2706
2707 return args->opcnt > session->se_fchannel.maxops;
2708}
2709
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002710static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2711 struct nfsd4_session *session)
2712{
2713 struct xdr_buf *xb = &rqstp->rq_arg;
2714
2715 return xb->len > session->se_fchannel.maxreq_sz;
2716}
2717
Andy Adamson069b6ad2009-04-03 08:27:58 +03002718__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002719nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002720 struct nfsd4_compound_state *cstate,
2721 struct nfsd4_sequence *seq)
2722{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002723 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002724 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002725 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002726 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002727 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002728 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002729 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002730 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002731 struct net *net = SVC_NET(rqstp);
2732 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002733
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002734 if (resp->opcnt != 1)
2735 return nfserr_sequence_pos;
2736
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002737 /*
2738 * Will be either used or freed by nfsd4_sequence_check_conn
2739 * below.
2740 */
2741 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2742 if (!conn)
2743 return nfserr_jukebox;
2744
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002745 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002746 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002747 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002748 goto out_no_session;
2749 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002750
Mi Jinlong868b89c2011-04-27 09:09:58 +08002751 status = nfserr_too_many_ops;
2752 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002753 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002754
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002755 status = nfserr_req_too_big;
2756 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002757 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002758
Benny Halevyb85d4c02009-04-03 08:28:08 +03002759 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002760 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002761 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002762
Andy Adamson557ce262009-08-28 08:45:04 -04002763 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002764 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2765
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002766 /* We do not negotiate the number of slots yet, so set the
2767 * maxslots to the session maxreqs which is used to encode
2768 * sr_highest_slotid and the sr_target_slot id to maxslots */
2769 seq->maxslots = session->se_fchannel.maxreqs;
2770
J. Bruce Fields73e79482012-02-13 16:39:00 -05002771 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2772 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002773 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002774 status = nfserr_seq_misordered;
2775 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002776 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002777 cstate->slot = slot;
2778 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002779 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002780 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002781 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002782 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002783 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002784 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002785 }
2786 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002787 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002788
J. Bruce Fields57266a62013-04-13 14:27:29 -04002789 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002790 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002791 if (status)
2792 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002793
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002794 buflen = (seq->cachethis) ?
2795 session->se_fchannel.maxresp_cached :
2796 session->se_fchannel.maxresp_sz;
2797 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2798 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002799 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002800 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002801 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002802
2803 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002804 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002805 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002806 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002807 if (seq->cachethis)
2808 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002809 else
2810 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002811
2812 cstate->slot = slot;
2813 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002814 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002815
Benny Halevyb85d4c02009-04-03 08:28:08 +03002816out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002817 switch (clp->cl_cb_state) {
2818 case NFSD4_CB_DOWN:
2819 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2820 break;
2821 case NFSD4_CB_FAULT:
2822 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2823 break;
2824 default:
2825 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002826 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002827 if (!list_empty(&clp->cl_revoked))
2828 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002829out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002830 if (conn)
2831 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002832 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002833 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002834out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002835 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002836 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002837}
2838
Trond Myklebustb6076642014-06-30 11:48:35 -04002839void
2840nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2841{
2842 struct nfsd4_compound_state *cs = &resp->cstate;
2843
2844 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002845 if (cs->status != nfserr_replay_cache) {
2846 nfsd4_store_cache_entry(resp);
2847 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2848 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002849 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002850 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002851 } else if (cs->clp)
2852 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002853}
2854
Mi Jinlong345c2842011-10-20 17:51:39 +08002855__be32
2856nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2857{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002858 struct nfs4_client *conf, *unconf;
2859 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002860 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002861 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002862
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002863 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002864 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002865 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002866 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002867
2868 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002869 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002870 status = nfserr_clientid_busy;
2871 goto out;
2872 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04002873 status = mark_client_expired_locked(conf);
2874 if (status)
2875 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002876 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08002877 } else if (unconf)
2878 clp = unconf;
2879 else {
2880 status = nfserr_stale_clientid;
2881 goto out;
2882 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002883 if (!mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002884 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002885 status = nfserr_wrong_cred;
2886 goto out;
2887 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002888 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002889out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002890 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002891 if (clp)
2892 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002893 return status;
2894}
2895
Andy Adamson069b6ad2009-04-03 08:27:58 +03002896__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002897nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2898{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002899 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002900
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002901 if (rc->rca_one_fs) {
2902 if (!cstate->current_fh.fh_dentry)
2903 return nfserr_nofilehandle;
2904 /*
2905 * We don't take advantage of the rca_one_fs case.
2906 * That's OK, it's optional, we can safely ignore it.
2907 */
2908 return nfs_ok;
2909 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002910
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002911 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002912 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2913 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002914 goto out;
2915
2916 status = nfserr_stale_clientid;
2917 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002918 /*
2919 * The following error isn't really legal.
2920 * But we only get here if the client just explicitly
2921 * destroyed the client. Surely it no longer cares what
2922 * error it gets back on an operation for the dead
2923 * client.
2924 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002925 goto out;
2926
2927 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002928 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002929out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002930 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002931}
2932
2933__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002934nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2935 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002937 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002939 struct nfs4_client *conf, *new;
2940 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002941 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002942 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2943
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002944 new = create_client(clname, rqstp, &clverifier);
2945 if (new == NULL)
2946 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002947 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002948 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002949 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002950 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002951 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002953 if (clp_used_exchangeid(conf))
2954 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002955 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002956 char addr_str[INET6_ADDRSTRLEN];
2957 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2958 sizeof(addr_str));
2959 dprintk("NFSD: setclientid: string in use by client "
2960 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 goto out;
2962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002964 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002965 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002966 unhash_client_locked(unconf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002967 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002968 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002970 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002971 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002972 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002973 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002974 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2976 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2977 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002978 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 status = nfs_ok;
2980out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002981 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002982 if (new)
2983 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002984 if (unconf)
2985 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 return status;
2987}
2988
2989
Al Virob37ad282006-10-19 23:28:59 -07002990__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002991nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2992 struct nfsd4_compound_state *cstate,
2993 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994{
NeilBrown21ab45a2005-06-23 22:04:14 -07002995 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002996 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2998 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002999 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003000 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003002 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003004
Jeff Laytond20c11d2014-07-30 08:27:07 -04003005 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003006 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003007 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003008 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003009 * We try hard to give out unique clientid's, so if we get an
3010 * attempt to confirm the same clientid with a different cred,
3011 * there's a bug somewhere. Let's charitably assume it's our
3012 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003013 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04003014 status = nfserr_serverfault;
3015 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3016 goto out;
3017 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3018 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003019 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003020 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3021 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003023 else /* case 4: client hasn't noticed we rebooted yet? */
3024 status = nfserr_stale_clientid;
3025 goto out;
3026 }
3027 status = nfs_ok;
3028 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003029 old = unconf;
3030 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003031 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003032 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003033 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3034 if (old) {
3035 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003036 if (status) {
3037 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003038 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003039 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003040 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003041 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003042 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003043 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003044 get_client_locked(conf);
3045 spin_unlock(&nn->client_lock);
3046 nfsd4_probe_callback(conf);
3047 spin_lock(&nn->client_lock);
3048 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003050 spin_unlock(&nn->client_lock);
3051 if (old)
3052 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 return status;
3054}
3055
J. Bruce Fields32513b42011-10-13 16:00:16 -04003056static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003058 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3059}
3060
3061/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003062static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3063 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003064{
Trond Myklebust950e0112014-06-30 11:48:31 -04003065 lockdep_assert_held(&state_lock);
3066
J. Bruce Fields32513b42011-10-13 16:00:16 -04003067 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003068 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003069 INIT_LIST_HEAD(&fp->fi_stateids);
3070 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003071 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003072 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003073 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003074 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003075 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3076 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton5b095e92014-10-23 08:01:02 -04003077 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078}
3079
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003080void
NeilBrowne60d4392005-06-23 22:03:01 -07003081nfsd4_free_slabs(void)
3082{
Christoph Hellwigabf11352014-05-21 07:43:03 -07003083 kmem_cache_destroy(openowner_slab);
3084 kmem_cache_destroy(lockowner_slab);
3085 kmem_cache_destroy(file_slab);
3086 kmem_cache_destroy(stateid_slab);
3087 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003088}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Bryan Schumaker72083392011-11-01 15:24:59 -04003090int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091nfsd4_init_slabs(void)
3092{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003093 openowner_slab = kmem_cache_create("nfsd4_openowners",
3094 sizeof(struct nfs4_openowner), 0, 0, NULL);
3095 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003096 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003097 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003098 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003099 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003100 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003101 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003102 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003103 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003104 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003105 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003106 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003107 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003108 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003109 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003110 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003111 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003112 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003114
3115out_free_stateid_slab:
3116 kmem_cache_destroy(stateid_slab);
3117out_free_file_slab:
3118 kmem_cache_destroy(file_slab);
3119out_free_lockowner_slab:
3120 kmem_cache_destroy(lockowner_slab);
3121out_free_openowner_slab:
3122 kmem_cache_destroy(openowner_slab);
3123out:
NeilBrowne60d4392005-06-23 22:03:01 -07003124 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3125 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126}
3127
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003128static void init_nfs4_replay(struct nfs4_replay *rp)
3129{
3130 rp->rp_status = nfserr_serverfault;
3131 rp->rp_buflen = 0;
3132 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003133 mutex_init(&rp->rp_mutex);
3134}
3135
3136static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3137 struct nfs4_stateowner *so)
3138{
3139 if (!nfsd4_has_session(cstate)) {
3140 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003141 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04003142 }
3143}
3144
3145void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3146{
3147 struct nfs4_stateowner *so = cstate->replay_owner;
3148
3149 if (so != NULL) {
3150 cstate->replay_owner = NULL;
3151 mutex_unlock(&so->so_replay.rp_mutex);
3152 nfs4_put_stateowner(so);
3153 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003154}
3155
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003156static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157{
3158 struct nfs4_stateowner *sop;
3159
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003160 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003161 if (!sop)
3162 return NULL;
3163
3164 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3165 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003166 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003167 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003169 sop->so_owner.len = owner->len;
3170
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003171 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003172 sop->so_client = clp;
3173 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003174 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003175 return sop;
3176}
3177
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003178static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003179{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003180 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003181
Trond Myklebustd4f04892014-07-29 21:34:36 -04003182 list_add(&oo->oo_owner.so_strhash,
3183 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003184 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185}
3186
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003187static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3188{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003189 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003190}
3191
Jeff Layton6b180f02014-07-29 21:34:26 -04003192static void nfs4_free_openowner(struct nfs4_stateowner *so)
3193{
3194 struct nfs4_openowner *oo = openowner(so);
3195
3196 kmem_cache_free(openowner_slab, oo);
3197}
3198
3199static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003200 .so_unhash = nfs4_unhash_openowner,
3201 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003202};
3203
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003204static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003205alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003206 struct nfsd4_compound_state *cstate)
3207{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003208 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003209 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003211 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3212 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003214 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003215 oo->oo_owner.so_is_open_owner = 1;
3216 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003217 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003218 if (nfsd4_has_session(cstate))
3219 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003220 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003221 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003222 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003223 spin_lock(&clp->cl_lock);
3224 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003225 if (ret == NULL) {
3226 hash_openowner(oo, clp, strhashval);
3227 ret = oo;
3228 } else
3229 nfs4_free_openowner(&oo->oo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003230 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003231 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232}
3233
J. Bruce Fields996e0932011-10-17 11:14:48 -04003234static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003235 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003237 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003238 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003239 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003240 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07003241 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003242 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 stp->st_access_bmap = 0;
3244 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003245 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003246 spin_lock(&oo->oo_owner.so_client->cl_lock);
3247 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003248 spin_lock(&fp->fi_lock);
3249 list_add(&stp->st_perfile, &fp->fi_stateids);
3250 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003251 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252}
3253
Jeff Laytond3134b12014-07-29 21:34:32 -04003254/*
3255 * In the 4.0 case we need to keep the owners around a little while to handle
3256 * CLOSE replay. We still do need to release any file access that is held by
3257 * them before returning however.
3258 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003260move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261{
Jeff Layton217526e2014-07-30 08:27:11 -04003262 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003263 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3264 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3265 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003266
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003267 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
Jeff Laytonb401be222014-07-29 21:34:33 -04003269 /*
3270 * We know that we hold one reference via nfsd4_close, and another
3271 * "persistent" reference for the client. If the refcount is higher
3272 * than 2, then there are still calls in progress that are using this
3273 * stateid. We can't put the sc_file reference until they are finished.
3274 * Wait for the refcount to drop to 2. Since it has been unhashed,
3275 * there should be no danger of the refcount going back up again at
3276 * this point.
3277 */
3278 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3279
Jeff Laytond3134b12014-07-29 21:34:32 -04003280 release_all_access(s);
3281 if (s->st_stid.sc_file) {
3282 put_nfs4_file(s->st_stid.sc_file);
3283 s->st_stid.sc_file = NULL;
3284 }
Jeff Layton217526e2014-07-30 08:27:11 -04003285
3286 spin_lock(&nn->client_lock);
3287 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003288 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003289 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003290 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003291 spin_unlock(&nn->client_lock);
3292 if (last)
3293 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294}
3295
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296/* search file_hashtbl[] for file */
3297static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04003298find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 struct nfs4_file *fp;
3301
Jeff Layton5b095e92014-10-23 08:01:02 -04003302 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003303 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
Jeff Layton5b095e92014-10-23 08:01:02 -04003304 if (atomic_inc_not_zero(&fp->fi_ref))
3305 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 }
3308 return NULL;
3309}
3310
Trond Myklebust950e0112014-06-30 11:48:31 -04003311static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003312find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003313{
3314 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003315 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003316
Jeff Layton5b095e92014-10-23 08:01:02 -04003317 rcu_read_lock();
3318 fp = find_file_locked(fh, hashval);
3319 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04003320 return fp;
3321}
3322
3323static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003324find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003325{
3326 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003327 unsigned int hashval = file_hashval(fh);
3328
3329 rcu_read_lock();
3330 fp = find_file_locked(fh, hashval);
3331 rcu_read_unlock();
3332 if (fp)
3333 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04003334
3335 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04003336 fp = find_file_locked(fh, hashval);
3337 if (likely(fp == NULL)) {
3338 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04003339 fp = new;
3340 }
3341 spin_unlock(&state_lock);
3342
3343 return fp;
3344}
3345
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003346/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 * Called to check deny when READ with all zero stateid or
3348 * WRITE with all zero or all one stateid
3349 */
Al Virob37ad282006-10-19 23:28:59 -07003350static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3352{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003354 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
Trond Myklebustca943212014-07-23 16:17:39 -04003356 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003357 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003358 return ret;
3359 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003360 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003361 if (fp->fi_share_deny & deny_type)
3362 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003363 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003364 put_nfs4_file(fp);
3365 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366}
3367
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003368static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003370 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04003371 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3372 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003373
Trond Myklebust11b91642014-07-29 21:34:08 -04003374 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003375
Jeff Layton02e12152014-07-16 10:31:57 -04003376 /*
3377 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003378 * already holding inode->i_lock.
3379 *
Jeff Laytondff13992014-07-08 14:02:49 -04003380 * If the dl_time != 0, then we know that it has already been
3381 * queued for a lease break. Don't queue it again.
3382 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003383 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003384 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003385 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003386 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003387 }
Jeff Layton02e12152014-07-16 10:31:57 -04003388 spin_unlock(&state_lock);
3389}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003391static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3392 struct rpc_task *task)
3393{
3394 struct nfs4_delegation *dp = cb_to_delegation(cb);
3395
3396 switch (task->tk_status) {
3397 case 0:
3398 return 1;
3399 case -EBADHANDLE:
3400 case -NFS4ERR_BAD_STATEID:
3401 /*
3402 * Race: client probably got cb_recall before open reply
3403 * granting delegation.
3404 */
3405 if (dp->dl_retries--) {
3406 rpc_delay(task, 2 * HZ);
3407 return 0;
3408 }
3409 /*FALLTHRU*/
3410 default:
3411 return -1;
3412 }
3413}
3414
3415static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3416{
3417 struct nfs4_delegation *dp = cb_to_delegation(cb);
3418
3419 nfs4_put_stid(&dp->dl_stid);
3420}
3421
3422static struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3423 .prepare = nfsd4_cb_recall_prepare,
3424 .done = nfsd4_cb_recall_done,
3425 .release = nfsd4_cb_recall_release,
3426};
3427
Jeff Layton02e12152014-07-16 10:31:57 -04003428static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3429{
3430 /*
3431 * We're assuming the state code never drops its reference
3432 * without first removing the lease. Since we're in this lease
3433 * callback (and since the lease code is serialized by the kernel
3434 * lock) we know the server hasn't removed the lease yet, we know
3435 * it's safe to take a reference.
3436 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003437 atomic_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02003438 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003439}
3440
Jeff Layton1c8c6012013-06-21 08:58:15 -04003441/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003442static bool
3443nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003444{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003445 bool ret = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003446 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3447 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003448
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003449 if (!fp) {
3450 WARN(1, "(%p)->fl_owner NULL\n", fl);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003451 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003452 }
3453 if (fp->fi_had_conflict) {
3454 WARN(1, "duplicate break on %p\n", fp);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003455 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003456 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003457 /*
3458 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003459 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003460 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003461 */
3462 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
Jeff Layton02e12152014-07-16 10:31:57 -04003464 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003465 fp->fi_had_conflict = true;
3466 /*
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003467 * If there are no delegations on the list, then return true
3468 * so that the lease code will go ahead and delete it.
Jeff Layton417c6622014-07-21 09:34:57 -04003469 */
3470 if (list_empty(&fp->fi_delegations))
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003471 ret = true;
Jeff Layton417c6622014-07-21 09:34:57 -04003472 else
3473 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3474 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003475 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003476 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477}
3478
Jeff Laytonc45198e2014-09-01 07:12:07 -04003479static int
Jeff Layton7448cc32015-01-16 15:05:57 -05003480nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3481 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482{
3483 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04003484 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 else
3486 return -EAGAIN;
3487}
3488
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003489static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003490 .lm_break = nfsd_break_deleg_cb,
3491 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492};
3493
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003494static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3495{
3496 if (nfsd4_has_session(cstate))
3497 return nfs_ok;
3498 if (seqid == so->so_seqid - 1)
3499 return nfserr_replay_me;
3500 if (seqid == so->so_seqid)
3501 return nfs_ok;
3502 return nfserr_bad_seqid;
3503}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
Jeff Layton4b24ca72014-06-30 11:48:44 -04003505static __be32 lookup_clientid(clientid_t *clid,
3506 struct nfsd4_compound_state *cstate,
3507 struct nfsd_net *nn)
3508{
3509 struct nfs4_client *found;
3510
3511 if (cstate->clp) {
3512 found = cstate->clp;
3513 if (!same_clid(&found->cl_clientid, clid))
3514 return nfserr_stale_clientid;
3515 return nfs_ok;
3516 }
3517
3518 if (STALE_CLIENTID(clid, nn))
3519 return nfserr_stale_clientid;
3520
3521 /*
3522 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3523 * cached already then we know this is for is for v4.0 and "sessions"
3524 * will be false.
3525 */
3526 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003527 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003528 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003529 if (!found) {
3530 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003531 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003532 }
3533 atomic_inc(&found->cl_refcount);
3534 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003535
3536 /* Cache the nfs4_client in cstate! */
3537 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003538 return nfs_ok;
3539}
3540
Al Virob37ad282006-10-19 23:28:59 -07003541__be32
Andy Adamson66689582009-04-03 08:28:45 +03003542nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003543 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 clientid_t *clientid = &open->op_clientid;
3546 struct nfs4_client *clp = NULL;
3547 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003548 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003549 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003551 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003553 /*
3554 * In case we need it later, after we've already created the
3555 * file and don't want to risk a further failure:
3556 */
3557 open->op_file = nfsd4_alloc_file();
3558 if (open->op_file == NULL)
3559 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Trond Myklebust2d91e892014-06-30 11:48:46 -04003561 status = lookup_clientid(clientid, cstate, nn);
3562 if (status)
3563 return status;
3564 clp = cstate->clp;
3565
Trond Myklebustd4f04892014-07-29 21:34:36 -04003566 strhashval = ownerstr_hashval(&open->op_owner);
3567 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003568 open->op_openowner = oo;
3569 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003570 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003572 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003573 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003574 release_openowner(oo);
3575 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003576 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003577 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003578 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3579 if (status)
3580 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003581 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003582new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003583 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003584 if (oo == NULL)
3585 return nfserr_jukebox;
3586 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003587alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003588 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003589 if (!open->op_stp)
3590 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003591 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592}
3593
Al Virob37ad282006-10-19 23:28:59 -07003594static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003595nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3596{
3597 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3598 return nfserr_openmode;
3599 else
3600 return nfs_ok;
3601}
3602
Daniel Mackc47d8322011-05-16 16:38:14 +02003603static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003604{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003605 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3606}
3607
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003608static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003609{
3610 struct nfs4_stid *ret;
3611
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003612 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003613 if (!ret)
3614 return NULL;
3615 return delegstateid(ret);
3616}
3617
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003618static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3619{
3620 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3621 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3622}
3623
Al Virob37ad282006-10-19 23:28:59 -07003624static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003625nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003626 struct nfs4_delegation **dp)
3627{
3628 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003629 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003630 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003631
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003632 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3633 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003634 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003635 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003636 status = nfs4_check_delegmode(deleg, flags);
3637 if (status) {
3638 nfs4_put_stid(&deleg->dl_stid);
3639 goto out;
3640 }
3641 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003642out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003643 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003644 return nfs_ok;
3645 if (status)
3646 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003647 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003648 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003649}
3650
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003651static struct nfs4_ol_stateid *
3652nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003654 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003655 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
Trond Myklebust1d31a252014-07-10 14:07:25 -04003657 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003658 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 /* ignore lock owners */
3660 if (local->st_stateowner->so_is_open_owner == 0)
3661 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003662 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003663 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003664 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003665 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003668 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003669 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670}
3671
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003672static inline int nfs4_access_to_access(u32 nfs4_access)
3673{
3674 int flags = 0;
3675
3676 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3677 flags |= NFSD_MAY_READ;
3678 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3679 flags |= NFSD_MAY_WRITE;
3680 return flags;
3681}
3682
Al Virob37ad282006-10-19 23:28:59 -07003683static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3685 struct nfsd4_open *open)
3686{
3687 struct iattr iattr = {
3688 .ia_valid = ATTR_SIZE,
3689 .ia_size = 0,
3690 };
3691 if (!open->op_truncate)
3692 return 0;
3693 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003694 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3696}
3697
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003698static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003699 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3700 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003701{
Trond Myklebustde186432014-07-10 14:07:26 -04003702 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003703 __be32 status;
3704 int oflag = nfs4_access_to_omode(open->op_share_access);
3705 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003706 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003707
Trond Myklebustde186432014-07-10 14:07:26 -04003708 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003709
3710 /*
3711 * Are we trying to set a deny mode that would conflict with
3712 * current access?
3713 */
3714 status = nfs4_file_check_deny(fp, open->op_share_deny);
3715 if (status != nfs_ok) {
3716 spin_unlock(&fp->fi_lock);
3717 goto out;
3718 }
3719
3720 /* set access to the file */
3721 status = nfs4_file_get_access(fp, open->op_share_access);
3722 if (status != nfs_ok) {
3723 spin_unlock(&fp->fi_lock);
3724 goto out;
3725 }
3726
3727 /* Set access bits in stateid */
3728 old_access_bmap = stp->st_access_bmap;
3729 set_access(open->op_share_access, stp);
3730
3731 /* Set new deny mask */
3732 old_deny_bmap = stp->st_deny_bmap;
3733 set_deny(open->op_share_deny, stp);
3734 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3735
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003736 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003737 spin_unlock(&fp->fi_lock);
3738 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003739 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003740 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003741 spin_lock(&fp->fi_lock);
3742 if (!fp->fi_fds[oflag]) {
3743 fp->fi_fds[oflag] = filp;
3744 filp = NULL;
3745 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003746 }
Trond Myklebustde186432014-07-10 14:07:26 -04003747 spin_unlock(&fp->fi_lock);
3748 if (filp)
3749 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003750
3751 status = nfsd4_truncate(rqstp, cur_fh, open);
3752 if (status)
3753 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003754out:
3755 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003756out_put_access:
3757 stp->st_access_bmap = old_access_bmap;
3758 nfs4_file_put_access(fp, open->op_share_access);
3759 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3760 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003761}
3762
Al Virob37ad282006-10-19 23:28:59 -07003763static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003764nfs4_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 -07003765{
Al Virob37ad282006-10-19 23:28:59 -07003766 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003767 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003769 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003770 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003771
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003772 /* test and set deny mode */
3773 spin_lock(&fp->fi_lock);
3774 status = nfs4_file_check_deny(fp, open->op_share_deny);
3775 if (status == nfs_ok) {
3776 old_deny_bmap = stp->st_deny_bmap;
3777 set_deny(open->op_share_deny, stp);
3778 fp->fi_share_deny |=
3779 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3780 }
3781 spin_unlock(&fp->fi_lock);
3782
3783 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003786 status = nfsd4_truncate(rqstp, cur_fh, open);
3787 if (status != nfs_ok)
3788 reset_union_bmap_deny(old_deny_bmap, stp);
3789 return status;
3790}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003793nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003795 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796}
3797
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003798/* Should we give out recallable state?: */
3799static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3800{
3801 if (clp->cl_cb_state == NFSD4_CB_UP)
3802 return true;
3803 /*
3804 * In the sessions case, since we don't have to establish a
3805 * separate connection for callbacks, we assume it's OK
3806 * until we hear otherwise:
3807 */
3808 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3809}
3810
Jeff Laytond564fbe2014-07-16 10:31:58 -04003811static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003812{
3813 struct file_lock *fl;
3814
3815 fl = locks_alloc_lock();
3816 if (!fl)
3817 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003818 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003819 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003820 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3821 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003822 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003823 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003824 return fl;
3825}
3826
J. Bruce Fields99c41512013-05-21 16:21:25 -04003827static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003828{
Trond Myklebust11b91642014-07-29 21:34:08 -04003829 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton415b96c2014-08-22 12:26:36 -04003830 struct file_lock *fl, *ret;
Jeff Layton417c6622014-07-21 09:34:57 -04003831 struct file *filp;
3832 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003833
Jeff Laytond564fbe2014-07-16 10:31:58 -04003834 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003835 if (!fl)
3836 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003837 filp = find_readable_file(fp);
3838 if (!filp) {
3839 /* We should always have a readable file here */
3840 WARN_ON_ONCE(1);
3841 return -EBADF;
3842 }
3843 fl->fl_file = filp;
Jeff Layton415b96c2014-08-22 12:26:36 -04003844 ret = fl;
Jeff Laytone6f5c782014-08-22 10:40:25 -04003845 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003846 if (fl)
Jeff Layton417c6622014-07-21 09:34:57 -04003847 locks_free_lock(fl);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003848 if (status)
Jeff Layton417c6622014-07-21 09:34:57 -04003849 goto out_fput;
Benny Halevycdc97502014-05-30 09:09:30 -04003850 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003851 spin_lock(&fp->fi_lock);
3852 /* Did the lease get broken before we took the lock? */
3853 status = -EAGAIN;
3854 if (fp->fi_had_conflict)
3855 goto out_unlock;
3856 /* Race breaker */
Jeff Layton0c637be2014-08-22 12:05:43 -04003857 if (fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04003858 status = 0;
3859 atomic_inc(&fp->fi_delegees);
3860 hash_delegation_locked(dp, fp);
3861 goto out_unlock;
3862 }
Jeff Layton417c6622014-07-21 09:34:57 -04003863 fp->fi_deleg_file = filp;
3864 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003865 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003866 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003867 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003868 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003869out_unlock:
3870 spin_unlock(&fp->fi_lock);
3871 spin_unlock(&state_lock);
3872out_fput:
3873 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003874 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003875}
3876
Jeff Layton0b266932014-07-25 07:34:25 -04003877static struct nfs4_delegation *
3878nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3879 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003880{
Jeff Layton0b266932014-07-25 07:34:25 -04003881 int status;
3882 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003883
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003884 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003885 return ERR_PTR(-EAGAIN);
3886
3887 dp = alloc_init_deleg(clp, fh);
3888 if (!dp)
3889 return ERR_PTR(-ENOMEM);
3890
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003891 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003892 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003893 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003894 dp->dl_stid.sc_file = fp;
Jeff Layton0c637be2014-08-22 12:05:43 -04003895 if (!fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04003896 spin_unlock(&fp->fi_lock);
3897 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003898 status = nfs4_setlease(dp);
3899 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003900 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003901 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003902 status = -EAGAIN;
3903 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003904 }
Jeff Layton94ae1db2014-12-13 09:11:39 -05003905 atomic_inc(&fp->fi_delegees);
Benny Halevy931ee562014-05-30 09:09:27 -04003906 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003907 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003908out_unlock:
3909 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003910 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003911out:
3912 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003913 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003914 return ERR_PTR(status);
3915 }
3916 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003917}
3918
Benny Halevy4aa89132012-02-21 14:16:44 -08003919static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3920{
3921 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3922 if (status == -EAGAIN)
3923 open->op_why_no_deleg = WND4_CONTENTION;
3924 else {
3925 open->op_why_no_deleg = WND4_RESOURCE;
3926 switch (open->op_deleg_want) {
3927 case NFS4_SHARE_WANT_READ_DELEG:
3928 case NFS4_SHARE_WANT_WRITE_DELEG:
3929 case NFS4_SHARE_WANT_ANY_DELEG:
3930 break;
3931 case NFS4_SHARE_WANT_CANCEL:
3932 open->op_why_no_deleg = WND4_CANCELLED;
3933 break;
3934 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003935 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003936 }
3937 }
3938}
3939
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940/*
3941 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003942 *
3943 * Note we don't support write delegations, and won't until the vfs has
3944 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 */
3946static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003947nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3948 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949{
3950 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003951 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3952 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003953 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003954 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003956 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003957 open->op_recall = 0;
3958 switch (open->op_claim_type) {
3959 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003960 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003961 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003962 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3963 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003964 break;
3965 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003966 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003967 /*
3968 * Let's not give out any delegations till everyone's
3969 * had the chance to reclaim theirs....
3970 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003971 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003972 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003973 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003974 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003975 /*
3976 * Also, if the file was opened for write or
3977 * create, there's a good chance the client's
3978 * about to write to it, resulting in an
3979 * immediate recall (since we don't support
3980 * write delegations):
3981 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003982 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003983 goto out_no_deleg;
3984 if (open->op_create == NFS4_OPEN_CREATE)
3985 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003986 break;
3987 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003988 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003989 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003990 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003991 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003992 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003994 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003996 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003997 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003998 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003999 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004000 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004001out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004002 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4003 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004004 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04004005 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004006 open->op_recall = 1;
4007 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004008
4009 /* 4.1 client asking for a delegation? */
4010 if (open->op_deleg_want)
4011 nfsd4_open_deleg_none_ext(open, status);
4012 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013}
4014
Benny Halevye27f49c2012-02-21 14:16:54 -08004015static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4016 struct nfs4_delegation *dp)
4017{
4018 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4019 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4020 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4021 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4022 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4023 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4024 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4025 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4026 }
4027 /* Otherwise the client must be confused wanting a delegation
4028 * it already has, therefore we don't return
4029 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4030 */
4031}
4032
Al Virob37ad282006-10-19 23:28:59 -07004033__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4035{
Andy Adamson66689582009-04-03 08:28:45 +03004036 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004037 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004039 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004040 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004041 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 /*
4044 * Lookup file; if found, lookup stateid and check open request,
4045 * and check for delegations in the process of being recalled.
4046 * If not found, create the nfs4_file struct
4047 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004048 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004049 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004050 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004051 if (status)
4052 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04004053 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004055 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004056 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004057 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004058 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004059 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 }
4061
4062 /*
4063 * OPEN the file, or upgrade an existing OPEN.
4064 * If truncate fails, the OPEN fails.
4065 */
4066 if (stp) {
4067 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004068 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 if (status)
4070 goto out;
4071 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004072 stp = open->op_stp;
4073 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004074 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004075 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4076 if (status) {
4077 release_open_stateid(stp);
4078 goto out;
4079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004081 update_stateid(&stp->st_stid.sc_stateid);
4082 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083
Benny Halevyd24433c2012-02-16 20:57:17 +02004084 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004085 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4086 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4087 open->op_why_no_deleg = WND4_NOT_WANTED;
4088 goto nodeleg;
4089 }
4090 }
4091
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 /*
4093 * Attempt to hand out a delegation. No error return, because the
4094 * OPEN succeeds even if we fail.
4095 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004096 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004097nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 status = nfs_ok;
4099
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004100 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004101 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004103 /* 4.1 client trying to upgrade/downgrade delegation? */
4104 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004105 open->op_deleg_want)
4106 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004107
NeilBrown13cd2182005-06-23 22:03:10 -07004108 if (fp)
4109 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004110 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05004111 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 /*
4113 * To finish the open response, we just need to set the rflags.
4114 */
4115 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004116 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004117 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004119 if (dp)
4120 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004121 if (stp)
4122 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123
4124 return status;
4125}
4126
Jeff Layton58fb12e2014-07-29 21:34:27 -04004127void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4128 struct nfsd4_open *open, __be32 status)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004129{
4130 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004131 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004132
Jeff Laytond3134b12014-07-29 21:34:32 -04004133 nfsd4_cstate_assign_replay(cstate, so);
4134 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004135 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004136 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04004137 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004138 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004139 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004140}
4141
Al Virob37ad282006-10-19 23:28:59 -07004142__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004143nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4144 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145{
4146 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004147 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004148 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 dprintk("process_renew(%08x/%08x): starting\n",
4151 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004152 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004153 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004155 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004157 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004158 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 goto out;
4160 status = nfs_ok;
4161out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 return status;
4163}
4164
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004165void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004166nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004167{
Jeff Layton33dcc482012-04-10 11:08:48 -04004168 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004169 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004170 return;
4171
NeilBrowna76b4312005-06-23 22:04:01 -07004172 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004173 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004174 /*
4175 * If the server goes down again right now, an NFSv4
4176 * client will still be allowed to reclaim after it comes back up,
4177 * even if it hasn't yet had a chance to reclaim state this time.
4178 *
4179 */
Jeff Layton919b8042014-09-12 16:40:20 -04004180 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004181 /*
4182 * At this point, NFSv4 clients can still reclaim. But if the
4183 * server crashes, any that have not yet reclaimed will be out
4184 * of luck on the next boot.
4185 *
4186 * (NFSv4.1+ clients are considered to have reclaimed once they
4187 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4188 * have reclaimed after their first OPEN.)
4189 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004190 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004191 /*
4192 * At this point, and once lockd and/or any other containers
4193 * exit their grace period, further reclaims will fail and
4194 * regular locking can resume.
4195 */
NeilBrowna76b4312005-06-23 22:04:01 -07004196}
4197
NeilBrownfd39ca92005-06-23 22:04:03 -07004198static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004199nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200{
4201 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004202 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004204 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004206 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004207 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004210 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004211 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004212 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004213 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 clp = list_entry(pos, struct nfs4_client, cl_lru);
4215 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4216 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004217 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 break;
4219 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004220 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004221 dprintk("NFSD: client in use (clientid %08x)\n",
4222 clp->cl_clientid.cl_id);
4223 continue;
4224 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004225 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004226 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004227 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004228 list_for_each_safe(pos, next, &reaplist) {
4229 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 dprintk("NFSD: purging unused client (clientid %08x)\n",
4231 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004232 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 expire_client(clp);
4234 }
Benny Halevycdc97502014-05-30 09:09:30 -04004235 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004236 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004238 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4239 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004241 t = dp->dl_time - cutoff;
4242 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 break;
4244 }
Jeff Layton42690672014-07-25 07:34:20 -04004245 unhash_delegation_locked(dp);
4246 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 }
Benny Halevycdc97502014-05-30 09:09:30 -04004248 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004249 while (!list_empty(&reaplist)) {
4250 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4251 dl_recall_lru);
4252 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004253 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 }
Jeff Layton217526e2014-07-30 08:27:11 -04004255
4256 spin_lock(&nn->client_lock);
4257 while (!list_empty(&nn->close_lru)) {
4258 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4259 oo_close_lru);
4260 if (time_after((unsigned long)oo->oo_time,
4261 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004262 t = oo->oo_time - cutoff;
4263 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 break;
4265 }
Jeff Layton217526e2014-07-30 08:27:11 -04004266 list_del_init(&oo->oo_close_lru);
4267 stp = oo->oo_last_closed_stid;
4268 oo->oo_last_closed_stid = NULL;
4269 spin_unlock(&nn->client_lock);
4270 nfs4_put_stid(&stp->st_stid);
4271 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 }
Jeff Layton217526e2014-07-30 08:27:11 -04004273 spin_unlock(&nn->client_lock);
4274
Jeff Laytona832e7a2014-05-30 09:09:26 -04004275 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004276 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277}
4278
Harvey Harrisona254b242008-02-20 12:49:00 -08004279static struct workqueue_struct *laundry_wq;
4280static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004281
4282static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004283laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284{
4285 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004286 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4287 work);
4288 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4289 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004291 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004293 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294}
4295
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004296static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004297{
Trond Myklebust11b91642014-07-29 21:34:08 -04004298 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004299 return nfserr_bad_stateid;
4300 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301}
4302
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004304access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004306 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4307 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4308 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309}
4310
4311static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004312access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004314 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4315 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316}
4317
4318static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004319__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320{
Al Virob37ad282006-10-19 23:28:59 -07004321 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322
J. Bruce Fields02921912010-07-29 15:16:59 -04004323 /* For lock stateid's, we test the parent open, not the lock: */
4324 if (stp->st_openstp)
4325 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004326 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004328 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 goto out;
4330 status = nfs_ok;
4331out:
4332 return status;
4333}
4334
Al Virob37ad282006-10-19 23:28:59 -07004335static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004336check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004338 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004340 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004341 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 * conflicting state; so we must wait out the grace period. */
4343 return nfserr_grace;
4344 } else if (flags & WR_STATE)
4345 return nfs4_share_conflict(current_fh,
4346 NFS4_SHARE_DENY_WRITE);
4347 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4348 return nfs4_share_conflict(current_fh,
4349 NFS4_SHARE_DENY_READ);
4350}
4351
4352/*
4353 * Allow READ/WRITE during grace period on recovered state only for files
4354 * that are not able to provide mandatory locking.
4355 */
4356static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004357grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004359 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360}
4361
J. Bruce Fields81b82962011-08-23 11:03:29 -04004362/* Returns true iff a is later than b: */
4363static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4364{
Jim Rees1a9357f2013-05-17 17:33:00 -04004365 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004366}
4367
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004368static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004369{
Andy Adamson66689582009-04-03 08:28:45 +03004370 /*
4371 * When sessions are used the stateid generation number is ignored
4372 * when it is zero.
4373 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004374 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004375 return nfs_ok;
4376
4377 if (in->si_generation == ref->si_generation)
4378 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004379
J. Bruce Fields0836f582008-01-26 19:08:12 -05004380 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004381 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004382 return nfserr_bad_stateid;
4383 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004384 * However, we could see a stateid from the past, even from a
4385 * non-buggy client. For example, if the client sends a lock
4386 * while some IO is outstanding, the lock may bump si_generation
4387 * while the IO is still in flight. The client could avoid that
4388 * situation by waiting for responses on all the IO requests,
4389 * but better performance may result in retrying IO that
4390 * receives an old_stateid error if requests are rarely
4391 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004392 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004393 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004394}
4395
Chuck Lever7df302f2012-05-29 13:56:37 -04004396static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004397{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004398 struct nfs4_stid *s;
4399 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004400 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004401
Chuck Lever7df302f2012-05-29 13:56:37 -04004402 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004403 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004404 /* Client debugging aid. */
4405 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4406 char addr_str[INET6_ADDRSTRLEN];
4407 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4408 sizeof(addr_str));
4409 pr_warn_ratelimited("NFSD: client %s testing state ID "
4410 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004411 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004412 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004413 spin_lock(&cl->cl_lock);
4414 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004415 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004416 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004417 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004418 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004419 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004420 switch (s->sc_type) {
4421 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004422 status = nfs_ok;
4423 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004424 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004425 status = nfserr_deleg_revoked;
4426 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004427 case NFS4_OPEN_STID:
4428 case NFS4_LOCK_STID:
4429 ols = openlockstateid(s);
4430 if (ols->st_stateowner->so_is_open_owner
4431 && !(openowner(ols->st_stateowner)->oo_flags
4432 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004433 status = nfserr_bad_stateid;
4434 else
4435 status = nfs_ok;
4436 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004437 default:
4438 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004439 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004440 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004441 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004442 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004443 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004444out_unlock:
4445 spin_unlock(&cl->cl_lock);
4446 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004447}
4448
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004449static __be32
4450nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4451 stateid_t *stateid, unsigned char typemask,
4452 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004453{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004454 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004455
4456 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4457 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004458 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004459 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004460 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004461 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004462 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004463 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004464 if (status)
4465 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004466 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004467 if (!*s)
4468 return nfserr_bad_stateid;
4469 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004470}
4471
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472/*
4473* Checks for stateid operations
4474*/
Al Virob37ad282006-10-19 23:28:59 -07004475__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004476nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004477 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004479 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004480 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004482 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004484 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004485 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004486 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 if (filpp)
4489 *filpp = NULL;
4490
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004491 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 return nfserr_grace;
4493
4494 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004495 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004497 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004498 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004499 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004500 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04004501 return status;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004502 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4503 if (status)
4504 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004505 switch (s->sc_type) {
4506 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004507 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004508 status = nfs4_check_delegmode(dp, flags);
4509 if (status)
4510 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004511 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004512 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004513 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004514 WARN_ON_ONCE(1);
4515 status = nfserr_serverfault;
4516 goto out;
4517 }
Trond Myklebustde186432014-07-10 14:07:26 -04004518 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004519 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004520 break;
4521 case NFS4_OPEN_STID:
4522 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004523 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004524 status = nfs4_check_fh(current_fh, stp);
4525 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004527 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004528 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004530 status = nfs4_check_openmode(stp, flags);
4531 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004533 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004534 struct nfs4_file *fp = stp->st_stid.sc_file;
4535
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004536 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004537 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004538 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004539 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004540 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004541 break;
4542 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004543 status = nfserr_bad_stateid;
4544 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 }
4546 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004547 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004548 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004550 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 return status;
4552}
4553
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004554/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004555 * Test if the stateid is valid
4556 */
4557__be32
4558nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4559 struct nfsd4_test_stateid *test_stateid)
4560{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004561 struct nfsd4_test_stateid_id *stateid;
4562 struct nfs4_client *cl = cstate->session->se_client;
4563
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004564 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004565 stateid->ts_id_status =
4566 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004567
Bryan Schumaker17456802011-07-13 10:50:48 -04004568 return nfs_ok;
4569}
4570
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004571__be32
4572nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4573 struct nfsd4_free_stateid *free_stateid)
4574{
4575 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004576 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004577 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004578 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004579 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004580 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004581
Jeff Layton1af71cc2014-07-29 21:34:14 -04004582 spin_lock(&cl->cl_lock);
4583 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004584 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004585 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004586 switch (s->sc_type) {
4587 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004588 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004589 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004590 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004591 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4592 if (ret)
4593 break;
4594 ret = nfserr_locks_held;
4595 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004596 case NFS4_LOCK_STID:
4597 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4598 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004599 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004600 stp = openlockstateid(s);
4601 ret = nfserr_locks_held;
4602 if (check_for_locks(stp->st_stid.sc_file,
4603 lockowner(stp->st_stateowner)))
4604 break;
4605 unhash_lock_stateid(stp);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004606 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004607 nfs4_put_stid(s);
4608 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004609 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004610 case NFS4_REVOKED_DELEG_STID:
4611 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004612 list_del_init(&dp->dl_recall_lru);
4613 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004614 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004615 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004616 goto out;
4617 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004618 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004619out_unlock:
4620 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004621out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004622 return ret;
4623}
4624
NeilBrown4c4cd222005-07-07 17:59:27 -07004625static inline int
4626setlkflg (int type)
4627{
4628 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4629 RD_STATE : WR_STATE;
4630}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004632static __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 -04004633{
4634 struct svc_fh *current_fh = &cstate->current_fh;
4635 struct nfs4_stateowner *sop = stp->st_stateowner;
4636 __be32 status;
4637
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004638 status = nfsd4_check_seqid(cstate, sop, seqid);
4639 if (status)
4640 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004641 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4642 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004643 /*
4644 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004645 * nfserr_replay_me from the previous step, and
4646 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004647 */
4648 return nfserr_bad_stateid;
4649 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4650 if (status)
4651 return status;
4652 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004653}
4654
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655/*
4656 * Checks for sequence id mutating operations.
4657 */
Al Virob37ad282006-10-19 23:28:59 -07004658static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004659nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004660 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004661 struct nfs4_ol_stateid **stpp,
4662 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004664 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004665 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004666 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004668 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4669 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004670
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004672 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004673 if (status)
4674 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004675 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004676 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004678 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004679 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004680 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004681 else
4682 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004683 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004684}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004685
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004686static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4687 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004688{
4689 __be32 status;
4690 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004691 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004693 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004694 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004695 if (status)
4696 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004697 oo = openowner(stp->st_stateowner);
4698 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4699 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004700 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004701 }
4702 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004703 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704}
4705
Al Virob37ad282006-10-19 23:28:59 -07004706__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004707nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004708 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709{
Al Virob37ad282006-10-19 23:28:59 -07004710 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004711 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004712 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004713 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
Al Viroa6a9f182013-09-16 10:57:01 -04004715 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4716 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004718 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004719 if (status)
4720 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004722 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004723 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004724 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004725 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004726 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004727 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004728 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004729 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004730 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004731 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004732 update_stateid(&stp->st_stid.sc_stateid);
4733 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004734 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004735 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004736
Jeff Layton2a4317c2012-03-21 16:42:43 -04004737 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004738 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004739put_stateid:
4740 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004742 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 return status;
4744}
4745
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004746static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004748 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004749 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004750 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004751 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004752}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004753
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004754static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4755{
4756 switch (to_access) {
4757 case NFS4_SHARE_ACCESS_READ:
4758 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4759 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4760 break;
4761 case NFS4_SHARE_ACCESS_WRITE:
4762 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4763 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4764 break;
4765 case NFS4_SHARE_ACCESS_BOTH:
4766 break;
4767 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004768 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 }
4770}
4771
Al Virob37ad282006-10-19 23:28:59 -07004772__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004773nfsd4_open_downgrade(struct svc_rqst *rqstp,
4774 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004775 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776{
Al Virob37ad282006-10-19 23:28:59 -07004777 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004778 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004779 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780
Al Viroa6a9f182013-09-16 10:57:01 -04004781 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4782 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004784 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004785 if (od->od_deleg_want)
4786 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4787 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004789 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004790 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004791 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004794 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004795 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004797 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004799 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004800 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004802 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004804 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805
Jeff Laytonce0fc432012-05-11 09:45:14 -04004806 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004808 update_stateid(&stp->st_stid.sc_stateid);
4809 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004811put_stateid:
4812 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004814 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 return status;
4816}
4817
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004818static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4819{
Trond Myklebustacf92952014-06-30 11:48:37 -04004820 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytond83017f2014-07-29 21:34:42 -04004821 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004822
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004823 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04004824 spin_lock(&clp->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04004825 unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004826
Jeff Laytond83017f2014-07-29 21:34:42 -04004827 if (clp->cl_minorversion) {
4828 put_ol_stateid_locked(s, &reaplist);
4829 spin_unlock(&clp->cl_lock);
4830 free_ol_stateid_reaplist(&reaplist);
4831 } else {
4832 spin_unlock(&clp->cl_lock);
4833 free_ol_stateid_reaplist(&reaplist);
Jeff Laytond3134b12014-07-29 21:34:32 -04004834 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04004835 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004836}
4837
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838/*
4839 * nfs4_unlock_state() called after encode
4840 */
Al Virob37ad282006-10-19 23:28:59 -07004841__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004842nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004843 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844{
Al Virob37ad282006-10-19 23:28:59 -07004845 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004846 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004847 struct net *net = SVC_NET(rqstp);
4848 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849
Al Viroa6a9f182013-09-16 10:57:01 -04004850 dprintk("NFSD: nfsd4_close on file %pd\n",
4851 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004853 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4854 &close->cl_stateid,
4855 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004856 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004857 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004858 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004860 update_stateid(&stp->st_stid.sc_stateid);
4861 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004863 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004864
4865 /* put reference from nfs4_preprocess_seqid_op */
4866 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 return status;
4869}
4870
Al Virob37ad282006-10-19 23:28:59 -07004871__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004872nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4873 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004875 struct nfs4_delegation *dp;
4876 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004877 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004878 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004879 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004881 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004882 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004884 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004885 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004886 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004887 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004888 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004889 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004890 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004891
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004892 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004893put_stateid:
4894 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895out:
4896 return status;
4897}
4898
4899
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901
Benny Halevy87df4de2008-12-15 19:42:03 +02004902static inline u64
4903end_offset(u64 start, u64 len)
4904{
4905 u64 end;
4906
4907 end = start + len;
4908 return end >= start ? end: NFS4_MAX_UINT64;
4909}
4910
4911/* last octet in a range */
4912static inline u64
4913last_byte_offset(u64 start, u64 len)
4914{
4915 u64 end;
4916
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004917 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004918 end = start + len;
4919 return end > start ? end - 1: NFS4_MAX_UINT64;
4920}
4921
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922/*
4923 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4924 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4925 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4926 * locking, this prevents us from being completely protocol-compliant. The
4927 * real solution to this problem is to start using unsigned file offsets in
4928 * the VFS, but this is a very deep change!
4929 */
4930static inline void
4931nfs4_transform_lock_offset(struct file_lock *lock)
4932{
4933 if (lock->fl_start < 0)
4934 lock->fl_start = OFFSET_MAX;
4935 if (lock->fl_end < 0)
4936 lock->fl_end = OFFSET_MAX;
4937}
4938
Kinglong Meeaef95832014-08-22 10:18:44 -04004939static void nfsd4_fl_get_owner(struct file_lock *dst, struct file_lock *src)
4940{
4941 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)src->fl_owner;
4942 dst->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lo->lo_owner));
4943}
4944
4945static void nfsd4_fl_put_owner(struct file_lock *fl)
4946{
4947 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
4948
4949 if (lo) {
4950 nfs4_put_stateowner(&lo->lo_owner);
4951 fl->fl_owner = NULL;
4952 }
4953}
4954
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004955static const struct lock_manager_operations nfsd_posix_mng_ops = {
Kinglong Meeaef95832014-08-22 10:18:44 -04004956 .lm_get_owner = nfsd4_fl_get_owner,
4957 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07004958};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959
4960static inline void
4961nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4962{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004963 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964
NeilBrownd5b90262006-04-10 22:55:22 -07004965 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004966 lo = (struct nfs4_lockowner *) fl->fl_owner;
4967 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4968 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004969 if (!deny->ld_owner.data)
4970 /* We just don't care that much */
4971 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004972 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4973 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004974 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004975nevermind:
4976 deny->ld_owner.len = 0;
4977 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004978 deny->ld_clientid.cl_boot = 0;
4979 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 }
4981 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004982 deny->ld_length = NFS4_MAX_UINT64;
4983 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4985 deny->ld_type = NFS4_READ_LT;
4986 if (fl->fl_type != F_RDLCK)
4987 deny->ld_type = NFS4_WRITE_LT;
4988}
4989
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004990static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004991find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004992 struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004994 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004995 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996
Trond Myklebust0a880a22014-07-30 08:27:10 -04004997 lockdep_assert_held(&clp->cl_lock);
4998
Trond Myklebustd4f04892014-07-29 21:34:36 -04004999 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5000 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005001 if (so->so_is_open_owner)
5002 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005003 if (same_owner_str(so, owner))
5004 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 }
5006 return NULL;
5007}
5008
Trond Myklebustc58c6612014-07-29 21:34:35 -04005009static struct nfs4_lockowner *
5010find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005011 struct nfs4_client *clp)
Trond Myklebustc58c6612014-07-29 21:34:35 -04005012{
5013 struct nfs4_lockowner *lo;
5014
Trond Myklebustd4f04892014-07-29 21:34:36 -04005015 spin_lock(&clp->cl_lock);
5016 lo = find_lockowner_str_locked(clid, owner, clp);
5017 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005018 return lo;
5019}
5020
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005021static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5022{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005023 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005024}
5025
Jeff Layton6b180f02014-07-29 21:34:26 -04005026static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5027{
5028 struct nfs4_lockowner *lo = lockowner(sop);
5029
5030 kmem_cache_free(lockowner_slab, lo);
5031}
5032
5033static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005034 .so_unhash = nfs4_unhash_lockowner,
5035 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005036};
5037
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038/*
5039 * Alloc a lock owner structure.
5040 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005041 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005043 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005045static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005046alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5047 struct nfs4_ol_stateid *open_stp,
5048 struct nfsd4_lock *lock)
5049{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005050 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005052 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5053 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005055 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5056 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005057 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005058 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005059 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005060 ret = find_lockowner_str_locked(&clp->cl_clientid,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005061 &lock->lk_new_owner, clp);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005062 if (ret == NULL) {
5063 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005064 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005065 ret = lo;
5066 } else
5067 nfs4_free_lockowner(&lo->lo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005068 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005069 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070}
5071
Jeff Layton356a95e2014-07-29 21:34:13 -04005072static void
5073init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5074 struct nfs4_file *fp, struct inode *inode,
5075 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005077 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078
Jeff Layton356a95e2014-07-29 21:34:13 -04005079 lockdep_assert_held(&clp->cl_lock);
5080
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005081 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005082 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005083 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07005084 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005085 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005086 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005087 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005089 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005090 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005091 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005092 spin_lock(&fp->fi_lock);
5093 list_add(&stp->st_perfile, &fp->fi_stateids);
5094 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095}
5096
Jeff Laytonc53530d2014-06-30 11:48:39 -04005097static struct nfs4_ol_stateid *
5098find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5099{
5100 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005101 struct nfs4_client *clp = lo->lo_owner.so_client;
5102
5103 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005104
5105 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005106 if (lst->st_stid.sc_file == fp) {
5107 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005108 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005109 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005110 }
5111 return NULL;
5112}
5113
Jeff Layton356a95e2014-07-29 21:34:13 -04005114static struct nfs4_ol_stateid *
5115find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5116 struct inode *inode, struct nfs4_ol_stateid *ost,
5117 bool *new)
5118{
5119 struct nfs4_stid *ns = NULL;
5120 struct nfs4_ol_stateid *lst;
5121 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5122 struct nfs4_client *clp = oo->oo_owner.so_client;
5123
5124 spin_lock(&clp->cl_lock);
5125 lst = find_lock_stateid(lo, fi);
5126 if (lst == NULL) {
5127 spin_unlock(&clp->cl_lock);
5128 ns = nfs4_alloc_stid(clp, stateid_slab);
5129 if (ns == NULL)
5130 return NULL;
5131
5132 spin_lock(&clp->cl_lock);
5133 lst = find_lock_stateid(lo, fi);
5134 if (likely(!lst)) {
5135 lst = openlockstateid(ns);
5136 init_lock_stateid(lst, lo, fi, inode, ost);
5137 ns = NULL;
5138 *new = true;
5139 }
5140 }
5141 spin_unlock(&clp->cl_lock);
5142 if (ns)
5143 nfs4_put_stid(ns);
5144 return lst;
5145}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005146
NeilBrownfd39ca92005-06-23 22:04:03 -07005147static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148check_lock_length(u64 offset, u64 length)
5149{
Benny Halevy87df4de2008-12-15 19:42:03 +02005150 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 LOFF_OVERFLOW(offset, length)));
5152}
5153
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005154static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005155{
Trond Myklebust11b91642014-07-29 21:34:08 -04005156 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005157
Jeff Layton7214e862014-07-10 14:07:33 -04005158 lockdep_assert_held(&fp->fi_lock);
5159
Jeff Layton82c5ff12012-05-11 09:45:13 -04005160 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005161 return;
Jeff Layton12659652014-07-10 14:07:28 -04005162 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005163 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005164}
5165
Jeff Layton356a95e2014-07-29 21:34:13 -04005166static __be32
5167lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5168 struct nfs4_ol_stateid *ost,
5169 struct nfsd4_lock *lock,
5170 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005171{
Jeff Layton5db1c032014-07-29 21:34:28 -04005172 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005173 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005174 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5175 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005176 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005177 struct nfs4_lockowner *lo;
5178 unsigned int strhashval;
5179
Trond Myklebustd4f04892014-07-29 21:34:36 -04005180 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005181 if (!lo) {
Trond Myklebustd4f04892014-07-29 21:34:36 -04005182 strhashval = ownerstr_hashval(&lock->v.new.owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005183 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5184 if (lo == NULL)
5185 return nfserr_jukebox;
5186 } else {
5187 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005188 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005189 if (!cstate->minorversion &&
5190 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005191 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005192 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005193
Jeff Layton356a95e2014-07-29 21:34:13 -04005194 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005195 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005196 status = nfserr_jukebox;
5197 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005198 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005199 status = nfs_ok;
5200out:
5201 nfs4_put_stateowner(&lo->lo_owner);
5202 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005203}
5204
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205/*
5206 * LOCK operation
5207 */
Al Virob37ad282006-10-19 23:28:59 -07005208__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005209nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005210 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005212 struct nfs4_openowner *open_sop = NULL;
5213 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005214 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005215 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005216 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005217 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005218 struct file_lock *file_lock = NULL;
5219 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005220 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005221 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005222 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005223 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005224 struct net *net = SVC_NET(rqstp);
5225 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226
5227 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5228 (long long) lock->lk_offset,
5229 (long long) lock->lk_length);
5230
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 if (check_lock_length(lock->lk_offset, lock->lk_length))
5232 return nfserr_inval;
5233
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005234 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005235 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005236 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5237 return status;
5238 }
5239
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005241 if (nfsd4_has_session(cstate))
5242 /* See rfc 5661 18.10.3: given clientid is ignored: */
5243 memcpy(&lock->v.new.clientid,
5244 &cstate->session->se_client->cl_clientid,
5245 sizeof(clientid_t));
5246
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005248 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005252 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 lock->lk_new_open_seqid,
5254 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005255 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005256 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005258 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005259 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005260 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005261 &lock->v.new.clientid))
5262 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005263 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005264 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005265 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005266 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005267 lock->lk_old_lock_seqid,
5268 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005269 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005270 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005271 if (status)
5272 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005273 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005275 lkflg = setlkflg(lock->lk_type);
5276 status = nfs4_check_openmode(lock_stp, lkflg);
5277 if (status)
5278 goto out;
5279
NeilBrown0dd395d2005-07-07 17:59:15 -07005280 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005281 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005282 goto out;
5283 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005284 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005285 goto out;
5286
Jeff Layton21179d82012-08-21 08:03:32 -04005287 file_lock = locks_alloc_lock();
5288 if (!file_lock) {
5289 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5290 status = nfserr_jukebox;
5291 goto out;
5292 }
5293
Trond Myklebust11b91642014-07-29 21:34:08 -04005294 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 switch (lock->lk_type) {
5296 case NFS4_READ_LT:
5297 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005298 spin_lock(&fp->fi_lock);
5299 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005300 if (filp)
5301 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005302 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005303 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005304 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 case NFS4_WRITE_LT:
5306 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005307 spin_lock(&fp->fi_lock);
5308 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005309 if (filp)
5310 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005311 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005312 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005313 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 default:
5315 status = nfserr_inval;
5316 goto out;
5317 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005318 if (!filp) {
5319 status = nfserr_openmode;
5320 goto out;
5321 }
Kinglong Meeaef95832014-08-22 10:18:44 -04005322
5323 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04005324 file_lock->fl_pid = current->tgid;
5325 file_lock->fl_file = filp;
5326 file_lock->fl_flags = FL_POSIX;
5327 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5328 file_lock->fl_start = lock->lk_offset;
5329 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5330 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331
Jeff Layton21179d82012-08-21 08:03:32 -04005332 conflock = locks_alloc_lock();
5333 if (!conflock) {
5334 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5335 status = nfserr_jukebox;
5336 goto out;
5337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338
Jeff Layton21179d82012-08-21 08:03:32 -04005339 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005340 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005342 update_stateid(&lock_stp->st_stid.sc_stateid);
5343 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005345 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005346 break;
5347 case (EAGAIN): /* conflock holds conflicting lock */
5348 status = nfserr_denied;
5349 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005350 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005351 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 case (EDEADLK):
5353 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005354 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005355 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005356 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005357 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005358 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360out:
Trond Myklebustde186432014-07-10 14:07:26 -04005361 if (filp)
5362 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005363 if (lock_stp) {
5364 /* Bump seqid manually if the 4.0 replay owner is openowner */
5365 if (cstate->replay_owner &&
5366 cstate->replay_owner != &lock_sop->lo_owner &&
5367 seqid_mutating_err(ntohl(status)))
5368 lock_sop->lo_owner.so_seqid++;
5369
5370 /*
5371 * If this is a new, never-before-used stateid, and we are
5372 * returning an error, then just go ahead and release it.
5373 */
5374 if (status && new)
5375 release_lock_stateid(lock_stp);
5376
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005377 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005378 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005379 if (open_stp)
5380 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005381 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005382 if (file_lock)
5383 locks_free_lock(file_lock);
5384 if (conflock)
5385 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 return status;
5387}
5388
5389/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005390 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5391 * so we do a temporary open here just to get an open file to pass to
5392 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5393 * inode operation.)
5394 */
Al Viro04da6e92012-04-13 00:00:04 -04005395static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005396{
5397 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005398 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5399 if (!err) {
5400 err = nfserrno(vfs_test_lock(file, lock));
5401 nfsd_close(file);
5402 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005403 return err;
5404}
5405
5406/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 * LOCKT operation
5408 */
Al Virob37ad282006-10-19 23:28:59 -07005409__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005410nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5411 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412{
Jeff Layton21179d82012-08-21 08:03:32 -04005413 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005414 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005415 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005416 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005418 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 return nfserr_grace;
5420
5421 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5422 return nfserr_inval;
5423
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005424 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005425 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005426 if (status)
5427 goto out;
5428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005430 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432
Jeff Layton21179d82012-08-21 08:03:32 -04005433 file_lock = locks_alloc_lock();
5434 if (!file_lock) {
5435 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5436 status = nfserr_jukebox;
5437 goto out;
5438 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005439
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 switch (lockt->lt_type) {
5441 case NFS4_READ_LT:
5442 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005443 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 break;
5445 case NFS4_WRITE_LT:
5446 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005447 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 break;
5449 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005450 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 status = nfserr_inval;
5452 goto out;
5453 }
5454
Trond Myklebustd4f04892014-07-29 21:34:36 -04005455 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5456 cstate->clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005457 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005458 file_lock->fl_owner = (fl_owner_t)lo;
5459 file_lock->fl_pid = current->tgid;
5460 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461
Jeff Layton21179d82012-08-21 08:03:32 -04005462 file_lock->fl_start = lockt->lt_offset;
5463 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464
Jeff Layton21179d82012-08-21 08:03:32 -04005465 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466
Jeff Layton21179d82012-08-21 08:03:32 -04005467 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005468 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005469 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005470
Jeff Layton21179d82012-08-21 08:03:32 -04005471 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005473 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 }
5475out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005476 if (lo)
5477 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04005478 if (file_lock)
5479 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 return status;
5481}
5482
Al Virob37ad282006-10-19 23:28:59 -07005483__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005484nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005485 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005487 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005489 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005490 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005491 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005492 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5493
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5495 (long long) locku->lu_offset,
5496 (long long) locku->lu_length);
5497
5498 if (check_lock_length(locku->lu_offset, locku->lu_length))
5499 return nfserr_inval;
5500
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005501 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005502 &locku->lu_stateid, NFS4_LOCK_STID,
5503 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005504 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005506 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005507 if (!filp) {
5508 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005509 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005510 }
Jeff Layton21179d82012-08-21 08:03:32 -04005511 file_lock = locks_alloc_lock();
5512 if (!file_lock) {
5513 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5514 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005515 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005516 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005517
Jeff Layton21179d82012-08-21 08:03:32 -04005518 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04005519 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04005520 file_lock->fl_pid = current->tgid;
5521 file_lock->fl_file = filp;
5522 file_lock->fl_flags = FL_POSIX;
5523 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5524 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525
Jeff Layton21179d82012-08-21 08:03:32 -04005526 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5527 locku->lu_length);
5528 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
Jeff Layton21179d82012-08-21 08:03:32 -04005530 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005531 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005532 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 goto out_nfserr;
5534 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005535 update_stateid(&stp->st_stid.sc_stateid);
5536 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005537fput:
5538 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005539put_stateid:
5540 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005542 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005543 if (file_lock)
5544 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 return status;
5546
5547out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005548 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005549 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550}
5551
5552/*
5553 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005554 * true: locks held by lockowner
5555 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005557static bool
5558check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005560 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005561 int status = false;
5562 struct file *filp = find_any_file(fp);
5563 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005564 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005565
5566 if (!filp) {
5567 /* Any valid lock stateid should have some sort of access */
5568 WARN_ON_ONCE(1);
5569 return status;
5570 }
5571
5572 inode = file_inode(filp);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005573 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005575 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05005576 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005577 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
5578 if (fl->fl_owner == (fl_owner_t)lowner) {
5579 status = true;
5580 break;
5581 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005582 }
Jeff Layton6109c852015-01-16 15:05:57 -05005583 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 }
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005585 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 return status;
5587}
5588
Al Virob37ad282006-10-19 23:28:59 -07005589__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005590nfsd4_release_lockowner(struct svc_rqst *rqstp,
5591 struct nfsd4_compound_state *cstate,
5592 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593{
5594 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005595 struct nfs4_stateowner *sop;
5596 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005597 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005599 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005600 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005601 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005602 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603
5604 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5605 clid->cl_boot, clid->cl_id);
5606
Jeff Layton4b24ca72014-06-30 11:48:44 -04005607 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005608 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04005609 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005610
Trond Myklebustd4f04892014-07-29 21:34:36 -04005611 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005612 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005613 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005614 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005615 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005616
5617 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005618 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005619
Jeff Layton882e9d22014-07-29 21:34:37 -04005620 /* see if there are still any locks associated with it */
5621 lo = lockowner(sop);
5622 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5623 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5624 status = nfserr_locks_held;
5625 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04005626 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04005627 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005628 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005629
Kinglong Meeb5971af2014-08-22 10:18:43 -04005630 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04005631 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005632 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005633 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005634 if (lo)
5635 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 return status;
5637}
5638
5639static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005640alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641{
NeilBrowna55370a2005-06-23 22:03:52 -07005642 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643}
5644
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005645bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005646nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005647{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005648 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005649
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005650 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005651 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005652}
5653
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654/*
5655 * failure => all reset bets are off, nfserr_no_grace...
5656 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005657struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005658nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659{
5660 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005661 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662
NeilBrowna55370a2005-06-23 22:03:52 -07005663 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5664 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005665 if (crp) {
5666 strhashval = clientstr_hashval(name);
5667 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005668 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005669 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005670 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005671 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005672 }
5673 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674}
5675
Jeff Layton2a4317c2012-03-21 16:42:43 -04005676void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005677nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005678{
5679 list_del(&crp->cr_strhash);
5680 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005681 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005682}
5683
5684void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005685nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686{
5687 struct nfs4_client_reclaim *crp = NULL;
5688 int i;
5689
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005691 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5692 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005694 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 }
5696 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005697 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698}
5699
5700/*
5701 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005702struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005703nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704{
5705 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 struct nfs4_client_reclaim *crp = NULL;
5707
Jeff Layton278c9312012-11-12 15:00:52 -05005708 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709
Jeff Layton278c9312012-11-12 15:00:52 -05005710 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005711 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005712 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 return crp;
5714 }
5715 }
5716 return NULL;
5717}
5718
5719/*
5720* Called from OPEN. Look for clientid in reclaim list.
5721*/
Al Virob37ad282006-10-19 23:28:59 -07005722__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005723nfs4_check_open_reclaim(clientid_t *clid,
5724 struct nfsd4_compound_state *cstate,
5725 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005727 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005728
5729 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005730 status = lookup_clientid(clid, cstate, nn);
5731 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005732 return nfserr_reclaim_bad;
5733
Jeff Layton3b3e7b72014-09-12 16:40:22 -04005734 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
5735 return nfserr_no_grace;
5736
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005737 if (nfsd4_client_record_check(cstate->clp))
5738 return nfserr_reclaim_bad;
5739
5740 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741}
5742
Bryan Schumaker65178db2011-11-01 13:35:21 -04005743#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04005744static inline void
5745put_client(struct nfs4_client *clp)
5746{
5747 atomic_dec(&clp->cl_refcount);
5748}
5749
Jeff Layton285abde2014-07-30 08:27:24 -04005750static struct nfs4_client *
5751nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5752{
5753 struct nfs4_client *clp;
5754 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5755 nfsd_net_id);
5756
5757 if (!nfsd_netns_ready(nn))
5758 return NULL;
5759
5760 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5761 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5762 return clp;
5763 }
5764 return NULL;
5765}
5766
Jeff Layton7ec0e362014-07-30 08:27:17 -04005767u64
Jeff Layton285abde2014-07-30 08:27:24 -04005768nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04005769{
5770 struct nfs4_client *clp;
5771 u64 count = 0;
5772 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5773 nfsd_net_id);
5774 char buf[INET6_ADDRSTRLEN];
5775
5776 if (!nfsd_netns_ready(nn))
5777 return 0;
5778
5779 spin_lock(&nn->client_lock);
5780 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5781 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5782 pr_info("NFS Client: %s\n", buf);
5783 ++count;
5784 }
5785 spin_unlock(&nn->client_lock);
5786
5787 return count;
5788}
Bryan Schumaker65178db2011-11-01 13:35:21 -04005789
Jeff Laytona0926d12014-07-30 08:27:18 -04005790u64
Jeff Layton285abde2014-07-30 08:27:24 -04005791nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04005792{
5793 u64 count = 0;
5794 struct nfs4_client *clp;
5795 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5796 nfsd_net_id);
5797
5798 if (!nfsd_netns_ready(nn))
5799 return count;
5800
5801 spin_lock(&nn->client_lock);
5802 clp = nfsd_find_client(addr, addr_size);
5803 if (clp) {
5804 if (mark_client_expired_locked(clp) == nfs_ok)
5805 ++count;
5806 else
5807 clp = NULL;
5808 }
5809 spin_unlock(&nn->client_lock);
5810
5811 if (clp)
5812 expire_client(clp);
5813
5814 return count;
5815}
5816
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005817u64
Jeff Layton285abde2014-07-30 08:27:24 -04005818nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005819{
5820 u64 count = 0;
5821 struct nfs4_client *clp, *next;
5822 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5823 nfsd_net_id);
5824 LIST_HEAD(reaplist);
5825
5826 if (!nfsd_netns_ready(nn))
5827 return count;
5828
5829 spin_lock(&nn->client_lock);
5830 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5831 if (mark_client_expired_locked(clp) == nfs_ok) {
5832 list_add(&clp->cl_lru, &reaplist);
5833 if (max != 0 && ++count >= max)
5834 break;
5835 }
5836 }
5837 spin_unlock(&nn->client_lock);
5838
5839 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
5840 expire_client(clp);
5841
5842 return count;
5843}
5844
Bryan Schumaker184c1842012-11-29 11:40:44 -05005845static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5846 const char *type)
5847{
5848 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005849 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005850 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5851}
5852
Jeff Layton016200c2014-07-30 08:27:21 -04005853static void
5854nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
5855 struct list_head *collect)
5856{
5857 struct nfs4_client *clp = lst->st_stid.sc_client;
5858 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5859 nfsd_net_id);
5860
5861 if (!collect)
5862 return;
5863
5864 lockdep_assert_held(&nn->client_lock);
5865 atomic_inc(&clp->cl_refcount);
5866 list_add(&lst->st_locks, collect);
5867}
5868
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005869static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04005870 struct list_head *collect,
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005871 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005872{
5873 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005874 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005875 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005876 u64 count = 0;
5877
Jeff Layton016200c2014-07-30 08:27:21 -04005878 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005879 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005880 list_for_each_entry_safe(stp, st_next,
5881 &oop->oo_owner.so_stateids, st_perstateowner) {
5882 list_for_each_entry_safe(lst, lst_next,
5883 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04005884 if (func) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005885 func(lst);
Jeff Layton016200c2014-07-30 08:27:21 -04005886 nfsd_inject_add_lock_to_list(lst,
5887 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04005888 }
Jeff Layton016200c2014-07-30 08:27:21 -04005889 ++count;
5890 /*
5891 * Despite the fact that these functions deal
5892 * with 64-bit integers for "count", we must
5893 * ensure that it doesn't blow up the
5894 * clp->cl_refcount. Throw a warning if we
5895 * start to approach INT_MAX here.
5896 */
5897 WARN_ON_ONCE(count == (INT_MAX / 2));
5898 if (count == max)
5899 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005900 }
5901 }
5902 }
Jeff Layton016200c2014-07-30 08:27:21 -04005903out:
5904 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005905
5906 return count;
5907}
5908
Jeff Layton016200c2014-07-30 08:27:21 -04005909static u64
5910nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
5911 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05005912{
Jeff Layton016200c2014-07-30 08:27:21 -04005913 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005914}
5915
Jeff Layton016200c2014-07-30 08:27:21 -04005916static u64
5917nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05005918{
Jeff Layton016200c2014-07-30 08:27:21 -04005919 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005920 nfsd_print_count(clp, count, "locked files");
5921 return count;
5922}
5923
Jeff Layton016200c2014-07-30 08:27:21 -04005924u64
Jeff Layton285abde2014-07-30 08:27:24 -04005925nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04005926{
5927 struct nfs4_client *clp;
5928 u64 count = 0;
5929 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5930 nfsd_net_id);
5931
5932 if (!nfsd_netns_ready(nn))
5933 return 0;
5934
5935 spin_lock(&nn->client_lock);
5936 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5937 count += nfsd_print_client_locks(clp);
5938 spin_unlock(&nn->client_lock);
5939
5940 return count;
5941}
5942
5943static void
5944nfsd_reap_locks(struct list_head *reaplist)
5945{
5946 struct nfs4_client *clp;
5947 struct nfs4_ol_stateid *stp, *next;
5948
5949 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
5950 list_del_init(&stp->st_locks);
5951 clp = stp->st_stid.sc_client;
5952 nfs4_put_stid(&stp->st_stid);
5953 put_client(clp);
5954 }
5955}
5956
5957u64
Jeff Layton285abde2014-07-30 08:27:24 -04005958nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04005959{
5960 unsigned int count = 0;
5961 struct nfs4_client *clp;
5962 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5963 nfsd_net_id);
5964 LIST_HEAD(reaplist);
5965
5966 if (!nfsd_netns_ready(nn))
5967 return count;
5968
5969 spin_lock(&nn->client_lock);
5970 clp = nfsd_find_client(addr, addr_size);
5971 if (clp)
5972 count = nfsd_collect_client_locks(clp, &reaplist, 0);
5973 spin_unlock(&nn->client_lock);
5974 nfsd_reap_locks(&reaplist);
5975 return count;
5976}
5977
5978u64
Jeff Layton285abde2014-07-30 08:27:24 -04005979nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04005980{
5981 u64 count = 0;
5982 struct nfs4_client *clp;
5983 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5984 nfsd_net_id);
5985 LIST_HEAD(reaplist);
5986
5987 if (!nfsd_netns_ready(nn))
5988 return count;
5989
5990 spin_lock(&nn->client_lock);
5991 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5992 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
5993 if (max != 0 && count >= max)
5994 break;
5995 }
5996 spin_unlock(&nn->client_lock);
5997 nfsd_reap_locks(&reaplist);
5998 return count;
5999}
6000
Jeff Layton82e05ef2014-07-30 08:27:22 -04006001static u64
6002nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6003 struct list_head *collect,
6004 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006005{
6006 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04006007 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6008 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006009 u64 count = 0;
6010
Jeff Layton82e05ef2014-07-30 08:27:22 -04006011 lockdep_assert_held(&nn->client_lock);
6012
6013 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006014 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04006015 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006016 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04006017 if (collect) {
6018 atomic_inc(&clp->cl_refcount);
6019 list_add(&oop->oo_perclient, collect);
6020 }
6021 }
6022 ++count;
6023 /*
6024 * Despite the fact that these functions deal with
6025 * 64-bit integers for "count", we must ensure that
6026 * it doesn't blow up the clp->cl_refcount. Throw a
6027 * warning if we start to approach INT_MAX here.
6028 */
6029 WARN_ON_ONCE(count == (INT_MAX / 2));
6030 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006031 break;
6032 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04006033 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006034
6035 return count;
6036}
6037
Jeff Layton82e05ef2014-07-30 08:27:22 -04006038static u64
6039nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006040{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006041 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6042
6043 nfsd_print_count(clp, count, "openowners");
6044 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006045}
6046
Jeff Layton82e05ef2014-07-30 08:27:22 -04006047static u64
6048nfsd_collect_client_openowners(struct nfs4_client *clp,
6049 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006050{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006051 return nfsd_foreach_client_openowner(clp, max, collect,
6052 unhash_openowner_locked);
6053}
6054
6055u64
Jeff Layton285abde2014-07-30 08:27:24 -04006056nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006057{
6058 struct nfs4_client *clp;
6059 u64 count = 0;
6060 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6061 nfsd_net_id);
6062
6063 if (!nfsd_netns_ready(nn))
6064 return 0;
6065
6066 spin_lock(&nn->client_lock);
6067 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6068 count += nfsd_print_client_openowners(clp);
6069 spin_unlock(&nn->client_lock);
6070
6071 return count;
6072}
6073
6074static void
6075nfsd_reap_openowners(struct list_head *reaplist)
6076{
6077 struct nfs4_client *clp;
6078 struct nfs4_openowner *oop, *next;
6079
6080 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6081 list_del_init(&oop->oo_perclient);
6082 clp = oop->oo_owner.so_client;
6083 release_openowner(oop);
6084 put_client(clp);
6085 }
6086}
6087
6088u64
Jeff Layton285abde2014-07-30 08:27:24 -04006089nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6090 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006091{
6092 unsigned int count = 0;
6093 struct nfs4_client *clp;
6094 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6095 nfsd_net_id);
6096 LIST_HEAD(reaplist);
6097
6098 if (!nfsd_netns_ready(nn))
6099 return count;
6100
6101 spin_lock(&nn->client_lock);
6102 clp = nfsd_find_client(addr, addr_size);
6103 if (clp)
6104 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6105 spin_unlock(&nn->client_lock);
6106 nfsd_reap_openowners(&reaplist);
6107 return count;
6108}
6109
6110u64
Jeff Layton285abde2014-07-30 08:27:24 -04006111nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006112{
6113 u64 count = 0;
6114 struct nfs4_client *clp;
6115 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6116 nfsd_net_id);
6117 LIST_HEAD(reaplist);
6118
6119 if (!nfsd_netns_ready(nn))
6120 return count;
6121
6122 spin_lock(&nn->client_lock);
6123 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6124 count += nfsd_collect_client_openowners(clp, &reaplist,
6125 max - count);
6126 if (max != 0 && count >= max)
6127 break;
6128 }
6129 spin_unlock(&nn->client_lock);
6130 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006131 return count;
6132}
6133
Bryan Schumaker269de302012-11-29 11:40:42 -05006134static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6135 struct list_head *victims)
6136{
6137 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006138 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6139 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006140 u64 count = 0;
6141
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006142 lockdep_assert_held(&nn->client_lock);
6143
6144 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006145 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006146 if (victims) {
6147 /*
6148 * It's not safe to mess with delegations that have a
6149 * non-zero dl_time. They might have already been broken
6150 * and could be processed by the laundromat outside of
6151 * the state_lock. Just leave them be.
6152 */
6153 if (dp->dl_time != 0)
6154 continue;
6155
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006156 atomic_inc(&clp->cl_refcount);
Jeff Layton42690672014-07-25 07:34:20 -04006157 unhash_delegation_locked(dp);
6158 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006159 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006160 ++count;
6161 /*
6162 * Despite the fact that these functions deal with
6163 * 64-bit integers for "count", we must ensure that
6164 * it doesn't blow up the clp->cl_refcount. Throw a
6165 * warning if we start to approach INT_MAX here.
6166 */
6167 WARN_ON_ONCE(count == (INT_MAX / 2));
6168 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006169 break;
6170 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006171 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006172 return count;
6173}
6174
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006175static u64
6176nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006177{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006178 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006179
6180 nfsd_print_count(clp, count, "delegations");
6181 return count;
6182}
6183
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006184u64
Jeff Layton285abde2014-07-30 08:27:24 -04006185nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006186{
6187 struct nfs4_client *clp;
6188 u64 count = 0;
6189 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6190 nfsd_net_id);
6191
6192 if (!nfsd_netns_ready(nn))
6193 return 0;
6194
6195 spin_lock(&nn->client_lock);
6196 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6197 count += nfsd_print_client_delegations(clp);
6198 spin_unlock(&nn->client_lock);
6199
6200 return count;
6201}
6202
6203static void
6204nfsd_forget_delegations(struct list_head *reaplist)
6205{
6206 struct nfs4_client *clp;
6207 struct nfs4_delegation *dp, *next;
6208
6209 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6210 list_del_init(&dp->dl_recall_lru);
6211 clp = dp->dl_stid.sc_client;
6212 revoke_delegation(dp);
6213 put_client(clp);
6214 }
6215}
6216
6217u64
Jeff Layton285abde2014-07-30 08:27:24 -04006218nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6219 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006220{
6221 u64 count = 0;
6222 struct nfs4_client *clp;
6223 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6224 nfsd_net_id);
6225 LIST_HEAD(reaplist);
6226
6227 if (!nfsd_netns_ready(nn))
6228 return count;
6229
6230 spin_lock(&nn->client_lock);
6231 clp = nfsd_find_client(addr, addr_size);
6232 if (clp)
6233 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6234 spin_unlock(&nn->client_lock);
6235
6236 nfsd_forget_delegations(&reaplist);
6237 return count;
6238}
6239
6240u64
Jeff Layton285abde2014-07-30 08:27:24 -04006241nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006242{
6243 u64 count = 0;
6244 struct nfs4_client *clp;
6245 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6246 nfsd_net_id);
6247 LIST_HEAD(reaplist);
6248
6249 if (!nfsd_netns_ready(nn))
6250 return count;
6251
6252 spin_lock(&nn->client_lock);
6253 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6254 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6255 if (max != 0 && count >= max)
6256 break;
6257 }
6258 spin_unlock(&nn->client_lock);
6259 nfsd_forget_delegations(&reaplist);
6260 return count;
6261}
6262
6263static void
6264nfsd_recall_delegations(struct list_head *reaplist)
6265{
6266 struct nfs4_client *clp;
6267 struct nfs4_delegation *dp, *next;
6268
6269 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6270 list_del_init(&dp->dl_recall_lru);
6271 clp = dp->dl_stid.sc_client;
6272 /*
6273 * We skipped all entries that had a zero dl_time before,
6274 * so we can now reset the dl_time back to 0. If a delegation
6275 * break comes in now, then it won't make any difference since
6276 * we're recalling it either way.
6277 */
6278 spin_lock(&state_lock);
6279 dp->dl_time = 0;
6280 spin_unlock(&state_lock);
6281 nfsd_break_one_deleg(dp);
6282 put_client(clp);
6283 }
6284}
6285
6286u64
Jeff Layton285abde2014-07-30 08:27:24 -04006287nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006288 size_t addr_size)
6289{
6290 u64 count = 0;
6291 struct nfs4_client *clp;
6292 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6293 nfsd_net_id);
6294 LIST_HEAD(reaplist);
6295
6296 if (!nfsd_netns_ready(nn))
6297 return count;
6298
6299 spin_lock(&nn->client_lock);
6300 clp = nfsd_find_client(addr, addr_size);
6301 if (clp)
6302 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6303 spin_unlock(&nn->client_lock);
6304
6305 nfsd_recall_delegations(&reaplist);
6306 return count;
6307}
6308
6309u64
Jeff Layton285abde2014-07-30 08:27:24 -04006310nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006311{
6312 u64 count = 0;
6313 struct nfs4_client *clp, *next;
6314 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6315 nfsd_net_id);
6316 LIST_HEAD(reaplist);
6317
6318 if (!nfsd_netns_ready(nn))
6319 return count;
6320
6321 spin_lock(&nn->client_lock);
6322 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6323 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6324 if (max != 0 && ++count >= max)
6325 break;
6326 }
6327 spin_unlock(&nn->client_lock);
6328 nfsd_recall_delegations(&reaplist);
6329 return count;
6330}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006331#endif /* CONFIG_NFSD_FAULT_INJECTION */
6332
Meelap Shahc2f1a552007-07-17 04:04:39 -07006333/*
6334 * Since the lifetime of a delegation isn't limited to that of an open, a
6335 * client may quite reasonably hang on to a delegation as long as it has
6336 * the inode cached. This becomes an obvious problem the first time a
6337 * client's inode cache approaches the size of the server's total memory.
6338 *
6339 * For now we avoid this problem by imposing a hard limit on the number
6340 * of delegations, which varies according to the server's memory size.
6341 */
6342static void
6343set_max_delegations(void)
6344{
6345 /*
6346 * Allow at most 4 delegations per megabyte of RAM. Quick
6347 * estimates suggest that in the worst case (where every delegation
6348 * is for a different inode), a delegation could take about 1.5K,
6349 * giving a worst case usage of about 6% of memory.
6350 */
6351 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6352}
6353
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006354static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006355{
6356 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6357 int i;
6358
6359 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6360 CLIENT_HASH_SIZE, GFP_KERNEL);
6361 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006362 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006363 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6364 CLIENT_HASH_SIZE, GFP_KERNEL);
6365 if (!nn->unconf_id_hashtbl)
6366 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006367 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6368 SESSION_HASH_SIZE, GFP_KERNEL);
6369 if (!nn->sessionid_hashtbl)
6370 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006371
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006372 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006373 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006374 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006375 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006376 for (i = 0; i < SESSION_HASH_SIZE; i++)
6377 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006378 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006379 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03006380 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03006381 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006382 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03006383 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006384
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006385 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006386 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006387
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006388 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006389
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006390err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03006391 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006392err_unconf_id:
6393 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006394err:
6395 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006396}
6397
6398static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006399nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006400{
6401 int i;
6402 struct nfs4_client *clp = NULL;
6403 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6404
6405 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6406 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6407 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6408 destroy_client(clp);
6409 }
6410 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006411
Kinglong Mee2b905632014-03-26 22:09:30 +08006412 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6413 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6414 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6415 destroy_client(clp);
6416 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006417 }
6418
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006419 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006420 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006421 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006422 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006423}
6424
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006425int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006426nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006427{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006428 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006429 int ret;
6430
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006431 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006432 if (ret)
6433 return ret;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006434 nn->boot_time = get_seconds();
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006435 nn->grace_ended = false;
Jeff Laytond4318ac2014-09-12 16:40:21 -04006436 locks_start_grace(net, &nn->nfsd4_manager);
6437 nfsd4_client_tracking_init(net);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006438 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006439 nn->nfsd4_grace, net);
6440 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006441 return 0;
6442}
6443
6444/* initialization to perform when the nfsd service is started: */
6445
6446int
6447nfs4_state_start(void)
6448{
6449 int ret;
6450
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006451 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006452 if (ret)
6453 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07006454 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05006455 if (laundry_wq == NULL) {
6456 ret = -ENOMEM;
6457 goto out_recovery;
6458 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006459 ret = nfsd4_create_callback_queue();
6460 if (ret)
6461 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006462
Meelap Shahc2f1a552007-07-17 04:04:39 -07006463 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006464
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006465 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006466
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006467out_free_laundry:
6468 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006469out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006470 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471}
6472
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006473void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006474nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006478 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006480 cancel_delayed_work_sync(&nn->laundromat_work);
6481 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006482
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04006484 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006485 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006487 unhash_delegation_locked(dp);
6488 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489 }
Benny Halevycdc97502014-05-30 09:09:30 -04006490 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491 list_for_each_safe(pos, next, &reaplist) {
6492 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006493 list_del_init(&dp->dl_recall_lru);
Jeff Laytonafbda402014-08-09 10:22:41 -04006494 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04006495 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496 }
6497
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006498 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006499 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500}
6501
6502void
6503nfs4_state_shutdown(void)
6504{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006505 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006506 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006508
6509static void
6510get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6511{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006512 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6513 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006514}
6515
6516static void
6517put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6518{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006519 if (cstate->minorversion) {
6520 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6521 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6522 }
6523}
6524
6525void
6526clear_current_stateid(struct nfsd4_compound_state *cstate)
6527{
6528 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006529}
6530
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006531/*
6532 * functions to set current state id
6533 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006534void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006535nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6536{
6537 put_stateid(cstate, &odp->od_stateid);
6538}
6539
6540void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006541nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6542{
6543 put_stateid(cstate, &open->op_stateid);
6544}
6545
6546void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006547nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6548{
6549 put_stateid(cstate, &close->cl_stateid);
6550}
6551
6552void
6553nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6554{
6555 put_stateid(cstate, &lock->lk_resp_stateid);
6556}
6557
6558/*
6559 * functions to consume current state id
6560 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006561
6562void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006563nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6564{
6565 get_stateid(cstate, &odp->od_stateid);
6566}
6567
6568void
6569nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6570{
6571 get_stateid(cstate, &drp->dr_stateid);
6572}
6573
6574void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006575nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6576{
6577 get_stateid(cstate, &fsp->fr_stateid);
6578}
6579
6580void
6581nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6582{
6583 get_stateid(cstate, &setattr->sa_stateid);
6584}
6585
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006586void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006587nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6588{
6589 get_stateid(cstate, &close->cl_stateid);
6590}
6591
6592void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006593nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006594{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006595 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006596}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006597
6598void
6599nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6600{
6601 get_stateid(cstate, &read->rd_stateid);
6602}
6603
6604void
6605nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6606{
6607 get_stateid(cstate, &write->wr_stateid);
6608}