blob: 85d7ac664691991c6438a82115bcd70626e92622 [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 *
Jeff Laytonf9416e22014-07-25 07:34:23 -0400582alloc_init_deleg(struct nfs4_client *clp, 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
Jeff Layton0b266932014-07-25 07:34:25 -04003555static struct nfs4_delegation *
3556nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3557 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003558{
Jeff Layton0b266932014-07-25 07:34:25 -04003559 int status;
3560 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003561
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003562 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003563 return ERR_PTR(-EAGAIN);
3564
3565 dp = alloc_init_deleg(clp, fh);
3566 if (!dp)
3567 return ERR_PTR(-ENOMEM);
3568
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003569 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003570 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003571 spin_lock(&fp->fi_lock);
3572 dp->dl_file = fp;
3573 if (!fp->fi_lease) {
3574 spin_unlock(&fp->fi_lock);
3575 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003576 status = nfs4_setlease(dp);
3577 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003578 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003579 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003580 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003581 status = -EAGAIN;
3582 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003583 }
Benny Halevy931ee562014-05-30 09:09:27 -04003584 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003585 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003586out_unlock:
3587 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003588 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003589out:
3590 if (status) {
3591 nfs4_put_delegation(dp);
3592 return ERR_PTR(status);
3593 }
3594 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003595}
3596
Benny Halevy4aa89132012-02-21 14:16:44 -08003597static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3598{
3599 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3600 if (status == -EAGAIN)
3601 open->op_why_no_deleg = WND4_CONTENTION;
3602 else {
3603 open->op_why_no_deleg = WND4_RESOURCE;
3604 switch (open->op_deleg_want) {
3605 case NFS4_SHARE_WANT_READ_DELEG:
3606 case NFS4_SHARE_WANT_WRITE_DELEG:
3607 case NFS4_SHARE_WANT_ANY_DELEG:
3608 break;
3609 case NFS4_SHARE_WANT_CANCEL:
3610 open->op_why_no_deleg = WND4_CANCELLED;
3611 break;
3612 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003613 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003614 }
3615 }
3616}
3617
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618/*
3619 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003620 *
3621 * Note we don't support write delegations, and won't until the vfs has
3622 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 */
3624static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003625nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3626 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627{
3628 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003629 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3630 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003631 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003632 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003634 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003635 open->op_recall = 0;
3636 switch (open->op_claim_type) {
3637 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003638 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003639 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003640 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3641 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003642 break;
3643 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003644 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003645 /*
3646 * Let's not give out any delegations till everyone's
3647 * had the chance to reclaim theirs....
3648 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003649 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003650 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003651 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003652 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003653 /*
3654 * Also, if the file was opened for write or
3655 * create, there's a good chance the client's
3656 * about to write to it, resulting in an
3657 * immediate recall (since we don't support
3658 * write delegations):
3659 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003660 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003661 goto out_no_deleg;
3662 if (open->op_create == NFS4_OPEN_CREATE)
3663 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003664 break;
3665 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003666 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003667 }
Jeff Layton0b266932014-07-25 07:34:25 -04003668 dp = nfs4_set_delegation(clp, fh, stp->st_file);
3669 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003670 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003672 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003674 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003675 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003676 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003677 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003678out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003679 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3680 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003681 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003682 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003683 open->op_recall = 1;
3684 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003685
3686 /* 4.1 client asking for a delegation? */
3687 if (open->op_deleg_want)
3688 nfsd4_open_deleg_none_ext(open, status);
3689 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690}
3691
Benny Halevye27f49c2012-02-21 14:16:54 -08003692static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3693 struct nfs4_delegation *dp)
3694{
3695 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3696 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3697 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3698 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3699 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3700 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3701 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3702 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3703 }
3704 /* Otherwise the client must be confused wanting a delegation
3705 * it already has, therefore we don't return
3706 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3707 */
3708}
3709
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710/*
3711 * called with nfs4_lock_state() held.
3712 */
Al Virob37ad282006-10-19 23:28:59 -07003713__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3715{
Andy Adamson66689582009-04-03 08:28:45 +03003716 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003717 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003719 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003720 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003721 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 /*
3724 * Lookup file; if found, lookup stateid and check open request,
3725 * and check for delegations in the process of being recalled.
3726 * If not found, create the nfs4_file struct
3727 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003728 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04003729 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04003730 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003731 if (status)
3732 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003733 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003735 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003736 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003737 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003738 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003739 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 }
3741
3742 /*
3743 * OPEN the file, or upgrade an existing OPEN.
3744 * If truncate fails, the OPEN fails.
3745 */
3746 if (stp) {
3747 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003748 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 if (status)
3750 goto out;
3751 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003752 stp = open->op_stp;
3753 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003754 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003755 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3756 if (status) {
3757 release_open_stateid(stp);
3758 goto out;
3759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003761 update_stateid(&stp->st_stid.sc_stateid);
3762 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763
Benny Halevyd24433c2012-02-16 20:57:17 +02003764 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02003765 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3766 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3767 open->op_why_no_deleg = WND4_NOT_WANTED;
3768 goto nodeleg;
3769 }
3770 }
3771
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 /*
3773 * Attempt to hand out a delegation. No error return, because the
3774 * OPEN succeeds even if we fail.
3775 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003776 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003777nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 status = nfs_ok;
3779
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003780 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003781 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003783 /* 4.1 client trying to upgrade/downgrade delegation? */
3784 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003785 open->op_deleg_want)
3786 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003787
NeilBrown13cd2182005-06-23 22:03:10 -07003788 if (fp)
3789 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003790 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003791 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 /*
3793 * To finish the open response, we just need to set the rflags.
3794 */
3795 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003796 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003797 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3799
3800 return status;
3801}
3802
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003803void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3804{
3805 if (open->op_openowner) {
3806 struct nfs4_openowner *oo = open->op_openowner;
3807
3808 if (!list_empty(&oo->oo_owner.so_stateids))
3809 list_del_init(&oo->oo_close_lru);
3810 if (oo->oo_flags & NFS4_OO_NEW) {
3811 if (status) {
3812 release_openowner(oo);
3813 open->op_openowner = NULL;
3814 } else
3815 oo->oo_flags &= ~NFS4_OO_NEW;
3816 }
3817 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003818 if (open->op_file)
3819 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003820 if (open->op_stp)
J. Bruce Fieldsef798592012-08-29 10:32:54 -07003821 free_generic_stateid(open->op_stp);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003822}
3823
Al Virob37ad282006-10-19 23:28:59 -07003824__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003825nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3826 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827{
3828 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003829 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003830 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831
3832 nfs4_lock_state();
3833 dprintk("process_renew(%08x/%08x): starting\n",
3834 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003835 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003836 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003838 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003840 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003841 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 goto out;
3843 status = nfs_ok;
3844out:
3845 nfs4_unlock_state();
3846 return status;
3847}
3848
NeilBrowna76b4312005-06-23 22:04:01 -07003849static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003850nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07003851{
Jeff Layton33dcc482012-04-10 11:08:48 -04003852 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003853 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003854 return;
3855
NeilBrowna76b4312005-06-23 22:04:01 -07003856 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003857 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003858 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003859 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003860 /*
3861 * Now that every NFSv4 client has had the chance to recover and
3862 * to see the (possibly new, possibly shorter) lease time, we
3863 * can safely set the next grace time to the current lease time:
3864 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03003865 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003866}
3867
NeilBrownfd39ca92005-06-23 22:04:03 -07003868static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003869nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870{
3871 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003872 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 struct nfs4_delegation *dp;
3874 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003875 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003876 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877
3878 nfs4_lock_state();
3879
3880 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003881 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03003882 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003883 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03003884 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 clp = list_entry(pos, struct nfs4_client, cl_lru);
3886 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3887 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003888 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 break;
3890 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003891 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03003892 dprintk("NFSD: client in use (clientid %08x)\n",
3893 clp->cl_clientid.cl_id);
3894 continue;
3895 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003896 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003897 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003898 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03003899 list_for_each_safe(pos, next, &reaplist) {
3900 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 dprintk("NFSD: purging unused client (clientid %08x)\n",
3902 clp->cl_clientid.cl_id);
3903 expire_client(clp);
3904 }
Benny Halevycdc97502014-05-30 09:09:30 -04003905 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003906 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03003908 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3909 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003911 t = dp->dl_time - cutoff;
3912 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 break;
3914 }
Jeff Layton42690672014-07-25 07:34:20 -04003915 unhash_delegation_locked(dp);
3916 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 }
Benny Halevycdc97502014-05-30 09:09:30 -04003918 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04003919 while (!list_empty(&reaplist)) {
3920 dp = list_first_entry(&reaplist, struct nfs4_delegation,
3921 dl_recall_lru);
3922 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003923 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003925 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003926 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3927 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003928 t = oo->oo_time - cutoff;
3929 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 break;
3931 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003932 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04003934 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04003936 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937}
3938
Harvey Harrisona254b242008-02-20 12:49:00 -08003939static struct workqueue_struct *laundry_wq;
3940static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08003941
3942static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003943laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944{
3945 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003946 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3947 work);
3948 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3949 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003951 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003953 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954}
3955
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003956static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003957{
Trond Myklebustb07c54a2014-07-23 16:17:40 -04003958 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003959 return nfserr_bad_stateid;
3960 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961}
3962
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003964access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003966 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3967 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3968 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969}
3970
3971static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003972access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003974 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3975 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976}
3977
3978static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003979__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980{
Al Virob37ad282006-10-19 23:28:59 -07003981 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
J. Bruce Fields02921912010-07-29 15:16:59 -04003983 /* For lock stateid's, we test the parent open, not the lock: */
3984 if (stp->st_openstp)
3985 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003986 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003988 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 goto out;
3990 status = nfs_ok;
3991out:
3992 return status;
3993}
3994
Al Virob37ad282006-10-19 23:28:59 -07003995static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003996check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003998 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004000 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004001 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 * conflicting state; so we must wait out the grace period. */
4003 return nfserr_grace;
4004 } else if (flags & WR_STATE)
4005 return nfs4_share_conflict(current_fh,
4006 NFS4_SHARE_DENY_WRITE);
4007 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4008 return nfs4_share_conflict(current_fh,
4009 NFS4_SHARE_DENY_READ);
4010}
4011
4012/*
4013 * Allow READ/WRITE during grace period on recovered state only for files
4014 * that are not able to provide mandatory locking.
4015 */
4016static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004017grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004019 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020}
4021
J. Bruce Fields81b82962011-08-23 11:03:29 -04004022/* Returns true iff a is later than b: */
4023static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4024{
Jim Rees1a9357f2013-05-17 17:33:00 -04004025 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004026}
4027
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004028static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004029{
Andy Adamson66689582009-04-03 08:28:45 +03004030 /*
4031 * When sessions are used the stateid generation number is ignored
4032 * when it is zero.
4033 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004034 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004035 return nfs_ok;
4036
4037 if (in->si_generation == ref->si_generation)
4038 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004039
J. Bruce Fields0836f582008-01-26 19:08:12 -05004040 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004041 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004042 return nfserr_bad_stateid;
4043 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004044 * However, we could see a stateid from the past, even from a
4045 * non-buggy client. For example, if the client sends a lock
4046 * while some IO is outstanding, the lock may bump si_generation
4047 * while the IO is still in flight. The client could avoid that
4048 * situation by waiting for responses on all the IO requests,
4049 * but better performance may result in retrying IO that
4050 * receives an old_stateid error if requests are rarely
4051 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004052 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004053 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004054}
4055
Chuck Lever7df302f2012-05-29 13:56:37 -04004056static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004057{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004058 struct nfs4_stid *s;
4059 struct nfs4_ol_stateid *ols;
4060 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004061
Chuck Lever7df302f2012-05-29 13:56:37 -04004062 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4063 return nfserr_bad_stateid;
4064 /* Client debugging aid. */
4065 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4066 char addr_str[INET6_ADDRSTRLEN];
4067 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4068 sizeof(addr_str));
4069 pr_warn_ratelimited("NFSD: client %s testing state ID "
4070 "with incorrect client ID\n", addr_str);
4071 return nfserr_bad_stateid;
4072 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004073 s = find_stateid(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004074 if (!s)
Chuck Lever7df302f2012-05-29 13:56:37 -04004075 return nfserr_bad_stateid;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004076 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004077 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004078 return status;
J. Bruce Fields23340032013-04-09 17:42:28 -04004079 switch (s->sc_type) {
4080 case NFS4_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004081 return nfs_ok;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004082 case NFS4_REVOKED_DELEG_STID:
4083 return nfserr_deleg_revoked;
J. Bruce Fields23340032013-04-09 17:42:28 -04004084 case NFS4_OPEN_STID:
4085 case NFS4_LOCK_STID:
4086 ols = openlockstateid(s);
4087 if (ols->st_stateowner->so_is_open_owner
4088 && !(openowner(ols->st_stateowner)->oo_flags
4089 & NFS4_OO_CONFIRMED))
4090 return nfserr_bad_stateid;
4091 return nfs_ok;
4092 default:
4093 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004094 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004095 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004096 case NFS4_CLOSED_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004097 return nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004098 }
Bryan Schumaker17456802011-07-13 10:50:48 -04004099}
4100
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004101static __be32
4102nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4103 stateid_t *stateid, unsigned char typemask,
4104 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004105{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004106 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004107
4108 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4109 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004110 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004111 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004112 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004113 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004114 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004115 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004116 if (status)
4117 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004118 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004119 if (!*s)
4120 return nfserr_bad_stateid;
4121 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004122}
4123
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124/*
4125* Checks for stateid operations
4126*/
Al Virob37ad282006-10-19 23:28:59 -07004127__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004128nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004129 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004131 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004132 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004134 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004136 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004137 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004138 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 if (filpp)
4141 *filpp = NULL;
4142
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004143 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 return nfserr_grace;
4145
4146 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004147 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148
Trond Myklebust14bcab12014-04-18 14:44:07 -04004149 nfs4_lock_state();
4150
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004151 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004152 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004153 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004154 if (status)
Trond Myklebust14bcab12014-04-18 14:44:07 -04004155 goto out;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004156 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4157 if (status)
4158 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004159 switch (s->sc_type) {
4160 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004161 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004162 status = nfs4_check_delegmode(dp, flags);
4163 if (status)
4164 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004165 if (filpp) {
Trond Myklebust14bcab12014-04-18 14:44:07 -04004166 file = dp->dl_file->fi_deleg_file;
4167 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004168 WARN_ON_ONCE(1);
4169 status = nfserr_serverfault;
4170 goto out;
4171 }
Trond Myklebustde186432014-07-10 14:07:26 -04004172 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004173 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004174 break;
4175 case NFS4_OPEN_STID:
4176 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004177 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004178 status = nfs4_check_fh(current_fh, stp);
4179 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004181 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004182 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004184 status = nfs4_check_openmode(stp, flags);
4185 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004187 if (filpp) {
4188 if (flags & RD_STATE)
Trond Myklebust14bcab12014-04-18 14:44:07 -04004189 file = find_readable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004190 else
Trond Myklebust14bcab12014-04-18 14:44:07 -04004191 file = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004192 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004193 break;
4194 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004195 status = nfserr_bad_stateid;
4196 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 }
4198 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004199 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004200 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201out:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004202 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 return status;
4204}
4205
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004206static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004207nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004208{
J. Bruce Fieldsa1b8ff42014-05-20 15:55:21 -04004209 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4210
4211 if (check_for_locks(stp->st_file, lo))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004212 return nfserr_locks_held;
Jeff Laytonc53530d2014-06-30 11:48:39 -04004213 release_lockowner_if_empty(lo);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004214 return nfs_ok;
4215}
4216
4217/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004218 * Test if the stateid is valid
4219 */
4220__be32
4221nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4222 struct nfsd4_test_stateid *test_stateid)
4223{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004224 struct nfsd4_test_stateid_id *stateid;
4225 struct nfs4_client *cl = cstate->session->se_client;
4226
4227 nfs4_lock_state();
4228 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004229 stateid->ts_id_status =
4230 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004231 nfs4_unlock_state();
4232
Bryan Schumaker17456802011-07-13 10:50:48 -04004233 return nfs_ok;
4234}
4235
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004236__be32
4237nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4238 struct nfsd4_free_stateid *free_stateid)
4239{
4240 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004241 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004242 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004243 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004244 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004245
4246 nfs4_lock_state();
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004247 s = find_stateid(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004248 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004249 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004250 switch (s->sc_type) {
4251 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004252 ret = nfserr_locks_held;
4253 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004254 case NFS4_OPEN_STID:
4255 case NFS4_LOCK_STID:
4256 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4257 if (ret)
4258 goto out;
4259 if (s->sc_type == NFS4_LOCK_STID)
4260 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4261 else
4262 ret = nfserr_locks_held;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004263 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004264 case NFS4_REVOKED_DELEG_STID:
4265 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004266 spin_lock(&cl->cl_lock);
4267 list_del_init(&dp->dl_recall_lru);
4268 spin_unlock(&cl->cl_lock);
4269 nfs4_put_delegation(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004270 ret = nfs_ok;
4271 break;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004272 default:
4273 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004274 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004275out:
4276 nfs4_unlock_state();
4277 return ret;
4278}
4279
NeilBrown4c4cd222005-07-07 17:59:27 -07004280static inline int
4281setlkflg (int type)
4282{
4283 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4284 RD_STATE : WR_STATE;
4285}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004287static __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 -04004288{
4289 struct svc_fh *current_fh = &cstate->current_fh;
4290 struct nfs4_stateowner *sop = stp->st_stateowner;
4291 __be32 status;
4292
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004293 status = nfsd4_check_seqid(cstate, sop, seqid);
4294 if (status)
4295 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004296 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4297 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004298 /*
4299 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004300 * nfserr_replay_me from the previous step, and
4301 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004302 */
4303 return nfserr_bad_stateid;
4304 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4305 if (status)
4306 return status;
4307 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004308}
4309
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310/*
4311 * Checks for sequence id mutating operations.
4312 */
Al Virob37ad282006-10-19 23:28:59 -07004313static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004314nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004315 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004316 struct nfs4_ol_stateid **stpp,
4317 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004319 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004320 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004321 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004323 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4324 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004325
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004327 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004328 if (status)
4329 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004330 stp = openlockstateid(s);
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04004331 if (!nfsd4_has_session(cstate))
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004332 cstate->replay_owner = stp->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004334 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4335 if (!status)
4336 *stpp = stp;
4337 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004338}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004339
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004340static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4341 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004342{
4343 __be32 status;
4344 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004346 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004347 NFS4_OPEN_STID, stpp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004348 if (status)
4349 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004350 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004351 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07004352 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004353 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354}
4355
Al Virob37ad282006-10-19 23:28:59 -07004356__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004357nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004358 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359{
Al Virob37ad282006-10-19 23:28:59 -07004360 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004361 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004362 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004363 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364
Al Viroa6a9f182013-09-16 10:57:01 -04004365 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4366 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004368 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004369 if (status)
4370 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371
4372 nfs4_lock_state();
4373
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004374 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004375 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004376 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004377 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004378 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004379 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004380 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004381 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004382 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004383 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004384 update_stateid(&stp->st_stid.sc_stateid);
4385 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004386 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004387 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004388
Jeff Layton2a4317c2012-03-21 16:42:43 -04004389 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004390 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004392 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004393 if (!cstate->replay_owner)
4394 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 return status;
4396}
4397
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004398static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004400 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004401 return;
Jeff Layton12659652014-07-10 14:07:28 -04004402 nfs4_file_put_access(stp->st_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004403 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004404}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004405
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004406static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4407{
4408 switch (to_access) {
4409 case NFS4_SHARE_ACCESS_READ:
4410 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4411 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4412 break;
4413 case NFS4_SHARE_ACCESS_WRITE:
4414 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4415 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4416 break;
4417 case NFS4_SHARE_ACCESS_BOTH:
4418 break;
4419 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004420 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 }
4422}
4423
Al Virob37ad282006-10-19 23:28:59 -07004424__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004425nfsd4_open_downgrade(struct svc_rqst *rqstp,
4426 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004427 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428{
Al Virob37ad282006-10-19 23:28:59 -07004429 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004430 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004431 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
Al Viroa6a9f182013-09-16 10:57:01 -04004433 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4434 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004436 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004437 if (od->od_deleg_want)
4438 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4439 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440
4441 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004442 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004443 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004444 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004447 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004448 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 stp->st_access_bmap, od->od_share_access);
4450 goto out;
4451 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004452 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004453 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 stp->st_deny_bmap, od->od_share_deny);
4455 goto out;
4456 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004457 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458
Jeff Laytonce0fc432012-05-11 09:45:14 -04004459 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004461 update_stateid(&stp->st_stid.sc_stateid);
4462 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 status = nfs_ok;
4464out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004465 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004466 if (!cstate->replay_owner)
4467 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 return status;
4469}
4470
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004471static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4472{
Trond Myklebustacf92952014-06-30 11:48:37 -04004473 struct nfs4_client *clp = s->st_stid.sc_client;
4474 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4475
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004476 s->st_stid.sc_type = NFS4_CLOSED_STID;
Trond Myklebustacf92952014-06-30 11:48:37 -04004477 unhash_open_stateid(s);
4478
4479 if (clp->cl_minorversion) {
4480 free_generic_stateid(s);
4481 if (list_empty(&oo->oo_owner.so_stateids))
4482 release_openowner(oo);
4483 } else {
Trond Myklebustf8338832014-07-25 07:34:19 -04004484 if (s->st_file) {
4485 put_nfs4_file(s->st_file);
4486 s->st_file = NULL;
4487 }
Trond Myklebustacf92952014-06-30 11:48:37 -04004488 oo->oo_last_closed_stid = s;
4489 /*
4490 * In the 4.0 case we need to keep the owners around a
4491 * little while to handle CLOSE replay.
4492 */
4493 if (list_empty(&oo->oo_owner.so_stateids))
4494 move_to_close_lru(oo, clp->net);
4495 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004496}
4497
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498/*
4499 * nfs4_unlock_state() called after encode
4500 */
Al Virob37ad282006-10-19 23:28:59 -07004501__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004502nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004503 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504{
Al Virob37ad282006-10-19 23:28:59 -07004505 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004506 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004507 struct net *net = SVC_NET(rqstp);
4508 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509
Al Viroa6a9f182013-09-16 10:57:01 -04004510 dprintk("NFSD: nfsd4_close on file %pd\n",
4511 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512
4513 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004514 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4515 &close->cl_stateid,
4516 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004517 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004518 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004519 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004521 update_stateid(&stp->st_stid.sc_stateid);
4522 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004524 nfsd4_close_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004526 if (!cstate->replay_owner)
4527 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 return status;
4529}
4530
Al Virob37ad282006-10-19 23:28:59 -07004531__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004532nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4533 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004535 struct nfs4_delegation *dp;
4536 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004537 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004538 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004539 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004541 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004542 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543
4544 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004545 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004546 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004547 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004548 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004549 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004550 if (status)
4551 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004552
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004553 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004555 nfs4_unlock_state();
4556
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 return status;
4558}
4559
4560
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562
Benny Halevy87df4de2008-12-15 19:42:03 +02004563static inline u64
4564end_offset(u64 start, u64 len)
4565{
4566 u64 end;
4567
4568 end = start + len;
4569 return end >= start ? end: NFS4_MAX_UINT64;
4570}
4571
4572/* last octet in a range */
4573static inline u64
4574last_byte_offset(u64 start, u64 len)
4575{
4576 u64 end;
4577
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004578 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004579 end = start + len;
4580 return end > start ? end - 1: NFS4_MAX_UINT64;
4581}
4582
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583/*
4584 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4585 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4586 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4587 * locking, this prevents us from being completely protocol-compliant. The
4588 * real solution to this problem is to start using unsigned file offsets in
4589 * the VFS, but this is a very deep change!
4590 */
4591static inline void
4592nfs4_transform_lock_offset(struct file_lock *lock)
4593{
4594 if (lock->fl_start < 0)
4595 lock->fl_start = OFFSET_MAX;
4596 if (lock->fl_end < 0)
4597 lock->fl_end = OFFSET_MAX;
4598}
4599
NeilBrownd5b90262006-04-10 22:55:22 -07004600/* Hack!: For now, we're defining this just so we can use a pointer to it
4601 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004602static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004603};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
4605static inline void
4606nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4607{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004608 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609
NeilBrownd5b90262006-04-10 22:55:22 -07004610 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004611 lo = (struct nfs4_lockowner *) fl->fl_owner;
4612 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4613 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004614 if (!deny->ld_owner.data)
4615 /* We just don't care that much */
4616 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004617 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4618 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004619 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004620nevermind:
4621 deny->ld_owner.len = 0;
4622 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004623 deny->ld_clientid.cl_boot = 0;
4624 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 }
4626 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004627 deny->ld_length = NFS4_MAX_UINT64;
4628 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4630 deny->ld_type = NFS4_READ_LT;
4631 if (fl->fl_type != F_RDLCK)
4632 deny->ld_type = NFS4_WRITE_LT;
4633}
4634
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004635static struct nfs4_lockowner *
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004636find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4637 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638{
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004639 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4640 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004642 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4643 if (so->so_is_open_owner)
4644 continue;
4645 if (!same_owner_str(so, owner, clid))
4646 continue;
4647 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 }
4649 return NULL;
4650}
4651
4652/*
4653 * Alloc a lock owner structure.
4654 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004655 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004657 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004659static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004660alloc_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 -04004661 struct nfs4_lockowner *lo;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004662 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004664 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4665 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004667 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4668 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07004669 /* It is the openowner seqid that will be incremented in encode in the
4670 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004671 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004672 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004673 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674}
4675
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004676static struct nfs4_ol_stateid *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004677alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp,
4678 struct inode *inode,
4679 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004681 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004682 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683
J. Bruce Fields996e0932011-10-17 11:14:48 -04004684 stp = nfs4_alloc_stateid(clp);
NeilBrown5ac049a2005-06-23 22:03:03 -07004685 if (stp == NULL)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04004686 return NULL;
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004687 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004688 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4689 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004690 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 stp->st_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004692 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004694 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004695 list_add(&stp->st_locks, &open_stp->st_locks);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004696 spin_lock(&fp->fi_lock);
4697 list_add(&stp->st_perfile, &fp->fi_stateids);
4698 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 return stp;
4700}
4701
Jeff Laytonc53530d2014-06-30 11:48:39 -04004702static struct nfs4_ol_stateid *
4703find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4704{
4705 struct nfs4_ol_stateid *lst;
4706
4707 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
4708 if (lst->st_file == fp)
4709 return lst;
4710 }
4711 return NULL;
4712}
4713
4714
NeilBrownfd39ca92005-06-23 22:04:03 -07004715static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716check_lock_length(u64 offset, u64 length)
4717{
Benny Halevy87df4de2008-12-15 19:42:03 +02004718 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 LOFF_OVERFLOW(offset, length)));
4720}
4721
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004722static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004723{
4724 struct nfs4_file *fp = lock_stp->st_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004725
Jeff Layton7214e862014-07-10 14:07:33 -04004726 lockdep_assert_held(&fp->fi_lock);
4727
Jeff Layton82c5ff12012-05-11 09:45:13 -04004728 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004729 return;
Jeff Layton12659652014-07-10 14:07:28 -04004730 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004731 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004732}
4733
J. Bruce Fields2355c592012-04-25 16:56:22 -04004734static __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 -04004735{
4736 struct nfs4_file *fi = ost->st_file;
4737 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4738 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004739 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004740 struct nfs4_lockowner *lo;
4741 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004742 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004743
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004744 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004745 if (!lo) {
4746 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4747 &lock->v.new.owner);
4748 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4749 if (lo == NULL)
4750 return nfserr_jukebox;
4751 } else {
4752 /* with an existing lockowner, seqids must be the same */
4753 if (!cstate->minorversion &&
4754 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004755 return nfserr_bad_seqid;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004756 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004757
4758 *lst = find_lock_stateid(lo, fi);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004759 if (*lst == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004760 *lst = alloc_init_lock_stateid(lo, fi, inode, ost);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004761 if (*lst == NULL) {
4762 release_lockowner_if_empty(lo);
4763 return nfserr_jukebox;
4764 }
4765 *new = true;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004766 }
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004767 return nfs_ok;
4768}
4769
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770/*
4771 * LOCK operation
4772 */
Al Virob37ad282006-10-19 23:28:59 -07004773__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004774nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004775 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004777 struct nfs4_openowner *open_sop = NULL;
4778 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004779 struct nfs4_ol_stateid *lock_stp;
Jeff Layton7214e862014-07-10 14:07:33 -04004780 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004781 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004782 struct file_lock *file_lock = NULL;
4783 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004784 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004785 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004786 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004787 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004788 struct net *net = SVC_NET(rqstp);
4789 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790
4791 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4792 (long long) lock->lk_offset,
4793 (long long) lock->lk_length);
4794
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 if (check_lock_length(lock->lk_offset, lock->lk_length))
4796 return nfserr_inval;
4797
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004798 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004799 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004800 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4801 return status;
4802 }
4803
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 nfs4_lock_state();
4805
4806 if (lock->lk_is_new) {
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004807 struct nfs4_ol_stateid *open_stp = NULL;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004808
4809 if (nfsd4_has_session(cstate))
4810 /* See rfc 5661 18.10.3: given clientid is ignored: */
4811 memcpy(&lock->v.new.clientid,
4812 &cstate->session->se_client->cl_clientid,
4813 sizeof(clientid_t));
4814
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004816 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004820 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 lock->lk_new_open_seqid,
4822 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004823 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07004824 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004826 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004827 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004828 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004829 &lock->v.new.clientid))
4830 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004831 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4832 &lock_stp, &new_state);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004833 } else
Benny Halevydd453df2009-04-03 08:28:41 +03004834 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004835 lock->lk_old_lock_seqid,
4836 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004837 NFS4_LOCK_STID, &lock_stp, nn);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004838 if (status)
4839 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004840 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004842 lkflg = setlkflg(lock->lk_type);
4843 status = nfs4_check_openmode(lock_stp, lkflg);
4844 if (status)
4845 goto out;
4846
NeilBrown0dd395d2005-07-07 17:59:15 -07004847 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004848 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004849 goto out;
4850 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004851 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004852 goto out;
4853
Jeff Layton21179d82012-08-21 08:03:32 -04004854 file_lock = locks_alloc_lock();
4855 if (!file_lock) {
4856 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4857 status = nfserr_jukebox;
4858 goto out;
4859 }
4860
Jeff Layton7214e862014-07-10 14:07:33 -04004861 fp = lock_stp->st_file;
Jeff Layton21179d82012-08-21 08:03:32 -04004862 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 switch (lock->lk_type) {
4864 case NFS4_READ_LT:
4865 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004866 spin_lock(&fp->fi_lock);
4867 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004868 if (filp)
4869 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04004870 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004871 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004872 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 case NFS4_WRITE_LT:
4874 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004875 spin_lock(&fp->fi_lock);
4876 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004877 if (filp)
4878 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04004879 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004880 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004881 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 default:
4883 status = nfserr_inval;
4884 goto out;
4885 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004886 if (!filp) {
4887 status = nfserr_openmode;
4888 goto out;
4889 }
Jeff Layton21179d82012-08-21 08:03:32 -04004890 file_lock->fl_owner = (fl_owner_t)lock_sop;
4891 file_lock->fl_pid = current->tgid;
4892 file_lock->fl_file = filp;
4893 file_lock->fl_flags = FL_POSIX;
4894 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4895 file_lock->fl_start = lock->lk_offset;
4896 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4897 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898
Jeff Layton21179d82012-08-21 08:03:32 -04004899 conflock = locks_alloc_lock();
4900 if (!conflock) {
4901 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4902 status = nfserr_jukebox;
4903 goto out;
4904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905
Jeff Layton21179d82012-08-21 08:03:32 -04004906 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004907 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004909 update_stateid(&lock_stp->st_stid.sc_stateid);
4910 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004912 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004913 break;
4914 case (EAGAIN): /* conflock holds conflicting lock */
4915 status = nfserr_denied;
4916 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04004917 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004918 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 case (EDEADLK):
4920 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004921 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004922 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004923 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004924 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004925 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927out:
Trond Myklebustde186432014-07-10 14:07:26 -04004928 if (filp)
4929 fput(filp);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004930 if (status && new_state)
Jeff Laytonc53530d2014-06-30 11:48:39 -04004931 release_lock_stateid(lock_stp);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004932 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004933 if (!cstate->replay_owner)
4934 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004935 if (file_lock)
4936 locks_free_lock(file_lock);
4937 if (conflock)
4938 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 return status;
4940}
4941
4942/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004943 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4944 * so we do a temporary open here just to get an open file to pass to
4945 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4946 * inode operation.)
4947 */
Al Viro04da6e92012-04-13 00:00:04 -04004948static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004949{
4950 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04004951 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4952 if (!err) {
4953 err = nfserrno(vfs_test_lock(file, lock));
4954 nfsd_close(file);
4955 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004956 return err;
4957}
4958
4959/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 * LOCKT operation
4961 */
Al Virob37ad282006-10-19 23:28:59 -07004962__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004963nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4964 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965{
Jeff Layton21179d82012-08-21 08:03:32 -04004966 struct file_lock *file_lock = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004967 struct nfs4_lockowner *lo;
Al Virob37ad282006-10-19 23:28:59 -07004968 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004969 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004971 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 return nfserr_grace;
4973
4974 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4975 return nfserr_inval;
4976
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 nfs4_lock_state();
4978
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004979 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004980 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004981 if (status)
4982 goto out;
4983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004985 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987
Jeff Layton21179d82012-08-21 08:03:32 -04004988 file_lock = locks_alloc_lock();
4989 if (!file_lock) {
4990 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4991 status = nfserr_jukebox;
4992 goto out;
4993 }
4994 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 switch (lockt->lt_type) {
4996 case NFS4_READ_LT:
4997 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004998 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 break;
5000 case NFS4_WRITE_LT:
5001 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005002 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 break;
5004 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005005 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 status = nfserr_inval;
5007 goto out;
5008 }
5009
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005010 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005011 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005012 file_lock->fl_owner = (fl_owner_t)lo;
5013 file_lock->fl_pid = current->tgid;
5014 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015
Jeff Layton21179d82012-08-21 08:03:32 -04005016 file_lock->fl_start = lockt->lt_offset;
5017 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018
Jeff Layton21179d82012-08-21 08:03:32 -04005019 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020
Jeff Layton21179d82012-08-21 08:03:32 -04005021 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005022 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005023 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005024
Jeff Layton21179d82012-08-21 08:03:32 -04005025 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005027 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 }
5029out:
5030 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005031 if (file_lock)
5032 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 return status;
5034}
5035
Al Virob37ad282006-10-19 23:28:59 -07005036__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005037nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005038 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005040 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005042 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005043 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005044 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005045 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5046
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5048 (long long) locku->lu_offset,
5049 (long long) locku->lu_length);
5050
5051 if (check_lock_length(locku->lu_offset, locku->lu_length))
5052 return nfserr_inval;
5053
5054 nfs4_lock_state();
5055
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005056 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005057 &locku->lu_stateid, NFS4_LOCK_STID,
5058 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005059 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005061 filp = find_any_file(stp->st_file);
5062 if (!filp) {
5063 status = nfserr_lock_range;
5064 goto out;
5065 }
Jeff Layton21179d82012-08-21 08:03:32 -04005066 file_lock = locks_alloc_lock();
5067 if (!file_lock) {
5068 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5069 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005070 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005071 }
5072 locks_init_lock(file_lock);
5073 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005074 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005075 file_lock->fl_pid = current->tgid;
5076 file_lock->fl_file = filp;
5077 file_lock->fl_flags = FL_POSIX;
5078 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5079 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080
Jeff Layton21179d82012-08-21 08:03:32 -04005081 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5082 locku->lu_length);
5083 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084
Jeff Layton21179d82012-08-21 08:03:32 -04005085 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005086 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005087 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 goto out_nfserr;
5089 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005090 update_stateid(&stp->st_stid.sc_stateid);
5091 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005092fput:
5093 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005095 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04005096 if (!cstate->replay_owner)
5097 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005098 if (file_lock)
5099 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 return status;
5101
5102out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005103 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005104 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105}
5106
5107/*
5108 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005109 * true: locks held by lockowner
5110 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005112static bool
5113check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114{
5115 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005116 int status = false;
5117 struct file *filp = find_any_file(fp);
5118 struct inode *inode;
5119
5120 if (!filp) {
5121 /* Any valid lock stateid should have some sort of access */
5122 WARN_ON_ONCE(1);
5123 return status;
5124 }
5125
5126 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127
Jeff Layton1c8c6012013-06-21 08:58:15 -04005128 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005130 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005131 status = true;
5132 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005135 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005136 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 return status;
5138}
5139
Al Virob37ad282006-10-19 23:28:59 -07005140__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005141nfsd4_release_lockowner(struct svc_rqst *rqstp,
5142 struct nfsd4_compound_state *cstate,
5143 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144{
5145 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Laytonfd449072014-06-30 11:48:41 -04005146 struct nfs4_stateowner *sop = NULL, *tmp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005147 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005148 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 struct xdr_netobj *owner = &rlockowner->rl_owner;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005150 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07005151 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005152 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153
5154 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5155 clid->cl_boot, clid->cl_id);
5156
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 nfs4_lock_state();
5158
Jeff Layton4b24ca72014-06-30 11:48:44 -04005159 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005160 if (status)
5161 goto out;
5162
NeilBrown3e9e3db2005-06-23 22:04:20 -07005163 status = nfserr_locks_held;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05005164
Jeff Laytonfd449072014-06-30 11:48:41 -04005165 /* Find the matching lock stateowner */
5166 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
5167 if (tmp->so_is_open_owner)
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005168 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005169 if (same_owner_str(tmp, owner, clid)) {
5170 sop = tmp;
5171 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07005173 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005174
5175 /* No matching owner found, maybe a replay? Just declare victory... */
5176 if (!sop) {
5177 status = nfs_ok;
5178 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005180
5181 lo = lockowner(sop);
5182 /* see if there are still any locks associated with it */
5183 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5184 if (check_for_locks(stp->st_file, lo))
5185 goto out;
5186 }
5187
5188 status = nfs_ok;
5189 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190out:
5191 nfs4_unlock_state();
5192 return status;
5193}
5194
5195static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005196alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197{
NeilBrowna55370a2005-06-23 22:03:52 -07005198 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199}
5200
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005201bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005202nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005203{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005204 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005205
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005206 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005207 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005208}
5209
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210/*
5211 * failure => all reset bets are off, nfserr_no_grace...
5212 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005213struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005214nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215{
5216 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005217 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218
NeilBrowna55370a2005-06-23 22:03:52 -07005219 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5220 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005221 if (crp) {
5222 strhashval = clientstr_hashval(name);
5223 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005224 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005225 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005226 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005227 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005228 }
5229 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230}
5231
Jeff Layton2a4317c2012-03-21 16:42:43 -04005232void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005233nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005234{
5235 list_del(&crp->cr_strhash);
5236 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005237 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005238}
5239
5240void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005241nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242{
5243 struct nfs4_client_reclaim *crp = NULL;
5244 int i;
5245
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005247 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5248 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005250 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 }
5252 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005253 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254}
5255
5256/*
5257 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005258struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005259nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260{
5261 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 struct nfs4_client_reclaim *crp = NULL;
5263
Jeff Layton278c9312012-11-12 15:00:52 -05005264 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265
Jeff Layton278c9312012-11-12 15:00:52 -05005266 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005267 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005268 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 return crp;
5270 }
5271 }
5272 return NULL;
5273}
5274
5275/*
5276* Called from OPEN. Look for clientid in reclaim list.
5277*/
Al Virob37ad282006-10-19 23:28:59 -07005278__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005279nfs4_check_open_reclaim(clientid_t *clid,
5280 struct nfsd4_compound_state *cstate,
5281 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005283 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005284
5285 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005286 status = lookup_clientid(clid, cstate, nn);
5287 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005288 return nfserr_reclaim_bad;
5289
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005290 if (nfsd4_client_record_check(cstate->clp))
5291 return nfserr_reclaim_bad;
5292
5293 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294}
5295
Bryan Schumaker65178db2011-11-01 13:35:21 -04005296#ifdef CONFIG_NFSD_FAULT_INJECTION
5297
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005298u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5299{
J. Bruce Fields221a6872013-04-01 22:23:49 -04005300 if (mark_client_expired(clp))
5301 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005302 expire_client(clp);
5303 return 1;
5304}
5305
Bryan Schumaker184c1842012-11-29 11:40:44 -05005306u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5307{
5308 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005309 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005310 printk(KERN_INFO "NFS Client: %s\n", buf);
5311 return 1;
5312}
5313
5314static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5315 const char *type)
5316{
5317 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005318 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005319 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5320}
5321
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005322static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5323 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005324{
5325 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005326 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005327 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005328 u64 count = 0;
5329
5330 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005331 list_for_each_entry_safe(stp, st_next,
5332 &oop->oo_owner.so_stateids, st_perstateowner) {
5333 list_for_each_entry_safe(lst, lst_next,
5334 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005335 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005336 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005337 if (++count == max)
5338 return count;
5339 }
5340 }
5341 }
5342
5343 return count;
5344}
5345
5346u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5347{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005348 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005349}
5350
Bryan Schumaker184c1842012-11-29 11:40:44 -05005351u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5352{
5353 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5354 nfsd_print_count(clp, count, "locked files");
5355 return count;
5356}
5357
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005358static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5359{
5360 struct nfs4_openowner *oop, *next;
5361 u64 count = 0;
5362
5363 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5364 if (func)
5365 func(oop);
5366 if (++count == max)
5367 break;
5368 }
5369
5370 return count;
5371}
5372
5373u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5374{
5375 return nfsd_foreach_client_open(clp, max, release_openowner);
5376}
5377
Bryan Schumaker184c1842012-11-29 11:40:44 -05005378u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5379{
5380 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5381 nfsd_print_count(clp, count, "open files");
5382 return count;
5383}
5384
Bryan Schumaker269de302012-11-29 11:40:42 -05005385static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5386 struct list_head *victims)
5387{
5388 struct nfs4_delegation *dp, *next;
5389 u64 count = 0;
5390
Benny Halevycdc97502014-05-30 09:09:30 -04005391 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005392 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005393 if (victims) {
5394 /*
5395 * It's not safe to mess with delegations that have a
5396 * non-zero dl_time. They might have already been broken
5397 * and could be processed by the laundromat outside of
5398 * the state_lock. Just leave them be.
5399 */
5400 if (dp->dl_time != 0)
5401 continue;
5402
Jeff Layton42690672014-07-25 07:34:20 -04005403 unhash_delegation_locked(dp);
5404 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005405 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005406 if (++count == max)
5407 break;
5408 }
5409 return count;
5410}
5411
5412u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5413{
5414 struct nfs4_delegation *dp, *next;
5415 LIST_HEAD(victims);
5416 u64 count;
5417
Benny Halevycdc97502014-05-30 09:09:30 -04005418 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005419 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005420 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005421
Jeff Layton2d4a5322014-07-25 07:34:21 -04005422 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5423 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005424 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005425 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005426
5427 return count;
5428}
5429
5430u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5431{
Jeff Laytondff13992014-07-08 14:02:49 -04005432 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005433 LIST_HEAD(victims);
5434 u64 count;
5435
Benny Halevycdc97502014-05-30 09:09:30 -04005436 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005437 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005438 while (!list_empty(&victims)) {
5439 dp = list_first_entry(&victims, struct nfs4_delegation,
5440 dl_recall_lru);
5441 list_del_init(&dp->dl_recall_lru);
5442 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005443 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005444 }
Benny Halevycdc97502014-05-30 09:09:30 -04005445 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005446
5447 return count;
5448}
5449
Bryan Schumaker184c1842012-11-29 11:40:44 -05005450u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5451{
5452 u64 count = 0;
5453
Benny Halevycdc97502014-05-30 09:09:30 -04005454 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005455 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005456 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005457
5458 nfsd_print_count(clp, count, "delegations");
5459 return count;
5460}
5461
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005462u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005463{
5464 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005465 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005466 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005467
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005468 if (!nfsd_netns_ready(nn))
5469 return 0;
5470
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005471 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005472 count += func(clp, max - count);
5473 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005474 break;
5475 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005476
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005477 return count;
5478}
5479
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005480struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5481{
5482 struct nfs4_client *clp;
5483 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5484
5485 if (!nfsd_netns_ready(nn))
5486 return NULL;
5487
5488 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5489 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5490 return clp;
5491 }
5492 return NULL;
5493}
5494
Bryan Schumaker65178db2011-11-01 13:35:21 -04005495#endif /* CONFIG_NFSD_FAULT_INJECTION */
5496
Meelap Shahc2f1a552007-07-17 04:04:39 -07005497/*
5498 * Since the lifetime of a delegation isn't limited to that of an open, a
5499 * client may quite reasonably hang on to a delegation as long as it has
5500 * the inode cached. This becomes an obvious problem the first time a
5501 * client's inode cache approaches the size of the server's total memory.
5502 *
5503 * For now we avoid this problem by imposing a hard limit on the number
5504 * of delegations, which varies according to the server's memory size.
5505 */
5506static void
5507set_max_delegations(void)
5508{
5509 /*
5510 * Allow at most 4 delegations per megabyte of RAM. Quick
5511 * estimates suggest that in the worst case (where every delegation
5512 * is for a different inode), a delegation could take about 1.5K,
5513 * giving a worst case usage of about 6% of memory.
5514 */
5515 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5516}
5517
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005518static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005519{
5520 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5521 int i;
5522
5523 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5524 CLIENT_HASH_SIZE, GFP_KERNEL);
5525 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005526 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005527 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5528 CLIENT_HASH_SIZE, GFP_KERNEL);
5529 if (!nn->unconf_id_hashtbl)
5530 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005531 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5532 OWNER_HASH_SIZE, GFP_KERNEL);
5533 if (!nn->ownerstr_hashtbl)
5534 goto err_ownerstr;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005535 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5536 SESSION_HASH_SIZE, GFP_KERNEL);
5537 if (!nn->sessionid_hashtbl)
5538 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005539
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005540 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005541 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005542 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005543 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005544 for (i = 0; i < OWNER_HASH_SIZE; i++)
5545 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005546 for (i = 0; i < SESSION_HASH_SIZE; i++)
5547 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005548 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005549 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005550 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005551 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005552 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005553 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005554
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005555 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005556 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005557
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005558 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005559
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005560err_sessionid:
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03005561 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005562err_ownerstr:
5563 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005564err_unconf_id:
5565 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005566err:
5567 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005568}
5569
5570static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005571nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005572{
5573 int i;
5574 struct nfs4_client *clp = NULL;
5575 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5576
5577 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5578 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5579 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5580 destroy_client(clp);
5581 }
5582 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005583
Kinglong Mee2b905632014-03-26 22:09:30 +08005584 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5585 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5586 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5587 destroy_client(clp);
5588 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005589 }
5590
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005591 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005592 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005593 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005594 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005595 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005596}
5597
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005598int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005599nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005600{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005601 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005602 int ret;
5603
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005604 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005605 if (ret)
5606 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005607 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005608 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005609 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005610 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005611 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005612 nn->nfsd4_grace, net);
5613 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005614 return 0;
5615}
5616
5617/* initialization to perform when the nfsd service is started: */
5618
5619int
5620nfs4_state_start(void)
5621{
5622 int ret;
5623
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005624 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005625 if (ret)
5626 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005627 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005628 if (laundry_wq == NULL) {
5629 ret = -ENOMEM;
5630 goto out_recovery;
5631 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005632 ret = nfsd4_create_callback_queue();
5633 if (ret)
5634 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005635
Meelap Shahc2f1a552007-07-17 04:04:39 -07005636 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005637
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005638 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005639
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005640out_free_laundry:
5641 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005642out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005643 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644}
5645
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005646void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005647nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005651 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005653 cancel_delayed_work_sync(&nn->laundromat_work);
5654 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005655
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005656 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04005658 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005659 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005661 unhash_delegation_locked(dp);
5662 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 }
Benny Halevycdc97502014-05-30 09:09:30 -04005664 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 list_for_each_safe(pos, next, &reaplist) {
5666 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005667 list_del_init(&dp->dl_recall_lru);
5668 nfs4_put_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 }
5670
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005671 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005672 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005673 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674}
5675
5676void
5677nfs4_state_shutdown(void)
5678{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005679 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005680 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005682
5683static void
5684get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5685{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005686 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5687 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005688}
5689
5690static void
5691put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5692{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005693 if (cstate->minorversion) {
5694 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5695 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5696 }
5697}
5698
5699void
5700clear_current_stateid(struct nfsd4_compound_state *cstate)
5701{
5702 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005703}
5704
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005705/*
5706 * functions to set current state id
5707 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005708void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005709nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5710{
5711 put_stateid(cstate, &odp->od_stateid);
5712}
5713
5714void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005715nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5716{
5717 put_stateid(cstate, &open->op_stateid);
5718}
5719
5720void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005721nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5722{
5723 put_stateid(cstate, &close->cl_stateid);
5724}
5725
5726void
5727nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5728{
5729 put_stateid(cstate, &lock->lk_resp_stateid);
5730}
5731
5732/*
5733 * functions to consume current state id
5734 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005735
5736void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005737nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5738{
5739 get_stateid(cstate, &odp->od_stateid);
5740}
5741
5742void
5743nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5744{
5745 get_stateid(cstate, &drp->dr_stateid);
5746}
5747
5748void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005749nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5750{
5751 get_stateid(cstate, &fsp->fr_stateid);
5752}
5753
5754void
5755nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5756{
5757 get_stateid(cstate, &setattr->sa_stateid);
5758}
5759
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005760void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005761nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5762{
5763 get_stateid(cstate, &close->cl_stateid);
5764}
5765
5766void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005767nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005768{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005769 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005770}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01005771
5772void
5773nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5774{
5775 get_stateid(cstate, &read->rd_stateid);
5776}
5777
5778void
5779nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5780{
5781 get_stateid(cstate, &write->wr_stateid);
5782}