blob: 24065e1b2bb2005d2ed714547829134ce047f0ff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Chuck Lever7df302f2012-05-29 13:56:37 -040041#include <linux/ratelimit.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050042#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton59766872013-02-04 12:50:00 -050043#include <linux/sunrpc/addr.h>
NeilBrown6282cd52014-06-04 17:39:26 +100044#include <linux/hash.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020045#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040046#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050047#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040048#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040050#include "netns.h"
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define NFSDDBG_FACILITY NFSDDBG_PROC
53
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050054#define all_ones {{~0,~0},~0}
55static const stateid_t one_stateid = {
56 .si_generation = ~0,
57 .si_opaque = all_ones,
58};
59static const stateid_t zero_stateid = {
60 /* all fields zero */
61};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010062static const stateid_t currentstateid = {
63 .si_generation = 1,
64};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050065
Andy Adamsonec6b5d72009-04-03 08:28:28 +030066static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070067
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050068#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
69#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010070#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040073static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
J. Bruce Fields8b671b82009-02-22 14:51:34 -080075/* Locking: */
76
77/* Currently used for almost all code touching nfsv4 state: */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080078static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
J. Bruce Fields8b671b82009-02-22 14:51:34 -080080/*
81 * Currently used for the del_recall_lru and file hash table. In an
82 * effort to decrease the scope of the client_mutex, this spinlock may
83 * eventually cover more:
84 */
Benny Halevycdc97502014-05-30 09:09:30 -040085static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080086
Christoph Hellwigabf11352014-05-21 07:43:03 -070087static struct kmem_cache *openowner_slab;
88static struct kmem_cache *lockowner_slab;
89static struct kmem_cache *file_slab;
90static struct kmem_cache *stateid_slab;
91static struct kmem_cache *deleg_slab;
NeilBrowne60d4392005-06-23 22:03:01 -070092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093void
94nfs4_lock_state(void)
95{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080096 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -040099static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800100
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400101static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800102{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400103 return ses->se_flags & NFS4_SESSION_DEAD;
104}
105
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400106static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
107{
108 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400109 return nfserr_jukebox;
110 ses->se_flags |= NFS4_SESSION_DEAD;
111 return nfs_ok;
112}
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114void
115nfs4_unlock_state(void)
116{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800117 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
J. Bruce Fields221a6872013-04-01 22:23:49 -0400120static bool is_client_expired(struct nfs4_client *clp)
121{
122 return clp->cl_time == 0;
123}
124
125static __be32 mark_client_expired_locked(struct nfs4_client *clp)
126{
127 if (atomic_read(&clp->cl_refcount))
128 return nfserr_jukebox;
129 clp->cl_time = 0;
130 return nfs_ok;
131}
132
133static __be32 mark_client_expired(struct nfs4_client *clp)
134{
135 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
136 __be32 ret;
137
138 spin_lock(&nn->client_lock);
139 ret = mark_client_expired_locked(clp);
140 spin_unlock(&nn->client_lock);
141 return ret;
142}
143
144static __be32 get_client_locked(struct nfs4_client *clp)
145{
146 if (is_client_expired(clp))
147 return nfserr_expired;
148 atomic_inc(&clp->cl_refcount);
149 return nfs_ok;
150}
151
152/* must be called under the client_lock */
153static inline void
154renew_client_locked(struct nfs4_client *clp)
155{
156 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
157
158 if (is_client_expired(clp)) {
159 WARN_ON(1);
160 printk("%s: client (clientid %08x/%08x) already expired\n",
161 __func__,
162 clp->cl_clientid.cl_boot,
163 clp->cl_clientid.cl_id);
164 return;
165 }
166
167 dprintk("renewing client (clientid %08x/%08x)\n",
168 clp->cl_clientid.cl_boot,
169 clp->cl_clientid.cl_id);
170 list_move_tail(&clp->cl_lru, &nn->client_lru);
171 clp->cl_time = get_seconds();
172}
173
174static inline void
175renew_client(struct nfs4_client *clp)
176{
177 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
178
179 spin_lock(&nn->client_lock);
180 renew_client_locked(clp);
181 spin_unlock(&nn->client_lock);
182}
183
Fengguang Wuba138432013-04-16 22:14:15 -0400184static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400185{
186 if (!atomic_dec_and_test(&clp->cl_refcount))
187 return;
188 if (!is_client_expired(clp))
189 renew_client_locked(clp);
190}
191
Jeff Layton4b24ca72014-06-30 11:48:44 -0400192static void put_client_renew(struct nfs4_client *clp)
193{
194 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
195
Jeff Laytond6c249b2014-07-08 14:02:50 -0400196 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
197 return;
198 if (!is_client_expired(clp))
199 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400200 spin_unlock(&nn->client_lock);
201}
202
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400203static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
204{
205 __be32 status;
206
207 if (is_session_dead(ses))
208 return nfserr_badsession;
209 status = get_client_locked(ses->se_client);
210 if (status)
211 return status;
212 atomic_inc(&ses->se_ref);
213 return nfs_ok;
214}
215
216static void nfsd4_put_session_locked(struct nfsd4_session *ses)
217{
218 struct nfs4_client *clp = ses->se_client;
219
220 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
221 free_session(ses);
222 put_client_renew_locked(clp);
223}
224
225static void nfsd4_put_session(struct nfsd4_session *ses)
226{
227 struct nfs4_client *clp = ses->se_client;
228 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
229
230 spin_lock(&nn->client_lock);
231 nfsd4_put_session_locked(ses);
232 spin_unlock(&nn->client_lock);
233}
234
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236static inline u32
237opaque_hashval(const void *ptr, int nbytes)
238{
239 unsigned char *cptr = (unsigned char *) ptr;
240
241 u32 x = 0;
242 while (nbytes--) {
243 x *= 37;
244 x += *cptr++;
245 }
246 return x;
247}
248
J. Bruce Fields32513b42011-10-13 16:00:16 -0400249static void nfsd4_free_file(struct nfs4_file *f)
250{
251 kmem_cache_free(file_slab, f);
252}
253
NeilBrown13cd2182005-06-23 22:03:10 -0700254static inline void
255put_nfs4_file(struct nfs4_file *fi)
256{
Jeff Layton02e12152014-07-16 10:31:57 -0400257 might_lock(&state_lock);
258
Benny Halevycdc97502014-05-30 09:09:30 -0400259 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton89876f82013-04-02 09:01:59 -0400260 hlist_del(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400261 spin_unlock(&state_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400262 nfsd4_free_file(fi);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800263 }
NeilBrown13cd2182005-06-23 22:03:10 -0700264}
265
266static inline void
267get_nfs4_file(struct nfs4_file *fi)
268{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800269 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700270}
271
Trond Myklebustde186432014-07-10 14:07:26 -0400272static struct file *
273__nfs4_get_fd(struct nfs4_file *f, int oflag)
274{
275 if (f->fi_fds[oflag])
276 return get_file(f->fi_fds[oflag]);
277 return NULL;
278}
279
280static struct file *
281find_writeable_file_locked(struct nfs4_file *f)
282{
283 struct file *ret;
284
285 lockdep_assert_held(&f->fi_lock);
286
287 ret = __nfs4_get_fd(f, O_WRONLY);
288 if (!ret)
289 ret = __nfs4_get_fd(f, O_RDWR);
290 return ret;
291}
292
293static struct file *
294find_writeable_file(struct nfs4_file *f)
295{
296 struct file *ret;
297
298 spin_lock(&f->fi_lock);
299 ret = find_writeable_file_locked(f);
300 spin_unlock(&f->fi_lock);
301
302 return ret;
303}
304
305static struct file *find_readable_file_locked(struct nfs4_file *f)
306{
307 struct file *ret;
308
309 lockdep_assert_held(&f->fi_lock);
310
311 ret = __nfs4_get_fd(f, O_RDONLY);
312 if (!ret)
313 ret = __nfs4_get_fd(f, O_RDWR);
314 return ret;
315}
316
317static struct file *
318find_readable_file(struct nfs4_file *f)
319{
320 struct file *ret;
321
322 spin_lock(&f->fi_lock);
323 ret = find_readable_file_locked(f);
324 spin_unlock(&f->fi_lock);
325
326 return ret;
327}
328
329static struct file *
330find_any_file(struct nfs4_file *f)
331{
332 struct file *ret;
333
334 spin_lock(&f->fi_lock);
335 ret = __nfs4_get_fd(f, O_RDWR);
336 if (!ret) {
337 ret = __nfs4_get_fd(f, O_WRONLY);
338 if (!ret)
339 ret = __nfs4_get_fd(f, O_RDONLY);
340 }
341 spin_unlock(&f->fi_lock);
342 return ret;
343}
344
Trond Myklebust02a35082014-07-25 07:34:22 -0400345static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800346unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700347
348/*
349 * Open owner state (share locks)
350 */
351
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500352/* hash tables for lock and open owners */
353#define OWNER_HASH_BITS 8
354#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
355#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700356
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500357static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400358{
359 unsigned int ret;
360
361 ret = opaque_hashval(ownername->data, ownername->len);
362 ret += clientid;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500363 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400364}
NeilBrownef0f3392006-04-10 22:55:41 -0700365
NeilBrownef0f3392006-04-10 22:55:41 -0700366/* hash table for nfs4_file */
367#define FILE_HASH_BITS 8
368#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800369
Trond Myklebustca943212014-07-23 16:17:39 -0400370static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400371{
Trond Myklebustca943212014-07-23 16:17:39 -0400372 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
373}
374
375static unsigned int file_hashval(struct knfsd_fh *fh)
376{
377 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
378}
379
380static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
381{
382 return fh1->fh_size == fh2->fh_size &&
383 !memcmp(fh1->fh_base.fh_pad,
384 fh2->fh_base.fh_pad,
385 fh1->fh_size);
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400386}
387
Jeff Layton89876f82013-04-02 09:01:59 -0400388static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700389
Jeff Layton12659652014-07-10 14:07:28 -0400390static void
391__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400392{
Jeff Layton7214e862014-07-10 14:07:33 -0400393 lockdep_assert_held(&fp->fi_lock);
394
Jeff Layton12659652014-07-10 14:07:28 -0400395 if (access & NFS4_SHARE_ACCESS_WRITE)
396 atomic_inc(&fp->fi_access[O_WRONLY]);
397 if (access & NFS4_SHARE_ACCESS_READ)
398 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400399}
400
Jeff Layton12659652014-07-10 14:07:28 -0400401static __be32
402nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400403{
Jeff Layton7214e862014-07-10 14:07:33 -0400404 lockdep_assert_held(&fp->fi_lock);
405
Jeff Layton12659652014-07-10 14:07:28 -0400406 /* Does this access mode make sense? */
407 if (access & ~NFS4_SHARE_ACCESS_BOTH)
408 return nfserr_inval;
409
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400410 /* Does it conflict with a deny mode already set? */
411 if ((access & fp->fi_share_deny) != 0)
412 return nfserr_share_denied;
413
Jeff Layton12659652014-07-10 14:07:28 -0400414 __nfs4_file_get_access(fp, access);
415 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400416}
417
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400418static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
419{
420 /* Common case is that there is no deny mode. */
421 if (deny) {
422 /* Does this deny mode make sense? */
423 if (deny & ~NFS4_SHARE_DENY_BOTH)
424 return nfserr_inval;
425
426 if ((deny & NFS4_SHARE_DENY_READ) &&
427 atomic_read(&fp->fi_access[O_RDONLY]))
428 return nfserr_share_denied;
429
430 if ((deny & NFS4_SHARE_DENY_WRITE) &&
431 atomic_read(&fp->fi_access[O_WRONLY]))
432 return nfserr_share_denied;
433 }
434 return nfs_ok;
435}
436
J. Bruce Fields998db522010-08-07 09:21:41 -0400437static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400438{
Trond Myklebustde186432014-07-10 14:07:26 -0400439 might_lock(&fp->fi_lock);
440
441 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
442 struct file *f1 = NULL;
443 struct file *f2 = NULL;
444
Jeff Layton6d338b52014-07-10 14:07:29 -0400445 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400446 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400447 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400448 spin_unlock(&fp->fi_lock);
449 if (f1)
450 fput(f1);
451 if (f2)
452 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400453 }
454}
455
Jeff Layton12659652014-07-10 14:07:28 -0400456static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400457{
Jeff Layton12659652014-07-10 14:07:28 -0400458 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
459
460 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400461 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400462 if (access & NFS4_SHARE_ACCESS_READ)
463 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400464}
465
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500466static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct
467kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400468{
469 struct idr *stateids = &cl->cl_stateids;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500470 struct nfs4_stid *stid;
471 int new_id;
472
Trond Myklebustf8338832014-07-25 07:34:19 -0400473 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500474 if (!stid)
475 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400476
Jeff Layton398c33a2013-04-29 16:21:20 -0700477 new_id = idr_alloc_cyclic(stateids, stid, 0, 0, GFP_KERNEL);
Tejun Heoebd6c702013-03-13 14:59:37 -0700478 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500479 goto out_free;
480 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500481 stid->sc_stateid.si_opaque.so_id = new_id;
482 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
483 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400484 atomic_set(&stid->sc_count, 1);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500485
J. Bruce Fields996e0932011-10-17 11:14:48 -0400486 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500487 * It shouldn't be a problem to reuse an opaque stateid value.
488 * I don't think it is for 4.1. But with 4.0 I worry that, for
489 * example, a stray write retransmission could be accepted by
490 * the server when it should have been rejected. Therefore,
491 * adopt a trick from the sctp code to attempt to maximize the
492 * amount of time until an id is reused, by ensuring they always
493 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400494 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500495 return stid;
496out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800497 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500498 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400499}
500
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400501static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
502{
503 return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
504}
505
NeilBrown6282cd52014-06-04 17:39:26 +1000506/*
507 * When we recall a delegation, we should be careful not to hand it
508 * out again straight away.
509 * To ensure this we keep a pair of bloom filters ('new' and 'old')
510 * in which the filehandles of recalled delegations are "stored".
511 * If a filehandle appear in either filter, a delegation is blocked.
512 * When a delegation is recalled, the filehandle is stored in the "new"
513 * filter.
514 * Every 30 seconds we swap the filters and clear the "new" one,
515 * unless both are empty of course.
516 *
517 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
518 * low 3 bytes as hash-table indices.
519 *
520 * 'state_lock', which is always held when block_delegations() is called,
521 * is used to manage concurrent access. Testing does not need the lock
522 * except when swapping the two filters.
523 */
524static struct bloom_pair {
525 int entries, old_entries;
526 time_t swap_time;
527 int new; /* index into 'set' */
528 DECLARE_BITMAP(set[2], 256);
529} blocked_delegations;
530
531static int delegation_blocked(struct knfsd_fh *fh)
532{
533 u32 hash;
534 struct bloom_pair *bd = &blocked_delegations;
535
536 if (bd->entries == 0)
537 return 0;
538 if (seconds_since_boot() - bd->swap_time > 30) {
539 spin_lock(&state_lock);
540 if (seconds_since_boot() - bd->swap_time > 30) {
541 bd->entries -= bd->old_entries;
542 bd->old_entries = bd->entries;
543 memset(bd->set[bd->new], 0,
544 sizeof(bd->set[0]));
545 bd->new = 1-bd->new;
546 bd->swap_time = seconds_since_boot();
547 }
548 spin_unlock(&state_lock);
549 }
550 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
551 if (test_bit(hash&255, bd->set[0]) &&
552 test_bit((hash>>8)&255, bd->set[0]) &&
553 test_bit((hash>>16)&255, bd->set[0]))
554 return 1;
555
556 if (test_bit(hash&255, bd->set[1]) &&
557 test_bit((hash>>8)&255, bd->set[1]) &&
558 test_bit((hash>>16)&255, bd->set[1]))
559 return 1;
560
561 return 0;
562}
563
564static void block_delegations(struct knfsd_fh *fh)
565{
566 u32 hash;
567 struct bloom_pair *bd = &blocked_delegations;
568
Jeff Layton02e12152014-07-16 10:31:57 -0400569 lockdep_assert_held(&state_lock);
570
NeilBrown6282cd52014-06-04 17:39:26 +1000571 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
572
573 __set_bit(hash&255, bd->set[bd->new]);
574 __set_bit((hash>>8)&255, bd->set[bd->new]);
575 __set_bit((hash>>16)&255, bd->set[bd->new]);
576 if (bd->entries == 0)
577 bd->swap_time = seconds_since_boot();
578 bd->entries += 1;
579}
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581static struct nfs4_delegation *
Jeff Laytonf9416e22014-07-25 07:34:23 -0400582alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400585 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400588 n = atomic_long_inc_return(&num_delegations);
589 if (n < 0 || n > max_delegations)
590 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000591 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400592 goto out_dec;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400593 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700594 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400595 goto out_dec;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400596 /*
597 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400598 * meaning of seqid 0 isn't meaningful, really, but let's avoid
599 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400600 */
601 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700602 INIT_LIST_HEAD(&dp->dl_perfile);
603 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400605 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
J. Bruce Fields6c02eaa2009-02-02 17:30:51 -0500606 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
Jeff Layton02e12152014-07-16 10:31:57 -0400607 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400609out_dec:
610 atomic_long_dec(&num_delegations);
611 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
J. Bruce Fields68a33962013-03-21 11:21:50 -0400614static void remove_stid(struct nfs4_stid *s)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500615{
616 struct idr *stateids = &s->sc_client->cl_stateids;
617
618 idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500619}
620
Benny Halevy9857df82013-10-14 13:44:52 +0300621static void nfs4_free_stid(struct kmem_cache *slab, struct nfs4_stid *s)
622{
623 kmem_cache_free(slab, s);
624}
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626void
627nfs4_put_delegation(struct nfs4_delegation *dp)
628{
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400629 if (atomic_dec_and_test(&dp->dl_stid.sc_count)) {
Trond Myklebustf8338832014-07-25 07:34:19 -0400630 if (dp->dl_file)
631 put_nfs4_file(dp->dl_file);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400632 remove_stid(&dp->dl_stid);
Benny Halevy9857df82013-10-14 13:44:52 +0300633 nfs4_free_stid(deleg_slab, &dp->dl_stid);
Trond Myklebust02a35082014-07-25 07:34:22 -0400634 atomic_long_dec(&num_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636}
637
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500638static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Jeff Layton417c6622014-07-21 09:34:57 -0400640 lockdep_assert_held(&state_lock);
641
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -0500642 if (!fp->fi_lease)
643 return;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500644 if (atomic_dec_and_test(&fp->fi_delegees)) {
645 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
646 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400647 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500648 fp->fi_deleg_file = NULL;
649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400652static void unhash_stid(struct nfs4_stid *s)
653{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500654 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400655}
656
Benny Halevy931ee562014-05-30 09:09:27 -0400657static void
658hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
659{
Benny Halevycdc97502014-05-30 09:09:30 -0400660 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400661 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400662
Benny Halevy3fb87d12014-05-30 09:09:31 -0400663 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400664 list_add(&dp->dl_perfile, &fp->fi_delegations);
665 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
666}
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668static void
Jeff Layton42690672014-07-25 07:34:20 -0400669unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Jeff Layton02e12152014-07-16 10:31:57 -0400671 struct nfs4_file *fp = dp->dl_file;
672
Jeff Layton42690672014-07-25 07:34:20 -0400673 lockdep_assert_held(&state_lock);
674
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400675 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400676 /* Ensure that deleg break won't try to requeue it */
677 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400678 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400679 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400681 list_del_init(&dp->dl_perfile);
682 spin_unlock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400683 if (fp)
Trond Myklebustf8338832014-07-25 07:34:19 -0400684 nfs4_put_deleg_lease(fp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400685}
686
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400687static void destroy_delegation(struct nfs4_delegation *dp)
688{
Jeff Layton42690672014-07-25 07:34:20 -0400689 spin_lock(&state_lock);
690 unhash_delegation_locked(dp);
691 spin_unlock(&state_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400692 nfs4_put_delegation(dp);
693}
694
695static void revoke_delegation(struct nfs4_delegation *dp)
696{
697 struct nfs4_client *clp = dp->dl_stid.sc_client;
698
Jeff Layton2d4a5322014-07-25 07:34:21 -0400699 WARN_ON(!list_empty(&dp->dl_recall_lru));
700
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400701 if (clp->cl_minorversion == 0)
Jeff Layton2d4a5322014-07-25 07:34:21 -0400702 nfs4_put_delegation(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400703 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400704 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400705 spin_lock(&clp->cl_lock);
706 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
707 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400708 }
709}
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711/*
712 * SETCLIENTID state
713 */
714
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400715static unsigned int clientid_hashval(u32 id)
716{
717 return id & CLIENT_HASH_MASK;
718}
719
720static unsigned int clientstr_hashval(const char *name)
721{
722 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
723}
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400726 * We store the NONE, READ, WRITE, and BOTH bits separately in the
727 * st_{access,deny}_bmap field of the stateid, in order to track not
728 * only what share bits are currently in force, but also what
729 * combinations of share bits previous opens have used. This allows us
730 * to enforce the recommendation of rfc 3530 14.2.19 that the server
731 * return an error if the client attempt to downgrade to a combination
732 * of share bits not explicable by closing some of its previous opens.
733 *
734 * XXX: This enforcement is actually incomplete, since we don't keep
735 * track of access/deny bit combinations; so, e.g., we allow:
736 *
737 * OPEN allow read, deny write
738 * OPEN allow both, deny none
739 * DOWNGRADE allow read, deny none
740 *
741 * which we should reject.
742 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400743static unsigned int
744bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400745 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400746 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400747
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400748 for (i = 1; i < 4; i++) {
749 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400750 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400751 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400752 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400753}
754
Jeff Layton82c5ff12012-05-11 09:45:13 -0400755/* set share access for a given stateid */
756static inline void
757set_access(u32 access, struct nfs4_ol_stateid *stp)
758{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400759 unsigned char mask = 1 << access;
760
761 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
762 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400763}
764
765/* clear share access for a given stateid */
766static inline void
767clear_access(u32 access, struct nfs4_ol_stateid *stp)
768{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400769 unsigned char mask = 1 << access;
770
771 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
772 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400773}
774
775/* test whether a given stateid has access */
776static inline bool
777test_access(u32 access, struct nfs4_ol_stateid *stp)
778{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400779 unsigned char mask = 1 << access;
780
781 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400782}
783
Jeff Laytonce0fc432012-05-11 09:45:14 -0400784/* set share deny for a given stateid */
785static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400786set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400787{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400788 unsigned char mask = 1 << deny;
789
790 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
791 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400792}
793
794/* clear share deny for a given stateid */
795static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400796clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400797{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400798 unsigned char mask = 1 << deny;
799
800 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
801 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400802}
803
804/* test whether a given stateid is denying specific access */
805static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400806test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400807{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400808 unsigned char mask = 1 << deny;
809
810 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400811}
812
813static int nfs4_access_to_omode(u32 access)
814{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400815 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400816 case NFS4_SHARE_ACCESS_READ:
817 return O_RDONLY;
818 case NFS4_SHARE_ACCESS_WRITE:
819 return O_WRONLY;
820 case NFS4_SHARE_ACCESS_BOTH:
821 return O_RDWR;
822 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500823 WARN_ON_ONCE(1);
824 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400825}
826
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400827/*
828 * A stateid that had a deny mode associated with it is being released
829 * or downgraded. Recalculate the deny mode on the file.
830 */
831static void
832recalculate_deny_mode(struct nfs4_file *fp)
833{
834 struct nfs4_ol_stateid *stp;
835
836 spin_lock(&fp->fi_lock);
837 fp->fi_share_deny = 0;
838 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
839 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
840 spin_unlock(&fp->fi_lock);
841}
842
843static void
844reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
845{
846 int i;
847 bool change = false;
848
849 for (i = 1; i < 4; i++) {
850 if ((i & deny) != i) {
851 change = true;
852 clear_deny(i, stp);
853 }
854 }
855
856 /* Recalculate per-file deny mode if there was a change */
857 if (change)
858 recalculate_deny_mode(stp->st_file);
859}
860
Jeff Layton82c5ff12012-05-11 09:45:13 -0400861/* release all access and file references for a given stateid */
862static void
863release_all_access(struct nfs4_ol_stateid *stp)
864{
865 int i;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400866 struct nfs4_file *fp = stp->st_file;
867
868 if (fp && stp->st_deny_bmap != 0)
869 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400870
871 for (i = 1; i < 4; i++) {
872 if (test_access(i, stp))
Jeff Layton12659652014-07-10 14:07:28 -0400873 nfs4_file_put_access(stp->st_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400874 clear_access(i, stp);
875 }
876}
877
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400878static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500879{
Trond Myklebust1d31a252014-07-10 14:07:25 -0400880 struct nfs4_file *fp = stp->st_file;
881
882 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500883 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400884 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500885 list_del(&stp->st_perstateowner);
886}
887
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400888static void close_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500889{
Jeff Layton82c5ff12012-05-11 09:45:13 -0400890 release_all_access(stp);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400891}
892
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400893static void free_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400894{
Trond Myklebustf8338832014-07-25 07:34:19 -0400895 if (stp->st_file)
896 put_nfs4_file(stp->st_file);
J. Bruce Fields68a33962013-03-21 11:21:50 -0400897 remove_stid(&stp->st_stid);
Benny Halevy9857df82013-10-14 13:44:52 +0300898 nfs4_free_stid(stateid_slab, &stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500899}
900
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400901static void __release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500902{
903 struct file *file;
904
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400905 list_del(&stp->st_locks);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500906 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400907 unhash_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500908 file = find_any_file(stp->st_file);
Trond Myklebuste20fcf12014-07-10 14:07:27 -0400909 if (file)
910 filp_close(file, (fl_owner_t)lockowner(stp->st_stateowner));
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400911 close_generic_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500912 free_generic_stateid(stp);
913}
914
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400915static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500916{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400917 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500918
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400919 list_del(&lo->lo_owner.so_strhash);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400920 while (!list_empty(&lo->lo_owner.so_stateids)) {
921 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400922 struct nfs4_ol_stateid, st_perstateowner);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400923 __release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500924 }
925}
926
Trond Myklebust50cc6232014-04-18 14:44:03 -0400927static void nfs4_free_lockowner(struct nfs4_lockowner *lo)
928{
929 kfree(lo->lo_owner.so_owner.data);
930 kmem_cache_free(lockowner_slab, lo);
931}
932
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400933static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500934{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400935 unhash_lockowner(lo);
936 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500937}
938
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400939static void release_lockowner_if_empty(struct nfs4_lockowner *lo)
940{
941 if (list_empty(&lo->lo_owner.so_stateids))
942 release_lockowner(lo);
943}
944
945static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500946{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400947 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500948
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400949 lo = lockowner(stp->st_stateowner);
950 __release_lock_stateid(stp);
951 release_lockowner_if_empty(lo);
952}
953
954static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
955{
956 struct nfs4_ol_stateid *stp;
957
958 while (!list_empty(&open_stp->st_locks)) {
959 stp = list_entry(open_stp->st_locks.next,
960 struct nfs4_ol_stateid, st_locks);
961 release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500962 }
963}
964
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400965static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500966{
967 unhash_generic_stateid(stp);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400968 release_open_stateid_locks(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400969 close_generic_stateid(stp);
970}
971
972static void release_open_stateid(struct nfs4_ol_stateid *stp)
973{
974 unhash_open_stateid(stp);
J. Bruce Fields22839632009-01-11 14:27:17 -0500975 free_generic_stateid(stp);
976}
977
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400978static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500979{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400980 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500981
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400982 list_del(&oo->oo_owner.so_strhash);
983 list_del(&oo->oo_perclient);
984 while (!list_empty(&oo->oo_owner.so_stateids)) {
985 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400986 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500987 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500988 }
989}
990
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400991static void release_last_closed_stateid(struct nfs4_openowner *oo)
992{
993 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
994
995 if (s) {
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400996 free_generic_stateid(s);
997 oo->oo_last_closed_stid = NULL;
998 }
999}
1000
Trond Myklebust50cc6232014-04-18 14:44:03 -04001001static void nfs4_free_openowner(struct nfs4_openowner *oo)
1002{
1003 kfree(oo->oo_owner.so_owner.data);
1004 kmem_cache_free(openowner_slab, oo);
1005}
1006
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001007static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001008{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001009 unhash_openowner(oo);
1010 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001011 release_last_closed_stateid(oo);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001012 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001013}
1014
Marc Eshel5282fd72009-04-03 08:27:52 +03001015static inline int
1016hash_sessionid(struct nfs4_sessionid *sessionid)
1017{
1018 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1019
1020 return sid->sequence % SESSION_HASH_SIZE;
1021}
1022
Trond Myklebust8f199b82012-03-20 15:11:17 -04001023#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001024static inline void
1025dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1026{
1027 u32 *ptr = (u32 *)(&sessionid->data[0]);
1028 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1029}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001030#else
1031static inline void
1032dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1033{
1034}
1035#endif
1036
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001037/*
1038 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1039 * won't be used for replay.
1040 */
1041void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1042{
1043 struct nfs4_stateowner *so = cstate->replay_owner;
1044
1045 if (nfserr == nfserr_replay_me)
1046 return;
1047
1048 if (!seqid_mutating_err(ntohl(nfserr))) {
1049 cstate->replay_owner = NULL;
1050 return;
1051 }
1052 if (!so)
1053 return;
1054 if (so->so_is_open_owner)
1055 release_last_closed_stateid(openowner(so));
1056 so->so_seqid++;
1057 return;
1058}
Marc Eshel5282fd72009-04-03 08:27:52 +03001059
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001060static void
1061gen_sessionid(struct nfsd4_session *ses)
1062{
1063 struct nfs4_client *clp = ses->se_client;
1064 struct nfsd4_sessionid *sid;
1065
1066 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1067 sid->clientid = clp->cl_clientid;
1068 sid->sequence = current_sessionid++;
1069 sid->reserved = 0;
1070}
1071
1072/*
Andy Adamsona6496372009-08-28 08:45:01 -04001073 * The protocol defines ca_maxresponssize_cached to include the size of
1074 * the rpc header, but all we need to cache is the data starting after
1075 * the end of the initial SEQUENCE operation--the rest we regenerate
1076 * each time. Therefore we can advertise a ca_maxresponssize_cached
1077 * value that is the number of bytes in our cache plus a few additional
1078 * bytes. In order to stay on the safe side, and not promise more than
1079 * we can cache, those additional bytes must be the minimum possible: 24
1080 * bytes of rpc header (xid through accept state, with AUTH_NULL
1081 * verifier), 12 for the compound header (with zero-length tag), and 44
1082 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001083 */
Andy Adamsona6496372009-08-28 08:45:01 -04001084#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1085
Andy Adamson557ce262009-08-28 08:45:04 -04001086static void
1087free_session_slots(struct nfsd4_session *ses)
1088{
1089 int i;
1090
1091 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1092 kfree(ses->se_slots[i]);
1093}
1094
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001095/*
1096 * We don't actually need to cache the rpc and session headers, so we
1097 * can allocate a little less for each slot:
1098 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001099static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001100{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001101 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001102
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001103 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1104 size = 0;
1105 else
1106 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1107 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001108}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001109
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001110/*
1111 * XXX: If we run out of reserved DRC memory we could (up to a point)
1112 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001113 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001114 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001115static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001116{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001117 u32 slotsize = slot_bytes(ca);
1118 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001119 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001120
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001121 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001122 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1123 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001124 num = min_t(int, num, avail / slotsize);
1125 nfsd_drc_mem_used += num * slotsize;
1126 spin_unlock(&nfsd_drc_lock);
1127
1128 return num;
1129}
1130
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001131static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001132{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001133 int slotsize = slot_bytes(ca);
1134
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001135 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001136 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001137 spin_unlock(&nfsd_drc_lock);
1138}
1139
Kinglong Mee60810e52014-01-01 00:35:47 +08001140static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1141 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001142{
Kinglong Mee60810e52014-01-01 00:35:47 +08001143 int numslots = fattrs->maxreqs;
1144 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001145 struct nfsd4_session *new;
1146 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001147
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001148 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001149 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1150 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001151
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001152 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001153 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001154 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001155 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001156 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001157 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001158 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001159 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001160 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001161
1162 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1163 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1164
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001165 return new;
1166out_free:
1167 while (i--)
1168 kfree(new->se_slots[i]);
1169 kfree(new);
1170 return NULL;
1171}
1172
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001173static void free_conn(struct nfsd4_conn *c)
1174{
1175 svc_xprt_put(c->cn_xprt);
1176 kfree(c);
1177}
1178
1179static void nfsd4_conn_lost(struct svc_xpt_user *u)
1180{
1181 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1182 struct nfs4_client *clp = c->cn_session->se_client;
1183
1184 spin_lock(&clp->cl_lock);
1185 if (!list_empty(&c->cn_persession)) {
1186 list_del(&c->cn_persession);
1187 free_conn(c);
1188 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001189 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001190 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001191}
1192
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001193static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001194{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001195 struct nfsd4_conn *conn;
1196
1197 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1198 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001199 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001200 svc_xprt_get(rqstp->rq_xprt);
1201 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001202 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001203 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1204 return conn;
1205}
1206
J. Bruce Fields328ead22010-09-29 16:11:06 -04001207static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1208{
1209 conn->cn_session = ses;
1210 list_add(&conn->cn_persession, &ses->se_conns);
1211}
1212
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001213static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1214{
1215 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001216
1217 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001218 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001219 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001220}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001221
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001222static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001223{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001224 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001225 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001226}
1227
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001228static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001229{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001230 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001231
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001232 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001233 ret = nfsd4_register_conn(conn);
1234 if (ret)
1235 /* oops; xprt is already down: */
1236 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001237 /* We may have gained or lost a callback channel: */
1238 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001239}
1240
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001241static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001242{
1243 u32 dir = NFS4_CDFC4_FORE;
1244
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001245 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001246 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001247 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001248}
1249
1250/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001251static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001252{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001253 struct nfs4_client *clp = s->se_client;
1254 struct nfsd4_conn *c;
1255
1256 spin_lock(&clp->cl_lock);
1257 while (!list_empty(&s->se_conns)) {
1258 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1259 list_del_init(&c->cn_persession);
1260 spin_unlock(&clp->cl_lock);
1261
1262 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1263 free_conn(c);
1264
1265 spin_lock(&clp->cl_lock);
1266 }
1267 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001268}
1269
J. Bruce Fields1377b692012-09-11 21:42:40 -04001270static void __free_session(struct nfsd4_session *ses)
1271{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001272 free_session_slots(ses);
1273 kfree(ses);
1274}
1275
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001276static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001277{
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001278 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1279
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001280 lockdep_assert_held(&nn->client_lock);
1281 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001282 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001283 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001284}
Andy Adamson557ce262009-08-28 08:45:04 -04001285
Fengguang Wu135ae822012-11-10 07:20:25 -05001286static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001287{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001288 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001289 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001290
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001291 new->se_client = clp;
1292 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001293
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001294 INIT_LIST_HEAD(&new->se_conns);
1295
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001296 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001297 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001298 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001299 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001300 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001301 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001302 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001303 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001304 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001305 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001306 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001307 spin_unlock(&nn->client_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001308
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001309 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001310 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001311 /*
1312 * This is a little silly; with sessions there's no real
1313 * use for the callback address. Use the peer address
1314 * as a reasonable default for now, but consider fixing
1315 * the rpc client not to require an address in the
1316 * future:
1317 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001318 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1319 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001320 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001321}
1322
Benny Halevy9089f1b2010-05-12 00:12:26 +03001323/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001324static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001325__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001326{
1327 struct nfsd4_session *elem;
1328 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001329 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001330
1331 dump_sessionid(__func__, sessionid);
1332 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001333 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001334 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001335 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1336 NFS4_MAX_SESSIONID_LEN)) {
1337 return elem;
1338 }
1339 }
1340
1341 dprintk("%s: session not found\n", __func__);
1342 return NULL;
1343}
1344
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001345static struct nfsd4_session *
1346find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1347 __be32 *ret)
1348{
1349 struct nfsd4_session *session;
1350 __be32 status = nfserr_badsession;
1351
1352 session = __find_in_sessionid_hashtbl(sessionid, net);
1353 if (!session)
1354 goto out;
1355 status = nfsd4_get_session_locked(session);
1356 if (status)
1357 session = NULL;
1358out:
1359 *ret = status;
1360 return session;
1361}
1362
Benny Halevy9089f1b2010-05-12 00:12:26 +03001363/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001364static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001365unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001366{
1367 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001368 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001369 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001370 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001371}
1372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1374static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001375STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001377 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001379 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001380 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return 1;
1382}
1383
1384/*
1385 * XXX Should we use a slab cache ?
1386 * This type of memory management is somewhat inefficient, but we use it
1387 * anyway since SETCLIENTID is not a common operation.
1388 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001389static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 struct nfs4_client *clp;
1392
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001393 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1394 if (clp == NULL)
1395 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001396 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001397 if (clp->cl_name.data == NULL) {
1398 kfree(clp);
1399 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001401 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001402 INIT_LIST_HEAD(&clp->cl_sessions);
1403 idr_init(&clp->cl_stateids);
1404 atomic_set(&clp->cl_refcount, 0);
1405 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1406 INIT_LIST_HEAD(&clp->cl_idhash);
1407 INIT_LIST_HEAD(&clp->cl_openowners);
1408 INIT_LIST_HEAD(&clp->cl_delegations);
1409 INIT_LIST_HEAD(&clp->cl_lru);
1410 INIT_LIST_HEAD(&clp->cl_callbacks);
1411 INIT_LIST_HEAD(&clp->cl_revoked);
1412 spin_lock_init(&clp->cl_lock);
1413 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return clp;
1415}
1416
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001417static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418free_client(struct nfs4_client *clp)
1419{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001420 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001421
1422 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001423 while (!list_empty(&clp->cl_sessions)) {
1424 struct nfsd4_session *ses;
1425 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1426 se_perclnt);
1427 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001428 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1429 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001430 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001431 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001432 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001434 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 kfree(clp);
1436}
1437
Benny Halevy84d38ac2010-05-12 00:13:16 +03001438/* must be called under the client_lock */
1439static inline void
1440unhash_client_locked(struct nfs4_client *clp)
1441{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001442 struct nfsd4_session *ses;
1443
Benny Halevy84d38ac2010-05-12 00:13:16 +03001444 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001445 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001446 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1447 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001448 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001449}
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001452destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001454 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001457 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001460 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001461 while (!list_empty(&clp->cl_delegations)) {
1462 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001463 unhash_delegation_locked(dp);
1464 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
Benny Halevycdc97502014-05-30 09:09:30 -04001466 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 while (!list_empty(&reaplist)) {
1468 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001469 list_del_init(&dp->dl_recall_lru);
1470 nfs4_put_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001472 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001473 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001474 list_del_init(&dp->dl_recall_lru);
1475 nfs4_put_delegation(dp);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001476 }
NeilBrownea1da632005-06-23 22:04:17 -07001477 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001478 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1479 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001481 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001482 if (clp->cl_cb_conn.cb_xprt)
1483 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001484 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001485 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001486 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001487 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001488 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001489 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001490 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001491 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1492 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001493 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001496static void expire_client(struct nfs4_client *clp)
1497{
1498 nfsd4_client_record_remove(clp);
1499 destroy_client(clp);
1500}
1501
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001502static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1503{
1504 memcpy(target->cl_verifier.data, source->data,
1505 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506}
1507
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001508static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1509{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1511 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1512}
1513
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001514static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001515{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001516 if (source->cr_principal) {
1517 target->cr_principal =
1518 kstrdup(source->cr_principal, GFP_KERNEL);
1519 if (target->cr_principal == NULL)
1520 return -ENOMEM;
1521 } else
1522 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001523 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 target->cr_uid = source->cr_uid;
1525 target->cr_gid = source->cr_gid;
1526 target->cr_group_info = source->cr_group_info;
1527 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001528 target->cr_gss_mech = source->cr_gss_mech;
1529 if (source->cr_gss_mech)
1530 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001531 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001534static long long
1535compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1536{
1537 long long res;
1538
1539 res = o1->len - o2->len;
1540 if (res)
1541 return res;
1542 return (long long)memcmp(o1->data, o2->data, o1->len);
1543}
1544
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001545static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001546{
NeilBrowna55370a2005-06-23 22:03:52 -07001547 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
1550static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001551same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1552{
1553 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
1555
1556static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001557same_clid(clientid_t *cl1, clientid_t *cl2)
1558{
1559 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001562static bool groups_equal(struct group_info *g1, struct group_info *g2)
1563{
1564 int i;
1565
1566 if (g1->ngroups != g2->ngroups)
1567 return false;
1568 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001569 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001570 return false;
1571 return true;
1572}
1573
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001574/*
1575 * RFC 3530 language requires clid_inuse be returned when the
1576 * "principal" associated with a requests differs from that previously
1577 * used. We use uid, gid's, and gss principal string as our best
1578 * approximation. We also don't want to allow non-gss use of a client
1579 * established using gss: in theory cr_principal should catch that
1580 * change, but in practice cr_principal can be null even in the gss case
1581 * since gssd doesn't always pass down a principal string.
1582 */
1583static bool is_gss_cred(struct svc_cred *cr)
1584{
1585 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1586 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1587}
1588
1589
Vivek Trivedi5559b502012-07-24 21:18:20 +05301590static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001591same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1592{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001593 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001594 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1595 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001596 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1597 return false;
1598 if (cr1->cr_principal == cr2->cr_principal)
1599 return true;
1600 if (!cr1->cr_principal || !cr2->cr_principal)
1601 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301602 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
J. Bruce Fields57266a62013-04-13 14:27:29 -04001605static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1606{
1607 struct svc_cred *cr = &rqstp->rq_cred;
1608 u32 service;
1609
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001610 if (!cr->cr_gss_mech)
1611 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001612 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1613 return service == RPC_GSS_SVC_INTEGRITY ||
1614 service == RPC_GSS_SVC_PRIVACY;
1615}
1616
1617static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1618{
1619 struct svc_cred *cr = &rqstp->rq_cred;
1620
1621 if (!cl->cl_mach_cred)
1622 return true;
1623 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1624 return false;
1625 if (!svc_rqst_integrity_protected(rqstp))
1626 return false;
1627 if (!cr->cr_principal)
1628 return false;
1629 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1630}
1631
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001632static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001633{
1634 static u32 current_clientid = 1;
1635
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001636 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 clp->cl_clientid.cl_id = current_clientid++;
1638}
1639
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001640static void gen_confirm(struct nfs4_client *clp)
1641{
Chuck Leverab4684d2012-03-02 17:13:50 -05001642 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001643 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Jeff Laytonf4199922014-06-17 07:44:11 -04001645 /*
1646 * This is opaque to client, so no need to byte-swap. Use
1647 * __force to keep sparse happy
1648 */
1649 verf[0] = (__force __be32)get_seconds();
1650 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001651 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001654static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001655{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001656 struct nfs4_stid *ret;
1657
1658 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1659 if (!ret || !ret->sc_type)
1660 return NULL;
1661 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001662}
1663
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001664static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001665{
1666 struct nfs4_stid *s;
1667
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001668 s = find_stateid(cl, t);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001669 if (!s)
1670 return NULL;
1671 if (typemask & s->sc_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001672 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001673 return NULL;
1674}
1675
Jeff Layton2216d442012-11-12 15:00:57 -05001676static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001677 struct svc_rqst *rqstp, nfs4_verifier *verf)
1678{
1679 struct nfs4_client *clp;
1680 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001681 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001682 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001683 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001684
1685 clp = alloc_client(name);
1686 if (clp == NULL)
1687 return NULL;
1688
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001689 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1690 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001691 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001692 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001693 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001694 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001695 }
Jeff Layton02e12152014-07-16 10:31:57 -04001696 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001697 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001698 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001699 copy_verf(clp, verf);
1700 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001701 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001702 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001703 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001704 return clp;
1705}
1706
NeilBrownfd39ca92005-06-23 22:04:03 -07001707static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001708add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1709{
1710 struct rb_node **new = &(root->rb_node), *parent = NULL;
1711 struct nfs4_client *clp;
1712
1713 while (*new) {
1714 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1715 parent = *new;
1716
1717 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1718 new = &((*new)->rb_left);
1719 else
1720 new = &((*new)->rb_right);
1721 }
1722
1723 rb_link_node(&new_clp->cl_namenode, parent, new);
1724 rb_insert_color(&new_clp->cl_namenode, root);
1725}
1726
1727static struct nfs4_client *
1728find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1729{
1730 long long cmp;
1731 struct rb_node *node = root->rb_node;
1732 struct nfs4_client *clp;
1733
1734 while (node) {
1735 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1736 cmp = compare_blob(&clp->cl_name, name);
1737 if (cmp > 0)
1738 node = node->rb_left;
1739 else if (cmp < 0)
1740 node = node->rb_right;
1741 else
1742 return clp;
1743 }
1744 return NULL;
1745}
1746
1747static void
1748add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
1750 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001751 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001753 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001754 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001756 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001757 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
NeilBrownfd39ca92005-06-23 22:04:03 -07001760static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761move_to_confirmed(struct nfs4_client *clp)
1762{
1763 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001764 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001767 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001768 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001769 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001770 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 renew_client(clp);
1772}
1773
1774static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001775find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
1777 struct nfs4_client *clp;
1778 unsigned int idhashval = clientid_hashval(clid->cl_id);
1779
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001780 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001781 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001782 if ((bool)clp->cl_minorversion != sessions)
1783 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001784 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
1788 return NULL;
1789}
1790
1791static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001792find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1793{
1794 struct list_head *tbl = nn->conf_id_hashtbl;
1795
1796 return find_client_in_id_table(tbl, clid, sessions);
1797}
1798
1799static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001800find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001802 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001804 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001807static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001808{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001809 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001810}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001811
NeilBrown28ce6052005-06-23 22:03:56 -07001812static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001813find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001814{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001815 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001816}
1817
1818static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001819find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001820{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001821 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001822}
1823
NeilBrownfd39ca92005-06-23 22:04:03 -07001824static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001825gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001827 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001828 struct sockaddr *sa = svc_addr(rqstp);
1829 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001830 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Jeff Layton7077ecb2009-08-14 12:57:58 -04001832 /* Currently, we only support tcp and tcp6 for the callback channel */
1833 if (se->se_callback_netid_len == 3 &&
1834 !memcmp(se->se_callback_netid_val, "tcp", 3))
1835 expected_family = AF_INET;
1836 else if (se->se_callback_netid_len == 4 &&
1837 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1838 expected_family = AF_INET6;
1839 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 goto out_err;
1841
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001842 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001843 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001844 (struct sockaddr *)&conn->cb_addr,
1845 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001846
J. Bruce Fields07263f12010-05-31 19:09:40 -04001847 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001849
J. Bruce Fields07263f12010-05-31 19:09:40 -04001850 if (conn->cb_addr.ss_family == AF_INET6)
1851 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001852
J. Bruce Fields07263f12010-05-31 19:09:40 -04001853 conn->cb_prog = se->se_callback_prog;
1854 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001855 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 return;
1857out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001858 conn->cb_addr.ss_family = AF_UNSPEC;
1859 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001860 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 "will not receive delegations\n",
1862 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 return;
1865}
1866
Andy Adamson074fe892009-04-03 08:28:15 +03001867/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04001868 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001869 */
Trond Myklebustb6076642014-06-30 11:48:35 -04001870static void
Andy Adamson074fe892009-04-03 08:28:15 +03001871nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1872{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001873 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04001874 struct nfsd4_slot *slot = resp->cstate.slot;
1875 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001876
Andy Adamson557ce262009-08-28 08:45:04 -04001877 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001878
Andy Adamson557ce262009-08-28 08:45:04 -04001879 slot->sl_opcnt = resp->opcnt;
1880 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001881
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001882 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001883 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001884 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001885 return;
1886 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001887 base = resp->cstate.data_offset;
1888 slot->sl_datalen = buf->len - base;
1889 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04001890 WARN("%s: sessions DRC could not cache compound\n", __func__);
1891 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001892}
1893
1894/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001895 * Encode the replay sequence operation from the slot values.
1896 * If cachethis is FALSE encode the uncached rep error on the next
1897 * operation which sets resp->p and increments resp->opcnt for
1898 * nfs4svc_encode_compoundres.
1899 *
Andy Adamson074fe892009-04-03 08:28:15 +03001900 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001901static __be32
1902nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1903 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001904{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001905 struct nfsd4_op *op;
1906 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001907
Andy Adamsonabfabf82009-07-23 19:02:18 -04001908 /* Encode the replayed sequence operation */
1909 op = &args->ops[resp->opcnt - 1];
1910 nfsd4_encode_operation(resp, op);
1911
1912 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001913 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001914 op = &args->ops[resp->opcnt++];
1915 op->status = nfserr_retry_uncached_rep;
1916 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001917 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001918 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001919}
1920
1921/*
Andy Adamson557ce262009-08-28 08:45:04 -04001922 * The sequence operation is not cached because we can use the slot and
1923 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001924 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04001925static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001926nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1927 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001928{
Andy Adamson557ce262009-08-28 08:45:04 -04001929 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001930 struct xdr_stream *xdr = &resp->xdr;
1931 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03001932 __be32 status;
1933
Andy Adamson557ce262009-08-28 08:45:04 -04001934 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001935
Andy Adamsonabfabf82009-07-23 19:02:18 -04001936 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04001937 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04001938 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001939
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001940 p = xdr_reserve_space(xdr, slot->sl_datalen);
1941 if (!p) {
1942 WARN_ON_ONCE(1);
1943 return nfserr_serverfault;
1944 }
1945 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1946 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03001947
Andy Adamson557ce262009-08-28 08:45:04 -04001948 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001949 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001950}
1951
Andy Adamson0733d212009-04-03 08:28:01 +03001952/*
1953 * Set the exchange_id flags returned by the server.
1954 */
1955static void
1956nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1957{
1958 /* pNFS is not supported */
1959 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1960
1961 /* Referrals are supported, Migration is not. */
1962 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1963
1964 /* set the wire flags to return to client. */
1965 clid->flags = new->cl_exchange_flags;
1966}
1967
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001968static bool client_has_state(struct nfs4_client *clp)
1969{
1970 /*
1971 * Note clp->cl_openowners check isn't quite right: there's no
1972 * need to count owners without stateid's.
1973 *
1974 * Also note we should probably be using this in 4.0 case too.
1975 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04001976 return !list_empty(&clp->cl_openowners)
1977 || !list_empty(&clp->cl_delegations)
1978 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001979}
1980
Al Virob37ad282006-10-19 23:28:59 -07001981__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001982nfsd4_exchange_id(struct svc_rqst *rqstp,
1983 struct nfsd4_compound_state *cstate,
1984 struct nfsd4_exchange_id *exid)
1985{
Andy Adamson0733d212009-04-03 08:28:01 +03001986 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001987 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04001988 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001989 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001990 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001991 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001992 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03001993
Jeff Layton363168b2009-08-14 12:57:56 -04001994 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001995 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001996 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001997 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001998 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03001999
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002000 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002001 return nfserr_inval;
2002
Andy Adamson0733d212009-04-03 08:28:01 +03002003 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002004 case SP4_MACH_CRED:
2005 if (!svc_rqst_integrity_protected(rqstp))
2006 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002007 case SP4_NONE:
2008 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002009 default: /* checked by xdr code */
2010 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002011 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002012 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002013 }
2014
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002015 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002016 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002017 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002018 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002019 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2020 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2021
J. Bruce Fields136e6582012-05-12 20:37:23 -04002022 if (update) {
2023 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002024 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002025 goto out;
2026 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002027 if (!mach_creds_match(conf, rqstp)) {
2028 status = nfserr_wrong_cred;
2029 goto out;
2030 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002031 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002032 status = nfserr_perm;
2033 goto out;
2034 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002035 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002036 status = nfserr_not_same;
2037 goto out;
2038 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002039 /* case 6 */
2040 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2041 new = conf;
2042 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002043 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002044 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002045 if (client_has_state(conf)) {
2046 status = nfserr_clid_inuse;
2047 goto out;
2048 }
Andy Adamson0733d212009-04-03 08:28:01 +03002049 expire_client(conf);
2050 goto out_new;
2051 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002052 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002053 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002054 new = conf;
2055 goto out_copy;
2056 }
2057 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04002058 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002059 }
2060
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002061 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002062 status = nfserr_noent;
2063 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002064 }
2065
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002066 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002067 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03002068 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002069
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002070 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002071out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05002072 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03002073 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04002074 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03002075 goto out;
2076 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002077 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002078 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002079
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002080 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002081 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03002082out_copy:
2083 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2084 exid->clientid.cl_id = new->cl_clientid.cl_id;
2085
J. Bruce Fields778df3f2012-05-25 21:40:23 -04002086 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03002087 nfsd4_set_ex_flags(new, exid);
2088
2089 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04002090 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002091 status = nfs_ok;
2092
2093out:
2094 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03002095 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002096}
2097
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002098static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002099check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002100{
Andy Adamson88e588d2009-07-23 19:02:15 -04002101 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2102 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002103
2104 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002105 if (slot_inuse) {
2106 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002107 return nfserr_jukebox;
2108 else
2109 return nfserr_seq_misordered;
2110 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002111 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002112 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002113 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002114 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002115 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002116 return nfserr_seq_misordered;
2117}
2118
Andy Adamson49557cc2009-07-23 19:02:16 -04002119/*
2120 * Cache the create session result into the create session single DRC
2121 * slot cache by saving the xdr structure. sl_seqid has been set.
2122 * Do this for solo or embedded create session operations.
2123 */
2124static void
2125nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002126 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002127{
2128 slot->sl_status = nfserr;
2129 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2130}
2131
2132static __be32
2133nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2134 struct nfsd4_clid_slot *slot)
2135{
2136 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2137 return slot->sl_status;
2138}
2139
Mi Jinlong1b74c252011-07-14 14:50:17 +08002140#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2141 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2142 1 + /* MIN tag is length with zero, only length */ \
2143 3 + /* version, opcount, opcode */ \
2144 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2145 /* seqid, slotID, slotID, cache */ \
2146 4 ) * sizeof(__be32))
2147
2148#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2149 2 + /* verifier: AUTH_NULL, length 0 */\
2150 1 + /* status */ \
2151 1 + /* MIN tag is length with zero, only length */ \
2152 3 + /* opcount, opcode, opstatus*/ \
2153 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2154 /* seqid, slotID, slotID, slotID, status */ \
2155 5 ) * sizeof(__be32))
2156
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002157static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002158{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002159 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2160
J. Bruce Fields373cd402013-04-08 15:42:12 -04002161 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2162 return nfserr_toosmall;
2163 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2164 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002165 ca->headerpadsz = 0;
2166 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2167 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2168 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2169 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2170 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2171 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2172 /*
2173 * Note decreasing slot size below client's request may make it
2174 * difficult for client to function correctly, whereas
2175 * decreasing the number of slots will (just?) affect
2176 * performance. When short on memory we therefore prefer to
2177 * decrease number of slots instead of their size. Clients that
2178 * request larger slots than they need will get poor results:
2179 */
2180 ca->maxreqs = nfsd4_get_drc_mem(ca);
2181 if (!ca->maxreqs)
2182 return nfserr_jukebox;
2183
J. Bruce Fields373cd402013-04-08 15:42:12 -04002184 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002185}
2186
Kinglong Mee8a891632013-12-23 18:11:02 +08002187#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2188 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2189#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2190 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2191
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002192static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2193{
2194 ca->headerpadsz = 0;
2195
2196 /*
2197 * These RPC_MAX_HEADER macros are overkill, especially since we
2198 * don't even do gss on the backchannel yet. But this is still
2199 * less than 1k. Tighten up this estimate in the unlikely event
2200 * it turns out to be a problem for some client:
2201 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002202 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002203 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002204 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002205 return nfserr_toosmall;
2206 ca->maxresp_cached = 0;
2207 if (ca->maxops < 2)
2208 return nfserr_toosmall;
2209
2210 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002211}
2212
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002213static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2214{
2215 switch (cbs->flavor) {
2216 case RPC_AUTH_NULL:
2217 case RPC_AUTH_UNIX:
2218 return nfs_ok;
2219 default:
2220 /*
2221 * GSS case: the spec doesn't allow us to return this
2222 * error. But it also doesn't allow us not to support
2223 * GSS.
2224 * I'd rather this fail hard than return some error the
2225 * client might think it can already handle:
2226 */
2227 return nfserr_encr_alg_unsupp;
2228 }
2229}
2230
Andy Adamson069b6ad2009-04-03 08:27:58 +03002231__be32
2232nfsd4_create_session(struct svc_rqst *rqstp,
2233 struct nfsd4_compound_state *cstate,
2234 struct nfsd4_create_session *cr_ses)
2235{
Jeff Layton363168b2009-08-14 12:57:56 -04002236 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002237 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002238 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002239 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002240 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002241 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002242 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002243
Mi Jinlonga62573d2011-03-23 17:57:07 +08002244 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2245 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002246 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2247 if (status)
2248 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002249 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002250 if (status)
2251 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002252 status = check_backchannel_attrs(&cr_ses->back_channel);
2253 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002254 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002255 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002256 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002257 if (!new)
2258 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002259 conn = alloc_conn_from_crses(rqstp, cr_ses);
2260 if (!conn)
2261 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002262
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002263 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002264 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002265 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002266 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002267
2268 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002269 status = nfserr_wrong_cred;
2270 if (!mach_creds_match(conf, rqstp))
2271 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002272 cs_slot = &conf->cl_cs_slot;
2273 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002274 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002275 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002276 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002277 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002278 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002279 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002280 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002281 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002282 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002283 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002284 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002285 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002286 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002287 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002288 status = nfserr_wrong_cred;
2289 if (!mach_creds_match(unconf, rqstp))
2290 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002291 cs_slot = &unconf->cl_cs_slot;
2292 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002293 if (status) {
2294 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002295 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002296 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002297 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002298 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002299 if (old) {
2300 status = mark_client_expired(old);
2301 if (status)
2302 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002303 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002304 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002305 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002306 conf = unconf;
2307 } else {
2308 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002309 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002310 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002311 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002312 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002313 * We do not support RDMA or persistent sessions
2314 */
2315 cr_ses->flags &= ~SESSION4_PERSIST;
2316 cr_ses->flags &= ~SESSION4_RDMA;
2317
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002318 init_session(rqstp, new, conf, cr_ses);
2319 nfsd4_init_conn(rqstp, conn, new);
2320
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002321 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002322 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002323 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002324 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002325
Andy Adamson49557cc2009-07-23 19:02:16 -04002326 /* cache solo and embedded create sessions under the state lock */
2327 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002328 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002329 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002330out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08002331 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002332 free_conn(conn);
2333out_free_session:
2334 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002335out_release_drc_mem:
2336 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002337 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002338}
2339
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002340static __be32 nfsd4_map_bcts_dir(u32 *dir)
2341{
2342 switch (*dir) {
2343 case NFS4_CDFC4_FORE:
2344 case NFS4_CDFC4_BACK:
2345 return nfs_ok;
2346 case NFS4_CDFC4_FORE_OR_BOTH:
2347 case NFS4_CDFC4_BACK_OR_BOTH:
2348 *dir = NFS4_CDFC4_BOTH;
2349 return nfs_ok;
2350 };
2351 return nfserr_inval;
2352}
2353
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002354__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2355{
2356 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002357 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002358 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002359
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002360 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2361 if (status)
2362 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002363 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002364 session->se_cb_prog = bc->bc_cb_program;
2365 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002366 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002367
2368 nfsd4_probe_callback(session->se_client);
2369
2370 return nfs_ok;
2371}
2372
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002373__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2374 struct nfsd4_compound_state *cstate,
2375 struct nfsd4_bind_conn_to_session *bcts)
2376{
2377 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002378 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002379 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002380 struct net *net = SVC_NET(rqstp);
2381 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002382
2383 if (!nfsd4_last_compound_op(rqstp))
2384 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002385 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002386 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002387 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002388 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002389 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002390 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002391 status = nfserr_wrong_cred;
2392 if (!mach_creds_match(session->se_client, rqstp))
2393 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002394 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002395 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002396 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002397 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002398 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002399 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002400 goto out;
2401 nfsd4_init_conn(rqstp, conn, session);
2402 status = nfs_ok;
2403out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002404 nfsd4_put_session(session);
2405out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002406 nfs4_unlock_state();
2407 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002408}
2409
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002410static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2411{
2412 if (!session)
2413 return 0;
2414 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2415}
2416
Andy Adamson069b6ad2009-04-03 08:27:58 +03002417__be32
2418nfsd4_destroy_session(struct svc_rqst *r,
2419 struct nfsd4_compound_state *cstate,
2420 struct nfsd4_destroy_session *sessionid)
2421{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002422 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002423 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002424 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002425 struct net *net = SVC_NET(r);
2426 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002427
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002428 nfs4_lock_state();
2429 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002430 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002431 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002432 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002433 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002434 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002435 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002436 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002437 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002438 if (!ses)
2439 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002440 status = nfserr_wrong_cred;
2441 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002442 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002443 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002444 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002445 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002446 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002447 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002448
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002449 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002450
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002451 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002452 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002453out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002454 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002455out_client_lock:
2456 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002457out:
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002458 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002459 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002460}
2461
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002462static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002463{
2464 struct nfsd4_conn *c;
2465
2466 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002467 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002468 return c;
2469 }
2470 }
2471 return NULL;
2472}
2473
J. Bruce Fields57266a62013-04-13 14:27:29 -04002474static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002475{
2476 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002477 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002478 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002479 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002480
2481 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002482 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002483 if (c)
2484 goto out_free;
2485 status = nfserr_conn_not_bound_to_session;
2486 if (clp->cl_mach_cred)
2487 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002488 __nfsd4_hash_conn(new, ses);
2489 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002490 ret = nfsd4_register_conn(new);
2491 if (ret)
2492 /* oops; xprt is already down: */
2493 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002494 return nfs_ok;
2495out_free:
2496 spin_unlock(&clp->cl_lock);
2497 free_conn(new);
2498 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002499}
2500
Mi Jinlong868b89c2011-04-27 09:09:58 +08002501static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2502{
2503 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2504
2505 return args->opcnt > session->se_fchannel.maxops;
2506}
2507
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002508static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2509 struct nfsd4_session *session)
2510{
2511 struct xdr_buf *xb = &rqstp->rq_arg;
2512
2513 return xb->len > session->se_fchannel.maxreq_sz;
2514}
2515
Andy Adamson069b6ad2009-04-03 08:27:58 +03002516__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002517nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002518 struct nfsd4_compound_state *cstate,
2519 struct nfsd4_sequence *seq)
2520{
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002521 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002522 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002523 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002524 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002525 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002526 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002527 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002528 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002529 struct net *net = SVC_NET(rqstp);
2530 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002531
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002532 if (resp->opcnt != 1)
2533 return nfserr_sequence_pos;
2534
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002535 /*
2536 * Will be either used or freed by nfsd4_sequence_check_conn
2537 * below.
2538 */
2539 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2540 if (!conn)
2541 return nfserr_jukebox;
2542
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002543 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002544 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002545 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002546 goto out_no_session;
2547 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002548
Mi Jinlong868b89c2011-04-27 09:09:58 +08002549 status = nfserr_too_many_ops;
2550 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002551 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002552
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002553 status = nfserr_req_too_big;
2554 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002555 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002556
Benny Halevyb85d4c02009-04-03 08:28:08 +03002557 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002558 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002559 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002560
Andy Adamson557ce262009-08-28 08:45:04 -04002561 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002562 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2563
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002564 /* We do not negotiate the number of slots yet, so set the
2565 * maxslots to the session maxreqs which is used to encode
2566 * sr_highest_slotid and the sr_target_slot id to maxslots */
2567 seq->maxslots = session->se_fchannel.maxreqs;
2568
J. Bruce Fields73e79482012-02-13 16:39:00 -05002569 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2570 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002571 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002572 status = nfserr_seq_misordered;
2573 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002574 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002575 cstate->slot = slot;
2576 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002577 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002578 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002579 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002580 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002581 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002582 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002583 }
2584 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002585 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002586
J. Bruce Fields57266a62013-04-13 14:27:29 -04002587 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002588 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002589 if (status)
2590 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002591
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002592 buflen = (seq->cachethis) ?
2593 session->se_fchannel.maxresp_cached :
2594 session->se_fchannel.maxresp_sz;
2595 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2596 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002597 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002598 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002599 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002600
2601 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002602 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002603 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002604 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002605 if (seq->cachethis)
2606 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002607 else
2608 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002609
2610 cstate->slot = slot;
2611 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002612 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002613
Benny Halevyb85d4c02009-04-03 08:28:08 +03002614out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002615 switch (clp->cl_cb_state) {
2616 case NFSD4_CB_DOWN:
2617 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2618 break;
2619 case NFSD4_CB_FAULT:
2620 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2621 break;
2622 default:
2623 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002624 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002625 if (!list_empty(&clp->cl_revoked))
2626 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002627out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002628 if (conn)
2629 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002630 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002631 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002632out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002633 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002634 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002635}
2636
Trond Myklebustb6076642014-06-30 11:48:35 -04002637void
2638nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2639{
2640 struct nfsd4_compound_state *cs = &resp->cstate;
2641
2642 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002643 if (cs->status != nfserr_replay_cache) {
2644 nfsd4_store_cache_entry(resp);
2645 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2646 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002647 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002648 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002649 } else if (cs->clp)
2650 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002651}
2652
Mi Jinlong345c2842011-10-20 17:51:39 +08002653__be32
2654nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2655{
2656 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002657 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002658 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002659
2660 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002661 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002662 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002663 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002664
2665 if (conf) {
2666 clp = conf;
2667
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002668 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002669 status = nfserr_clientid_busy;
2670 goto out;
2671 }
2672 } else if (unconf)
2673 clp = unconf;
2674 else {
2675 status = nfserr_stale_clientid;
2676 goto out;
2677 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002678 if (!mach_creds_match(clp, rqstp)) {
2679 status = nfserr_wrong_cred;
2680 goto out;
2681 }
Mi Jinlong345c2842011-10-20 17:51:39 +08002682 expire_client(clp);
2683out:
2684 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002685 return status;
2686}
2687
Andy Adamson069b6ad2009-04-03 08:27:58 +03002688__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002689nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2690{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002691 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002692
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002693 if (rc->rca_one_fs) {
2694 if (!cstate->current_fh.fh_dentry)
2695 return nfserr_nofilehandle;
2696 /*
2697 * We don't take advantage of the rca_one_fs case.
2698 * That's OK, it's optional, we can safely ignore it.
2699 */
2700 return nfs_ok;
2701 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002702
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002703 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002704 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002705 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2706 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002707 goto out;
2708
2709 status = nfserr_stale_clientid;
2710 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002711 /*
2712 * The following error isn't really legal.
2713 * But we only get here if the client just explicitly
2714 * destroyed the client. Surely it no longer cares what
2715 * error it gets back on an operation for the dead
2716 * client.
2717 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002718 goto out;
2719
2720 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002721 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002722out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002723 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002724 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002725}
2726
2727__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002728nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2729 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002731 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002733 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002734 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002735 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2736
J. Bruce Fields63db4632012-05-18 21:54:19 -04002737 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002739 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002740 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002741 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002743 if (clp_used_exchangeid(conf))
2744 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002745 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002746 char addr_str[INET6_ADDRSTRLEN];
2747 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2748 sizeof(addr_str));
2749 dprintk("NFSD: setclientid: string in use by client "
2750 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 goto out;
2752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002754 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002755 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002758 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002759 if (new == NULL)
2760 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002761 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002762 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002764 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002765 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002766 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002767 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002768 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2770 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2771 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2772 status = nfs_ok;
2773out:
2774 nfs4_unlock_state();
2775 return status;
2776}
2777
2778
Al Virob37ad282006-10-19 23:28:59 -07002779__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002780nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2781 struct nfsd4_compound_state *cstate,
2782 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783{
NeilBrown21ab45a2005-06-23 22:04:14 -07002784 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2786 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002787 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002788 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002790 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002793
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002794 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002795 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002796 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002797 * We try hard to give out unique clientid's, so if we get an
2798 * attempt to confirm the same clientid with a different cred,
2799 * there's a bug somewhere. Let's charitably assume it's our
2800 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002801 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002802 status = nfserr_serverfault;
2803 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2804 goto out;
2805 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2806 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002807 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002808 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2809 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002811 else /* case 4: client hasn't noticed we rebooted yet? */
2812 status = nfserr_stale_clientid;
2813 goto out;
2814 }
2815 status = nfs_ok;
2816 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002817 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2818 nfsd4_probe_callback(conf);
2819 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002820 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002821 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002822 if (conf) {
2823 status = mark_client_expired(conf);
2824 if (status)
2825 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002826 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002827 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002828 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002829 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 nfs4_unlock_state();
2833 return status;
2834}
2835
J. Bruce Fields32513b42011-10-13 16:00:16 -04002836static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002838 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2839}
2840
2841/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04002842static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04002843{
Trond Myklebustca943212014-07-23 16:17:39 -04002844 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
Trond Myklebust950e0112014-06-30 11:48:31 -04002846 lockdep_assert_held(&state_lock);
2847
J. Bruce Fields32513b42011-10-13 16:00:16 -04002848 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04002849 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002850 INIT_LIST_HEAD(&fp->fi_stateids);
2851 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04002852 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002853 fp->fi_had_conflict = false;
2854 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04002855 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002856 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2857 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04002858 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859}
2860
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002861void
NeilBrowne60d4392005-06-23 22:03:01 -07002862nfsd4_free_slabs(void)
2863{
Christoph Hellwigabf11352014-05-21 07:43:03 -07002864 kmem_cache_destroy(openowner_slab);
2865 kmem_cache_destroy(lockowner_slab);
2866 kmem_cache_destroy(file_slab);
2867 kmem_cache_destroy(stateid_slab);
2868 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002869}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
Bryan Schumaker72083392011-11-01 15:24:59 -04002871int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872nfsd4_init_slabs(void)
2873{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002874 openowner_slab = kmem_cache_create("nfsd4_openowners",
2875 sizeof(struct nfs4_openowner), 0, 0, NULL);
2876 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002877 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002878 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002879 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002880 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002881 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07002882 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002883 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002884 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002885 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07002886 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002887 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002888 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002889 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002890 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002891 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002892 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002893 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07002895
2896out_free_stateid_slab:
2897 kmem_cache_destroy(stateid_slab);
2898out_free_file_slab:
2899 kmem_cache_destroy(file_slab);
2900out_free_lockowner_slab:
2901 kmem_cache_destroy(lockowner_slab);
2902out_free_openowner_slab:
2903 kmem_cache_destroy(openowner_slab);
2904out:
NeilBrowne60d4392005-06-23 22:03:01 -07002905 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2906 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907}
2908
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002909static void init_nfs4_replay(struct nfs4_replay *rp)
2910{
2911 rp->rp_status = nfserr_serverfault;
2912 rp->rp_buflen = 0;
2913 rp->rp_buf = rp->rp_ibuf;
2914}
2915
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002916static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917{
2918 struct nfs4_stateowner *sop;
2919
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002920 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002921 if (!sop)
2922 return NULL;
2923
2924 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2925 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002926 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002927 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002929 sop->so_owner.len = owner->len;
2930
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002931 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002932 sop->so_client = clp;
2933 init_nfs4_replay(&sop->so_replay);
2934 return sop;
2935}
2936
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002937static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002938{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002939 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2940
2941 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002942 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943}
2944
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002945static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04002946alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002947 struct nfsd4_compound_state *cstate)
2948{
Trond Myklebust13d6f662014-06-30 11:48:45 -04002949 struct nfs4_client *clp = cstate->clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002950 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002952 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2953 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002955 oo->oo_owner.so_is_open_owner = 1;
2956 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04002957 oo->oo_flags = NFS4_OO_NEW;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002958 if (nfsd4_has_session(cstate))
2959 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002960 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002961 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002962 INIT_LIST_HEAD(&oo->oo_close_lru);
2963 hash_openowner(oo, clp, strhashval);
2964 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965}
2966
J. Bruce Fields996e0932011-10-17 11:14:48 -04002967static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002968 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002970 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04002971 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002972 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002973 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002974 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 stp->st_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 stp->st_access_bmap = 0;
2977 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07002978 stp->st_openstp = NULL;
Trond Myklebust1d31a252014-07-10 14:07:25 -04002979 spin_lock(&fp->fi_lock);
2980 list_add(&stp->st_perfile, &fp->fi_stateids);
2981 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982}
2983
2984static void
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002985move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986{
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002987 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2988
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002989 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002991 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002992 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993}
2994
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002996same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2997 clientid_t *clid)
2998{
2999 return (sop->so_owner.len == owner->len) &&
3000 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
3001 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002}
3003
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003004static struct nfs4_openowner *
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003005find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
3006 bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003008 struct nfs4_stateowner *so;
3009 struct nfs4_openowner *oo;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003010 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003012 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003013 if (!so->so_is_open_owner)
3014 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003015 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
3016 oo = openowner(so);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003017 clp = oo->oo_owner.so_client;
3018 if ((bool)clp->cl_minorversion != sessions)
3019 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003020 renew_client(oo->oo_owner.so_client);
3021 return oo;
3022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 }
3024 return NULL;
3025}
3026
3027/* search file_hashtbl[] for file */
3028static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003029find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030{
Trond Myklebustca943212014-07-23 16:17:39 -04003031 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 struct nfs4_file *fp;
3033
Trond Myklebust950e0112014-06-30 11:48:31 -04003034 lockdep_assert_held(&state_lock);
3035
Jeff Layton89876f82013-04-02 09:01:59 -04003036 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003037 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003038 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 }
3042 return NULL;
3043}
3044
Trond Myklebust950e0112014-06-30 11:48:31 -04003045static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003046find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003047{
3048 struct nfs4_file *fp;
3049
3050 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003051 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003052 spin_unlock(&state_lock);
3053 return fp;
3054}
3055
3056static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003057find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003058{
3059 struct nfs4_file *fp;
3060
3061 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003062 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003063 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003064 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003065 fp = new;
3066 }
3067 spin_unlock(&state_lock);
3068
3069 return fp;
3070}
3071
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003072/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 * Called to check deny when READ with all zero stateid or
3074 * WRITE with all zero or all one stateid
3075 */
Al Virob37ad282006-10-19 23:28:59 -07003076static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3078{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003080 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
Trond Myklebustca943212014-07-23 16:17:39 -04003082 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003083 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003084 return ret;
3085 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003086 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003087 if (fp->fi_share_deny & deny_type)
3088 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003089 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003090 put_nfs4_file(fp);
3091 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092}
3093
Jeff Layton02e12152014-07-16 10:31:57 -04003094void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095{
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003096 struct nfs4_client *clp = dp->dl_stid.sc_client;
3097 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3098
Jeff Layton02e12152014-07-16 10:31:57 -04003099 /*
3100 * We can't do this in nfsd_break_deleg_cb because it is
3101 * already holding inode->i_lock
3102 */
3103 spin_lock(&state_lock);
3104 block_delegations(&dp->dl_fh);
Jeff Laytondff13992014-07-08 14:02:49 -04003105 /*
3106 * If the dl_time != 0, then we know that it has already been
3107 * queued for a lease break. Don't queue it again.
3108 */
3109 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003110 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003111 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003112 }
Jeff Layton02e12152014-07-16 10:31:57 -04003113 spin_unlock(&state_lock);
3114}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
Jeff Layton02e12152014-07-16 10:31:57 -04003116static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3117{
3118 /*
3119 * We're assuming the state code never drops its reference
3120 * without first removing the lease. Since we're in this lease
3121 * callback (and since the lease code is serialized by the kernel
3122 * lock) we know the server hasn't removed the lease yet, we know
3123 * it's safe to take a reference.
3124 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003125 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003126 nfsd4_cb_recall(dp);
3127}
3128
Jeff Layton1c8c6012013-06-21 08:58:15 -04003129/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003130static void nfsd_break_deleg_cb(struct file_lock *fl)
3131{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003132 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3133 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003134
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003135 if (!fp) {
3136 WARN(1, "(%p)->fl_owner NULL\n", fl);
3137 return;
3138 }
3139 if (fp->fi_had_conflict) {
3140 WARN(1, "duplicate break on %p\n", fp);
3141 return;
3142 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003143 /*
3144 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003145 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003146 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003147 */
3148 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Jeff Layton02e12152014-07-16 10:31:57 -04003150 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003151 fp->fi_had_conflict = true;
3152 /*
3153 * If there are no delegations on the list, then we can't count on this
3154 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3155 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3156 * true should keep any new delegations from being hashed.
3157 */
3158 if (list_empty(&fp->fi_delegations))
3159 fl->fl_break_time = jiffies;
3160 else
3161 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3162 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003163 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164}
3165
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166static
3167int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3168{
3169 if (arg & F_UNLCK)
3170 return lease_modify(onlist, arg);
3171 else
3172 return -EAGAIN;
3173}
3174
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003175static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003176 .lm_break = nfsd_break_deleg_cb,
3177 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178};
3179
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003180static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3181{
3182 if (nfsd4_has_session(cstate))
3183 return nfs_ok;
3184 if (seqid == so->so_seqid - 1)
3185 return nfserr_replay_me;
3186 if (seqid == so->so_seqid)
3187 return nfs_ok;
3188 return nfserr_bad_seqid;
3189}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
Jeff Layton4b24ca72014-06-30 11:48:44 -04003191static __be32 lookup_clientid(clientid_t *clid,
3192 struct nfsd4_compound_state *cstate,
3193 struct nfsd_net *nn)
3194{
3195 struct nfs4_client *found;
3196
3197 if (cstate->clp) {
3198 found = cstate->clp;
3199 if (!same_clid(&found->cl_clientid, clid))
3200 return nfserr_stale_clientid;
3201 return nfs_ok;
3202 }
3203
3204 if (STALE_CLIENTID(clid, nn))
3205 return nfserr_stale_clientid;
3206
3207 /*
3208 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3209 * cached already then we know this is for is for v4.0 and "sessions"
3210 * will be false.
3211 */
3212 WARN_ON_ONCE(cstate->session);
3213 found = find_confirmed_client(clid, false, nn);
3214 if (!found)
3215 return nfserr_expired;
3216
3217 /* Cache the nfs4_client in cstate! */
3218 cstate->clp = found;
3219 atomic_inc(&found->cl_refcount);
3220 return nfs_ok;
3221}
3222
Al Virob37ad282006-10-19 23:28:59 -07003223__be32
Andy Adamson66689582009-04-03 08:28:45 +03003224nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003225 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 clientid_t *clientid = &open->op_clientid;
3228 struct nfs4_client *clp = NULL;
3229 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003230 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003231 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003233 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003235 /*
3236 * In case we need it later, after we've already created the
3237 * file and don't want to risk a further failure:
3238 */
3239 open->op_file = nfsd4_alloc_file();
3240 if (open->op_file == NULL)
3241 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
Trond Myklebust2d91e892014-06-30 11:48:46 -04003243 status = lookup_clientid(clientid, cstate, nn);
3244 if (status)
3245 return status;
3246 clp = cstate->clp;
3247
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003248 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003249 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003250 open->op_openowner = oo;
3251 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003252 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003254 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003255 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003256 release_openowner(oo);
3257 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003258 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003259 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003260 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3261 if (status)
3262 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003263 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003264new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003265 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003266 if (oo == NULL)
3267 return nfserr_jukebox;
3268 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003269alloc_stateid:
3270 open->op_stp = nfs4_alloc_stateid(clp);
3271 if (!open->op_stp)
3272 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003273 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274}
3275
Al Virob37ad282006-10-19 23:28:59 -07003276static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003277nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3278{
3279 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3280 return nfserr_openmode;
3281 else
3282 return nfs_ok;
3283}
3284
Daniel Mackc47d8322011-05-16 16:38:14 +02003285static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003286{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003287 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3288}
3289
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003290static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003291{
3292 struct nfs4_stid *ret;
3293
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003294 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003295 if (!ret)
3296 return NULL;
3297 return delegstateid(ret);
3298}
3299
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003300static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3301{
3302 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3303 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3304}
3305
Al Virob37ad282006-10-19 23:28:59 -07003306static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003307nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003308 struct nfs4_delegation **dp)
3309{
3310 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003311 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07003312
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003313 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07003314 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003315 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003316 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07003317 status = nfs4_check_delegmode(*dp, flags);
3318 if (status)
3319 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003320out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003321 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003322 return nfs_ok;
3323 if (status)
3324 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003325 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003326 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003327}
3328
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003329static struct nfs4_ol_stateid *
3330nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003332 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003333 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334
Trond Myklebust1d31a252014-07-10 14:07:25 -04003335 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003336 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 /* ignore lock owners */
3338 if (local->st_stateowner->so_is_open_owner == 0)
3339 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003340 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003341 ret = local;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003342 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003345 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003346 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347}
3348
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003349static inline int nfs4_access_to_access(u32 nfs4_access)
3350{
3351 int flags = 0;
3352
3353 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3354 flags |= NFSD_MAY_READ;
3355 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3356 flags |= NFSD_MAY_WRITE;
3357 return flags;
3358}
3359
Al Virob37ad282006-10-19 23:28:59 -07003360static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3362 struct nfsd4_open *open)
3363{
3364 struct iattr iattr = {
3365 .ia_valid = ATTR_SIZE,
3366 .ia_size = 0,
3367 };
3368 if (!open->op_truncate)
3369 return 0;
3370 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003371 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3373}
3374
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003375static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003376 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3377 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003378{
Trond Myklebustde186432014-07-10 14:07:26 -04003379 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003380 __be32 status;
3381 int oflag = nfs4_access_to_omode(open->op_share_access);
3382 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003383 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003384
Trond Myklebustde186432014-07-10 14:07:26 -04003385 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003386
3387 /*
3388 * Are we trying to set a deny mode that would conflict with
3389 * current access?
3390 */
3391 status = nfs4_file_check_deny(fp, open->op_share_deny);
3392 if (status != nfs_ok) {
3393 spin_unlock(&fp->fi_lock);
3394 goto out;
3395 }
3396
3397 /* set access to the file */
3398 status = nfs4_file_get_access(fp, open->op_share_access);
3399 if (status != nfs_ok) {
3400 spin_unlock(&fp->fi_lock);
3401 goto out;
3402 }
3403
3404 /* Set access bits in stateid */
3405 old_access_bmap = stp->st_access_bmap;
3406 set_access(open->op_share_access, stp);
3407
3408 /* Set new deny mask */
3409 old_deny_bmap = stp->st_deny_bmap;
3410 set_deny(open->op_share_deny, stp);
3411 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3412
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003413 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003414 spin_unlock(&fp->fi_lock);
3415 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003416 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003417 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003418 spin_lock(&fp->fi_lock);
3419 if (!fp->fi_fds[oflag]) {
3420 fp->fi_fds[oflag] = filp;
3421 filp = NULL;
3422 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003423 }
Trond Myklebustde186432014-07-10 14:07:26 -04003424 spin_unlock(&fp->fi_lock);
3425 if (filp)
3426 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003427
3428 status = nfsd4_truncate(rqstp, cur_fh, open);
3429 if (status)
3430 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003431out:
3432 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003433out_put_access:
3434 stp->st_access_bmap = old_access_bmap;
3435 nfs4_file_put_access(fp, open->op_share_access);
3436 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3437 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003438}
3439
Al Virob37ad282006-10-19 23:28:59 -07003440static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003441nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442{
Al Virob37ad282006-10-19 23:28:59 -07003443 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003444 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003446 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003447 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003448
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003449 /* test and set deny mode */
3450 spin_lock(&fp->fi_lock);
3451 status = nfs4_file_check_deny(fp, open->op_share_deny);
3452 if (status == nfs_ok) {
3453 old_deny_bmap = stp->st_deny_bmap;
3454 set_deny(open->op_share_deny, stp);
3455 fp->fi_share_deny |=
3456 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3457 }
3458 spin_unlock(&fp->fi_lock);
3459
3460 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003463 status = nfsd4_truncate(rqstp, cur_fh, open);
3464 if (status != nfs_ok)
3465 reset_union_bmap_deny(old_deny_bmap, stp);
3466 return status;
3467}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003470nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003472 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473}
3474
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003475/* Should we give out recallable state?: */
3476static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3477{
3478 if (clp->cl_cb_state == NFSD4_CB_UP)
3479 return true;
3480 /*
3481 * In the sessions case, since we don't have to establish a
3482 * separate connection for callbacks, we assume it's OK
3483 * until we hear otherwise:
3484 */
3485 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3486}
3487
Jeff Laytond564fbe2014-07-16 10:31:58 -04003488static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003489{
3490 struct file_lock *fl;
3491
3492 fl = locks_alloc_lock();
3493 if (!fl)
3494 return NULL;
3495 locks_init_lock(fl);
3496 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003497 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003498 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3499 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003500 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003501 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003502 return fl;
3503}
3504
J. Bruce Fields99c41512013-05-21 16:21:25 -04003505static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003506{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003507 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003508 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003509 struct file *filp;
3510 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003511
Jeff Laytond564fbe2014-07-16 10:31:58 -04003512 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003513 if (!fl)
3514 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003515 filp = find_readable_file(fp);
3516 if (!filp) {
3517 /* We should always have a readable file here */
3518 WARN_ON_ONCE(1);
3519 return -EBADF;
3520 }
3521 fl->fl_file = filp;
3522 status = vfs_setlease(filp, fl->fl_type, &fl);
3523 if (status) {
3524 locks_free_lock(fl);
3525 goto out_fput;
3526 }
Benny Halevycdc97502014-05-30 09:09:30 -04003527 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003528 spin_lock(&fp->fi_lock);
3529 /* Did the lease get broken before we took the lock? */
3530 status = -EAGAIN;
3531 if (fp->fi_had_conflict)
3532 goto out_unlock;
3533 /* Race breaker */
3534 if (fp->fi_lease) {
3535 status = 0;
3536 atomic_inc(&fp->fi_delegees);
3537 hash_delegation_locked(dp, fp);
3538 goto out_unlock;
3539 }
3540 fp->fi_lease = fl;
3541 fp->fi_deleg_file = filp;
3542 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003543 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003544 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003545 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003546 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003547out_unlock:
3548 spin_unlock(&fp->fi_lock);
3549 spin_unlock(&state_lock);
3550out_fput:
3551 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003552 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003553}
3554
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003555static int nfs4_set_delegation(struct nfs4_delegation *dp, struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003556{
Jeff Layton417c6622014-07-21 09:34:57 -04003557 int status = 0;
3558
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003559 if (fp->fi_had_conflict)
3560 return -EAGAIN;
3561 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003562 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003563 spin_lock(&fp->fi_lock);
3564 dp->dl_file = fp;
3565 if (!fp->fi_lease) {
3566 spin_unlock(&fp->fi_lock);
3567 spin_unlock(&state_lock);
3568 return nfs4_setlease(dp);
3569 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003570 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003571 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003572 status = -EAGAIN;
3573 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003574 }
Benny Halevy931ee562014-05-30 09:09:27 -04003575 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003576out_unlock:
3577 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003578 spin_unlock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003579 return status;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003580}
3581
Benny Halevy4aa89132012-02-21 14:16:44 -08003582static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3583{
3584 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3585 if (status == -EAGAIN)
3586 open->op_why_no_deleg = WND4_CONTENTION;
3587 else {
3588 open->op_why_no_deleg = WND4_RESOURCE;
3589 switch (open->op_deleg_want) {
3590 case NFS4_SHARE_WANT_READ_DELEG:
3591 case NFS4_SHARE_WANT_WRITE_DELEG:
3592 case NFS4_SHARE_WANT_ANY_DELEG:
3593 break;
3594 case NFS4_SHARE_WANT_CANCEL:
3595 open->op_why_no_deleg = WND4_CANCELLED;
3596 break;
3597 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003598 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003599 }
3600 }
3601}
3602
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603/*
3604 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003605 *
3606 * Note we don't support write delegations, and won't until the vfs has
3607 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 */
3609static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003610nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3611 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612{
3613 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003614 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3615 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003616 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003617 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003619 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003620 open->op_recall = 0;
3621 switch (open->op_claim_type) {
3622 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003623 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003624 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003625 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3626 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003627 break;
3628 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003629 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003630 /*
3631 * Let's not give out any delegations till everyone's
3632 * had the chance to reclaim theirs....
3633 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003634 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003635 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003636 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003637 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003638 /*
3639 * Also, if the file was opened for write or
3640 * create, there's a good chance the client's
3641 * about to write to it, resulting in an
3642 * immediate recall (since we don't support
3643 * write delegations):
3644 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003645 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003646 goto out_no_deleg;
3647 if (open->op_create == NFS4_OPEN_CREATE)
3648 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003649 break;
3650 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003651 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003652 }
Jeff Layton4cf59222014-07-25 07:34:24 -04003653 dp = alloc_init_deleg(clp, fh);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003654 if (dp == NULL)
3655 goto out_no_deleg;
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003656 status = nfs4_set_delegation(dp, stp->st_file);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003657 if (status)
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003658 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003660 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003662 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003663 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003664 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003665 return;
3666out_free:
Jeff Layton42690672014-07-25 07:34:20 -04003667 nfs4_put_delegation(dp);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003668out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003669 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3670 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003671 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003672 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003673 open->op_recall = 1;
3674 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003675
3676 /* 4.1 client asking for a delegation? */
3677 if (open->op_deleg_want)
3678 nfsd4_open_deleg_none_ext(open, status);
3679 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680}
3681
Benny Halevye27f49c2012-02-21 14:16:54 -08003682static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3683 struct nfs4_delegation *dp)
3684{
3685 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3686 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3687 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3688 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3689 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_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_UPGRADE;
3693 }
3694 /* Otherwise the client must be confused wanting a delegation
3695 * it already has, therefore we don't return
3696 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3697 */
3698}
3699
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700/*
3701 * called with nfs4_lock_state() held.
3702 */
Al Virob37ad282006-10-19 23:28:59 -07003703__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3705{
Andy Adamson66689582009-04-03 08:28:45 +03003706 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003707 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003709 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003710 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003711 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 /*
3714 * Lookup file; if found, lookup stateid and check open request,
3715 * and check for delegations in the process of being recalled.
3716 * If not found, create the nfs4_file struct
3717 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003718 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04003719 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04003720 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003721 if (status)
3722 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003723 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003725 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003726 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003727 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003728 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003729 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 }
3731
3732 /*
3733 * OPEN the file, or upgrade an existing OPEN.
3734 * If truncate fails, the OPEN fails.
3735 */
3736 if (stp) {
3737 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003738 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 if (status)
3740 goto out;
3741 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003742 stp = open->op_stp;
3743 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003744 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003745 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3746 if (status) {
3747 release_open_stateid(stp);
3748 goto out;
3749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003751 update_stateid(&stp->st_stid.sc_stateid);
3752 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
Benny Halevyd24433c2012-02-16 20:57:17 +02003754 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02003755 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3756 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3757 open->op_why_no_deleg = WND4_NOT_WANTED;
3758 goto nodeleg;
3759 }
3760 }
3761
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 /*
3763 * Attempt to hand out a delegation. No error return, because the
3764 * OPEN succeeds even if we fail.
3765 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003766 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003767nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 status = nfs_ok;
3769
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003770 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003771 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003773 /* 4.1 client trying to upgrade/downgrade delegation? */
3774 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003775 open->op_deleg_want)
3776 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003777
NeilBrown13cd2182005-06-23 22:03:10 -07003778 if (fp)
3779 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003780 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003781 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 /*
3783 * To finish the open response, we just need to set the rflags.
3784 */
3785 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003786 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003787 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3789
3790 return status;
3791}
3792
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003793void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3794{
3795 if (open->op_openowner) {
3796 struct nfs4_openowner *oo = open->op_openowner;
3797
3798 if (!list_empty(&oo->oo_owner.so_stateids))
3799 list_del_init(&oo->oo_close_lru);
3800 if (oo->oo_flags & NFS4_OO_NEW) {
3801 if (status) {
3802 release_openowner(oo);
3803 open->op_openowner = NULL;
3804 } else
3805 oo->oo_flags &= ~NFS4_OO_NEW;
3806 }
3807 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003808 if (open->op_file)
3809 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003810 if (open->op_stp)
J. Bruce Fieldsef798592012-08-29 10:32:54 -07003811 free_generic_stateid(open->op_stp);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003812}
3813
Al Virob37ad282006-10-19 23:28:59 -07003814__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003815nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3816 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817{
3818 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003819 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003820 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821
3822 nfs4_lock_state();
3823 dprintk("process_renew(%08x/%08x): starting\n",
3824 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003825 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003826 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003828 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003830 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003831 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 goto out;
3833 status = nfs_ok;
3834out:
3835 nfs4_unlock_state();
3836 return status;
3837}
3838
NeilBrowna76b4312005-06-23 22:04:01 -07003839static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003840nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07003841{
Jeff Layton33dcc482012-04-10 11:08:48 -04003842 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003843 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003844 return;
3845
NeilBrowna76b4312005-06-23 22:04:01 -07003846 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003847 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003848 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003849 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003850 /*
3851 * Now that every NFSv4 client has had the chance to recover and
3852 * to see the (possibly new, possibly shorter) lease time, we
3853 * can safely set the next grace time to the current lease time:
3854 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03003855 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003856}
3857
NeilBrownfd39ca92005-06-23 22:04:03 -07003858static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003859nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860{
3861 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003862 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 struct nfs4_delegation *dp;
3864 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003865 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003866 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
3868 nfs4_lock_state();
3869
3870 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003871 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03003872 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003873 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03003874 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 clp = list_entry(pos, struct nfs4_client, cl_lru);
3876 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3877 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003878 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 break;
3880 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003881 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03003882 dprintk("NFSD: client in use (clientid %08x)\n",
3883 clp->cl_clientid.cl_id);
3884 continue;
3885 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003886 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003887 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003888 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03003889 list_for_each_safe(pos, next, &reaplist) {
3890 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 dprintk("NFSD: purging unused client (clientid %08x)\n",
3892 clp->cl_clientid.cl_id);
3893 expire_client(clp);
3894 }
Benny Halevycdc97502014-05-30 09:09:30 -04003895 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003896 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03003898 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3899 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003901 t = dp->dl_time - cutoff;
3902 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 break;
3904 }
Jeff Layton42690672014-07-25 07:34:20 -04003905 unhash_delegation_locked(dp);
3906 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 }
Benny Halevycdc97502014-05-30 09:09:30 -04003908 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04003909 while (!list_empty(&reaplist)) {
3910 dp = list_first_entry(&reaplist, struct nfs4_delegation,
3911 dl_recall_lru);
3912 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003913 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003915 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003916 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3917 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003918 t = oo->oo_time - cutoff;
3919 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 break;
3921 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003922 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04003924 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04003926 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927}
3928
Harvey Harrisona254b242008-02-20 12:49:00 -08003929static struct workqueue_struct *laundry_wq;
3930static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08003931
3932static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003933laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934{
3935 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003936 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3937 work);
3938 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3939 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003941 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003943 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944}
3945
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003946static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003947{
Trond Myklebustb07c54a2014-07-23 16:17:40 -04003948 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003949 return nfserr_bad_stateid;
3950 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951}
3952
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003954access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003956 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3957 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3958 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959}
3960
3961static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003962access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003964 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3965 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966}
3967
3968static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003969__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970{
Al Virob37ad282006-10-19 23:28:59 -07003971 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972
J. Bruce Fields02921912010-07-29 15:16:59 -04003973 /* For lock stateid's, we test the parent open, not the lock: */
3974 if (stp->st_openstp)
3975 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003976 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003978 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 goto out;
3980 status = nfs_ok;
3981out:
3982 return status;
3983}
3984
Al Virob37ad282006-10-19 23:28:59 -07003985static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003986check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003988 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003990 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003991 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 * conflicting state; so we must wait out the grace period. */
3993 return nfserr_grace;
3994 } else if (flags & WR_STATE)
3995 return nfs4_share_conflict(current_fh,
3996 NFS4_SHARE_DENY_WRITE);
3997 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3998 return nfs4_share_conflict(current_fh,
3999 NFS4_SHARE_DENY_READ);
4000}
4001
4002/*
4003 * Allow READ/WRITE during grace period on recovered state only for files
4004 * that are not able to provide mandatory locking.
4005 */
4006static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004007grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004009 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010}
4011
J. Bruce Fields81b82962011-08-23 11:03:29 -04004012/* Returns true iff a is later than b: */
4013static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4014{
Jim Rees1a9357f2013-05-17 17:33:00 -04004015 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004016}
4017
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004018static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004019{
Andy Adamson66689582009-04-03 08:28:45 +03004020 /*
4021 * When sessions are used the stateid generation number is ignored
4022 * when it is zero.
4023 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004024 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004025 return nfs_ok;
4026
4027 if (in->si_generation == ref->si_generation)
4028 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004029
J. Bruce Fields0836f582008-01-26 19:08:12 -05004030 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004031 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004032 return nfserr_bad_stateid;
4033 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004034 * However, we could see a stateid from the past, even from a
4035 * non-buggy client. For example, if the client sends a lock
4036 * while some IO is outstanding, the lock may bump si_generation
4037 * while the IO is still in flight. The client could avoid that
4038 * situation by waiting for responses on all the IO requests,
4039 * but better performance may result in retrying IO that
4040 * receives an old_stateid error if requests are rarely
4041 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004042 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004043 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004044}
4045
Chuck Lever7df302f2012-05-29 13:56:37 -04004046static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004047{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004048 struct nfs4_stid *s;
4049 struct nfs4_ol_stateid *ols;
4050 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004051
Chuck Lever7df302f2012-05-29 13:56:37 -04004052 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4053 return nfserr_bad_stateid;
4054 /* Client debugging aid. */
4055 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4056 char addr_str[INET6_ADDRSTRLEN];
4057 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4058 sizeof(addr_str));
4059 pr_warn_ratelimited("NFSD: client %s testing state ID "
4060 "with incorrect client ID\n", addr_str);
4061 return nfserr_bad_stateid;
4062 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004063 s = find_stateid(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004064 if (!s)
Chuck Lever7df302f2012-05-29 13:56:37 -04004065 return nfserr_bad_stateid;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004066 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004067 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004068 return status;
J. Bruce Fields23340032013-04-09 17:42:28 -04004069 switch (s->sc_type) {
4070 case NFS4_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004071 return nfs_ok;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004072 case NFS4_REVOKED_DELEG_STID:
4073 return nfserr_deleg_revoked;
J. Bruce Fields23340032013-04-09 17:42:28 -04004074 case NFS4_OPEN_STID:
4075 case NFS4_LOCK_STID:
4076 ols = openlockstateid(s);
4077 if (ols->st_stateowner->so_is_open_owner
4078 && !(openowner(ols->st_stateowner)->oo_flags
4079 & NFS4_OO_CONFIRMED))
4080 return nfserr_bad_stateid;
4081 return nfs_ok;
4082 default:
4083 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004084 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004085 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004086 case NFS4_CLOSED_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004087 return nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004088 }
Bryan Schumaker17456802011-07-13 10:50:48 -04004089}
4090
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004091static __be32
4092nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4093 stateid_t *stateid, unsigned char typemask,
4094 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004095{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004096 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004097
4098 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4099 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004100 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004101 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004102 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004103 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004104 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004105 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004106 if (status)
4107 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004108 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004109 if (!*s)
4110 return nfserr_bad_stateid;
4111 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004112}
4113
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114/*
4115* Checks for stateid operations
4116*/
Al Virob37ad282006-10-19 23:28:59 -07004117__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004118nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004119 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004121 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004122 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004124 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004126 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004127 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004128 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 if (filpp)
4131 *filpp = NULL;
4132
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004133 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 return nfserr_grace;
4135
4136 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004137 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138
Trond Myklebust14bcab12014-04-18 14:44:07 -04004139 nfs4_lock_state();
4140
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004141 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004142 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004143 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004144 if (status)
Trond Myklebust14bcab12014-04-18 14:44:07 -04004145 goto out;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004146 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4147 if (status)
4148 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004149 switch (s->sc_type) {
4150 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004151 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004152 status = nfs4_check_delegmode(dp, flags);
4153 if (status)
4154 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004155 if (filpp) {
Trond Myklebust14bcab12014-04-18 14:44:07 -04004156 file = dp->dl_file->fi_deleg_file;
4157 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004158 WARN_ON_ONCE(1);
4159 status = nfserr_serverfault;
4160 goto out;
4161 }
Trond Myklebustde186432014-07-10 14:07:26 -04004162 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004163 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004164 break;
4165 case NFS4_OPEN_STID:
4166 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004167 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004168 status = nfs4_check_fh(current_fh, stp);
4169 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004171 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004172 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004174 status = nfs4_check_openmode(stp, flags);
4175 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004177 if (filpp) {
4178 if (flags & RD_STATE)
Trond Myklebust14bcab12014-04-18 14:44:07 -04004179 file = find_readable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004180 else
Trond Myklebust14bcab12014-04-18 14:44:07 -04004181 file = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004182 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004183 break;
4184 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004185 status = nfserr_bad_stateid;
4186 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 }
4188 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004189 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004190 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191out:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004192 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 return status;
4194}
4195
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004196static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004197nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004198{
J. Bruce Fieldsa1b8ff42014-05-20 15:55:21 -04004199 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4200
4201 if (check_for_locks(stp->st_file, lo))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004202 return nfserr_locks_held;
Jeff Laytonc53530d2014-06-30 11:48:39 -04004203 release_lockowner_if_empty(lo);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004204 return nfs_ok;
4205}
4206
4207/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004208 * Test if the stateid is valid
4209 */
4210__be32
4211nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4212 struct nfsd4_test_stateid *test_stateid)
4213{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004214 struct nfsd4_test_stateid_id *stateid;
4215 struct nfs4_client *cl = cstate->session->se_client;
4216
4217 nfs4_lock_state();
4218 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004219 stateid->ts_id_status =
4220 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004221 nfs4_unlock_state();
4222
Bryan Schumaker17456802011-07-13 10:50:48 -04004223 return nfs_ok;
4224}
4225
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004226__be32
4227nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4228 struct nfsd4_free_stateid *free_stateid)
4229{
4230 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004231 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004232 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004233 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004234 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004235
4236 nfs4_lock_state();
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004237 s = find_stateid(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004238 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004239 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004240 switch (s->sc_type) {
4241 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004242 ret = nfserr_locks_held;
4243 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004244 case NFS4_OPEN_STID:
4245 case NFS4_LOCK_STID:
4246 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4247 if (ret)
4248 goto out;
4249 if (s->sc_type == NFS4_LOCK_STID)
4250 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4251 else
4252 ret = nfserr_locks_held;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004253 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004254 case NFS4_REVOKED_DELEG_STID:
4255 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004256 spin_lock(&cl->cl_lock);
4257 list_del_init(&dp->dl_recall_lru);
4258 spin_unlock(&cl->cl_lock);
4259 nfs4_put_delegation(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004260 ret = nfs_ok;
4261 break;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004262 default:
4263 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004264 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004265out:
4266 nfs4_unlock_state();
4267 return ret;
4268}
4269
NeilBrown4c4cd222005-07-07 17:59:27 -07004270static inline int
4271setlkflg (int type)
4272{
4273 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4274 RD_STATE : WR_STATE;
4275}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004277static __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 -04004278{
4279 struct svc_fh *current_fh = &cstate->current_fh;
4280 struct nfs4_stateowner *sop = stp->st_stateowner;
4281 __be32 status;
4282
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004283 status = nfsd4_check_seqid(cstate, sop, seqid);
4284 if (status)
4285 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004286 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4287 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004288 /*
4289 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004290 * nfserr_replay_me from the previous step, and
4291 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004292 */
4293 return nfserr_bad_stateid;
4294 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4295 if (status)
4296 return status;
4297 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004298}
4299
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300/*
4301 * Checks for sequence id mutating operations.
4302 */
Al Virob37ad282006-10-19 23:28:59 -07004303static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004304nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004305 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004306 struct nfs4_ol_stateid **stpp,
4307 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004309 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004310 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004311 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004313 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4314 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004315
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004317 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004318 if (status)
4319 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004320 stp = openlockstateid(s);
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04004321 if (!nfsd4_has_session(cstate))
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004322 cstate->replay_owner = stp->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004324 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4325 if (!status)
4326 *stpp = stp;
4327 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004328}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004329
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004330static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4331 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004332{
4333 __be32 status;
4334 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004336 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004337 NFS4_OPEN_STID, stpp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004338 if (status)
4339 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004340 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004341 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07004342 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004343 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344}
4345
Al Virob37ad282006-10-19 23:28:59 -07004346__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004347nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004348 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349{
Al Virob37ad282006-10-19 23:28:59 -07004350 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004351 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004352 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004353 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354
Al Viroa6a9f182013-09-16 10:57:01 -04004355 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4356 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004358 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004359 if (status)
4360 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361
4362 nfs4_lock_state();
4363
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004364 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004365 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004366 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004367 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004368 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004369 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004370 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004371 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004372 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004373 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004374 update_stateid(&stp->st_stid.sc_stateid);
4375 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004376 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004377 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004378
Jeff Layton2a4317c2012-03-21 16:42:43 -04004379 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004380 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004382 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004383 if (!cstate->replay_owner)
4384 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 return status;
4386}
4387
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004388static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004390 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004391 return;
Jeff Layton12659652014-07-10 14:07:28 -04004392 nfs4_file_put_access(stp->st_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004393 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004394}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004395
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004396static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4397{
4398 switch (to_access) {
4399 case NFS4_SHARE_ACCESS_READ:
4400 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4401 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4402 break;
4403 case NFS4_SHARE_ACCESS_WRITE:
4404 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4405 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4406 break;
4407 case NFS4_SHARE_ACCESS_BOTH:
4408 break;
4409 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004410 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 }
4412}
4413
Al Virob37ad282006-10-19 23:28:59 -07004414__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004415nfsd4_open_downgrade(struct svc_rqst *rqstp,
4416 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004417 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418{
Al Virob37ad282006-10-19 23:28:59 -07004419 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004420 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004421 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
Al Viroa6a9f182013-09-16 10:57:01 -04004423 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4424 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004426 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004427 if (od->od_deleg_want)
4428 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4429 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430
4431 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004432 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004433 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004434 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004437 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004438 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 stp->st_access_bmap, od->od_share_access);
4440 goto out;
4441 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004442 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004443 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 stp->st_deny_bmap, od->od_share_deny);
4445 goto out;
4446 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004447 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448
Jeff Laytonce0fc432012-05-11 09:45:14 -04004449 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004451 update_stateid(&stp->st_stid.sc_stateid);
4452 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 status = nfs_ok;
4454out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004455 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004456 if (!cstate->replay_owner)
4457 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 return status;
4459}
4460
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004461static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4462{
Trond Myklebustacf92952014-06-30 11:48:37 -04004463 struct nfs4_client *clp = s->st_stid.sc_client;
4464 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4465
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004466 s->st_stid.sc_type = NFS4_CLOSED_STID;
Trond Myklebustacf92952014-06-30 11:48:37 -04004467 unhash_open_stateid(s);
4468
4469 if (clp->cl_minorversion) {
4470 free_generic_stateid(s);
4471 if (list_empty(&oo->oo_owner.so_stateids))
4472 release_openowner(oo);
4473 } else {
Trond Myklebustf8338832014-07-25 07:34:19 -04004474 if (s->st_file) {
4475 put_nfs4_file(s->st_file);
4476 s->st_file = NULL;
4477 }
Trond Myklebustacf92952014-06-30 11:48:37 -04004478 oo->oo_last_closed_stid = s;
4479 /*
4480 * In the 4.0 case we need to keep the owners around a
4481 * little while to handle CLOSE replay.
4482 */
4483 if (list_empty(&oo->oo_owner.so_stateids))
4484 move_to_close_lru(oo, clp->net);
4485 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004486}
4487
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488/*
4489 * nfs4_unlock_state() called after encode
4490 */
Al Virob37ad282006-10-19 23:28:59 -07004491__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004492nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004493 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494{
Al Virob37ad282006-10-19 23:28:59 -07004495 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004496 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004497 struct net *net = SVC_NET(rqstp);
4498 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499
Al Viroa6a9f182013-09-16 10:57:01 -04004500 dprintk("NFSD: nfsd4_close on file %pd\n",
4501 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
4503 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004504 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4505 &close->cl_stateid,
4506 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004507 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004508 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004509 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004511 update_stateid(&stp->st_stid.sc_stateid);
4512 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004514 nfsd4_close_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004516 if (!cstate->replay_owner)
4517 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 return status;
4519}
4520
Al Virob37ad282006-10-19 23:28:59 -07004521__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004522nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4523 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004525 struct nfs4_delegation *dp;
4526 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004527 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004528 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004529 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004531 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004532 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533
4534 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004535 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004536 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004537 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004538 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004539 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004540 if (status)
4541 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004542
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004543 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004545 nfs4_unlock_state();
4546
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 return status;
4548}
4549
4550
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552
Benny Halevy87df4de2008-12-15 19:42:03 +02004553static inline u64
4554end_offset(u64 start, u64 len)
4555{
4556 u64 end;
4557
4558 end = start + len;
4559 return end >= start ? end: NFS4_MAX_UINT64;
4560}
4561
4562/* last octet in a range */
4563static inline u64
4564last_byte_offset(u64 start, u64 len)
4565{
4566 u64 end;
4567
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004568 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004569 end = start + len;
4570 return end > start ? end - 1: NFS4_MAX_UINT64;
4571}
4572
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573/*
4574 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4575 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4576 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4577 * locking, this prevents us from being completely protocol-compliant. The
4578 * real solution to this problem is to start using unsigned file offsets in
4579 * the VFS, but this is a very deep change!
4580 */
4581static inline void
4582nfs4_transform_lock_offset(struct file_lock *lock)
4583{
4584 if (lock->fl_start < 0)
4585 lock->fl_start = OFFSET_MAX;
4586 if (lock->fl_end < 0)
4587 lock->fl_end = OFFSET_MAX;
4588}
4589
NeilBrownd5b90262006-04-10 22:55:22 -07004590/* Hack!: For now, we're defining this just so we can use a pointer to it
4591 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004592static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004593};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594
4595static inline void
4596nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4597{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004598 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599
NeilBrownd5b90262006-04-10 22:55:22 -07004600 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004601 lo = (struct nfs4_lockowner *) fl->fl_owner;
4602 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4603 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004604 if (!deny->ld_owner.data)
4605 /* We just don't care that much */
4606 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004607 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4608 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004609 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004610nevermind:
4611 deny->ld_owner.len = 0;
4612 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004613 deny->ld_clientid.cl_boot = 0;
4614 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 }
4616 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004617 deny->ld_length = NFS4_MAX_UINT64;
4618 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4620 deny->ld_type = NFS4_READ_LT;
4621 if (fl->fl_type != F_RDLCK)
4622 deny->ld_type = NFS4_WRITE_LT;
4623}
4624
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004625static struct nfs4_lockowner *
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004626find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4627 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628{
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004629 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4630 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004632 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4633 if (so->so_is_open_owner)
4634 continue;
4635 if (!same_owner_str(so, owner, clid))
4636 continue;
4637 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 }
4639 return NULL;
4640}
4641
4642/*
4643 * Alloc a lock owner structure.
4644 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004645 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004647 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004649static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004650alloc_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 -04004651 struct nfs4_lockowner *lo;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004652 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004654 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4655 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004657 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4658 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07004659 /* It is the openowner seqid that will be incremented in encode in the
4660 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004661 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004662 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004663 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664}
4665
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004666static struct nfs4_ol_stateid *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004667alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp,
4668 struct inode *inode,
4669 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004671 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004672 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673
J. Bruce Fields996e0932011-10-17 11:14:48 -04004674 stp = nfs4_alloc_stateid(clp);
NeilBrown5ac049a2005-06-23 22:03:03 -07004675 if (stp == NULL)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04004676 return NULL;
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004677 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004678 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4679 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004680 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 stp->st_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004682 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004684 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004685 list_add(&stp->st_locks, &open_stp->st_locks);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004686 spin_lock(&fp->fi_lock);
4687 list_add(&stp->st_perfile, &fp->fi_stateids);
4688 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 return stp;
4690}
4691
Jeff Laytonc53530d2014-06-30 11:48:39 -04004692static struct nfs4_ol_stateid *
4693find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4694{
4695 struct nfs4_ol_stateid *lst;
4696
4697 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
4698 if (lst->st_file == fp)
4699 return lst;
4700 }
4701 return NULL;
4702}
4703
4704
NeilBrownfd39ca92005-06-23 22:04:03 -07004705static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706check_lock_length(u64 offset, u64 length)
4707{
Benny Halevy87df4de2008-12-15 19:42:03 +02004708 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 LOFF_OVERFLOW(offset, length)));
4710}
4711
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004712static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004713{
4714 struct nfs4_file *fp = lock_stp->st_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004715
Jeff Layton7214e862014-07-10 14:07:33 -04004716 lockdep_assert_held(&fp->fi_lock);
4717
Jeff Layton82c5ff12012-05-11 09:45:13 -04004718 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004719 return;
Jeff Layton12659652014-07-10 14:07:28 -04004720 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004721 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004722}
4723
J. Bruce Fields2355c592012-04-25 16:56:22 -04004724static __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 -04004725{
4726 struct nfs4_file *fi = ost->st_file;
4727 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4728 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004729 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004730 struct nfs4_lockowner *lo;
4731 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004732 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004733
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004734 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004735 if (!lo) {
4736 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4737 &lock->v.new.owner);
4738 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4739 if (lo == NULL)
4740 return nfserr_jukebox;
4741 } else {
4742 /* with an existing lockowner, seqids must be the same */
4743 if (!cstate->minorversion &&
4744 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004745 return nfserr_bad_seqid;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004746 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004747
4748 *lst = find_lock_stateid(lo, fi);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004749 if (*lst == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004750 *lst = alloc_init_lock_stateid(lo, fi, inode, ost);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004751 if (*lst == NULL) {
4752 release_lockowner_if_empty(lo);
4753 return nfserr_jukebox;
4754 }
4755 *new = true;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004756 }
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004757 return nfs_ok;
4758}
4759
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760/*
4761 * LOCK operation
4762 */
Al Virob37ad282006-10-19 23:28:59 -07004763__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004764nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004765 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004767 struct nfs4_openowner *open_sop = NULL;
4768 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004769 struct nfs4_ol_stateid *lock_stp;
Jeff Layton7214e862014-07-10 14:07:33 -04004770 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004771 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004772 struct file_lock *file_lock = NULL;
4773 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004774 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004775 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004776 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004777 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004778 struct net *net = SVC_NET(rqstp);
4779 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780
4781 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4782 (long long) lock->lk_offset,
4783 (long long) lock->lk_length);
4784
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 if (check_lock_length(lock->lk_offset, lock->lk_length))
4786 return nfserr_inval;
4787
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004788 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004789 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004790 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4791 return status;
4792 }
4793
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 nfs4_lock_state();
4795
4796 if (lock->lk_is_new) {
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004797 struct nfs4_ol_stateid *open_stp = NULL;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004798
4799 if (nfsd4_has_session(cstate))
4800 /* See rfc 5661 18.10.3: given clientid is ignored: */
4801 memcpy(&lock->v.new.clientid,
4802 &cstate->session->se_client->cl_clientid,
4803 sizeof(clientid_t));
4804
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004806 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004810 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 lock->lk_new_open_seqid,
4812 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004813 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07004814 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004816 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004817 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004818 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004819 &lock->v.new.clientid))
4820 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004821 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4822 &lock_stp, &new_state);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004823 } else
Benny Halevydd453df2009-04-03 08:28:41 +03004824 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004825 lock->lk_old_lock_seqid,
4826 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004827 NFS4_LOCK_STID, &lock_stp, nn);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004828 if (status)
4829 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004830 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004832 lkflg = setlkflg(lock->lk_type);
4833 status = nfs4_check_openmode(lock_stp, lkflg);
4834 if (status)
4835 goto out;
4836
NeilBrown0dd395d2005-07-07 17:59:15 -07004837 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004838 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004839 goto out;
4840 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004841 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004842 goto out;
4843
Jeff Layton21179d82012-08-21 08:03:32 -04004844 file_lock = locks_alloc_lock();
4845 if (!file_lock) {
4846 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4847 status = nfserr_jukebox;
4848 goto out;
4849 }
4850
Jeff Layton7214e862014-07-10 14:07:33 -04004851 fp = lock_stp->st_file;
Jeff Layton21179d82012-08-21 08:03:32 -04004852 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 switch (lock->lk_type) {
4854 case NFS4_READ_LT:
4855 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004856 spin_lock(&fp->fi_lock);
4857 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004858 if (filp)
4859 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04004860 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004861 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004862 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 case NFS4_WRITE_LT:
4864 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004865 spin_lock(&fp->fi_lock);
4866 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004867 if (filp)
4868 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04004869 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004870 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004871 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 default:
4873 status = nfserr_inval;
4874 goto out;
4875 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004876 if (!filp) {
4877 status = nfserr_openmode;
4878 goto out;
4879 }
Jeff Layton21179d82012-08-21 08:03:32 -04004880 file_lock->fl_owner = (fl_owner_t)lock_sop;
4881 file_lock->fl_pid = current->tgid;
4882 file_lock->fl_file = filp;
4883 file_lock->fl_flags = FL_POSIX;
4884 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4885 file_lock->fl_start = lock->lk_offset;
4886 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4887 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888
Jeff Layton21179d82012-08-21 08:03:32 -04004889 conflock = locks_alloc_lock();
4890 if (!conflock) {
4891 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4892 status = nfserr_jukebox;
4893 goto out;
4894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895
Jeff Layton21179d82012-08-21 08:03:32 -04004896 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004897 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004899 update_stateid(&lock_stp->st_stid.sc_stateid);
4900 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004902 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004903 break;
4904 case (EAGAIN): /* conflock holds conflicting lock */
4905 status = nfserr_denied;
4906 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04004907 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004908 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 case (EDEADLK):
4910 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004911 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004912 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004913 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004914 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004915 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917out:
Trond Myklebustde186432014-07-10 14:07:26 -04004918 if (filp)
4919 fput(filp);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004920 if (status && new_state)
Jeff Laytonc53530d2014-06-30 11:48:39 -04004921 release_lock_stateid(lock_stp);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004922 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004923 if (!cstate->replay_owner)
4924 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004925 if (file_lock)
4926 locks_free_lock(file_lock);
4927 if (conflock)
4928 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 return status;
4930}
4931
4932/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004933 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4934 * so we do a temporary open here just to get an open file to pass to
4935 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4936 * inode operation.)
4937 */
Al Viro04da6e92012-04-13 00:00:04 -04004938static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004939{
4940 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04004941 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4942 if (!err) {
4943 err = nfserrno(vfs_test_lock(file, lock));
4944 nfsd_close(file);
4945 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004946 return err;
4947}
4948
4949/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 * LOCKT operation
4951 */
Al Virob37ad282006-10-19 23:28:59 -07004952__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004953nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4954 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955{
Jeff Layton21179d82012-08-21 08:03:32 -04004956 struct file_lock *file_lock = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004957 struct nfs4_lockowner *lo;
Al Virob37ad282006-10-19 23:28:59 -07004958 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004959 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004961 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 return nfserr_grace;
4963
4964 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4965 return nfserr_inval;
4966
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 nfs4_lock_state();
4968
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004969 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004970 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004971 if (status)
4972 goto out;
4973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004975 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
Jeff Layton21179d82012-08-21 08:03:32 -04004978 file_lock = locks_alloc_lock();
4979 if (!file_lock) {
4980 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4981 status = nfserr_jukebox;
4982 goto out;
4983 }
4984 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 switch (lockt->lt_type) {
4986 case NFS4_READ_LT:
4987 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004988 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 break;
4990 case NFS4_WRITE_LT:
4991 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004992 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 break;
4994 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04004995 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 status = nfserr_inval;
4997 goto out;
4998 }
4999
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005000 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005001 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005002 file_lock->fl_owner = (fl_owner_t)lo;
5003 file_lock->fl_pid = current->tgid;
5004 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005
Jeff Layton21179d82012-08-21 08:03:32 -04005006 file_lock->fl_start = lockt->lt_offset;
5007 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008
Jeff Layton21179d82012-08-21 08:03:32 -04005009 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010
Jeff Layton21179d82012-08-21 08:03:32 -04005011 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005012 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005013 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005014
Jeff Layton21179d82012-08-21 08:03:32 -04005015 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005017 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 }
5019out:
5020 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005021 if (file_lock)
5022 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 return status;
5024}
5025
Al Virob37ad282006-10-19 23:28:59 -07005026__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005027nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005028 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005030 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005032 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005033 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005034 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005035 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5036
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5038 (long long) locku->lu_offset,
5039 (long long) locku->lu_length);
5040
5041 if (check_lock_length(locku->lu_offset, locku->lu_length))
5042 return nfserr_inval;
5043
5044 nfs4_lock_state();
5045
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005046 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005047 &locku->lu_stateid, NFS4_LOCK_STID,
5048 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005049 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005051 filp = find_any_file(stp->st_file);
5052 if (!filp) {
5053 status = nfserr_lock_range;
5054 goto out;
5055 }
Jeff Layton21179d82012-08-21 08:03:32 -04005056 file_lock = locks_alloc_lock();
5057 if (!file_lock) {
5058 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5059 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005060 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005061 }
5062 locks_init_lock(file_lock);
5063 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005064 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005065 file_lock->fl_pid = current->tgid;
5066 file_lock->fl_file = filp;
5067 file_lock->fl_flags = FL_POSIX;
5068 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5069 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070
Jeff Layton21179d82012-08-21 08:03:32 -04005071 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5072 locku->lu_length);
5073 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074
Jeff Layton21179d82012-08-21 08:03:32 -04005075 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005076 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005077 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 goto out_nfserr;
5079 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005080 update_stateid(&stp->st_stid.sc_stateid);
5081 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005082fput:
5083 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005085 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04005086 if (!cstate->replay_owner)
5087 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005088 if (file_lock)
5089 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 return status;
5091
5092out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005093 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005094 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095}
5096
5097/*
5098 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005099 * true: locks held by lockowner
5100 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005102static bool
5103check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104{
5105 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005106 int status = false;
5107 struct file *filp = find_any_file(fp);
5108 struct inode *inode;
5109
5110 if (!filp) {
5111 /* Any valid lock stateid should have some sort of access */
5112 WARN_ON_ONCE(1);
5113 return status;
5114 }
5115
5116 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117
Jeff Layton1c8c6012013-06-21 08:58:15 -04005118 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005120 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005121 status = true;
5122 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005125 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005126 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 return status;
5128}
5129
Al Virob37ad282006-10-19 23:28:59 -07005130__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005131nfsd4_release_lockowner(struct svc_rqst *rqstp,
5132 struct nfsd4_compound_state *cstate,
5133 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134{
5135 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Laytonfd449072014-06-30 11:48:41 -04005136 struct nfs4_stateowner *sop = NULL, *tmp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005137 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005138 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 struct xdr_netobj *owner = &rlockowner->rl_owner;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005140 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07005141 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005142 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143
5144 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5145 clid->cl_boot, clid->cl_id);
5146
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 nfs4_lock_state();
5148
Jeff Layton4b24ca72014-06-30 11:48:44 -04005149 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005150 if (status)
5151 goto out;
5152
NeilBrown3e9e3db2005-06-23 22:04:20 -07005153 status = nfserr_locks_held;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05005154
Jeff Laytonfd449072014-06-30 11:48:41 -04005155 /* Find the matching lock stateowner */
5156 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
5157 if (tmp->so_is_open_owner)
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005158 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005159 if (same_owner_str(tmp, owner, clid)) {
5160 sop = tmp;
5161 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07005163 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005164
5165 /* No matching owner found, maybe a replay? Just declare victory... */
5166 if (!sop) {
5167 status = nfs_ok;
5168 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005170
5171 lo = lockowner(sop);
5172 /* see if there are still any locks associated with it */
5173 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5174 if (check_for_locks(stp->st_file, lo))
5175 goto out;
5176 }
5177
5178 status = nfs_ok;
5179 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180out:
5181 nfs4_unlock_state();
5182 return status;
5183}
5184
5185static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005186alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187{
NeilBrowna55370a2005-06-23 22:03:52 -07005188 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189}
5190
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005191bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005192nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005193{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005194 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005195
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005196 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005197 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005198}
5199
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200/*
5201 * failure => all reset bets are off, nfserr_no_grace...
5202 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005203struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005204nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205{
5206 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005207 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208
NeilBrowna55370a2005-06-23 22:03:52 -07005209 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5210 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005211 if (crp) {
5212 strhashval = clientstr_hashval(name);
5213 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005214 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005215 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005216 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005217 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005218 }
5219 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220}
5221
Jeff Layton2a4317c2012-03-21 16:42:43 -04005222void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005223nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005224{
5225 list_del(&crp->cr_strhash);
5226 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005227 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005228}
5229
5230void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005231nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232{
5233 struct nfs4_client_reclaim *crp = NULL;
5234 int i;
5235
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005237 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5238 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005240 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 }
5242 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005243 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244}
5245
5246/*
5247 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005248struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005249nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250{
5251 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 struct nfs4_client_reclaim *crp = NULL;
5253
Jeff Layton278c9312012-11-12 15:00:52 -05005254 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255
Jeff Layton278c9312012-11-12 15:00:52 -05005256 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005257 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005258 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 return crp;
5260 }
5261 }
5262 return NULL;
5263}
5264
5265/*
5266* Called from OPEN. Look for clientid in reclaim list.
5267*/
Al Virob37ad282006-10-19 23:28:59 -07005268__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005269nfs4_check_open_reclaim(clientid_t *clid,
5270 struct nfsd4_compound_state *cstate,
5271 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005273 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005274
5275 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005276 status = lookup_clientid(clid, cstate, nn);
5277 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005278 return nfserr_reclaim_bad;
5279
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005280 if (nfsd4_client_record_check(cstate->clp))
5281 return nfserr_reclaim_bad;
5282
5283 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284}
5285
Bryan Schumaker65178db2011-11-01 13:35:21 -04005286#ifdef CONFIG_NFSD_FAULT_INJECTION
5287
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005288u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5289{
J. Bruce Fields221a6872013-04-01 22:23:49 -04005290 if (mark_client_expired(clp))
5291 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005292 expire_client(clp);
5293 return 1;
5294}
5295
Bryan Schumaker184c1842012-11-29 11:40:44 -05005296u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5297{
5298 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005299 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005300 printk(KERN_INFO "NFS Client: %s\n", buf);
5301 return 1;
5302}
5303
5304static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5305 const char *type)
5306{
5307 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005308 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005309 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5310}
5311
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005312static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5313 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005314{
5315 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005316 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005317 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005318 u64 count = 0;
5319
5320 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005321 list_for_each_entry_safe(stp, st_next,
5322 &oop->oo_owner.so_stateids, st_perstateowner) {
5323 list_for_each_entry_safe(lst, lst_next,
5324 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005325 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005326 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005327 if (++count == max)
5328 return count;
5329 }
5330 }
5331 }
5332
5333 return count;
5334}
5335
5336u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5337{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005338 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005339}
5340
Bryan Schumaker184c1842012-11-29 11:40:44 -05005341u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5342{
5343 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5344 nfsd_print_count(clp, count, "locked files");
5345 return count;
5346}
5347
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005348static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5349{
5350 struct nfs4_openowner *oop, *next;
5351 u64 count = 0;
5352
5353 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5354 if (func)
5355 func(oop);
5356 if (++count == max)
5357 break;
5358 }
5359
5360 return count;
5361}
5362
5363u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5364{
5365 return nfsd_foreach_client_open(clp, max, release_openowner);
5366}
5367
Bryan Schumaker184c1842012-11-29 11:40:44 -05005368u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5369{
5370 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5371 nfsd_print_count(clp, count, "open files");
5372 return count;
5373}
5374
Bryan Schumaker269de302012-11-29 11:40:42 -05005375static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5376 struct list_head *victims)
5377{
5378 struct nfs4_delegation *dp, *next;
5379 u64 count = 0;
5380
Benny Halevycdc97502014-05-30 09:09:30 -04005381 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005382 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005383 if (victims) {
5384 /*
5385 * It's not safe to mess with delegations that have a
5386 * non-zero dl_time. They might have already been broken
5387 * and could be processed by the laundromat outside of
5388 * the state_lock. Just leave them be.
5389 */
5390 if (dp->dl_time != 0)
5391 continue;
5392
Jeff Layton42690672014-07-25 07:34:20 -04005393 unhash_delegation_locked(dp);
5394 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005395 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005396 if (++count == max)
5397 break;
5398 }
5399 return count;
5400}
5401
5402u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5403{
5404 struct nfs4_delegation *dp, *next;
5405 LIST_HEAD(victims);
5406 u64 count;
5407
Benny Halevycdc97502014-05-30 09:09:30 -04005408 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005409 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005410 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005411
Jeff Layton2d4a5322014-07-25 07:34:21 -04005412 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5413 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005414 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005415 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005416
5417 return count;
5418}
5419
5420u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5421{
Jeff Laytondff13992014-07-08 14:02:49 -04005422 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005423 LIST_HEAD(victims);
5424 u64 count;
5425
Benny Halevycdc97502014-05-30 09:09:30 -04005426 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005427 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005428 while (!list_empty(&victims)) {
5429 dp = list_first_entry(&victims, struct nfs4_delegation,
5430 dl_recall_lru);
5431 list_del_init(&dp->dl_recall_lru);
5432 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005433 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005434 }
Benny Halevycdc97502014-05-30 09:09:30 -04005435 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005436
5437 return count;
5438}
5439
Bryan Schumaker184c1842012-11-29 11:40:44 -05005440u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5441{
5442 u64 count = 0;
5443
Benny Halevycdc97502014-05-30 09:09:30 -04005444 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005445 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005446 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005447
5448 nfsd_print_count(clp, count, "delegations");
5449 return count;
5450}
5451
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005452u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005453{
5454 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005455 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005456 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005457
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005458 if (!nfsd_netns_ready(nn))
5459 return 0;
5460
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005461 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005462 count += func(clp, max - count);
5463 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005464 break;
5465 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005466
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005467 return count;
5468}
5469
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005470struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5471{
5472 struct nfs4_client *clp;
5473 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5474
5475 if (!nfsd_netns_ready(nn))
5476 return NULL;
5477
5478 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5479 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5480 return clp;
5481 }
5482 return NULL;
5483}
5484
Bryan Schumaker65178db2011-11-01 13:35:21 -04005485#endif /* CONFIG_NFSD_FAULT_INJECTION */
5486
Meelap Shahc2f1a552007-07-17 04:04:39 -07005487/*
5488 * Since the lifetime of a delegation isn't limited to that of an open, a
5489 * client may quite reasonably hang on to a delegation as long as it has
5490 * the inode cached. This becomes an obvious problem the first time a
5491 * client's inode cache approaches the size of the server's total memory.
5492 *
5493 * For now we avoid this problem by imposing a hard limit on the number
5494 * of delegations, which varies according to the server's memory size.
5495 */
5496static void
5497set_max_delegations(void)
5498{
5499 /*
5500 * Allow at most 4 delegations per megabyte of RAM. Quick
5501 * estimates suggest that in the worst case (where every delegation
5502 * is for a different inode), a delegation could take about 1.5K,
5503 * giving a worst case usage of about 6% of memory.
5504 */
5505 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5506}
5507
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005508static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005509{
5510 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5511 int i;
5512
5513 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5514 CLIENT_HASH_SIZE, GFP_KERNEL);
5515 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005516 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005517 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5518 CLIENT_HASH_SIZE, GFP_KERNEL);
5519 if (!nn->unconf_id_hashtbl)
5520 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005521 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5522 OWNER_HASH_SIZE, GFP_KERNEL);
5523 if (!nn->ownerstr_hashtbl)
5524 goto err_ownerstr;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005525 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5526 SESSION_HASH_SIZE, GFP_KERNEL);
5527 if (!nn->sessionid_hashtbl)
5528 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005529
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005530 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005531 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005532 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005533 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005534 for (i = 0; i < OWNER_HASH_SIZE; i++)
5535 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005536 for (i = 0; i < SESSION_HASH_SIZE; i++)
5537 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005538 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005539 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005540 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005541 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005542 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005543 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005544
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005545 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005546 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005547
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005548 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005549
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005550err_sessionid:
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03005551 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005552err_ownerstr:
5553 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005554err_unconf_id:
5555 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005556err:
5557 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005558}
5559
5560static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005561nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005562{
5563 int i;
5564 struct nfs4_client *clp = NULL;
5565 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5566
5567 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5568 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5569 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5570 destroy_client(clp);
5571 }
5572 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005573
Kinglong Mee2b905632014-03-26 22:09:30 +08005574 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5575 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5576 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5577 destroy_client(clp);
5578 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005579 }
5580
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005581 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005582 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005583 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005584 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005585 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005586}
5587
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005588int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005589nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005590{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005591 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005592 int ret;
5593
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005594 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005595 if (ret)
5596 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005597 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005598 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005599 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005600 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005601 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005602 nn->nfsd4_grace, net);
5603 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005604 return 0;
5605}
5606
5607/* initialization to perform when the nfsd service is started: */
5608
5609int
5610nfs4_state_start(void)
5611{
5612 int ret;
5613
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005614 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005615 if (ret)
5616 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005617 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005618 if (laundry_wq == NULL) {
5619 ret = -ENOMEM;
5620 goto out_recovery;
5621 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005622 ret = nfsd4_create_callback_queue();
5623 if (ret)
5624 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005625
Meelap Shahc2f1a552007-07-17 04:04:39 -07005626 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005627
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005628 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005629
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005630out_free_laundry:
5631 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005632out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005633 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634}
5635
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005636void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005637nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005641 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005643 cancel_delayed_work_sync(&nn->laundromat_work);
5644 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005645
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005646 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04005648 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005649 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005651 unhash_delegation_locked(dp);
5652 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 }
Benny Halevycdc97502014-05-30 09:09:30 -04005654 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 list_for_each_safe(pos, next, &reaplist) {
5656 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005657 list_del_init(&dp->dl_recall_lru);
5658 nfs4_put_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 }
5660
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005661 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005662 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005663 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664}
5665
5666void
5667nfs4_state_shutdown(void)
5668{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005669 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005670 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005672
5673static void
5674get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5675{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005676 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5677 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005678}
5679
5680static void
5681put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5682{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005683 if (cstate->minorversion) {
5684 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5685 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5686 }
5687}
5688
5689void
5690clear_current_stateid(struct nfsd4_compound_state *cstate)
5691{
5692 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005693}
5694
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005695/*
5696 * functions to set current state id
5697 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005698void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005699nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5700{
5701 put_stateid(cstate, &odp->od_stateid);
5702}
5703
5704void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005705nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5706{
5707 put_stateid(cstate, &open->op_stateid);
5708}
5709
5710void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005711nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5712{
5713 put_stateid(cstate, &close->cl_stateid);
5714}
5715
5716void
5717nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5718{
5719 put_stateid(cstate, &lock->lk_resp_stateid);
5720}
5721
5722/*
5723 * functions to consume current state id
5724 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005725
5726void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005727nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5728{
5729 get_stateid(cstate, &odp->od_stateid);
5730}
5731
5732void
5733nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5734{
5735 get_stateid(cstate, &drp->dr_stateid);
5736}
5737
5738void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005739nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5740{
5741 get_stateid(cstate, &fsp->fr_stateid);
5742}
5743
5744void
5745nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5746{
5747 get_stateid(cstate, &setattr->sa_stateid);
5748}
5749
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005750void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005751nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5752{
5753 get_stateid(cstate, &close->cl_stateid);
5754}
5755
5756void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005757nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005758{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005759 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005760}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01005761
5762void
5763nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5764{
5765 get_stateid(cstate, &read->rd_stateid);
5766}
5767
5768void
5769nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5770{
5771 get_stateid(cstate, &write->wr_stateid);
5772}