blob: 1dfc8ee85c93ff23f522c8b0391e1fc878fda9a0 [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
473 stid = kmem_cache_alloc(slab, GFP_KERNEL);
474 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;
481 stid->sc_type = 0;
482 stid->sc_stateid.si_opaque.so_id = new_id;
483 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
484 /* Will be incremented before return to client: */
485 stid->sc_stateid.si_generation = 0;
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400486 atomic_set(&stid->sc_count, 1);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500487
J. Bruce Fields996e0932011-10-17 11:14:48 -0400488 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500489 * It shouldn't be a problem to reuse an opaque stateid value.
490 * I don't think it is for 4.1. But with 4.0 I worry that, for
491 * example, a stray write retransmission could be accepted by
492 * the server when it should have been rejected. Therefore,
493 * adopt a trick from the sctp code to attempt to maximize the
494 * amount of time until an id is reused, by ensuring they always
495 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400496 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500497 return stid;
498out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800499 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500500 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400501}
502
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400503static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
504{
505 return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
506}
507
NeilBrown6282cd52014-06-04 17:39:26 +1000508/*
509 * When we recall a delegation, we should be careful not to hand it
510 * out again straight away.
511 * To ensure this we keep a pair of bloom filters ('new' and 'old')
512 * in which the filehandles of recalled delegations are "stored".
513 * If a filehandle appear in either filter, a delegation is blocked.
514 * When a delegation is recalled, the filehandle is stored in the "new"
515 * filter.
516 * Every 30 seconds we swap the filters and clear the "new" one,
517 * unless both are empty of course.
518 *
519 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
520 * low 3 bytes as hash-table indices.
521 *
522 * 'state_lock', which is always held when block_delegations() is called,
523 * is used to manage concurrent access. Testing does not need the lock
524 * except when swapping the two filters.
525 */
526static struct bloom_pair {
527 int entries, old_entries;
528 time_t swap_time;
529 int new; /* index into 'set' */
530 DECLARE_BITMAP(set[2], 256);
531} blocked_delegations;
532
533static int delegation_blocked(struct knfsd_fh *fh)
534{
535 u32 hash;
536 struct bloom_pair *bd = &blocked_delegations;
537
538 if (bd->entries == 0)
539 return 0;
540 if (seconds_since_boot() - bd->swap_time > 30) {
541 spin_lock(&state_lock);
542 if (seconds_since_boot() - bd->swap_time > 30) {
543 bd->entries -= bd->old_entries;
544 bd->old_entries = bd->entries;
545 memset(bd->set[bd->new], 0,
546 sizeof(bd->set[0]));
547 bd->new = 1-bd->new;
548 bd->swap_time = seconds_since_boot();
549 }
550 spin_unlock(&state_lock);
551 }
552 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
553 if (test_bit(hash&255, bd->set[0]) &&
554 test_bit((hash>>8)&255, bd->set[0]) &&
555 test_bit((hash>>16)&255, bd->set[0]))
556 return 1;
557
558 if (test_bit(hash&255, bd->set[1]) &&
559 test_bit((hash>>8)&255, bd->set[1]) &&
560 test_bit((hash>>16)&255, bd->set[1]))
561 return 1;
562
563 return 0;
564}
565
566static void block_delegations(struct knfsd_fh *fh)
567{
568 u32 hash;
569 struct bloom_pair *bd = &blocked_delegations;
570
Jeff Layton02e12152014-07-16 10:31:57 -0400571 lockdep_assert_held(&state_lock);
572
NeilBrown6282cd52014-06-04 17:39:26 +1000573 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
574
575 __set_bit(hash&255, bd->set[bd->new]);
576 __set_bit((hash>>8)&255, bd->set[bd->new]);
577 __set_bit((hash>>16)&255, bd->set[bd->new]);
578 if (bd->entries == 0)
579 bd->swap_time = seconds_since_boot();
580 bd->entries += 1;
581}
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583static struct nfs4_delegation *
J. Bruce Fields99c41512013-05-21 16:21:25 -0400584alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 dprintk("NFSD alloc_init_deleg\n");
Meelap Shahc2f1a552007-07-17 04:04:39 -0700589 if (num_delegations > max_delegations)
NeilBrownef0f3392006-04-10 22:55:41 -0700590 return NULL;
NeilBrown6282cd52014-06-04 17:39:26 +1000591 if (delegation_blocked(&current_fh->fh_handle))
592 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400593 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700594 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return dp;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400596 /*
597 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400598 * meaning of seqid 0 isn't meaningful, really, but let's avoid
599 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400600 */
601 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownef0f3392006-04-10 22:55:41 -0700602 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700603 INIT_LIST_HEAD(&dp->dl_perfile);
604 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -0400606 dp->dl_file = NULL;
J. Bruce Fields99c41512013-05-21 16:21:25 -0400607 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
J. Bruce Fields6c02eaa2009-02-02 17:30:51 -0500608 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 dp->dl_time = 0;
Jeff Layton02e12152014-07-16 10:31:57 -0400610 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return dp;
612}
613
J. Bruce Fields68a33962013-03-21 11:21:50 -0400614static void remove_stid(struct nfs4_stid *s)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500615{
616 struct idr *stateids = &s->sc_client->cl_stateids;
617
618 idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500619}
620
Benny Halevy9857df82013-10-14 13:44:52 +0300621static void nfs4_free_stid(struct kmem_cache *slab, struct nfs4_stid *s)
622{
623 kmem_cache_free(slab, s);
624}
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626void
627nfs4_put_delegation(struct nfs4_delegation *dp)
628{
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400629 if (atomic_dec_and_test(&dp->dl_stid.sc_count)) {
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400630 remove_stid(&dp->dl_stid);
Benny Halevy9857df82013-10-14 13:44:52 +0300631 nfs4_free_stid(deleg_slab, &dp->dl_stid);
NeilBrownef0f3392006-04-10 22:55:41 -0700632 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
634}
635
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500636static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Jeff Layton417c6622014-07-21 09:34:57 -0400638 lockdep_assert_held(&state_lock);
639
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -0500640 if (!fp->fi_lease)
641 return;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500642 if (atomic_dec_and_test(&fp->fi_delegees)) {
643 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
644 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400645 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500646 fp->fi_deleg_file = NULL;
647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400650static void unhash_stid(struct nfs4_stid *s)
651{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500652 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400653}
654
Benny Halevy931ee562014-05-30 09:09:27 -0400655static void
656hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
657{
Benny Halevycdc97502014-05-30 09:09:30 -0400658 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400659 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400660
Benny Halevy3fb87d12014-05-30 09:09:31 -0400661 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400662 list_add(&dp->dl_perfile, &fp->fi_delegations);
663 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
664}
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666/* Called under the state lock. */
667static void
668unhash_delegation(struct nfs4_delegation *dp)
669{
Jeff Layton02e12152014-07-16 10:31:57 -0400670 struct nfs4_file *fp = dp->dl_file;
671
Benny Halevycdc97502014-05-30 09:09:30 -0400672 spin_lock(&state_lock);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400673 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400674 /* Ensure that deleg break won't try to requeue it */
675 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400676 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400677 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400679 list_del_init(&dp->dl_perfile);
680 spin_unlock(&fp->fi_lock);
Jeff Layton02e12152014-07-16 10:31:57 -0400681 if (fp) {
682 nfs4_put_deleg_lease(fp);
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -0500683 dp->dl_file = NULL;
684 }
Jeff Layton417c6622014-07-21 09:34:57 -0400685 spin_unlock(&state_lock);
686 if (fp)
687 put_nfs4_file(fp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400688}
689
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400690static void destroy_revoked_delegation(struct nfs4_delegation *dp)
691{
692 list_del_init(&dp->dl_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 nfs4_put_delegation(dp);
694}
695
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400696static void destroy_delegation(struct nfs4_delegation *dp)
697{
698 unhash_delegation(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400699 nfs4_put_delegation(dp);
700}
701
702static void revoke_delegation(struct nfs4_delegation *dp)
703{
704 struct nfs4_client *clp = dp->dl_stid.sc_client;
705
706 if (clp->cl_minorversion == 0)
707 destroy_delegation(dp);
708 else {
709 unhash_delegation(dp);
710 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
711 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
712 }
713}
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715/*
716 * SETCLIENTID state
717 */
718
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400719static unsigned int clientid_hashval(u32 id)
720{
721 return id & CLIENT_HASH_MASK;
722}
723
724static unsigned int clientstr_hashval(const char *name)
725{
726 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
727}
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400730 * We store the NONE, READ, WRITE, and BOTH bits separately in the
731 * st_{access,deny}_bmap field of the stateid, in order to track not
732 * only what share bits are currently in force, but also what
733 * combinations of share bits previous opens have used. This allows us
734 * to enforce the recommendation of rfc 3530 14.2.19 that the server
735 * return an error if the client attempt to downgrade to a combination
736 * of share bits not explicable by closing some of its previous opens.
737 *
738 * XXX: This enforcement is actually incomplete, since we don't keep
739 * track of access/deny bit combinations; so, e.g., we allow:
740 *
741 * OPEN allow read, deny write
742 * OPEN allow both, deny none
743 * DOWNGRADE allow read, deny none
744 *
745 * which we should reject.
746 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400747static unsigned int
748bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400749 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400750 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400751
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400752 for (i = 1; i < 4; i++) {
753 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400754 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400755 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400756 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400757}
758
Jeff Layton82c5ff12012-05-11 09:45:13 -0400759/* set share access for a given stateid */
760static inline void
761set_access(u32 access, struct nfs4_ol_stateid *stp)
762{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400763 unsigned char mask = 1 << access;
764
765 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
766 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400767}
768
769/* clear share access for a given stateid */
770static inline void
771clear_access(u32 access, struct nfs4_ol_stateid *stp)
772{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400773 unsigned char mask = 1 << access;
774
775 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
776 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400777}
778
779/* test whether a given stateid has access */
780static inline bool
781test_access(u32 access, struct nfs4_ol_stateid *stp)
782{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400783 unsigned char mask = 1 << access;
784
785 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400786}
787
Jeff Laytonce0fc432012-05-11 09:45:14 -0400788/* set share deny for a given stateid */
789static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400790set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400791{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400792 unsigned char mask = 1 << deny;
793
794 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
795 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400796}
797
798/* clear share deny for a given stateid */
799static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400800clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400801{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400802 unsigned char mask = 1 << deny;
803
804 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
805 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400806}
807
808/* test whether a given stateid is denying specific access */
809static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400810test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400811{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400812 unsigned char mask = 1 << deny;
813
814 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400815}
816
817static int nfs4_access_to_omode(u32 access)
818{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400819 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400820 case NFS4_SHARE_ACCESS_READ:
821 return O_RDONLY;
822 case NFS4_SHARE_ACCESS_WRITE:
823 return O_WRONLY;
824 case NFS4_SHARE_ACCESS_BOTH:
825 return O_RDWR;
826 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500827 WARN_ON_ONCE(1);
828 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400829}
830
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400831/*
832 * A stateid that had a deny mode associated with it is being released
833 * or downgraded. Recalculate the deny mode on the file.
834 */
835static void
836recalculate_deny_mode(struct nfs4_file *fp)
837{
838 struct nfs4_ol_stateid *stp;
839
840 spin_lock(&fp->fi_lock);
841 fp->fi_share_deny = 0;
842 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
843 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
844 spin_unlock(&fp->fi_lock);
845}
846
847static void
848reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
849{
850 int i;
851 bool change = false;
852
853 for (i = 1; i < 4; i++) {
854 if ((i & deny) != i) {
855 change = true;
856 clear_deny(i, stp);
857 }
858 }
859
860 /* Recalculate per-file deny mode if there was a change */
861 if (change)
862 recalculate_deny_mode(stp->st_file);
863}
864
Jeff Layton82c5ff12012-05-11 09:45:13 -0400865/* release all access and file references for a given stateid */
866static void
867release_all_access(struct nfs4_ol_stateid *stp)
868{
869 int i;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400870 struct nfs4_file *fp = stp->st_file;
871
872 if (fp && stp->st_deny_bmap != 0)
873 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400874
875 for (i = 1; i < 4; i++) {
876 if (test_access(i, stp))
Jeff Layton12659652014-07-10 14:07:28 -0400877 nfs4_file_put_access(stp->st_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400878 clear_access(i, stp);
879 }
880}
881
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400882static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500883{
Trond Myklebust1d31a252014-07-10 14:07:25 -0400884 struct nfs4_file *fp = stp->st_file;
885
886 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500887 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400888 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500889 list_del(&stp->st_perstateowner);
890}
891
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400892static void close_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500893{
Jeff Layton82c5ff12012-05-11 09:45:13 -0400894 release_all_access(stp);
OGAWA Hirofumia96e5b92011-04-18 11:48:55 -0400895 put_nfs4_file(stp->st_file);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400896 stp->st_file = NULL;
897}
898
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400899static void free_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400900{
J. Bruce Fields68a33962013-03-21 11:21:50 -0400901 remove_stid(&stp->st_stid);
Benny Halevy9857df82013-10-14 13:44:52 +0300902 nfs4_free_stid(stateid_slab, &stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500903}
904
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400905static void __release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500906{
907 struct file *file;
908
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400909 list_del(&stp->st_locks);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500910 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400911 unhash_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500912 file = find_any_file(stp->st_file);
Trond Myklebuste20fcf12014-07-10 14:07:27 -0400913 if (file)
914 filp_close(file, (fl_owner_t)lockowner(stp->st_stateowner));
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400915 close_generic_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500916 free_generic_stateid(stp);
917}
918
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400919static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500920{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400921 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500922
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400923 list_del(&lo->lo_owner.so_strhash);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400924 while (!list_empty(&lo->lo_owner.so_stateids)) {
925 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400926 struct nfs4_ol_stateid, st_perstateowner);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400927 __release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500928 }
929}
930
Trond Myklebust50cc6232014-04-18 14:44:03 -0400931static void nfs4_free_lockowner(struct nfs4_lockowner *lo)
932{
933 kfree(lo->lo_owner.so_owner.data);
934 kmem_cache_free(lockowner_slab, lo);
935}
936
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400937static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500938{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400939 unhash_lockowner(lo);
940 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500941}
942
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400943static void release_lockowner_if_empty(struct nfs4_lockowner *lo)
944{
945 if (list_empty(&lo->lo_owner.so_stateids))
946 release_lockowner(lo);
947}
948
949static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500950{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400951 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500952
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400953 lo = lockowner(stp->st_stateowner);
954 __release_lock_stateid(stp);
955 release_lockowner_if_empty(lo);
956}
957
958static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
959{
960 struct nfs4_ol_stateid *stp;
961
962 while (!list_empty(&open_stp->st_locks)) {
963 stp = list_entry(open_stp->st_locks.next,
964 struct nfs4_ol_stateid, st_locks);
965 release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500966 }
967}
968
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400969static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500970{
971 unhash_generic_stateid(stp);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400972 release_open_stateid_locks(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400973 close_generic_stateid(stp);
974}
975
976static void release_open_stateid(struct nfs4_ol_stateid *stp)
977{
978 unhash_open_stateid(stp);
J. Bruce Fields22839632009-01-11 14:27:17 -0500979 free_generic_stateid(stp);
980}
981
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400982static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500983{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400984 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500985
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400986 list_del(&oo->oo_owner.so_strhash);
987 list_del(&oo->oo_perclient);
988 while (!list_empty(&oo->oo_owner.so_stateids)) {
989 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400990 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500991 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500992 }
993}
994
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400995static void release_last_closed_stateid(struct nfs4_openowner *oo)
996{
997 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
998
999 if (s) {
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001000 free_generic_stateid(s);
1001 oo->oo_last_closed_stid = NULL;
1002 }
1003}
1004
Trond Myklebust50cc6232014-04-18 14:44:03 -04001005static void nfs4_free_openowner(struct nfs4_openowner *oo)
1006{
1007 kfree(oo->oo_owner.so_owner.data);
1008 kmem_cache_free(openowner_slab, oo);
1009}
1010
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001011static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001012{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001013 unhash_openowner(oo);
1014 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001015 release_last_closed_stateid(oo);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001016 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001017}
1018
Marc Eshel5282fd72009-04-03 08:27:52 +03001019static inline int
1020hash_sessionid(struct nfs4_sessionid *sessionid)
1021{
1022 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1023
1024 return sid->sequence % SESSION_HASH_SIZE;
1025}
1026
Trond Myklebust8f199b82012-03-20 15:11:17 -04001027#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001028static inline void
1029dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1030{
1031 u32 *ptr = (u32 *)(&sessionid->data[0]);
1032 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1033}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001034#else
1035static inline void
1036dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1037{
1038}
1039#endif
1040
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001041/*
1042 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1043 * won't be used for replay.
1044 */
1045void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1046{
1047 struct nfs4_stateowner *so = cstate->replay_owner;
1048
1049 if (nfserr == nfserr_replay_me)
1050 return;
1051
1052 if (!seqid_mutating_err(ntohl(nfserr))) {
1053 cstate->replay_owner = NULL;
1054 return;
1055 }
1056 if (!so)
1057 return;
1058 if (so->so_is_open_owner)
1059 release_last_closed_stateid(openowner(so));
1060 so->so_seqid++;
1061 return;
1062}
Marc Eshel5282fd72009-04-03 08:27:52 +03001063
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001064static void
1065gen_sessionid(struct nfsd4_session *ses)
1066{
1067 struct nfs4_client *clp = ses->se_client;
1068 struct nfsd4_sessionid *sid;
1069
1070 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1071 sid->clientid = clp->cl_clientid;
1072 sid->sequence = current_sessionid++;
1073 sid->reserved = 0;
1074}
1075
1076/*
Andy Adamsona6496372009-08-28 08:45:01 -04001077 * The protocol defines ca_maxresponssize_cached to include the size of
1078 * the rpc header, but all we need to cache is the data starting after
1079 * the end of the initial SEQUENCE operation--the rest we regenerate
1080 * each time. Therefore we can advertise a ca_maxresponssize_cached
1081 * value that is the number of bytes in our cache plus a few additional
1082 * bytes. In order to stay on the safe side, and not promise more than
1083 * we can cache, those additional bytes must be the minimum possible: 24
1084 * bytes of rpc header (xid through accept state, with AUTH_NULL
1085 * verifier), 12 for the compound header (with zero-length tag), and 44
1086 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001087 */
Andy Adamsona6496372009-08-28 08:45:01 -04001088#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1089
Andy Adamson557ce262009-08-28 08:45:04 -04001090static void
1091free_session_slots(struct nfsd4_session *ses)
1092{
1093 int i;
1094
1095 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1096 kfree(ses->se_slots[i]);
1097}
1098
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001099/*
1100 * We don't actually need to cache the rpc and session headers, so we
1101 * can allocate a little less for each slot:
1102 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001103static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001104{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001105 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001106
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001107 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1108 size = 0;
1109 else
1110 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1111 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001112}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001113
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001114/*
1115 * XXX: If we run out of reserved DRC memory we could (up to a point)
1116 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001117 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001118 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001119static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001120{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001121 u32 slotsize = slot_bytes(ca);
1122 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001123 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001124
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001125 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001126 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1127 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001128 num = min_t(int, num, avail / slotsize);
1129 nfsd_drc_mem_used += num * slotsize;
1130 spin_unlock(&nfsd_drc_lock);
1131
1132 return num;
1133}
1134
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001135static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001136{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001137 int slotsize = slot_bytes(ca);
1138
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001139 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001140 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001141 spin_unlock(&nfsd_drc_lock);
1142}
1143
Kinglong Mee60810e52014-01-01 00:35:47 +08001144static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1145 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001146{
Kinglong Mee60810e52014-01-01 00:35:47 +08001147 int numslots = fattrs->maxreqs;
1148 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001149 struct nfsd4_session *new;
1150 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001151
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001152 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001153 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1154 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001155
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001156 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001157 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001158 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001159 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001160 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001161 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001162 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001163 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001164 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001165
1166 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1167 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1168
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001169 return new;
1170out_free:
1171 while (i--)
1172 kfree(new->se_slots[i]);
1173 kfree(new);
1174 return NULL;
1175}
1176
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001177static void free_conn(struct nfsd4_conn *c)
1178{
1179 svc_xprt_put(c->cn_xprt);
1180 kfree(c);
1181}
1182
1183static void nfsd4_conn_lost(struct svc_xpt_user *u)
1184{
1185 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1186 struct nfs4_client *clp = c->cn_session->se_client;
1187
1188 spin_lock(&clp->cl_lock);
1189 if (!list_empty(&c->cn_persession)) {
1190 list_del(&c->cn_persession);
1191 free_conn(c);
1192 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001193 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001194 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001195}
1196
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001197static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001198{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001199 struct nfsd4_conn *conn;
1200
1201 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1202 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001203 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001204 svc_xprt_get(rqstp->rq_xprt);
1205 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001206 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001207 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1208 return conn;
1209}
1210
J. Bruce Fields328ead22010-09-29 16:11:06 -04001211static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1212{
1213 conn->cn_session = ses;
1214 list_add(&conn->cn_persession, &ses->se_conns);
1215}
1216
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001217static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1218{
1219 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001220
1221 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001222 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001223 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001224}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001225
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001226static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001227{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001228 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001229 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001230}
1231
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001232static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001233{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001234 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001235
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001236 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001237 ret = nfsd4_register_conn(conn);
1238 if (ret)
1239 /* oops; xprt is already down: */
1240 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001241 /* We may have gained or lost a callback channel: */
1242 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001243}
1244
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001245static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001246{
1247 u32 dir = NFS4_CDFC4_FORE;
1248
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001249 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001250 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001251 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001252}
1253
1254/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001255static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001256{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001257 struct nfs4_client *clp = s->se_client;
1258 struct nfsd4_conn *c;
1259
1260 spin_lock(&clp->cl_lock);
1261 while (!list_empty(&s->se_conns)) {
1262 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1263 list_del_init(&c->cn_persession);
1264 spin_unlock(&clp->cl_lock);
1265
1266 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1267 free_conn(c);
1268
1269 spin_lock(&clp->cl_lock);
1270 }
1271 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001272}
1273
J. Bruce Fields1377b692012-09-11 21:42:40 -04001274static void __free_session(struct nfsd4_session *ses)
1275{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001276 free_session_slots(ses);
1277 kfree(ses);
1278}
1279
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001280static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001281{
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001282 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1283
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001284 lockdep_assert_held(&nn->client_lock);
1285 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001286 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001287 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001288}
Andy Adamson557ce262009-08-28 08:45:04 -04001289
Fengguang Wu135ae822012-11-10 07:20:25 -05001290static 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 -04001291{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001292 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001293 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001294
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001295 new->se_client = clp;
1296 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001297
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001298 INIT_LIST_HEAD(&new->se_conns);
1299
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001300 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001301 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001302 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001303 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001304 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001305 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001306 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001307 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001308 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001309 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001310 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001311 spin_unlock(&nn->client_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001312
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001313 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001314 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001315 /*
1316 * This is a little silly; with sessions there's no real
1317 * use for the callback address. Use the peer address
1318 * as a reasonable default for now, but consider fixing
1319 * the rpc client not to require an address in the
1320 * future:
1321 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001322 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1323 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001324 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001325}
1326
Benny Halevy9089f1b2010-05-12 00:12:26 +03001327/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001328static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001329__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001330{
1331 struct nfsd4_session *elem;
1332 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001333 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001334
1335 dump_sessionid(__func__, sessionid);
1336 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001337 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001338 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001339 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1340 NFS4_MAX_SESSIONID_LEN)) {
1341 return elem;
1342 }
1343 }
1344
1345 dprintk("%s: session not found\n", __func__);
1346 return NULL;
1347}
1348
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001349static struct nfsd4_session *
1350find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1351 __be32 *ret)
1352{
1353 struct nfsd4_session *session;
1354 __be32 status = nfserr_badsession;
1355
1356 session = __find_in_sessionid_hashtbl(sessionid, net);
1357 if (!session)
1358 goto out;
1359 status = nfsd4_get_session_locked(session);
1360 if (status)
1361 session = NULL;
1362out:
1363 *ret = status;
1364 return session;
1365}
1366
Benny Halevy9089f1b2010-05-12 00:12:26 +03001367/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001368static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001369unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001370{
1371 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001372 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001373 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001374 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001375}
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1378static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001379STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001381 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001383 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001384 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return 1;
1386}
1387
1388/*
1389 * XXX Should we use a slab cache ?
1390 * This type of memory management is somewhat inefficient, but we use it
1391 * anyway since SETCLIENTID is not a common operation.
1392 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001393static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
1395 struct nfs4_client *clp;
1396
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001397 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1398 if (clp == NULL)
1399 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001400 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001401 if (clp->cl_name.data == NULL) {
1402 kfree(clp);
1403 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001405 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001406 INIT_LIST_HEAD(&clp->cl_sessions);
1407 idr_init(&clp->cl_stateids);
1408 atomic_set(&clp->cl_refcount, 0);
1409 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1410 INIT_LIST_HEAD(&clp->cl_idhash);
1411 INIT_LIST_HEAD(&clp->cl_openowners);
1412 INIT_LIST_HEAD(&clp->cl_delegations);
1413 INIT_LIST_HEAD(&clp->cl_lru);
1414 INIT_LIST_HEAD(&clp->cl_callbacks);
1415 INIT_LIST_HEAD(&clp->cl_revoked);
1416 spin_lock_init(&clp->cl_lock);
1417 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return clp;
1419}
1420
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001421static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422free_client(struct nfs4_client *clp)
1423{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001424 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001425
1426 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001427 while (!list_empty(&clp->cl_sessions)) {
1428 struct nfsd4_session *ses;
1429 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1430 se_perclnt);
1431 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001432 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1433 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001434 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001435 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001436 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001438 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 kfree(clp);
1440}
1441
Benny Halevy84d38ac2010-05-12 00:13:16 +03001442/* must be called under the client_lock */
1443static inline void
1444unhash_client_locked(struct nfs4_client *clp)
1445{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001446 struct nfsd4_session *ses;
1447
Benny Halevy84d38ac2010-05-12 00:13:16 +03001448 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001449 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001450 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1451 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001452 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001453}
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001456destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001458 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001461 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001464 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001465 while (!list_empty(&clp->cl_delegations)) {
1466 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
NeilBrownea1da632005-06-23 22:04:17 -07001467 list_del_init(&dp->dl_perclnt);
Jeff Laytondff13992014-07-08 14:02:49 -04001468 /* Ensure that deleg break won't try to requeue it */
1469 ++dp->dl_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 list_move(&dp->dl_recall_lru, &reaplist);
1471 }
Benny Halevycdc97502014-05-30 09:09:30 -04001472 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 while (!list_empty(&reaplist)) {
1474 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001475 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 }
Benny Halevy956c4fe2013-10-29 11:39:12 +02001477 list_splice_init(&clp->cl_revoked, &reaplist);
1478 while (!list_empty(&reaplist)) {
1479 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1480 destroy_revoked_delegation(dp);
1481 }
NeilBrownea1da632005-06-23 22:04:17 -07001482 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001483 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1484 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001486 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001487 if (clp->cl_cb_conn.cb_xprt)
1488 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001489 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001490 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001491 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001492 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001493 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001494 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001495 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001496 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1497 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001498 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001501static void expire_client(struct nfs4_client *clp)
1502{
1503 nfsd4_client_record_remove(clp);
1504 destroy_client(clp);
1505}
1506
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001507static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1508{
1509 memcpy(target->cl_verifier.data, source->data,
1510 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
1512
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001513static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1514{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1516 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1517}
1518
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001519static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001520{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001521 if (source->cr_principal) {
1522 target->cr_principal =
1523 kstrdup(source->cr_principal, GFP_KERNEL);
1524 if (target->cr_principal == NULL)
1525 return -ENOMEM;
1526 } else
1527 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001528 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 target->cr_uid = source->cr_uid;
1530 target->cr_gid = source->cr_gid;
1531 target->cr_group_info = source->cr_group_info;
1532 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001533 target->cr_gss_mech = source->cr_gss_mech;
1534 if (source->cr_gss_mech)
1535 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001536 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001539static long long
1540compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1541{
1542 long long res;
1543
1544 res = o1->len - o2->len;
1545 if (res)
1546 return res;
1547 return (long long)memcmp(o1->data, o2->data, o1->len);
1548}
1549
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001550static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001551{
NeilBrowna55370a2005-06-23 22:03:52 -07001552 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
1554
1555static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001556same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1557{
1558 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
1561static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001562same_clid(clientid_t *cl1, clientid_t *cl2)
1563{
1564 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001567static bool groups_equal(struct group_info *g1, struct group_info *g2)
1568{
1569 int i;
1570
1571 if (g1->ngroups != g2->ngroups)
1572 return false;
1573 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001574 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001575 return false;
1576 return true;
1577}
1578
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001579/*
1580 * RFC 3530 language requires clid_inuse be returned when the
1581 * "principal" associated with a requests differs from that previously
1582 * used. We use uid, gid's, and gss principal string as our best
1583 * approximation. We also don't want to allow non-gss use of a client
1584 * established using gss: in theory cr_principal should catch that
1585 * change, but in practice cr_principal can be null even in the gss case
1586 * since gssd doesn't always pass down a principal string.
1587 */
1588static bool is_gss_cred(struct svc_cred *cr)
1589{
1590 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1591 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1592}
1593
1594
Vivek Trivedi5559b502012-07-24 21:18:20 +05301595static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001596same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1597{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001598 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001599 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1600 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001601 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1602 return false;
1603 if (cr1->cr_principal == cr2->cr_principal)
1604 return true;
1605 if (!cr1->cr_principal || !cr2->cr_principal)
1606 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301607 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609
J. Bruce Fields57266a62013-04-13 14:27:29 -04001610static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1611{
1612 struct svc_cred *cr = &rqstp->rq_cred;
1613 u32 service;
1614
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001615 if (!cr->cr_gss_mech)
1616 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001617 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1618 return service == RPC_GSS_SVC_INTEGRITY ||
1619 service == RPC_GSS_SVC_PRIVACY;
1620}
1621
1622static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1623{
1624 struct svc_cred *cr = &rqstp->rq_cred;
1625
1626 if (!cl->cl_mach_cred)
1627 return true;
1628 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1629 return false;
1630 if (!svc_rqst_integrity_protected(rqstp))
1631 return false;
1632 if (!cr->cr_principal)
1633 return false;
1634 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1635}
1636
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001637static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001638{
1639 static u32 current_clientid = 1;
1640
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001641 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 clp->cl_clientid.cl_id = current_clientid++;
1643}
1644
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001645static void gen_confirm(struct nfs4_client *clp)
1646{
Chuck Leverab4684d2012-03-02 17:13:50 -05001647 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001648 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Jeff Laytonf4199922014-06-17 07:44:11 -04001650 /*
1651 * This is opaque to client, so no need to byte-swap. Use
1652 * __force to keep sparse happy
1653 */
1654 verf[0] = (__force __be32)get_seconds();
1655 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001656 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001659static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001660{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001661 struct nfs4_stid *ret;
1662
1663 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1664 if (!ret || !ret->sc_type)
1665 return NULL;
1666 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001667}
1668
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001669static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001670{
1671 struct nfs4_stid *s;
1672
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001673 s = find_stateid(cl, t);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001674 if (!s)
1675 return NULL;
1676 if (typemask & s->sc_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001677 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001678 return NULL;
1679}
1680
Jeff Layton2216d442012-11-12 15:00:57 -05001681static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001682 struct svc_rqst *rqstp, nfs4_verifier *verf)
1683{
1684 struct nfs4_client *clp;
1685 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001686 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001687 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001688 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001689
1690 clp = alloc_client(name);
1691 if (clp == NULL)
1692 return NULL;
1693
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001694 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1695 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001696 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001697 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001698 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001699 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001700 }
Jeff Layton02e12152014-07-16 10:31:57 -04001701 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001702 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001703 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001704 copy_verf(clp, verf);
1705 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001706 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001707 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001708 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001709 return clp;
1710}
1711
NeilBrownfd39ca92005-06-23 22:04:03 -07001712static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001713add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1714{
1715 struct rb_node **new = &(root->rb_node), *parent = NULL;
1716 struct nfs4_client *clp;
1717
1718 while (*new) {
1719 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1720 parent = *new;
1721
1722 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1723 new = &((*new)->rb_left);
1724 else
1725 new = &((*new)->rb_right);
1726 }
1727
1728 rb_link_node(&new_clp->cl_namenode, parent, new);
1729 rb_insert_color(&new_clp->cl_namenode, root);
1730}
1731
1732static struct nfs4_client *
1733find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1734{
1735 long long cmp;
1736 struct rb_node *node = root->rb_node;
1737 struct nfs4_client *clp;
1738
1739 while (node) {
1740 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1741 cmp = compare_blob(&clp->cl_name, name);
1742 if (cmp > 0)
1743 node = node->rb_left;
1744 else if (cmp < 0)
1745 node = node->rb_right;
1746 else
1747 return clp;
1748 }
1749 return NULL;
1750}
1751
1752static void
1753add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
1755 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001756 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001758 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001759 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001761 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001762 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}
1764
NeilBrownfd39ca92005-06-23 22:04:03 -07001765static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766move_to_confirmed(struct nfs4_client *clp)
1767{
1768 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001769 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001772 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001773 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001774 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001775 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 renew_client(clp);
1777}
1778
1779static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001780find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
1782 struct nfs4_client *clp;
1783 unsigned int idhashval = clientid_hashval(clid->cl_id);
1784
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001785 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001786 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001787 if ((bool)clp->cl_minorversion != sessions)
1788 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001789 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
1793 return NULL;
1794}
1795
1796static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001797find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1798{
1799 struct list_head *tbl = nn->conf_id_hashtbl;
1800
1801 return find_client_in_id_table(tbl, clid, sessions);
1802}
1803
1804static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001805find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001807 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001809 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810}
1811
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001812static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001813{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001814 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001815}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001816
NeilBrown28ce6052005-06-23 22:03:56 -07001817static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001818find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001819{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001820 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001821}
1822
1823static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001824find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001825{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001826 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001827}
1828
NeilBrownfd39ca92005-06-23 22:04:03 -07001829static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001830gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001832 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001833 struct sockaddr *sa = svc_addr(rqstp);
1834 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001835 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Jeff Layton7077ecb2009-08-14 12:57:58 -04001837 /* Currently, we only support tcp and tcp6 for the callback channel */
1838 if (se->se_callback_netid_len == 3 &&
1839 !memcmp(se->se_callback_netid_val, "tcp", 3))
1840 expected_family = AF_INET;
1841 else if (se->se_callback_netid_len == 4 &&
1842 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1843 expected_family = AF_INET6;
1844 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 goto out_err;
1846
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001847 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001848 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001849 (struct sockaddr *)&conn->cb_addr,
1850 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001851
J. Bruce Fields07263f12010-05-31 19:09:40 -04001852 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001854
J. Bruce Fields07263f12010-05-31 19:09:40 -04001855 if (conn->cb_addr.ss_family == AF_INET6)
1856 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001857
J. Bruce Fields07263f12010-05-31 19:09:40 -04001858 conn->cb_prog = se->se_callback_prog;
1859 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001860 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return;
1862out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001863 conn->cb_addr.ss_family = AF_UNSPEC;
1864 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001865 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 "will not receive delegations\n",
1867 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 return;
1870}
1871
Andy Adamson074fe892009-04-03 08:28:15 +03001872/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04001873 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001874 */
Trond Myklebustb6076642014-06-30 11:48:35 -04001875static void
Andy Adamson074fe892009-04-03 08:28:15 +03001876nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1877{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001878 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04001879 struct nfsd4_slot *slot = resp->cstate.slot;
1880 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001881
Andy Adamson557ce262009-08-28 08:45:04 -04001882 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001883
Andy Adamson557ce262009-08-28 08:45:04 -04001884 slot->sl_opcnt = resp->opcnt;
1885 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001886
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001887 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001888 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001889 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001890 return;
1891 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001892 base = resp->cstate.data_offset;
1893 slot->sl_datalen = buf->len - base;
1894 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04001895 WARN("%s: sessions DRC could not cache compound\n", __func__);
1896 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001897}
1898
1899/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001900 * Encode the replay sequence operation from the slot values.
1901 * If cachethis is FALSE encode the uncached rep error on the next
1902 * operation which sets resp->p and increments resp->opcnt for
1903 * nfs4svc_encode_compoundres.
1904 *
Andy Adamson074fe892009-04-03 08:28:15 +03001905 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001906static __be32
1907nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1908 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001909{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001910 struct nfsd4_op *op;
1911 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001912
Andy Adamsonabfabf82009-07-23 19:02:18 -04001913 /* Encode the replayed sequence operation */
1914 op = &args->ops[resp->opcnt - 1];
1915 nfsd4_encode_operation(resp, op);
1916
1917 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001918 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001919 op = &args->ops[resp->opcnt++];
1920 op->status = nfserr_retry_uncached_rep;
1921 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001922 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001923 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001924}
1925
1926/*
Andy Adamson557ce262009-08-28 08:45:04 -04001927 * The sequence operation is not cached because we can use the slot and
1928 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001929 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04001930static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001931nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1932 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001933{
Andy Adamson557ce262009-08-28 08:45:04 -04001934 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001935 struct xdr_stream *xdr = &resp->xdr;
1936 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03001937 __be32 status;
1938
Andy Adamson557ce262009-08-28 08:45:04 -04001939 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001940
Andy Adamsonabfabf82009-07-23 19:02:18 -04001941 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04001942 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04001943 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001944
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001945 p = xdr_reserve_space(xdr, slot->sl_datalen);
1946 if (!p) {
1947 WARN_ON_ONCE(1);
1948 return nfserr_serverfault;
1949 }
1950 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1951 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03001952
Andy Adamson557ce262009-08-28 08:45:04 -04001953 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001954 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001955}
1956
Andy Adamson0733d212009-04-03 08:28:01 +03001957/*
1958 * Set the exchange_id flags returned by the server.
1959 */
1960static void
1961nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1962{
1963 /* pNFS is not supported */
1964 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1965
1966 /* Referrals are supported, Migration is not. */
1967 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1968
1969 /* set the wire flags to return to client. */
1970 clid->flags = new->cl_exchange_flags;
1971}
1972
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001973static bool client_has_state(struct nfs4_client *clp)
1974{
1975 /*
1976 * Note clp->cl_openowners check isn't quite right: there's no
1977 * need to count owners without stateid's.
1978 *
1979 * Also note we should probably be using this in 4.0 case too.
1980 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04001981 return !list_empty(&clp->cl_openowners)
1982 || !list_empty(&clp->cl_delegations)
1983 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001984}
1985
Al Virob37ad282006-10-19 23:28:59 -07001986__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001987nfsd4_exchange_id(struct svc_rqst *rqstp,
1988 struct nfsd4_compound_state *cstate,
1989 struct nfsd4_exchange_id *exid)
1990{
Andy Adamson0733d212009-04-03 08:28:01 +03001991 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001992 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04001993 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001994 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001995 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001996 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001997 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03001998
Jeff Layton363168b2009-08-14 12:57:56 -04001999 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002000 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002001 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002002 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002003 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002004
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002005 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002006 return nfserr_inval;
2007
Andy Adamson0733d212009-04-03 08:28:01 +03002008 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002009 case SP4_MACH_CRED:
2010 if (!svc_rqst_integrity_protected(rqstp))
2011 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002012 case SP4_NONE:
2013 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002014 default: /* checked by xdr code */
2015 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002016 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002017 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002018 }
2019
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002020 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002021 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002022 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002023 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002024 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2025 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2026
J. Bruce Fields136e6582012-05-12 20:37:23 -04002027 if (update) {
2028 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002029 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002030 goto out;
2031 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002032 if (!mach_creds_match(conf, rqstp)) {
2033 status = nfserr_wrong_cred;
2034 goto out;
2035 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002036 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002037 status = nfserr_perm;
2038 goto out;
2039 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002040 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002041 status = nfserr_not_same;
2042 goto out;
2043 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002044 /* case 6 */
2045 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2046 new = conf;
2047 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002048 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002049 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002050 if (client_has_state(conf)) {
2051 status = nfserr_clid_inuse;
2052 goto out;
2053 }
Andy Adamson0733d212009-04-03 08:28:01 +03002054 expire_client(conf);
2055 goto out_new;
2056 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002057 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002058 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002059 new = conf;
2060 goto out_copy;
2061 }
2062 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04002063 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002064 }
2065
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002066 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002067 status = nfserr_noent;
2068 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002069 }
2070
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002071 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002072 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03002073 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002074
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002075 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002076out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05002077 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03002078 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04002079 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03002080 goto out;
2081 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002082 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002083 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002084
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002085 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002086 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03002087out_copy:
2088 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2089 exid->clientid.cl_id = new->cl_clientid.cl_id;
2090
J. Bruce Fields778df3f2012-05-25 21:40:23 -04002091 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03002092 nfsd4_set_ex_flags(new, exid);
2093
2094 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04002095 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002096 status = nfs_ok;
2097
2098out:
2099 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03002100 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002101}
2102
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002103static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002104check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002105{
Andy Adamson88e588d2009-07-23 19:02:15 -04002106 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2107 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002108
2109 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002110 if (slot_inuse) {
2111 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002112 return nfserr_jukebox;
2113 else
2114 return nfserr_seq_misordered;
2115 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002116 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002117 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002118 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002119 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002120 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002121 return nfserr_seq_misordered;
2122}
2123
Andy Adamson49557cc2009-07-23 19:02:16 -04002124/*
2125 * Cache the create session result into the create session single DRC
2126 * slot cache by saving the xdr structure. sl_seqid has been set.
2127 * Do this for solo or embedded create session operations.
2128 */
2129static void
2130nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002131 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002132{
2133 slot->sl_status = nfserr;
2134 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2135}
2136
2137static __be32
2138nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2139 struct nfsd4_clid_slot *slot)
2140{
2141 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2142 return slot->sl_status;
2143}
2144
Mi Jinlong1b74c252011-07-14 14:50:17 +08002145#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2146 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2147 1 + /* MIN tag is length with zero, only length */ \
2148 3 + /* version, opcount, opcode */ \
2149 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2150 /* seqid, slotID, slotID, cache */ \
2151 4 ) * sizeof(__be32))
2152
2153#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2154 2 + /* verifier: AUTH_NULL, length 0 */\
2155 1 + /* status */ \
2156 1 + /* MIN tag is length with zero, only length */ \
2157 3 + /* opcount, opcode, opstatus*/ \
2158 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2159 /* seqid, slotID, slotID, slotID, status */ \
2160 5 ) * sizeof(__be32))
2161
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002162static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002163{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002164 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2165
J. Bruce Fields373cd402013-04-08 15:42:12 -04002166 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2167 return nfserr_toosmall;
2168 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2169 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002170 ca->headerpadsz = 0;
2171 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2172 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2173 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2174 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2175 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2176 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2177 /*
2178 * Note decreasing slot size below client's request may make it
2179 * difficult for client to function correctly, whereas
2180 * decreasing the number of slots will (just?) affect
2181 * performance. When short on memory we therefore prefer to
2182 * decrease number of slots instead of their size. Clients that
2183 * request larger slots than they need will get poor results:
2184 */
2185 ca->maxreqs = nfsd4_get_drc_mem(ca);
2186 if (!ca->maxreqs)
2187 return nfserr_jukebox;
2188
J. Bruce Fields373cd402013-04-08 15:42:12 -04002189 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002190}
2191
Kinglong Mee8a891632013-12-23 18:11:02 +08002192#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2193 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2194#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2195 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2196
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002197static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2198{
2199 ca->headerpadsz = 0;
2200
2201 /*
2202 * These RPC_MAX_HEADER macros are overkill, especially since we
2203 * don't even do gss on the backchannel yet. But this is still
2204 * less than 1k. Tighten up this estimate in the unlikely event
2205 * it turns out to be a problem for some client:
2206 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002207 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002208 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002209 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002210 return nfserr_toosmall;
2211 ca->maxresp_cached = 0;
2212 if (ca->maxops < 2)
2213 return nfserr_toosmall;
2214
2215 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002216}
2217
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002218static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2219{
2220 switch (cbs->flavor) {
2221 case RPC_AUTH_NULL:
2222 case RPC_AUTH_UNIX:
2223 return nfs_ok;
2224 default:
2225 /*
2226 * GSS case: the spec doesn't allow us to return this
2227 * error. But it also doesn't allow us not to support
2228 * GSS.
2229 * I'd rather this fail hard than return some error the
2230 * client might think it can already handle:
2231 */
2232 return nfserr_encr_alg_unsupp;
2233 }
2234}
2235
Andy Adamson069b6ad2009-04-03 08:27:58 +03002236__be32
2237nfsd4_create_session(struct svc_rqst *rqstp,
2238 struct nfsd4_compound_state *cstate,
2239 struct nfsd4_create_session *cr_ses)
2240{
Jeff Layton363168b2009-08-14 12:57:56 -04002241 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002242 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002243 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002244 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002245 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002246 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002247 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002248
Mi Jinlonga62573d2011-03-23 17:57:07 +08002249 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2250 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002251 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2252 if (status)
2253 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002254 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002255 if (status)
2256 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002257 status = check_backchannel_attrs(&cr_ses->back_channel);
2258 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002259 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002260 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002261 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002262 if (!new)
2263 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002264 conn = alloc_conn_from_crses(rqstp, cr_ses);
2265 if (!conn)
2266 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002267
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002268 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002269 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002270 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002271 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002272
2273 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002274 status = nfserr_wrong_cred;
2275 if (!mach_creds_match(conf, rqstp))
2276 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002277 cs_slot = &conf->cl_cs_slot;
2278 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002279 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002280 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002281 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002282 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002283 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002284 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002285 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002286 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002287 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002288 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002289 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002290 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002291 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002292 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002293 status = nfserr_wrong_cred;
2294 if (!mach_creds_match(unconf, rqstp))
2295 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002296 cs_slot = &unconf->cl_cs_slot;
2297 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002298 if (status) {
2299 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002300 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002301 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002302 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002303 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002304 if (old) {
2305 status = mark_client_expired(old);
2306 if (status)
2307 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002308 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002309 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002310 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002311 conf = unconf;
2312 } else {
2313 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002314 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002315 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002316 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002317 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002318 * We do not support RDMA or persistent sessions
2319 */
2320 cr_ses->flags &= ~SESSION4_PERSIST;
2321 cr_ses->flags &= ~SESSION4_RDMA;
2322
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002323 init_session(rqstp, new, conf, cr_ses);
2324 nfsd4_init_conn(rqstp, conn, new);
2325
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002326 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002327 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002328 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002329 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002330
Andy Adamson49557cc2009-07-23 19:02:16 -04002331 /* cache solo and embedded create sessions under the state lock */
2332 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002333 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002334 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002335out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08002336 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002337 free_conn(conn);
2338out_free_session:
2339 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002340out_release_drc_mem:
2341 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002342 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002343}
2344
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002345static __be32 nfsd4_map_bcts_dir(u32 *dir)
2346{
2347 switch (*dir) {
2348 case NFS4_CDFC4_FORE:
2349 case NFS4_CDFC4_BACK:
2350 return nfs_ok;
2351 case NFS4_CDFC4_FORE_OR_BOTH:
2352 case NFS4_CDFC4_BACK_OR_BOTH:
2353 *dir = NFS4_CDFC4_BOTH;
2354 return nfs_ok;
2355 };
2356 return nfserr_inval;
2357}
2358
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002359__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2360{
2361 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002362 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002363 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002364
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002365 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2366 if (status)
2367 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002368 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002369 session->se_cb_prog = bc->bc_cb_program;
2370 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002371 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002372
2373 nfsd4_probe_callback(session->se_client);
2374
2375 return nfs_ok;
2376}
2377
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002378__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2379 struct nfsd4_compound_state *cstate,
2380 struct nfsd4_bind_conn_to_session *bcts)
2381{
2382 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002383 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002384 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002385 struct net *net = SVC_NET(rqstp);
2386 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002387
2388 if (!nfsd4_last_compound_op(rqstp))
2389 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002390 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002391 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002392 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002393 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002394 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002395 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002396 status = nfserr_wrong_cred;
2397 if (!mach_creds_match(session->se_client, rqstp))
2398 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002399 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002400 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002401 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002402 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002403 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002404 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002405 goto out;
2406 nfsd4_init_conn(rqstp, conn, session);
2407 status = nfs_ok;
2408out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002409 nfsd4_put_session(session);
2410out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002411 nfs4_unlock_state();
2412 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002413}
2414
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002415static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2416{
2417 if (!session)
2418 return 0;
2419 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2420}
2421
Andy Adamson069b6ad2009-04-03 08:27:58 +03002422__be32
2423nfsd4_destroy_session(struct svc_rqst *r,
2424 struct nfsd4_compound_state *cstate,
2425 struct nfsd4_destroy_session *sessionid)
2426{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002427 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002428 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002429 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002430 struct net *net = SVC_NET(r);
2431 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002432
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002433 nfs4_lock_state();
2434 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002435 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002436 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002437 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002438 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002439 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002440 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002441 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002442 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002443 if (!ses)
2444 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002445 status = nfserr_wrong_cred;
2446 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002447 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002448 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002449 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002450 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002451 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002452 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002453
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002454 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002455
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002456 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002457 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002458out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002459 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002460out_client_lock:
2461 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002462out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002463 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002464 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002465}
2466
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002467static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002468{
2469 struct nfsd4_conn *c;
2470
2471 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002472 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002473 return c;
2474 }
2475 }
2476 return NULL;
2477}
2478
J. Bruce Fields57266a62013-04-13 14:27:29 -04002479static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002480{
2481 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002482 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002483 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002484 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002485
2486 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002487 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002488 if (c)
2489 goto out_free;
2490 status = nfserr_conn_not_bound_to_session;
2491 if (clp->cl_mach_cred)
2492 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002493 __nfsd4_hash_conn(new, ses);
2494 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002495 ret = nfsd4_register_conn(new);
2496 if (ret)
2497 /* oops; xprt is already down: */
2498 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002499 return nfs_ok;
2500out_free:
2501 spin_unlock(&clp->cl_lock);
2502 free_conn(new);
2503 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002504}
2505
Mi Jinlong868b89c2011-04-27 09:09:58 +08002506static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2507{
2508 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2509
2510 return args->opcnt > session->se_fchannel.maxops;
2511}
2512
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002513static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2514 struct nfsd4_session *session)
2515{
2516 struct xdr_buf *xb = &rqstp->rq_arg;
2517
2518 return xb->len > session->se_fchannel.maxreq_sz;
2519}
2520
Andy Adamson069b6ad2009-04-03 08:27:58 +03002521__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002522nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002523 struct nfsd4_compound_state *cstate,
2524 struct nfsd4_sequence *seq)
2525{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002526 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002527 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002528 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002529 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002530 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002531 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002532 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002533 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002534 struct net *net = SVC_NET(rqstp);
2535 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002536
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002537 if (resp->opcnt != 1)
2538 return nfserr_sequence_pos;
2539
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002540 /*
2541 * Will be either used or freed by nfsd4_sequence_check_conn
2542 * below.
2543 */
2544 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2545 if (!conn)
2546 return nfserr_jukebox;
2547
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002548 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002549 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002550 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002551 goto out_no_session;
2552 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002553
Mi Jinlong868b89c2011-04-27 09:09:58 +08002554 status = nfserr_too_many_ops;
2555 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002556 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002557
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002558 status = nfserr_req_too_big;
2559 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002560 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002561
Benny Halevyb85d4c02009-04-03 08:28:08 +03002562 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002563 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002564 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002565
Andy Adamson557ce262009-08-28 08:45:04 -04002566 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002567 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2568
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002569 /* We do not negotiate the number of slots yet, so set the
2570 * maxslots to the session maxreqs which is used to encode
2571 * sr_highest_slotid and the sr_target_slot id to maxslots */
2572 seq->maxslots = session->se_fchannel.maxreqs;
2573
J. Bruce Fields73e79482012-02-13 16:39:00 -05002574 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2575 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002576 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002577 status = nfserr_seq_misordered;
2578 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002579 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002580 cstate->slot = slot;
2581 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002582 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002583 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002584 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002585 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002586 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002587 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002588 }
2589 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002590 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002591
J. Bruce Fields57266a62013-04-13 14:27:29 -04002592 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002593 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002594 if (status)
2595 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002596
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002597 buflen = (seq->cachethis) ?
2598 session->se_fchannel.maxresp_cached :
2599 session->se_fchannel.maxresp_sz;
2600 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2601 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002602 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002603 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002604 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002605
2606 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002607 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002608 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002609 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002610 if (seq->cachethis)
2611 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002612 else
2613 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002614
2615 cstate->slot = slot;
2616 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002617 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002618
Benny Halevyb85d4c02009-04-03 08:28:08 +03002619out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002620 switch (clp->cl_cb_state) {
2621 case NFSD4_CB_DOWN:
2622 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2623 break;
2624 case NFSD4_CB_FAULT:
2625 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2626 break;
2627 default:
2628 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002629 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002630 if (!list_empty(&clp->cl_revoked))
2631 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002632out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002633 if (conn)
2634 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002635 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002636 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002637out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002638 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002639 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002640}
2641
Trond Myklebustb6076642014-06-30 11:48:35 -04002642void
2643nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2644{
2645 struct nfsd4_compound_state *cs = &resp->cstate;
2646
2647 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002648 if (cs->status != nfserr_replay_cache) {
2649 nfsd4_store_cache_entry(resp);
2650 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2651 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002652 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002653 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002654 } else if (cs->clp)
2655 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002656}
2657
Mi Jinlong345c2842011-10-20 17:51:39 +08002658__be32
2659nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2660{
2661 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002662 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002663 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002664
2665 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002666 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002667 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002668 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002669
2670 if (conf) {
2671 clp = conf;
2672
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002673 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002674 status = nfserr_clientid_busy;
2675 goto out;
2676 }
2677 } else if (unconf)
2678 clp = unconf;
2679 else {
2680 status = nfserr_stale_clientid;
2681 goto out;
2682 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002683 if (!mach_creds_match(clp, rqstp)) {
2684 status = nfserr_wrong_cred;
2685 goto out;
2686 }
Mi Jinlong345c2842011-10-20 17:51:39 +08002687 expire_client(clp);
2688out:
2689 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002690 return status;
2691}
2692
Andy Adamson069b6ad2009-04-03 08:27:58 +03002693__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002694nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2695{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002696 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002697
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002698 if (rc->rca_one_fs) {
2699 if (!cstate->current_fh.fh_dentry)
2700 return nfserr_nofilehandle;
2701 /*
2702 * We don't take advantage of the rca_one_fs case.
2703 * That's OK, it's optional, we can safely ignore it.
2704 */
2705 return nfs_ok;
2706 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002707
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002708 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002709 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002710 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2711 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002712 goto out;
2713
2714 status = nfserr_stale_clientid;
2715 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002716 /*
2717 * The following error isn't really legal.
2718 * But we only get here if the client just explicitly
2719 * destroyed the client. Surely it no longer cares what
2720 * error it gets back on an operation for the dead
2721 * client.
2722 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002723 goto out;
2724
2725 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002726 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002727out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002728 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002729 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002730}
2731
2732__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002733nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2734 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002736 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002738 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002739 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002740 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2741
J. Bruce Fields63db4632012-05-18 21:54:19 -04002742 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002744 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002745 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002746 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002748 if (clp_used_exchangeid(conf))
2749 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002750 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002751 char addr_str[INET6_ADDRSTRLEN];
2752 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2753 sizeof(addr_str));
2754 dprintk("NFSD: setclientid: string in use by client "
2755 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 goto out;
2757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002759 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002760 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002763 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002764 if (new == NULL)
2765 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002766 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002767 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002769 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002770 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002771 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002772 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002773 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2775 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2776 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2777 status = nfs_ok;
2778out:
2779 nfs4_unlock_state();
2780 return status;
2781}
2782
2783
Al Virob37ad282006-10-19 23:28:59 -07002784__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002785nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2786 struct nfsd4_compound_state *cstate,
2787 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788{
NeilBrown21ab45a2005-06-23 22:04:14 -07002789 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2791 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002792 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002793 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002795 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002798
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002799 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002800 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002801 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002802 * We try hard to give out unique clientid's, so if we get an
2803 * attempt to confirm the same clientid with a different cred,
2804 * there's a bug somewhere. Let's charitably assume it's our
2805 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002806 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002807 status = nfserr_serverfault;
2808 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2809 goto out;
2810 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2811 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002812 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002813 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2814 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002816 else /* case 4: client hasn't noticed we rebooted yet? */
2817 status = nfserr_stale_clientid;
2818 goto out;
2819 }
2820 status = nfs_ok;
2821 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002822 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2823 nfsd4_probe_callback(conf);
2824 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002825 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002826 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002827 if (conf) {
2828 status = mark_client_expired(conf);
2829 if (status)
2830 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002831 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002832 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002833 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002834 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 nfs4_unlock_state();
2838 return status;
2839}
2840
J. Bruce Fields32513b42011-10-13 16:00:16 -04002841static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002843 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2844}
2845
2846/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04002847static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04002848{
Trond Myklebustca943212014-07-23 16:17:39 -04002849 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
Trond Myklebust950e0112014-06-30 11:48:31 -04002851 lockdep_assert_held(&state_lock);
2852
J. Bruce Fields32513b42011-10-13 16:00:16 -04002853 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04002854 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002855 INIT_LIST_HEAD(&fp->fi_stateids);
2856 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04002857 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002858 fp->fi_had_conflict = false;
2859 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04002860 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002861 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2862 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04002863 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864}
2865
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002866void
NeilBrowne60d4392005-06-23 22:03:01 -07002867nfsd4_free_slabs(void)
2868{
Christoph Hellwigabf11352014-05-21 07:43:03 -07002869 kmem_cache_destroy(openowner_slab);
2870 kmem_cache_destroy(lockowner_slab);
2871 kmem_cache_destroy(file_slab);
2872 kmem_cache_destroy(stateid_slab);
2873 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002874}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Bryan Schumaker72083392011-11-01 15:24:59 -04002876int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877nfsd4_init_slabs(void)
2878{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002879 openowner_slab = kmem_cache_create("nfsd4_openowners",
2880 sizeof(struct nfs4_openowner), 0, 0, NULL);
2881 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002882 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002883 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002884 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002885 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002886 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07002887 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002888 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002889 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002890 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07002891 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002892 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002893 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002894 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002895 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002896 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002897 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002898 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07002900
2901out_free_stateid_slab:
2902 kmem_cache_destroy(stateid_slab);
2903out_free_file_slab:
2904 kmem_cache_destroy(file_slab);
2905out_free_lockowner_slab:
2906 kmem_cache_destroy(lockowner_slab);
2907out_free_openowner_slab:
2908 kmem_cache_destroy(openowner_slab);
2909out:
NeilBrowne60d4392005-06-23 22:03:01 -07002910 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2911 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912}
2913
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002914static void init_nfs4_replay(struct nfs4_replay *rp)
2915{
2916 rp->rp_status = nfserr_serverfault;
2917 rp->rp_buflen = 0;
2918 rp->rp_buf = rp->rp_ibuf;
2919}
2920
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002921static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922{
2923 struct nfs4_stateowner *sop;
2924
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002925 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002926 if (!sop)
2927 return NULL;
2928
2929 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2930 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002931 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002932 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002934 sop->so_owner.len = owner->len;
2935
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002936 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002937 sop->so_client = clp;
2938 init_nfs4_replay(&sop->so_replay);
2939 return sop;
2940}
2941
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002942static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002943{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002944 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2945
2946 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002947 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948}
2949
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002950static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04002951alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002952 struct nfsd4_compound_state *cstate)
2953{
Trond Myklebust13d6f662014-06-30 11:48:45 -04002954 struct nfs4_client *clp = cstate->clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002955 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002957 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2958 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002960 oo->oo_owner.so_is_open_owner = 1;
2961 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04002962 oo->oo_flags = NFS4_OO_NEW;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002963 if (nfsd4_has_session(cstate))
2964 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002965 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002966 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002967 INIT_LIST_HEAD(&oo->oo_close_lru);
2968 hash_openowner(oo, clp, strhashval);
2969 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970}
2971
J. Bruce Fields996e0932011-10-17 11:14:48 -04002972static 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 -04002973 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002975 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04002976 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002977 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002978 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002979 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 stp->st_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 stp->st_access_bmap = 0;
2982 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07002983 stp->st_openstp = NULL;
Trond Myklebust1d31a252014-07-10 14:07:25 -04002984 spin_lock(&fp->fi_lock);
2985 list_add(&stp->st_perfile, &fp->fi_stateids);
2986 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987}
2988
2989static void
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002990move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991{
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002992 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2993
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002994 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002996 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002997 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998}
2999
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04003001same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
3002 clientid_t *clid)
3003{
3004 return (sop->so_owner.len == owner->len) &&
3005 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
3006 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007}
3008
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003009static struct nfs4_openowner *
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003010find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
3011 bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003013 struct nfs4_stateowner *so;
3014 struct nfs4_openowner *oo;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003015 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003017 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003018 if (!so->so_is_open_owner)
3019 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003020 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
3021 oo = openowner(so);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003022 clp = oo->oo_owner.so_client;
3023 if ((bool)clp->cl_minorversion != sessions)
3024 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003025 renew_client(oo->oo_owner.so_client);
3026 return oo;
3027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 }
3029 return NULL;
3030}
3031
3032/* search file_hashtbl[] for file */
3033static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003034find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035{
Trond Myklebustca943212014-07-23 16:17:39 -04003036 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 struct nfs4_file *fp;
3038
Trond Myklebust950e0112014-06-30 11:48:31 -04003039 lockdep_assert_held(&state_lock);
3040
Jeff Layton89876f82013-04-02 09:01:59 -04003041 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003042 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003043 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 }
3047 return NULL;
3048}
3049
Trond Myklebust950e0112014-06-30 11:48:31 -04003050static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003051find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003052{
3053 struct nfs4_file *fp;
3054
3055 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003056 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003057 spin_unlock(&state_lock);
3058 return fp;
3059}
3060
3061static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003062find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003063{
3064 struct nfs4_file *fp;
3065
3066 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003067 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003068 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003069 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003070 fp = new;
3071 }
3072 spin_unlock(&state_lock);
3073
3074 return fp;
3075}
3076
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003077/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 * Called to check deny when READ with all zero stateid or
3079 * WRITE with all zero or all one stateid
3080 */
Al Virob37ad282006-10-19 23:28:59 -07003081static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3083{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003085 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
Trond Myklebustca943212014-07-23 16:17:39 -04003087 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003088 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003089 return ret;
3090 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003091 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003092 if (fp->fi_share_deny & deny_type)
3093 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003094 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003095 put_nfs4_file(fp);
3096 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097}
3098
Jeff Layton02e12152014-07-16 10:31:57 -04003099void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100{
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003101 struct nfs4_client *clp = dp->dl_stid.sc_client;
3102 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3103
Jeff Layton02e12152014-07-16 10:31:57 -04003104 /*
3105 * We can't do this in nfsd_break_deleg_cb because it is
3106 * already holding inode->i_lock
3107 */
3108 spin_lock(&state_lock);
3109 block_delegations(&dp->dl_fh);
Jeff Laytondff13992014-07-08 14:02:49 -04003110 /*
3111 * If the dl_time != 0, then we know that it has already been
3112 * queued for a lease break. Don't queue it again.
3113 */
3114 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003115 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003116 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003117 }
Jeff Layton02e12152014-07-16 10:31:57 -04003118 spin_unlock(&state_lock);
3119}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
Jeff Layton02e12152014-07-16 10:31:57 -04003121static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3122{
3123 /*
3124 * We're assuming the state code never drops its reference
3125 * without first removing the lease. Since we're in this lease
3126 * callback (and since the lease code is serialized by the kernel
3127 * lock) we know the server hasn't removed the lease yet, we know
3128 * it's safe to take a reference.
3129 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003130 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003131 nfsd4_cb_recall(dp);
3132}
3133
Jeff Layton1c8c6012013-06-21 08:58:15 -04003134/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003135static void nfsd_break_deleg_cb(struct file_lock *fl)
3136{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003137 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3138 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003139
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003140 if (!fp) {
3141 WARN(1, "(%p)->fl_owner NULL\n", fl);
3142 return;
3143 }
3144 if (fp->fi_had_conflict) {
3145 WARN(1, "duplicate break on %p\n", fp);
3146 return;
3147 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003148 /*
3149 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003150 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003151 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003152 */
3153 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Jeff Layton02e12152014-07-16 10:31:57 -04003155 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003156 fp->fi_had_conflict = true;
3157 /*
3158 * If there are no delegations on the list, then we can't count on this
3159 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3160 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3161 * true should keep any new delegations from being hashed.
3162 */
3163 if (list_empty(&fp->fi_delegations))
3164 fl->fl_break_time = jiffies;
3165 else
3166 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3167 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003168 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169}
3170
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171static
3172int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3173{
3174 if (arg & F_UNLCK)
3175 return lease_modify(onlist, arg);
3176 else
3177 return -EAGAIN;
3178}
3179
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003180static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003181 .lm_break = nfsd_break_deleg_cb,
3182 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183};
3184
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003185static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3186{
3187 if (nfsd4_has_session(cstate))
3188 return nfs_ok;
3189 if (seqid == so->so_seqid - 1)
3190 return nfserr_replay_me;
3191 if (seqid == so->so_seqid)
3192 return nfs_ok;
3193 return nfserr_bad_seqid;
3194}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
Jeff Layton4b24ca72014-06-30 11:48:44 -04003196static __be32 lookup_clientid(clientid_t *clid,
3197 struct nfsd4_compound_state *cstate,
3198 struct nfsd_net *nn)
3199{
3200 struct nfs4_client *found;
3201
3202 if (cstate->clp) {
3203 found = cstate->clp;
3204 if (!same_clid(&found->cl_clientid, clid))
3205 return nfserr_stale_clientid;
3206 return nfs_ok;
3207 }
3208
3209 if (STALE_CLIENTID(clid, nn))
3210 return nfserr_stale_clientid;
3211
3212 /*
3213 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3214 * cached already then we know this is for is for v4.0 and "sessions"
3215 * will be false.
3216 */
3217 WARN_ON_ONCE(cstate->session);
3218 found = find_confirmed_client(clid, false, nn);
3219 if (!found)
3220 return nfserr_expired;
3221
3222 /* Cache the nfs4_client in cstate! */
3223 cstate->clp = found;
3224 atomic_inc(&found->cl_refcount);
3225 return nfs_ok;
3226}
3227
Al Virob37ad282006-10-19 23:28:59 -07003228__be32
Andy Adamson66689582009-04-03 08:28:45 +03003229nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003230 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 clientid_t *clientid = &open->op_clientid;
3233 struct nfs4_client *clp = NULL;
3234 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003235 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003236 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003238 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003240 /*
3241 * In case we need it later, after we've already created the
3242 * file and don't want to risk a further failure:
3243 */
3244 open->op_file = nfsd4_alloc_file();
3245 if (open->op_file == NULL)
3246 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
Trond Myklebust2d91e892014-06-30 11:48:46 -04003248 status = lookup_clientid(clientid, cstate, nn);
3249 if (status)
3250 return status;
3251 clp = cstate->clp;
3252
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003253 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003254 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003255 open->op_openowner = oo;
3256 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003257 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003259 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003260 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003261 release_openowner(oo);
3262 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003263 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003264 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003265 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3266 if (status)
3267 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003268 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003269new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003270 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003271 if (oo == NULL)
3272 return nfserr_jukebox;
3273 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003274alloc_stateid:
3275 open->op_stp = nfs4_alloc_stateid(clp);
3276 if (!open->op_stp)
3277 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003278 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279}
3280
Al Virob37ad282006-10-19 23:28:59 -07003281static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003282nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3283{
3284 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3285 return nfserr_openmode;
3286 else
3287 return nfs_ok;
3288}
3289
Daniel Mackc47d8322011-05-16 16:38:14 +02003290static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003291{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003292 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3293}
3294
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003295static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003296{
3297 struct nfs4_stid *ret;
3298
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003299 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003300 if (!ret)
3301 return NULL;
3302 return delegstateid(ret);
3303}
3304
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003305static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3306{
3307 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3308 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3309}
3310
Al Virob37ad282006-10-19 23:28:59 -07003311static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003312nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003313 struct nfs4_delegation **dp)
3314{
3315 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003316 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07003317
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003318 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07003319 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003320 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003321 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07003322 status = nfs4_check_delegmode(*dp, flags);
3323 if (status)
3324 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003325out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003326 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003327 return nfs_ok;
3328 if (status)
3329 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003330 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003331 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003332}
3333
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003334static struct nfs4_ol_stateid *
3335nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003337 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003338 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339
Trond Myklebust1d31a252014-07-10 14:07:25 -04003340 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003341 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 /* ignore lock owners */
3343 if (local->st_stateowner->so_is_open_owner == 0)
3344 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003345 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003346 ret = local;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003347 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003350 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003351 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352}
3353
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003354static inline int nfs4_access_to_access(u32 nfs4_access)
3355{
3356 int flags = 0;
3357
3358 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3359 flags |= NFSD_MAY_READ;
3360 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3361 flags |= NFSD_MAY_WRITE;
3362 return flags;
3363}
3364
Al Virob37ad282006-10-19 23:28:59 -07003365static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3367 struct nfsd4_open *open)
3368{
3369 struct iattr iattr = {
3370 .ia_valid = ATTR_SIZE,
3371 .ia_size = 0,
3372 };
3373 if (!open->op_truncate)
3374 return 0;
3375 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003376 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3378}
3379
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003380static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003381 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3382 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003383{
Trond Myklebustde186432014-07-10 14:07:26 -04003384 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003385 __be32 status;
3386 int oflag = nfs4_access_to_omode(open->op_share_access);
3387 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003388 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003389
Trond Myklebustde186432014-07-10 14:07:26 -04003390 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003391
3392 /*
3393 * Are we trying to set a deny mode that would conflict with
3394 * current access?
3395 */
3396 status = nfs4_file_check_deny(fp, open->op_share_deny);
3397 if (status != nfs_ok) {
3398 spin_unlock(&fp->fi_lock);
3399 goto out;
3400 }
3401
3402 /* set access to the file */
3403 status = nfs4_file_get_access(fp, open->op_share_access);
3404 if (status != nfs_ok) {
3405 spin_unlock(&fp->fi_lock);
3406 goto out;
3407 }
3408
3409 /* Set access bits in stateid */
3410 old_access_bmap = stp->st_access_bmap;
3411 set_access(open->op_share_access, stp);
3412
3413 /* Set new deny mask */
3414 old_deny_bmap = stp->st_deny_bmap;
3415 set_deny(open->op_share_deny, stp);
3416 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3417
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003418 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003419 spin_unlock(&fp->fi_lock);
3420 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003421 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003422 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003423 spin_lock(&fp->fi_lock);
3424 if (!fp->fi_fds[oflag]) {
3425 fp->fi_fds[oflag] = filp;
3426 filp = NULL;
3427 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003428 }
Trond Myklebustde186432014-07-10 14:07:26 -04003429 spin_unlock(&fp->fi_lock);
3430 if (filp)
3431 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003432
3433 status = nfsd4_truncate(rqstp, cur_fh, open);
3434 if (status)
3435 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003436out:
3437 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003438out_put_access:
3439 stp->st_access_bmap = old_access_bmap;
3440 nfs4_file_put_access(fp, open->op_share_access);
3441 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3442 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003443}
3444
Al Virob37ad282006-10-19 23:28:59 -07003445static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003446nfs4_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 -07003447{
Al Virob37ad282006-10-19 23:28:59 -07003448 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003449 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003451 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003452 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003453
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003454 /* test and set deny mode */
3455 spin_lock(&fp->fi_lock);
3456 status = nfs4_file_check_deny(fp, open->op_share_deny);
3457 if (status == nfs_ok) {
3458 old_deny_bmap = stp->st_deny_bmap;
3459 set_deny(open->op_share_deny, stp);
3460 fp->fi_share_deny |=
3461 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3462 }
3463 spin_unlock(&fp->fi_lock);
3464
3465 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003468 status = nfsd4_truncate(rqstp, cur_fh, open);
3469 if (status != nfs_ok)
3470 reset_union_bmap_deny(old_deny_bmap, stp);
3471 return status;
3472}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003475nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003477 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478}
3479
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003480/* Should we give out recallable state?: */
3481static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3482{
3483 if (clp->cl_cb_state == NFSD4_CB_UP)
3484 return true;
3485 /*
3486 * In the sessions case, since we don't have to establish a
3487 * separate connection for callbacks, we assume it's OK
3488 * until we hear otherwise:
3489 */
3490 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3491}
3492
Jeff Laytond564fbe2014-07-16 10:31:58 -04003493static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003494{
3495 struct file_lock *fl;
3496
3497 fl = locks_alloc_lock();
3498 if (!fl)
3499 return NULL;
3500 locks_init_lock(fl);
3501 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003502 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003503 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3504 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003505 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003506 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003507 return fl;
3508}
3509
J. Bruce Fields99c41512013-05-21 16:21:25 -04003510static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003511{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003512 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003513 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003514 struct file *filp;
3515 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003516
Jeff Laytond564fbe2014-07-16 10:31:58 -04003517 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003518 if (!fl)
3519 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003520 filp = find_readable_file(fp);
3521 if (!filp) {
3522 /* We should always have a readable file here */
3523 WARN_ON_ONCE(1);
3524 return -EBADF;
3525 }
3526 fl->fl_file = filp;
3527 status = vfs_setlease(filp, fl->fl_type, &fl);
3528 if (status) {
3529 locks_free_lock(fl);
3530 goto out_fput;
3531 }
Benny Halevycdc97502014-05-30 09:09:30 -04003532 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003533 spin_lock(&fp->fi_lock);
3534 /* Did the lease get broken before we took the lock? */
3535 status = -EAGAIN;
3536 if (fp->fi_had_conflict)
3537 goto out_unlock;
3538 /* Race breaker */
3539 if (fp->fi_lease) {
3540 status = 0;
3541 atomic_inc(&fp->fi_delegees);
3542 hash_delegation_locked(dp, fp);
3543 goto out_unlock;
3544 }
3545 fp->fi_lease = fl;
3546 fp->fi_deleg_file = filp;
3547 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003548 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003549 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003550 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003551 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003552out_unlock:
3553 spin_unlock(&fp->fi_lock);
3554 spin_unlock(&state_lock);
3555out_fput:
3556 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003557 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003558}
3559
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003560static int nfs4_set_delegation(struct nfs4_delegation *dp, struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003561{
Jeff Layton417c6622014-07-21 09:34:57 -04003562 int status = 0;
3563
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003564 if (fp->fi_had_conflict)
3565 return -EAGAIN;
3566 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003567 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003568 spin_lock(&fp->fi_lock);
3569 dp->dl_file = fp;
3570 if (!fp->fi_lease) {
3571 spin_unlock(&fp->fi_lock);
3572 spin_unlock(&state_lock);
3573 return nfs4_setlease(dp);
3574 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003575 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003576 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003577 status = -EAGAIN;
3578 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003579 }
Benny Halevy931ee562014-05-30 09:09:27 -04003580 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003581out_unlock:
3582 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003583 spin_unlock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003584 return status;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003585}
3586
Benny Halevy4aa89132012-02-21 14:16:44 -08003587static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3588{
3589 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3590 if (status == -EAGAIN)
3591 open->op_why_no_deleg = WND4_CONTENTION;
3592 else {
3593 open->op_why_no_deleg = WND4_RESOURCE;
3594 switch (open->op_deleg_want) {
3595 case NFS4_SHARE_WANT_READ_DELEG:
3596 case NFS4_SHARE_WANT_WRITE_DELEG:
3597 case NFS4_SHARE_WANT_ANY_DELEG:
3598 break;
3599 case NFS4_SHARE_WANT_CANCEL:
3600 open->op_why_no_deleg = WND4_CANCELLED;
3601 break;
3602 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003603 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003604 }
3605 }
3606}
3607
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608/*
3609 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003610 *
3611 * Note we don't support write delegations, and won't until the vfs has
3612 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 */
3614static void
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003615nfs4_open_delegation(struct net *net, struct svc_fh *fh,
3616 struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617{
3618 struct nfs4_delegation *dp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003619 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003620 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003621 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003623 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003624 open->op_recall = 0;
3625 switch (open->op_claim_type) {
3626 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003627 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003628 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003629 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3630 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003631 break;
3632 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003633 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003634 /*
3635 * Let's not give out any delegations till everyone's
3636 * had the chance to reclaim theirs....
3637 */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003638 if (locks_in_grace(net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003639 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003640 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003641 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003642 /*
3643 * Also, if the file was opened for write or
3644 * create, there's a good chance the client's
3645 * about to write to it, resulting in an
3646 * immediate recall (since we don't support
3647 * write delegations):
3648 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003649 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003650 goto out_no_deleg;
3651 if (open->op_create == NFS4_OPEN_CREATE)
3652 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003653 break;
3654 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003655 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003656 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003657 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003658 if (dp == NULL)
3659 goto out_no_deleg;
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003660 status = nfs4_set_delegation(dp, stp->st_file);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003661 if (status)
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003662 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003664 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003666 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003667 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003668 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003669 return;
3670out_free:
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003671 destroy_delegation(dp);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003672out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003673 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3674 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003675 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003676 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003677 open->op_recall = 1;
3678 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003679
3680 /* 4.1 client asking for a delegation? */
3681 if (open->op_deleg_want)
3682 nfsd4_open_deleg_none_ext(open, status);
3683 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684}
3685
Benny Halevye27f49c2012-02-21 14:16:54 -08003686static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3687 struct nfs4_delegation *dp)
3688{
3689 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3690 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3691 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3692 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3693 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3694 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3695 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3696 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3697 }
3698 /* Otherwise the client must be confused wanting a delegation
3699 * it already has, therefore we don't return
3700 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3701 */
3702}
3703
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704/*
3705 * called with nfs4_lock_state() held.
3706 */
Al Virob37ad282006-10-19 23:28:59 -07003707__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3709{
Andy Adamson66689582009-04-03 08:28:45 +03003710 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003711 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003713 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003714 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003715 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 /*
3718 * Lookup file; if found, lookup stateid and check open request,
3719 * and check for delegations in the process of being recalled.
3720 * If not found, create the nfs4_file struct
3721 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003722 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04003723 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04003724 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003725 if (status)
3726 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003727 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003729 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003730 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003731 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003732 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003733 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 }
3735
3736 /*
3737 * OPEN the file, or upgrade an existing OPEN.
3738 * If truncate fails, the OPEN fails.
3739 */
3740 if (stp) {
3741 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003742 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 if (status)
3744 goto out;
3745 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003746 stp = open->op_stp;
3747 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003748 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003749 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3750 if (status) {
3751 release_open_stateid(stp);
3752 goto out;
3753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003755 update_stateid(&stp->st_stid.sc_stateid);
3756 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
Benny Halevyd24433c2012-02-16 20:57:17 +02003758 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02003759 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3760 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3761 open->op_why_no_deleg = WND4_NOT_WANTED;
3762 goto nodeleg;
3763 }
3764 }
3765
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 /*
3767 * Attempt to hand out a delegation. No error return, because the
3768 * OPEN succeeds even if we fail.
3769 */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003770 nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003771nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 status = nfs_ok;
3773
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003774 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003775 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003777 /* 4.1 client trying to upgrade/downgrade delegation? */
3778 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003779 open->op_deleg_want)
3780 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003781
NeilBrown13cd2182005-06-23 22:03:10 -07003782 if (fp)
3783 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003784 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003785 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 /*
3787 * To finish the open response, we just need to set the rflags.
3788 */
3789 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003790 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003791 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3793
3794 return status;
3795}
3796
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003797void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3798{
3799 if (open->op_openowner) {
3800 struct nfs4_openowner *oo = open->op_openowner;
3801
3802 if (!list_empty(&oo->oo_owner.so_stateids))
3803 list_del_init(&oo->oo_close_lru);
3804 if (oo->oo_flags & NFS4_OO_NEW) {
3805 if (status) {
3806 release_openowner(oo);
3807 open->op_openowner = NULL;
3808 } else
3809 oo->oo_flags &= ~NFS4_OO_NEW;
3810 }
3811 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003812 if (open->op_file)
3813 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003814 if (open->op_stp)
J. Bruce Fieldsef798592012-08-29 10:32:54 -07003815 free_generic_stateid(open->op_stp);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003816}
3817
Al Virob37ad282006-10-19 23:28:59 -07003818__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003819nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3820 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821{
3822 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003823 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003824 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825
3826 nfs4_lock_state();
3827 dprintk("process_renew(%08x/%08x): starting\n",
3828 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003829 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003830 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003832 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003834 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003835 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 goto out;
3837 status = nfs_ok;
3838out:
3839 nfs4_unlock_state();
3840 return status;
3841}
3842
NeilBrowna76b4312005-06-23 22:04:01 -07003843static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003844nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07003845{
Jeff Layton33dcc482012-04-10 11:08:48 -04003846 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003847 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003848 return;
3849
NeilBrowna76b4312005-06-23 22:04:01 -07003850 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003851 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003852 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003853 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003854 /*
3855 * Now that every NFSv4 client has had the chance to recover and
3856 * to see the (possibly new, possibly shorter) lease time, we
3857 * can safely set the next grace time to the current lease time:
3858 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03003859 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003860}
3861
NeilBrownfd39ca92005-06-23 22:04:03 -07003862static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003863nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864{
3865 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003866 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 struct nfs4_delegation *dp;
3868 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003869 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003870 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871
3872 nfs4_lock_state();
3873
3874 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003875 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03003876 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003877 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03003878 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 clp = list_entry(pos, struct nfs4_client, cl_lru);
3880 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3881 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003882 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 break;
3884 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003885 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03003886 dprintk("NFSD: client in use (clientid %08x)\n",
3887 clp->cl_clientid.cl_id);
3888 continue;
3889 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003890 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003891 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003892 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03003893 list_for_each_safe(pos, next, &reaplist) {
3894 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 dprintk("NFSD: purging unused client (clientid %08x)\n",
3896 clp->cl_clientid.cl_id);
3897 expire_client(clp);
3898 }
Benny Halevycdc97502014-05-30 09:09:30 -04003899 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003900 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03003902 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3903 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003905 t = dp->dl_time - cutoff;
3906 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 break;
3908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 list_move(&dp->dl_recall_lru, &reaplist);
3910 }
Benny Halevycdc97502014-05-30 09:09:30 -04003911 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 list_for_each_safe(pos, next, &reaplist) {
3913 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003914 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003916 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003917 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3918 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003919 t = oo->oo_time - cutoff;
3920 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 break;
3922 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003923 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04003925 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04003927 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928}
3929
Harvey Harrisona254b242008-02-20 12:49:00 -08003930static struct workqueue_struct *laundry_wq;
3931static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08003932
3933static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003934laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935{
3936 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003937 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3938 work);
3939 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3940 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003942 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003944 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945}
3946
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003947static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003948{
Trond Myklebustb07c54a2014-07-23 16:17:40 -04003949 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003950 return nfserr_bad_stateid;
3951 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952}
3953
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003955access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003957 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3958 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3959 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960}
3961
3962static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003963access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003965 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3966 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967}
3968
3969static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003970__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971{
Al Virob37ad282006-10-19 23:28:59 -07003972 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
J. Bruce Fields02921912010-07-29 15:16:59 -04003974 /* For lock stateid's, we test the parent open, not the lock: */
3975 if (stp->st_openstp)
3976 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003977 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003979 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 goto out;
3981 status = nfs_ok;
3982out:
3983 return status;
3984}
3985
Al Virob37ad282006-10-19 23:28:59 -07003986static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003987check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003989 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003991 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003992 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 * conflicting state; so we must wait out the grace period. */
3994 return nfserr_grace;
3995 } else if (flags & WR_STATE)
3996 return nfs4_share_conflict(current_fh,
3997 NFS4_SHARE_DENY_WRITE);
3998 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3999 return nfs4_share_conflict(current_fh,
4000 NFS4_SHARE_DENY_READ);
4001}
4002
4003/*
4004 * Allow READ/WRITE during grace period on recovered state only for files
4005 * that are not able to provide mandatory locking.
4006 */
4007static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004008grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004010 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011}
4012
J. Bruce Fields81b82962011-08-23 11:03:29 -04004013/* Returns true iff a is later than b: */
4014static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4015{
Jim Rees1a9357f2013-05-17 17:33:00 -04004016 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004017}
4018
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004019static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004020{
Andy Adamson66689582009-04-03 08:28:45 +03004021 /*
4022 * When sessions are used the stateid generation number is ignored
4023 * when it is zero.
4024 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004025 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004026 return nfs_ok;
4027
4028 if (in->si_generation == ref->si_generation)
4029 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004030
J. Bruce Fields0836f582008-01-26 19:08:12 -05004031 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004032 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004033 return nfserr_bad_stateid;
4034 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004035 * However, we could see a stateid from the past, even from a
4036 * non-buggy client. For example, if the client sends a lock
4037 * while some IO is outstanding, the lock may bump si_generation
4038 * while the IO is still in flight. The client could avoid that
4039 * situation by waiting for responses on all the IO requests,
4040 * but better performance may result in retrying IO that
4041 * receives an old_stateid error if requests are rarely
4042 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004043 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004044 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004045}
4046
Chuck Lever7df302f2012-05-29 13:56:37 -04004047static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004048{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004049 struct nfs4_stid *s;
4050 struct nfs4_ol_stateid *ols;
4051 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004052
Chuck Lever7df302f2012-05-29 13:56:37 -04004053 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4054 return nfserr_bad_stateid;
4055 /* Client debugging aid. */
4056 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4057 char addr_str[INET6_ADDRSTRLEN];
4058 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4059 sizeof(addr_str));
4060 pr_warn_ratelimited("NFSD: client %s testing state ID "
4061 "with incorrect client ID\n", addr_str);
4062 return nfserr_bad_stateid;
4063 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004064 s = find_stateid(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004065 if (!s)
Chuck Lever7df302f2012-05-29 13:56:37 -04004066 return nfserr_bad_stateid;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004067 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004068 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004069 return status;
J. Bruce Fields23340032013-04-09 17:42:28 -04004070 switch (s->sc_type) {
4071 case NFS4_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004072 return nfs_ok;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004073 case NFS4_REVOKED_DELEG_STID:
4074 return nfserr_deleg_revoked;
J. Bruce Fields23340032013-04-09 17:42:28 -04004075 case NFS4_OPEN_STID:
4076 case NFS4_LOCK_STID:
4077 ols = openlockstateid(s);
4078 if (ols->st_stateowner->so_is_open_owner
4079 && !(openowner(ols->st_stateowner)->oo_flags
4080 & NFS4_OO_CONFIRMED))
4081 return nfserr_bad_stateid;
4082 return nfs_ok;
4083 default:
4084 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004085 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004086 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004087 case NFS4_CLOSED_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004088 return nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004089 }
Bryan Schumaker17456802011-07-13 10:50:48 -04004090}
4091
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004092static __be32
4093nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4094 stateid_t *stateid, unsigned char typemask,
4095 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004096{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004097 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004098
4099 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4100 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004101 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004102 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004103 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004104 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004105 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004106 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004107 if (status)
4108 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004109 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004110 if (!*s)
4111 return nfserr_bad_stateid;
4112 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004113}
4114
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115/*
4116* Checks for stateid operations
4117*/
Al Virob37ad282006-10-19 23:28:59 -07004118__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004119nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004120 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004122 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004123 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004125 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004127 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004128 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004129 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 if (filpp)
4132 *filpp = NULL;
4133
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004134 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 return nfserr_grace;
4136
4137 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004138 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
Trond Myklebust14bcab12014-04-18 14:44:07 -04004140 nfs4_lock_state();
4141
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004142 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004143 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004144 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004145 if (status)
Trond Myklebust14bcab12014-04-18 14:44:07 -04004146 goto out;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004147 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4148 if (status)
4149 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004150 switch (s->sc_type) {
4151 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004152 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004153 status = nfs4_check_delegmode(dp, flags);
4154 if (status)
4155 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004156 if (filpp) {
Trond Myklebust14bcab12014-04-18 14:44:07 -04004157 file = dp->dl_file->fi_deleg_file;
4158 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004159 WARN_ON_ONCE(1);
4160 status = nfserr_serverfault;
4161 goto out;
4162 }
Trond Myklebustde186432014-07-10 14:07:26 -04004163 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004164 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004165 break;
4166 case NFS4_OPEN_STID:
4167 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004168 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004169 status = nfs4_check_fh(current_fh, stp);
4170 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004172 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004173 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004175 status = nfs4_check_openmode(stp, flags);
4176 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004178 if (filpp) {
4179 if (flags & RD_STATE)
Trond Myklebust14bcab12014-04-18 14:44:07 -04004180 file = find_readable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004181 else
Trond Myklebust14bcab12014-04-18 14:44:07 -04004182 file = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004183 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004184 break;
4185 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004186 status = nfserr_bad_stateid;
4187 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 }
4189 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004190 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004191 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192out:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004193 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 return status;
4195}
4196
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004197static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004198nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004199{
J. Bruce Fieldsa1b8ff42014-05-20 15:55:21 -04004200 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4201
4202 if (check_for_locks(stp->st_file, lo))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004203 return nfserr_locks_held;
Jeff Laytonc53530d2014-06-30 11:48:39 -04004204 release_lockowner_if_empty(lo);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004205 return nfs_ok;
4206}
4207
4208/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004209 * Test if the stateid is valid
4210 */
4211__be32
4212nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4213 struct nfsd4_test_stateid *test_stateid)
4214{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004215 struct nfsd4_test_stateid_id *stateid;
4216 struct nfs4_client *cl = cstate->session->se_client;
4217
4218 nfs4_lock_state();
4219 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004220 stateid->ts_id_status =
4221 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004222 nfs4_unlock_state();
4223
Bryan Schumaker17456802011-07-13 10:50:48 -04004224 return nfs_ok;
4225}
4226
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004227__be32
4228nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4229 struct nfsd4_free_stateid *free_stateid)
4230{
4231 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004232 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004233 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004234 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004235 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004236
4237 nfs4_lock_state();
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004238 s = find_stateid(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004239 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004240 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004241 switch (s->sc_type) {
4242 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004243 ret = nfserr_locks_held;
4244 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004245 case NFS4_OPEN_STID:
4246 case NFS4_LOCK_STID:
4247 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4248 if (ret)
4249 goto out;
4250 if (s->sc_type == NFS4_LOCK_STID)
4251 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4252 else
4253 ret = nfserr_locks_held;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004254 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004255 case NFS4_REVOKED_DELEG_STID:
4256 dp = delegstateid(s);
4257 destroy_revoked_delegation(dp);
4258 ret = nfs_ok;
4259 break;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004260 default:
4261 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004262 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004263out:
4264 nfs4_unlock_state();
4265 return ret;
4266}
4267
NeilBrown4c4cd222005-07-07 17:59:27 -07004268static inline int
4269setlkflg (int type)
4270{
4271 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4272 RD_STATE : WR_STATE;
4273}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004275static __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 -04004276{
4277 struct svc_fh *current_fh = &cstate->current_fh;
4278 struct nfs4_stateowner *sop = stp->st_stateowner;
4279 __be32 status;
4280
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004281 status = nfsd4_check_seqid(cstate, sop, seqid);
4282 if (status)
4283 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004284 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4285 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004286 /*
4287 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004288 * nfserr_replay_me from the previous step, and
4289 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004290 */
4291 return nfserr_bad_stateid;
4292 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4293 if (status)
4294 return status;
4295 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004296}
4297
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298/*
4299 * Checks for sequence id mutating operations.
4300 */
Al Virob37ad282006-10-19 23:28:59 -07004301static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004302nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004303 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004304 struct nfs4_ol_stateid **stpp,
4305 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004307 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004308 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004309 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004311 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4312 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004313
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004315 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004316 if (status)
4317 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004318 stp = openlockstateid(s);
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04004319 if (!nfsd4_has_session(cstate))
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004320 cstate->replay_owner = stp->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004322 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4323 if (!status)
4324 *stpp = stp;
4325 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004326}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004327
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004328static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4329 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004330{
4331 __be32 status;
4332 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004334 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004335 NFS4_OPEN_STID, stpp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004336 if (status)
4337 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004338 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004339 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07004340 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004341 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342}
4343
Al Virob37ad282006-10-19 23:28:59 -07004344__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004345nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004346 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347{
Al Virob37ad282006-10-19 23:28:59 -07004348 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004349 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004350 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004351 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
Al Viroa6a9f182013-09-16 10:57:01 -04004353 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4354 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004356 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004357 if (status)
4358 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
4360 nfs4_lock_state();
4361
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004362 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004363 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004364 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004365 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004366 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004367 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004368 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004369 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004370 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004371 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004372 update_stateid(&stp->st_stid.sc_stateid);
4373 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004374 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004375 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004376
Jeff Layton2a4317c2012-03-21 16:42:43 -04004377 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004378 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004380 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004381 if (!cstate->replay_owner)
4382 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 return status;
4384}
4385
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004386static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004388 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004389 return;
Jeff Layton12659652014-07-10 14:07:28 -04004390 nfs4_file_put_access(stp->st_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004391 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004392}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004393
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004394static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4395{
4396 switch (to_access) {
4397 case NFS4_SHARE_ACCESS_READ:
4398 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4399 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4400 break;
4401 case NFS4_SHARE_ACCESS_WRITE:
4402 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4403 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4404 break;
4405 case NFS4_SHARE_ACCESS_BOTH:
4406 break;
4407 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004408 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 }
4410}
4411
Al Virob37ad282006-10-19 23:28:59 -07004412__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004413nfsd4_open_downgrade(struct svc_rqst *rqstp,
4414 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004415 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416{
Al Virob37ad282006-10-19 23:28:59 -07004417 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004418 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004419 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
Al Viroa6a9f182013-09-16 10:57:01 -04004421 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4422 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004424 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004425 if (od->od_deleg_want)
4426 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4427 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428
4429 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004430 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004431 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004432 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004435 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004436 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 stp->st_access_bmap, od->od_share_access);
4438 goto out;
4439 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004440 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004441 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 stp->st_deny_bmap, od->od_share_deny);
4443 goto out;
4444 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004445 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
Jeff Laytonce0fc432012-05-11 09:45:14 -04004447 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004449 update_stateid(&stp->st_stid.sc_stateid);
4450 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 status = nfs_ok;
4452out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004453 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004454 if (!cstate->replay_owner)
4455 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 return status;
4457}
4458
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004459static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4460{
Trond Myklebustacf92952014-06-30 11:48:37 -04004461 struct nfs4_client *clp = s->st_stid.sc_client;
4462 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4463
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004464 s->st_stid.sc_type = NFS4_CLOSED_STID;
Trond Myklebustacf92952014-06-30 11:48:37 -04004465 unhash_open_stateid(s);
4466
4467 if (clp->cl_minorversion) {
4468 free_generic_stateid(s);
4469 if (list_empty(&oo->oo_owner.so_stateids))
4470 release_openowner(oo);
4471 } else {
4472 oo->oo_last_closed_stid = s;
4473 /*
4474 * In the 4.0 case we need to keep the owners around a
4475 * little while to handle CLOSE replay.
4476 */
4477 if (list_empty(&oo->oo_owner.so_stateids))
4478 move_to_close_lru(oo, clp->net);
4479 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004480}
4481
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482/*
4483 * nfs4_unlock_state() called after encode
4484 */
Al Virob37ad282006-10-19 23:28:59 -07004485__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004486nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004487 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488{
Al Virob37ad282006-10-19 23:28:59 -07004489 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004490 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004491 struct net *net = SVC_NET(rqstp);
4492 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493
Al Viroa6a9f182013-09-16 10:57:01 -04004494 dprintk("NFSD: nfsd4_close on file %pd\n",
4495 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
4497 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004498 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4499 &close->cl_stateid,
4500 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004501 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004502 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004503 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004505 update_stateid(&stp->st_stid.sc_stateid);
4506 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004508 nfsd4_close_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004510 if (!cstate->replay_owner)
4511 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 return status;
4513}
4514
Al Virob37ad282006-10-19 23:28:59 -07004515__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004516nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4517 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004519 struct nfs4_delegation *dp;
4520 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004521 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004522 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004523 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004525 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004526 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527
4528 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004529 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004530 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004531 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004532 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004533 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004534 if (status)
4535 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004536
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004537 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004539 nfs4_unlock_state();
4540
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 return status;
4542}
4543
4544
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546
Benny Halevy87df4de2008-12-15 19:42:03 +02004547static inline u64
4548end_offset(u64 start, u64 len)
4549{
4550 u64 end;
4551
4552 end = start + len;
4553 return end >= start ? end: NFS4_MAX_UINT64;
4554}
4555
4556/* last octet in a range */
4557static inline u64
4558last_byte_offset(u64 start, u64 len)
4559{
4560 u64 end;
4561
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004562 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004563 end = start + len;
4564 return end > start ? end - 1: NFS4_MAX_UINT64;
4565}
4566
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567/*
4568 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4569 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4570 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4571 * locking, this prevents us from being completely protocol-compliant. The
4572 * real solution to this problem is to start using unsigned file offsets in
4573 * the VFS, but this is a very deep change!
4574 */
4575static inline void
4576nfs4_transform_lock_offset(struct file_lock *lock)
4577{
4578 if (lock->fl_start < 0)
4579 lock->fl_start = OFFSET_MAX;
4580 if (lock->fl_end < 0)
4581 lock->fl_end = OFFSET_MAX;
4582}
4583
NeilBrownd5b90262006-04-10 22:55:22 -07004584/* Hack!: For now, we're defining this just so we can use a pointer to it
4585 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004586static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004587};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588
4589static inline void
4590nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4591{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004592 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593
NeilBrownd5b90262006-04-10 22:55:22 -07004594 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004595 lo = (struct nfs4_lockowner *) fl->fl_owner;
4596 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4597 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004598 if (!deny->ld_owner.data)
4599 /* We just don't care that much */
4600 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004601 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4602 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004603 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004604nevermind:
4605 deny->ld_owner.len = 0;
4606 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004607 deny->ld_clientid.cl_boot = 0;
4608 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 }
4610 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004611 deny->ld_length = NFS4_MAX_UINT64;
4612 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4614 deny->ld_type = NFS4_READ_LT;
4615 if (fl->fl_type != F_RDLCK)
4616 deny->ld_type = NFS4_WRITE_LT;
4617}
4618
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004619static struct nfs4_lockowner *
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004620find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4621 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622{
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004623 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4624 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004626 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4627 if (so->so_is_open_owner)
4628 continue;
4629 if (!same_owner_str(so, owner, clid))
4630 continue;
4631 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 }
4633 return NULL;
4634}
4635
4636/*
4637 * Alloc a lock owner structure.
4638 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004639 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004641 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004643static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004644alloc_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 -04004645 struct nfs4_lockowner *lo;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004646 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004648 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4649 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004651 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4652 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07004653 /* It is the openowner seqid that will be incremented in encode in the
4654 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004655 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004656 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004657 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658}
4659
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004660static struct nfs4_ol_stateid *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004661alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp,
4662 struct inode *inode,
4663 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004665 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004666 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
J. Bruce Fields996e0932011-10-17 11:14:48 -04004668 stp = nfs4_alloc_stateid(clp);
NeilBrown5ac049a2005-06-23 22:03:03 -07004669 if (stp == NULL)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04004670 return NULL;
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004671 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004672 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4673 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004674 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 stp->st_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004676 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004678 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004679 list_add(&stp->st_locks, &open_stp->st_locks);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004680 spin_lock(&fp->fi_lock);
4681 list_add(&stp->st_perfile, &fp->fi_stateids);
4682 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 return stp;
4684}
4685
Jeff Laytonc53530d2014-06-30 11:48:39 -04004686static struct nfs4_ol_stateid *
4687find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4688{
4689 struct nfs4_ol_stateid *lst;
4690
4691 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
4692 if (lst->st_file == fp)
4693 return lst;
4694 }
4695 return NULL;
4696}
4697
4698
NeilBrownfd39ca92005-06-23 22:04:03 -07004699static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700check_lock_length(u64 offset, u64 length)
4701{
Benny Halevy87df4de2008-12-15 19:42:03 +02004702 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 LOFF_OVERFLOW(offset, length)));
4704}
4705
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004706static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004707{
4708 struct nfs4_file *fp = lock_stp->st_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004709
Jeff Layton7214e862014-07-10 14:07:33 -04004710 lockdep_assert_held(&fp->fi_lock);
4711
Jeff Layton82c5ff12012-05-11 09:45:13 -04004712 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004713 return;
Jeff Layton12659652014-07-10 14:07:28 -04004714 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004715 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004716}
4717
J. Bruce Fields2355c592012-04-25 16:56:22 -04004718static __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 -04004719{
4720 struct nfs4_file *fi = ost->st_file;
4721 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4722 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004723 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004724 struct nfs4_lockowner *lo;
4725 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004726 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004727
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004728 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004729 if (!lo) {
4730 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4731 &lock->v.new.owner);
4732 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4733 if (lo == NULL)
4734 return nfserr_jukebox;
4735 } else {
4736 /* with an existing lockowner, seqids must be the same */
4737 if (!cstate->minorversion &&
4738 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004739 return nfserr_bad_seqid;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004740 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004741
4742 *lst = find_lock_stateid(lo, fi);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004743 if (*lst == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004744 *lst = alloc_init_lock_stateid(lo, fi, inode, ost);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004745 if (*lst == NULL) {
4746 release_lockowner_if_empty(lo);
4747 return nfserr_jukebox;
4748 }
4749 *new = true;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004750 }
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004751 return nfs_ok;
4752}
4753
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754/*
4755 * LOCK operation
4756 */
Al Virob37ad282006-10-19 23:28:59 -07004757__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004758nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004759 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004761 struct nfs4_openowner *open_sop = NULL;
4762 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004763 struct nfs4_ol_stateid *lock_stp;
Jeff Layton7214e862014-07-10 14:07:33 -04004764 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004765 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004766 struct file_lock *file_lock = NULL;
4767 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004768 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004769 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004770 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004771 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004772 struct net *net = SVC_NET(rqstp);
4773 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
4775 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4776 (long long) lock->lk_offset,
4777 (long long) lock->lk_length);
4778
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 if (check_lock_length(lock->lk_offset, lock->lk_length))
4780 return nfserr_inval;
4781
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004782 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004783 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004784 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4785 return status;
4786 }
4787
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 nfs4_lock_state();
4789
4790 if (lock->lk_is_new) {
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004791 struct nfs4_ol_stateid *open_stp = NULL;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004792
4793 if (nfsd4_has_session(cstate))
4794 /* See rfc 5661 18.10.3: given clientid is ignored: */
4795 memcpy(&lock->v.new.clientid,
4796 &cstate->session->se_client->cl_clientid,
4797 sizeof(clientid_t));
4798
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004800 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004804 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 lock->lk_new_open_seqid,
4806 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004807 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07004808 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004810 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004811 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004812 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004813 &lock->v.new.clientid))
4814 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004815 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4816 &lock_stp, &new_state);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004817 } else
Benny Halevydd453df2009-04-03 08:28:41 +03004818 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004819 lock->lk_old_lock_seqid,
4820 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004821 NFS4_LOCK_STID, &lock_stp, nn);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004822 if (status)
4823 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004824 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004826 lkflg = setlkflg(lock->lk_type);
4827 status = nfs4_check_openmode(lock_stp, lkflg);
4828 if (status)
4829 goto out;
4830
NeilBrown0dd395d2005-07-07 17:59:15 -07004831 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004832 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004833 goto out;
4834 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004835 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004836 goto out;
4837
Jeff Layton21179d82012-08-21 08:03:32 -04004838 file_lock = locks_alloc_lock();
4839 if (!file_lock) {
4840 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4841 status = nfserr_jukebox;
4842 goto out;
4843 }
4844
Jeff Layton7214e862014-07-10 14:07:33 -04004845 fp = lock_stp->st_file;
Jeff Layton21179d82012-08-21 08:03:32 -04004846 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 switch (lock->lk_type) {
4848 case NFS4_READ_LT:
4849 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004850 spin_lock(&fp->fi_lock);
4851 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004852 if (filp)
4853 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04004854 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004855 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004856 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 case NFS4_WRITE_LT:
4858 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004859 spin_lock(&fp->fi_lock);
4860 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004861 if (filp)
4862 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04004863 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004864 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004865 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 default:
4867 status = nfserr_inval;
4868 goto out;
4869 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004870 if (!filp) {
4871 status = nfserr_openmode;
4872 goto out;
4873 }
Jeff Layton21179d82012-08-21 08:03:32 -04004874 file_lock->fl_owner = (fl_owner_t)lock_sop;
4875 file_lock->fl_pid = current->tgid;
4876 file_lock->fl_file = filp;
4877 file_lock->fl_flags = FL_POSIX;
4878 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4879 file_lock->fl_start = lock->lk_offset;
4880 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4881 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882
Jeff Layton21179d82012-08-21 08:03:32 -04004883 conflock = locks_alloc_lock();
4884 if (!conflock) {
4885 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4886 status = nfserr_jukebox;
4887 goto out;
4888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889
Jeff Layton21179d82012-08-21 08:03:32 -04004890 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004891 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004893 update_stateid(&lock_stp->st_stid.sc_stateid);
4894 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004896 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004897 break;
4898 case (EAGAIN): /* conflock holds conflicting lock */
4899 status = nfserr_denied;
4900 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04004901 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004902 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 case (EDEADLK):
4904 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004905 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004906 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004907 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004908 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004909 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911out:
Trond Myklebustde186432014-07-10 14:07:26 -04004912 if (filp)
4913 fput(filp);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004914 if (status && new_state)
Jeff Laytonc53530d2014-06-30 11:48:39 -04004915 release_lock_stateid(lock_stp);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004916 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004917 if (!cstate->replay_owner)
4918 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004919 if (file_lock)
4920 locks_free_lock(file_lock);
4921 if (conflock)
4922 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 return status;
4924}
4925
4926/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004927 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4928 * so we do a temporary open here just to get an open file to pass to
4929 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4930 * inode operation.)
4931 */
Al Viro04da6e92012-04-13 00:00:04 -04004932static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004933{
4934 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04004935 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4936 if (!err) {
4937 err = nfserrno(vfs_test_lock(file, lock));
4938 nfsd_close(file);
4939 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004940 return err;
4941}
4942
4943/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 * LOCKT operation
4945 */
Al Virob37ad282006-10-19 23:28:59 -07004946__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004947nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4948 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949{
Jeff Layton21179d82012-08-21 08:03:32 -04004950 struct file_lock *file_lock = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004951 struct nfs4_lockowner *lo;
Al Virob37ad282006-10-19 23:28:59 -07004952 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004953 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004955 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 return nfserr_grace;
4957
4958 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4959 return nfserr_inval;
4960
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 nfs4_lock_state();
4962
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004963 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004964 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004965 if (status)
4966 goto out;
4967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004969 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971
Jeff Layton21179d82012-08-21 08:03:32 -04004972 file_lock = locks_alloc_lock();
4973 if (!file_lock) {
4974 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4975 status = nfserr_jukebox;
4976 goto out;
4977 }
4978 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 switch (lockt->lt_type) {
4980 case NFS4_READ_LT:
4981 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004982 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 break;
4984 case NFS4_WRITE_LT:
4985 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004986 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 break;
4988 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04004989 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 status = nfserr_inval;
4991 goto out;
4992 }
4993
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004994 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004995 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04004996 file_lock->fl_owner = (fl_owner_t)lo;
4997 file_lock->fl_pid = current->tgid;
4998 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999
Jeff Layton21179d82012-08-21 08:03:32 -04005000 file_lock->fl_start = lockt->lt_offset;
5001 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002
Jeff Layton21179d82012-08-21 08:03:32 -04005003 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004
Jeff Layton21179d82012-08-21 08:03:32 -04005005 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005006 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005007 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005008
Jeff Layton21179d82012-08-21 08:03:32 -04005009 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005011 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 }
5013out:
5014 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005015 if (file_lock)
5016 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 return status;
5018}
5019
Al Virob37ad282006-10-19 23:28:59 -07005020__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005021nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005022 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005024 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005026 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005027 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005028 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005029 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5030
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5032 (long long) locku->lu_offset,
5033 (long long) locku->lu_length);
5034
5035 if (check_lock_length(locku->lu_offset, locku->lu_length))
5036 return nfserr_inval;
5037
5038 nfs4_lock_state();
5039
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005040 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005041 &locku->lu_stateid, NFS4_LOCK_STID,
5042 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005043 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005045 filp = find_any_file(stp->st_file);
5046 if (!filp) {
5047 status = nfserr_lock_range;
5048 goto out;
5049 }
Jeff Layton21179d82012-08-21 08:03:32 -04005050 file_lock = locks_alloc_lock();
5051 if (!file_lock) {
5052 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5053 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005054 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005055 }
5056 locks_init_lock(file_lock);
5057 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005058 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005059 file_lock->fl_pid = current->tgid;
5060 file_lock->fl_file = filp;
5061 file_lock->fl_flags = FL_POSIX;
5062 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5063 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064
Jeff Layton21179d82012-08-21 08:03:32 -04005065 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5066 locku->lu_length);
5067 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068
Jeff Layton21179d82012-08-21 08:03:32 -04005069 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005070 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005071 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 goto out_nfserr;
5073 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005074 update_stateid(&stp->st_stid.sc_stateid);
5075 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005076fput:
5077 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005079 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04005080 if (!cstate->replay_owner)
5081 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005082 if (file_lock)
5083 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 return status;
5085
5086out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005087 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005088 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089}
5090
5091/*
5092 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005093 * true: locks held by lockowner
5094 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005096static bool
5097check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098{
5099 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005100 int status = false;
5101 struct file *filp = find_any_file(fp);
5102 struct inode *inode;
5103
5104 if (!filp) {
5105 /* Any valid lock stateid should have some sort of access */
5106 WARN_ON_ONCE(1);
5107 return status;
5108 }
5109
5110 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111
Jeff Layton1c8c6012013-06-21 08:58:15 -04005112 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005114 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005115 status = true;
5116 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005119 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005120 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 return status;
5122}
5123
Al Virob37ad282006-10-19 23:28:59 -07005124__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005125nfsd4_release_lockowner(struct svc_rqst *rqstp,
5126 struct nfsd4_compound_state *cstate,
5127 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128{
5129 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Laytonfd449072014-06-30 11:48:41 -04005130 struct nfs4_stateowner *sop = NULL, *tmp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005131 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005132 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 struct xdr_netobj *owner = &rlockowner->rl_owner;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005134 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07005135 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005136 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137
5138 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5139 clid->cl_boot, clid->cl_id);
5140
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 nfs4_lock_state();
5142
Jeff Layton4b24ca72014-06-30 11:48:44 -04005143 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005144 if (status)
5145 goto out;
5146
NeilBrown3e9e3db2005-06-23 22:04:20 -07005147 status = nfserr_locks_held;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05005148
Jeff Laytonfd449072014-06-30 11:48:41 -04005149 /* Find the matching lock stateowner */
5150 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
5151 if (tmp->so_is_open_owner)
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005152 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005153 if (same_owner_str(tmp, owner, clid)) {
5154 sop = tmp;
5155 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07005157 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005158
5159 /* No matching owner found, maybe a replay? Just declare victory... */
5160 if (!sop) {
5161 status = nfs_ok;
5162 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005164
5165 lo = lockowner(sop);
5166 /* see if there are still any locks associated with it */
5167 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5168 if (check_for_locks(stp->st_file, lo))
5169 goto out;
5170 }
5171
5172 status = nfs_ok;
5173 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174out:
5175 nfs4_unlock_state();
5176 return status;
5177}
5178
5179static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005180alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181{
NeilBrowna55370a2005-06-23 22:03:52 -07005182 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183}
5184
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005185bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005186nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005187{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005188 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005189
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005190 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005191 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005192}
5193
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194/*
5195 * failure => all reset bets are off, nfserr_no_grace...
5196 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005197struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005198nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199{
5200 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005201 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202
NeilBrowna55370a2005-06-23 22:03:52 -07005203 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5204 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005205 if (crp) {
5206 strhashval = clientstr_hashval(name);
5207 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005208 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005209 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005210 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005211 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005212 }
5213 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214}
5215
Jeff Layton2a4317c2012-03-21 16:42:43 -04005216void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005217nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005218{
5219 list_del(&crp->cr_strhash);
5220 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005221 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005222}
5223
5224void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005225nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226{
5227 struct nfs4_client_reclaim *crp = NULL;
5228 int i;
5229
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005231 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5232 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005234 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 }
5236 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005237 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238}
5239
5240/*
5241 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005242struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005243nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244{
5245 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 struct nfs4_client_reclaim *crp = NULL;
5247
Jeff Layton278c9312012-11-12 15:00:52 -05005248 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249
Jeff Layton278c9312012-11-12 15:00:52 -05005250 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005251 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005252 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 return crp;
5254 }
5255 }
5256 return NULL;
5257}
5258
5259/*
5260* Called from OPEN. Look for clientid in reclaim list.
5261*/
Al Virob37ad282006-10-19 23:28:59 -07005262__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005263nfs4_check_open_reclaim(clientid_t *clid,
5264 struct nfsd4_compound_state *cstate,
5265 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005267 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005268
5269 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005270 status = lookup_clientid(clid, cstate, nn);
5271 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005272 return nfserr_reclaim_bad;
5273
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005274 if (nfsd4_client_record_check(cstate->clp))
5275 return nfserr_reclaim_bad;
5276
5277 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278}
5279
Bryan Schumaker65178db2011-11-01 13:35:21 -04005280#ifdef CONFIG_NFSD_FAULT_INJECTION
5281
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005282u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5283{
J. Bruce Fields221a6872013-04-01 22:23:49 -04005284 if (mark_client_expired(clp))
5285 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005286 expire_client(clp);
5287 return 1;
5288}
5289
Bryan Schumaker184c1842012-11-29 11:40:44 -05005290u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5291{
5292 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005293 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005294 printk(KERN_INFO "NFS Client: %s\n", buf);
5295 return 1;
5296}
5297
5298static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5299 const char *type)
5300{
5301 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005302 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005303 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5304}
5305
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005306static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5307 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005308{
5309 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005310 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005311 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005312 u64 count = 0;
5313
5314 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005315 list_for_each_entry_safe(stp, st_next,
5316 &oop->oo_owner.so_stateids, st_perstateowner) {
5317 list_for_each_entry_safe(lst, lst_next,
5318 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005319 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005320 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005321 if (++count == max)
5322 return count;
5323 }
5324 }
5325 }
5326
5327 return count;
5328}
5329
5330u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5331{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005332 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005333}
5334
Bryan Schumaker184c1842012-11-29 11:40:44 -05005335u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5336{
5337 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5338 nfsd_print_count(clp, count, "locked files");
5339 return count;
5340}
5341
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005342static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5343{
5344 struct nfs4_openowner *oop, *next;
5345 u64 count = 0;
5346
5347 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5348 if (func)
5349 func(oop);
5350 if (++count == max)
5351 break;
5352 }
5353
5354 return count;
5355}
5356
5357u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5358{
5359 return nfsd_foreach_client_open(clp, max, release_openowner);
5360}
5361
Bryan Schumaker184c1842012-11-29 11:40:44 -05005362u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5363{
5364 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5365 nfsd_print_count(clp, count, "open files");
5366 return count;
5367}
5368
Bryan Schumaker269de302012-11-29 11:40:42 -05005369static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5370 struct list_head *victims)
5371{
5372 struct nfs4_delegation *dp, *next;
5373 u64 count = 0;
5374
Benny Halevycdc97502014-05-30 09:09:30 -04005375 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005376 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005377 if (victims) {
5378 /*
5379 * It's not safe to mess with delegations that have a
5380 * non-zero dl_time. They might have already been broken
5381 * and could be processed by the laundromat outside of
5382 * the state_lock. Just leave them be.
5383 */
5384 if (dp->dl_time != 0)
5385 continue;
5386
5387 /*
5388 * Increment dl_time to ensure that delegation breaks
5389 * don't monkey with it now that we are.
5390 */
5391 ++dp->dl_time;
Bryan Schumaker269de302012-11-29 11:40:42 -05005392 list_move(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005393 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005394 if (++count == max)
5395 break;
5396 }
5397 return count;
5398}
5399
5400u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5401{
5402 struct nfs4_delegation *dp, *next;
5403 LIST_HEAD(victims);
5404 u64 count;
5405
Benny Halevycdc97502014-05-30 09:09:30 -04005406 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005407 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005408 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005409
5410 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005411 revoke_delegation(dp);
Bryan Schumaker269de302012-11-29 11:40:42 -05005412
5413 return count;
5414}
5415
5416u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5417{
Jeff Laytondff13992014-07-08 14:02:49 -04005418 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005419 LIST_HEAD(victims);
5420 u64 count;
5421
Benny Halevycdc97502014-05-30 09:09:30 -04005422 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005423 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005424 while (!list_empty(&victims)) {
5425 dp = list_first_entry(&victims, struct nfs4_delegation,
5426 dl_recall_lru);
5427 list_del_init(&dp->dl_recall_lru);
5428 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005429 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005430 }
Benny Halevycdc97502014-05-30 09:09:30 -04005431 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005432
5433 return count;
5434}
5435
Bryan Schumaker184c1842012-11-29 11:40:44 -05005436u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5437{
5438 u64 count = 0;
5439
Benny Halevycdc97502014-05-30 09:09:30 -04005440 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005441 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005442 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005443
5444 nfsd_print_count(clp, count, "delegations");
5445 return count;
5446}
5447
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005448u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005449{
5450 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005451 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005452 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005453
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005454 if (!nfsd_netns_ready(nn))
5455 return 0;
5456
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005457 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005458 count += func(clp, max - count);
5459 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005460 break;
5461 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005462
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005463 return count;
5464}
5465
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005466struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5467{
5468 struct nfs4_client *clp;
5469 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5470
5471 if (!nfsd_netns_ready(nn))
5472 return NULL;
5473
5474 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5475 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5476 return clp;
5477 }
5478 return NULL;
5479}
5480
Bryan Schumaker65178db2011-11-01 13:35:21 -04005481#endif /* CONFIG_NFSD_FAULT_INJECTION */
5482
Meelap Shahc2f1a552007-07-17 04:04:39 -07005483/*
5484 * Since the lifetime of a delegation isn't limited to that of an open, a
5485 * client may quite reasonably hang on to a delegation as long as it has
5486 * the inode cached. This becomes an obvious problem the first time a
5487 * client's inode cache approaches the size of the server's total memory.
5488 *
5489 * For now we avoid this problem by imposing a hard limit on the number
5490 * of delegations, which varies according to the server's memory size.
5491 */
5492static void
5493set_max_delegations(void)
5494{
5495 /*
5496 * Allow at most 4 delegations per megabyte of RAM. Quick
5497 * estimates suggest that in the worst case (where every delegation
5498 * is for a different inode), a delegation could take about 1.5K,
5499 * giving a worst case usage of about 6% of memory.
5500 */
5501 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5502}
5503
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005504static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005505{
5506 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5507 int i;
5508
5509 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5510 CLIENT_HASH_SIZE, GFP_KERNEL);
5511 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005512 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005513 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5514 CLIENT_HASH_SIZE, GFP_KERNEL);
5515 if (!nn->unconf_id_hashtbl)
5516 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005517 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5518 OWNER_HASH_SIZE, GFP_KERNEL);
5519 if (!nn->ownerstr_hashtbl)
5520 goto err_ownerstr;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005521 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5522 SESSION_HASH_SIZE, GFP_KERNEL);
5523 if (!nn->sessionid_hashtbl)
5524 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005525
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005526 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005527 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005528 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005529 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005530 for (i = 0; i < OWNER_HASH_SIZE; i++)
5531 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005532 for (i = 0; i < SESSION_HASH_SIZE; i++)
5533 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005534 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005535 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005536 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005537 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005538 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005539 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005540
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005541 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005542 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005543
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005544 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005545
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005546err_sessionid:
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03005547 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005548err_ownerstr:
5549 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005550err_unconf_id:
5551 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005552err:
5553 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005554}
5555
5556static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005557nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005558{
5559 int i;
5560 struct nfs4_client *clp = NULL;
5561 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5562
5563 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5564 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5565 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5566 destroy_client(clp);
5567 }
5568 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005569
Kinglong Mee2b905632014-03-26 22:09:30 +08005570 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5571 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5572 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5573 destroy_client(clp);
5574 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005575 }
5576
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005577 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005578 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005579 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005580 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005581 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005582}
5583
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005584int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005585nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005586{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005587 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005588 int ret;
5589
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005590 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005591 if (ret)
5592 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005593 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005594 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005595 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005596 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005597 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005598 nn->nfsd4_grace, net);
5599 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005600 return 0;
5601}
5602
5603/* initialization to perform when the nfsd service is started: */
5604
5605int
5606nfs4_state_start(void)
5607{
5608 int ret;
5609
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005610 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005611 if (ret)
5612 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005613 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005614 if (laundry_wq == NULL) {
5615 ret = -ENOMEM;
5616 goto out_recovery;
5617 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005618 ret = nfsd4_create_callback_queue();
5619 if (ret)
5620 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005621
Meelap Shahc2f1a552007-07-17 04:04:39 -07005622 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005623
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005624 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005625
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005626out_free_laundry:
5627 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005628out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005629 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630}
5631
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005632void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005633nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005637 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005639 cancel_delayed_work_sync(&nn->laundromat_work);
5640 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005641
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005642 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04005644 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005645 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5647 list_move(&dp->dl_recall_lru, &reaplist);
5648 }
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);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005652 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 }
5654
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005655 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005656 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005657 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658}
5659
5660void
5661nfs4_state_shutdown(void)
5662{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005663 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005664 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005666
5667static void
5668get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5669{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005670 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5671 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005672}
5673
5674static void
5675put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5676{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005677 if (cstate->minorversion) {
5678 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5679 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5680 }
5681}
5682
5683void
5684clear_current_stateid(struct nfsd4_compound_state *cstate)
5685{
5686 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005687}
5688
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005689/*
5690 * functions to set current state id
5691 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005692void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005693nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5694{
5695 put_stateid(cstate, &odp->od_stateid);
5696}
5697
5698void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005699nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5700{
5701 put_stateid(cstate, &open->op_stateid);
5702}
5703
5704void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005705nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5706{
5707 put_stateid(cstate, &close->cl_stateid);
5708}
5709
5710void
5711nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5712{
5713 put_stateid(cstate, &lock->lk_resp_stateid);
5714}
5715
5716/*
5717 * functions to consume current state id
5718 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005719
5720void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005721nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5722{
5723 get_stateid(cstate, &odp->od_stateid);
5724}
5725
5726void
5727nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5728{
5729 get_stateid(cstate, &drp->dr_stateid);
5730}
5731
5732void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005733nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5734{
5735 get_stateid(cstate, &fsp->fr_stateid);
5736}
5737
5738void
5739nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5740{
5741 get_stateid(cstate, &setattr->sa_stateid);
5742}
5743
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005744void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005745nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5746{
5747 get_stateid(cstate, &close->cl_stateid);
5748}
5749
5750void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005751nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005752{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005753 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005754}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01005755
5756void
5757nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5758{
5759 get_stateid(cstate, &read->rd_stateid);
5760}
5761
5762void
5763nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5764{
5765 get_stateid(cstate, &write->wr_stateid);
5766}