blob: b421b51c3a9e0c07a74b94bb326fe62767daf420 [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>
NeilBrown6282cd52014-06-04 17:39:26 +100044#include <linux/hash.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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
J. Bruce Fields8b671b82009-02-22 14:51:34 -080075/* Locking: */
76
77/* Currently used for almost all code touching nfsv4 state: */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080078static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
J. Bruce Fields8b671b82009-02-22 14:51:34 -080080/*
81 * Currently used for the del_recall_lru and file hash table. In an
82 * effort to decrease the scope of the client_mutex, this spinlock may
83 * eventually cover more:
84 */
Benny Halevycdc97502014-05-30 09:09:30 -040085static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080086
Christoph Hellwigabf11352014-05-21 07:43:03 -070087static struct kmem_cache *openowner_slab;
88static struct kmem_cache *lockowner_slab;
89static struct kmem_cache *file_slab;
90static struct kmem_cache *stateid_slab;
91static struct kmem_cache *deleg_slab;
NeilBrowne60d4392005-06-23 22:03:01 -070092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093void
94nfs4_lock_state(void)
95{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080096 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -040099static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800100
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114void
115nfs4_unlock_state(void)
116{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800117 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
J. Bruce Fields221a6872013-04-01 22:23:49 -0400120static bool is_client_expired(struct nfs4_client *clp)
121{
122 return clp->cl_time == 0;
123}
124
125static __be32 mark_client_expired_locked(struct nfs4_client *clp)
126{
127 if (atomic_read(&clp->cl_refcount))
128 return nfserr_jukebox;
129 clp->cl_time = 0;
130 return nfs_ok;
131}
132
133static __be32 mark_client_expired(struct nfs4_client *clp)
134{
135 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
136 __be32 ret;
137
138 spin_lock(&nn->client_lock);
139 ret = mark_client_expired_locked(clp);
140 spin_unlock(&nn->client_lock);
141 return ret;
142}
143
144static __be32 get_client_locked(struct nfs4_client *clp)
145{
146 if (is_client_expired(clp))
147 return nfserr_expired;
148 atomic_inc(&clp->cl_refcount);
149 return nfs_ok;
150}
151
152/* must be called under the client_lock */
153static inline void
154renew_client_locked(struct nfs4_client *clp)
155{
156 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
157
158 if (is_client_expired(clp)) {
159 WARN_ON(1);
160 printk("%s: client (clientid %08x/%08x) already expired\n",
161 __func__,
162 clp->cl_clientid.cl_boot,
163 clp->cl_clientid.cl_id);
164 return;
165 }
166
167 dprintk("renewing client (clientid %08x/%08x)\n",
168 clp->cl_clientid.cl_boot,
169 clp->cl_clientid.cl_id);
170 list_move_tail(&clp->cl_lru, &nn->client_lru);
171 clp->cl_time = get_seconds();
172}
173
174static inline void
175renew_client(struct nfs4_client *clp)
176{
177 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
178
179 spin_lock(&nn->client_lock);
180 renew_client_locked(clp);
181 spin_unlock(&nn->client_lock);
182}
183
Fengguang Wuba138432013-04-16 22:14:15 -0400184static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400185{
186 if (!atomic_dec_and_test(&clp->cl_refcount))
187 return;
188 if (!is_client_expired(clp))
189 renew_client_locked(clp);
190}
191
Jeff Layton4b24ca72014-06-30 11:48:44 -0400192static void put_client_renew(struct nfs4_client *clp)
193{
194 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
195
Jeff Laytond6c249b2014-07-08 14:02:50 -0400196 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
197 return;
198 if (!is_client_expired(clp))
199 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400200 spin_unlock(&nn->client_lock);
201}
202
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400203static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
204{
205 __be32 status;
206
207 if (is_session_dead(ses))
208 return nfserr_badsession;
209 status = get_client_locked(ses->se_client);
210 if (status)
211 return status;
212 atomic_inc(&ses->se_ref);
213 return nfs_ok;
214}
215
216static void nfsd4_put_session_locked(struct nfsd4_session *ses)
217{
218 struct nfs4_client *clp = ses->se_client;
219
220 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
221 free_session(ses);
222 put_client_renew_locked(clp);
223}
224
225static void nfsd4_put_session(struct nfsd4_session *ses)
226{
227 struct nfs4_client *clp = ses->se_client;
228 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
229
230 spin_lock(&nn->client_lock);
231 nfsd4_put_session_locked(ses);
232 spin_unlock(&nn->client_lock);
233}
234
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236static inline u32
237opaque_hashval(const void *ptr, int nbytes)
238{
239 unsigned char *cptr = (unsigned char *) ptr;
240
241 u32 x = 0;
242 while (nbytes--) {
243 x *= 37;
244 x += *cptr++;
245 }
246 return x;
247}
248
J. Bruce Fields32513b42011-10-13 16:00:16 -0400249static void nfsd4_free_file(struct nfs4_file *f)
250{
251 kmem_cache_free(file_slab, f);
252}
253
NeilBrown13cd2182005-06-23 22:03:10 -0700254static inline void
255put_nfs4_file(struct nfs4_file *fi)
256{
Jeff Layton02e12152014-07-16 10:31:57 -0400257 might_lock(&state_lock);
258
Benny Halevycdc97502014-05-30 09:09:30 -0400259 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton89876f82013-04-02 09:01:59 -0400260 hlist_del(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400261 spin_unlock(&state_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400262 nfsd4_free_file(fi);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800263 }
NeilBrown13cd2182005-06-23 22:03:10 -0700264}
265
266static inline void
267get_nfs4_file(struct nfs4_file *fi)
268{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800269 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700270}
271
Trond Myklebustde186432014-07-10 14:07:26 -0400272static struct file *
273__nfs4_get_fd(struct nfs4_file *f, int oflag)
274{
275 if (f->fi_fds[oflag])
276 return get_file(f->fi_fds[oflag]);
277 return NULL;
278}
279
280static struct file *
281find_writeable_file_locked(struct nfs4_file *f)
282{
283 struct file *ret;
284
285 lockdep_assert_held(&f->fi_lock);
286
287 ret = __nfs4_get_fd(f, O_WRONLY);
288 if (!ret)
289 ret = __nfs4_get_fd(f, O_RDWR);
290 return ret;
291}
292
293static struct file *
294find_writeable_file(struct nfs4_file *f)
295{
296 struct file *ret;
297
298 spin_lock(&f->fi_lock);
299 ret = find_writeable_file_locked(f);
300 spin_unlock(&f->fi_lock);
301
302 return ret;
303}
304
305static struct file *find_readable_file_locked(struct nfs4_file *f)
306{
307 struct file *ret;
308
309 lockdep_assert_held(&f->fi_lock);
310
311 ret = __nfs4_get_fd(f, O_RDONLY);
312 if (!ret)
313 ret = __nfs4_get_fd(f, O_RDWR);
314 return ret;
315}
316
317static struct file *
318find_readable_file(struct nfs4_file *f)
319{
320 struct file *ret;
321
322 spin_lock(&f->fi_lock);
323 ret = find_readable_file_locked(f);
324 spin_unlock(&f->fi_lock);
325
326 return ret;
327}
328
329static struct file *
330find_any_file(struct nfs4_file *f)
331{
332 struct file *ret;
333
334 spin_lock(&f->fi_lock);
335 ret = __nfs4_get_fd(f, O_RDWR);
336 if (!ret) {
337 ret = __nfs4_get_fd(f, O_WRONLY);
338 if (!ret)
339 ret = __nfs4_get_fd(f, O_RDONLY);
340 }
341 spin_unlock(&f->fi_lock);
342 return ret;
343}
344
Trond Myklebust02a35082014-07-25 07:34:22 -0400345static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800346unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700347
348/*
349 * Open owner state (share locks)
350 */
351
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500352/* hash tables for lock and open owners */
353#define OWNER_HASH_BITS 8
354#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
355#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700356
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500357static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400358{
359 unsigned int ret;
360
361 ret = opaque_hashval(ownername->data, ownername->len);
362 ret += clientid;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500363 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400364}
NeilBrownef0f3392006-04-10 22:55:41 -0700365
NeilBrownef0f3392006-04-10 22:55:41 -0700366/* hash table for nfs4_file */
367#define FILE_HASH_BITS 8
368#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800369
Trond Myklebustca943212014-07-23 16:17:39 -0400370static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400371{
Trond Myklebustca943212014-07-23 16:17:39 -0400372 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
373}
374
375static unsigned int file_hashval(struct knfsd_fh *fh)
376{
377 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
378}
379
380static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
381{
382 return fh1->fh_size == fh2->fh_size &&
383 !memcmp(fh1->fh_base.fh_pad,
384 fh2->fh_base.fh_pad,
385 fh1->fh_size);
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400386}
387
Jeff Layton89876f82013-04-02 09:01:59 -0400388static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700389
Jeff Layton12659652014-07-10 14:07:28 -0400390static void
391__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400392{
Jeff Layton7214e862014-07-10 14:07:33 -0400393 lockdep_assert_held(&fp->fi_lock);
394
Jeff Layton12659652014-07-10 14:07:28 -0400395 if (access & NFS4_SHARE_ACCESS_WRITE)
396 atomic_inc(&fp->fi_access[O_WRONLY]);
397 if (access & NFS4_SHARE_ACCESS_READ)
398 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400399}
400
Jeff Layton12659652014-07-10 14:07:28 -0400401static __be32
402nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400403{
Jeff Layton7214e862014-07-10 14:07:33 -0400404 lockdep_assert_held(&fp->fi_lock);
405
Jeff Layton12659652014-07-10 14:07:28 -0400406 /* Does this access mode make sense? */
407 if (access & ~NFS4_SHARE_ACCESS_BOTH)
408 return nfserr_inval;
409
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400410 /* Does it conflict with a deny mode already set? */
411 if ((access & fp->fi_share_deny) != 0)
412 return nfserr_share_denied;
413
Jeff Layton12659652014-07-10 14:07:28 -0400414 __nfs4_file_get_access(fp, access);
415 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400416}
417
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400418static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
419{
420 /* Common case is that there is no deny mode. */
421 if (deny) {
422 /* Does this deny mode make sense? */
423 if (deny & ~NFS4_SHARE_DENY_BOTH)
424 return nfserr_inval;
425
426 if ((deny & NFS4_SHARE_DENY_READ) &&
427 atomic_read(&fp->fi_access[O_RDONLY]))
428 return nfserr_share_denied;
429
430 if ((deny & NFS4_SHARE_DENY_WRITE) &&
431 atomic_read(&fp->fi_access[O_WRONLY]))
432 return nfserr_share_denied;
433 }
434 return nfs_ok;
435}
436
J. Bruce Fields998db522010-08-07 09:21:41 -0400437static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400438{
Trond Myklebustde186432014-07-10 14:07:26 -0400439 might_lock(&fp->fi_lock);
440
441 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
442 struct file *f1 = NULL;
443 struct file *f2 = NULL;
444
Jeff Layton6d338b52014-07-10 14:07:29 -0400445 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400446 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400447 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400448 spin_unlock(&fp->fi_lock);
449 if (f1)
450 fput(f1);
451 if (f2)
452 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400453 }
454}
455
Jeff Layton12659652014-07-10 14:07:28 -0400456static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400457{
Jeff Layton12659652014-07-10 14:07:28 -0400458 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
459
460 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400461 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400462 if (access & NFS4_SHARE_ACCESS_READ)
463 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400464}
465
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500466static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct
467kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400468{
469 struct idr *stateids = &cl->cl_stateids;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500470 struct nfs4_stid *stid;
471 int new_id;
472
Trond Myklebustf8338832014-07-25 07:34:19 -0400473 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500474 if (!stid)
475 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400476
Jeff Layton398c33a2013-04-29 16:21:20 -0700477 new_id = idr_alloc_cyclic(stateids, stid, 0, 0, GFP_KERNEL);
Tejun Heoebd6c702013-03-13 14:59:37 -0700478 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500479 goto out_free;
480 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500481 stid->sc_stateid.si_opaque.so_id = new_id;
482 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
483 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400484 atomic_set(&stid->sc_count, 1);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500485
J. Bruce Fields996e0932011-10-17 11:14:48 -0400486 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500487 * It shouldn't be a problem to reuse an opaque stateid value.
488 * I don't think it is for 4.1. But with 4.0 I worry that, for
489 * example, a stray write retransmission could be accepted by
490 * the server when it should have been rejected. Therefore,
491 * adopt a trick from the sctp code to attempt to maximize the
492 * amount of time until an id is reused, by ensuring they always
493 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400494 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500495 return stid;
496out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800497 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500498 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400499}
500
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400501static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
502{
503 return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
504}
505
NeilBrown6282cd52014-06-04 17:39:26 +1000506/*
507 * When we recall a delegation, we should be careful not to hand it
508 * out again straight away.
509 * To ensure this we keep a pair of bloom filters ('new' and 'old')
510 * in which the filehandles of recalled delegations are "stored".
511 * If a filehandle appear in either filter, a delegation is blocked.
512 * When a delegation is recalled, the filehandle is stored in the "new"
513 * filter.
514 * Every 30 seconds we swap the filters and clear the "new" one,
515 * unless both are empty of course.
516 *
517 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
518 * low 3 bytes as hash-table indices.
519 *
520 * 'state_lock', which is always held when block_delegations() is called,
521 * is used to manage concurrent access. Testing does not need the lock
522 * except when swapping the two filters.
523 */
524static struct bloom_pair {
525 int entries, old_entries;
526 time_t swap_time;
527 int new; /* index into 'set' */
528 DECLARE_BITMAP(set[2], 256);
529} blocked_delegations;
530
531static int delegation_blocked(struct knfsd_fh *fh)
532{
533 u32 hash;
534 struct bloom_pair *bd = &blocked_delegations;
535
536 if (bd->entries == 0)
537 return 0;
538 if (seconds_since_boot() - bd->swap_time > 30) {
539 spin_lock(&state_lock);
540 if (seconds_since_boot() - bd->swap_time > 30) {
541 bd->entries -= bd->old_entries;
542 bd->old_entries = bd->entries;
543 memset(bd->set[bd->new], 0,
544 sizeof(bd->set[0]));
545 bd->new = 1-bd->new;
546 bd->swap_time = seconds_since_boot();
547 }
548 spin_unlock(&state_lock);
549 }
550 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
551 if (test_bit(hash&255, bd->set[0]) &&
552 test_bit((hash>>8)&255, bd->set[0]) &&
553 test_bit((hash>>16)&255, bd->set[0]))
554 return 1;
555
556 if (test_bit(hash&255, bd->set[1]) &&
557 test_bit((hash>>8)&255, bd->set[1]) &&
558 test_bit((hash>>16)&255, bd->set[1]))
559 return 1;
560
561 return 0;
562}
563
564static void block_delegations(struct knfsd_fh *fh)
565{
566 u32 hash;
567 struct bloom_pair *bd = &blocked_delegations;
568
Jeff Layton02e12152014-07-16 10:31:57 -0400569 lockdep_assert_held(&state_lock);
570
NeilBrown6282cd52014-06-04 17:39:26 +1000571 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
572
573 __set_bit(hash&255, bd->set[bd->new]);
574 __set_bit((hash>>8)&255, bd->set[bd->new]);
575 __set_bit((hash>>16)&255, bd->set[bd->new]);
576 if (bd->entries == 0)
577 bd->swap_time = seconds_since_boot();
578 bd->entries += 1;
579}
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581static struct nfs4_delegation *
J. Bruce Fields99c41512013-05-21 16:21:25 -0400582alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400585 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400588 n = atomic_long_inc_return(&num_delegations);
589 if (n < 0 || n > max_delegations)
590 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000591 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400592 goto out_dec;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400593 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700594 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400595 goto out_dec;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400596 /*
597 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400598 * meaning of seqid 0 isn't meaningful, really, but let's avoid
599 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400600 */
601 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700602 INIT_LIST_HEAD(&dp->dl_perfile);
603 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400605 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
J. Bruce Fields6c02eaa2009-02-02 17:30:51 -0500606 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
Jeff Layton02e12152014-07-16 10:31:57 -0400607 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400609out_dec:
610 atomic_long_dec(&num_delegations);
611 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
J. Bruce Fields68a33962013-03-21 11:21:50 -0400614static void remove_stid(struct nfs4_stid *s)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500615{
616 struct idr *stateids = &s->sc_client->cl_stateids;
617
618 idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500619}
620
Benny Halevy9857df82013-10-14 13:44:52 +0300621static void nfs4_free_stid(struct kmem_cache *slab, struct nfs4_stid *s)
622{
623 kmem_cache_free(slab, s);
624}
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626void
627nfs4_put_delegation(struct nfs4_delegation *dp)
628{
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400629 if (atomic_dec_and_test(&dp->dl_stid.sc_count)) {
Trond Myklebustf8338832014-07-25 07:34:19 -0400630 if (dp->dl_file)
631 put_nfs4_file(dp->dl_file);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400632 remove_stid(&dp->dl_stid);
Benny Halevy9857df82013-10-14 13:44:52 +0300633 nfs4_free_stid(deleg_slab, &dp->dl_stid);
Trond Myklebust02a35082014-07-25 07:34:22 -0400634 atomic_long_dec(&num_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636}
637
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500638static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Jeff Layton417c6622014-07-21 09:34:57 -0400640 lockdep_assert_held(&state_lock);
641
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -0500642 if (!fp->fi_lease)
643 return;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500644 if (atomic_dec_and_test(&fp->fi_delegees)) {
645 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
646 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400647 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500648 fp->fi_deleg_file = NULL;
649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400652static void unhash_stid(struct nfs4_stid *s)
653{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500654 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400655}
656
Benny Halevy931ee562014-05-30 09:09:27 -0400657static void
658hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
659{
Benny Halevycdc97502014-05-30 09:09:30 -0400660 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400661 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400662
Benny Halevy3fb87d12014-05-30 09:09:31 -0400663 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400664 list_add(&dp->dl_perfile, &fp->fi_delegations);
665 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
666}
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668static void
Jeff Layton42690672014-07-25 07:34:20 -0400669unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Jeff Layton02e12152014-07-16 10:31:57 -0400671 struct nfs4_file *fp = dp->dl_file;
672
Jeff Layton42690672014-07-25 07:34:20 -0400673 lockdep_assert_held(&state_lock);
674
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400675 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400676 /* Ensure that deleg break won't try to requeue it */
677 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400678 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400679 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400681 list_del_init(&dp->dl_perfile);
682 spin_unlock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400683 if (fp)
Trond Myklebustf8338832014-07-25 07:34:19 -0400684 nfs4_put_deleg_lease(fp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400685}
686
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400687static void destroy_delegation(struct nfs4_delegation *dp)
688{
Jeff Layton42690672014-07-25 07:34:20 -0400689 spin_lock(&state_lock);
690 unhash_delegation_locked(dp);
691 spin_unlock(&state_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400692 nfs4_put_delegation(dp);
693}
694
695static void revoke_delegation(struct nfs4_delegation *dp)
696{
697 struct nfs4_client *clp = dp->dl_stid.sc_client;
698
Jeff Layton2d4a5322014-07-25 07:34:21 -0400699 WARN_ON(!list_empty(&dp->dl_recall_lru));
700
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400701 if (clp->cl_minorversion == 0)
Jeff Layton2d4a5322014-07-25 07:34:21 -0400702 nfs4_put_delegation(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400703 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400704 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400705 spin_lock(&clp->cl_lock);
706 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
707 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400708 }
709}
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711/*
712 * SETCLIENTID state
713 */
714
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400715static unsigned int clientid_hashval(u32 id)
716{
717 return id & CLIENT_HASH_MASK;
718}
719
720static unsigned int clientstr_hashval(const char *name)
721{
722 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
723}
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400726 * We store the NONE, READ, WRITE, and BOTH bits separately in the
727 * st_{access,deny}_bmap field of the stateid, in order to track not
728 * only what share bits are currently in force, but also what
729 * combinations of share bits previous opens have used. This allows us
730 * to enforce the recommendation of rfc 3530 14.2.19 that the server
731 * return an error if the client attempt to downgrade to a combination
732 * of share bits not explicable by closing some of its previous opens.
733 *
734 * XXX: This enforcement is actually incomplete, since we don't keep
735 * track of access/deny bit combinations; so, e.g., we allow:
736 *
737 * OPEN allow read, deny write
738 * OPEN allow both, deny none
739 * DOWNGRADE allow read, deny none
740 *
741 * which we should reject.
742 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400743static unsigned int
744bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400745 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400746 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400747
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400748 for (i = 1; i < 4; i++) {
749 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400750 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400751 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400752 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400753}
754
Jeff Layton82c5ff12012-05-11 09:45:13 -0400755/* set share access for a given stateid */
756static inline void
757set_access(u32 access, struct nfs4_ol_stateid *stp)
758{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400759 unsigned char mask = 1 << access;
760
761 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
762 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400763}
764
765/* clear share access for a given stateid */
766static inline void
767clear_access(u32 access, struct nfs4_ol_stateid *stp)
768{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400769 unsigned char mask = 1 << access;
770
771 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
772 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400773}
774
775/* test whether a given stateid has access */
776static inline bool
777test_access(u32 access, struct nfs4_ol_stateid *stp)
778{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400779 unsigned char mask = 1 << access;
780
781 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400782}
783
Jeff Laytonce0fc432012-05-11 09:45:14 -0400784/* set share deny for a given stateid */
785static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400786set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400787{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400788 unsigned char mask = 1 << deny;
789
790 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
791 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400792}
793
794/* clear share deny for a given stateid */
795static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400796clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400797{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400798 unsigned char mask = 1 << deny;
799
800 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
801 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400802}
803
804/* test whether a given stateid is denying specific access */
805static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400806test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400807{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400808 unsigned char mask = 1 << deny;
809
810 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400811}
812
813static int nfs4_access_to_omode(u32 access)
814{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400815 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400816 case NFS4_SHARE_ACCESS_READ:
817 return O_RDONLY;
818 case NFS4_SHARE_ACCESS_WRITE:
819 return O_WRONLY;
820 case NFS4_SHARE_ACCESS_BOTH:
821 return O_RDWR;
822 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500823 WARN_ON_ONCE(1);
824 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400825}
826
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400827/*
828 * A stateid that had a deny mode associated with it is being released
829 * or downgraded. Recalculate the deny mode on the file.
830 */
831static void
832recalculate_deny_mode(struct nfs4_file *fp)
833{
834 struct nfs4_ol_stateid *stp;
835
836 spin_lock(&fp->fi_lock);
837 fp->fi_share_deny = 0;
838 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
839 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
840 spin_unlock(&fp->fi_lock);
841}
842
843static void
844reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
845{
846 int i;
847 bool change = false;
848
849 for (i = 1; i < 4; i++) {
850 if ((i & deny) != i) {
851 change = true;
852 clear_deny(i, stp);
853 }
854 }
855
856 /* Recalculate per-file deny mode if there was a change */
857 if (change)
858 recalculate_deny_mode(stp->st_file);
859}
860
Jeff Layton82c5ff12012-05-11 09:45:13 -0400861/* release all access and file references for a given stateid */
862static void
863release_all_access(struct nfs4_ol_stateid *stp)
864{
865 int i;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400866 struct nfs4_file *fp = stp->st_file;
867
868 if (fp && stp->st_deny_bmap != 0)
869 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400870
871 for (i = 1; i < 4; i++) {
872 if (test_access(i, stp))
Jeff Layton12659652014-07-10 14:07:28 -0400873 nfs4_file_put_access(stp->st_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400874 clear_access(i, stp);
875 }
876}
877
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400878static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500879{
Trond Myklebust1d31a252014-07-10 14:07:25 -0400880 struct nfs4_file *fp = stp->st_file;
881
882 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500883 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400884 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500885 list_del(&stp->st_perstateowner);
886}
887
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400888static void close_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500889{
Jeff Layton82c5ff12012-05-11 09:45:13 -0400890 release_all_access(stp);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400891}
892
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400893static void free_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400894{
Trond Myklebustf8338832014-07-25 07:34:19 -0400895 if (stp->st_file)
896 put_nfs4_file(stp->st_file);
J. Bruce Fields68a33962013-03-21 11:21:50 -0400897 remove_stid(&stp->st_stid);
Benny Halevy9857df82013-10-14 13:44:52 +0300898 nfs4_free_stid(stateid_slab, &stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500899}
900
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400901static void __release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500902{
903 struct file *file;
904
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400905 list_del(&stp->st_locks);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500906 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400907 unhash_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500908 file = find_any_file(stp->st_file);
Trond Myklebuste20fcf12014-07-10 14:07:27 -0400909 if (file)
910 filp_close(file, (fl_owner_t)lockowner(stp->st_stateowner));
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400911 close_generic_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500912 free_generic_stateid(stp);
913}
914
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400915static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500916{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400917 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500918
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400919 list_del(&lo->lo_owner.so_strhash);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400920 while (!list_empty(&lo->lo_owner.so_stateids)) {
921 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400922 struct nfs4_ol_stateid, st_perstateowner);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400923 __release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500924 }
925}
926
Trond Myklebust50cc6232014-04-18 14:44:03 -0400927static void nfs4_free_lockowner(struct nfs4_lockowner *lo)
928{
929 kfree(lo->lo_owner.so_owner.data);
930 kmem_cache_free(lockowner_slab, lo);
931}
932
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400933static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500934{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400935 unhash_lockowner(lo);
936 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500937}
938
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400939static void release_lockowner_if_empty(struct nfs4_lockowner *lo)
940{
941 if (list_empty(&lo->lo_owner.so_stateids))
942 release_lockowner(lo);
943}
944
945static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500946{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400947 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500948
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400949 lo = lockowner(stp->st_stateowner);
950 __release_lock_stateid(stp);
951 release_lockowner_if_empty(lo);
952}
953
954static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
955{
956 struct nfs4_ol_stateid *stp;
957
958 while (!list_empty(&open_stp->st_locks)) {
959 stp = list_entry(open_stp->st_locks.next,
960 struct nfs4_ol_stateid, st_locks);
961 release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500962 }
963}
964
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400965static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500966{
967 unhash_generic_stateid(stp);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400968 release_open_stateid_locks(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400969 close_generic_stateid(stp);
970}
971
972static void release_open_stateid(struct nfs4_ol_stateid *stp)
973{
974 unhash_open_stateid(stp);
J. Bruce Fields22839632009-01-11 14:27:17 -0500975 free_generic_stateid(stp);
976}
977
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400978static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500979{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400980 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500981
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400982 list_del(&oo->oo_owner.so_strhash);
983 list_del(&oo->oo_perclient);
984 while (!list_empty(&oo->oo_owner.so_stateids)) {
985 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400986 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500987 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500988 }
989}
990
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400991static void release_last_closed_stateid(struct nfs4_openowner *oo)
992{
993 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
994
995 if (s) {
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400996 free_generic_stateid(s);
997 oo->oo_last_closed_stid = NULL;
998 }
999}
1000
Trond Myklebust50cc6232014-04-18 14:44:03 -04001001static void nfs4_free_openowner(struct nfs4_openowner *oo)
1002{
1003 kfree(oo->oo_owner.so_owner.data);
1004 kmem_cache_free(openowner_slab, oo);
1005}
1006
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001007static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001008{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001009 unhash_openowner(oo);
1010 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001011 release_last_closed_stateid(oo);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001012 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001013}
1014
Marc Eshel5282fd72009-04-03 08:27:52 +03001015static inline int
1016hash_sessionid(struct nfs4_sessionid *sessionid)
1017{
1018 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1019
1020 return sid->sequence % SESSION_HASH_SIZE;
1021}
1022
Trond Myklebust8f199b82012-03-20 15:11:17 -04001023#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001024static inline void
1025dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1026{
1027 u32 *ptr = (u32 *)(&sessionid->data[0]);
1028 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1029}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001030#else
1031static inline void
1032dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1033{
1034}
1035#endif
1036
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001037/*
1038 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1039 * won't be used for replay.
1040 */
1041void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1042{
1043 struct nfs4_stateowner *so = cstate->replay_owner;
1044
1045 if (nfserr == nfserr_replay_me)
1046 return;
1047
1048 if (!seqid_mutating_err(ntohl(nfserr))) {
1049 cstate->replay_owner = NULL;
1050 return;
1051 }
1052 if (!so)
1053 return;
1054 if (so->so_is_open_owner)
1055 release_last_closed_stateid(openowner(so));
1056 so->so_seqid++;
1057 return;
1058}
Marc Eshel5282fd72009-04-03 08:27:52 +03001059
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001060static void
1061gen_sessionid(struct nfsd4_session *ses)
1062{
1063 struct nfs4_client *clp = ses->se_client;
1064 struct nfsd4_sessionid *sid;
1065
1066 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1067 sid->clientid = clp->cl_clientid;
1068 sid->sequence = current_sessionid++;
1069 sid->reserved = 0;
1070}
1071
1072/*
Andy Adamsona6496372009-08-28 08:45:01 -04001073 * The protocol defines ca_maxresponssize_cached to include the size of
1074 * the rpc header, but all we need to cache is the data starting after
1075 * the end of the initial SEQUENCE operation--the rest we regenerate
1076 * each time. Therefore we can advertise a ca_maxresponssize_cached
1077 * value that is the number of bytes in our cache plus a few additional
1078 * bytes. In order to stay on the safe side, and not promise more than
1079 * we can cache, those additional bytes must be the minimum possible: 24
1080 * bytes of rpc header (xid through accept state, with AUTH_NULL
1081 * verifier), 12 for the compound header (with zero-length tag), and 44
1082 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001083 */
Andy Adamsona6496372009-08-28 08:45:01 -04001084#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1085
Andy Adamson557ce262009-08-28 08:45:04 -04001086static void
1087free_session_slots(struct nfsd4_session *ses)
1088{
1089 int i;
1090
1091 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1092 kfree(ses->se_slots[i]);
1093}
1094
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001095/*
1096 * We don't actually need to cache the rpc and session headers, so we
1097 * can allocate a little less for each slot:
1098 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001099static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001100{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001101 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001102
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001103 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1104 size = 0;
1105 else
1106 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1107 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001108}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001109
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001110/*
1111 * XXX: If we run out of reserved DRC memory we could (up to a point)
1112 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001113 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001114 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001115static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001116{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001117 u32 slotsize = slot_bytes(ca);
1118 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001119 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001120
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001121 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001122 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1123 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001124 num = min_t(int, num, avail / slotsize);
1125 nfsd_drc_mem_used += num * slotsize;
1126 spin_unlock(&nfsd_drc_lock);
1127
1128 return num;
1129}
1130
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001131static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001132{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001133 int slotsize = slot_bytes(ca);
1134
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001135 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001136 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001137 spin_unlock(&nfsd_drc_lock);
1138}
1139
Kinglong Mee60810e52014-01-01 00:35:47 +08001140static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1141 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001142{
Kinglong Mee60810e52014-01-01 00:35:47 +08001143 int numslots = fattrs->maxreqs;
1144 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001145 struct nfsd4_session *new;
1146 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001147
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001148 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001149 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1150 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001151
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001152 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001153 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001154 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001155 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001156 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001157 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001158 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001159 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001160 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001161
1162 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1163 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1164
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001165 return new;
1166out_free:
1167 while (i--)
1168 kfree(new->se_slots[i]);
1169 kfree(new);
1170 return NULL;
1171}
1172
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001173static void free_conn(struct nfsd4_conn *c)
1174{
1175 svc_xprt_put(c->cn_xprt);
1176 kfree(c);
1177}
1178
1179static void nfsd4_conn_lost(struct svc_xpt_user *u)
1180{
1181 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1182 struct nfs4_client *clp = c->cn_session->se_client;
1183
1184 spin_lock(&clp->cl_lock);
1185 if (!list_empty(&c->cn_persession)) {
1186 list_del(&c->cn_persession);
1187 free_conn(c);
1188 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001189 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001190 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001191}
1192
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001193static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001194{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001195 struct nfsd4_conn *conn;
1196
1197 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1198 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001199 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001200 svc_xprt_get(rqstp->rq_xprt);
1201 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001202 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001203 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1204 return conn;
1205}
1206
J. Bruce Fields328ead22010-09-29 16:11:06 -04001207static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1208{
1209 conn->cn_session = ses;
1210 list_add(&conn->cn_persession, &ses->se_conns);
1211}
1212
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001213static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1214{
1215 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001216
1217 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001218 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001219 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001220}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001221
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001222static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001223{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001224 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001225 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001226}
1227
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001228static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001229{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001230 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001231
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001232 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001233 ret = nfsd4_register_conn(conn);
1234 if (ret)
1235 /* oops; xprt is already down: */
1236 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001237 /* We may have gained or lost a callback channel: */
1238 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001239}
1240
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001241static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001242{
1243 u32 dir = NFS4_CDFC4_FORE;
1244
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001245 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001246 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001247 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001248}
1249
1250/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001251static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001252{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001253 struct nfs4_client *clp = s->se_client;
1254 struct nfsd4_conn *c;
1255
1256 spin_lock(&clp->cl_lock);
1257 while (!list_empty(&s->se_conns)) {
1258 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1259 list_del_init(&c->cn_persession);
1260 spin_unlock(&clp->cl_lock);
1261
1262 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1263 free_conn(c);
1264
1265 spin_lock(&clp->cl_lock);
1266 }
1267 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001268}
1269
J. Bruce Fields1377b692012-09-11 21:42:40 -04001270static void __free_session(struct nfsd4_session *ses)
1271{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001272 free_session_slots(ses);
1273 kfree(ses);
1274}
1275
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001276static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001277{
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001278 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1279
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001280 lockdep_assert_held(&nn->client_lock);
1281 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001282 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001283 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001284}
Andy Adamson557ce262009-08-28 08:45:04 -04001285
Fengguang Wu135ae822012-11-10 07:20:25 -05001286static 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 -04001287{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001288 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001289 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001290
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001291 new->se_client = clp;
1292 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001293
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001294 INIT_LIST_HEAD(&new->se_conns);
1295
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001296 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001297 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001298 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001299 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001300 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001301 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001302 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001303 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001304 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001305 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001306 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001307 spin_unlock(&nn->client_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001308
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001309 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001310 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001311 /*
1312 * This is a little silly; with sessions there's no real
1313 * use for the callback address. Use the peer address
1314 * as a reasonable default for now, but consider fixing
1315 * the rpc client not to require an address in the
1316 * future:
1317 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001318 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1319 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001320 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001321}
1322
Benny Halevy9089f1b2010-05-12 00:12:26 +03001323/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001324static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001325__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001326{
1327 struct nfsd4_session *elem;
1328 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001329 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001330
1331 dump_sessionid(__func__, sessionid);
1332 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001333 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001334 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001335 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1336 NFS4_MAX_SESSIONID_LEN)) {
1337 return elem;
1338 }
1339 }
1340
1341 dprintk("%s: session not found\n", __func__);
1342 return NULL;
1343}
1344
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001345static struct nfsd4_session *
1346find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1347 __be32 *ret)
1348{
1349 struct nfsd4_session *session;
1350 __be32 status = nfserr_badsession;
1351
1352 session = __find_in_sessionid_hashtbl(sessionid, net);
1353 if (!session)
1354 goto out;
1355 status = nfsd4_get_session_locked(session);
1356 if (status)
1357 session = NULL;
1358out:
1359 *ret = status;
1360 return session;
1361}
1362
Benny Halevy9089f1b2010-05-12 00:12:26 +03001363/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001364static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001365unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001366{
1367 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001368 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001369 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001370 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001371}
1372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1374static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001375STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001377 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001379 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001380 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return 1;
1382}
1383
1384/*
1385 * XXX Should we use a slab cache ?
1386 * This type of memory management is somewhat inefficient, but we use it
1387 * anyway since SETCLIENTID is not a common operation.
1388 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001389static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 struct nfs4_client *clp;
1392
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001393 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1394 if (clp == NULL)
1395 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001396 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001397 if (clp->cl_name.data == NULL) {
1398 kfree(clp);
1399 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001401 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001402 INIT_LIST_HEAD(&clp->cl_sessions);
1403 idr_init(&clp->cl_stateids);
1404 atomic_set(&clp->cl_refcount, 0);
1405 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1406 INIT_LIST_HEAD(&clp->cl_idhash);
1407 INIT_LIST_HEAD(&clp->cl_openowners);
1408 INIT_LIST_HEAD(&clp->cl_delegations);
1409 INIT_LIST_HEAD(&clp->cl_lru);
1410 INIT_LIST_HEAD(&clp->cl_callbacks);
1411 INIT_LIST_HEAD(&clp->cl_revoked);
1412 spin_lock_init(&clp->cl_lock);
1413 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return clp;
1415}
1416
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001417static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418free_client(struct nfs4_client *clp)
1419{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001420 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001421
1422 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001423 while (!list_empty(&clp->cl_sessions)) {
1424 struct nfsd4_session *ses;
1425 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1426 se_perclnt);
1427 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001428 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1429 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001430 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001431 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001432 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001434 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 kfree(clp);
1436}
1437
Benny Halevy84d38ac2010-05-12 00:13:16 +03001438/* must be called under the client_lock */
1439static inline void
1440unhash_client_locked(struct nfs4_client *clp)
1441{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001442 struct nfsd4_session *ses;
1443
Benny Halevy84d38ac2010-05-12 00:13:16 +03001444 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001445 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001446 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1447 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001448 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001449}
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001452destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001454 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001457 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001460 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001461 while (!list_empty(&clp->cl_delegations)) {
1462 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001463 unhash_delegation_locked(dp);
1464 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
Benny Halevycdc97502014-05-30 09:09:30 -04001466 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 while (!list_empty(&reaplist)) {
1468 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001469 list_del_init(&dp->dl_recall_lru);
1470 nfs4_put_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001472 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001473 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001474 list_del_init(&dp->dl_recall_lru);
1475 nfs4_put_delegation(dp);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001476 }
NeilBrownea1da632005-06-23 22:04:17 -07001477 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001478 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1479 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001481 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001482 if (clp->cl_cb_conn.cb_xprt)
1483 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001484 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001485 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001486 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001487 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001488 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001489 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001490 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001491 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1492 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001493 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001496static void expire_client(struct nfs4_client *clp)
1497{
1498 nfsd4_client_record_remove(clp);
1499 destroy_client(clp);
1500}
1501
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001502static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1503{
1504 memcpy(target->cl_verifier.data, source->data,
1505 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506}
1507
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001508static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1509{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1511 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1512}
1513
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001514static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001515{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001516 if (source->cr_principal) {
1517 target->cr_principal =
1518 kstrdup(source->cr_principal, GFP_KERNEL);
1519 if (target->cr_principal == NULL)
1520 return -ENOMEM;
1521 } else
1522 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001523 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 target->cr_uid = source->cr_uid;
1525 target->cr_gid = source->cr_gid;
1526 target->cr_group_info = source->cr_group_info;
1527 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001528 target->cr_gss_mech = source->cr_gss_mech;
1529 if (source->cr_gss_mech)
1530 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001531 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001534static long long
1535compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1536{
1537 long long res;
1538
1539 res = o1->len - o2->len;
1540 if (res)
1541 return res;
1542 return (long long)memcmp(o1->data, o2->data, o1->len);
1543}
1544
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001545static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001546{
NeilBrowna55370a2005-06-23 22:03:52 -07001547 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
1550static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001551same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1552{
1553 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
1555
1556static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001557same_clid(clientid_t *cl1, clientid_t *cl2)
1558{
1559 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001562static bool groups_equal(struct group_info *g1, struct group_info *g2)
1563{
1564 int i;
1565
1566 if (g1->ngroups != g2->ngroups)
1567 return false;
1568 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001569 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001570 return false;
1571 return true;
1572}
1573
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001574/*
1575 * RFC 3530 language requires clid_inuse be returned when the
1576 * "principal" associated with a requests differs from that previously
1577 * used. We use uid, gid's, and gss principal string as our best
1578 * approximation. We also don't want to allow non-gss use of a client
1579 * established using gss: in theory cr_principal should catch that
1580 * change, but in practice cr_principal can be null even in the gss case
1581 * since gssd doesn't always pass down a principal string.
1582 */
1583static bool is_gss_cred(struct svc_cred *cr)
1584{
1585 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1586 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1587}
1588
1589
Vivek Trivedi5559b502012-07-24 21:18:20 +05301590static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001591same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1592{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001593 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001594 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1595 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001596 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1597 return false;
1598 if (cr1->cr_principal == cr2->cr_principal)
1599 return true;
1600 if (!cr1->cr_principal || !cr2->cr_principal)
1601 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301602 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
J. Bruce Fields57266a62013-04-13 14:27:29 -04001605static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1606{
1607 struct svc_cred *cr = &rqstp->rq_cred;
1608 u32 service;
1609
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001610 if (!cr->cr_gss_mech)
1611 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001612 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1613 return service == RPC_GSS_SVC_INTEGRITY ||
1614 service == RPC_GSS_SVC_PRIVACY;
1615}
1616
1617static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1618{
1619 struct svc_cred *cr = &rqstp->rq_cred;
1620
1621 if (!cl->cl_mach_cred)
1622 return true;
1623 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1624 return false;
1625 if (!svc_rqst_integrity_protected(rqstp))
1626 return false;
1627 if (!cr->cr_principal)
1628 return false;
1629 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1630}
1631
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001632static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001633{
1634 static u32 current_clientid = 1;
1635
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001636 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 clp->cl_clientid.cl_id = current_clientid++;
1638}
1639
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001640static void gen_confirm(struct nfs4_client *clp)
1641{
Chuck Leverab4684d2012-03-02 17:13:50 -05001642 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001643 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Jeff Laytonf4199922014-06-17 07:44:11 -04001645 /*
1646 * This is opaque to client, so no need to byte-swap. Use
1647 * __force to keep sparse happy
1648 */
1649 verf[0] = (__force __be32)get_seconds();
1650 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001651 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001654static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001655{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001656 struct nfs4_stid *ret;
1657
1658 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1659 if (!ret || !ret->sc_type)
1660 return NULL;
1661 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001662}
1663
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001664static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001665{
1666 struct nfs4_stid *s;
1667
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001668 s = find_stateid(cl, t);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001669 if (!s)
1670 return NULL;
1671 if (typemask & s->sc_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001672 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001673 return NULL;
1674}
1675
Jeff Layton2216d442012-11-12 15:00:57 -05001676static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001677 struct svc_rqst *rqstp, nfs4_verifier *verf)
1678{
1679 struct nfs4_client *clp;
1680 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001681 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001682 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001683 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001684
1685 clp = alloc_client(name);
1686 if (clp == NULL)
1687 return NULL;
1688
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001689 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1690 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001691 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001692 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001693 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001694 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001695 }
Jeff Layton02e12152014-07-16 10:31:57 -04001696 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001697 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001698 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001699 copy_verf(clp, verf);
1700 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001701 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001702 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001703 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001704 return clp;
1705}
1706
NeilBrownfd39ca92005-06-23 22:04:03 -07001707static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001708add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1709{
1710 struct rb_node **new = &(root->rb_node), *parent = NULL;
1711 struct nfs4_client *clp;
1712
1713 while (*new) {
1714 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1715 parent = *new;
1716
1717 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1718 new = &((*new)->rb_left);
1719 else
1720 new = &((*new)->rb_right);
1721 }
1722
1723 rb_link_node(&new_clp->cl_namenode, parent, new);
1724 rb_insert_color(&new_clp->cl_namenode, root);
1725}
1726
1727static struct nfs4_client *
1728find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1729{
1730 long long cmp;
1731 struct rb_node *node = root->rb_node;
1732 struct nfs4_client *clp;
1733
1734 while (node) {
1735 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1736 cmp = compare_blob(&clp->cl_name, name);
1737 if (cmp > 0)
1738 node = node->rb_left;
1739 else if (cmp < 0)
1740 node = node->rb_right;
1741 else
1742 return clp;
1743 }
1744 return NULL;
1745}
1746
1747static void
1748add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
1750 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001751 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001753 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001754 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001756 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001757 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
NeilBrownfd39ca92005-06-23 22:04:03 -07001760static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761move_to_confirmed(struct nfs4_client *clp)
1762{
1763 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001764 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001767 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001768 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001769 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001770 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 renew_client(clp);
1772}
1773
1774static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001775find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
1777 struct nfs4_client *clp;
1778 unsigned int idhashval = clientid_hashval(clid->cl_id);
1779
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001780 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001781 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001782 if ((bool)clp->cl_minorversion != sessions)
1783 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001784 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
1788 return NULL;
1789}
1790
1791static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001792find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1793{
1794 struct list_head *tbl = nn->conf_id_hashtbl;
1795
1796 return find_client_in_id_table(tbl, clid, sessions);
1797}
1798
1799static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001800find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001802 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001804 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001807static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001808{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001809 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001810}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001811
NeilBrown28ce6052005-06-23 22:03:56 -07001812static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001813find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001814{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001815 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001816}
1817
1818static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001819find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001820{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001821 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001822}
1823
NeilBrownfd39ca92005-06-23 22:04:03 -07001824static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001825gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001827 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001828 struct sockaddr *sa = svc_addr(rqstp);
1829 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001830 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Jeff Layton7077ecb2009-08-14 12:57:58 -04001832 /* Currently, we only support tcp and tcp6 for the callback channel */
1833 if (se->se_callback_netid_len == 3 &&
1834 !memcmp(se->se_callback_netid_val, "tcp", 3))
1835 expected_family = AF_INET;
1836 else if (se->se_callback_netid_len == 4 &&
1837 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1838 expected_family = AF_INET6;
1839 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 goto out_err;
1841
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001842 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001843 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001844 (struct sockaddr *)&conn->cb_addr,
1845 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001846
J. Bruce Fields07263f12010-05-31 19:09:40 -04001847 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001849
J. Bruce Fields07263f12010-05-31 19:09:40 -04001850 if (conn->cb_addr.ss_family == AF_INET6)
1851 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001852
J. Bruce Fields07263f12010-05-31 19:09:40 -04001853 conn->cb_prog = se->se_callback_prog;
1854 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001855 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 return;
1857out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001858 conn->cb_addr.ss_family = AF_UNSPEC;
1859 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001860 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 "will not receive delegations\n",
1862 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 return;
1865}
1866
Andy Adamson074fe892009-04-03 08:28:15 +03001867/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04001868 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001869 */
Trond Myklebustb6076642014-06-30 11:48:35 -04001870static void
Andy Adamson074fe892009-04-03 08:28:15 +03001871nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1872{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001873 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04001874 struct nfsd4_slot *slot = resp->cstate.slot;
1875 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001876
Andy Adamson557ce262009-08-28 08:45:04 -04001877 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001878
Andy Adamson557ce262009-08-28 08:45:04 -04001879 slot->sl_opcnt = resp->opcnt;
1880 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001881
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001882 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001883 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001884 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001885 return;
1886 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001887 base = resp->cstate.data_offset;
1888 slot->sl_datalen = buf->len - base;
1889 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04001890 WARN("%s: sessions DRC could not cache compound\n", __func__);
1891 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001892}
1893
1894/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001895 * Encode the replay sequence operation from the slot values.
1896 * If cachethis is FALSE encode the uncached rep error on the next
1897 * operation which sets resp->p and increments resp->opcnt for
1898 * nfs4svc_encode_compoundres.
1899 *
Andy Adamson074fe892009-04-03 08:28:15 +03001900 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001901static __be32
1902nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1903 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001904{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001905 struct nfsd4_op *op;
1906 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001907
Andy Adamsonabfabf82009-07-23 19:02:18 -04001908 /* Encode the replayed sequence operation */
1909 op = &args->ops[resp->opcnt - 1];
1910 nfsd4_encode_operation(resp, op);
1911
1912 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001913 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001914 op = &args->ops[resp->opcnt++];
1915 op->status = nfserr_retry_uncached_rep;
1916 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001917 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001918 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001919}
1920
1921/*
Andy Adamson557ce262009-08-28 08:45:04 -04001922 * The sequence operation is not cached because we can use the slot and
1923 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001924 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04001925static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001926nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1927 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001928{
Andy Adamson557ce262009-08-28 08:45:04 -04001929 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001930 struct xdr_stream *xdr = &resp->xdr;
1931 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03001932 __be32 status;
1933
Andy Adamson557ce262009-08-28 08:45:04 -04001934 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001935
Andy Adamsonabfabf82009-07-23 19:02:18 -04001936 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04001937 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04001938 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001939
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001940 p = xdr_reserve_space(xdr, slot->sl_datalen);
1941 if (!p) {
1942 WARN_ON_ONCE(1);
1943 return nfserr_serverfault;
1944 }
1945 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1946 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03001947
Andy Adamson557ce262009-08-28 08:45:04 -04001948 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001949 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001950}
1951
Andy Adamson0733d212009-04-03 08:28:01 +03001952/*
1953 * Set the exchange_id flags returned by the server.
1954 */
1955static void
1956nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1957{
1958 /* pNFS is not supported */
1959 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1960
1961 /* Referrals are supported, Migration is not. */
1962 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1963
1964 /* set the wire flags to return to client. */
1965 clid->flags = new->cl_exchange_flags;
1966}
1967
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001968static bool client_has_state(struct nfs4_client *clp)
1969{
1970 /*
1971 * Note clp->cl_openowners check isn't quite right: there's no
1972 * need to count owners without stateid's.
1973 *
1974 * Also note we should probably be using this in 4.0 case too.
1975 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04001976 return !list_empty(&clp->cl_openowners)
1977 || !list_empty(&clp->cl_delegations)
1978 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001979}
1980
Al Virob37ad282006-10-19 23:28:59 -07001981__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001982nfsd4_exchange_id(struct svc_rqst *rqstp,
1983 struct nfsd4_compound_state *cstate,
1984 struct nfsd4_exchange_id *exid)
1985{
Andy Adamson0733d212009-04-03 08:28:01 +03001986 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001987 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04001988 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001989 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001990 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001991 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001992 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03001993
Jeff Layton363168b2009-08-14 12:57:56 -04001994 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001995 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001996 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001997 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001998 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03001999
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002000 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002001 return nfserr_inval;
2002
Andy Adamson0733d212009-04-03 08:28:01 +03002003 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002004 case SP4_MACH_CRED:
2005 if (!svc_rqst_integrity_protected(rqstp))
2006 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002007 case SP4_NONE:
2008 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002009 default: /* checked by xdr code */
2010 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002011 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002012 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002013 }
2014
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002015 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002016 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002017 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002018 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002019 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2020 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2021
J. Bruce Fields136e6582012-05-12 20:37:23 -04002022 if (update) {
2023 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002024 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002025 goto out;
2026 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002027 if (!mach_creds_match(conf, rqstp)) {
2028 status = nfserr_wrong_cred;
2029 goto out;
2030 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002031 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002032 status = nfserr_perm;
2033 goto out;
2034 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002035 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002036 status = nfserr_not_same;
2037 goto out;
2038 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002039 /* case 6 */
2040 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2041 new = conf;
2042 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002043 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002044 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002045 if (client_has_state(conf)) {
2046 status = nfserr_clid_inuse;
2047 goto out;
2048 }
Andy Adamson0733d212009-04-03 08:28:01 +03002049 expire_client(conf);
2050 goto out_new;
2051 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002052 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002053 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002054 new = conf;
2055 goto out_copy;
2056 }
2057 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04002058 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002059 }
2060
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002061 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002062 status = nfserr_noent;
2063 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002064 }
2065
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002066 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002067 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03002068 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002069
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002070 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002071out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05002072 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03002073 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04002074 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03002075 goto out;
2076 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002077 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002078 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002079
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002080 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002081 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03002082out_copy:
2083 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2084 exid->clientid.cl_id = new->cl_clientid.cl_id;
2085
J. Bruce Fields778df3f2012-05-25 21:40:23 -04002086 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03002087 nfsd4_set_ex_flags(new, exid);
2088
2089 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04002090 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002091 status = nfs_ok;
2092
2093out:
2094 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03002095 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002096}
2097
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002098static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002099check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002100{
Andy Adamson88e588d2009-07-23 19:02:15 -04002101 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2102 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002103
2104 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002105 if (slot_inuse) {
2106 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002107 return nfserr_jukebox;
2108 else
2109 return nfserr_seq_misordered;
2110 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002111 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002112 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002113 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002114 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002115 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002116 return nfserr_seq_misordered;
2117}
2118
Andy Adamson49557cc2009-07-23 19:02:16 -04002119/*
2120 * Cache the create session result into the create session single DRC
2121 * slot cache by saving the xdr structure. sl_seqid has been set.
2122 * Do this for solo or embedded create session operations.
2123 */
2124static void
2125nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002126 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002127{
2128 slot->sl_status = nfserr;
2129 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2130}
2131
2132static __be32
2133nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2134 struct nfsd4_clid_slot *slot)
2135{
2136 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2137 return slot->sl_status;
2138}
2139
Mi Jinlong1b74c252011-07-14 14:50:17 +08002140#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2141 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2142 1 + /* MIN tag is length with zero, only length */ \
2143 3 + /* version, opcount, opcode */ \
2144 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2145 /* seqid, slotID, slotID, cache */ \
2146 4 ) * sizeof(__be32))
2147
2148#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2149 2 + /* verifier: AUTH_NULL, length 0 */\
2150 1 + /* status */ \
2151 1 + /* MIN tag is length with zero, only length */ \
2152 3 + /* opcount, opcode, opstatus*/ \
2153 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2154 /* seqid, slotID, slotID, slotID, status */ \
2155 5 ) * sizeof(__be32))
2156
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002157static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002158{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002159 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2160
J. Bruce Fields373cd402013-04-08 15:42:12 -04002161 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2162 return nfserr_toosmall;
2163 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2164 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002165 ca->headerpadsz = 0;
2166 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2167 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2168 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2169 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2170 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2171 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2172 /*
2173 * Note decreasing slot size below client's request may make it
2174 * difficult for client to function correctly, whereas
2175 * decreasing the number of slots will (just?) affect
2176 * performance. When short on memory we therefore prefer to
2177 * decrease number of slots instead of their size. Clients that
2178 * request larger slots than they need will get poor results:
2179 */
2180 ca->maxreqs = nfsd4_get_drc_mem(ca);
2181 if (!ca->maxreqs)
2182 return nfserr_jukebox;
2183
J. Bruce Fields373cd402013-04-08 15:42:12 -04002184 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002185}
2186
Kinglong Mee8a891632013-12-23 18:11:02 +08002187#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2188 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2189#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2190 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2191
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002192static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2193{
2194 ca->headerpadsz = 0;
2195
2196 /*
2197 * These RPC_MAX_HEADER macros are overkill, especially since we
2198 * don't even do gss on the backchannel yet. But this is still
2199 * less than 1k. Tighten up this estimate in the unlikely event
2200 * it turns out to be a problem for some client:
2201 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002202 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002203 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002204 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002205 return nfserr_toosmall;
2206 ca->maxresp_cached = 0;
2207 if (ca->maxops < 2)
2208 return nfserr_toosmall;
2209
2210 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002211}
2212
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002213static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2214{
2215 switch (cbs->flavor) {
2216 case RPC_AUTH_NULL:
2217 case RPC_AUTH_UNIX:
2218 return nfs_ok;
2219 default:
2220 /*
2221 * GSS case: the spec doesn't allow us to return this
2222 * error. But it also doesn't allow us not to support
2223 * GSS.
2224 * I'd rather this fail hard than return some error the
2225 * client might think it can already handle:
2226 */
2227 return nfserr_encr_alg_unsupp;
2228 }
2229}
2230
Andy Adamson069b6ad2009-04-03 08:27:58 +03002231__be32
2232nfsd4_create_session(struct svc_rqst *rqstp,
2233 struct nfsd4_compound_state *cstate,
2234 struct nfsd4_create_session *cr_ses)
2235{
Jeff Layton363168b2009-08-14 12:57:56 -04002236 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002237 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002238 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002239 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002240 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002241 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002242 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002243
Mi Jinlonga62573d2011-03-23 17:57:07 +08002244 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2245 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002246 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2247 if (status)
2248 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002249 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002250 if (status)
2251 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002252 status = check_backchannel_attrs(&cr_ses->back_channel);
2253 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002254 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002255 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002256 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002257 if (!new)
2258 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002259 conn = alloc_conn_from_crses(rqstp, cr_ses);
2260 if (!conn)
2261 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002262
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002263 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002264 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002265 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002266 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002267
2268 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002269 status = nfserr_wrong_cred;
2270 if (!mach_creds_match(conf, rqstp))
2271 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002272 cs_slot = &conf->cl_cs_slot;
2273 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002274 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002275 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002276 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002277 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002278 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002279 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002280 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002281 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002282 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002283 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002284 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002285 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002286 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002287 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002288 status = nfserr_wrong_cred;
2289 if (!mach_creds_match(unconf, rqstp))
2290 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002291 cs_slot = &unconf->cl_cs_slot;
2292 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002293 if (status) {
2294 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002295 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002296 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002297 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002298 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002299 if (old) {
2300 status = mark_client_expired(old);
2301 if (status)
2302 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002303 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002304 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002305 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002306 conf = unconf;
2307 } else {
2308 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002309 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002310 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002311 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002312 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002313 * We do not support RDMA or persistent sessions
2314 */
2315 cr_ses->flags &= ~SESSION4_PERSIST;
2316 cr_ses->flags &= ~SESSION4_RDMA;
2317
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002318 init_session(rqstp, new, conf, cr_ses);
2319 nfsd4_init_conn(rqstp, conn, new);
2320
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002321 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002322 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002323 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002324 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002325
Andy Adamson49557cc2009-07-23 19:02:16 -04002326 /* cache solo and embedded create sessions under the state lock */
2327 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002328 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002329 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002330out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08002331 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002332 free_conn(conn);
2333out_free_session:
2334 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002335out_release_drc_mem:
2336 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002337 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002338}
2339
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002340static __be32 nfsd4_map_bcts_dir(u32 *dir)
2341{
2342 switch (*dir) {
2343 case NFS4_CDFC4_FORE:
2344 case NFS4_CDFC4_BACK:
2345 return nfs_ok;
2346 case NFS4_CDFC4_FORE_OR_BOTH:
2347 case NFS4_CDFC4_BACK_OR_BOTH:
2348 *dir = NFS4_CDFC4_BOTH;
2349 return nfs_ok;
2350 };
2351 return nfserr_inval;
2352}
2353
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002354__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2355{
2356 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002357 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002358 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002359
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002360 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2361 if (status)
2362 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002363 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002364 session->se_cb_prog = bc->bc_cb_program;
2365 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002366 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002367
2368 nfsd4_probe_callback(session->se_client);
2369
2370 return nfs_ok;
2371}
2372
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002373__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2374 struct nfsd4_compound_state *cstate,
2375 struct nfsd4_bind_conn_to_session *bcts)
2376{
2377 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002378 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002379 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002380 struct net *net = SVC_NET(rqstp);
2381 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002382
2383 if (!nfsd4_last_compound_op(rqstp))
2384 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002385 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002386 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002387 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002388 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002389 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002390 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002391 status = nfserr_wrong_cred;
2392 if (!mach_creds_match(session->se_client, rqstp))
2393 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002394 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002395 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002396 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002397 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002398 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002399 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002400 goto out;
2401 nfsd4_init_conn(rqstp, conn, session);
2402 status = nfs_ok;
2403out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002404 nfsd4_put_session(session);
2405out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002406 nfs4_unlock_state();
2407 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002408}
2409
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002410static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2411{
2412 if (!session)
2413 return 0;
2414 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2415}
2416
Andy Adamson069b6ad2009-04-03 08:27:58 +03002417__be32
2418nfsd4_destroy_session(struct svc_rqst *r,
2419 struct nfsd4_compound_state *cstate,
2420 struct nfsd4_destroy_session *sessionid)
2421{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002422 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002423 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002424 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002425 struct net *net = SVC_NET(r);
2426 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002427
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002428 nfs4_lock_state();
2429 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002430 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002431 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002432 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002433 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002434 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002435 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002436 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002437 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002438 if (!ses)
2439 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002440 status = nfserr_wrong_cred;
2441 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002442 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002443 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002444 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002445 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002446 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002447 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002448
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002449 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002450
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002451 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002452 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002453out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002454 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002455out_client_lock:
2456 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002457out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002458 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002459 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002460}
2461
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002462static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002463{
2464 struct nfsd4_conn *c;
2465
2466 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002467 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002468 return c;
2469 }
2470 }
2471 return NULL;
2472}
2473
J. Bruce Fields57266a62013-04-13 14:27:29 -04002474static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002475{
2476 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002477 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002478 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002479 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002480
2481 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002482 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002483 if (c)
2484 goto out_free;
2485 status = nfserr_conn_not_bound_to_session;
2486 if (clp->cl_mach_cred)
2487 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002488 __nfsd4_hash_conn(new, ses);
2489 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002490 ret = nfsd4_register_conn(new);
2491 if (ret)
2492 /* oops; xprt is already down: */
2493 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002494 return nfs_ok;
2495out_free:
2496 spin_unlock(&clp->cl_lock);
2497 free_conn(new);
2498 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002499}
2500
Mi Jinlong868b89c2011-04-27 09:09:58 +08002501static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2502{
2503 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2504
2505 return args->opcnt > session->se_fchannel.maxops;
2506}
2507
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002508static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2509 struct nfsd4_session *session)
2510{
2511 struct xdr_buf *xb = &rqstp->rq_arg;
2512
2513 return xb->len > session->se_fchannel.maxreq_sz;
2514}
2515
Andy Adamson069b6ad2009-04-03 08:27:58 +03002516__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002517nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002518 struct nfsd4_compound_state *cstate,
2519 struct nfsd4_sequence *seq)
2520{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002521 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002522 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002523 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002524 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002525 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002526 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002527 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002528 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002529 struct net *net = SVC_NET(rqstp);
2530 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002531
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002532 if (resp->opcnt != 1)
2533 return nfserr_sequence_pos;
2534
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002535 /*
2536 * Will be either used or freed by nfsd4_sequence_check_conn
2537 * below.
2538 */
2539 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2540 if (!conn)
2541 return nfserr_jukebox;
2542
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002543 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002544 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002545 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002546 goto out_no_session;
2547 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002548
Mi Jinlong868b89c2011-04-27 09:09:58 +08002549 status = nfserr_too_many_ops;
2550 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002551 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002552
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002553 status = nfserr_req_too_big;
2554 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002555 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002556
Benny Halevyb85d4c02009-04-03 08:28:08 +03002557 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002558 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002559 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002560
Andy Adamson557ce262009-08-28 08:45:04 -04002561 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002562 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2563
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002564 /* We do not negotiate the number of slots yet, so set the
2565 * maxslots to the session maxreqs which is used to encode
2566 * sr_highest_slotid and the sr_target_slot id to maxslots */
2567 seq->maxslots = session->se_fchannel.maxreqs;
2568
J. Bruce Fields73e79482012-02-13 16:39:00 -05002569 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2570 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002571 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002572 status = nfserr_seq_misordered;
2573 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002574 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002575 cstate->slot = slot;
2576 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002577 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002578 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002579 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002580 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002581 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002582 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002583 }
2584 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002585 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002586
J. Bruce Fields57266a62013-04-13 14:27:29 -04002587 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002588 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002589 if (status)
2590 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002591
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002592 buflen = (seq->cachethis) ?
2593 session->se_fchannel.maxresp_cached :
2594 session->se_fchannel.maxresp_sz;
2595 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2596 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002597 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002598 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002599 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002600
2601 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002602 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002603 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002604 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002605 if (seq->cachethis)
2606 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002607 else
2608 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002609
2610 cstate->slot = slot;
2611 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002612 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002613
Benny Halevyb85d4c02009-04-03 08:28:08 +03002614out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002615 switch (clp->cl_cb_state) {
2616 case NFSD4_CB_DOWN:
2617 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2618 break;
2619 case NFSD4_CB_FAULT:
2620 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2621 break;
2622 default:
2623 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002624 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002625 if (!list_empty(&clp->cl_revoked))
2626 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002627out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002628 if (conn)
2629 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002630 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002631 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002632out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002633 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002634 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002635}
2636
Trond Myklebustb6076642014-06-30 11:48:35 -04002637void
2638nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2639{
2640 struct nfsd4_compound_state *cs = &resp->cstate;
2641
2642 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002643 if (cs->status != nfserr_replay_cache) {
2644 nfsd4_store_cache_entry(resp);
2645 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2646 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002647 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002648 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002649 } else if (cs->clp)
2650 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002651}
2652
Mi Jinlong345c2842011-10-20 17:51:39 +08002653__be32
2654nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2655{
2656 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002657 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002658 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002659
2660 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002661 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002662 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002663 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002664
2665 if (conf) {
2666 clp = conf;
2667
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002668 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002669 status = nfserr_clientid_busy;
2670 goto out;
2671 }
2672 } else if (unconf)
2673 clp = unconf;
2674 else {
2675 status = nfserr_stale_clientid;
2676 goto out;
2677 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002678 if (!mach_creds_match(clp, rqstp)) {
2679 status = nfserr_wrong_cred;
2680 goto out;
2681 }
Mi Jinlong345c2842011-10-20 17:51:39 +08002682 expire_client(clp);
2683out:
2684 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002685 return status;
2686}
2687
Andy Adamson069b6ad2009-04-03 08:27:58 +03002688__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002689nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2690{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002691 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002692
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002693 if (rc->rca_one_fs) {
2694 if (!cstate->current_fh.fh_dentry)
2695 return nfserr_nofilehandle;
2696 /*
2697 * We don't take advantage of the rca_one_fs case.
2698 * That's OK, it's optional, we can safely ignore it.
2699 */
2700 return nfs_ok;
2701 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002702
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002703 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002704 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002705 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2706 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002707 goto out;
2708
2709 status = nfserr_stale_clientid;
2710 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002711 /*
2712 * The following error isn't really legal.
2713 * But we only get here if the client just explicitly
2714 * destroyed the client. Surely it no longer cares what
2715 * error it gets back on an operation for the dead
2716 * client.
2717 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002718 goto out;
2719
2720 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002721 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002722out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002723 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002724 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002725}
2726
2727__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002728nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2729 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002731 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002733 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002734 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002735 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2736
J. Bruce Fields63db4632012-05-18 21:54:19 -04002737 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002739 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002740 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002741 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002743 if (clp_used_exchangeid(conf))
2744 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002745 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002746 char addr_str[INET6_ADDRSTRLEN];
2747 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2748 sizeof(addr_str));
2749 dprintk("NFSD: setclientid: string in use by client "
2750 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 goto out;
2752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002754 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002755 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002758 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002759 if (new == NULL)
2760 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002761 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002762 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002764 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002765 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002766 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002767 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002768 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2770 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2771 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2772 status = nfs_ok;
2773out:
2774 nfs4_unlock_state();
2775 return status;
2776}
2777
2778
Al Virob37ad282006-10-19 23:28:59 -07002779__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002780nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2781 struct nfsd4_compound_state *cstate,
2782 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783{
NeilBrown21ab45a2005-06-23 22:04:14 -07002784 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2786 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002787 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002788 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002790 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002793
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002794 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002795 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002796 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002797 * We try hard to give out unique clientid's, so if we get an
2798 * attempt to confirm the same clientid with a different cred,
2799 * there's a bug somewhere. Let's charitably assume it's our
2800 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002801 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002802 status = nfserr_serverfault;
2803 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2804 goto out;
2805 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2806 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002807 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002808 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2809 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002811 else /* case 4: client hasn't noticed we rebooted yet? */
2812 status = nfserr_stale_clientid;
2813 goto out;
2814 }
2815 status = nfs_ok;
2816 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002817 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2818 nfsd4_probe_callback(conf);
2819 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002820 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002821 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002822 if (conf) {
2823 status = mark_client_expired(conf);
2824 if (status)
2825 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002826 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002827 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002828 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002829 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 nfs4_unlock_state();
2833 return status;
2834}
2835
J. Bruce Fields32513b42011-10-13 16:00:16 -04002836static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002838 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2839}
2840
2841/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04002842static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04002843{
Trond Myklebustca943212014-07-23 16:17:39 -04002844 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
Trond Myklebust950e0112014-06-30 11:48:31 -04002846 lockdep_assert_held(&state_lock);
2847
J. Bruce Fields32513b42011-10-13 16:00:16 -04002848 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04002849 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002850 INIT_LIST_HEAD(&fp->fi_stateids);
2851 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04002852 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002853 fp->fi_had_conflict = false;
2854 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04002855 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002856 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2857 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04002858 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859}
2860
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002861void
NeilBrowne60d4392005-06-23 22:03:01 -07002862nfsd4_free_slabs(void)
2863{
Christoph Hellwigabf11352014-05-21 07:43:03 -07002864 kmem_cache_destroy(openowner_slab);
2865 kmem_cache_destroy(lockowner_slab);
2866 kmem_cache_destroy(file_slab);
2867 kmem_cache_destroy(stateid_slab);
2868 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002869}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
Bryan Schumaker72083392011-11-01 15:24:59 -04002871int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872nfsd4_init_slabs(void)
2873{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002874 openowner_slab = kmem_cache_create("nfsd4_openowners",
2875 sizeof(struct nfs4_openowner), 0, 0, NULL);
2876 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002877 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002878 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002879 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002880 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002881 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07002882 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002883 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002884 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002885 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07002886 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002887 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002888 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002889 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002890 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002891 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002892 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002893 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07002895
2896out_free_stateid_slab:
2897 kmem_cache_destroy(stateid_slab);
2898out_free_file_slab:
2899 kmem_cache_destroy(file_slab);
2900out_free_lockowner_slab:
2901 kmem_cache_destroy(lockowner_slab);
2902out_free_openowner_slab:
2903 kmem_cache_destroy(openowner_slab);
2904out:
NeilBrowne60d4392005-06-23 22:03:01 -07002905 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2906 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907}
2908
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002909static void init_nfs4_replay(struct nfs4_replay *rp)
2910{
2911 rp->rp_status = nfserr_serverfault;
2912 rp->rp_buflen = 0;
2913 rp->rp_buf = rp->rp_ibuf;
2914}
2915
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002916static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917{
2918 struct nfs4_stateowner *sop;
2919
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002920 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002921 if (!sop)
2922 return NULL;
2923
2924 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2925 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002926 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002927 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002929 sop->so_owner.len = owner->len;
2930
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002931 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002932 sop->so_client = clp;
2933 init_nfs4_replay(&sop->so_replay);
2934 return sop;
2935}
2936
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002937static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002938{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002939 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2940
2941 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002942 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943}
2944
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002945static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04002946alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002947 struct nfsd4_compound_state *cstate)
2948{
Trond Myklebust13d6f662014-06-30 11:48:45 -04002949 struct nfs4_client *clp = cstate->clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002950 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002952 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2953 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002955 oo->oo_owner.so_is_open_owner = 1;
2956 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04002957 oo->oo_flags = NFS4_OO_NEW;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002958 if (nfsd4_has_session(cstate))
2959 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002960 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002961 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002962 INIT_LIST_HEAD(&oo->oo_close_lru);
2963 hash_openowner(oo, clp, strhashval);
2964 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965}
2966
J. Bruce Fields996e0932011-10-17 11:14:48 -04002967static 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 -04002968 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002970 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04002971 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002972 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002973 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002974 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 stp->st_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 stp->st_access_bmap = 0;
2977 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07002978 stp->st_openstp = NULL;
Trond Myklebust1d31a252014-07-10 14:07:25 -04002979 spin_lock(&fp->fi_lock);
2980 list_add(&stp->st_perfile, &fp->fi_stateids);
2981 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982}
2983
2984static void
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002985move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986{
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002987 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2988
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002989 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002991 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002992 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993}
2994
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002996same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2997 clientid_t *clid)
2998{
2999 return (sop->so_owner.len == owner->len) &&
3000 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
3001 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002}
3003
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003004static struct nfs4_openowner *
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003005find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
3006 bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003008 struct nfs4_stateowner *so;
3009 struct nfs4_openowner *oo;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003010 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003012 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003013 if (!so->so_is_open_owner)
3014 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003015 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
3016 oo = openowner(so);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003017 clp = oo->oo_owner.so_client;
3018 if ((bool)clp->cl_minorversion != sessions)
3019 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003020 renew_client(oo->oo_owner.so_client);
3021 return oo;
3022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 }
3024 return NULL;
3025}
3026
3027/* search file_hashtbl[] for file */
3028static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003029find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030{
Trond Myklebustca943212014-07-23 16:17:39 -04003031 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 struct nfs4_file *fp;
3033
Trond Myklebust950e0112014-06-30 11:48:31 -04003034 lockdep_assert_held(&state_lock);
3035
Jeff Layton89876f82013-04-02 09:01:59 -04003036 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003037 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003038 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 }
3042 return NULL;
3043}
3044
Trond Myklebust950e0112014-06-30 11:48:31 -04003045static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003046find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003047{
3048 struct nfs4_file *fp;
3049
3050 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003051 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003052 spin_unlock(&state_lock);
3053 return fp;
3054}
3055
3056static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003057find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003058{
3059 struct nfs4_file *fp;
3060
3061 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003062 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003063 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003064 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003065 fp = new;
3066 }
3067 spin_unlock(&state_lock);
3068
3069 return fp;
3070}
3071
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003072/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 * Called to check deny when READ with all zero stateid or
3074 * WRITE with all zero or all one stateid
3075 */
Al Virob37ad282006-10-19 23:28:59 -07003076static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3078{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003080 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
Trond Myklebustca943212014-07-23 16:17:39 -04003082 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003083 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003084 return ret;
3085 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003086 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003087 if (fp->fi_share_deny & deny_type)
3088 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003089 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003090 put_nfs4_file(fp);
3091 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092}
3093
Jeff Layton02e12152014-07-16 10:31:57 -04003094void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095{
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003096 struct nfs4_client *clp = dp->dl_stid.sc_client;
3097 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3098
Jeff Layton02e12152014-07-16 10:31:57 -04003099 /*
3100 * We can't do this in nfsd_break_deleg_cb because it is
3101 * already holding inode->i_lock
3102 */
3103 spin_lock(&state_lock);
3104 block_delegations(&dp->dl_fh);
Jeff Laytondff13992014-07-08 14:02:49 -04003105 /*
3106 * If the dl_time != 0, then we know that it has already been
3107 * queued for a lease break. Don't queue it again.
3108 */
3109 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003110 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003111 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003112 }
Jeff Layton02e12152014-07-16 10:31:57 -04003113 spin_unlock(&state_lock);
3114}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
Jeff Layton02e12152014-07-16 10:31:57 -04003116static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3117{
3118 /*
3119 * We're assuming the state code never drops its reference
3120 * without first removing the lease. Since we're in this lease
3121 * callback (and since the lease code is serialized by the kernel
3122 * lock) we know the server hasn't removed the lease yet, we know
3123 * it's safe to take a reference.
3124 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003125 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003126 nfsd4_cb_recall(dp);
3127}
3128
Jeff Layton1c8c6012013-06-21 08:58:15 -04003129/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003130static void nfsd_break_deleg_cb(struct file_lock *fl)
3131{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003132 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3133 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003134
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003135 if (!fp) {
3136 WARN(1, "(%p)->fl_owner NULL\n", fl);
3137 return;
3138 }
3139 if (fp->fi_had_conflict) {
3140 WARN(1, "duplicate break on %p\n", fp);
3141 return;
3142 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003143 /*
3144 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003145 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003146 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003147 */
3148 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Jeff Layton02e12152014-07-16 10:31:57 -04003150 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003151 fp->fi_had_conflict = true;
3152 /*
3153 * If there are no delegations on the list, then we can't count on this
3154 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3155 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3156 * true should keep any new delegations from being hashed.
3157 */
3158 if (list_empty(&fp->fi_delegations))
3159 fl->fl_break_time = jiffies;
3160 else
3161 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3162 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003163 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164}
3165
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166static
3167int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3168{
3169 if (arg & F_UNLCK)
3170 return lease_modify(onlist, arg);
3171 else
3172 return -EAGAIN;
3173}
3174
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003175static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003176 .lm_break = nfsd_break_deleg_cb,
3177 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178};
3179
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003180static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3181{
3182 if (nfsd4_has_session(cstate))
3183 return nfs_ok;
3184 if (seqid == so->so_seqid - 1)
3185 return nfserr_replay_me;
3186 if (seqid == so->so_seqid)
3187 return nfs_ok;
3188 return nfserr_bad_seqid;
3189}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
Jeff Layton4b24ca72014-06-30 11:48:44 -04003191static __be32 lookup_clientid(clientid_t *clid,
3192 struct nfsd4_compound_state *cstate,
3193 struct nfsd_net *nn)
3194{
3195 struct nfs4_client *found;
3196
3197 if (cstate->clp) {
3198 found = cstate->clp;
3199 if (!same_clid(&found->cl_clientid, clid))
3200 return nfserr_stale_clientid;
3201 return nfs_ok;
3202 }
3203
3204 if (STALE_CLIENTID(clid, nn))
3205 return nfserr_stale_clientid;
3206
3207 /*
3208 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3209 * cached already then we know this is for is for v4.0 and "sessions"
3210 * will be false.
3211 */
3212 WARN_ON_ONCE(cstate->session);
3213 found = find_confirmed_client(clid, false, nn);
3214 if (!found)
3215 return nfserr_expired;
3216
3217 /* Cache the nfs4_client in cstate! */
3218 cstate->clp = found;
3219 atomic_inc(&found->cl_refcount);
3220 return nfs_ok;
3221}
3222
Al Virob37ad282006-10-19 23:28:59 -07003223__be32
Andy Adamson66689582009-04-03 08:28:45 +03003224nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003225 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 clientid_t *clientid = &open->op_clientid;
3228 struct nfs4_client *clp = NULL;
3229 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003230 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003231 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003233 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003235 /*
3236 * In case we need it later, after we've already created the
3237 * file and don't want to risk a further failure:
3238 */
3239 open->op_file = nfsd4_alloc_file();
3240 if (open->op_file == NULL)
3241 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
Trond Myklebust2d91e892014-06-30 11:48:46 -04003243 status = lookup_clientid(clientid, cstate, nn);
3244 if (status)
3245 return status;
3246 clp = cstate->clp;
3247
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003248 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003249 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003250 open->op_openowner = oo;
3251 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003252 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003254 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003255 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003256 release_openowner(oo);
3257 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003258 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003259 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003260 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3261 if (status)
3262 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003263 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003264new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003265 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003266 if (oo == NULL)
3267 return nfserr_jukebox;
3268 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003269alloc_stateid:
3270 open->op_stp = nfs4_alloc_stateid(clp);
3271 if (!open->op_stp)
3272 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003273 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274}
3275
Al Virob37ad282006-10-19 23:28:59 -07003276static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003277nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3278{
3279 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3280 return nfserr_openmode;
3281 else
3282 return nfs_ok;
3283}
3284
Daniel Mackc47d8322011-05-16 16:38:14 +02003285static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003286{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003287 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3288}
3289
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003290static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003291{
3292 struct nfs4_stid *ret;
3293
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003294 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003295 if (!ret)
3296 return NULL;
3297 return delegstateid(ret);
3298}
3299
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003300static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3301{
3302 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3303 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3304}
3305
Al Virob37ad282006-10-19 23:28:59 -07003306static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003307nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003308 struct nfs4_delegation **dp)
3309{
3310 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003311 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07003312
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003313 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07003314 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003315 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003316 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07003317 status = nfs4_check_delegmode(*dp, flags);
3318 if (status)
3319 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003320out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003321 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003322 return nfs_ok;
3323 if (status)
3324 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003325 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003326 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003327}
3328
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003329static struct nfs4_ol_stateid *
3330nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003332 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003333 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334
Trond Myklebust1d31a252014-07-10 14:07:25 -04003335 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003336 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 /* ignore lock owners */
3338 if (local->st_stateowner->so_is_open_owner == 0)
3339 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003340 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003341 ret = local;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003342 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003345 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003346 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347}
3348
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003349static inline int nfs4_access_to_access(u32 nfs4_access)
3350{
3351 int flags = 0;
3352
3353 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3354 flags |= NFSD_MAY_READ;
3355 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3356 flags |= NFSD_MAY_WRITE;
3357 return flags;
3358}
3359
Al Virob37ad282006-10-19 23:28:59 -07003360static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3362 struct nfsd4_open *open)
3363{
3364 struct iattr iattr = {
3365 .ia_valid = ATTR_SIZE,
3366 .ia_size = 0,
3367 };
3368 if (!open->op_truncate)
3369 return 0;
3370 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003371 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3373}
3374
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003375static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003376 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3377 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003378{
Trond Myklebustde186432014-07-10 14:07:26 -04003379 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003380 __be32 status;
3381 int oflag = nfs4_access_to_omode(open->op_share_access);
3382 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003383 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003384
Trond Myklebustde186432014-07-10 14:07:26 -04003385 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003386
3387 /*
3388 * Are we trying to set a deny mode that would conflict with
3389 * current access?
3390 */
3391 status = nfs4_file_check_deny(fp, open->op_share_deny);
3392 if (status != nfs_ok) {
3393 spin_unlock(&fp->fi_lock);
3394 goto out;
3395 }
3396
3397 /* set access to the file */
3398 status = nfs4_file_get_access(fp, open->op_share_access);
3399 if (status != nfs_ok) {
3400 spin_unlock(&fp->fi_lock);
3401 goto out;
3402 }
3403
3404 /* Set access bits in stateid */
3405 old_access_bmap = stp->st_access_bmap;
3406 set_access(open->op_share_access, stp);
3407
3408 /* Set new deny mask */
3409 old_deny_bmap = stp->st_deny_bmap;
3410 set_deny(open->op_share_deny, stp);
3411 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3412
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003413 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003414 spin_unlock(&fp->fi_lock);
3415 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003416 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003417 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003418 spin_lock(&fp->fi_lock);
3419 if (!fp->fi_fds[oflag]) {
3420 fp->fi_fds[oflag] = filp;
3421 filp = NULL;
3422 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003423 }
Trond Myklebustde186432014-07-10 14:07:26 -04003424 spin_unlock(&fp->fi_lock);
3425 if (filp)
3426 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003427
3428 status = nfsd4_truncate(rqstp, cur_fh, open);
3429 if (status)
3430 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003431out:
3432 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003433out_put_access:
3434 stp->st_access_bmap = old_access_bmap;
3435 nfs4_file_put_access(fp, open->op_share_access);
3436 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3437 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003438}
3439
Al Virob37ad282006-10-19 23:28:59 -07003440static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003441nfs4_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 -07003442{
Al Virob37ad282006-10-19 23:28:59 -07003443 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003444 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003446 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003447 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003448
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003449 /* test and set deny mode */
3450 spin_lock(&fp->fi_lock);
3451 status = nfs4_file_check_deny(fp, open->op_share_deny);
3452 if (status == nfs_ok) {
3453 old_deny_bmap = stp->st_deny_bmap;
3454 set_deny(open->op_share_deny, stp);
3455 fp->fi_share_deny |=
3456 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3457 }
3458 spin_unlock(&fp->fi_lock);
3459
3460 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003463 status = nfsd4_truncate(rqstp, cur_fh, open);
3464 if (status != nfs_ok)
3465 reset_union_bmap_deny(old_deny_bmap, stp);
3466 return status;
3467}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003470nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003472 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473}
3474
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003475/* Should we give out recallable state?: */
3476static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3477{
3478 if (clp->cl_cb_state == NFSD4_CB_UP)
3479 return true;
3480 /*
3481 * In the sessions case, since we don't have to establish a
3482 * separate connection for callbacks, we assume it's OK
3483 * until we hear otherwise:
3484 */
3485 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3486}
3487
Jeff Laytond564fbe2014-07-16 10:31:58 -04003488static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003489{
3490 struct file_lock *fl;
3491
3492 fl = locks_alloc_lock();
3493 if (!fl)
3494 return NULL;
3495 locks_init_lock(fl);
3496 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003497 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003498 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3499 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003500 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003501 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003502 return fl;
3503}
3504
J. Bruce Fields99c41512013-05-21 16:21:25 -04003505static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003506{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003507 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003508 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003509 struct file *filp;
3510 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003511
Jeff Laytond564fbe2014-07-16 10:31:58 -04003512 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003513 if (!fl)
3514 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003515 filp = find_readable_file(fp);
3516 if (!filp) {
3517 /* We should always have a readable file here */
3518 WARN_ON_ONCE(1);
3519 return -EBADF;
3520 }
3521 fl->fl_file = filp;
3522 status = vfs_setlease(filp, fl->fl_type, &fl);
3523 if (status) {
3524 locks_free_lock(fl);
3525 goto out_fput;
3526 }
Benny Halevycdc97502014-05-30 09:09:30 -04003527 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003528 spin_lock(&fp->fi_lock);
3529 /* Did the lease get broken before we took the lock? */
3530 status = -EAGAIN;
3531 if (fp->fi_had_conflict)
3532 goto out_unlock;
3533 /* Race breaker */
3534 if (fp->fi_lease) {
3535 status = 0;
3536 atomic_inc(&fp->fi_delegees);
3537 hash_delegation_locked(dp, fp);
3538 goto out_unlock;
3539 }
3540 fp->fi_lease = fl;
3541 fp->fi_deleg_file = filp;
3542 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003543 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003544 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003545 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003546 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003547out_unlock:
3548 spin_unlock(&fp->fi_lock);
3549 spin_unlock(&state_lock);
3550out_fput:
3551 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003552 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003553}
3554
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003555static int nfs4_set_delegation(struct nfs4_delegation *dp, struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003556{
Jeff Layton417c6622014-07-21 09:34:57 -04003557 int status = 0;
3558
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003559 if (fp->fi_had_conflict)
3560 return -EAGAIN;
3561 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003562 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003563 spin_lock(&fp->fi_lock);
3564 dp->dl_file = fp;
3565 if (!fp->fi_lease) {
3566 spin_unlock(&fp->fi_lock);
3567 spin_unlock(&state_lock);
3568 return nfs4_setlease(dp);
3569 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003570 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003571 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003572 status = -EAGAIN;
3573 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003574 }
Benny Halevy931ee562014-05-30 09:09:27 -04003575 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003576out_unlock:
3577 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003578 spin_unlock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003579 return status;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003580}
3581
Benny Halevy4aa89132012-02-21 14:16:44 -08003582static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3583{
3584 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3585 if (status == -EAGAIN)
3586 open->op_why_no_deleg = WND4_CONTENTION;
3587 else {
3588 open->op_why_no_deleg = WND4_RESOURCE;
3589 switch (open->op_deleg_want) {
3590 case NFS4_SHARE_WANT_READ_DELEG:
3591 case NFS4_SHARE_WANT_WRITE_DELEG:
3592 case NFS4_SHARE_WANT_ANY_DELEG:
3593 break;
3594 case NFS4_SHARE_WANT_CANCEL:
3595 open->op_why_no_deleg = WND4_CANCELLED;
3596 break;
3597 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003598 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003599 }
3600 }
3601}
3602
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603/*
3604 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003605 *
3606 * Note we don't support write delegations, and won't until the vfs has
3607 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 */
3609static void
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003610nfs4_open_delegation(struct net *net, struct svc_fh *fh,
3611 struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612{
3613 struct nfs4_delegation *dp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003614 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003615 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003616 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003618 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003619 open->op_recall = 0;
3620 switch (open->op_claim_type) {
3621 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003622 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003623 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003624 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3625 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003626 break;
3627 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003628 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003629 /*
3630 * Let's not give out any delegations till everyone's
3631 * had the chance to reclaim theirs....
3632 */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003633 if (locks_in_grace(net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003634 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003635 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003636 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003637 /*
3638 * Also, if the file was opened for write or
3639 * create, there's a good chance the client's
3640 * about to write to it, resulting in an
3641 * immediate recall (since we don't support
3642 * write delegations):
3643 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003644 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003645 goto out_no_deleg;
3646 if (open->op_create == NFS4_OPEN_CREATE)
3647 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003648 break;
3649 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003650 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003651 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003652 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003653 if (dp == NULL)
3654 goto out_no_deleg;
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003655 status = nfs4_set_delegation(dp, stp->st_file);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003656 if (status)
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003657 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003659 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003661 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003662 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003663 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003664 return;
3665out_free:
Jeff Layton42690672014-07-25 07:34:20 -04003666 nfs4_put_delegation(dp);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003667out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003668 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3669 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003670 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003671 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003672 open->op_recall = 1;
3673 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003674
3675 /* 4.1 client asking for a delegation? */
3676 if (open->op_deleg_want)
3677 nfsd4_open_deleg_none_ext(open, status);
3678 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679}
3680
Benny Halevye27f49c2012-02-21 14:16:54 -08003681static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3682 struct nfs4_delegation *dp)
3683{
3684 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3685 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3686 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3687 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3688 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3689 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3690 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3691 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3692 }
3693 /* Otherwise the client must be confused wanting a delegation
3694 * it already has, therefore we don't return
3695 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3696 */
3697}
3698
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699/*
3700 * called with nfs4_lock_state() held.
3701 */
Al Virob37ad282006-10-19 23:28:59 -07003702__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3704{
Andy Adamson66689582009-04-03 08:28:45 +03003705 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003706 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003708 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003709 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003710 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 /*
3713 * Lookup file; if found, lookup stateid and check open request,
3714 * and check for delegations in the process of being recalled.
3715 * If not found, create the nfs4_file struct
3716 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003717 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04003718 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04003719 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003720 if (status)
3721 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003722 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003724 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003725 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003726 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003727 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003728 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 }
3730
3731 /*
3732 * OPEN the file, or upgrade an existing OPEN.
3733 * If truncate fails, the OPEN fails.
3734 */
3735 if (stp) {
3736 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003737 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 if (status)
3739 goto out;
3740 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003741 stp = open->op_stp;
3742 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003743 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003744 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3745 if (status) {
3746 release_open_stateid(stp);
3747 goto out;
3748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003750 update_stateid(&stp->st_stid.sc_stateid);
3751 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752
Benny Halevyd24433c2012-02-16 20:57:17 +02003753 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02003754 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3755 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3756 open->op_why_no_deleg = WND4_NOT_WANTED;
3757 goto nodeleg;
3758 }
3759 }
3760
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 /*
3762 * Attempt to hand out a delegation. No error return, because the
3763 * OPEN succeeds even if we fail.
3764 */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003765 nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003766nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 status = nfs_ok;
3768
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003769 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003770 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003772 /* 4.1 client trying to upgrade/downgrade delegation? */
3773 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003774 open->op_deleg_want)
3775 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003776
NeilBrown13cd2182005-06-23 22:03:10 -07003777 if (fp)
3778 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003779 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003780 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 /*
3782 * To finish the open response, we just need to set the rflags.
3783 */
3784 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003785 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003786 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3788
3789 return status;
3790}
3791
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003792void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3793{
3794 if (open->op_openowner) {
3795 struct nfs4_openowner *oo = open->op_openowner;
3796
3797 if (!list_empty(&oo->oo_owner.so_stateids))
3798 list_del_init(&oo->oo_close_lru);
3799 if (oo->oo_flags & NFS4_OO_NEW) {
3800 if (status) {
3801 release_openowner(oo);
3802 open->op_openowner = NULL;
3803 } else
3804 oo->oo_flags &= ~NFS4_OO_NEW;
3805 }
3806 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003807 if (open->op_file)
3808 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003809 if (open->op_stp)
J. Bruce Fieldsef798592012-08-29 10:32:54 -07003810 free_generic_stateid(open->op_stp);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003811}
3812
Al Virob37ad282006-10-19 23:28:59 -07003813__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003814nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3815 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816{
3817 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003818 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003819 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820
3821 nfs4_lock_state();
3822 dprintk("process_renew(%08x/%08x): starting\n",
3823 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003824 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003825 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003827 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003829 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003830 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 goto out;
3832 status = nfs_ok;
3833out:
3834 nfs4_unlock_state();
3835 return status;
3836}
3837
NeilBrowna76b4312005-06-23 22:04:01 -07003838static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003839nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07003840{
Jeff Layton33dcc482012-04-10 11:08:48 -04003841 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003842 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003843 return;
3844
NeilBrowna76b4312005-06-23 22:04:01 -07003845 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003846 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003847 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003848 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003849 /*
3850 * Now that every NFSv4 client has had the chance to recover and
3851 * to see the (possibly new, possibly shorter) lease time, we
3852 * can safely set the next grace time to the current lease time:
3853 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03003854 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003855}
3856
NeilBrownfd39ca92005-06-23 22:04:03 -07003857static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003858nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859{
3860 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003861 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 struct nfs4_delegation *dp;
3863 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003864 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003865 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866
3867 nfs4_lock_state();
3868
3869 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003870 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03003871 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003872 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03003873 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 clp = list_entry(pos, struct nfs4_client, cl_lru);
3875 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3876 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003877 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 break;
3879 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003880 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03003881 dprintk("NFSD: client in use (clientid %08x)\n",
3882 clp->cl_clientid.cl_id);
3883 continue;
3884 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003885 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003886 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003887 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03003888 list_for_each_safe(pos, next, &reaplist) {
3889 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 dprintk("NFSD: purging unused client (clientid %08x)\n",
3891 clp->cl_clientid.cl_id);
3892 expire_client(clp);
3893 }
Benny Halevycdc97502014-05-30 09:09:30 -04003894 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003895 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03003897 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3898 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003900 t = dp->dl_time - cutoff;
3901 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 break;
3903 }
Jeff Layton42690672014-07-25 07:34:20 -04003904 unhash_delegation_locked(dp);
3905 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 }
Benny Halevycdc97502014-05-30 09:09:30 -04003907 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04003908 while (!list_empty(&reaplist)) {
3909 dp = list_first_entry(&reaplist, struct nfs4_delegation,
3910 dl_recall_lru);
3911 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003912 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003914 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003915 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3916 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003917 t = oo->oo_time - cutoff;
3918 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 break;
3920 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003921 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04003923 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04003925 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926}
3927
Harvey Harrisona254b242008-02-20 12:49:00 -08003928static struct workqueue_struct *laundry_wq;
3929static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08003930
3931static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003932laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933{
3934 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003935 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3936 work);
3937 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3938 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003940 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003942 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943}
3944
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003945static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003946{
Trond Myklebustb07c54a2014-07-23 16:17:40 -04003947 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003948 return nfserr_bad_stateid;
3949 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950}
3951
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003953access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003955 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3956 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3957 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958}
3959
3960static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003961access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003963 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3964 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965}
3966
3967static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003968__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969{
Al Virob37ad282006-10-19 23:28:59 -07003970 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971
J. Bruce Fields02921912010-07-29 15:16:59 -04003972 /* For lock stateid's, we test the parent open, not the lock: */
3973 if (stp->st_openstp)
3974 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003975 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003977 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 goto out;
3979 status = nfs_ok;
3980out:
3981 return status;
3982}
3983
Al Virob37ad282006-10-19 23:28:59 -07003984static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003985check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003987 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003989 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003990 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 * conflicting state; so we must wait out the grace period. */
3992 return nfserr_grace;
3993 } else if (flags & WR_STATE)
3994 return nfs4_share_conflict(current_fh,
3995 NFS4_SHARE_DENY_WRITE);
3996 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3997 return nfs4_share_conflict(current_fh,
3998 NFS4_SHARE_DENY_READ);
3999}
4000
4001/*
4002 * Allow READ/WRITE during grace period on recovered state only for files
4003 * that are not able to provide mandatory locking.
4004 */
4005static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004006grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004008 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009}
4010
J. Bruce Fields81b82962011-08-23 11:03:29 -04004011/* Returns true iff a is later than b: */
4012static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4013{
Jim Rees1a9357f2013-05-17 17:33:00 -04004014 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004015}
4016
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004017static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004018{
Andy Adamson66689582009-04-03 08:28:45 +03004019 /*
4020 * When sessions are used the stateid generation number is ignored
4021 * when it is zero.
4022 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004023 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004024 return nfs_ok;
4025
4026 if (in->si_generation == ref->si_generation)
4027 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004028
J. Bruce Fields0836f582008-01-26 19:08:12 -05004029 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004030 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004031 return nfserr_bad_stateid;
4032 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004033 * However, we could see a stateid from the past, even from a
4034 * non-buggy client. For example, if the client sends a lock
4035 * while some IO is outstanding, the lock may bump si_generation
4036 * while the IO is still in flight. The client could avoid that
4037 * situation by waiting for responses on all the IO requests,
4038 * but better performance may result in retrying IO that
4039 * receives an old_stateid error if requests are rarely
4040 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004041 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004042 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004043}
4044
Chuck Lever7df302f2012-05-29 13:56:37 -04004045static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004046{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004047 struct nfs4_stid *s;
4048 struct nfs4_ol_stateid *ols;
4049 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004050
Chuck Lever7df302f2012-05-29 13:56:37 -04004051 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4052 return nfserr_bad_stateid;
4053 /* Client debugging aid. */
4054 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4055 char addr_str[INET6_ADDRSTRLEN];
4056 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4057 sizeof(addr_str));
4058 pr_warn_ratelimited("NFSD: client %s testing state ID "
4059 "with incorrect client ID\n", addr_str);
4060 return nfserr_bad_stateid;
4061 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004062 s = find_stateid(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004063 if (!s)
Chuck Lever7df302f2012-05-29 13:56:37 -04004064 return nfserr_bad_stateid;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004065 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004066 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004067 return status;
J. Bruce Fields23340032013-04-09 17:42:28 -04004068 switch (s->sc_type) {
4069 case NFS4_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004070 return nfs_ok;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004071 case NFS4_REVOKED_DELEG_STID:
4072 return nfserr_deleg_revoked;
J. Bruce Fields23340032013-04-09 17:42:28 -04004073 case NFS4_OPEN_STID:
4074 case NFS4_LOCK_STID:
4075 ols = openlockstateid(s);
4076 if (ols->st_stateowner->so_is_open_owner
4077 && !(openowner(ols->st_stateowner)->oo_flags
4078 & NFS4_OO_CONFIRMED))
4079 return nfserr_bad_stateid;
4080 return nfs_ok;
4081 default:
4082 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004083 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004084 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004085 case NFS4_CLOSED_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004086 return nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004087 }
Bryan Schumaker17456802011-07-13 10:50:48 -04004088}
4089
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004090static __be32
4091nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4092 stateid_t *stateid, unsigned char typemask,
4093 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004094{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004095 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004096
4097 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4098 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004099 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004100 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004101 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004102 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004103 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004104 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004105 if (status)
4106 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004107 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004108 if (!*s)
4109 return nfserr_bad_stateid;
4110 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004111}
4112
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113/*
4114* Checks for stateid operations
4115*/
Al Virob37ad282006-10-19 23:28:59 -07004116__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004117nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004118 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004120 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004121 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004123 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004125 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004126 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004127 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 if (filpp)
4130 *filpp = NULL;
4131
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004132 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 return nfserr_grace;
4134
4135 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004136 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137
Trond Myklebust14bcab12014-04-18 14:44:07 -04004138 nfs4_lock_state();
4139
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004140 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004141 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004142 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004143 if (status)
Trond Myklebust14bcab12014-04-18 14:44:07 -04004144 goto out;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004145 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4146 if (status)
4147 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004148 switch (s->sc_type) {
4149 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004150 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004151 status = nfs4_check_delegmode(dp, flags);
4152 if (status)
4153 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004154 if (filpp) {
Trond Myklebust14bcab12014-04-18 14:44:07 -04004155 file = dp->dl_file->fi_deleg_file;
4156 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004157 WARN_ON_ONCE(1);
4158 status = nfserr_serverfault;
4159 goto out;
4160 }
Trond Myklebustde186432014-07-10 14:07:26 -04004161 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004162 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004163 break;
4164 case NFS4_OPEN_STID:
4165 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004166 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004167 status = nfs4_check_fh(current_fh, stp);
4168 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004170 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004171 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004173 status = nfs4_check_openmode(stp, flags);
4174 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004176 if (filpp) {
4177 if (flags & RD_STATE)
Trond Myklebust14bcab12014-04-18 14:44:07 -04004178 file = find_readable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004179 else
Trond Myklebust14bcab12014-04-18 14:44:07 -04004180 file = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004181 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004182 break;
4183 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004184 status = nfserr_bad_stateid;
4185 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 }
4187 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004188 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004189 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190out:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004191 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 return status;
4193}
4194
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004195static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004196nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004197{
J. Bruce Fieldsa1b8ff42014-05-20 15:55:21 -04004198 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4199
4200 if (check_for_locks(stp->st_file, lo))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004201 return nfserr_locks_held;
Jeff Laytonc53530d2014-06-30 11:48:39 -04004202 release_lockowner_if_empty(lo);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004203 return nfs_ok;
4204}
4205
4206/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004207 * Test if the stateid is valid
4208 */
4209__be32
4210nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4211 struct nfsd4_test_stateid *test_stateid)
4212{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004213 struct nfsd4_test_stateid_id *stateid;
4214 struct nfs4_client *cl = cstate->session->se_client;
4215
4216 nfs4_lock_state();
4217 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004218 stateid->ts_id_status =
4219 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004220 nfs4_unlock_state();
4221
Bryan Schumaker17456802011-07-13 10:50:48 -04004222 return nfs_ok;
4223}
4224
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004225__be32
4226nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4227 struct nfsd4_free_stateid *free_stateid)
4228{
4229 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004230 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004231 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004232 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004233 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004234
4235 nfs4_lock_state();
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004236 s = find_stateid(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004237 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004238 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004239 switch (s->sc_type) {
4240 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004241 ret = nfserr_locks_held;
4242 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004243 case NFS4_OPEN_STID:
4244 case NFS4_LOCK_STID:
4245 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4246 if (ret)
4247 goto out;
4248 if (s->sc_type == NFS4_LOCK_STID)
4249 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4250 else
4251 ret = nfserr_locks_held;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004252 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004253 case NFS4_REVOKED_DELEG_STID:
4254 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004255 spin_lock(&cl->cl_lock);
4256 list_del_init(&dp->dl_recall_lru);
4257 spin_unlock(&cl->cl_lock);
4258 nfs4_put_delegation(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004259 ret = nfs_ok;
4260 break;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004261 default:
4262 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004263 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004264out:
4265 nfs4_unlock_state();
4266 return ret;
4267}
4268
NeilBrown4c4cd222005-07-07 17:59:27 -07004269static inline int
4270setlkflg (int type)
4271{
4272 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4273 RD_STATE : WR_STATE;
4274}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004276static __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 -04004277{
4278 struct svc_fh *current_fh = &cstate->current_fh;
4279 struct nfs4_stateowner *sop = stp->st_stateowner;
4280 __be32 status;
4281
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004282 status = nfsd4_check_seqid(cstate, sop, seqid);
4283 if (status)
4284 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004285 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4286 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004287 /*
4288 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004289 * nfserr_replay_me from the previous step, and
4290 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004291 */
4292 return nfserr_bad_stateid;
4293 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4294 if (status)
4295 return status;
4296 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004297}
4298
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299/*
4300 * Checks for sequence id mutating operations.
4301 */
Al Virob37ad282006-10-19 23:28:59 -07004302static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004303nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004304 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004305 struct nfs4_ol_stateid **stpp,
4306 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004308 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004309 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004310 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004312 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4313 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004314
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004316 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004317 if (status)
4318 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004319 stp = openlockstateid(s);
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04004320 if (!nfsd4_has_session(cstate))
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004321 cstate->replay_owner = stp->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004323 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4324 if (!status)
4325 *stpp = stp;
4326 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004327}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004328
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004329static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4330 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004331{
4332 __be32 status;
4333 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004335 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004336 NFS4_OPEN_STID, stpp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004337 if (status)
4338 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004339 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004340 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07004341 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004342 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343}
4344
Al Virob37ad282006-10-19 23:28:59 -07004345__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004346nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004347 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348{
Al Virob37ad282006-10-19 23:28:59 -07004349 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004350 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004351 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004352 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353
Al Viroa6a9f182013-09-16 10:57:01 -04004354 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4355 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004357 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004358 if (status)
4359 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360
4361 nfs4_lock_state();
4362
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004363 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004364 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004365 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004366 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004367 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004368 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004369 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004370 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004371 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004372 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004373 update_stateid(&stp->st_stid.sc_stateid);
4374 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004375 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004376 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004377
Jeff Layton2a4317c2012-03-21 16:42:43 -04004378 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004379 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004381 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004382 if (!cstate->replay_owner)
4383 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 return status;
4385}
4386
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004387static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004389 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004390 return;
Jeff Layton12659652014-07-10 14:07:28 -04004391 nfs4_file_put_access(stp->st_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004392 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004393}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004394
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004395static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4396{
4397 switch (to_access) {
4398 case NFS4_SHARE_ACCESS_READ:
4399 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4400 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4401 break;
4402 case NFS4_SHARE_ACCESS_WRITE:
4403 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4404 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4405 break;
4406 case NFS4_SHARE_ACCESS_BOTH:
4407 break;
4408 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004409 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 }
4411}
4412
Al Virob37ad282006-10-19 23:28:59 -07004413__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004414nfsd4_open_downgrade(struct svc_rqst *rqstp,
4415 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004416 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417{
Al Virob37ad282006-10-19 23:28:59 -07004418 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004419 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004420 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
Al Viroa6a9f182013-09-16 10:57:01 -04004422 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4423 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004425 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004426 if (od->od_deleg_want)
4427 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4428 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
4430 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004431 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004432 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004433 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004436 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004437 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 stp->st_access_bmap, od->od_share_access);
4439 goto out;
4440 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004441 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004442 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 stp->st_deny_bmap, od->od_share_deny);
4444 goto out;
4445 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004446 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
Jeff Laytonce0fc432012-05-11 09:45:14 -04004448 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004450 update_stateid(&stp->st_stid.sc_stateid);
4451 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 status = nfs_ok;
4453out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004454 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004455 if (!cstate->replay_owner)
4456 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 return status;
4458}
4459
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004460static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4461{
Trond Myklebustacf92952014-06-30 11:48:37 -04004462 struct nfs4_client *clp = s->st_stid.sc_client;
4463 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4464
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004465 s->st_stid.sc_type = NFS4_CLOSED_STID;
Trond Myklebustacf92952014-06-30 11:48:37 -04004466 unhash_open_stateid(s);
4467
4468 if (clp->cl_minorversion) {
4469 free_generic_stateid(s);
4470 if (list_empty(&oo->oo_owner.so_stateids))
4471 release_openowner(oo);
4472 } else {
Trond Myklebustf8338832014-07-25 07:34:19 -04004473 if (s->st_file) {
4474 put_nfs4_file(s->st_file);
4475 s->st_file = NULL;
4476 }
Trond Myklebustacf92952014-06-30 11:48:37 -04004477 oo->oo_last_closed_stid = s;
4478 /*
4479 * In the 4.0 case we need to keep the owners around a
4480 * little while to handle CLOSE replay.
4481 */
4482 if (list_empty(&oo->oo_owner.so_stateids))
4483 move_to_close_lru(oo, clp->net);
4484 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004485}
4486
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487/*
4488 * nfs4_unlock_state() called after encode
4489 */
Al Virob37ad282006-10-19 23:28:59 -07004490__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004491nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004492 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493{
Al Virob37ad282006-10-19 23:28:59 -07004494 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004495 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004496 struct net *net = SVC_NET(rqstp);
4497 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498
Al Viroa6a9f182013-09-16 10:57:01 -04004499 dprintk("NFSD: nfsd4_close on file %pd\n",
4500 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501
4502 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004503 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4504 &close->cl_stateid,
4505 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004506 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004507 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004508 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004510 update_stateid(&stp->st_stid.sc_stateid);
4511 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004513 nfsd4_close_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004515 if (!cstate->replay_owner)
4516 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 return status;
4518}
4519
Al Virob37ad282006-10-19 23:28:59 -07004520__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004521nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4522 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004524 struct nfs4_delegation *dp;
4525 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004526 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004527 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004528 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004530 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004531 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
4533 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004534 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004535 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004536 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004537 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004538 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004539 if (status)
4540 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004541
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004542 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004544 nfs4_unlock_state();
4545
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 return status;
4547}
4548
4549
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551
Benny Halevy87df4de2008-12-15 19:42:03 +02004552static inline u64
4553end_offset(u64 start, u64 len)
4554{
4555 u64 end;
4556
4557 end = start + len;
4558 return end >= start ? end: NFS4_MAX_UINT64;
4559}
4560
4561/* last octet in a range */
4562static inline u64
4563last_byte_offset(u64 start, u64 len)
4564{
4565 u64 end;
4566
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004567 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004568 end = start + len;
4569 return end > start ? end - 1: NFS4_MAX_UINT64;
4570}
4571
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572/*
4573 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4574 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4575 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4576 * locking, this prevents us from being completely protocol-compliant. The
4577 * real solution to this problem is to start using unsigned file offsets in
4578 * the VFS, but this is a very deep change!
4579 */
4580static inline void
4581nfs4_transform_lock_offset(struct file_lock *lock)
4582{
4583 if (lock->fl_start < 0)
4584 lock->fl_start = OFFSET_MAX;
4585 if (lock->fl_end < 0)
4586 lock->fl_end = OFFSET_MAX;
4587}
4588
NeilBrownd5b90262006-04-10 22:55:22 -07004589/* Hack!: For now, we're defining this just so we can use a pointer to it
4590 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004591static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004592};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593
4594static inline void
4595nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4596{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004597 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
NeilBrownd5b90262006-04-10 22:55:22 -07004599 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004600 lo = (struct nfs4_lockowner *) fl->fl_owner;
4601 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4602 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004603 if (!deny->ld_owner.data)
4604 /* We just don't care that much */
4605 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004606 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4607 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004608 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004609nevermind:
4610 deny->ld_owner.len = 0;
4611 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004612 deny->ld_clientid.cl_boot = 0;
4613 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 }
4615 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004616 deny->ld_length = NFS4_MAX_UINT64;
4617 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4619 deny->ld_type = NFS4_READ_LT;
4620 if (fl->fl_type != F_RDLCK)
4621 deny->ld_type = NFS4_WRITE_LT;
4622}
4623
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004624static struct nfs4_lockowner *
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004625find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4626 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627{
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004628 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4629 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004631 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4632 if (so->so_is_open_owner)
4633 continue;
4634 if (!same_owner_str(so, owner, clid))
4635 continue;
4636 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 }
4638 return NULL;
4639}
4640
4641/*
4642 * Alloc a lock owner structure.
4643 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004644 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004646 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004648static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004649alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004650 struct nfs4_lockowner *lo;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004651 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004653 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4654 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004656 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4657 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07004658 /* It is the openowner seqid that will be incremented in encode in the
4659 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004660 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004661 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004662 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663}
4664
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004665static struct nfs4_ol_stateid *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004666alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp,
4667 struct inode *inode,
4668 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004670 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004671 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
J. Bruce Fields996e0932011-10-17 11:14:48 -04004673 stp = nfs4_alloc_stateid(clp);
NeilBrown5ac049a2005-06-23 22:03:03 -07004674 if (stp == NULL)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04004675 return NULL;
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004676 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004677 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4678 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004679 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 stp->st_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004681 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004683 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004684 list_add(&stp->st_locks, &open_stp->st_locks);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004685 spin_lock(&fp->fi_lock);
4686 list_add(&stp->st_perfile, &fp->fi_stateids);
4687 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 return stp;
4689}
4690
Jeff Laytonc53530d2014-06-30 11:48:39 -04004691static struct nfs4_ol_stateid *
4692find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4693{
4694 struct nfs4_ol_stateid *lst;
4695
4696 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
4697 if (lst->st_file == fp)
4698 return lst;
4699 }
4700 return NULL;
4701}
4702
4703
NeilBrownfd39ca92005-06-23 22:04:03 -07004704static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705check_lock_length(u64 offset, u64 length)
4706{
Benny Halevy87df4de2008-12-15 19:42:03 +02004707 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 LOFF_OVERFLOW(offset, length)));
4709}
4710
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004711static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004712{
4713 struct nfs4_file *fp = lock_stp->st_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004714
Jeff Layton7214e862014-07-10 14:07:33 -04004715 lockdep_assert_held(&fp->fi_lock);
4716
Jeff Layton82c5ff12012-05-11 09:45:13 -04004717 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004718 return;
Jeff Layton12659652014-07-10 14:07:28 -04004719 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004720 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004721}
4722
J. Bruce Fields2355c592012-04-25 16:56:22 -04004723static __be32 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, struct nfs4_ol_stateid *ost, struct nfsd4_lock *lock, struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004724{
4725 struct nfs4_file *fi = ost->st_file;
4726 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4727 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004728 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004729 struct nfs4_lockowner *lo;
4730 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004731 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004732
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004733 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004734 if (!lo) {
4735 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4736 &lock->v.new.owner);
4737 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4738 if (lo == NULL)
4739 return nfserr_jukebox;
4740 } else {
4741 /* with an existing lockowner, seqids must be the same */
4742 if (!cstate->minorversion &&
4743 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004744 return nfserr_bad_seqid;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004745 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004746
4747 *lst = find_lock_stateid(lo, fi);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004748 if (*lst == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004749 *lst = alloc_init_lock_stateid(lo, fi, inode, ost);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004750 if (*lst == NULL) {
4751 release_lockowner_if_empty(lo);
4752 return nfserr_jukebox;
4753 }
4754 *new = true;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004755 }
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004756 return nfs_ok;
4757}
4758
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759/*
4760 * LOCK operation
4761 */
Al Virob37ad282006-10-19 23:28:59 -07004762__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004763nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004764 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004766 struct nfs4_openowner *open_sop = NULL;
4767 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004768 struct nfs4_ol_stateid *lock_stp;
Jeff Layton7214e862014-07-10 14:07:33 -04004769 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004770 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004771 struct file_lock *file_lock = NULL;
4772 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004773 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004774 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004775 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004776 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004777 struct net *net = SVC_NET(rqstp);
4778 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779
4780 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4781 (long long) lock->lk_offset,
4782 (long long) lock->lk_length);
4783
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 if (check_lock_length(lock->lk_offset, lock->lk_length))
4785 return nfserr_inval;
4786
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004787 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004788 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004789 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4790 return status;
4791 }
4792
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 nfs4_lock_state();
4794
4795 if (lock->lk_is_new) {
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004796 struct nfs4_ol_stateid *open_stp = NULL;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004797
4798 if (nfsd4_has_session(cstate))
4799 /* See rfc 5661 18.10.3: given clientid is ignored: */
4800 memcpy(&lock->v.new.clientid,
4801 &cstate->session->se_client->cl_clientid,
4802 sizeof(clientid_t));
4803
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004805 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004809 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 lock->lk_new_open_seqid,
4811 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004812 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07004813 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004815 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004816 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004817 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004818 &lock->v.new.clientid))
4819 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004820 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4821 &lock_stp, &new_state);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004822 } else
Benny Halevydd453df2009-04-03 08:28:41 +03004823 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004824 lock->lk_old_lock_seqid,
4825 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004826 NFS4_LOCK_STID, &lock_stp, nn);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004827 if (status)
4828 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004829 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004831 lkflg = setlkflg(lock->lk_type);
4832 status = nfs4_check_openmode(lock_stp, lkflg);
4833 if (status)
4834 goto out;
4835
NeilBrown0dd395d2005-07-07 17:59:15 -07004836 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004837 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004838 goto out;
4839 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004840 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004841 goto out;
4842
Jeff Layton21179d82012-08-21 08:03:32 -04004843 file_lock = locks_alloc_lock();
4844 if (!file_lock) {
4845 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4846 status = nfserr_jukebox;
4847 goto out;
4848 }
4849
Jeff Layton7214e862014-07-10 14:07:33 -04004850 fp = lock_stp->st_file;
Jeff Layton21179d82012-08-21 08:03:32 -04004851 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 switch (lock->lk_type) {
4853 case NFS4_READ_LT:
4854 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004855 spin_lock(&fp->fi_lock);
4856 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004857 if (filp)
4858 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04004859 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004860 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004861 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 case NFS4_WRITE_LT:
4863 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004864 spin_lock(&fp->fi_lock);
4865 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004866 if (filp)
4867 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04004868 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004869 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004870 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 default:
4872 status = nfserr_inval;
4873 goto out;
4874 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004875 if (!filp) {
4876 status = nfserr_openmode;
4877 goto out;
4878 }
Jeff Layton21179d82012-08-21 08:03:32 -04004879 file_lock->fl_owner = (fl_owner_t)lock_sop;
4880 file_lock->fl_pid = current->tgid;
4881 file_lock->fl_file = filp;
4882 file_lock->fl_flags = FL_POSIX;
4883 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4884 file_lock->fl_start = lock->lk_offset;
4885 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4886 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887
Jeff Layton21179d82012-08-21 08:03:32 -04004888 conflock = locks_alloc_lock();
4889 if (!conflock) {
4890 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4891 status = nfserr_jukebox;
4892 goto out;
4893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894
Jeff Layton21179d82012-08-21 08:03:32 -04004895 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004896 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004898 update_stateid(&lock_stp->st_stid.sc_stateid);
4899 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004901 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004902 break;
4903 case (EAGAIN): /* conflock holds conflicting lock */
4904 status = nfserr_denied;
4905 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04004906 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004907 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 case (EDEADLK):
4909 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004910 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004911 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004912 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004913 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004914 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916out:
Trond Myklebustde186432014-07-10 14:07:26 -04004917 if (filp)
4918 fput(filp);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004919 if (status && new_state)
Jeff Laytonc53530d2014-06-30 11:48:39 -04004920 release_lock_stateid(lock_stp);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004921 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004922 if (!cstate->replay_owner)
4923 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004924 if (file_lock)
4925 locks_free_lock(file_lock);
4926 if (conflock)
4927 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 return status;
4929}
4930
4931/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004932 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4933 * so we do a temporary open here just to get an open file to pass to
4934 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4935 * inode operation.)
4936 */
Al Viro04da6e92012-04-13 00:00:04 -04004937static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004938{
4939 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04004940 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4941 if (!err) {
4942 err = nfserrno(vfs_test_lock(file, lock));
4943 nfsd_close(file);
4944 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004945 return err;
4946}
4947
4948/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 * LOCKT operation
4950 */
Al Virob37ad282006-10-19 23:28:59 -07004951__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004952nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4953 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954{
Jeff Layton21179d82012-08-21 08:03:32 -04004955 struct file_lock *file_lock = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004956 struct nfs4_lockowner *lo;
Al Virob37ad282006-10-19 23:28:59 -07004957 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004958 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004960 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 return nfserr_grace;
4962
4963 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4964 return nfserr_inval;
4965
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 nfs4_lock_state();
4967
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004968 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004969 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004970 if (status)
4971 goto out;
4972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004974 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976
Jeff Layton21179d82012-08-21 08:03:32 -04004977 file_lock = locks_alloc_lock();
4978 if (!file_lock) {
4979 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4980 status = nfserr_jukebox;
4981 goto out;
4982 }
4983 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 switch (lockt->lt_type) {
4985 case NFS4_READ_LT:
4986 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004987 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 break;
4989 case NFS4_WRITE_LT:
4990 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004991 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 break;
4993 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04004994 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 status = nfserr_inval;
4996 goto out;
4997 }
4998
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004999 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005000 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005001 file_lock->fl_owner = (fl_owner_t)lo;
5002 file_lock->fl_pid = current->tgid;
5003 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004
Jeff Layton21179d82012-08-21 08:03:32 -04005005 file_lock->fl_start = lockt->lt_offset;
5006 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007
Jeff Layton21179d82012-08-21 08:03:32 -04005008 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009
Jeff Layton21179d82012-08-21 08:03:32 -04005010 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005011 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005012 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005013
Jeff Layton21179d82012-08-21 08:03:32 -04005014 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005016 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 }
5018out:
5019 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005020 if (file_lock)
5021 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 return status;
5023}
5024
Al Virob37ad282006-10-19 23:28:59 -07005025__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005026nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005027 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005029 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005031 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005032 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005033 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005034 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5035
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5037 (long long) locku->lu_offset,
5038 (long long) locku->lu_length);
5039
5040 if (check_lock_length(locku->lu_offset, locku->lu_length))
5041 return nfserr_inval;
5042
5043 nfs4_lock_state();
5044
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005045 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005046 &locku->lu_stateid, NFS4_LOCK_STID,
5047 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005048 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005050 filp = find_any_file(stp->st_file);
5051 if (!filp) {
5052 status = nfserr_lock_range;
5053 goto out;
5054 }
Jeff Layton21179d82012-08-21 08:03:32 -04005055 file_lock = locks_alloc_lock();
5056 if (!file_lock) {
5057 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5058 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005059 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005060 }
5061 locks_init_lock(file_lock);
5062 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005063 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005064 file_lock->fl_pid = current->tgid;
5065 file_lock->fl_file = filp;
5066 file_lock->fl_flags = FL_POSIX;
5067 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5068 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069
Jeff Layton21179d82012-08-21 08:03:32 -04005070 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5071 locku->lu_length);
5072 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073
Jeff Layton21179d82012-08-21 08:03:32 -04005074 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005075 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005076 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 goto out_nfserr;
5078 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005079 update_stateid(&stp->st_stid.sc_stateid);
5080 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005081fput:
5082 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005084 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04005085 if (!cstate->replay_owner)
5086 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005087 if (file_lock)
5088 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 return status;
5090
5091out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005092 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005093 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094}
5095
5096/*
5097 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005098 * true: locks held by lockowner
5099 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005101static bool
5102check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103{
5104 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005105 int status = false;
5106 struct file *filp = find_any_file(fp);
5107 struct inode *inode;
5108
5109 if (!filp) {
5110 /* Any valid lock stateid should have some sort of access */
5111 WARN_ON_ONCE(1);
5112 return status;
5113 }
5114
5115 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116
Jeff Layton1c8c6012013-06-21 08:58:15 -04005117 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005119 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005120 status = true;
5121 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005124 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005125 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 return status;
5127}
5128
Al Virob37ad282006-10-19 23:28:59 -07005129__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005130nfsd4_release_lockowner(struct svc_rqst *rqstp,
5131 struct nfsd4_compound_state *cstate,
5132 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133{
5134 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Laytonfd449072014-06-30 11:48:41 -04005135 struct nfs4_stateowner *sop = NULL, *tmp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005136 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005137 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 struct xdr_netobj *owner = &rlockowner->rl_owner;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005139 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07005140 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005141 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142
5143 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5144 clid->cl_boot, clid->cl_id);
5145
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 nfs4_lock_state();
5147
Jeff Layton4b24ca72014-06-30 11:48:44 -04005148 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005149 if (status)
5150 goto out;
5151
NeilBrown3e9e3db2005-06-23 22:04:20 -07005152 status = nfserr_locks_held;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05005153
Jeff Laytonfd449072014-06-30 11:48:41 -04005154 /* Find the matching lock stateowner */
5155 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
5156 if (tmp->so_is_open_owner)
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005157 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005158 if (same_owner_str(tmp, owner, clid)) {
5159 sop = tmp;
5160 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07005162 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005163
5164 /* No matching owner found, maybe a replay? Just declare victory... */
5165 if (!sop) {
5166 status = nfs_ok;
5167 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005169
5170 lo = lockowner(sop);
5171 /* see if there are still any locks associated with it */
5172 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5173 if (check_for_locks(stp->st_file, lo))
5174 goto out;
5175 }
5176
5177 status = nfs_ok;
5178 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179out:
5180 nfs4_unlock_state();
5181 return status;
5182}
5183
5184static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005185alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186{
NeilBrowna55370a2005-06-23 22:03:52 -07005187 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188}
5189
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005190bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005191nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005192{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005193 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005194
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005195 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005196 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005197}
5198
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199/*
5200 * failure => all reset bets are off, nfserr_no_grace...
5201 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005202struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005203nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204{
5205 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005206 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207
NeilBrowna55370a2005-06-23 22:03:52 -07005208 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5209 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005210 if (crp) {
5211 strhashval = clientstr_hashval(name);
5212 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005213 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005214 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005215 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005216 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005217 }
5218 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219}
5220
Jeff Layton2a4317c2012-03-21 16:42:43 -04005221void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005222nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005223{
5224 list_del(&crp->cr_strhash);
5225 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005226 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005227}
5228
5229void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005230nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231{
5232 struct nfs4_client_reclaim *crp = NULL;
5233 int i;
5234
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005236 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5237 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005239 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 }
5241 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005242 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243}
5244
5245/*
5246 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005247struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005248nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249{
5250 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 struct nfs4_client_reclaim *crp = NULL;
5252
Jeff Layton278c9312012-11-12 15:00:52 -05005253 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254
Jeff Layton278c9312012-11-12 15:00:52 -05005255 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005256 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005257 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 return crp;
5259 }
5260 }
5261 return NULL;
5262}
5263
5264/*
5265* Called from OPEN. Look for clientid in reclaim list.
5266*/
Al Virob37ad282006-10-19 23:28:59 -07005267__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005268nfs4_check_open_reclaim(clientid_t *clid,
5269 struct nfsd4_compound_state *cstate,
5270 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005272 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005273
5274 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005275 status = lookup_clientid(clid, cstate, nn);
5276 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005277 return nfserr_reclaim_bad;
5278
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005279 if (nfsd4_client_record_check(cstate->clp))
5280 return nfserr_reclaim_bad;
5281
5282 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283}
5284
Bryan Schumaker65178db2011-11-01 13:35:21 -04005285#ifdef CONFIG_NFSD_FAULT_INJECTION
5286
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005287u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5288{
J. Bruce Fields221a6872013-04-01 22:23:49 -04005289 if (mark_client_expired(clp))
5290 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005291 expire_client(clp);
5292 return 1;
5293}
5294
Bryan Schumaker184c1842012-11-29 11:40:44 -05005295u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5296{
5297 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005298 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005299 printk(KERN_INFO "NFS Client: %s\n", buf);
5300 return 1;
5301}
5302
5303static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5304 const char *type)
5305{
5306 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005307 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005308 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5309}
5310
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005311static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5312 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005313{
5314 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005315 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005316 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005317 u64 count = 0;
5318
5319 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005320 list_for_each_entry_safe(stp, st_next,
5321 &oop->oo_owner.so_stateids, st_perstateowner) {
5322 list_for_each_entry_safe(lst, lst_next,
5323 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005324 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005325 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005326 if (++count == max)
5327 return count;
5328 }
5329 }
5330 }
5331
5332 return count;
5333}
5334
5335u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5336{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005337 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005338}
5339
Bryan Schumaker184c1842012-11-29 11:40:44 -05005340u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5341{
5342 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5343 nfsd_print_count(clp, count, "locked files");
5344 return count;
5345}
5346
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005347static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5348{
5349 struct nfs4_openowner *oop, *next;
5350 u64 count = 0;
5351
5352 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5353 if (func)
5354 func(oop);
5355 if (++count == max)
5356 break;
5357 }
5358
5359 return count;
5360}
5361
5362u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5363{
5364 return nfsd_foreach_client_open(clp, max, release_openowner);
5365}
5366
Bryan Schumaker184c1842012-11-29 11:40:44 -05005367u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5368{
5369 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5370 nfsd_print_count(clp, count, "open files");
5371 return count;
5372}
5373
Bryan Schumaker269de302012-11-29 11:40:42 -05005374static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5375 struct list_head *victims)
5376{
5377 struct nfs4_delegation *dp, *next;
5378 u64 count = 0;
5379
Benny Halevycdc97502014-05-30 09:09:30 -04005380 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005381 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005382 if (victims) {
5383 /*
5384 * It's not safe to mess with delegations that have a
5385 * non-zero dl_time. They might have already been broken
5386 * and could be processed by the laundromat outside of
5387 * the state_lock. Just leave them be.
5388 */
5389 if (dp->dl_time != 0)
5390 continue;
5391
Jeff Layton42690672014-07-25 07:34:20 -04005392 unhash_delegation_locked(dp);
5393 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005394 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005395 if (++count == max)
5396 break;
5397 }
5398 return count;
5399}
5400
5401u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5402{
5403 struct nfs4_delegation *dp, *next;
5404 LIST_HEAD(victims);
5405 u64 count;
5406
Benny Halevycdc97502014-05-30 09:09:30 -04005407 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005408 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005409 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005410
Jeff Layton2d4a5322014-07-25 07:34:21 -04005411 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5412 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005413 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005414 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005415
5416 return count;
5417}
5418
5419u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5420{
Jeff Laytondff13992014-07-08 14:02:49 -04005421 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005422 LIST_HEAD(victims);
5423 u64 count;
5424
Benny Halevycdc97502014-05-30 09:09:30 -04005425 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005426 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005427 while (!list_empty(&victims)) {
5428 dp = list_first_entry(&victims, struct nfs4_delegation,
5429 dl_recall_lru);
5430 list_del_init(&dp->dl_recall_lru);
5431 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005432 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005433 }
Benny Halevycdc97502014-05-30 09:09:30 -04005434 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005435
5436 return count;
5437}
5438
Bryan Schumaker184c1842012-11-29 11:40:44 -05005439u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5440{
5441 u64 count = 0;
5442
Benny Halevycdc97502014-05-30 09:09:30 -04005443 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005444 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005445 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005446
5447 nfsd_print_count(clp, count, "delegations");
5448 return count;
5449}
5450
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005451u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005452{
5453 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005454 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005455 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005456
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005457 if (!nfsd_netns_ready(nn))
5458 return 0;
5459
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005460 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005461 count += func(clp, max - count);
5462 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005463 break;
5464 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005465
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005466 return count;
5467}
5468
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005469struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5470{
5471 struct nfs4_client *clp;
5472 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5473
5474 if (!nfsd_netns_ready(nn))
5475 return NULL;
5476
5477 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5478 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5479 return clp;
5480 }
5481 return NULL;
5482}
5483
Bryan Schumaker65178db2011-11-01 13:35:21 -04005484#endif /* CONFIG_NFSD_FAULT_INJECTION */
5485
Meelap Shahc2f1a552007-07-17 04:04:39 -07005486/*
5487 * Since the lifetime of a delegation isn't limited to that of an open, a
5488 * client may quite reasonably hang on to a delegation as long as it has
5489 * the inode cached. This becomes an obvious problem the first time a
5490 * client's inode cache approaches the size of the server's total memory.
5491 *
5492 * For now we avoid this problem by imposing a hard limit on the number
5493 * of delegations, which varies according to the server's memory size.
5494 */
5495static void
5496set_max_delegations(void)
5497{
5498 /*
5499 * Allow at most 4 delegations per megabyte of RAM. Quick
5500 * estimates suggest that in the worst case (where every delegation
5501 * is for a different inode), a delegation could take about 1.5K,
5502 * giving a worst case usage of about 6% of memory.
5503 */
5504 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5505}
5506
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005507static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005508{
5509 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5510 int i;
5511
5512 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5513 CLIENT_HASH_SIZE, GFP_KERNEL);
5514 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005515 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005516 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5517 CLIENT_HASH_SIZE, GFP_KERNEL);
5518 if (!nn->unconf_id_hashtbl)
5519 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005520 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5521 OWNER_HASH_SIZE, GFP_KERNEL);
5522 if (!nn->ownerstr_hashtbl)
5523 goto err_ownerstr;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005524 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5525 SESSION_HASH_SIZE, GFP_KERNEL);
5526 if (!nn->sessionid_hashtbl)
5527 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005528
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005529 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005530 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005531 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005532 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005533 for (i = 0; i < OWNER_HASH_SIZE; i++)
5534 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005535 for (i = 0; i < SESSION_HASH_SIZE; i++)
5536 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005537 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005538 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005539 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005540 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005541 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005542 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005543
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005544 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005545 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005546
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005547 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005548
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005549err_sessionid:
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03005550 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005551err_ownerstr:
5552 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005553err_unconf_id:
5554 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005555err:
5556 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005557}
5558
5559static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005560nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005561{
5562 int i;
5563 struct nfs4_client *clp = NULL;
5564 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5565
5566 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5567 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5568 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5569 destroy_client(clp);
5570 }
5571 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005572
Kinglong Mee2b905632014-03-26 22:09:30 +08005573 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5574 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5575 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5576 destroy_client(clp);
5577 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005578 }
5579
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005580 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005581 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005582 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005583 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005584 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005585}
5586
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005587int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005588nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005589{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005590 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005591 int ret;
5592
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005593 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005594 if (ret)
5595 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005596 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005597 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005598 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005599 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005600 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005601 nn->nfsd4_grace, net);
5602 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005603 return 0;
5604}
5605
5606/* initialization to perform when the nfsd service is started: */
5607
5608int
5609nfs4_state_start(void)
5610{
5611 int ret;
5612
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005613 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005614 if (ret)
5615 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005616 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005617 if (laundry_wq == NULL) {
5618 ret = -ENOMEM;
5619 goto out_recovery;
5620 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005621 ret = nfsd4_create_callback_queue();
5622 if (ret)
5623 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005624
Meelap Shahc2f1a552007-07-17 04:04:39 -07005625 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005626
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005627 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005628
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005629out_free_laundry:
5630 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005631out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005632 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633}
5634
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005635void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005636nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005640 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005642 cancel_delayed_work_sync(&nn->laundromat_work);
5643 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005644
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005645 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04005647 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005648 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005650 unhash_delegation_locked(dp);
5651 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 }
Benny Halevycdc97502014-05-30 09:09:30 -04005653 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 list_for_each_safe(pos, next, &reaplist) {
5655 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005656 list_del_init(&dp->dl_recall_lru);
5657 nfs4_put_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 }
5659
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005660 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005661 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005662 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663}
5664
5665void
5666nfs4_state_shutdown(void)
5667{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005668 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005669 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005671
5672static void
5673get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5674{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005675 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5676 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005677}
5678
5679static void
5680put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5681{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005682 if (cstate->minorversion) {
5683 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5684 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5685 }
5686}
5687
5688void
5689clear_current_stateid(struct nfsd4_compound_state *cstate)
5690{
5691 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005692}
5693
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005694/*
5695 * functions to set current state id
5696 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005697void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005698nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5699{
5700 put_stateid(cstate, &odp->od_stateid);
5701}
5702
5703void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005704nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5705{
5706 put_stateid(cstate, &open->op_stateid);
5707}
5708
5709void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005710nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5711{
5712 put_stateid(cstate, &close->cl_stateid);
5713}
5714
5715void
5716nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5717{
5718 put_stateid(cstate, &lock->lk_resp_stateid);
5719}
5720
5721/*
5722 * functions to consume current state id
5723 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005724
5725void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005726nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5727{
5728 get_stateid(cstate, &odp->od_stateid);
5729}
5730
5731void
5732nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5733{
5734 get_stateid(cstate, &drp->dr_stateid);
5735}
5736
5737void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005738nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5739{
5740 get_stateid(cstate, &fsp->fr_stateid);
5741}
5742
5743void
5744nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5745{
5746 get_stateid(cstate, &setattr->sa_stateid);
5747}
5748
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005749void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005750nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5751{
5752 get_stateid(cstate, &close->cl_stateid);
5753}
5754
5755void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005756nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005757{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005758 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005759}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01005760
5761void
5762nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5763{
5764 get_stateid(cstate, &read->rd_stateid);
5765}
5766
5767void
5768nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5769{
5770 get_stateid(cstate, &write->wr_stateid);
5771}