blob: 9c912c004247c3e80c3ea9ffbb3f69845bf36143 [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
NeilBrownef0f3392006-04-10 22:55:41 -0700345static int num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800346unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700347
348/*
349 * Open owner state (share locks)
350 */
351
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500352/* hash tables for lock and open owners */
353#define OWNER_HASH_BITS 8
354#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
355#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700356
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500357static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400358{
359 unsigned int ret;
360
361 ret = opaque_hashval(ownername->data, ownername->len);
362 ret += clientid;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500363 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400364}
NeilBrownef0f3392006-04-10 22:55:41 -0700365
NeilBrownef0f3392006-04-10 22:55:41 -0700366/* hash table for nfs4_file */
367#define FILE_HASH_BITS 8
368#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800369
Trond Myklebustca943212014-07-23 16:17:39 -0400370static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400371{
Trond Myklebustca943212014-07-23 16:17:39 -0400372 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
373}
374
375static unsigned int file_hashval(struct knfsd_fh *fh)
376{
377 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
378}
379
380static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
381{
382 return fh1->fh_size == fh2->fh_size &&
383 !memcmp(fh1->fh_base.fh_pad,
384 fh2->fh_base.fh_pad,
385 fh1->fh_size);
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400386}
387
Jeff Layton89876f82013-04-02 09:01:59 -0400388static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700389
Jeff Layton12659652014-07-10 14:07:28 -0400390static void
391__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400392{
Jeff Layton7214e862014-07-10 14:07:33 -0400393 lockdep_assert_held(&fp->fi_lock);
394
Jeff Layton12659652014-07-10 14:07:28 -0400395 if (access & NFS4_SHARE_ACCESS_WRITE)
396 atomic_inc(&fp->fi_access[O_WRONLY]);
397 if (access & NFS4_SHARE_ACCESS_READ)
398 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400399}
400
Jeff Layton12659652014-07-10 14:07:28 -0400401static __be32
402nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400403{
Jeff Layton7214e862014-07-10 14:07:33 -0400404 lockdep_assert_held(&fp->fi_lock);
405
Jeff Layton12659652014-07-10 14:07:28 -0400406 /* Does this access mode make sense? */
407 if (access & ~NFS4_SHARE_ACCESS_BOTH)
408 return nfserr_inval;
409
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400410 /* Does it conflict with a deny mode already set? */
411 if ((access & fp->fi_share_deny) != 0)
412 return nfserr_share_denied;
413
Jeff Layton12659652014-07-10 14:07:28 -0400414 __nfs4_file_get_access(fp, access);
415 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400416}
417
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400418static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
419{
420 /* Common case is that there is no deny mode. */
421 if (deny) {
422 /* Does this deny mode make sense? */
423 if (deny & ~NFS4_SHARE_DENY_BOTH)
424 return nfserr_inval;
425
426 if ((deny & NFS4_SHARE_DENY_READ) &&
427 atomic_read(&fp->fi_access[O_RDONLY]))
428 return nfserr_share_denied;
429
430 if ((deny & NFS4_SHARE_DENY_WRITE) &&
431 atomic_read(&fp->fi_access[O_WRONLY]))
432 return nfserr_share_denied;
433 }
434 return nfs_ok;
435}
436
J. Bruce Fields998db522010-08-07 09:21:41 -0400437static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400438{
Trond Myklebustde186432014-07-10 14:07:26 -0400439 might_lock(&fp->fi_lock);
440
441 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
442 struct file *f1 = NULL;
443 struct file *f2 = NULL;
444
Jeff Layton6d338b52014-07-10 14:07:29 -0400445 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400446 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400447 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400448 spin_unlock(&fp->fi_lock);
449 if (f1)
450 fput(f1);
451 if (f2)
452 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400453 }
454}
455
Jeff Layton12659652014-07-10 14:07:28 -0400456static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400457{
Jeff Layton12659652014-07-10 14:07:28 -0400458 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
459
460 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400461 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400462 if (access & NFS4_SHARE_ACCESS_READ)
463 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400464}
465
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500466static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct
467kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400468{
469 struct idr *stateids = &cl->cl_stateids;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500470 struct nfs4_stid *stid;
471 int new_id;
472
Trond Myklebustf8338832014-07-25 07:34:19 -0400473 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500474 if (!stid)
475 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400476
Jeff Layton398c33a2013-04-29 16:21:20 -0700477 new_id = idr_alloc_cyclic(stateids, stid, 0, 0, GFP_KERNEL);
Tejun Heoebd6c702013-03-13 14:59:37 -0700478 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500479 goto out_free;
480 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500481 stid->sc_stateid.si_opaque.so_id = new_id;
482 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
483 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400484 atomic_set(&stid->sc_count, 1);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500485
J. Bruce Fields996e0932011-10-17 11:14:48 -0400486 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500487 * It shouldn't be a problem to reuse an opaque stateid value.
488 * I don't think it is for 4.1. But with 4.0 I worry that, for
489 * example, a stray write retransmission could be accepted by
490 * the server when it should have been rejected. Therefore,
491 * adopt a trick from the sctp code to attempt to maximize the
492 * amount of time until an id is reused, by ensuring they always
493 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400494 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500495 return stid;
496out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800497 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500498 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400499}
500
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400501static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
502{
503 return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
504}
505
NeilBrown6282cd52014-06-04 17:39:26 +1000506/*
507 * When we recall a delegation, we should be careful not to hand it
508 * out again straight away.
509 * To ensure this we keep a pair of bloom filters ('new' and 'old')
510 * in which the filehandles of recalled delegations are "stored".
511 * If a filehandle appear in either filter, a delegation is blocked.
512 * When a delegation is recalled, the filehandle is stored in the "new"
513 * filter.
514 * Every 30 seconds we swap the filters and clear the "new" one,
515 * unless both are empty of course.
516 *
517 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
518 * low 3 bytes as hash-table indices.
519 *
520 * 'state_lock', which is always held when block_delegations() is called,
521 * is used to manage concurrent access. Testing does not need the lock
522 * except when swapping the two filters.
523 */
524static struct bloom_pair {
525 int entries, old_entries;
526 time_t swap_time;
527 int new; /* index into 'set' */
528 DECLARE_BITMAP(set[2], 256);
529} blocked_delegations;
530
531static int delegation_blocked(struct knfsd_fh *fh)
532{
533 u32 hash;
534 struct bloom_pair *bd = &blocked_delegations;
535
536 if (bd->entries == 0)
537 return 0;
538 if (seconds_since_boot() - bd->swap_time > 30) {
539 spin_lock(&state_lock);
540 if (seconds_since_boot() - bd->swap_time > 30) {
541 bd->entries -= bd->old_entries;
542 bd->old_entries = bd->entries;
543 memset(bd->set[bd->new], 0,
544 sizeof(bd->set[0]));
545 bd->new = 1-bd->new;
546 bd->swap_time = seconds_since_boot();
547 }
548 spin_unlock(&state_lock);
549 }
550 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
551 if (test_bit(hash&255, bd->set[0]) &&
552 test_bit((hash>>8)&255, bd->set[0]) &&
553 test_bit((hash>>16)&255, bd->set[0]))
554 return 1;
555
556 if (test_bit(hash&255, bd->set[1]) &&
557 test_bit((hash>>8)&255, bd->set[1]) &&
558 test_bit((hash>>16)&255, bd->set[1]))
559 return 1;
560
561 return 0;
562}
563
564static void block_delegations(struct knfsd_fh *fh)
565{
566 u32 hash;
567 struct bloom_pair *bd = &blocked_delegations;
568
Jeff Layton02e12152014-07-16 10:31:57 -0400569 lockdep_assert_held(&state_lock);
570
NeilBrown6282cd52014-06-04 17:39:26 +1000571 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
572
573 __set_bit(hash&255, bd->set[bd->new]);
574 __set_bit((hash>>8)&255, bd->set[bd->new]);
575 __set_bit((hash>>16)&255, bd->set[bd->new]);
576 if (bd->entries == 0)
577 bd->swap_time = seconds_since_boot();
578 bd->entries += 1;
579}
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581static struct nfs4_delegation *
J. Bruce Fields99c41512013-05-21 16:21:25 -0400582alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 dprintk("NFSD alloc_init_deleg\n");
Meelap Shahc2f1a552007-07-17 04:04:39 -0700587 if (num_delegations > max_delegations)
NeilBrownef0f3392006-04-10 22:55:41 -0700588 return NULL;
NeilBrown6282cd52014-06-04 17:39:26 +1000589 if (delegation_blocked(&current_fh->fh_handle))
590 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400591 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700592 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return dp;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400594 /*
595 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400596 * meaning of seqid 0 isn't meaningful, really, but let's avoid
597 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400598 */
599 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownef0f3392006-04-10 22:55:41 -0700600 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700601 INIT_LIST_HEAD(&dp->dl_perfile);
602 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400604 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
J. Bruce Fields6c02eaa2009-02-02 17:30:51 -0500605 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
Jeff Layton02e12152014-07-16 10:31:57 -0400606 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return dp;
608}
609
J. Bruce Fields68a33962013-03-21 11:21:50 -0400610static void remove_stid(struct nfs4_stid *s)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500611{
612 struct idr *stateids = &s->sc_client->cl_stateids;
613
614 idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500615}
616
Benny Halevy9857df82013-10-14 13:44:52 +0300617static void nfs4_free_stid(struct kmem_cache *slab, struct nfs4_stid *s)
618{
619 kmem_cache_free(slab, s);
620}
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622void
623nfs4_put_delegation(struct nfs4_delegation *dp)
624{
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400625 if (atomic_dec_and_test(&dp->dl_stid.sc_count)) {
Trond Myklebustf8338832014-07-25 07:34:19 -0400626 if (dp->dl_file)
627 put_nfs4_file(dp->dl_file);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400628 remove_stid(&dp->dl_stid);
Benny Halevy9857df82013-10-14 13:44:52 +0300629 nfs4_free_stid(deleg_slab, &dp->dl_stid);
NeilBrownef0f3392006-04-10 22:55:41 -0700630 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632}
633
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500634static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Jeff Layton417c6622014-07-21 09:34:57 -0400636 lockdep_assert_held(&state_lock);
637
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -0500638 if (!fp->fi_lease)
639 return;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500640 if (atomic_dec_and_test(&fp->fi_delegees)) {
641 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
642 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400643 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500644 fp->fi_deleg_file = NULL;
645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400648static void unhash_stid(struct nfs4_stid *s)
649{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500650 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400651}
652
Benny Halevy931ee562014-05-30 09:09:27 -0400653static void
654hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
655{
Benny Halevycdc97502014-05-30 09:09:30 -0400656 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400657 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400658
Benny Halevy3fb87d12014-05-30 09:09:31 -0400659 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400660 list_add(&dp->dl_perfile, &fp->fi_delegations);
661 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
662}
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664static void
Jeff Layton42690672014-07-25 07:34:20 -0400665unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Jeff Layton02e12152014-07-16 10:31:57 -0400667 struct nfs4_file *fp = dp->dl_file;
668
Jeff Layton42690672014-07-25 07:34:20 -0400669 lockdep_assert_held(&state_lock);
670
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400671 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400672 /* Ensure that deleg break won't try to requeue it */
673 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400674 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400675 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400677 list_del_init(&dp->dl_perfile);
678 spin_unlock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400679 if (fp)
Trond Myklebustf8338832014-07-25 07:34:19 -0400680 nfs4_put_deleg_lease(fp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400681}
682
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400683static void destroy_delegation(struct nfs4_delegation *dp)
684{
Jeff Layton42690672014-07-25 07:34:20 -0400685 spin_lock(&state_lock);
686 unhash_delegation_locked(dp);
687 spin_unlock(&state_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400688 nfs4_put_delegation(dp);
689}
690
691static void revoke_delegation(struct nfs4_delegation *dp)
692{
693 struct nfs4_client *clp = dp->dl_stid.sc_client;
694
Jeff Layton2d4a5322014-07-25 07:34:21 -0400695 WARN_ON(!list_empty(&dp->dl_recall_lru));
696
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400697 if (clp->cl_minorversion == 0)
Jeff Layton2d4a5322014-07-25 07:34:21 -0400698 nfs4_put_delegation(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400699 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400700 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400701 spin_lock(&clp->cl_lock);
702 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
703 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400704 }
705}
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707/*
708 * SETCLIENTID state
709 */
710
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400711static unsigned int clientid_hashval(u32 id)
712{
713 return id & CLIENT_HASH_MASK;
714}
715
716static unsigned int clientstr_hashval(const char *name)
717{
718 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
719}
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400722 * We store the NONE, READ, WRITE, and BOTH bits separately in the
723 * st_{access,deny}_bmap field of the stateid, in order to track not
724 * only what share bits are currently in force, but also what
725 * combinations of share bits previous opens have used. This allows us
726 * to enforce the recommendation of rfc 3530 14.2.19 that the server
727 * return an error if the client attempt to downgrade to a combination
728 * of share bits not explicable by closing some of its previous opens.
729 *
730 * XXX: This enforcement is actually incomplete, since we don't keep
731 * track of access/deny bit combinations; so, e.g., we allow:
732 *
733 * OPEN allow read, deny write
734 * OPEN allow both, deny none
735 * DOWNGRADE allow read, deny none
736 *
737 * which we should reject.
738 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400739static unsigned int
740bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400741 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400742 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400743
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400744 for (i = 1; i < 4; i++) {
745 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400746 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400747 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400748 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400749}
750
Jeff Layton82c5ff12012-05-11 09:45:13 -0400751/* set share access for a given stateid */
752static inline void
753set_access(u32 access, struct nfs4_ol_stateid *stp)
754{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400755 unsigned char mask = 1 << access;
756
757 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
758 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400759}
760
761/* clear share access for a given stateid */
762static inline void
763clear_access(u32 access, struct nfs4_ol_stateid *stp)
764{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400765 unsigned char mask = 1 << access;
766
767 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
768 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400769}
770
771/* test whether a given stateid has access */
772static inline bool
773test_access(u32 access, struct nfs4_ol_stateid *stp)
774{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400775 unsigned char mask = 1 << access;
776
777 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400778}
779
Jeff Laytonce0fc432012-05-11 09:45:14 -0400780/* set share deny for a given stateid */
781static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400782set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400783{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400784 unsigned char mask = 1 << deny;
785
786 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
787 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400788}
789
790/* clear share deny for a given stateid */
791static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400792clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400793{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400794 unsigned char mask = 1 << deny;
795
796 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
797 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400798}
799
800/* test whether a given stateid is denying specific access */
801static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400802test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400803{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400804 unsigned char mask = 1 << deny;
805
806 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400807}
808
809static int nfs4_access_to_omode(u32 access)
810{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400811 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400812 case NFS4_SHARE_ACCESS_READ:
813 return O_RDONLY;
814 case NFS4_SHARE_ACCESS_WRITE:
815 return O_WRONLY;
816 case NFS4_SHARE_ACCESS_BOTH:
817 return O_RDWR;
818 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500819 WARN_ON_ONCE(1);
820 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400821}
822
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400823/*
824 * A stateid that had a deny mode associated with it is being released
825 * or downgraded. Recalculate the deny mode on the file.
826 */
827static void
828recalculate_deny_mode(struct nfs4_file *fp)
829{
830 struct nfs4_ol_stateid *stp;
831
832 spin_lock(&fp->fi_lock);
833 fp->fi_share_deny = 0;
834 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
835 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
836 spin_unlock(&fp->fi_lock);
837}
838
839static void
840reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
841{
842 int i;
843 bool change = false;
844
845 for (i = 1; i < 4; i++) {
846 if ((i & deny) != i) {
847 change = true;
848 clear_deny(i, stp);
849 }
850 }
851
852 /* Recalculate per-file deny mode if there was a change */
853 if (change)
854 recalculate_deny_mode(stp->st_file);
855}
856
Jeff Layton82c5ff12012-05-11 09:45:13 -0400857/* release all access and file references for a given stateid */
858static void
859release_all_access(struct nfs4_ol_stateid *stp)
860{
861 int i;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400862 struct nfs4_file *fp = stp->st_file;
863
864 if (fp && stp->st_deny_bmap != 0)
865 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400866
867 for (i = 1; i < 4; i++) {
868 if (test_access(i, stp))
Jeff Layton12659652014-07-10 14:07:28 -0400869 nfs4_file_put_access(stp->st_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400870 clear_access(i, stp);
871 }
872}
873
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400874static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500875{
Trond Myklebust1d31a252014-07-10 14:07:25 -0400876 struct nfs4_file *fp = stp->st_file;
877
878 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500879 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400880 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500881 list_del(&stp->st_perstateowner);
882}
883
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400884static void close_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500885{
Jeff Layton82c5ff12012-05-11 09:45:13 -0400886 release_all_access(stp);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400887}
888
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400889static void free_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400890{
Trond Myklebustf8338832014-07-25 07:34:19 -0400891 if (stp->st_file)
892 put_nfs4_file(stp->st_file);
J. Bruce Fields68a33962013-03-21 11:21:50 -0400893 remove_stid(&stp->st_stid);
Benny Halevy9857df82013-10-14 13:44:52 +0300894 nfs4_free_stid(stateid_slab, &stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500895}
896
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400897static void __release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500898{
899 struct file *file;
900
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400901 list_del(&stp->st_locks);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500902 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400903 unhash_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500904 file = find_any_file(stp->st_file);
Trond Myklebuste20fcf12014-07-10 14:07:27 -0400905 if (file)
906 filp_close(file, (fl_owner_t)lockowner(stp->st_stateowner));
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400907 close_generic_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500908 free_generic_stateid(stp);
909}
910
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400911static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500912{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400913 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500914
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400915 list_del(&lo->lo_owner.so_strhash);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400916 while (!list_empty(&lo->lo_owner.so_stateids)) {
917 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400918 struct nfs4_ol_stateid, st_perstateowner);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400919 __release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500920 }
921}
922
Trond Myklebust50cc6232014-04-18 14:44:03 -0400923static void nfs4_free_lockowner(struct nfs4_lockowner *lo)
924{
925 kfree(lo->lo_owner.so_owner.data);
926 kmem_cache_free(lockowner_slab, lo);
927}
928
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400929static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500930{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400931 unhash_lockowner(lo);
932 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500933}
934
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400935static void release_lockowner_if_empty(struct nfs4_lockowner *lo)
936{
937 if (list_empty(&lo->lo_owner.so_stateids))
938 release_lockowner(lo);
939}
940
941static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500942{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400943 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500944
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400945 lo = lockowner(stp->st_stateowner);
946 __release_lock_stateid(stp);
947 release_lockowner_if_empty(lo);
948}
949
950static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
951{
952 struct nfs4_ol_stateid *stp;
953
954 while (!list_empty(&open_stp->st_locks)) {
955 stp = list_entry(open_stp->st_locks.next,
956 struct nfs4_ol_stateid, st_locks);
957 release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500958 }
959}
960
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400961static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500962{
963 unhash_generic_stateid(stp);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400964 release_open_stateid_locks(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400965 close_generic_stateid(stp);
966}
967
968static void release_open_stateid(struct nfs4_ol_stateid *stp)
969{
970 unhash_open_stateid(stp);
J. Bruce Fields22839632009-01-11 14:27:17 -0500971 free_generic_stateid(stp);
972}
973
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400974static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500975{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400976 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500977
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400978 list_del(&oo->oo_owner.so_strhash);
979 list_del(&oo->oo_perclient);
980 while (!list_empty(&oo->oo_owner.so_stateids)) {
981 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400982 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500983 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500984 }
985}
986
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400987static void release_last_closed_stateid(struct nfs4_openowner *oo)
988{
989 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
990
991 if (s) {
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400992 free_generic_stateid(s);
993 oo->oo_last_closed_stid = NULL;
994 }
995}
996
Trond Myklebust50cc6232014-04-18 14:44:03 -0400997static void nfs4_free_openowner(struct nfs4_openowner *oo)
998{
999 kfree(oo->oo_owner.so_owner.data);
1000 kmem_cache_free(openowner_slab, oo);
1001}
1002
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001003static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001004{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001005 unhash_openowner(oo);
1006 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001007 release_last_closed_stateid(oo);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001008 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001009}
1010
Marc Eshel5282fd72009-04-03 08:27:52 +03001011static inline int
1012hash_sessionid(struct nfs4_sessionid *sessionid)
1013{
1014 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1015
1016 return sid->sequence % SESSION_HASH_SIZE;
1017}
1018
Trond Myklebust8f199b82012-03-20 15:11:17 -04001019#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001020static inline void
1021dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1022{
1023 u32 *ptr = (u32 *)(&sessionid->data[0]);
1024 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1025}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001026#else
1027static inline void
1028dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1029{
1030}
1031#endif
1032
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001033/*
1034 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1035 * won't be used for replay.
1036 */
1037void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1038{
1039 struct nfs4_stateowner *so = cstate->replay_owner;
1040
1041 if (nfserr == nfserr_replay_me)
1042 return;
1043
1044 if (!seqid_mutating_err(ntohl(nfserr))) {
1045 cstate->replay_owner = NULL;
1046 return;
1047 }
1048 if (!so)
1049 return;
1050 if (so->so_is_open_owner)
1051 release_last_closed_stateid(openowner(so));
1052 so->so_seqid++;
1053 return;
1054}
Marc Eshel5282fd72009-04-03 08:27:52 +03001055
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001056static void
1057gen_sessionid(struct nfsd4_session *ses)
1058{
1059 struct nfs4_client *clp = ses->se_client;
1060 struct nfsd4_sessionid *sid;
1061
1062 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1063 sid->clientid = clp->cl_clientid;
1064 sid->sequence = current_sessionid++;
1065 sid->reserved = 0;
1066}
1067
1068/*
Andy Adamsona6496372009-08-28 08:45:01 -04001069 * The protocol defines ca_maxresponssize_cached to include the size of
1070 * the rpc header, but all we need to cache is the data starting after
1071 * the end of the initial SEQUENCE operation--the rest we regenerate
1072 * each time. Therefore we can advertise a ca_maxresponssize_cached
1073 * value that is the number of bytes in our cache plus a few additional
1074 * bytes. In order to stay on the safe side, and not promise more than
1075 * we can cache, those additional bytes must be the minimum possible: 24
1076 * bytes of rpc header (xid through accept state, with AUTH_NULL
1077 * verifier), 12 for the compound header (with zero-length tag), and 44
1078 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001079 */
Andy Adamsona6496372009-08-28 08:45:01 -04001080#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1081
Andy Adamson557ce262009-08-28 08:45:04 -04001082static void
1083free_session_slots(struct nfsd4_session *ses)
1084{
1085 int i;
1086
1087 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1088 kfree(ses->se_slots[i]);
1089}
1090
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001091/*
1092 * We don't actually need to cache the rpc and session headers, so we
1093 * can allocate a little less for each slot:
1094 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001095static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001096{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001097 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001098
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001099 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1100 size = 0;
1101 else
1102 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1103 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001104}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001105
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001106/*
1107 * XXX: If we run out of reserved DRC memory we could (up to a point)
1108 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001109 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001110 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001111static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001112{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001113 u32 slotsize = slot_bytes(ca);
1114 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001115 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001116
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001117 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001118 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1119 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001120 num = min_t(int, num, avail / slotsize);
1121 nfsd_drc_mem_used += num * slotsize;
1122 spin_unlock(&nfsd_drc_lock);
1123
1124 return num;
1125}
1126
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001127static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001128{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001129 int slotsize = slot_bytes(ca);
1130
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001131 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001132 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001133 spin_unlock(&nfsd_drc_lock);
1134}
1135
Kinglong Mee60810e52014-01-01 00:35:47 +08001136static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1137 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001138{
Kinglong Mee60810e52014-01-01 00:35:47 +08001139 int numslots = fattrs->maxreqs;
1140 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001141 struct nfsd4_session *new;
1142 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001143
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001144 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001145 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1146 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001147
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001148 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001149 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001150 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001151 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001152 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001153 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001154 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001155 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001156 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001157
1158 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1159 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1160
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001161 return new;
1162out_free:
1163 while (i--)
1164 kfree(new->se_slots[i]);
1165 kfree(new);
1166 return NULL;
1167}
1168
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001169static void free_conn(struct nfsd4_conn *c)
1170{
1171 svc_xprt_put(c->cn_xprt);
1172 kfree(c);
1173}
1174
1175static void nfsd4_conn_lost(struct svc_xpt_user *u)
1176{
1177 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1178 struct nfs4_client *clp = c->cn_session->se_client;
1179
1180 spin_lock(&clp->cl_lock);
1181 if (!list_empty(&c->cn_persession)) {
1182 list_del(&c->cn_persession);
1183 free_conn(c);
1184 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001185 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001186 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001187}
1188
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001189static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001190{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001191 struct nfsd4_conn *conn;
1192
1193 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1194 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001195 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001196 svc_xprt_get(rqstp->rq_xprt);
1197 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001198 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001199 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1200 return conn;
1201}
1202
J. Bruce Fields328ead22010-09-29 16:11:06 -04001203static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1204{
1205 conn->cn_session = ses;
1206 list_add(&conn->cn_persession, &ses->se_conns);
1207}
1208
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001209static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1210{
1211 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001212
1213 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001214 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001215 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001216}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001217
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001218static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001219{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001220 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001221 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001222}
1223
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001224static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001225{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001226 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001227
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001228 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001229 ret = nfsd4_register_conn(conn);
1230 if (ret)
1231 /* oops; xprt is already down: */
1232 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001233 /* We may have gained or lost a callback channel: */
1234 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001235}
1236
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001237static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001238{
1239 u32 dir = NFS4_CDFC4_FORE;
1240
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001241 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001242 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001243 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001244}
1245
1246/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001247static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001248{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001249 struct nfs4_client *clp = s->se_client;
1250 struct nfsd4_conn *c;
1251
1252 spin_lock(&clp->cl_lock);
1253 while (!list_empty(&s->se_conns)) {
1254 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1255 list_del_init(&c->cn_persession);
1256 spin_unlock(&clp->cl_lock);
1257
1258 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1259 free_conn(c);
1260
1261 spin_lock(&clp->cl_lock);
1262 }
1263 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001264}
1265
J. Bruce Fields1377b692012-09-11 21:42:40 -04001266static void __free_session(struct nfsd4_session *ses)
1267{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001268 free_session_slots(ses);
1269 kfree(ses);
1270}
1271
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001272static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001273{
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001274 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1275
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001276 lockdep_assert_held(&nn->client_lock);
1277 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001278 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001279 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001280}
Andy Adamson557ce262009-08-28 08:45:04 -04001281
Fengguang Wu135ae822012-11-10 07:20:25 -05001282static 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 -04001283{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001284 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001285 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001286
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001287 new->se_client = clp;
1288 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001289
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001290 INIT_LIST_HEAD(&new->se_conns);
1291
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001292 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001293 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001294 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001295 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001296 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001297 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001298 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001299 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001300 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001301 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001302 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001303 spin_unlock(&nn->client_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001304
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001305 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001306 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001307 /*
1308 * This is a little silly; with sessions there's no real
1309 * use for the callback address. Use the peer address
1310 * as a reasonable default for now, but consider fixing
1311 * the rpc client not to require an address in the
1312 * future:
1313 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001314 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1315 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001316 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001317}
1318
Benny Halevy9089f1b2010-05-12 00:12:26 +03001319/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001320static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001321__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001322{
1323 struct nfsd4_session *elem;
1324 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001325 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001326
1327 dump_sessionid(__func__, sessionid);
1328 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001329 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001330 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001331 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1332 NFS4_MAX_SESSIONID_LEN)) {
1333 return elem;
1334 }
1335 }
1336
1337 dprintk("%s: session not found\n", __func__);
1338 return NULL;
1339}
1340
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001341static struct nfsd4_session *
1342find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1343 __be32 *ret)
1344{
1345 struct nfsd4_session *session;
1346 __be32 status = nfserr_badsession;
1347
1348 session = __find_in_sessionid_hashtbl(sessionid, net);
1349 if (!session)
1350 goto out;
1351 status = nfsd4_get_session_locked(session);
1352 if (status)
1353 session = NULL;
1354out:
1355 *ret = status;
1356 return session;
1357}
1358
Benny Halevy9089f1b2010-05-12 00:12:26 +03001359/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001360static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001361unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001362{
1363 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001364 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001365 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001366 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001367}
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1370static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001371STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001373 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001375 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001376 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return 1;
1378}
1379
1380/*
1381 * XXX Should we use a slab cache ?
1382 * This type of memory management is somewhat inefficient, but we use it
1383 * anyway since SETCLIENTID is not a common operation.
1384 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001385static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
1387 struct nfs4_client *clp;
1388
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001389 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1390 if (clp == NULL)
1391 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001392 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001393 if (clp->cl_name.data == NULL) {
1394 kfree(clp);
1395 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001397 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001398 INIT_LIST_HEAD(&clp->cl_sessions);
1399 idr_init(&clp->cl_stateids);
1400 atomic_set(&clp->cl_refcount, 0);
1401 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1402 INIT_LIST_HEAD(&clp->cl_idhash);
1403 INIT_LIST_HEAD(&clp->cl_openowners);
1404 INIT_LIST_HEAD(&clp->cl_delegations);
1405 INIT_LIST_HEAD(&clp->cl_lru);
1406 INIT_LIST_HEAD(&clp->cl_callbacks);
1407 INIT_LIST_HEAD(&clp->cl_revoked);
1408 spin_lock_init(&clp->cl_lock);
1409 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return clp;
1411}
1412
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001413static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414free_client(struct nfs4_client *clp)
1415{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001416 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001417
1418 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001419 while (!list_empty(&clp->cl_sessions)) {
1420 struct nfsd4_session *ses;
1421 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1422 se_perclnt);
1423 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001424 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1425 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001426 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001427 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001428 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001430 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 kfree(clp);
1432}
1433
Benny Halevy84d38ac2010-05-12 00:13:16 +03001434/* must be called under the client_lock */
1435static inline void
1436unhash_client_locked(struct nfs4_client *clp)
1437{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001438 struct nfsd4_session *ses;
1439
Benny Halevy84d38ac2010-05-12 00:13:16 +03001440 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001441 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001442 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1443 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001444 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001445}
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001448destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001450 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001453 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001456 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001457 while (!list_empty(&clp->cl_delegations)) {
1458 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001459 unhash_delegation_locked(dp);
1460 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
Benny Halevycdc97502014-05-30 09:09:30 -04001462 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 while (!list_empty(&reaplist)) {
1464 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001465 list_del_init(&dp->dl_recall_lru);
1466 nfs4_put_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001468 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001469 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001470 list_del_init(&dp->dl_recall_lru);
1471 nfs4_put_delegation(dp);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001472 }
NeilBrownea1da632005-06-23 22:04:17 -07001473 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001474 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1475 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001477 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001478 if (clp->cl_cb_conn.cb_xprt)
1479 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001480 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001481 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001482 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001483 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001484 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001485 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001486 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001487 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1488 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001489 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001492static void expire_client(struct nfs4_client *clp)
1493{
1494 nfsd4_client_record_remove(clp);
1495 destroy_client(clp);
1496}
1497
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001498static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1499{
1500 memcpy(target->cl_verifier.data, source->data,
1501 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001504static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1505{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1507 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1508}
1509
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001510static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001511{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001512 if (source->cr_principal) {
1513 target->cr_principal =
1514 kstrdup(source->cr_principal, GFP_KERNEL);
1515 if (target->cr_principal == NULL)
1516 return -ENOMEM;
1517 } else
1518 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001519 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 target->cr_uid = source->cr_uid;
1521 target->cr_gid = source->cr_gid;
1522 target->cr_group_info = source->cr_group_info;
1523 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001524 target->cr_gss_mech = source->cr_gss_mech;
1525 if (source->cr_gss_mech)
1526 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001527 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
1529
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001530static long long
1531compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1532{
1533 long long res;
1534
1535 res = o1->len - o2->len;
1536 if (res)
1537 return res;
1538 return (long long)memcmp(o1->data, o2->data, o1->len);
1539}
1540
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001541static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001542{
NeilBrowna55370a2005-06-23 22:03:52 -07001543 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
1545
1546static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001547same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1548{
1549 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
1551
1552static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001553same_clid(clientid_t *cl1, clientid_t *cl2)
1554{
1555 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001558static bool groups_equal(struct group_info *g1, struct group_info *g2)
1559{
1560 int i;
1561
1562 if (g1->ngroups != g2->ngroups)
1563 return false;
1564 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001565 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001566 return false;
1567 return true;
1568}
1569
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001570/*
1571 * RFC 3530 language requires clid_inuse be returned when the
1572 * "principal" associated with a requests differs from that previously
1573 * used. We use uid, gid's, and gss principal string as our best
1574 * approximation. We also don't want to allow non-gss use of a client
1575 * established using gss: in theory cr_principal should catch that
1576 * change, but in practice cr_principal can be null even in the gss case
1577 * since gssd doesn't always pass down a principal string.
1578 */
1579static bool is_gss_cred(struct svc_cred *cr)
1580{
1581 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1582 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1583}
1584
1585
Vivek Trivedi5559b502012-07-24 21:18:20 +05301586static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001587same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1588{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001589 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001590 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1591 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001592 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1593 return false;
1594 if (cr1->cr_principal == cr2->cr_principal)
1595 return true;
1596 if (!cr1->cr_principal || !cr2->cr_principal)
1597 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301598 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
J. Bruce Fields57266a62013-04-13 14:27:29 -04001601static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1602{
1603 struct svc_cred *cr = &rqstp->rq_cred;
1604 u32 service;
1605
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001606 if (!cr->cr_gss_mech)
1607 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001608 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1609 return service == RPC_GSS_SVC_INTEGRITY ||
1610 service == RPC_GSS_SVC_PRIVACY;
1611}
1612
1613static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1614{
1615 struct svc_cred *cr = &rqstp->rq_cred;
1616
1617 if (!cl->cl_mach_cred)
1618 return true;
1619 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1620 return false;
1621 if (!svc_rqst_integrity_protected(rqstp))
1622 return false;
1623 if (!cr->cr_principal)
1624 return false;
1625 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1626}
1627
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001628static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001629{
1630 static u32 current_clientid = 1;
1631
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001632 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 clp->cl_clientid.cl_id = current_clientid++;
1634}
1635
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001636static void gen_confirm(struct nfs4_client *clp)
1637{
Chuck Leverab4684d2012-03-02 17:13:50 -05001638 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001639 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Jeff Laytonf4199922014-06-17 07:44:11 -04001641 /*
1642 * This is opaque to client, so no need to byte-swap. Use
1643 * __force to keep sparse happy
1644 */
1645 verf[0] = (__force __be32)get_seconds();
1646 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001647 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
1649
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001650static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001651{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001652 struct nfs4_stid *ret;
1653
1654 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1655 if (!ret || !ret->sc_type)
1656 return NULL;
1657 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001658}
1659
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001660static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001661{
1662 struct nfs4_stid *s;
1663
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001664 s = find_stateid(cl, t);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001665 if (!s)
1666 return NULL;
1667 if (typemask & s->sc_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001668 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001669 return NULL;
1670}
1671
Jeff Layton2216d442012-11-12 15:00:57 -05001672static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001673 struct svc_rqst *rqstp, nfs4_verifier *verf)
1674{
1675 struct nfs4_client *clp;
1676 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001677 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001678 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001679 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001680
1681 clp = alloc_client(name);
1682 if (clp == NULL)
1683 return NULL;
1684
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001685 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1686 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001687 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001688 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001689 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001690 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001691 }
Jeff Layton02e12152014-07-16 10:31:57 -04001692 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001693 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001694 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001695 copy_verf(clp, verf);
1696 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001697 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001698 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001699 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001700 return clp;
1701}
1702
NeilBrownfd39ca92005-06-23 22:04:03 -07001703static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001704add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1705{
1706 struct rb_node **new = &(root->rb_node), *parent = NULL;
1707 struct nfs4_client *clp;
1708
1709 while (*new) {
1710 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1711 parent = *new;
1712
1713 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1714 new = &((*new)->rb_left);
1715 else
1716 new = &((*new)->rb_right);
1717 }
1718
1719 rb_link_node(&new_clp->cl_namenode, parent, new);
1720 rb_insert_color(&new_clp->cl_namenode, root);
1721}
1722
1723static struct nfs4_client *
1724find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1725{
1726 long long cmp;
1727 struct rb_node *node = root->rb_node;
1728 struct nfs4_client *clp;
1729
1730 while (node) {
1731 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1732 cmp = compare_blob(&clp->cl_name, name);
1733 if (cmp > 0)
1734 node = node->rb_left;
1735 else if (cmp < 0)
1736 node = node->rb_right;
1737 else
1738 return clp;
1739 }
1740 return NULL;
1741}
1742
1743static void
1744add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
1746 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001747 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001749 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001750 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001752 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001753 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
NeilBrownfd39ca92005-06-23 22:04:03 -07001756static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757move_to_confirmed(struct nfs4_client *clp)
1758{
1759 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001760 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001763 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001764 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001765 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001766 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 renew_client(clp);
1768}
1769
1770static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001771find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
1773 struct nfs4_client *clp;
1774 unsigned int idhashval = clientid_hashval(clid->cl_id);
1775
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001776 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001777 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001778 if ((bool)clp->cl_minorversion != sessions)
1779 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001780 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
1784 return NULL;
1785}
1786
1787static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001788find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1789{
1790 struct list_head *tbl = nn->conf_id_hashtbl;
1791
1792 return find_client_in_id_table(tbl, clid, sessions);
1793}
1794
1795static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001796find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001798 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001800 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
1802
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001803static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001804{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001805 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001806}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001807
NeilBrown28ce6052005-06-23 22:03:56 -07001808static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001809find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001810{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001811 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001812}
1813
1814static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001815find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001816{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001817 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001818}
1819
NeilBrownfd39ca92005-06-23 22:04:03 -07001820static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001821gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001823 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001824 struct sockaddr *sa = svc_addr(rqstp);
1825 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001826 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Jeff Layton7077ecb2009-08-14 12:57:58 -04001828 /* Currently, we only support tcp and tcp6 for the callback channel */
1829 if (se->se_callback_netid_len == 3 &&
1830 !memcmp(se->se_callback_netid_val, "tcp", 3))
1831 expected_family = AF_INET;
1832 else if (se->se_callback_netid_len == 4 &&
1833 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1834 expected_family = AF_INET6;
1835 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 goto out_err;
1837
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001838 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001839 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001840 (struct sockaddr *)&conn->cb_addr,
1841 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001842
J. Bruce Fields07263f12010-05-31 19:09:40 -04001843 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001845
J. Bruce Fields07263f12010-05-31 19:09:40 -04001846 if (conn->cb_addr.ss_family == AF_INET6)
1847 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001848
J. Bruce Fields07263f12010-05-31 19:09:40 -04001849 conn->cb_prog = se->se_callback_prog;
1850 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001851 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return;
1853out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001854 conn->cb_addr.ss_family = AF_UNSPEC;
1855 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001856 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 "will not receive delegations\n",
1858 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return;
1861}
1862
Andy Adamson074fe892009-04-03 08:28:15 +03001863/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04001864 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001865 */
Trond Myklebustb6076642014-06-30 11:48:35 -04001866static void
Andy Adamson074fe892009-04-03 08:28:15 +03001867nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1868{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001869 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04001870 struct nfsd4_slot *slot = resp->cstate.slot;
1871 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001872
Andy Adamson557ce262009-08-28 08:45:04 -04001873 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001874
Andy Adamson557ce262009-08-28 08:45:04 -04001875 slot->sl_opcnt = resp->opcnt;
1876 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001877
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001878 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001879 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001880 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001881 return;
1882 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001883 base = resp->cstate.data_offset;
1884 slot->sl_datalen = buf->len - base;
1885 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04001886 WARN("%s: sessions DRC could not cache compound\n", __func__);
1887 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001888}
1889
1890/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001891 * Encode the replay sequence operation from the slot values.
1892 * If cachethis is FALSE encode the uncached rep error on the next
1893 * operation which sets resp->p and increments resp->opcnt for
1894 * nfs4svc_encode_compoundres.
1895 *
Andy Adamson074fe892009-04-03 08:28:15 +03001896 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001897static __be32
1898nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1899 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001900{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001901 struct nfsd4_op *op;
1902 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001903
Andy Adamsonabfabf82009-07-23 19:02:18 -04001904 /* Encode the replayed sequence operation */
1905 op = &args->ops[resp->opcnt - 1];
1906 nfsd4_encode_operation(resp, op);
1907
1908 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001909 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001910 op = &args->ops[resp->opcnt++];
1911 op->status = nfserr_retry_uncached_rep;
1912 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001913 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001914 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001915}
1916
1917/*
Andy Adamson557ce262009-08-28 08:45:04 -04001918 * The sequence operation is not cached because we can use the slot and
1919 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001920 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04001921static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001922nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1923 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001924{
Andy Adamson557ce262009-08-28 08:45:04 -04001925 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001926 struct xdr_stream *xdr = &resp->xdr;
1927 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03001928 __be32 status;
1929
Andy Adamson557ce262009-08-28 08:45:04 -04001930 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001931
Andy Adamsonabfabf82009-07-23 19:02:18 -04001932 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04001933 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04001934 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001935
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001936 p = xdr_reserve_space(xdr, slot->sl_datalen);
1937 if (!p) {
1938 WARN_ON_ONCE(1);
1939 return nfserr_serverfault;
1940 }
1941 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1942 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03001943
Andy Adamson557ce262009-08-28 08:45:04 -04001944 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001945 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001946}
1947
Andy Adamson0733d212009-04-03 08:28:01 +03001948/*
1949 * Set the exchange_id flags returned by the server.
1950 */
1951static void
1952nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1953{
1954 /* pNFS is not supported */
1955 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1956
1957 /* Referrals are supported, Migration is not. */
1958 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1959
1960 /* set the wire flags to return to client. */
1961 clid->flags = new->cl_exchange_flags;
1962}
1963
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001964static bool client_has_state(struct nfs4_client *clp)
1965{
1966 /*
1967 * Note clp->cl_openowners check isn't quite right: there's no
1968 * need to count owners without stateid's.
1969 *
1970 * Also note we should probably be using this in 4.0 case too.
1971 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04001972 return !list_empty(&clp->cl_openowners)
1973 || !list_empty(&clp->cl_delegations)
1974 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001975}
1976
Al Virob37ad282006-10-19 23:28:59 -07001977__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001978nfsd4_exchange_id(struct svc_rqst *rqstp,
1979 struct nfsd4_compound_state *cstate,
1980 struct nfsd4_exchange_id *exid)
1981{
Andy Adamson0733d212009-04-03 08:28:01 +03001982 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001983 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04001984 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001985 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001986 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001987 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001988 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03001989
Jeff Layton363168b2009-08-14 12:57:56 -04001990 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001991 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001992 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001993 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001994 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03001995
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001996 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03001997 return nfserr_inval;
1998
Andy Adamson0733d212009-04-03 08:28:01 +03001999 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002000 case SP4_MACH_CRED:
2001 if (!svc_rqst_integrity_protected(rqstp))
2002 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002003 case SP4_NONE:
2004 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002005 default: /* checked by xdr code */
2006 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002007 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002008 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002009 }
2010
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002011 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002012 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002013 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002014 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002015 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2016 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2017
J. Bruce Fields136e6582012-05-12 20:37:23 -04002018 if (update) {
2019 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002020 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002021 goto out;
2022 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002023 if (!mach_creds_match(conf, rqstp)) {
2024 status = nfserr_wrong_cred;
2025 goto out;
2026 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002027 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002028 status = nfserr_perm;
2029 goto out;
2030 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002031 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002032 status = nfserr_not_same;
2033 goto out;
2034 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002035 /* case 6 */
2036 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2037 new = conf;
2038 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002039 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002040 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002041 if (client_has_state(conf)) {
2042 status = nfserr_clid_inuse;
2043 goto out;
2044 }
Andy Adamson0733d212009-04-03 08:28:01 +03002045 expire_client(conf);
2046 goto out_new;
2047 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002048 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002049 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002050 new = conf;
2051 goto out_copy;
2052 }
2053 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04002054 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002055 }
2056
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002057 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002058 status = nfserr_noent;
2059 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002060 }
2061
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002062 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002063 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03002064 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002065
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002066 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002067out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05002068 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03002069 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04002070 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03002071 goto out;
2072 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002073 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002074 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002075
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002076 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002077 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03002078out_copy:
2079 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2080 exid->clientid.cl_id = new->cl_clientid.cl_id;
2081
J. Bruce Fields778df3f2012-05-25 21:40:23 -04002082 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03002083 nfsd4_set_ex_flags(new, exid);
2084
2085 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04002086 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002087 status = nfs_ok;
2088
2089out:
2090 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03002091 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002092}
2093
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002094static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002095check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002096{
Andy Adamson88e588d2009-07-23 19:02:15 -04002097 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2098 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002099
2100 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002101 if (slot_inuse) {
2102 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002103 return nfserr_jukebox;
2104 else
2105 return nfserr_seq_misordered;
2106 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002107 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002108 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002109 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002110 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002111 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002112 return nfserr_seq_misordered;
2113}
2114
Andy Adamson49557cc2009-07-23 19:02:16 -04002115/*
2116 * Cache the create session result into the create session single DRC
2117 * slot cache by saving the xdr structure. sl_seqid has been set.
2118 * Do this for solo or embedded create session operations.
2119 */
2120static void
2121nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002122 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002123{
2124 slot->sl_status = nfserr;
2125 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2126}
2127
2128static __be32
2129nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2130 struct nfsd4_clid_slot *slot)
2131{
2132 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2133 return slot->sl_status;
2134}
2135
Mi Jinlong1b74c252011-07-14 14:50:17 +08002136#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2137 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2138 1 + /* MIN tag is length with zero, only length */ \
2139 3 + /* version, opcount, opcode */ \
2140 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2141 /* seqid, slotID, slotID, cache */ \
2142 4 ) * sizeof(__be32))
2143
2144#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2145 2 + /* verifier: AUTH_NULL, length 0 */\
2146 1 + /* status */ \
2147 1 + /* MIN tag is length with zero, only length */ \
2148 3 + /* opcount, opcode, opstatus*/ \
2149 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2150 /* seqid, slotID, slotID, slotID, status */ \
2151 5 ) * sizeof(__be32))
2152
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002153static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002154{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002155 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2156
J. Bruce Fields373cd402013-04-08 15:42:12 -04002157 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2158 return nfserr_toosmall;
2159 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2160 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002161 ca->headerpadsz = 0;
2162 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2163 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2164 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2165 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2166 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2167 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2168 /*
2169 * Note decreasing slot size below client's request may make it
2170 * difficult for client to function correctly, whereas
2171 * decreasing the number of slots will (just?) affect
2172 * performance. When short on memory we therefore prefer to
2173 * decrease number of slots instead of their size. Clients that
2174 * request larger slots than they need will get poor results:
2175 */
2176 ca->maxreqs = nfsd4_get_drc_mem(ca);
2177 if (!ca->maxreqs)
2178 return nfserr_jukebox;
2179
J. Bruce Fields373cd402013-04-08 15:42:12 -04002180 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002181}
2182
Kinglong Mee8a891632013-12-23 18:11:02 +08002183#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2184 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2185#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2186 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2187
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002188static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2189{
2190 ca->headerpadsz = 0;
2191
2192 /*
2193 * These RPC_MAX_HEADER macros are overkill, especially since we
2194 * don't even do gss on the backchannel yet. But this is still
2195 * less than 1k. Tighten up this estimate in the unlikely event
2196 * it turns out to be a problem for some client:
2197 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002198 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002199 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002200 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002201 return nfserr_toosmall;
2202 ca->maxresp_cached = 0;
2203 if (ca->maxops < 2)
2204 return nfserr_toosmall;
2205
2206 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002207}
2208
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002209static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2210{
2211 switch (cbs->flavor) {
2212 case RPC_AUTH_NULL:
2213 case RPC_AUTH_UNIX:
2214 return nfs_ok;
2215 default:
2216 /*
2217 * GSS case: the spec doesn't allow us to return this
2218 * error. But it also doesn't allow us not to support
2219 * GSS.
2220 * I'd rather this fail hard than return some error the
2221 * client might think it can already handle:
2222 */
2223 return nfserr_encr_alg_unsupp;
2224 }
2225}
2226
Andy Adamson069b6ad2009-04-03 08:27:58 +03002227__be32
2228nfsd4_create_session(struct svc_rqst *rqstp,
2229 struct nfsd4_compound_state *cstate,
2230 struct nfsd4_create_session *cr_ses)
2231{
Jeff Layton363168b2009-08-14 12:57:56 -04002232 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002233 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002234 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002235 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002236 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002237 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002238 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002239
Mi Jinlonga62573d2011-03-23 17:57:07 +08002240 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2241 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002242 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2243 if (status)
2244 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002245 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002246 if (status)
2247 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002248 status = check_backchannel_attrs(&cr_ses->back_channel);
2249 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002250 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002251 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002252 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002253 if (!new)
2254 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002255 conn = alloc_conn_from_crses(rqstp, cr_ses);
2256 if (!conn)
2257 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002258
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002259 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002260 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002261 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002262 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002263
2264 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002265 status = nfserr_wrong_cred;
2266 if (!mach_creds_match(conf, rqstp))
2267 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002268 cs_slot = &conf->cl_cs_slot;
2269 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002270 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002271 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002272 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002273 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002274 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002275 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002276 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002277 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002278 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002279 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002280 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002281 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002282 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002283 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002284 status = nfserr_wrong_cred;
2285 if (!mach_creds_match(unconf, rqstp))
2286 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002287 cs_slot = &unconf->cl_cs_slot;
2288 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002289 if (status) {
2290 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002291 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002292 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002293 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002294 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002295 if (old) {
2296 status = mark_client_expired(old);
2297 if (status)
2298 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002299 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002300 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002301 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002302 conf = unconf;
2303 } else {
2304 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002305 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002306 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002307 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002308 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002309 * We do not support RDMA or persistent sessions
2310 */
2311 cr_ses->flags &= ~SESSION4_PERSIST;
2312 cr_ses->flags &= ~SESSION4_RDMA;
2313
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002314 init_session(rqstp, new, conf, cr_ses);
2315 nfsd4_init_conn(rqstp, conn, new);
2316
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002317 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002318 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002319 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002320 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002321
Andy Adamson49557cc2009-07-23 19:02:16 -04002322 /* cache solo and embedded create sessions under the state lock */
2323 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002324 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002325 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002326out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08002327 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002328 free_conn(conn);
2329out_free_session:
2330 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002331out_release_drc_mem:
2332 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002333 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002334}
2335
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002336static __be32 nfsd4_map_bcts_dir(u32 *dir)
2337{
2338 switch (*dir) {
2339 case NFS4_CDFC4_FORE:
2340 case NFS4_CDFC4_BACK:
2341 return nfs_ok;
2342 case NFS4_CDFC4_FORE_OR_BOTH:
2343 case NFS4_CDFC4_BACK_OR_BOTH:
2344 *dir = NFS4_CDFC4_BOTH;
2345 return nfs_ok;
2346 };
2347 return nfserr_inval;
2348}
2349
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002350__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2351{
2352 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002353 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002354 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002355
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002356 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2357 if (status)
2358 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002359 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002360 session->se_cb_prog = bc->bc_cb_program;
2361 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002362 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002363
2364 nfsd4_probe_callback(session->se_client);
2365
2366 return nfs_ok;
2367}
2368
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002369__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2370 struct nfsd4_compound_state *cstate,
2371 struct nfsd4_bind_conn_to_session *bcts)
2372{
2373 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002374 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002375 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002376 struct net *net = SVC_NET(rqstp);
2377 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002378
2379 if (!nfsd4_last_compound_op(rqstp))
2380 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002381 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002382 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002383 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002384 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002385 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002386 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002387 status = nfserr_wrong_cred;
2388 if (!mach_creds_match(session->se_client, rqstp))
2389 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002390 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002391 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002392 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002393 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002394 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002395 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002396 goto out;
2397 nfsd4_init_conn(rqstp, conn, session);
2398 status = nfs_ok;
2399out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002400 nfsd4_put_session(session);
2401out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002402 nfs4_unlock_state();
2403 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002404}
2405
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002406static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2407{
2408 if (!session)
2409 return 0;
2410 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2411}
2412
Andy Adamson069b6ad2009-04-03 08:27:58 +03002413__be32
2414nfsd4_destroy_session(struct svc_rqst *r,
2415 struct nfsd4_compound_state *cstate,
2416 struct nfsd4_destroy_session *sessionid)
2417{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002418 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002419 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002420 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002421 struct net *net = SVC_NET(r);
2422 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002423
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002424 nfs4_lock_state();
2425 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002426 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002427 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002428 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002429 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002430 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002431 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002432 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002433 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002434 if (!ses)
2435 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002436 status = nfserr_wrong_cred;
2437 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002438 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002439 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002440 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002441 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002442 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002443 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002444
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002445 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002446
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002447 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002448 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002449out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002450 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002451out_client_lock:
2452 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002453out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002454 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002455 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002456}
2457
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002458static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002459{
2460 struct nfsd4_conn *c;
2461
2462 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002463 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002464 return c;
2465 }
2466 }
2467 return NULL;
2468}
2469
J. Bruce Fields57266a62013-04-13 14:27:29 -04002470static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002471{
2472 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002473 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002474 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002475 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002476
2477 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002478 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002479 if (c)
2480 goto out_free;
2481 status = nfserr_conn_not_bound_to_session;
2482 if (clp->cl_mach_cred)
2483 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002484 __nfsd4_hash_conn(new, ses);
2485 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002486 ret = nfsd4_register_conn(new);
2487 if (ret)
2488 /* oops; xprt is already down: */
2489 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002490 return nfs_ok;
2491out_free:
2492 spin_unlock(&clp->cl_lock);
2493 free_conn(new);
2494 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002495}
2496
Mi Jinlong868b89c2011-04-27 09:09:58 +08002497static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2498{
2499 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2500
2501 return args->opcnt > session->se_fchannel.maxops;
2502}
2503
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002504static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2505 struct nfsd4_session *session)
2506{
2507 struct xdr_buf *xb = &rqstp->rq_arg;
2508
2509 return xb->len > session->se_fchannel.maxreq_sz;
2510}
2511
Andy Adamson069b6ad2009-04-03 08:27:58 +03002512__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002513nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002514 struct nfsd4_compound_state *cstate,
2515 struct nfsd4_sequence *seq)
2516{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002517 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002518 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002519 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002520 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002521 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002522 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002523 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002524 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002525 struct net *net = SVC_NET(rqstp);
2526 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002527
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002528 if (resp->opcnt != 1)
2529 return nfserr_sequence_pos;
2530
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002531 /*
2532 * Will be either used or freed by nfsd4_sequence_check_conn
2533 * below.
2534 */
2535 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2536 if (!conn)
2537 return nfserr_jukebox;
2538
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002539 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002540 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002541 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002542 goto out_no_session;
2543 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002544
Mi Jinlong868b89c2011-04-27 09:09:58 +08002545 status = nfserr_too_many_ops;
2546 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002547 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002548
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002549 status = nfserr_req_too_big;
2550 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002551 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002552
Benny Halevyb85d4c02009-04-03 08:28:08 +03002553 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002554 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002555 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002556
Andy Adamson557ce262009-08-28 08:45:04 -04002557 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002558 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2559
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002560 /* We do not negotiate the number of slots yet, so set the
2561 * maxslots to the session maxreqs which is used to encode
2562 * sr_highest_slotid and the sr_target_slot id to maxslots */
2563 seq->maxslots = session->se_fchannel.maxreqs;
2564
J. Bruce Fields73e79482012-02-13 16:39:00 -05002565 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2566 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002567 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002568 status = nfserr_seq_misordered;
2569 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002570 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002571 cstate->slot = slot;
2572 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002573 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002574 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002575 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002576 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002577 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002578 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002579 }
2580 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002581 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002582
J. Bruce Fields57266a62013-04-13 14:27:29 -04002583 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002584 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002585 if (status)
2586 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002587
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002588 buflen = (seq->cachethis) ?
2589 session->se_fchannel.maxresp_cached :
2590 session->se_fchannel.maxresp_sz;
2591 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2592 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002593 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002594 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002595 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002596
2597 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002598 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002599 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002600 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002601 if (seq->cachethis)
2602 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002603 else
2604 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002605
2606 cstate->slot = slot;
2607 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002608 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002609
Benny Halevyb85d4c02009-04-03 08:28:08 +03002610out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002611 switch (clp->cl_cb_state) {
2612 case NFSD4_CB_DOWN:
2613 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2614 break;
2615 case NFSD4_CB_FAULT:
2616 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2617 break;
2618 default:
2619 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002620 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002621 if (!list_empty(&clp->cl_revoked))
2622 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002623out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002624 if (conn)
2625 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002626 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002627 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002628out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002629 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002630 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002631}
2632
Trond Myklebustb6076642014-06-30 11:48:35 -04002633void
2634nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2635{
2636 struct nfsd4_compound_state *cs = &resp->cstate;
2637
2638 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002639 if (cs->status != nfserr_replay_cache) {
2640 nfsd4_store_cache_entry(resp);
2641 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2642 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002643 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002644 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002645 } else if (cs->clp)
2646 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002647}
2648
Mi Jinlong345c2842011-10-20 17:51:39 +08002649__be32
2650nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2651{
2652 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002653 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002654 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002655
2656 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002657 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002658 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002659 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002660
2661 if (conf) {
2662 clp = conf;
2663
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002664 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002665 status = nfserr_clientid_busy;
2666 goto out;
2667 }
2668 } else if (unconf)
2669 clp = unconf;
2670 else {
2671 status = nfserr_stale_clientid;
2672 goto out;
2673 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002674 if (!mach_creds_match(clp, rqstp)) {
2675 status = nfserr_wrong_cred;
2676 goto out;
2677 }
Mi Jinlong345c2842011-10-20 17:51:39 +08002678 expire_client(clp);
2679out:
2680 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002681 return status;
2682}
2683
Andy Adamson069b6ad2009-04-03 08:27:58 +03002684__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002685nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2686{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002687 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002688
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002689 if (rc->rca_one_fs) {
2690 if (!cstate->current_fh.fh_dentry)
2691 return nfserr_nofilehandle;
2692 /*
2693 * We don't take advantage of the rca_one_fs case.
2694 * That's OK, it's optional, we can safely ignore it.
2695 */
2696 return nfs_ok;
2697 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002698
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002699 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002700 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002701 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2702 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002703 goto out;
2704
2705 status = nfserr_stale_clientid;
2706 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002707 /*
2708 * The following error isn't really legal.
2709 * But we only get here if the client just explicitly
2710 * destroyed the client. Surely it no longer cares what
2711 * error it gets back on an operation for the dead
2712 * client.
2713 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002714 goto out;
2715
2716 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002717 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002718out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002719 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002720 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002721}
2722
2723__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002724nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2725 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002727 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002729 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002730 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002731 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2732
J. Bruce Fields63db4632012-05-18 21:54:19 -04002733 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002735 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002736 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002737 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002739 if (clp_used_exchangeid(conf))
2740 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002741 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002742 char addr_str[INET6_ADDRSTRLEN];
2743 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2744 sizeof(addr_str));
2745 dprintk("NFSD: setclientid: string in use by client "
2746 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 goto out;
2748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002750 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002751 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002754 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002755 if (new == NULL)
2756 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002757 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002758 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002760 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002761 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002762 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002763 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002764 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2766 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2767 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2768 status = nfs_ok;
2769out:
2770 nfs4_unlock_state();
2771 return status;
2772}
2773
2774
Al Virob37ad282006-10-19 23:28:59 -07002775__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002776nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2777 struct nfsd4_compound_state *cstate,
2778 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779{
NeilBrown21ab45a2005-06-23 22:04:14 -07002780 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2782 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002783 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002784 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002786 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002789
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002790 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002791 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002792 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002793 * We try hard to give out unique clientid's, so if we get an
2794 * attempt to confirm the same clientid with a different cred,
2795 * there's a bug somewhere. Let's charitably assume it's our
2796 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002797 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002798 status = nfserr_serverfault;
2799 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2800 goto out;
2801 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2802 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002803 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002804 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2805 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002807 else /* case 4: client hasn't noticed we rebooted yet? */
2808 status = nfserr_stale_clientid;
2809 goto out;
2810 }
2811 status = nfs_ok;
2812 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002813 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2814 nfsd4_probe_callback(conf);
2815 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002816 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002817 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002818 if (conf) {
2819 status = mark_client_expired(conf);
2820 if (status)
2821 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002822 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002823 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002824 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002825 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 nfs4_unlock_state();
2829 return status;
2830}
2831
J. Bruce Fields32513b42011-10-13 16:00:16 -04002832static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002834 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2835}
2836
2837/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04002838static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04002839{
Trond Myklebustca943212014-07-23 16:17:39 -04002840 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
Trond Myklebust950e0112014-06-30 11:48:31 -04002842 lockdep_assert_held(&state_lock);
2843
J. Bruce Fields32513b42011-10-13 16:00:16 -04002844 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04002845 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002846 INIT_LIST_HEAD(&fp->fi_stateids);
2847 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04002848 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002849 fp->fi_had_conflict = false;
2850 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04002851 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002852 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2853 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04002854 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855}
2856
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002857void
NeilBrowne60d4392005-06-23 22:03:01 -07002858nfsd4_free_slabs(void)
2859{
Christoph Hellwigabf11352014-05-21 07:43:03 -07002860 kmem_cache_destroy(openowner_slab);
2861 kmem_cache_destroy(lockowner_slab);
2862 kmem_cache_destroy(file_slab);
2863 kmem_cache_destroy(stateid_slab);
2864 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002865}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Bryan Schumaker72083392011-11-01 15:24:59 -04002867int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868nfsd4_init_slabs(void)
2869{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002870 openowner_slab = kmem_cache_create("nfsd4_openowners",
2871 sizeof(struct nfs4_openowner), 0, 0, NULL);
2872 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002873 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002874 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002875 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002876 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002877 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07002878 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002879 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002880 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002881 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07002882 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002883 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002884 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002885 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002886 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002887 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002888 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002889 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07002891
2892out_free_stateid_slab:
2893 kmem_cache_destroy(stateid_slab);
2894out_free_file_slab:
2895 kmem_cache_destroy(file_slab);
2896out_free_lockowner_slab:
2897 kmem_cache_destroy(lockowner_slab);
2898out_free_openowner_slab:
2899 kmem_cache_destroy(openowner_slab);
2900out:
NeilBrowne60d4392005-06-23 22:03:01 -07002901 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2902 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903}
2904
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002905static void init_nfs4_replay(struct nfs4_replay *rp)
2906{
2907 rp->rp_status = nfserr_serverfault;
2908 rp->rp_buflen = 0;
2909 rp->rp_buf = rp->rp_ibuf;
2910}
2911
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002912static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913{
2914 struct nfs4_stateowner *sop;
2915
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002916 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002917 if (!sop)
2918 return NULL;
2919
2920 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2921 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002922 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002923 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002925 sop->so_owner.len = owner->len;
2926
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002927 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002928 sop->so_client = clp;
2929 init_nfs4_replay(&sop->so_replay);
2930 return sop;
2931}
2932
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002933static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002934{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002935 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2936
2937 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002938 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939}
2940
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002941static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04002942alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002943 struct nfsd4_compound_state *cstate)
2944{
Trond Myklebust13d6f662014-06-30 11:48:45 -04002945 struct nfs4_client *clp = cstate->clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002946 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002948 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2949 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002951 oo->oo_owner.so_is_open_owner = 1;
2952 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04002953 oo->oo_flags = NFS4_OO_NEW;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002954 if (nfsd4_has_session(cstate))
2955 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002956 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002957 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002958 INIT_LIST_HEAD(&oo->oo_close_lru);
2959 hash_openowner(oo, clp, strhashval);
2960 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961}
2962
J. Bruce Fields996e0932011-10-17 11:14:48 -04002963static 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 -04002964 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002966 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04002967 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002968 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002969 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002970 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 stp->st_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 stp->st_access_bmap = 0;
2973 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07002974 stp->st_openstp = NULL;
Trond Myklebust1d31a252014-07-10 14:07:25 -04002975 spin_lock(&fp->fi_lock);
2976 list_add(&stp->st_perfile, &fp->fi_stateids);
2977 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978}
2979
2980static void
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002981move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982{
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002983 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2984
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002985 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002987 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002988 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989}
2990
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002992same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2993 clientid_t *clid)
2994{
2995 return (sop->so_owner.len == owner->len) &&
2996 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2997 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998}
2999
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003000static struct nfs4_openowner *
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003001find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
3002 bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003004 struct nfs4_stateowner *so;
3005 struct nfs4_openowner *oo;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003006 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003008 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003009 if (!so->so_is_open_owner)
3010 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003011 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
3012 oo = openowner(so);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003013 clp = oo->oo_owner.so_client;
3014 if ((bool)clp->cl_minorversion != sessions)
3015 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003016 renew_client(oo->oo_owner.so_client);
3017 return oo;
3018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 }
3020 return NULL;
3021}
3022
3023/* search file_hashtbl[] for file */
3024static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003025find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026{
Trond Myklebustca943212014-07-23 16:17:39 -04003027 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 struct nfs4_file *fp;
3029
Trond Myklebust950e0112014-06-30 11:48:31 -04003030 lockdep_assert_held(&state_lock);
3031
Jeff Layton89876f82013-04-02 09:01:59 -04003032 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003033 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003034 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 }
3038 return NULL;
3039}
3040
Trond Myklebust950e0112014-06-30 11:48:31 -04003041static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003042find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003043{
3044 struct nfs4_file *fp;
3045
3046 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003047 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003048 spin_unlock(&state_lock);
3049 return fp;
3050}
3051
3052static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003053find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003054{
3055 struct nfs4_file *fp;
3056
3057 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003058 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003059 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003060 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003061 fp = new;
3062 }
3063 spin_unlock(&state_lock);
3064
3065 return fp;
3066}
3067
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003068/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 * Called to check deny when READ with all zero stateid or
3070 * WRITE with all zero or all one stateid
3071 */
Al Virob37ad282006-10-19 23:28:59 -07003072static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3074{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003076 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
Trond Myklebustca943212014-07-23 16:17:39 -04003078 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003079 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003080 return ret;
3081 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003082 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003083 if (fp->fi_share_deny & deny_type)
3084 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003085 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003086 put_nfs4_file(fp);
3087 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088}
3089
Jeff Layton02e12152014-07-16 10:31:57 -04003090void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091{
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003092 struct nfs4_client *clp = dp->dl_stid.sc_client;
3093 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3094
Jeff Layton02e12152014-07-16 10:31:57 -04003095 /*
3096 * We can't do this in nfsd_break_deleg_cb because it is
3097 * already holding inode->i_lock
3098 */
3099 spin_lock(&state_lock);
3100 block_delegations(&dp->dl_fh);
Jeff Laytondff13992014-07-08 14:02:49 -04003101 /*
3102 * If the dl_time != 0, then we know that it has already been
3103 * queued for a lease break. Don't queue it again.
3104 */
3105 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003106 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003107 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003108 }
Jeff Layton02e12152014-07-16 10:31:57 -04003109 spin_unlock(&state_lock);
3110}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Jeff Layton02e12152014-07-16 10:31:57 -04003112static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3113{
3114 /*
3115 * We're assuming the state code never drops its reference
3116 * without first removing the lease. Since we're in this lease
3117 * callback (and since the lease code is serialized by the kernel
3118 * lock) we know the server hasn't removed the lease yet, we know
3119 * it's safe to take a reference.
3120 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003121 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003122 nfsd4_cb_recall(dp);
3123}
3124
Jeff Layton1c8c6012013-06-21 08:58:15 -04003125/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003126static void nfsd_break_deleg_cb(struct file_lock *fl)
3127{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003128 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3129 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003130
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003131 if (!fp) {
3132 WARN(1, "(%p)->fl_owner NULL\n", fl);
3133 return;
3134 }
3135 if (fp->fi_had_conflict) {
3136 WARN(1, "duplicate break on %p\n", fp);
3137 return;
3138 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003139 /*
3140 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003141 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003142 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003143 */
3144 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
Jeff Layton02e12152014-07-16 10:31:57 -04003146 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003147 fp->fi_had_conflict = true;
3148 /*
3149 * If there are no delegations on the list, then we can't count on this
3150 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3151 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3152 * true should keep any new delegations from being hashed.
3153 */
3154 if (list_empty(&fp->fi_delegations))
3155 fl->fl_break_time = jiffies;
3156 else
3157 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3158 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003159 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160}
3161
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162static
3163int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3164{
3165 if (arg & F_UNLCK)
3166 return lease_modify(onlist, arg);
3167 else
3168 return -EAGAIN;
3169}
3170
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003171static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003172 .lm_break = nfsd_break_deleg_cb,
3173 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174};
3175
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003176static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3177{
3178 if (nfsd4_has_session(cstate))
3179 return nfs_ok;
3180 if (seqid == so->so_seqid - 1)
3181 return nfserr_replay_me;
3182 if (seqid == so->so_seqid)
3183 return nfs_ok;
3184 return nfserr_bad_seqid;
3185}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
Jeff Layton4b24ca72014-06-30 11:48:44 -04003187static __be32 lookup_clientid(clientid_t *clid,
3188 struct nfsd4_compound_state *cstate,
3189 struct nfsd_net *nn)
3190{
3191 struct nfs4_client *found;
3192
3193 if (cstate->clp) {
3194 found = cstate->clp;
3195 if (!same_clid(&found->cl_clientid, clid))
3196 return nfserr_stale_clientid;
3197 return nfs_ok;
3198 }
3199
3200 if (STALE_CLIENTID(clid, nn))
3201 return nfserr_stale_clientid;
3202
3203 /*
3204 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3205 * cached already then we know this is for is for v4.0 and "sessions"
3206 * will be false.
3207 */
3208 WARN_ON_ONCE(cstate->session);
3209 found = find_confirmed_client(clid, false, nn);
3210 if (!found)
3211 return nfserr_expired;
3212
3213 /* Cache the nfs4_client in cstate! */
3214 cstate->clp = found;
3215 atomic_inc(&found->cl_refcount);
3216 return nfs_ok;
3217}
3218
Al Virob37ad282006-10-19 23:28:59 -07003219__be32
Andy Adamson66689582009-04-03 08:28:45 +03003220nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003221 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 clientid_t *clientid = &open->op_clientid;
3224 struct nfs4_client *clp = NULL;
3225 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003226 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003227 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003229 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003231 /*
3232 * In case we need it later, after we've already created the
3233 * file and don't want to risk a further failure:
3234 */
3235 open->op_file = nfsd4_alloc_file();
3236 if (open->op_file == NULL)
3237 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Trond Myklebust2d91e892014-06-30 11:48:46 -04003239 status = lookup_clientid(clientid, cstate, nn);
3240 if (status)
3241 return status;
3242 clp = cstate->clp;
3243
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003244 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003245 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003246 open->op_openowner = oo;
3247 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003248 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003250 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003251 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003252 release_openowner(oo);
3253 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003254 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003255 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003256 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3257 if (status)
3258 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003259 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003260new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003261 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003262 if (oo == NULL)
3263 return nfserr_jukebox;
3264 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003265alloc_stateid:
3266 open->op_stp = nfs4_alloc_stateid(clp);
3267 if (!open->op_stp)
3268 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003269 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270}
3271
Al Virob37ad282006-10-19 23:28:59 -07003272static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003273nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3274{
3275 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3276 return nfserr_openmode;
3277 else
3278 return nfs_ok;
3279}
3280
Daniel Mackc47d8322011-05-16 16:38:14 +02003281static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003282{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003283 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3284}
3285
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003286static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003287{
3288 struct nfs4_stid *ret;
3289
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003290 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003291 if (!ret)
3292 return NULL;
3293 return delegstateid(ret);
3294}
3295
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003296static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3297{
3298 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3299 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3300}
3301
Al Virob37ad282006-10-19 23:28:59 -07003302static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003303nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003304 struct nfs4_delegation **dp)
3305{
3306 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003307 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07003308
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003309 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07003310 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003311 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003312 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07003313 status = nfs4_check_delegmode(*dp, flags);
3314 if (status)
3315 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003316out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003317 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003318 return nfs_ok;
3319 if (status)
3320 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003321 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003322 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003323}
3324
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003325static struct nfs4_ol_stateid *
3326nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003328 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003329 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330
Trond Myklebust1d31a252014-07-10 14:07:25 -04003331 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003332 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 /* ignore lock owners */
3334 if (local->st_stateowner->so_is_open_owner == 0)
3335 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003336 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003337 ret = local;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003338 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003341 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003342 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343}
3344
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003345static inline int nfs4_access_to_access(u32 nfs4_access)
3346{
3347 int flags = 0;
3348
3349 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3350 flags |= NFSD_MAY_READ;
3351 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3352 flags |= NFSD_MAY_WRITE;
3353 return flags;
3354}
3355
Al Virob37ad282006-10-19 23:28:59 -07003356static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3358 struct nfsd4_open *open)
3359{
3360 struct iattr iattr = {
3361 .ia_valid = ATTR_SIZE,
3362 .ia_size = 0,
3363 };
3364 if (!open->op_truncate)
3365 return 0;
3366 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003367 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3369}
3370
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003371static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003372 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3373 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003374{
Trond Myklebustde186432014-07-10 14:07:26 -04003375 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003376 __be32 status;
3377 int oflag = nfs4_access_to_omode(open->op_share_access);
3378 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003379 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003380
Trond Myklebustde186432014-07-10 14:07:26 -04003381 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003382
3383 /*
3384 * Are we trying to set a deny mode that would conflict with
3385 * current access?
3386 */
3387 status = nfs4_file_check_deny(fp, open->op_share_deny);
3388 if (status != nfs_ok) {
3389 spin_unlock(&fp->fi_lock);
3390 goto out;
3391 }
3392
3393 /* set access to the file */
3394 status = nfs4_file_get_access(fp, open->op_share_access);
3395 if (status != nfs_ok) {
3396 spin_unlock(&fp->fi_lock);
3397 goto out;
3398 }
3399
3400 /* Set access bits in stateid */
3401 old_access_bmap = stp->st_access_bmap;
3402 set_access(open->op_share_access, stp);
3403
3404 /* Set new deny mask */
3405 old_deny_bmap = stp->st_deny_bmap;
3406 set_deny(open->op_share_deny, stp);
3407 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3408
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003409 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003410 spin_unlock(&fp->fi_lock);
3411 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003412 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003413 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003414 spin_lock(&fp->fi_lock);
3415 if (!fp->fi_fds[oflag]) {
3416 fp->fi_fds[oflag] = filp;
3417 filp = NULL;
3418 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003419 }
Trond Myklebustde186432014-07-10 14:07:26 -04003420 spin_unlock(&fp->fi_lock);
3421 if (filp)
3422 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003423
3424 status = nfsd4_truncate(rqstp, cur_fh, open);
3425 if (status)
3426 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003427out:
3428 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003429out_put_access:
3430 stp->st_access_bmap = old_access_bmap;
3431 nfs4_file_put_access(fp, open->op_share_access);
3432 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3433 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003434}
3435
Al Virob37ad282006-10-19 23:28:59 -07003436static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003437nfs4_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 -07003438{
Al Virob37ad282006-10-19 23:28:59 -07003439 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003440 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003442 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003443 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003444
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003445 /* test and set deny mode */
3446 spin_lock(&fp->fi_lock);
3447 status = nfs4_file_check_deny(fp, open->op_share_deny);
3448 if (status == nfs_ok) {
3449 old_deny_bmap = stp->st_deny_bmap;
3450 set_deny(open->op_share_deny, stp);
3451 fp->fi_share_deny |=
3452 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3453 }
3454 spin_unlock(&fp->fi_lock);
3455
3456 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003459 status = nfsd4_truncate(rqstp, cur_fh, open);
3460 if (status != nfs_ok)
3461 reset_union_bmap_deny(old_deny_bmap, stp);
3462 return status;
3463}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003466nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003468 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469}
3470
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003471/* Should we give out recallable state?: */
3472static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3473{
3474 if (clp->cl_cb_state == NFSD4_CB_UP)
3475 return true;
3476 /*
3477 * In the sessions case, since we don't have to establish a
3478 * separate connection for callbacks, we assume it's OK
3479 * until we hear otherwise:
3480 */
3481 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3482}
3483
Jeff Laytond564fbe2014-07-16 10:31:58 -04003484static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003485{
3486 struct file_lock *fl;
3487
3488 fl = locks_alloc_lock();
3489 if (!fl)
3490 return NULL;
3491 locks_init_lock(fl);
3492 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003493 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003494 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3495 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003496 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003497 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003498 return fl;
3499}
3500
J. Bruce Fields99c41512013-05-21 16:21:25 -04003501static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003502{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003503 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003504 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003505 struct file *filp;
3506 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003507
Jeff Laytond564fbe2014-07-16 10:31:58 -04003508 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003509 if (!fl)
3510 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003511 filp = find_readable_file(fp);
3512 if (!filp) {
3513 /* We should always have a readable file here */
3514 WARN_ON_ONCE(1);
3515 return -EBADF;
3516 }
3517 fl->fl_file = filp;
3518 status = vfs_setlease(filp, fl->fl_type, &fl);
3519 if (status) {
3520 locks_free_lock(fl);
3521 goto out_fput;
3522 }
Benny Halevycdc97502014-05-30 09:09:30 -04003523 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003524 spin_lock(&fp->fi_lock);
3525 /* Did the lease get broken before we took the lock? */
3526 status = -EAGAIN;
3527 if (fp->fi_had_conflict)
3528 goto out_unlock;
3529 /* Race breaker */
3530 if (fp->fi_lease) {
3531 status = 0;
3532 atomic_inc(&fp->fi_delegees);
3533 hash_delegation_locked(dp, fp);
3534 goto out_unlock;
3535 }
3536 fp->fi_lease = fl;
3537 fp->fi_deleg_file = filp;
3538 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003539 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003540 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003541 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003542 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003543out_unlock:
3544 spin_unlock(&fp->fi_lock);
3545 spin_unlock(&state_lock);
3546out_fput:
3547 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003548 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003549}
3550
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003551static int nfs4_set_delegation(struct nfs4_delegation *dp, struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003552{
Jeff Layton417c6622014-07-21 09:34:57 -04003553 int status = 0;
3554
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003555 if (fp->fi_had_conflict)
3556 return -EAGAIN;
3557 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003558 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003559 spin_lock(&fp->fi_lock);
3560 dp->dl_file = fp;
3561 if (!fp->fi_lease) {
3562 spin_unlock(&fp->fi_lock);
3563 spin_unlock(&state_lock);
3564 return nfs4_setlease(dp);
3565 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003566 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003567 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003568 status = -EAGAIN;
3569 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003570 }
Benny Halevy931ee562014-05-30 09:09:27 -04003571 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003572out_unlock:
3573 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003574 spin_unlock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003575 return status;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003576}
3577
Benny Halevy4aa89132012-02-21 14:16:44 -08003578static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3579{
3580 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3581 if (status == -EAGAIN)
3582 open->op_why_no_deleg = WND4_CONTENTION;
3583 else {
3584 open->op_why_no_deleg = WND4_RESOURCE;
3585 switch (open->op_deleg_want) {
3586 case NFS4_SHARE_WANT_READ_DELEG:
3587 case NFS4_SHARE_WANT_WRITE_DELEG:
3588 case NFS4_SHARE_WANT_ANY_DELEG:
3589 break;
3590 case NFS4_SHARE_WANT_CANCEL:
3591 open->op_why_no_deleg = WND4_CANCELLED;
3592 break;
3593 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003594 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003595 }
3596 }
3597}
3598
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599/*
3600 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003601 *
3602 * Note we don't support write delegations, and won't until the vfs has
3603 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 */
3605static void
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003606nfs4_open_delegation(struct net *net, struct svc_fh *fh,
3607 struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608{
3609 struct nfs4_delegation *dp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003610 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003611 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003612 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003614 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003615 open->op_recall = 0;
3616 switch (open->op_claim_type) {
3617 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003618 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003619 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003620 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3621 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003622 break;
3623 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003624 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003625 /*
3626 * Let's not give out any delegations till everyone's
3627 * had the chance to reclaim theirs....
3628 */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003629 if (locks_in_grace(net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003630 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003631 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003632 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003633 /*
3634 * Also, if the file was opened for write or
3635 * create, there's a good chance the client's
3636 * about to write to it, resulting in an
3637 * immediate recall (since we don't support
3638 * write delegations):
3639 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003640 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003641 goto out_no_deleg;
3642 if (open->op_create == NFS4_OPEN_CREATE)
3643 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003644 break;
3645 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003646 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003647 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003648 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003649 if (dp == NULL)
3650 goto out_no_deleg;
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003651 status = nfs4_set_delegation(dp, stp->st_file);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003652 if (status)
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003653 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003655 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003657 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003658 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003659 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003660 return;
3661out_free:
Jeff Layton42690672014-07-25 07:34:20 -04003662 nfs4_put_delegation(dp);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003663out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003664 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3665 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003666 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003667 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003668 open->op_recall = 1;
3669 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003670
3671 /* 4.1 client asking for a delegation? */
3672 if (open->op_deleg_want)
3673 nfsd4_open_deleg_none_ext(open, status);
3674 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675}
3676
Benny Halevye27f49c2012-02-21 14:16:54 -08003677static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3678 struct nfs4_delegation *dp)
3679{
3680 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3681 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3682 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3683 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3684 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3685 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3686 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3687 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3688 }
3689 /* Otherwise the client must be confused wanting a delegation
3690 * it already has, therefore we don't return
3691 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3692 */
3693}
3694
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695/*
3696 * called with nfs4_lock_state() held.
3697 */
Al Virob37ad282006-10-19 23:28:59 -07003698__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3700{
Andy Adamson66689582009-04-03 08:28:45 +03003701 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003702 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003704 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003705 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003706 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 /*
3709 * Lookup file; if found, lookup stateid and check open request,
3710 * and check for delegations in the process of being recalled.
3711 * If not found, create the nfs4_file struct
3712 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003713 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04003714 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04003715 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003716 if (status)
3717 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003718 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003720 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003721 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003722 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003723 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003724 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 }
3726
3727 /*
3728 * OPEN the file, or upgrade an existing OPEN.
3729 * If truncate fails, the OPEN fails.
3730 */
3731 if (stp) {
3732 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003733 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 if (status)
3735 goto out;
3736 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003737 stp = open->op_stp;
3738 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003739 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003740 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3741 if (status) {
3742 release_open_stateid(stp);
3743 goto out;
3744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003746 update_stateid(&stp->st_stid.sc_stateid);
3747 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
Benny Halevyd24433c2012-02-16 20:57:17 +02003749 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02003750 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3751 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3752 open->op_why_no_deleg = WND4_NOT_WANTED;
3753 goto nodeleg;
3754 }
3755 }
3756
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 /*
3758 * Attempt to hand out a delegation. No error return, because the
3759 * OPEN succeeds even if we fail.
3760 */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003761 nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003762nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 status = nfs_ok;
3764
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003765 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003766 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003768 /* 4.1 client trying to upgrade/downgrade delegation? */
3769 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003770 open->op_deleg_want)
3771 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003772
NeilBrown13cd2182005-06-23 22:03:10 -07003773 if (fp)
3774 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003775 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003776 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 /*
3778 * To finish the open response, we just need to set the rflags.
3779 */
3780 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003781 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003782 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3784
3785 return status;
3786}
3787
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003788void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3789{
3790 if (open->op_openowner) {
3791 struct nfs4_openowner *oo = open->op_openowner;
3792
3793 if (!list_empty(&oo->oo_owner.so_stateids))
3794 list_del_init(&oo->oo_close_lru);
3795 if (oo->oo_flags & NFS4_OO_NEW) {
3796 if (status) {
3797 release_openowner(oo);
3798 open->op_openowner = NULL;
3799 } else
3800 oo->oo_flags &= ~NFS4_OO_NEW;
3801 }
3802 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003803 if (open->op_file)
3804 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003805 if (open->op_stp)
J. Bruce Fieldsef798592012-08-29 10:32:54 -07003806 free_generic_stateid(open->op_stp);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003807}
3808
Al Virob37ad282006-10-19 23:28:59 -07003809__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003810nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3811 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812{
3813 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003814 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003815 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816
3817 nfs4_lock_state();
3818 dprintk("process_renew(%08x/%08x): starting\n",
3819 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003820 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003821 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003823 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003825 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003826 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 goto out;
3828 status = nfs_ok;
3829out:
3830 nfs4_unlock_state();
3831 return status;
3832}
3833
NeilBrowna76b4312005-06-23 22:04:01 -07003834static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003835nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07003836{
Jeff Layton33dcc482012-04-10 11:08:48 -04003837 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003838 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003839 return;
3840
NeilBrowna76b4312005-06-23 22:04:01 -07003841 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003842 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003843 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003844 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003845 /*
3846 * Now that every NFSv4 client has had the chance to recover and
3847 * to see the (possibly new, possibly shorter) lease time, we
3848 * can safely set the next grace time to the current lease time:
3849 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03003850 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003851}
3852
NeilBrownfd39ca92005-06-23 22:04:03 -07003853static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003854nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855{
3856 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003857 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 struct nfs4_delegation *dp;
3859 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003860 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003861 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862
3863 nfs4_lock_state();
3864
3865 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003866 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03003867 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003868 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03003869 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 clp = list_entry(pos, struct nfs4_client, cl_lru);
3871 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3872 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003873 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 break;
3875 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003876 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03003877 dprintk("NFSD: client in use (clientid %08x)\n",
3878 clp->cl_clientid.cl_id);
3879 continue;
3880 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003881 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003882 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003883 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03003884 list_for_each_safe(pos, next, &reaplist) {
3885 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 dprintk("NFSD: purging unused client (clientid %08x)\n",
3887 clp->cl_clientid.cl_id);
3888 expire_client(clp);
3889 }
Benny Halevycdc97502014-05-30 09:09:30 -04003890 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003891 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03003893 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3894 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003896 t = dp->dl_time - cutoff;
3897 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 break;
3899 }
Jeff Layton42690672014-07-25 07:34:20 -04003900 unhash_delegation_locked(dp);
3901 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 }
Benny Halevycdc97502014-05-30 09:09:30 -04003903 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04003904 while (!list_empty(&reaplist)) {
3905 dp = list_first_entry(&reaplist, struct nfs4_delegation,
3906 dl_recall_lru);
3907 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003908 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003910 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003911 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3912 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003913 t = oo->oo_time - cutoff;
3914 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 break;
3916 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003917 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04003919 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04003921 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922}
3923
Harvey Harrisona254b242008-02-20 12:49:00 -08003924static struct workqueue_struct *laundry_wq;
3925static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08003926
3927static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003928laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929{
3930 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003931 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3932 work);
3933 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3934 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003936 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003938 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939}
3940
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003941static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003942{
Trond Myklebustb07c54a2014-07-23 16:17:40 -04003943 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003944 return nfserr_bad_stateid;
3945 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946}
3947
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003949access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003951 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3952 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3953 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954}
3955
3956static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003957access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003959 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3960 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961}
3962
3963static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003964__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965{
Al Virob37ad282006-10-19 23:28:59 -07003966 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967
J. Bruce Fields02921912010-07-29 15:16:59 -04003968 /* For lock stateid's, we test the parent open, not the lock: */
3969 if (stp->st_openstp)
3970 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003971 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003973 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 goto out;
3975 status = nfs_ok;
3976out:
3977 return status;
3978}
3979
Al Virob37ad282006-10-19 23:28:59 -07003980static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003981check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003983 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003985 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003986 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 * conflicting state; so we must wait out the grace period. */
3988 return nfserr_grace;
3989 } else if (flags & WR_STATE)
3990 return nfs4_share_conflict(current_fh,
3991 NFS4_SHARE_DENY_WRITE);
3992 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3993 return nfs4_share_conflict(current_fh,
3994 NFS4_SHARE_DENY_READ);
3995}
3996
3997/*
3998 * Allow READ/WRITE during grace period on recovered state only for files
3999 * that are not able to provide mandatory locking.
4000 */
4001static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004002grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004004 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005}
4006
J. Bruce Fields81b82962011-08-23 11:03:29 -04004007/* Returns true iff a is later than b: */
4008static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4009{
Jim Rees1a9357f2013-05-17 17:33:00 -04004010 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004011}
4012
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004013static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004014{
Andy Adamson66689582009-04-03 08:28:45 +03004015 /*
4016 * When sessions are used the stateid generation number is ignored
4017 * when it is zero.
4018 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004019 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004020 return nfs_ok;
4021
4022 if (in->si_generation == ref->si_generation)
4023 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004024
J. Bruce Fields0836f582008-01-26 19:08:12 -05004025 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004026 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004027 return nfserr_bad_stateid;
4028 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004029 * However, we could see a stateid from the past, even from a
4030 * non-buggy client. For example, if the client sends a lock
4031 * while some IO is outstanding, the lock may bump si_generation
4032 * while the IO is still in flight. The client could avoid that
4033 * situation by waiting for responses on all the IO requests,
4034 * but better performance may result in retrying IO that
4035 * receives an old_stateid error if requests are rarely
4036 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004037 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004038 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004039}
4040
Chuck Lever7df302f2012-05-29 13:56:37 -04004041static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004042{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004043 struct nfs4_stid *s;
4044 struct nfs4_ol_stateid *ols;
4045 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004046
Chuck Lever7df302f2012-05-29 13:56:37 -04004047 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4048 return nfserr_bad_stateid;
4049 /* Client debugging aid. */
4050 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4051 char addr_str[INET6_ADDRSTRLEN];
4052 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4053 sizeof(addr_str));
4054 pr_warn_ratelimited("NFSD: client %s testing state ID "
4055 "with incorrect client ID\n", addr_str);
4056 return nfserr_bad_stateid;
4057 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004058 s = find_stateid(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004059 if (!s)
Chuck Lever7df302f2012-05-29 13:56:37 -04004060 return nfserr_bad_stateid;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004061 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004062 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004063 return status;
J. Bruce Fields23340032013-04-09 17:42:28 -04004064 switch (s->sc_type) {
4065 case NFS4_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004066 return nfs_ok;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004067 case NFS4_REVOKED_DELEG_STID:
4068 return nfserr_deleg_revoked;
J. Bruce Fields23340032013-04-09 17:42:28 -04004069 case NFS4_OPEN_STID:
4070 case NFS4_LOCK_STID:
4071 ols = openlockstateid(s);
4072 if (ols->st_stateowner->so_is_open_owner
4073 && !(openowner(ols->st_stateowner)->oo_flags
4074 & NFS4_OO_CONFIRMED))
4075 return nfserr_bad_stateid;
4076 return nfs_ok;
4077 default:
4078 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004079 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004080 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004081 case NFS4_CLOSED_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004082 return nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004083 }
Bryan Schumaker17456802011-07-13 10:50:48 -04004084}
4085
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004086static __be32
4087nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4088 stateid_t *stateid, unsigned char typemask,
4089 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004090{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004091 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004092
4093 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4094 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004095 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004096 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004097 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004098 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004099 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004100 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004101 if (status)
4102 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004103 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004104 if (!*s)
4105 return nfserr_bad_stateid;
4106 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004107}
4108
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109/*
4110* Checks for stateid operations
4111*/
Al Virob37ad282006-10-19 23:28:59 -07004112__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004113nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004114 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004116 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004117 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004119 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004121 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004122 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004123 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 if (filpp)
4126 *filpp = NULL;
4127
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004128 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 return nfserr_grace;
4130
4131 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004132 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133
Trond Myklebust14bcab12014-04-18 14:44:07 -04004134 nfs4_lock_state();
4135
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004136 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004137 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004138 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004139 if (status)
Trond Myklebust14bcab12014-04-18 14:44:07 -04004140 goto out;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004141 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4142 if (status)
4143 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004144 switch (s->sc_type) {
4145 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004146 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004147 status = nfs4_check_delegmode(dp, flags);
4148 if (status)
4149 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004150 if (filpp) {
Trond Myklebust14bcab12014-04-18 14:44:07 -04004151 file = dp->dl_file->fi_deleg_file;
4152 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004153 WARN_ON_ONCE(1);
4154 status = nfserr_serverfault;
4155 goto out;
4156 }
Trond Myklebustde186432014-07-10 14:07:26 -04004157 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004158 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004159 break;
4160 case NFS4_OPEN_STID:
4161 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004162 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004163 status = nfs4_check_fh(current_fh, stp);
4164 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004166 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004167 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004169 status = nfs4_check_openmode(stp, flags);
4170 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004172 if (filpp) {
4173 if (flags & RD_STATE)
Trond Myklebust14bcab12014-04-18 14:44:07 -04004174 file = find_readable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004175 else
Trond Myklebust14bcab12014-04-18 14:44:07 -04004176 file = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004177 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004178 break;
4179 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004180 status = nfserr_bad_stateid;
4181 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 }
4183 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004184 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004185 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186out:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004187 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 return status;
4189}
4190
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004191static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004192nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004193{
J. Bruce Fieldsa1b8ff42014-05-20 15:55:21 -04004194 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4195
4196 if (check_for_locks(stp->st_file, lo))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004197 return nfserr_locks_held;
Jeff Laytonc53530d2014-06-30 11:48:39 -04004198 release_lockowner_if_empty(lo);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004199 return nfs_ok;
4200}
4201
4202/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004203 * Test if the stateid is valid
4204 */
4205__be32
4206nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4207 struct nfsd4_test_stateid *test_stateid)
4208{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004209 struct nfsd4_test_stateid_id *stateid;
4210 struct nfs4_client *cl = cstate->session->se_client;
4211
4212 nfs4_lock_state();
4213 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004214 stateid->ts_id_status =
4215 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004216 nfs4_unlock_state();
4217
Bryan Schumaker17456802011-07-13 10:50:48 -04004218 return nfs_ok;
4219}
4220
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004221__be32
4222nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4223 struct nfsd4_free_stateid *free_stateid)
4224{
4225 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004226 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004227 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004228 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004229 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004230
4231 nfs4_lock_state();
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004232 s = find_stateid(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004233 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004234 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004235 switch (s->sc_type) {
4236 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004237 ret = nfserr_locks_held;
4238 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004239 case NFS4_OPEN_STID:
4240 case NFS4_LOCK_STID:
4241 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4242 if (ret)
4243 goto out;
4244 if (s->sc_type == NFS4_LOCK_STID)
4245 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4246 else
4247 ret = nfserr_locks_held;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004248 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004249 case NFS4_REVOKED_DELEG_STID:
4250 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004251 spin_lock(&cl->cl_lock);
4252 list_del_init(&dp->dl_recall_lru);
4253 spin_unlock(&cl->cl_lock);
4254 nfs4_put_delegation(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004255 ret = nfs_ok;
4256 break;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004257 default:
4258 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004259 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004260out:
4261 nfs4_unlock_state();
4262 return ret;
4263}
4264
NeilBrown4c4cd222005-07-07 17:59:27 -07004265static inline int
4266setlkflg (int type)
4267{
4268 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4269 RD_STATE : WR_STATE;
4270}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004272static __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 -04004273{
4274 struct svc_fh *current_fh = &cstate->current_fh;
4275 struct nfs4_stateowner *sop = stp->st_stateowner;
4276 __be32 status;
4277
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004278 status = nfsd4_check_seqid(cstate, sop, seqid);
4279 if (status)
4280 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004281 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4282 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004283 /*
4284 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004285 * nfserr_replay_me from the previous step, and
4286 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004287 */
4288 return nfserr_bad_stateid;
4289 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4290 if (status)
4291 return status;
4292 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004293}
4294
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295/*
4296 * Checks for sequence id mutating operations.
4297 */
Al Virob37ad282006-10-19 23:28:59 -07004298static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004299nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004300 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004301 struct nfs4_ol_stateid **stpp,
4302 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004304 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004305 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004306 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004308 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4309 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004310
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004312 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004313 if (status)
4314 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004315 stp = openlockstateid(s);
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04004316 if (!nfsd4_has_session(cstate))
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004317 cstate->replay_owner = stp->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004319 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4320 if (!status)
4321 *stpp = stp;
4322 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004323}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004324
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004325static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4326 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004327{
4328 __be32 status;
4329 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004331 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004332 NFS4_OPEN_STID, stpp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004333 if (status)
4334 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004335 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004336 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07004337 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004338 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339}
4340
Al Virob37ad282006-10-19 23:28:59 -07004341__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004342nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004343 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344{
Al Virob37ad282006-10-19 23:28:59 -07004345 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004346 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004347 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004348 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
Al Viroa6a9f182013-09-16 10:57:01 -04004350 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4351 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004353 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004354 if (status)
4355 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356
4357 nfs4_lock_state();
4358
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004359 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004360 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004361 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004362 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004363 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004364 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004365 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004366 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004367 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004368 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004369 update_stateid(&stp->st_stid.sc_stateid);
4370 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004371 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004372 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004373
Jeff Layton2a4317c2012-03-21 16:42:43 -04004374 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004375 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004377 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004378 if (!cstate->replay_owner)
4379 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 return status;
4381}
4382
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004383static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004385 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004386 return;
Jeff Layton12659652014-07-10 14:07:28 -04004387 nfs4_file_put_access(stp->st_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004388 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004389}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004390
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004391static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4392{
4393 switch (to_access) {
4394 case NFS4_SHARE_ACCESS_READ:
4395 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4396 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4397 break;
4398 case NFS4_SHARE_ACCESS_WRITE:
4399 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4400 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4401 break;
4402 case NFS4_SHARE_ACCESS_BOTH:
4403 break;
4404 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004405 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 }
4407}
4408
Al Virob37ad282006-10-19 23:28:59 -07004409__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004410nfsd4_open_downgrade(struct svc_rqst *rqstp,
4411 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004412 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413{
Al Virob37ad282006-10-19 23:28:59 -07004414 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004415 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004416 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
Al Viroa6a9f182013-09-16 10:57:01 -04004418 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4419 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004421 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004422 if (od->od_deleg_want)
4423 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4424 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425
4426 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004427 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004428 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004429 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004432 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004433 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 stp->st_access_bmap, od->od_share_access);
4435 goto out;
4436 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004437 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004438 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 stp->st_deny_bmap, od->od_share_deny);
4440 goto out;
4441 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004442 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
Jeff Laytonce0fc432012-05-11 09:45:14 -04004444 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004446 update_stateid(&stp->st_stid.sc_stateid);
4447 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 status = nfs_ok;
4449out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004450 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004451 if (!cstate->replay_owner)
4452 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 return status;
4454}
4455
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004456static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4457{
Trond Myklebustacf92952014-06-30 11:48:37 -04004458 struct nfs4_client *clp = s->st_stid.sc_client;
4459 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4460
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004461 s->st_stid.sc_type = NFS4_CLOSED_STID;
Trond Myklebustacf92952014-06-30 11:48:37 -04004462 unhash_open_stateid(s);
4463
4464 if (clp->cl_minorversion) {
4465 free_generic_stateid(s);
4466 if (list_empty(&oo->oo_owner.so_stateids))
4467 release_openowner(oo);
4468 } else {
Trond Myklebustf8338832014-07-25 07:34:19 -04004469 if (s->st_file) {
4470 put_nfs4_file(s->st_file);
4471 s->st_file = NULL;
4472 }
Trond Myklebustacf92952014-06-30 11:48:37 -04004473 oo->oo_last_closed_stid = s;
4474 /*
4475 * In the 4.0 case we need to keep the owners around a
4476 * little while to handle CLOSE replay.
4477 */
4478 if (list_empty(&oo->oo_owner.so_stateids))
4479 move_to_close_lru(oo, clp->net);
4480 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004481}
4482
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483/*
4484 * nfs4_unlock_state() called after encode
4485 */
Al Virob37ad282006-10-19 23:28:59 -07004486__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004487nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004488 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489{
Al Virob37ad282006-10-19 23:28:59 -07004490 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004491 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004492 struct net *net = SVC_NET(rqstp);
4493 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494
Al Viroa6a9f182013-09-16 10:57:01 -04004495 dprintk("NFSD: nfsd4_close on file %pd\n",
4496 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497
4498 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004499 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4500 &close->cl_stateid,
4501 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004502 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004503 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004504 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004506 update_stateid(&stp->st_stid.sc_stateid);
4507 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004509 nfsd4_close_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004511 if (!cstate->replay_owner)
4512 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 return status;
4514}
4515
Al Virob37ad282006-10-19 23:28:59 -07004516__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004517nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4518 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004520 struct nfs4_delegation *dp;
4521 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004522 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004523 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004524 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004526 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004527 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528
4529 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004530 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004531 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004532 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004533 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004534 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004535 if (status)
4536 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004537
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004538 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004540 nfs4_unlock_state();
4541
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 return status;
4543}
4544
4545
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547
Benny Halevy87df4de2008-12-15 19:42:03 +02004548static inline u64
4549end_offset(u64 start, u64 len)
4550{
4551 u64 end;
4552
4553 end = start + len;
4554 return end >= start ? end: NFS4_MAX_UINT64;
4555}
4556
4557/* last octet in a range */
4558static inline u64
4559last_byte_offset(u64 start, u64 len)
4560{
4561 u64 end;
4562
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004563 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004564 end = start + len;
4565 return end > start ? end - 1: NFS4_MAX_UINT64;
4566}
4567
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568/*
4569 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4570 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4571 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4572 * locking, this prevents us from being completely protocol-compliant. The
4573 * real solution to this problem is to start using unsigned file offsets in
4574 * the VFS, but this is a very deep change!
4575 */
4576static inline void
4577nfs4_transform_lock_offset(struct file_lock *lock)
4578{
4579 if (lock->fl_start < 0)
4580 lock->fl_start = OFFSET_MAX;
4581 if (lock->fl_end < 0)
4582 lock->fl_end = OFFSET_MAX;
4583}
4584
NeilBrownd5b90262006-04-10 22:55:22 -07004585/* Hack!: For now, we're defining this just so we can use a pointer to it
4586 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004587static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004588};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589
4590static inline void
4591nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4592{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004593 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594
NeilBrownd5b90262006-04-10 22:55:22 -07004595 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004596 lo = (struct nfs4_lockowner *) fl->fl_owner;
4597 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4598 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004599 if (!deny->ld_owner.data)
4600 /* We just don't care that much */
4601 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004602 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4603 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004604 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004605nevermind:
4606 deny->ld_owner.len = 0;
4607 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004608 deny->ld_clientid.cl_boot = 0;
4609 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 }
4611 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004612 deny->ld_length = NFS4_MAX_UINT64;
4613 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4615 deny->ld_type = NFS4_READ_LT;
4616 if (fl->fl_type != F_RDLCK)
4617 deny->ld_type = NFS4_WRITE_LT;
4618}
4619
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004620static struct nfs4_lockowner *
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004621find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4622 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623{
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004624 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4625 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004627 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4628 if (so->so_is_open_owner)
4629 continue;
4630 if (!same_owner_str(so, owner, clid))
4631 continue;
4632 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 }
4634 return NULL;
4635}
4636
4637/*
4638 * Alloc a lock owner structure.
4639 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004640 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004642 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004644static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004645alloc_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 -04004646 struct nfs4_lockowner *lo;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004647 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004649 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4650 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004652 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4653 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07004654 /* It is the openowner seqid that will be incremented in encode in the
4655 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004656 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004657 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004658 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659}
4660
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004661static struct nfs4_ol_stateid *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004662alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp,
4663 struct inode *inode,
4664 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004666 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004667 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
J. Bruce Fields996e0932011-10-17 11:14:48 -04004669 stp = nfs4_alloc_stateid(clp);
NeilBrown5ac049a2005-06-23 22:03:03 -07004670 if (stp == NULL)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04004671 return NULL;
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004672 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004673 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4674 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004675 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 stp->st_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004677 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004679 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004680 list_add(&stp->st_locks, &open_stp->st_locks);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004681 spin_lock(&fp->fi_lock);
4682 list_add(&stp->st_perfile, &fp->fi_stateids);
4683 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 return stp;
4685}
4686
Jeff Laytonc53530d2014-06-30 11:48:39 -04004687static struct nfs4_ol_stateid *
4688find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4689{
4690 struct nfs4_ol_stateid *lst;
4691
4692 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
4693 if (lst->st_file == fp)
4694 return lst;
4695 }
4696 return NULL;
4697}
4698
4699
NeilBrownfd39ca92005-06-23 22:04:03 -07004700static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701check_lock_length(u64 offset, u64 length)
4702{
Benny Halevy87df4de2008-12-15 19:42:03 +02004703 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 LOFF_OVERFLOW(offset, length)));
4705}
4706
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004707static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004708{
4709 struct nfs4_file *fp = lock_stp->st_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004710
Jeff Layton7214e862014-07-10 14:07:33 -04004711 lockdep_assert_held(&fp->fi_lock);
4712
Jeff Layton82c5ff12012-05-11 09:45:13 -04004713 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004714 return;
Jeff Layton12659652014-07-10 14:07:28 -04004715 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004716 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004717}
4718
J. Bruce Fields2355c592012-04-25 16:56:22 -04004719static __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 -04004720{
4721 struct nfs4_file *fi = ost->st_file;
4722 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4723 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004724 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004725 struct nfs4_lockowner *lo;
4726 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004727 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004728
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004729 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004730 if (!lo) {
4731 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4732 &lock->v.new.owner);
4733 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4734 if (lo == NULL)
4735 return nfserr_jukebox;
4736 } else {
4737 /* with an existing lockowner, seqids must be the same */
4738 if (!cstate->minorversion &&
4739 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004740 return nfserr_bad_seqid;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004741 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004742
4743 *lst = find_lock_stateid(lo, fi);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004744 if (*lst == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004745 *lst = alloc_init_lock_stateid(lo, fi, inode, ost);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004746 if (*lst == NULL) {
4747 release_lockowner_if_empty(lo);
4748 return nfserr_jukebox;
4749 }
4750 *new = true;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004751 }
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004752 return nfs_ok;
4753}
4754
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755/*
4756 * LOCK operation
4757 */
Al Virob37ad282006-10-19 23:28:59 -07004758__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004759nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004760 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004762 struct nfs4_openowner *open_sop = NULL;
4763 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004764 struct nfs4_ol_stateid *lock_stp;
Jeff Layton7214e862014-07-10 14:07:33 -04004765 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004766 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004767 struct file_lock *file_lock = NULL;
4768 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004769 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004770 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004771 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004772 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004773 struct net *net = SVC_NET(rqstp);
4774 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775
4776 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4777 (long long) lock->lk_offset,
4778 (long long) lock->lk_length);
4779
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 if (check_lock_length(lock->lk_offset, lock->lk_length))
4781 return nfserr_inval;
4782
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004783 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004784 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004785 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4786 return status;
4787 }
4788
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 nfs4_lock_state();
4790
4791 if (lock->lk_is_new) {
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004792 struct nfs4_ol_stateid *open_stp = NULL;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004793
4794 if (nfsd4_has_session(cstate))
4795 /* See rfc 5661 18.10.3: given clientid is ignored: */
4796 memcpy(&lock->v.new.clientid,
4797 &cstate->session->se_client->cl_clientid,
4798 sizeof(clientid_t));
4799
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004801 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004805 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 lock->lk_new_open_seqid,
4807 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004808 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07004809 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004811 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004812 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004813 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004814 &lock->v.new.clientid))
4815 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004816 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4817 &lock_stp, &new_state);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004818 } else
Benny Halevydd453df2009-04-03 08:28:41 +03004819 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004820 lock->lk_old_lock_seqid,
4821 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004822 NFS4_LOCK_STID, &lock_stp, nn);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004823 if (status)
4824 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004825 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004827 lkflg = setlkflg(lock->lk_type);
4828 status = nfs4_check_openmode(lock_stp, lkflg);
4829 if (status)
4830 goto out;
4831
NeilBrown0dd395d2005-07-07 17:59:15 -07004832 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004833 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004834 goto out;
4835 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004836 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004837 goto out;
4838
Jeff Layton21179d82012-08-21 08:03:32 -04004839 file_lock = locks_alloc_lock();
4840 if (!file_lock) {
4841 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4842 status = nfserr_jukebox;
4843 goto out;
4844 }
4845
Jeff Layton7214e862014-07-10 14:07:33 -04004846 fp = lock_stp->st_file;
Jeff Layton21179d82012-08-21 08:03:32 -04004847 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 switch (lock->lk_type) {
4849 case NFS4_READ_LT:
4850 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004851 spin_lock(&fp->fi_lock);
4852 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004853 if (filp)
4854 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04004855 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004856 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004857 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 case NFS4_WRITE_LT:
4859 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004860 spin_lock(&fp->fi_lock);
4861 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004862 if (filp)
4863 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04004864 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004865 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004866 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 default:
4868 status = nfserr_inval;
4869 goto out;
4870 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004871 if (!filp) {
4872 status = nfserr_openmode;
4873 goto out;
4874 }
Jeff Layton21179d82012-08-21 08:03:32 -04004875 file_lock->fl_owner = (fl_owner_t)lock_sop;
4876 file_lock->fl_pid = current->tgid;
4877 file_lock->fl_file = filp;
4878 file_lock->fl_flags = FL_POSIX;
4879 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4880 file_lock->fl_start = lock->lk_offset;
4881 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4882 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883
Jeff Layton21179d82012-08-21 08:03:32 -04004884 conflock = locks_alloc_lock();
4885 if (!conflock) {
4886 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4887 status = nfserr_jukebox;
4888 goto out;
4889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890
Jeff Layton21179d82012-08-21 08:03:32 -04004891 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004892 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004894 update_stateid(&lock_stp->st_stid.sc_stateid);
4895 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004897 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004898 break;
4899 case (EAGAIN): /* conflock holds conflicting lock */
4900 status = nfserr_denied;
4901 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04004902 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004903 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 case (EDEADLK):
4905 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004906 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004907 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004908 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004909 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004910 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912out:
Trond Myklebustde186432014-07-10 14:07:26 -04004913 if (filp)
4914 fput(filp);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004915 if (status && new_state)
Jeff Laytonc53530d2014-06-30 11:48:39 -04004916 release_lock_stateid(lock_stp);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004917 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004918 if (!cstate->replay_owner)
4919 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004920 if (file_lock)
4921 locks_free_lock(file_lock);
4922 if (conflock)
4923 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 return status;
4925}
4926
4927/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004928 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4929 * so we do a temporary open here just to get an open file to pass to
4930 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4931 * inode operation.)
4932 */
Al Viro04da6e92012-04-13 00:00:04 -04004933static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004934{
4935 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04004936 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4937 if (!err) {
4938 err = nfserrno(vfs_test_lock(file, lock));
4939 nfsd_close(file);
4940 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004941 return err;
4942}
4943
4944/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 * LOCKT operation
4946 */
Al Virob37ad282006-10-19 23:28:59 -07004947__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004948nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4949 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950{
Jeff Layton21179d82012-08-21 08:03:32 -04004951 struct file_lock *file_lock = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004952 struct nfs4_lockowner *lo;
Al Virob37ad282006-10-19 23:28:59 -07004953 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004954 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004956 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 return nfserr_grace;
4958
4959 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4960 return nfserr_inval;
4961
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 nfs4_lock_state();
4963
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004964 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004965 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004966 if (status)
4967 goto out;
4968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004970 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972
Jeff Layton21179d82012-08-21 08:03:32 -04004973 file_lock = locks_alloc_lock();
4974 if (!file_lock) {
4975 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4976 status = nfserr_jukebox;
4977 goto out;
4978 }
4979 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 switch (lockt->lt_type) {
4981 case NFS4_READ_LT:
4982 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004983 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 break;
4985 case NFS4_WRITE_LT:
4986 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004987 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 break;
4989 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04004990 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 status = nfserr_inval;
4992 goto out;
4993 }
4994
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004995 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004996 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04004997 file_lock->fl_owner = (fl_owner_t)lo;
4998 file_lock->fl_pid = current->tgid;
4999 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000
Jeff Layton21179d82012-08-21 08:03:32 -04005001 file_lock->fl_start = lockt->lt_offset;
5002 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003
Jeff Layton21179d82012-08-21 08:03:32 -04005004 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005
Jeff Layton21179d82012-08-21 08:03:32 -04005006 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005007 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005008 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005009
Jeff Layton21179d82012-08-21 08:03:32 -04005010 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005012 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 }
5014out:
5015 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005016 if (file_lock)
5017 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 return status;
5019}
5020
Al Virob37ad282006-10-19 23:28:59 -07005021__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005022nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005023 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005025 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005027 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005028 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005029 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005030 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5031
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5033 (long long) locku->lu_offset,
5034 (long long) locku->lu_length);
5035
5036 if (check_lock_length(locku->lu_offset, locku->lu_length))
5037 return nfserr_inval;
5038
5039 nfs4_lock_state();
5040
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005041 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005042 &locku->lu_stateid, NFS4_LOCK_STID,
5043 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005044 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005046 filp = find_any_file(stp->st_file);
5047 if (!filp) {
5048 status = nfserr_lock_range;
5049 goto out;
5050 }
Jeff Layton21179d82012-08-21 08:03:32 -04005051 file_lock = locks_alloc_lock();
5052 if (!file_lock) {
5053 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5054 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005055 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005056 }
5057 locks_init_lock(file_lock);
5058 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005059 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005060 file_lock->fl_pid = current->tgid;
5061 file_lock->fl_file = filp;
5062 file_lock->fl_flags = FL_POSIX;
5063 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5064 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065
Jeff Layton21179d82012-08-21 08:03:32 -04005066 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5067 locku->lu_length);
5068 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069
Jeff Layton21179d82012-08-21 08:03:32 -04005070 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005071 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005072 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 goto out_nfserr;
5074 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005075 update_stateid(&stp->st_stid.sc_stateid);
5076 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005077fput:
5078 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005080 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04005081 if (!cstate->replay_owner)
5082 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005083 if (file_lock)
5084 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 return status;
5086
5087out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005088 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005089 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090}
5091
5092/*
5093 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005094 * true: locks held by lockowner
5095 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005097static bool
5098check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099{
5100 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005101 int status = false;
5102 struct file *filp = find_any_file(fp);
5103 struct inode *inode;
5104
5105 if (!filp) {
5106 /* Any valid lock stateid should have some sort of access */
5107 WARN_ON_ONCE(1);
5108 return status;
5109 }
5110
5111 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112
Jeff Layton1c8c6012013-06-21 08:58:15 -04005113 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005115 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005116 status = true;
5117 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005120 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005121 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 return status;
5123}
5124
Al Virob37ad282006-10-19 23:28:59 -07005125__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005126nfsd4_release_lockowner(struct svc_rqst *rqstp,
5127 struct nfsd4_compound_state *cstate,
5128 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129{
5130 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Laytonfd449072014-06-30 11:48:41 -04005131 struct nfs4_stateowner *sop = NULL, *tmp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005132 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005133 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 struct xdr_netobj *owner = &rlockowner->rl_owner;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005135 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07005136 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005137 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138
5139 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5140 clid->cl_boot, clid->cl_id);
5141
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 nfs4_lock_state();
5143
Jeff Layton4b24ca72014-06-30 11:48:44 -04005144 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005145 if (status)
5146 goto out;
5147
NeilBrown3e9e3db2005-06-23 22:04:20 -07005148 status = nfserr_locks_held;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05005149
Jeff Laytonfd449072014-06-30 11:48:41 -04005150 /* Find the matching lock stateowner */
5151 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
5152 if (tmp->so_is_open_owner)
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005153 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005154 if (same_owner_str(tmp, owner, clid)) {
5155 sop = tmp;
5156 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07005158 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005159
5160 /* No matching owner found, maybe a replay? Just declare victory... */
5161 if (!sop) {
5162 status = nfs_ok;
5163 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005165
5166 lo = lockowner(sop);
5167 /* see if there are still any locks associated with it */
5168 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5169 if (check_for_locks(stp->st_file, lo))
5170 goto out;
5171 }
5172
5173 status = nfs_ok;
5174 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175out:
5176 nfs4_unlock_state();
5177 return status;
5178}
5179
5180static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005181alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182{
NeilBrowna55370a2005-06-23 22:03:52 -07005183 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184}
5185
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005186bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005187nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005188{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005189 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005190
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005191 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005192 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005193}
5194
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195/*
5196 * failure => all reset bets are off, nfserr_no_grace...
5197 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005198struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005199nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200{
5201 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005202 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203
NeilBrowna55370a2005-06-23 22:03:52 -07005204 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5205 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005206 if (crp) {
5207 strhashval = clientstr_hashval(name);
5208 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005209 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005210 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005211 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005212 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005213 }
5214 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215}
5216
Jeff Layton2a4317c2012-03-21 16:42:43 -04005217void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005218nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005219{
5220 list_del(&crp->cr_strhash);
5221 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005222 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005223}
5224
5225void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005226nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227{
5228 struct nfs4_client_reclaim *crp = NULL;
5229 int i;
5230
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005232 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5233 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005235 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 }
5237 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005238 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239}
5240
5241/*
5242 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005243struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005244nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245{
5246 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 struct nfs4_client_reclaim *crp = NULL;
5248
Jeff Layton278c9312012-11-12 15:00:52 -05005249 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250
Jeff Layton278c9312012-11-12 15:00:52 -05005251 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005252 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005253 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 return crp;
5255 }
5256 }
5257 return NULL;
5258}
5259
5260/*
5261* Called from OPEN. Look for clientid in reclaim list.
5262*/
Al Virob37ad282006-10-19 23:28:59 -07005263__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005264nfs4_check_open_reclaim(clientid_t *clid,
5265 struct nfsd4_compound_state *cstate,
5266 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005268 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005269
5270 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005271 status = lookup_clientid(clid, cstate, nn);
5272 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005273 return nfserr_reclaim_bad;
5274
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005275 if (nfsd4_client_record_check(cstate->clp))
5276 return nfserr_reclaim_bad;
5277
5278 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279}
5280
Bryan Schumaker65178db2011-11-01 13:35:21 -04005281#ifdef CONFIG_NFSD_FAULT_INJECTION
5282
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005283u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5284{
J. Bruce Fields221a6872013-04-01 22:23:49 -04005285 if (mark_client_expired(clp))
5286 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005287 expire_client(clp);
5288 return 1;
5289}
5290
Bryan Schumaker184c1842012-11-29 11:40:44 -05005291u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5292{
5293 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005294 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005295 printk(KERN_INFO "NFS Client: %s\n", buf);
5296 return 1;
5297}
5298
5299static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5300 const char *type)
5301{
5302 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005303 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005304 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5305}
5306
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005307static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5308 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005309{
5310 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005311 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005312 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005313 u64 count = 0;
5314
5315 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005316 list_for_each_entry_safe(stp, st_next,
5317 &oop->oo_owner.so_stateids, st_perstateowner) {
5318 list_for_each_entry_safe(lst, lst_next,
5319 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005320 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005321 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005322 if (++count == max)
5323 return count;
5324 }
5325 }
5326 }
5327
5328 return count;
5329}
5330
5331u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5332{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005333 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005334}
5335
Bryan Schumaker184c1842012-11-29 11:40:44 -05005336u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5337{
5338 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5339 nfsd_print_count(clp, count, "locked files");
5340 return count;
5341}
5342
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005343static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5344{
5345 struct nfs4_openowner *oop, *next;
5346 u64 count = 0;
5347
5348 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5349 if (func)
5350 func(oop);
5351 if (++count == max)
5352 break;
5353 }
5354
5355 return count;
5356}
5357
5358u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5359{
5360 return nfsd_foreach_client_open(clp, max, release_openowner);
5361}
5362
Bryan Schumaker184c1842012-11-29 11:40:44 -05005363u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5364{
5365 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5366 nfsd_print_count(clp, count, "open files");
5367 return count;
5368}
5369
Bryan Schumaker269de302012-11-29 11:40:42 -05005370static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5371 struct list_head *victims)
5372{
5373 struct nfs4_delegation *dp, *next;
5374 u64 count = 0;
5375
Benny Halevycdc97502014-05-30 09:09:30 -04005376 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005377 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005378 if (victims) {
5379 /*
5380 * It's not safe to mess with delegations that have a
5381 * non-zero dl_time. They might have already been broken
5382 * and could be processed by the laundromat outside of
5383 * the state_lock. Just leave them be.
5384 */
5385 if (dp->dl_time != 0)
5386 continue;
5387
Jeff Layton42690672014-07-25 07:34:20 -04005388 unhash_delegation_locked(dp);
5389 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005390 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005391 if (++count == max)
5392 break;
5393 }
5394 return count;
5395}
5396
5397u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5398{
5399 struct nfs4_delegation *dp, *next;
5400 LIST_HEAD(victims);
5401 u64 count;
5402
Benny Halevycdc97502014-05-30 09:09:30 -04005403 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005404 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005405 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005406
Jeff Layton2d4a5322014-07-25 07:34:21 -04005407 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5408 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005409 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005410 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005411
5412 return count;
5413}
5414
5415u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5416{
Jeff Laytondff13992014-07-08 14:02:49 -04005417 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005418 LIST_HEAD(victims);
5419 u64 count;
5420
Benny Halevycdc97502014-05-30 09:09:30 -04005421 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005422 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005423 while (!list_empty(&victims)) {
5424 dp = list_first_entry(&victims, struct nfs4_delegation,
5425 dl_recall_lru);
5426 list_del_init(&dp->dl_recall_lru);
5427 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005428 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005429 }
Benny Halevycdc97502014-05-30 09:09:30 -04005430 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005431
5432 return count;
5433}
5434
Bryan Schumaker184c1842012-11-29 11:40:44 -05005435u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5436{
5437 u64 count = 0;
5438
Benny Halevycdc97502014-05-30 09:09:30 -04005439 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005440 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005441 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005442
5443 nfsd_print_count(clp, count, "delegations");
5444 return count;
5445}
5446
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005447u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005448{
5449 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005450 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005451 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005452
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005453 if (!nfsd_netns_ready(nn))
5454 return 0;
5455
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005456 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005457 count += func(clp, max - count);
5458 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005459 break;
5460 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005461
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005462 return count;
5463}
5464
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005465struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5466{
5467 struct nfs4_client *clp;
5468 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5469
5470 if (!nfsd_netns_ready(nn))
5471 return NULL;
5472
5473 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5474 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5475 return clp;
5476 }
5477 return NULL;
5478}
5479
Bryan Schumaker65178db2011-11-01 13:35:21 -04005480#endif /* CONFIG_NFSD_FAULT_INJECTION */
5481
Meelap Shahc2f1a552007-07-17 04:04:39 -07005482/*
5483 * Since the lifetime of a delegation isn't limited to that of an open, a
5484 * client may quite reasonably hang on to a delegation as long as it has
5485 * the inode cached. This becomes an obvious problem the first time a
5486 * client's inode cache approaches the size of the server's total memory.
5487 *
5488 * For now we avoid this problem by imposing a hard limit on the number
5489 * of delegations, which varies according to the server's memory size.
5490 */
5491static void
5492set_max_delegations(void)
5493{
5494 /*
5495 * Allow at most 4 delegations per megabyte of RAM. Quick
5496 * estimates suggest that in the worst case (where every delegation
5497 * is for a different inode), a delegation could take about 1.5K,
5498 * giving a worst case usage of about 6% of memory.
5499 */
5500 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5501}
5502
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005503static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005504{
5505 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5506 int i;
5507
5508 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5509 CLIENT_HASH_SIZE, GFP_KERNEL);
5510 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005511 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005512 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5513 CLIENT_HASH_SIZE, GFP_KERNEL);
5514 if (!nn->unconf_id_hashtbl)
5515 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005516 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5517 OWNER_HASH_SIZE, GFP_KERNEL);
5518 if (!nn->ownerstr_hashtbl)
5519 goto err_ownerstr;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005520 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5521 SESSION_HASH_SIZE, GFP_KERNEL);
5522 if (!nn->sessionid_hashtbl)
5523 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005524
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005525 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005526 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005527 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005528 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005529 for (i = 0; i < OWNER_HASH_SIZE; i++)
5530 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005531 for (i = 0; i < SESSION_HASH_SIZE; i++)
5532 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005533 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005534 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005535 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005536 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005537 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005538 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005539
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005540 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005541 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005542
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005543 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005544
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005545err_sessionid:
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03005546 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005547err_ownerstr:
5548 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005549err_unconf_id:
5550 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005551err:
5552 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005553}
5554
5555static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005556nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005557{
5558 int i;
5559 struct nfs4_client *clp = NULL;
5560 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5561
5562 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5563 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5564 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5565 destroy_client(clp);
5566 }
5567 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005568
Kinglong Mee2b905632014-03-26 22:09:30 +08005569 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5570 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5571 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5572 destroy_client(clp);
5573 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005574 }
5575
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005576 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005577 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005578 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005579 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005580 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005581}
5582
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005583int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005584nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005585{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005586 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005587 int ret;
5588
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005589 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005590 if (ret)
5591 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005592 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005593 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005594 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005595 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005596 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005597 nn->nfsd4_grace, net);
5598 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005599 return 0;
5600}
5601
5602/* initialization to perform when the nfsd service is started: */
5603
5604int
5605nfs4_state_start(void)
5606{
5607 int ret;
5608
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005609 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005610 if (ret)
5611 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005612 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005613 if (laundry_wq == NULL) {
5614 ret = -ENOMEM;
5615 goto out_recovery;
5616 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005617 ret = nfsd4_create_callback_queue();
5618 if (ret)
5619 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005620
Meelap Shahc2f1a552007-07-17 04:04:39 -07005621 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005622
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005623 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005624
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005625out_free_laundry:
5626 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005627out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005628 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629}
5630
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005631void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005632nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005636 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005638 cancel_delayed_work_sync(&nn->laundromat_work);
5639 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005640
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005641 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04005643 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005644 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005646 unhash_delegation_locked(dp);
5647 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 }
Benny Halevycdc97502014-05-30 09:09:30 -04005649 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 list_for_each_safe(pos, next, &reaplist) {
5651 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005652 list_del_init(&dp->dl_recall_lru);
5653 nfs4_put_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 }
5655
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005656 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005657 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005658 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659}
5660
5661void
5662nfs4_state_shutdown(void)
5663{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005664 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005665 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005667
5668static void
5669get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5670{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005671 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5672 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005673}
5674
5675static void
5676put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5677{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005678 if (cstate->minorversion) {
5679 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5680 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5681 }
5682}
5683
5684void
5685clear_current_stateid(struct nfsd4_compound_state *cstate)
5686{
5687 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005688}
5689
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005690/*
5691 * functions to set current state id
5692 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005693void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005694nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5695{
5696 put_stateid(cstate, &odp->od_stateid);
5697}
5698
5699void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005700nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5701{
5702 put_stateid(cstate, &open->op_stateid);
5703}
5704
5705void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005706nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5707{
5708 put_stateid(cstate, &close->cl_stateid);
5709}
5710
5711void
5712nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5713{
5714 put_stateid(cstate, &lock->lk_resp_stateid);
5715}
5716
5717/*
5718 * functions to consume current state id
5719 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005720
5721void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005722nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5723{
5724 get_stateid(cstate, &odp->od_stateid);
5725}
5726
5727void
5728nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5729{
5730 get_stateid(cstate, &drp->dr_stateid);
5731}
5732
5733void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005734nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5735{
5736 get_stateid(cstate, &fsp->fr_stateid);
5737}
5738
5739void
5740nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5741{
5742 get_stateid(cstate, &setattr->sa_stateid);
5743}
5744
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005745void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005746nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5747{
5748 get_stateid(cstate, &close->cl_stateid);
5749}
5750
5751void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005752nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005753{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005754 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005755}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01005756
5757void
5758nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5759{
5760 get_stateid(cstate, &read->rd_stateid);
5761}
5762
5763void
5764nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5765{
5766 get_stateid(cstate, &write->wr_stateid);
5767}