blob: 80242f5bd621d1807dfac7c68cdef70482916e7d [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
3480nfsd_change_deleg_cb(struct file_lock **onlist, int arg, struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481{
3482 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04003483 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 else
3485 return -EAGAIN;
3486}
3487
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003488static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003489 .lm_break = nfsd_break_deleg_cb,
3490 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491};
3492
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003493static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3494{
3495 if (nfsd4_has_session(cstate))
3496 return nfs_ok;
3497 if (seqid == so->so_seqid - 1)
3498 return nfserr_replay_me;
3499 if (seqid == so->so_seqid)
3500 return nfs_ok;
3501 return nfserr_bad_seqid;
3502}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
Jeff Layton4b24ca72014-06-30 11:48:44 -04003504static __be32 lookup_clientid(clientid_t *clid,
3505 struct nfsd4_compound_state *cstate,
3506 struct nfsd_net *nn)
3507{
3508 struct nfs4_client *found;
3509
3510 if (cstate->clp) {
3511 found = cstate->clp;
3512 if (!same_clid(&found->cl_clientid, clid))
3513 return nfserr_stale_clientid;
3514 return nfs_ok;
3515 }
3516
3517 if (STALE_CLIENTID(clid, nn))
3518 return nfserr_stale_clientid;
3519
3520 /*
3521 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3522 * cached already then we know this is for is for v4.0 and "sessions"
3523 * will be false.
3524 */
3525 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003526 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003527 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003528 if (!found) {
3529 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003530 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003531 }
3532 atomic_inc(&found->cl_refcount);
3533 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003534
3535 /* Cache the nfs4_client in cstate! */
3536 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003537 return nfs_ok;
3538}
3539
Al Virob37ad282006-10-19 23:28:59 -07003540__be32
Andy Adamson66689582009-04-03 08:28:45 +03003541nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003542 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 clientid_t *clientid = &open->op_clientid;
3545 struct nfs4_client *clp = NULL;
3546 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003547 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003548 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003550 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003552 /*
3553 * In case we need it later, after we've already created the
3554 * file and don't want to risk a further failure:
3555 */
3556 open->op_file = nfsd4_alloc_file();
3557 if (open->op_file == NULL)
3558 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
Trond Myklebust2d91e892014-06-30 11:48:46 -04003560 status = lookup_clientid(clientid, cstate, nn);
3561 if (status)
3562 return status;
3563 clp = cstate->clp;
3564
Trond Myklebustd4f04892014-07-29 21:34:36 -04003565 strhashval = ownerstr_hashval(&open->op_owner);
3566 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003567 open->op_openowner = oo;
3568 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003569 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003571 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003572 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003573 release_openowner(oo);
3574 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003575 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003576 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003577 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3578 if (status)
3579 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003580 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003581new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003582 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003583 if (oo == NULL)
3584 return nfserr_jukebox;
3585 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003586alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003587 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003588 if (!open->op_stp)
3589 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003590 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591}
3592
Al Virob37ad282006-10-19 23:28:59 -07003593static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003594nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3595{
3596 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3597 return nfserr_openmode;
3598 else
3599 return nfs_ok;
3600}
3601
Daniel Mackc47d8322011-05-16 16:38:14 +02003602static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003603{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003604 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3605}
3606
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003607static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003608{
3609 struct nfs4_stid *ret;
3610
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003611 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003612 if (!ret)
3613 return NULL;
3614 return delegstateid(ret);
3615}
3616
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003617static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3618{
3619 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3620 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3621}
3622
Al Virob37ad282006-10-19 23:28:59 -07003623static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003624nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003625 struct nfs4_delegation **dp)
3626{
3627 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003628 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003629 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003630
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003631 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3632 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003633 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003634 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003635 status = nfs4_check_delegmode(deleg, flags);
3636 if (status) {
3637 nfs4_put_stid(&deleg->dl_stid);
3638 goto out;
3639 }
3640 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003641out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003642 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003643 return nfs_ok;
3644 if (status)
3645 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003646 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003647 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003648}
3649
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003650static struct nfs4_ol_stateid *
3651nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003653 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003654 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655
Trond Myklebust1d31a252014-07-10 14:07:25 -04003656 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003657 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 /* ignore lock owners */
3659 if (local->st_stateowner->so_is_open_owner == 0)
3660 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003661 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003662 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003663 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003664 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003667 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003668 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669}
3670
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003671static inline int nfs4_access_to_access(u32 nfs4_access)
3672{
3673 int flags = 0;
3674
3675 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3676 flags |= NFSD_MAY_READ;
3677 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3678 flags |= NFSD_MAY_WRITE;
3679 return flags;
3680}
3681
Al Virob37ad282006-10-19 23:28:59 -07003682static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3684 struct nfsd4_open *open)
3685{
3686 struct iattr iattr = {
3687 .ia_valid = ATTR_SIZE,
3688 .ia_size = 0,
3689 };
3690 if (!open->op_truncate)
3691 return 0;
3692 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003693 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3695}
3696
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003697static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003698 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3699 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003700{
Trond Myklebustde186432014-07-10 14:07:26 -04003701 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003702 __be32 status;
3703 int oflag = nfs4_access_to_omode(open->op_share_access);
3704 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003705 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003706
Trond Myklebustde186432014-07-10 14:07:26 -04003707 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003708
3709 /*
3710 * Are we trying to set a deny mode that would conflict with
3711 * current access?
3712 */
3713 status = nfs4_file_check_deny(fp, open->op_share_deny);
3714 if (status != nfs_ok) {
3715 spin_unlock(&fp->fi_lock);
3716 goto out;
3717 }
3718
3719 /* set access to the file */
3720 status = nfs4_file_get_access(fp, open->op_share_access);
3721 if (status != nfs_ok) {
3722 spin_unlock(&fp->fi_lock);
3723 goto out;
3724 }
3725
3726 /* Set access bits in stateid */
3727 old_access_bmap = stp->st_access_bmap;
3728 set_access(open->op_share_access, stp);
3729
3730 /* Set new deny mask */
3731 old_deny_bmap = stp->st_deny_bmap;
3732 set_deny(open->op_share_deny, stp);
3733 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3734
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003735 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003736 spin_unlock(&fp->fi_lock);
3737 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003738 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003739 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003740 spin_lock(&fp->fi_lock);
3741 if (!fp->fi_fds[oflag]) {
3742 fp->fi_fds[oflag] = filp;
3743 filp = NULL;
3744 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003745 }
Trond Myklebustde186432014-07-10 14:07:26 -04003746 spin_unlock(&fp->fi_lock);
3747 if (filp)
3748 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003749
3750 status = nfsd4_truncate(rqstp, cur_fh, open);
3751 if (status)
3752 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003753out:
3754 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003755out_put_access:
3756 stp->st_access_bmap = old_access_bmap;
3757 nfs4_file_put_access(fp, open->op_share_access);
3758 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3759 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003760}
3761
Al Virob37ad282006-10-19 23:28:59 -07003762static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003763nfs4_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 -07003764{
Al Virob37ad282006-10-19 23:28:59 -07003765 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003766 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003768 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003769 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003770
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003771 /* test and set deny mode */
3772 spin_lock(&fp->fi_lock);
3773 status = nfs4_file_check_deny(fp, open->op_share_deny);
3774 if (status == nfs_ok) {
3775 old_deny_bmap = stp->st_deny_bmap;
3776 set_deny(open->op_share_deny, stp);
3777 fp->fi_share_deny |=
3778 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3779 }
3780 spin_unlock(&fp->fi_lock);
3781
3782 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003785 status = nfsd4_truncate(rqstp, cur_fh, open);
3786 if (status != nfs_ok)
3787 reset_union_bmap_deny(old_deny_bmap, stp);
3788 return status;
3789}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003792nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003794 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795}
3796
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003797/* Should we give out recallable state?: */
3798static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3799{
3800 if (clp->cl_cb_state == NFSD4_CB_UP)
3801 return true;
3802 /*
3803 * In the sessions case, since we don't have to establish a
3804 * separate connection for callbacks, we assume it's OK
3805 * until we hear otherwise:
3806 */
3807 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3808}
3809
Jeff Laytond564fbe2014-07-16 10:31:58 -04003810static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003811{
3812 struct file_lock *fl;
3813
3814 fl = locks_alloc_lock();
3815 if (!fl)
3816 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003817 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003818 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003819 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3820 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003821 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003822 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003823 return fl;
3824}
3825
J. Bruce Fields99c41512013-05-21 16:21:25 -04003826static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003827{
Trond Myklebust11b91642014-07-29 21:34:08 -04003828 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton415b96c2014-08-22 12:26:36 -04003829 struct file_lock *fl, *ret;
Jeff Layton417c6622014-07-21 09:34:57 -04003830 struct file *filp;
3831 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003832
Jeff Laytond564fbe2014-07-16 10:31:58 -04003833 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003834 if (!fl)
3835 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003836 filp = find_readable_file(fp);
3837 if (!filp) {
3838 /* We should always have a readable file here */
3839 WARN_ON_ONCE(1);
3840 return -EBADF;
3841 }
3842 fl->fl_file = filp;
Jeff Layton415b96c2014-08-22 12:26:36 -04003843 ret = fl;
Jeff Laytone6f5c782014-08-22 10:40:25 -04003844 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003845 if (fl)
Jeff Layton417c6622014-07-21 09:34:57 -04003846 locks_free_lock(fl);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003847 if (status)
Jeff Layton417c6622014-07-21 09:34:57 -04003848 goto out_fput;
Benny Halevycdc97502014-05-30 09:09:30 -04003849 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003850 spin_lock(&fp->fi_lock);
3851 /* Did the lease get broken before we took the lock? */
3852 status = -EAGAIN;
3853 if (fp->fi_had_conflict)
3854 goto out_unlock;
3855 /* Race breaker */
Jeff Layton0c637be2014-08-22 12:05:43 -04003856 if (fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04003857 status = 0;
3858 atomic_inc(&fp->fi_delegees);
3859 hash_delegation_locked(dp, fp);
3860 goto out_unlock;
3861 }
Jeff Layton417c6622014-07-21 09:34:57 -04003862 fp->fi_deleg_file = filp;
3863 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003864 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003865 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003866 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003867 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003868out_unlock:
3869 spin_unlock(&fp->fi_lock);
3870 spin_unlock(&state_lock);
3871out_fput:
3872 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003873 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003874}
3875
Jeff Layton0b266932014-07-25 07:34:25 -04003876static struct nfs4_delegation *
3877nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3878 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003879{
Jeff Layton0b266932014-07-25 07:34:25 -04003880 int status;
3881 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003882
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003883 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003884 return ERR_PTR(-EAGAIN);
3885
3886 dp = alloc_init_deleg(clp, fh);
3887 if (!dp)
3888 return ERR_PTR(-ENOMEM);
3889
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003890 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003891 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003892 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003893 dp->dl_stid.sc_file = fp;
Jeff Layton0c637be2014-08-22 12:05:43 -04003894 if (!fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04003895 spin_unlock(&fp->fi_lock);
3896 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003897 status = nfs4_setlease(dp);
3898 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003899 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003900 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003901 status = -EAGAIN;
3902 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003903 }
Jeff Layton94ae1db2014-12-13 09:11:39 -05003904 atomic_inc(&fp->fi_delegees);
Benny Halevy931ee562014-05-30 09:09:27 -04003905 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003906 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003907out_unlock:
3908 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003909 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003910out:
3911 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003912 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003913 return ERR_PTR(status);
3914 }
3915 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003916}
3917
Benny Halevy4aa89132012-02-21 14:16:44 -08003918static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3919{
3920 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3921 if (status == -EAGAIN)
3922 open->op_why_no_deleg = WND4_CONTENTION;
3923 else {
3924 open->op_why_no_deleg = WND4_RESOURCE;
3925 switch (open->op_deleg_want) {
3926 case NFS4_SHARE_WANT_READ_DELEG:
3927 case NFS4_SHARE_WANT_WRITE_DELEG:
3928 case NFS4_SHARE_WANT_ANY_DELEG:
3929 break;
3930 case NFS4_SHARE_WANT_CANCEL:
3931 open->op_why_no_deleg = WND4_CANCELLED;
3932 break;
3933 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003934 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003935 }
3936 }
3937}
3938
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939/*
3940 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003941 *
3942 * Note we don't support write delegations, and won't until the vfs has
3943 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 */
3945static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003946nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3947 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948{
3949 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003950 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3951 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003952 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003953 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003955 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003956 open->op_recall = 0;
3957 switch (open->op_claim_type) {
3958 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003959 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003960 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003961 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3962 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003963 break;
3964 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003965 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003966 /*
3967 * Let's not give out any delegations till everyone's
3968 * had the chance to reclaim theirs....
3969 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003970 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003971 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003972 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003973 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003974 /*
3975 * Also, if the file was opened for write or
3976 * create, there's a good chance the client's
3977 * about to write to it, resulting in an
3978 * immediate recall (since we don't support
3979 * write delegations):
3980 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003981 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003982 goto out_no_deleg;
3983 if (open->op_create == NFS4_OPEN_CREATE)
3984 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003985 break;
3986 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003987 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003988 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003989 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003990 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003991 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003993 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003995 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003996 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003997 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003998 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003999 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004000out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004001 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4002 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004003 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04004004 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004005 open->op_recall = 1;
4006 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004007
4008 /* 4.1 client asking for a delegation? */
4009 if (open->op_deleg_want)
4010 nfsd4_open_deleg_none_ext(open, status);
4011 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012}
4013
Benny Halevye27f49c2012-02-21 14:16:54 -08004014static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4015 struct nfs4_delegation *dp)
4016{
4017 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4018 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4019 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4020 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4021 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4022 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4023 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4024 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4025 }
4026 /* Otherwise the client must be confused wanting a delegation
4027 * it already has, therefore we don't return
4028 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4029 */
4030}
4031
Al Virob37ad282006-10-19 23:28:59 -07004032__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4034{
Andy Adamson66689582009-04-03 08:28:45 +03004035 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004036 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004038 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004039 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004040 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 /*
4043 * Lookup file; if found, lookup stateid and check open request,
4044 * and check for delegations in the process of being recalled.
4045 * If not found, create the nfs4_file struct
4046 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004047 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004048 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004049 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004050 if (status)
4051 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04004052 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004054 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004055 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004056 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004057 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004058 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 }
4060
4061 /*
4062 * OPEN the file, or upgrade an existing OPEN.
4063 * If truncate fails, the OPEN fails.
4064 */
4065 if (stp) {
4066 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004067 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 if (status)
4069 goto out;
4070 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004071 stp = open->op_stp;
4072 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004073 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004074 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4075 if (status) {
4076 release_open_stateid(stp);
4077 goto out;
4078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004080 update_stateid(&stp->st_stid.sc_stateid);
4081 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082
Benny Halevyd24433c2012-02-16 20:57:17 +02004083 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004084 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4085 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4086 open->op_why_no_deleg = WND4_NOT_WANTED;
4087 goto nodeleg;
4088 }
4089 }
4090
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 /*
4092 * Attempt to hand out a delegation. No error return, because the
4093 * OPEN succeeds even if we fail.
4094 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004095 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004096nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 status = nfs_ok;
4098
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004099 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004100 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004102 /* 4.1 client trying to upgrade/downgrade delegation? */
4103 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004104 open->op_deleg_want)
4105 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004106
NeilBrown13cd2182005-06-23 22:03:10 -07004107 if (fp)
4108 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004109 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05004110 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 /*
4112 * To finish the open response, we just need to set the rflags.
4113 */
4114 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004115 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004116 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004118 if (dp)
4119 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004120 if (stp)
4121 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122
4123 return status;
4124}
4125
Jeff Layton58fb12e2014-07-29 21:34:27 -04004126void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4127 struct nfsd4_open *open, __be32 status)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004128{
4129 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004130 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004131
Jeff Laytond3134b12014-07-29 21:34:32 -04004132 nfsd4_cstate_assign_replay(cstate, so);
4133 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004134 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004135 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04004136 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004137 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004138 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004139}
4140
Al Virob37ad282006-10-19 23:28:59 -07004141__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004142nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4143 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144{
4145 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004146 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004147 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 dprintk("process_renew(%08x/%08x): starting\n",
4150 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004151 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004152 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004154 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004156 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004157 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 goto out;
4159 status = nfs_ok;
4160out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 return status;
4162}
4163
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004164void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004165nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004166{
Jeff Layton33dcc482012-04-10 11:08:48 -04004167 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004168 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004169 return;
4170
NeilBrowna76b4312005-06-23 22:04:01 -07004171 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004172 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004173 /*
4174 * If the server goes down again right now, an NFSv4
4175 * client will still be allowed to reclaim after it comes back up,
4176 * even if it hasn't yet had a chance to reclaim state this time.
4177 *
4178 */
Jeff Layton919b8042014-09-12 16:40:20 -04004179 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004180 /*
4181 * At this point, NFSv4 clients can still reclaim. But if the
4182 * server crashes, any that have not yet reclaimed will be out
4183 * of luck on the next boot.
4184 *
4185 * (NFSv4.1+ clients are considered to have reclaimed once they
4186 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4187 * have reclaimed after their first OPEN.)
4188 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004189 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004190 /*
4191 * At this point, and once lockd and/or any other containers
4192 * exit their grace period, further reclaims will fail and
4193 * regular locking can resume.
4194 */
NeilBrowna76b4312005-06-23 22:04:01 -07004195}
4196
NeilBrownfd39ca92005-06-23 22:04:03 -07004197static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004198nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199{
4200 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004201 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004203 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004205 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004206 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004209 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004210 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004211 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004212 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 clp = list_entry(pos, struct nfs4_client, cl_lru);
4214 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4215 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004216 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 break;
4218 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004219 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004220 dprintk("NFSD: client in use (clientid %08x)\n",
4221 clp->cl_clientid.cl_id);
4222 continue;
4223 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004224 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004225 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004226 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004227 list_for_each_safe(pos, next, &reaplist) {
4228 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 dprintk("NFSD: purging unused client (clientid %08x)\n",
4230 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004231 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 expire_client(clp);
4233 }
Benny Halevycdc97502014-05-30 09:09:30 -04004234 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004235 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004237 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4238 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004240 t = dp->dl_time - cutoff;
4241 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 break;
4243 }
Jeff Layton42690672014-07-25 07:34:20 -04004244 unhash_delegation_locked(dp);
4245 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 }
Benny Halevycdc97502014-05-30 09:09:30 -04004247 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004248 while (!list_empty(&reaplist)) {
4249 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4250 dl_recall_lru);
4251 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004252 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 }
Jeff Layton217526e2014-07-30 08:27:11 -04004254
4255 spin_lock(&nn->client_lock);
4256 while (!list_empty(&nn->close_lru)) {
4257 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4258 oo_close_lru);
4259 if (time_after((unsigned long)oo->oo_time,
4260 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004261 t = oo->oo_time - cutoff;
4262 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 break;
4264 }
Jeff Layton217526e2014-07-30 08:27:11 -04004265 list_del_init(&oo->oo_close_lru);
4266 stp = oo->oo_last_closed_stid;
4267 oo->oo_last_closed_stid = NULL;
4268 spin_unlock(&nn->client_lock);
4269 nfs4_put_stid(&stp->st_stid);
4270 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 }
Jeff Layton217526e2014-07-30 08:27:11 -04004272 spin_unlock(&nn->client_lock);
4273
Jeff Laytona832e7a2014-05-30 09:09:26 -04004274 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004275 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276}
4277
Harvey Harrisona254b242008-02-20 12:49:00 -08004278static struct workqueue_struct *laundry_wq;
4279static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004280
4281static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004282laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283{
4284 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004285 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4286 work);
4287 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4288 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004290 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004292 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293}
4294
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004295static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004296{
Trond Myklebust11b91642014-07-29 21:34:08 -04004297 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004298 return nfserr_bad_stateid;
4299 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300}
4301
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004303access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004305 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4306 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4307 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308}
4309
4310static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004311access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004313 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4314 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315}
4316
4317static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004318__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319{
Al Virob37ad282006-10-19 23:28:59 -07004320 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321
J. Bruce Fields02921912010-07-29 15:16:59 -04004322 /* For lock stateid's, we test the parent open, not the lock: */
4323 if (stp->st_openstp)
4324 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004325 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004327 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 goto out;
4329 status = nfs_ok;
4330out:
4331 return status;
4332}
4333
Al Virob37ad282006-10-19 23:28:59 -07004334static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004335check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004337 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004339 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004340 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 * conflicting state; so we must wait out the grace period. */
4342 return nfserr_grace;
4343 } else if (flags & WR_STATE)
4344 return nfs4_share_conflict(current_fh,
4345 NFS4_SHARE_DENY_WRITE);
4346 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4347 return nfs4_share_conflict(current_fh,
4348 NFS4_SHARE_DENY_READ);
4349}
4350
4351/*
4352 * Allow READ/WRITE during grace period on recovered state only for files
4353 * that are not able to provide mandatory locking.
4354 */
4355static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004356grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004358 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359}
4360
J. Bruce Fields81b82962011-08-23 11:03:29 -04004361/* Returns true iff a is later than b: */
4362static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4363{
Jim Rees1a9357f2013-05-17 17:33:00 -04004364 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004365}
4366
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004367static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004368{
Andy Adamson66689582009-04-03 08:28:45 +03004369 /*
4370 * When sessions are used the stateid generation number is ignored
4371 * when it is zero.
4372 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004373 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004374 return nfs_ok;
4375
4376 if (in->si_generation == ref->si_generation)
4377 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004378
J. Bruce Fields0836f582008-01-26 19:08:12 -05004379 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004380 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004381 return nfserr_bad_stateid;
4382 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004383 * However, we could see a stateid from the past, even from a
4384 * non-buggy client. For example, if the client sends a lock
4385 * while some IO is outstanding, the lock may bump si_generation
4386 * while the IO is still in flight. The client could avoid that
4387 * situation by waiting for responses on all the IO requests,
4388 * but better performance may result in retrying IO that
4389 * receives an old_stateid error if requests are rarely
4390 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004391 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004392 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004393}
4394
Chuck Lever7df302f2012-05-29 13:56:37 -04004395static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004396{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004397 struct nfs4_stid *s;
4398 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004399 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004400
Chuck Lever7df302f2012-05-29 13:56:37 -04004401 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004402 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004403 /* Client debugging aid. */
4404 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4405 char addr_str[INET6_ADDRSTRLEN];
4406 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4407 sizeof(addr_str));
4408 pr_warn_ratelimited("NFSD: client %s testing state ID "
4409 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004410 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004411 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004412 spin_lock(&cl->cl_lock);
4413 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004414 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004415 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004416 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004417 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004418 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004419 switch (s->sc_type) {
4420 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004421 status = nfs_ok;
4422 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004423 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004424 status = nfserr_deleg_revoked;
4425 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004426 case NFS4_OPEN_STID:
4427 case NFS4_LOCK_STID:
4428 ols = openlockstateid(s);
4429 if (ols->st_stateowner->so_is_open_owner
4430 && !(openowner(ols->st_stateowner)->oo_flags
4431 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004432 status = nfserr_bad_stateid;
4433 else
4434 status = nfs_ok;
4435 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004436 default:
4437 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004438 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004439 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004440 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004441 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004442 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004443out_unlock:
4444 spin_unlock(&cl->cl_lock);
4445 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004446}
4447
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004448static __be32
4449nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4450 stateid_t *stateid, unsigned char typemask,
4451 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004452{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004453 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004454
4455 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4456 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004457 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004458 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004459 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004460 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004461 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004462 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004463 if (status)
4464 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004465 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004466 if (!*s)
4467 return nfserr_bad_stateid;
4468 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004469}
4470
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471/*
4472* Checks for stateid operations
4473*/
Al Virob37ad282006-10-19 23:28:59 -07004474__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004475nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004476 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004478 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004479 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004481 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004483 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004484 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004485 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 if (filpp)
4488 *filpp = NULL;
4489
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004490 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 return nfserr_grace;
4492
4493 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004494 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004496 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004497 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004498 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004499 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04004500 return status;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004501 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4502 if (status)
4503 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004504 switch (s->sc_type) {
4505 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004506 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004507 status = nfs4_check_delegmode(dp, flags);
4508 if (status)
4509 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004510 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004511 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004512 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004513 WARN_ON_ONCE(1);
4514 status = nfserr_serverfault;
4515 goto out;
4516 }
Trond Myklebustde186432014-07-10 14:07:26 -04004517 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004518 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004519 break;
4520 case NFS4_OPEN_STID:
4521 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004522 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004523 status = nfs4_check_fh(current_fh, stp);
4524 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004526 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004527 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004529 status = nfs4_check_openmode(stp, flags);
4530 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004532 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004533 struct nfs4_file *fp = stp->st_stid.sc_file;
4534
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004535 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004536 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004537 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004538 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004539 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004540 break;
4541 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004542 status = nfserr_bad_stateid;
4543 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 }
4545 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004546 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004547 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004549 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 return status;
4551}
4552
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004553/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004554 * Test if the stateid is valid
4555 */
4556__be32
4557nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4558 struct nfsd4_test_stateid *test_stateid)
4559{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004560 struct nfsd4_test_stateid_id *stateid;
4561 struct nfs4_client *cl = cstate->session->se_client;
4562
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004563 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004564 stateid->ts_id_status =
4565 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004566
Bryan Schumaker17456802011-07-13 10:50:48 -04004567 return nfs_ok;
4568}
4569
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004570__be32
4571nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4572 struct nfsd4_free_stateid *free_stateid)
4573{
4574 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004575 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004576 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004577 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004578 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004579 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004580
Jeff Layton1af71cc2014-07-29 21:34:14 -04004581 spin_lock(&cl->cl_lock);
4582 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004583 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004584 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004585 switch (s->sc_type) {
4586 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004587 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004588 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004589 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004590 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4591 if (ret)
4592 break;
4593 ret = nfserr_locks_held;
4594 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004595 case NFS4_LOCK_STID:
4596 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4597 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004598 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004599 stp = openlockstateid(s);
4600 ret = nfserr_locks_held;
4601 if (check_for_locks(stp->st_stid.sc_file,
4602 lockowner(stp->st_stateowner)))
4603 break;
4604 unhash_lock_stateid(stp);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004605 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004606 nfs4_put_stid(s);
4607 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004608 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004609 case NFS4_REVOKED_DELEG_STID:
4610 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004611 list_del_init(&dp->dl_recall_lru);
4612 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004613 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004614 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004615 goto out;
4616 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004617 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004618out_unlock:
4619 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004620out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004621 return ret;
4622}
4623
NeilBrown4c4cd222005-07-07 17:59:27 -07004624static inline int
4625setlkflg (int type)
4626{
4627 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4628 RD_STATE : WR_STATE;
4629}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004631static __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 -04004632{
4633 struct svc_fh *current_fh = &cstate->current_fh;
4634 struct nfs4_stateowner *sop = stp->st_stateowner;
4635 __be32 status;
4636
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004637 status = nfsd4_check_seqid(cstate, sop, seqid);
4638 if (status)
4639 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004640 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4641 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004642 /*
4643 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004644 * nfserr_replay_me from the previous step, and
4645 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004646 */
4647 return nfserr_bad_stateid;
4648 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4649 if (status)
4650 return status;
4651 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004652}
4653
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654/*
4655 * Checks for sequence id mutating operations.
4656 */
Al Virob37ad282006-10-19 23:28:59 -07004657static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004658nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004659 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004660 struct nfs4_ol_stateid **stpp,
4661 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004663 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004664 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004665 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004667 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4668 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004669
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004671 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004672 if (status)
4673 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004674 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004675 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004677 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004678 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004679 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004680 else
4681 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004682 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004683}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004684
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004685static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4686 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004687{
4688 __be32 status;
4689 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004690 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004692 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004693 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004694 if (status)
4695 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004696 oo = openowner(stp->st_stateowner);
4697 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4698 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004699 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004700 }
4701 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004702 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703}
4704
Al Virob37ad282006-10-19 23:28:59 -07004705__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004706nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004707 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708{
Al Virob37ad282006-10-19 23:28:59 -07004709 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004710 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004711 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004712 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713
Al Viroa6a9f182013-09-16 10:57:01 -04004714 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4715 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004717 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004718 if (status)
4719 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004721 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004722 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004723 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004724 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004725 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004726 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004727 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004728 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004729 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004730 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004731 update_stateid(&stp->st_stid.sc_stateid);
4732 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004733 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004734 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004735
Jeff Layton2a4317c2012-03-21 16:42:43 -04004736 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004737 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004738put_stateid:
4739 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004741 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 return status;
4743}
4744
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004745static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004747 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004748 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004749 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004750 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004751}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004752
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004753static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4754{
4755 switch (to_access) {
4756 case NFS4_SHARE_ACCESS_READ:
4757 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4758 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4759 break;
4760 case NFS4_SHARE_ACCESS_WRITE:
4761 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4762 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4763 break;
4764 case NFS4_SHARE_ACCESS_BOTH:
4765 break;
4766 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004767 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 }
4769}
4770
Al Virob37ad282006-10-19 23:28:59 -07004771__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004772nfsd4_open_downgrade(struct svc_rqst *rqstp,
4773 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004774 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775{
Al Virob37ad282006-10-19 23:28:59 -07004776 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004777 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004778 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779
Al Viroa6a9f182013-09-16 10:57:01 -04004780 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4781 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004783 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004784 if (od->od_deleg_want)
4785 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4786 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004788 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004789 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004790 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004793 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004794 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004796 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004798 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004799 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004801 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004803 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
Jeff Laytonce0fc432012-05-11 09:45:14 -04004805 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004807 update_stateid(&stp->st_stid.sc_stateid);
4808 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004810put_stateid:
4811 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004813 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 return status;
4815}
4816
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004817static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4818{
Trond Myklebustacf92952014-06-30 11:48:37 -04004819 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytond83017f2014-07-29 21:34:42 -04004820 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004821
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004822 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04004823 spin_lock(&clp->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04004824 unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004825
Jeff Laytond83017f2014-07-29 21:34:42 -04004826 if (clp->cl_minorversion) {
4827 put_ol_stateid_locked(s, &reaplist);
4828 spin_unlock(&clp->cl_lock);
4829 free_ol_stateid_reaplist(&reaplist);
4830 } else {
4831 spin_unlock(&clp->cl_lock);
4832 free_ol_stateid_reaplist(&reaplist);
Jeff Laytond3134b12014-07-29 21:34:32 -04004833 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04004834 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004835}
4836
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837/*
4838 * nfs4_unlock_state() called after encode
4839 */
Al Virob37ad282006-10-19 23:28:59 -07004840__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004841nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004842 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843{
Al Virob37ad282006-10-19 23:28:59 -07004844 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004845 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004846 struct net *net = SVC_NET(rqstp);
4847 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848
Al Viroa6a9f182013-09-16 10:57:01 -04004849 dprintk("NFSD: nfsd4_close on file %pd\n",
4850 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004852 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4853 &close->cl_stateid,
4854 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004855 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004856 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004857 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004859 update_stateid(&stp->st_stid.sc_stateid);
4860 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004862 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004863
4864 /* put reference from nfs4_preprocess_seqid_op */
4865 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 return status;
4868}
4869
Al Virob37ad282006-10-19 23:28:59 -07004870__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004871nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4872 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004874 struct nfs4_delegation *dp;
4875 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004876 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004877 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004878 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004880 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004881 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004883 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004884 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004885 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004886 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004887 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004888 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004889 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004890
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004891 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004892put_stateid:
4893 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894out:
4895 return status;
4896}
4897
4898
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900
Benny Halevy87df4de2008-12-15 19:42:03 +02004901static inline u64
4902end_offset(u64 start, u64 len)
4903{
4904 u64 end;
4905
4906 end = start + len;
4907 return end >= start ? end: NFS4_MAX_UINT64;
4908}
4909
4910/* last octet in a range */
4911static inline u64
4912last_byte_offset(u64 start, u64 len)
4913{
4914 u64 end;
4915
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004916 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004917 end = start + len;
4918 return end > start ? end - 1: NFS4_MAX_UINT64;
4919}
4920
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921/*
4922 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4923 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4924 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4925 * locking, this prevents us from being completely protocol-compliant. The
4926 * real solution to this problem is to start using unsigned file offsets in
4927 * the VFS, but this is a very deep change!
4928 */
4929static inline void
4930nfs4_transform_lock_offset(struct file_lock *lock)
4931{
4932 if (lock->fl_start < 0)
4933 lock->fl_start = OFFSET_MAX;
4934 if (lock->fl_end < 0)
4935 lock->fl_end = OFFSET_MAX;
4936}
4937
Kinglong Meeaef95832014-08-22 10:18:44 -04004938static void nfsd4_fl_get_owner(struct file_lock *dst, struct file_lock *src)
4939{
4940 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)src->fl_owner;
4941 dst->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lo->lo_owner));
4942}
4943
4944static void nfsd4_fl_put_owner(struct file_lock *fl)
4945{
4946 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
4947
4948 if (lo) {
4949 nfs4_put_stateowner(&lo->lo_owner);
4950 fl->fl_owner = NULL;
4951 }
4952}
4953
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004954static const struct lock_manager_operations nfsd_posix_mng_ops = {
Kinglong Meeaef95832014-08-22 10:18:44 -04004955 .lm_get_owner = nfsd4_fl_get_owner,
4956 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07004957};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958
4959static inline void
4960nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4961{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004962 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963
NeilBrownd5b90262006-04-10 22:55:22 -07004964 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004965 lo = (struct nfs4_lockowner *) fl->fl_owner;
4966 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4967 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004968 if (!deny->ld_owner.data)
4969 /* We just don't care that much */
4970 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004971 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4972 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004973 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004974nevermind:
4975 deny->ld_owner.len = 0;
4976 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004977 deny->ld_clientid.cl_boot = 0;
4978 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 }
4980 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004981 deny->ld_length = NFS4_MAX_UINT64;
4982 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4984 deny->ld_type = NFS4_READ_LT;
4985 if (fl->fl_type != F_RDLCK)
4986 deny->ld_type = NFS4_WRITE_LT;
4987}
4988
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004989static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004990find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004991 struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004993 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004994 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995
Trond Myklebust0a880a22014-07-30 08:27:10 -04004996 lockdep_assert_held(&clp->cl_lock);
4997
Trond Myklebustd4f04892014-07-29 21:34:36 -04004998 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4999 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005000 if (so->so_is_open_owner)
5001 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005002 if (same_owner_str(so, owner))
5003 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 }
5005 return NULL;
5006}
5007
Trond Myklebustc58c6612014-07-29 21:34:35 -04005008static struct nfs4_lockowner *
5009find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005010 struct nfs4_client *clp)
Trond Myklebustc58c6612014-07-29 21:34:35 -04005011{
5012 struct nfs4_lockowner *lo;
5013
Trond Myklebustd4f04892014-07-29 21:34:36 -04005014 spin_lock(&clp->cl_lock);
5015 lo = find_lockowner_str_locked(clid, owner, clp);
5016 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005017 return lo;
5018}
5019
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005020static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5021{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005022 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005023}
5024
Jeff Layton6b180f02014-07-29 21:34:26 -04005025static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5026{
5027 struct nfs4_lockowner *lo = lockowner(sop);
5028
5029 kmem_cache_free(lockowner_slab, lo);
5030}
5031
5032static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005033 .so_unhash = nfs4_unhash_lockowner,
5034 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005035};
5036
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037/*
5038 * Alloc a lock owner structure.
5039 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005040 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005042 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005044static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005045alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5046 struct nfs4_ol_stateid *open_stp,
5047 struct nfsd4_lock *lock)
5048{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005049 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005051 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5052 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005054 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5055 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005056 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005057 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005058 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005059 ret = find_lockowner_str_locked(&clp->cl_clientid,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005060 &lock->lk_new_owner, clp);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005061 if (ret == NULL) {
5062 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005063 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005064 ret = lo;
5065 } else
5066 nfs4_free_lockowner(&lo->lo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005067 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005068 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069}
5070
Jeff Layton356a95e2014-07-29 21:34:13 -04005071static void
5072init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5073 struct nfs4_file *fp, struct inode *inode,
5074 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005076 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077
Jeff Layton356a95e2014-07-29 21:34:13 -04005078 lockdep_assert_held(&clp->cl_lock);
5079
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005080 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005081 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005082 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07005083 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005084 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005085 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005086 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005088 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005089 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005090 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005091 spin_lock(&fp->fi_lock);
5092 list_add(&stp->st_perfile, &fp->fi_stateids);
5093 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094}
5095
Jeff Laytonc53530d2014-06-30 11:48:39 -04005096static struct nfs4_ol_stateid *
5097find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5098{
5099 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005100 struct nfs4_client *clp = lo->lo_owner.so_client;
5101
5102 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005103
5104 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005105 if (lst->st_stid.sc_file == fp) {
5106 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005107 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005108 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005109 }
5110 return NULL;
5111}
5112
Jeff Layton356a95e2014-07-29 21:34:13 -04005113static struct nfs4_ol_stateid *
5114find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5115 struct inode *inode, struct nfs4_ol_stateid *ost,
5116 bool *new)
5117{
5118 struct nfs4_stid *ns = NULL;
5119 struct nfs4_ol_stateid *lst;
5120 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5121 struct nfs4_client *clp = oo->oo_owner.so_client;
5122
5123 spin_lock(&clp->cl_lock);
5124 lst = find_lock_stateid(lo, fi);
5125 if (lst == NULL) {
5126 spin_unlock(&clp->cl_lock);
5127 ns = nfs4_alloc_stid(clp, stateid_slab);
5128 if (ns == NULL)
5129 return NULL;
5130
5131 spin_lock(&clp->cl_lock);
5132 lst = find_lock_stateid(lo, fi);
5133 if (likely(!lst)) {
5134 lst = openlockstateid(ns);
5135 init_lock_stateid(lst, lo, fi, inode, ost);
5136 ns = NULL;
5137 *new = true;
5138 }
5139 }
5140 spin_unlock(&clp->cl_lock);
5141 if (ns)
5142 nfs4_put_stid(ns);
5143 return lst;
5144}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005145
NeilBrownfd39ca92005-06-23 22:04:03 -07005146static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147check_lock_length(u64 offset, u64 length)
5148{
Benny Halevy87df4de2008-12-15 19:42:03 +02005149 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 LOFF_OVERFLOW(offset, length)));
5151}
5152
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005153static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005154{
Trond Myklebust11b91642014-07-29 21:34:08 -04005155 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005156
Jeff Layton7214e862014-07-10 14:07:33 -04005157 lockdep_assert_held(&fp->fi_lock);
5158
Jeff Layton82c5ff12012-05-11 09:45:13 -04005159 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005160 return;
Jeff Layton12659652014-07-10 14:07:28 -04005161 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005162 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005163}
5164
Jeff Layton356a95e2014-07-29 21:34:13 -04005165static __be32
5166lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5167 struct nfs4_ol_stateid *ost,
5168 struct nfsd4_lock *lock,
5169 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005170{
Jeff Layton5db1c032014-07-29 21:34:28 -04005171 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005172 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005173 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5174 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005175 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005176 struct nfs4_lockowner *lo;
5177 unsigned int strhashval;
5178
Trond Myklebustd4f04892014-07-29 21:34:36 -04005179 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005180 if (!lo) {
Trond Myklebustd4f04892014-07-29 21:34:36 -04005181 strhashval = ownerstr_hashval(&lock->v.new.owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005182 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5183 if (lo == NULL)
5184 return nfserr_jukebox;
5185 } else {
5186 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005187 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005188 if (!cstate->minorversion &&
5189 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005190 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005191 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005192
Jeff Layton356a95e2014-07-29 21:34:13 -04005193 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005194 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005195 status = nfserr_jukebox;
5196 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005197 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005198 status = nfs_ok;
5199out:
5200 nfs4_put_stateowner(&lo->lo_owner);
5201 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005202}
5203
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204/*
5205 * LOCK operation
5206 */
Al Virob37ad282006-10-19 23:28:59 -07005207__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005208nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005209 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005211 struct nfs4_openowner *open_sop = NULL;
5212 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005213 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005214 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005215 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005216 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005217 struct file_lock *file_lock = NULL;
5218 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005219 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005220 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005221 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005222 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005223 struct net *net = SVC_NET(rqstp);
5224 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225
5226 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5227 (long long) lock->lk_offset,
5228 (long long) lock->lk_length);
5229
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 if (check_lock_length(lock->lk_offset, lock->lk_length))
5231 return nfserr_inval;
5232
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005233 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005234 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005235 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5236 return status;
5237 }
5238
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005240 if (nfsd4_has_session(cstate))
5241 /* See rfc 5661 18.10.3: given clientid is ignored: */
5242 memcpy(&lock->v.new.clientid,
5243 &cstate->session->se_client->cl_clientid,
5244 sizeof(clientid_t));
5245
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005247 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005251 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 lock->lk_new_open_seqid,
5253 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005254 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005255 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005257 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005258 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005259 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005260 &lock->v.new.clientid))
5261 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005262 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005263 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005264 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005265 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005266 lock->lk_old_lock_seqid,
5267 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005268 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005269 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005270 if (status)
5271 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005272 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005274 lkflg = setlkflg(lock->lk_type);
5275 status = nfs4_check_openmode(lock_stp, lkflg);
5276 if (status)
5277 goto out;
5278
NeilBrown0dd395d2005-07-07 17:59:15 -07005279 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005280 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005281 goto out;
5282 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005283 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005284 goto out;
5285
Jeff Layton21179d82012-08-21 08:03:32 -04005286 file_lock = locks_alloc_lock();
5287 if (!file_lock) {
5288 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5289 status = nfserr_jukebox;
5290 goto out;
5291 }
5292
Trond Myklebust11b91642014-07-29 21:34:08 -04005293 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 switch (lock->lk_type) {
5295 case NFS4_READ_LT:
5296 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005297 spin_lock(&fp->fi_lock);
5298 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005299 if (filp)
5300 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005301 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005302 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005303 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 case NFS4_WRITE_LT:
5305 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005306 spin_lock(&fp->fi_lock);
5307 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005308 if (filp)
5309 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005310 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005311 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005312 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 default:
5314 status = nfserr_inval;
5315 goto out;
5316 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005317 if (!filp) {
5318 status = nfserr_openmode;
5319 goto out;
5320 }
Kinglong Meeaef95832014-08-22 10:18:44 -04005321
5322 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04005323 file_lock->fl_pid = current->tgid;
5324 file_lock->fl_file = filp;
5325 file_lock->fl_flags = FL_POSIX;
5326 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5327 file_lock->fl_start = lock->lk_offset;
5328 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5329 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330
Jeff Layton21179d82012-08-21 08:03:32 -04005331 conflock = locks_alloc_lock();
5332 if (!conflock) {
5333 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5334 status = nfserr_jukebox;
5335 goto out;
5336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337
Jeff Layton21179d82012-08-21 08:03:32 -04005338 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005339 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005341 update_stateid(&lock_stp->st_stid.sc_stateid);
5342 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005344 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005345 break;
5346 case (EAGAIN): /* conflock holds conflicting lock */
5347 status = nfserr_denied;
5348 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005349 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005350 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 case (EDEADLK):
5352 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005353 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005354 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005355 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005356 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005357 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359out:
Trond Myklebustde186432014-07-10 14:07:26 -04005360 if (filp)
5361 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005362 if (lock_stp) {
5363 /* Bump seqid manually if the 4.0 replay owner is openowner */
5364 if (cstate->replay_owner &&
5365 cstate->replay_owner != &lock_sop->lo_owner &&
5366 seqid_mutating_err(ntohl(status)))
5367 lock_sop->lo_owner.so_seqid++;
5368
5369 /*
5370 * If this is a new, never-before-used stateid, and we are
5371 * returning an error, then just go ahead and release it.
5372 */
5373 if (status && new)
5374 release_lock_stateid(lock_stp);
5375
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005376 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005377 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005378 if (open_stp)
5379 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005380 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005381 if (file_lock)
5382 locks_free_lock(file_lock);
5383 if (conflock)
5384 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 return status;
5386}
5387
5388/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005389 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5390 * so we do a temporary open here just to get an open file to pass to
5391 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5392 * inode operation.)
5393 */
Al Viro04da6e92012-04-13 00:00:04 -04005394static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005395{
5396 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005397 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5398 if (!err) {
5399 err = nfserrno(vfs_test_lock(file, lock));
5400 nfsd_close(file);
5401 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005402 return err;
5403}
5404
5405/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 * LOCKT operation
5407 */
Al Virob37ad282006-10-19 23:28:59 -07005408__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005409nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5410 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411{
Jeff Layton21179d82012-08-21 08:03:32 -04005412 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005413 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005414 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005415 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005417 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 return nfserr_grace;
5419
5420 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5421 return nfserr_inval;
5422
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005423 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005424 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005425 if (status)
5426 goto out;
5427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005429 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431
Jeff Layton21179d82012-08-21 08:03:32 -04005432 file_lock = locks_alloc_lock();
5433 if (!file_lock) {
5434 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5435 status = nfserr_jukebox;
5436 goto out;
5437 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005438
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 switch (lockt->lt_type) {
5440 case NFS4_READ_LT:
5441 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005442 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 break;
5444 case NFS4_WRITE_LT:
5445 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005446 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 break;
5448 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005449 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 status = nfserr_inval;
5451 goto out;
5452 }
5453
Trond Myklebustd4f04892014-07-29 21:34:36 -04005454 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5455 cstate->clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005456 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005457 file_lock->fl_owner = (fl_owner_t)lo;
5458 file_lock->fl_pid = current->tgid;
5459 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460
Jeff Layton21179d82012-08-21 08:03:32 -04005461 file_lock->fl_start = lockt->lt_offset;
5462 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463
Jeff Layton21179d82012-08-21 08:03:32 -04005464 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465
Jeff Layton21179d82012-08-21 08:03:32 -04005466 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005467 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005468 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005469
Jeff Layton21179d82012-08-21 08:03:32 -04005470 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005472 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 }
5474out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005475 if (lo)
5476 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04005477 if (file_lock)
5478 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 return status;
5480}
5481
Al Virob37ad282006-10-19 23:28:59 -07005482__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005483nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005484 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005486 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005488 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005489 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005490 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005491 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5492
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5494 (long long) locku->lu_offset,
5495 (long long) locku->lu_length);
5496
5497 if (check_lock_length(locku->lu_offset, locku->lu_length))
5498 return nfserr_inval;
5499
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005500 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005501 &locku->lu_stateid, NFS4_LOCK_STID,
5502 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005503 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005505 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005506 if (!filp) {
5507 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005508 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005509 }
Jeff Layton21179d82012-08-21 08:03:32 -04005510 file_lock = locks_alloc_lock();
5511 if (!file_lock) {
5512 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5513 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005514 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005515 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005516
Jeff Layton21179d82012-08-21 08:03:32 -04005517 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04005518 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04005519 file_lock->fl_pid = current->tgid;
5520 file_lock->fl_file = filp;
5521 file_lock->fl_flags = FL_POSIX;
5522 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5523 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524
Jeff Layton21179d82012-08-21 08:03:32 -04005525 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5526 locku->lu_length);
5527 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528
Jeff Layton21179d82012-08-21 08:03:32 -04005529 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005530 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005531 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 goto out_nfserr;
5533 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005534 update_stateid(&stp->st_stid.sc_stateid);
5535 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005536fput:
5537 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005538put_stateid:
5539 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005541 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005542 if (file_lock)
5543 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 return status;
5545
5546out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005547 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005548 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549}
5550
5551/*
5552 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005553 * true: locks held by lockowner
5554 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005556static bool
5557check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005559 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005560 int status = false;
5561 struct file *filp = find_any_file(fp);
5562 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005563 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005564
5565 if (!filp) {
5566 /* Any valid lock stateid should have some sort of access */
5567 WARN_ON_ONCE(1);
5568 return status;
5569 }
5570
5571 inode = file_inode(filp);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005572 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005574 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05005575 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005576 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
5577 if (fl->fl_owner == (fl_owner_t)lowner) {
5578 status = true;
5579 break;
5580 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005581 }
Jeff Layton6109c852015-01-16 15:05:57 -05005582 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 }
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005584 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 return status;
5586}
5587
Al Virob37ad282006-10-19 23:28:59 -07005588__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005589nfsd4_release_lockowner(struct svc_rqst *rqstp,
5590 struct nfsd4_compound_state *cstate,
5591 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592{
5593 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005594 struct nfs4_stateowner *sop;
5595 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005596 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005598 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005599 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005600 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005601 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602
5603 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5604 clid->cl_boot, clid->cl_id);
5605
Jeff Layton4b24ca72014-06-30 11:48:44 -04005606 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005607 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04005608 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005609
Trond Myklebustd4f04892014-07-29 21:34:36 -04005610 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005611 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005612 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005613 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005614 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005615
5616 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005617 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005618
Jeff Layton882e9d22014-07-29 21:34:37 -04005619 /* see if there are still any locks associated with it */
5620 lo = lockowner(sop);
5621 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5622 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5623 status = nfserr_locks_held;
5624 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04005625 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04005626 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005627 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005628
Kinglong Meeb5971af2014-08-22 10:18:43 -04005629 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04005630 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005631 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005632 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005633 if (lo)
5634 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 return status;
5636}
5637
5638static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005639alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640{
NeilBrowna55370a2005-06-23 22:03:52 -07005641 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642}
5643
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005644bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005645nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005646{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005647 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005648
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005649 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005650 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005651}
5652
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653/*
5654 * failure => all reset bets are off, nfserr_no_grace...
5655 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005656struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005657nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658{
5659 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005660 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661
NeilBrowna55370a2005-06-23 22:03:52 -07005662 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5663 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005664 if (crp) {
5665 strhashval = clientstr_hashval(name);
5666 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005667 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005668 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005669 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005670 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005671 }
5672 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673}
5674
Jeff Layton2a4317c2012-03-21 16:42:43 -04005675void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005676nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005677{
5678 list_del(&crp->cr_strhash);
5679 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005680 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005681}
5682
5683void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005684nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685{
5686 struct nfs4_client_reclaim *crp = NULL;
5687 int i;
5688
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005690 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5691 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005693 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 }
5695 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005696 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697}
5698
5699/*
5700 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005701struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005702nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703{
5704 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 struct nfs4_client_reclaim *crp = NULL;
5706
Jeff Layton278c9312012-11-12 15:00:52 -05005707 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708
Jeff Layton278c9312012-11-12 15:00:52 -05005709 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005710 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005711 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 return crp;
5713 }
5714 }
5715 return NULL;
5716}
5717
5718/*
5719* Called from OPEN. Look for clientid in reclaim list.
5720*/
Al Virob37ad282006-10-19 23:28:59 -07005721__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005722nfs4_check_open_reclaim(clientid_t *clid,
5723 struct nfsd4_compound_state *cstate,
5724 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005726 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005727
5728 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005729 status = lookup_clientid(clid, cstate, nn);
5730 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005731 return nfserr_reclaim_bad;
5732
Jeff Layton3b3e7b72014-09-12 16:40:22 -04005733 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
5734 return nfserr_no_grace;
5735
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005736 if (nfsd4_client_record_check(cstate->clp))
5737 return nfserr_reclaim_bad;
5738
5739 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740}
5741
Bryan Schumaker65178db2011-11-01 13:35:21 -04005742#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04005743static inline void
5744put_client(struct nfs4_client *clp)
5745{
5746 atomic_dec(&clp->cl_refcount);
5747}
5748
Jeff Layton285abde2014-07-30 08:27:24 -04005749static struct nfs4_client *
5750nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5751{
5752 struct nfs4_client *clp;
5753 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5754 nfsd_net_id);
5755
5756 if (!nfsd_netns_ready(nn))
5757 return NULL;
5758
5759 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5760 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5761 return clp;
5762 }
5763 return NULL;
5764}
5765
Jeff Layton7ec0e362014-07-30 08:27:17 -04005766u64
Jeff Layton285abde2014-07-30 08:27:24 -04005767nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04005768{
5769 struct nfs4_client *clp;
5770 u64 count = 0;
5771 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5772 nfsd_net_id);
5773 char buf[INET6_ADDRSTRLEN];
5774
5775 if (!nfsd_netns_ready(nn))
5776 return 0;
5777
5778 spin_lock(&nn->client_lock);
5779 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5780 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5781 pr_info("NFS Client: %s\n", buf);
5782 ++count;
5783 }
5784 spin_unlock(&nn->client_lock);
5785
5786 return count;
5787}
Bryan Schumaker65178db2011-11-01 13:35:21 -04005788
Jeff Laytona0926d12014-07-30 08:27:18 -04005789u64
Jeff Layton285abde2014-07-30 08:27:24 -04005790nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04005791{
5792 u64 count = 0;
5793 struct nfs4_client *clp;
5794 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5795 nfsd_net_id);
5796
5797 if (!nfsd_netns_ready(nn))
5798 return count;
5799
5800 spin_lock(&nn->client_lock);
5801 clp = nfsd_find_client(addr, addr_size);
5802 if (clp) {
5803 if (mark_client_expired_locked(clp) == nfs_ok)
5804 ++count;
5805 else
5806 clp = NULL;
5807 }
5808 spin_unlock(&nn->client_lock);
5809
5810 if (clp)
5811 expire_client(clp);
5812
5813 return count;
5814}
5815
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005816u64
Jeff Layton285abde2014-07-30 08:27:24 -04005817nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005818{
5819 u64 count = 0;
5820 struct nfs4_client *clp, *next;
5821 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5822 nfsd_net_id);
5823 LIST_HEAD(reaplist);
5824
5825 if (!nfsd_netns_ready(nn))
5826 return count;
5827
5828 spin_lock(&nn->client_lock);
5829 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5830 if (mark_client_expired_locked(clp) == nfs_ok) {
5831 list_add(&clp->cl_lru, &reaplist);
5832 if (max != 0 && ++count >= max)
5833 break;
5834 }
5835 }
5836 spin_unlock(&nn->client_lock);
5837
5838 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
5839 expire_client(clp);
5840
5841 return count;
5842}
5843
Bryan Schumaker184c1842012-11-29 11:40:44 -05005844static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5845 const char *type)
5846{
5847 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005848 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005849 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5850}
5851
Jeff Layton016200c2014-07-30 08:27:21 -04005852static void
5853nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
5854 struct list_head *collect)
5855{
5856 struct nfs4_client *clp = lst->st_stid.sc_client;
5857 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5858 nfsd_net_id);
5859
5860 if (!collect)
5861 return;
5862
5863 lockdep_assert_held(&nn->client_lock);
5864 atomic_inc(&clp->cl_refcount);
5865 list_add(&lst->st_locks, collect);
5866}
5867
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005868static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04005869 struct list_head *collect,
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005870 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005871{
5872 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005873 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005874 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005875 u64 count = 0;
5876
Jeff Layton016200c2014-07-30 08:27:21 -04005877 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005878 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005879 list_for_each_entry_safe(stp, st_next,
5880 &oop->oo_owner.so_stateids, st_perstateowner) {
5881 list_for_each_entry_safe(lst, lst_next,
5882 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04005883 if (func) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005884 func(lst);
Jeff Layton016200c2014-07-30 08:27:21 -04005885 nfsd_inject_add_lock_to_list(lst,
5886 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04005887 }
Jeff Layton016200c2014-07-30 08:27:21 -04005888 ++count;
5889 /*
5890 * Despite the fact that these functions deal
5891 * with 64-bit integers for "count", we must
5892 * ensure that it doesn't blow up the
5893 * clp->cl_refcount. Throw a warning if we
5894 * start to approach INT_MAX here.
5895 */
5896 WARN_ON_ONCE(count == (INT_MAX / 2));
5897 if (count == max)
5898 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005899 }
5900 }
5901 }
Jeff Layton016200c2014-07-30 08:27:21 -04005902out:
5903 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005904
5905 return count;
5906}
5907
Jeff Layton016200c2014-07-30 08:27:21 -04005908static u64
5909nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
5910 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05005911{
Jeff Layton016200c2014-07-30 08:27:21 -04005912 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005913}
5914
Jeff Layton016200c2014-07-30 08:27:21 -04005915static u64
5916nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05005917{
Jeff Layton016200c2014-07-30 08:27:21 -04005918 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005919 nfsd_print_count(clp, count, "locked files");
5920 return count;
5921}
5922
Jeff Layton016200c2014-07-30 08:27:21 -04005923u64
Jeff Layton285abde2014-07-30 08:27:24 -04005924nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04005925{
5926 struct nfs4_client *clp;
5927 u64 count = 0;
5928 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5929 nfsd_net_id);
5930
5931 if (!nfsd_netns_ready(nn))
5932 return 0;
5933
5934 spin_lock(&nn->client_lock);
5935 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5936 count += nfsd_print_client_locks(clp);
5937 spin_unlock(&nn->client_lock);
5938
5939 return count;
5940}
5941
5942static void
5943nfsd_reap_locks(struct list_head *reaplist)
5944{
5945 struct nfs4_client *clp;
5946 struct nfs4_ol_stateid *stp, *next;
5947
5948 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
5949 list_del_init(&stp->st_locks);
5950 clp = stp->st_stid.sc_client;
5951 nfs4_put_stid(&stp->st_stid);
5952 put_client(clp);
5953 }
5954}
5955
5956u64
Jeff Layton285abde2014-07-30 08:27:24 -04005957nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04005958{
5959 unsigned int count = 0;
5960 struct nfs4_client *clp;
5961 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5962 nfsd_net_id);
5963 LIST_HEAD(reaplist);
5964
5965 if (!nfsd_netns_ready(nn))
5966 return count;
5967
5968 spin_lock(&nn->client_lock);
5969 clp = nfsd_find_client(addr, addr_size);
5970 if (clp)
5971 count = nfsd_collect_client_locks(clp, &reaplist, 0);
5972 spin_unlock(&nn->client_lock);
5973 nfsd_reap_locks(&reaplist);
5974 return count;
5975}
5976
5977u64
Jeff Layton285abde2014-07-30 08:27:24 -04005978nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04005979{
5980 u64 count = 0;
5981 struct nfs4_client *clp;
5982 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5983 nfsd_net_id);
5984 LIST_HEAD(reaplist);
5985
5986 if (!nfsd_netns_ready(nn))
5987 return count;
5988
5989 spin_lock(&nn->client_lock);
5990 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5991 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
5992 if (max != 0 && count >= max)
5993 break;
5994 }
5995 spin_unlock(&nn->client_lock);
5996 nfsd_reap_locks(&reaplist);
5997 return count;
5998}
5999
Jeff Layton82e05ef2014-07-30 08:27:22 -04006000static u64
6001nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6002 struct list_head *collect,
6003 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006004{
6005 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04006006 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6007 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006008 u64 count = 0;
6009
Jeff Layton82e05ef2014-07-30 08:27:22 -04006010 lockdep_assert_held(&nn->client_lock);
6011
6012 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006013 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04006014 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006015 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04006016 if (collect) {
6017 atomic_inc(&clp->cl_refcount);
6018 list_add(&oop->oo_perclient, collect);
6019 }
6020 }
6021 ++count;
6022 /*
6023 * Despite the fact that these functions deal with
6024 * 64-bit integers for "count", we must ensure that
6025 * it doesn't blow up the clp->cl_refcount. Throw a
6026 * warning if we start to approach INT_MAX here.
6027 */
6028 WARN_ON_ONCE(count == (INT_MAX / 2));
6029 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006030 break;
6031 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04006032 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006033
6034 return count;
6035}
6036
Jeff Layton82e05ef2014-07-30 08:27:22 -04006037static u64
6038nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006039{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006040 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6041
6042 nfsd_print_count(clp, count, "openowners");
6043 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006044}
6045
Jeff Layton82e05ef2014-07-30 08:27:22 -04006046static u64
6047nfsd_collect_client_openowners(struct nfs4_client *clp,
6048 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006049{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006050 return nfsd_foreach_client_openowner(clp, max, collect,
6051 unhash_openowner_locked);
6052}
6053
6054u64
Jeff Layton285abde2014-07-30 08:27:24 -04006055nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006056{
6057 struct nfs4_client *clp;
6058 u64 count = 0;
6059 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6060 nfsd_net_id);
6061
6062 if (!nfsd_netns_ready(nn))
6063 return 0;
6064
6065 spin_lock(&nn->client_lock);
6066 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6067 count += nfsd_print_client_openowners(clp);
6068 spin_unlock(&nn->client_lock);
6069
6070 return count;
6071}
6072
6073static void
6074nfsd_reap_openowners(struct list_head *reaplist)
6075{
6076 struct nfs4_client *clp;
6077 struct nfs4_openowner *oop, *next;
6078
6079 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6080 list_del_init(&oop->oo_perclient);
6081 clp = oop->oo_owner.so_client;
6082 release_openowner(oop);
6083 put_client(clp);
6084 }
6085}
6086
6087u64
Jeff Layton285abde2014-07-30 08:27:24 -04006088nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6089 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006090{
6091 unsigned int count = 0;
6092 struct nfs4_client *clp;
6093 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6094 nfsd_net_id);
6095 LIST_HEAD(reaplist);
6096
6097 if (!nfsd_netns_ready(nn))
6098 return count;
6099
6100 spin_lock(&nn->client_lock);
6101 clp = nfsd_find_client(addr, addr_size);
6102 if (clp)
6103 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6104 spin_unlock(&nn->client_lock);
6105 nfsd_reap_openowners(&reaplist);
6106 return count;
6107}
6108
6109u64
Jeff Layton285abde2014-07-30 08:27:24 -04006110nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006111{
6112 u64 count = 0;
6113 struct nfs4_client *clp;
6114 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6115 nfsd_net_id);
6116 LIST_HEAD(reaplist);
6117
6118 if (!nfsd_netns_ready(nn))
6119 return count;
6120
6121 spin_lock(&nn->client_lock);
6122 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6123 count += nfsd_collect_client_openowners(clp, &reaplist,
6124 max - count);
6125 if (max != 0 && count >= max)
6126 break;
6127 }
6128 spin_unlock(&nn->client_lock);
6129 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006130 return count;
6131}
6132
Bryan Schumaker269de302012-11-29 11:40:42 -05006133static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6134 struct list_head *victims)
6135{
6136 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006137 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6138 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006139 u64 count = 0;
6140
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006141 lockdep_assert_held(&nn->client_lock);
6142
6143 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006144 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006145 if (victims) {
6146 /*
6147 * It's not safe to mess with delegations that have a
6148 * non-zero dl_time. They might have already been broken
6149 * and could be processed by the laundromat outside of
6150 * the state_lock. Just leave them be.
6151 */
6152 if (dp->dl_time != 0)
6153 continue;
6154
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006155 atomic_inc(&clp->cl_refcount);
Jeff Layton42690672014-07-25 07:34:20 -04006156 unhash_delegation_locked(dp);
6157 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006158 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006159 ++count;
6160 /*
6161 * Despite the fact that these functions deal with
6162 * 64-bit integers for "count", we must ensure that
6163 * it doesn't blow up the clp->cl_refcount. Throw a
6164 * warning if we start to approach INT_MAX here.
6165 */
6166 WARN_ON_ONCE(count == (INT_MAX / 2));
6167 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006168 break;
6169 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006170 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006171 return count;
6172}
6173
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006174static u64
6175nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006176{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006177 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006178
6179 nfsd_print_count(clp, count, "delegations");
6180 return count;
6181}
6182
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006183u64
Jeff Layton285abde2014-07-30 08:27:24 -04006184nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006185{
6186 struct nfs4_client *clp;
6187 u64 count = 0;
6188 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6189 nfsd_net_id);
6190
6191 if (!nfsd_netns_ready(nn))
6192 return 0;
6193
6194 spin_lock(&nn->client_lock);
6195 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6196 count += nfsd_print_client_delegations(clp);
6197 spin_unlock(&nn->client_lock);
6198
6199 return count;
6200}
6201
6202static void
6203nfsd_forget_delegations(struct list_head *reaplist)
6204{
6205 struct nfs4_client *clp;
6206 struct nfs4_delegation *dp, *next;
6207
6208 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6209 list_del_init(&dp->dl_recall_lru);
6210 clp = dp->dl_stid.sc_client;
6211 revoke_delegation(dp);
6212 put_client(clp);
6213 }
6214}
6215
6216u64
Jeff Layton285abde2014-07-30 08:27:24 -04006217nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6218 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006219{
6220 u64 count = 0;
6221 struct nfs4_client *clp;
6222 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6223 nfsd_net_id);
6224 LIST_HEAD(reaplist);
6225
6226 if (!nfsd_netns_ready(nn))
6227 return count;
6228
6229 spin_lock(&nn->client_lock);
6230 clp = nfsd_find_client(addr, addr_size);
6231 if (clp)
6232 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6233 spin_unlock(&nn->client_lock);
6234
6235 nfsd_forget_delegations(&reaplist);
6236 return count;
6237}
6238
6239u64
Jeff Layton285abde2014-07-30 08:27:24 -04006240nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006241{
6242 u64 count = 0;
6243 struct nfs4_client *clp;
6244 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6245 nfsd_net_id);
6246 LIST_HEAD(reaplist);
6247
6248 if (!nfsd_netns_ready(nn))
6249 return count;
6250
6251 spin_lock(&nn->client_lock);
6252 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6253 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6254 if (max != 0 && count >= max)
6255 break;
6256 }
6257 spin_unlock(&nn->client_lock);
6258 nfsd_forget_delegations(&reaplist);
6259 return count;
6260}
6261
6262static void
6263nfsd_recall_delegations(struct list_head *reaplist)
6264{
6265 struct nfs4_client *clp;
6266 struct nfs4_delegation *dp, *next;
6267
6268 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6269 list_del_init(&dp->dl_recall_lru);
6270 clp = dp->dl_stid.sc_client;
6271 /*
6272 * We skipped all entries that had a zero dl_time before,
6273 * so we can now reset the dl_time back to 0. If a delegation
6274 * break comes in now, then it won't make any difference since
6275 * we're recalling it either way.
6276 */
6277 spin_lock(&state_lock);
6278 dp->dl_time = 0;
6279 spin_unlock(&state_lock);
6280 nfsd_break_one_deleg(dp);
6281 put_client(clp);
6282 }
6283}
6284
6285u64
Jeff Layton285abde2014-07-30 08:27:24 -04006286nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006287 size_t addr_size)
6288{
6289 u64 count = 0;
6290 struct nfs4_client *clp;
6291 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6292 nfsd_net_id);
6293 LIST_HEAD(reaplist);
6294
6295 if (!nfsd_netns_ready(nn))
6296 return count;
6297
6298 spin_lock(&nn->client_lock);
6299 clp = nfsd_find_client(addr, addr_size);
6300 if (clp)
6301 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6302 spin_unlock(&nn->client_lock);
6303
6304 nfsd_recall_delegations(&reaplist);
6305 return count;
6306}
6307
6308u64
Jeff Layton285abde2014-07-30 08:27:24 -04006309nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006310{
6311 u64 count = 0;
6312 struct nfs4_client *clp, *next;
6313 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6314 nfsd_net_id);
6315 LIST_HEAD(reaplist);
6316
6317 if (!nfsd_netns_ready(nn))
6318 return count;
6319
6320 spin_lock(&nn->client_lock);
6321 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6322 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6323 if (max != 0 && ++count >= max)
6324 break;
6325 }
6326 spin_unlock(&nn->client_lock);
6327 nfsd_recall_delegations(&reaplist);
6328 return count;
6329}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006330#endif /* CONFIG_NFSD_FAULT_INJECTION */
6331
Meelap Shahc2f1a552007-07-17 04:04:39 -07006332/*
6333 * Since the lifetime of a delegation isn't limited to that of an open, a
6334 * client may quite reasonably hang on to a delegation as long as it has
6335 * the inode cached. This becomes an obvious problem the first time a
6336 * client's inode cache approaches the size of the server's total memory.
6337 *
6338 * For now we avoid this problem by imposing a hard limit on the number
6339 * of delegations, which varies according to the server's memory size.
6340 */
6341static void
6342set_max_delegations(void)
6343{
6344 /*
6345 * Allow at most 4 delegations per megabyte of RAM. Quick
6346 * estimates suggest that in the worst case (where every delegation
6347 * is for a different inode), a delegation could take about 1.5K,
6348 * giving a worst case usage of about 6% of memory.
6349 */
6350 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6351}
6352
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006353static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006354{
6355 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6356 int i;
6357
6358 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6359 CLIENT_HASH_SIZE, GFP_KERNEL);
6360 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006361 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006362 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6363 CLIENT_HASH_SIZE, GFP_KERNEL);
6364 if (!nn->unconf_id_hashtbl)
6365 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006366 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6367 SESSION_HASH_SIZE, GFP_KERNEL);
6368 if (!nn->sessionid_hashtbl)
6369 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006370
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006371 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006372 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006373 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006374 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006375 for (i = 0; i < SESSION_HASH_SIZE; i++)
6376 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006377 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006378 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03006379 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03006380 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006381 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03006382 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006383
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006384 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006385 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006386
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006387 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006388
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006389err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03006390 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006391err_unconf_id:
6392 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006393err:
6394 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006395}
6396
6397static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006398nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006399{
6400 int i;
6401 struct nfs4_client *clp = NULL;
6402 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6403
6404 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6405 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6406 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6407 destroy_client(clp);
6408 }
6409 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006410
Kinglong Mee2b905632014-03-26 22:09:30 +08006411 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6412 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6413 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6414 destroy_client(clp);
6415 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006416 }
6417
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006418 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006419 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006420 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006421 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006422}
6423
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006424int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006425nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006426{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006427 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006428 int ret;
6429
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006430 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006431 if (ret)
6432 return ret;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006433 nn->boot_time = get_seconds();
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006434 nn->grace_ended = false;
Jeff Laytond4318ac2014-09-12 16:40:21 -04006435 locks_start_grace(net, &nn->nfsd4_manager);
6436 nfsd4_client_tracking_init(net);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006437 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006438 nn->nfsd4_grace, net);
6439 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006440 return 0;
6441}
6442
6443/* initialization to perform when the nfsd service is started: */
6444
6445int
6446nfs4_state_start(void)
6447{
6448 int ret;
6449
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006450 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006451 if (ret)
6452 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07006453 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05006454 if (laundry_wq == NULL) {
6455 ret = -ENOMEM;
6456 goto out_recovery;
6457 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006458 ret = nfsd4_create_callback_queue();
6459 if (ret)
6460 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006461
Meelap Shahc2f1a552007-07-17 04:04:39 -07006462 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006463
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006464 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006465
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006466out_free_laundry:
6467 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006468out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006469 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470}
6471
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006472void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006473nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006477 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006479 cancel_delayed_work_sync(&nn->laundromat_work);
6480 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006481
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04006483 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006484 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006486 unhash_delegation_locked(dp);
6487 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488 }
Benny Halevycdc97502014-05-30 09:09:30 -04006489 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490 list_for_each_safe(pos, next, &reaplist) {
6491 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006492 list_del_init(&dp->dl_recall_lru);
Jeff Laytonafbda402014-08-09 10:22:41 -04006493 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04006494 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495 }
6496
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006497 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006498 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499}
6500
6501void
6502nfs4_state_shutdown(void)
6503{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006504 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006505 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006507
6508static void
6509get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6510{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006511 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6512 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006513}
6514
6515static void
6516put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6517{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006518 if (cstate->minorversion) {
6519 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6520 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6521 }
6522}
6523
6524void
6525clear_current_stateid(struct nfsd4_compound_state *cstate)
6526{
6527 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006528}
6529
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006530/*
6531 * functions to set current state id
6532 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006533void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006534nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6535{
6536 put_stateid(cstate, &odp->od_stateid);
6537}
6538
6539void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006540nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6541{
6542 put_stateid(cstate, &open->op_stateid);
6543}
6544
6545void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006546nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6547{
6548 put_stateid(cstate, &close->cl_stateid);
6549}
6550
6551void
6552nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6553{
6554 put_stateid(cstate, &lock->lk_resp_stateid);
6555}
6556
6557/*
6558 * functions to consume current state id
6559 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006560
6561void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006562nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6563{
6564 get_stateid(cstate, &odp->od_stateid);
6565}
6566
6567void
6568nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6569{
6570 get_stateid(cstate, &drp->dr_stateid);
6571}
6572
6573void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006574nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6575{
6576 get_stateid(cstate, &fsp->fr_stateid);
6577}
6578
6579void
6580nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6581{
6582 get_stateid(cstate, &setattr->sa_stateid);
6583}
6584
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006585void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006586nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6587{
6588 get_stateid(cstate, &close->cl_stateid);
6589}
6590
6591void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006592nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006593{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006594 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006595}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006596
6597void
6598nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6599{
6600 get_stateid(cstate, &read->rd_stateid);
6601}
6602
6603void
6604nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6605{
6606 get_stateid(cstate, &write->wr_stateid);
6607}