blob: da88b31c0afe01e6eacda1b6bcd35d8fd1580887 [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 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040073static int check_for_locks(struct nfs4_file *filp, 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{
Benny Halevycdc97502014-05-30 09:09:30 -0400257 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton89876f82013-04-02 09:01:59 -0400258 hlist_del(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400259 spin_unlock(&state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800260 iput(fi->fi_inode);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400261 nfsd4_free_file(fi);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800262 }
NeilBrown13cd2182005-06-23 22:03:10 -0700263}
264
265static inline void
266get_nfs4_file(struct nfs4_file *fi)
267{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800268 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700269}
270
Trond Myklebustde186432014-07-10 14:07:26 -0400271static struct file *
272__nfs4_get_fd(struct nfs4_file *f, int oflag)
273{
274 if (f->fi_fds[oflag])
275 return get_file(f->fi_fds[oflag]);
276 return NULL;
277}
278
279static struct file *
280find_writeable_file_locked(struct nfs4_file *f)
281{
282 struct file *ret;
283
284 lockdep_assert_held(&f->fi_lock);
285
286 ret = __nfs4_get_fd(f, O_WRONLY);
287 if (!ret)
288 ret = __nfs4_get_fd(f, O_RDWR);
289 return ret;
290}
291
292static struct file *
293find_writeable_file(struct nfs4_file *f)
294{
295 struct file *ret;
296
297 spin_lock(&f->fi_lock);
298 ret = find_writeable_file_locked(f);
299 spin_unlock(&f->fi_lock);
300
301 return ret;
302}
303
304static struct file *find_readable_file_locked(struct nfs4_file *f)
305{
306 struct file *ret;
307
308 lockdep_assert_held(&f->fi_lock);
309
310 ret = __nfs4_get_fd(f, O_RDONLY);
311 if (!ret)
312 ret = __nfs4_get_fd(f, O_RDWR);
313 return ret;
314}
315
316static struct file *
317find_readable_file(struct nfs4_file *f)
318{
319 struct file *ret;
320
321 spin_lock(&f->fi_lock);
322 ret = find_readable_file_locked(f);
323 spin_unlock(&f->fi_lock);
324
325 return ret;
326}
327
328static struct file *
329find_any_file(struct nfs4_file *f)
330{
331 struct file *ret;
332
333 spin_lock(&f->fi_lock);
334 ret = __nfs4_get_fd(f, O_RDWR);
335 if (!ret) {
336 ret = __nfs4_get_fd(f, O_WRONLY);
337 if (!ret)
338 ret = __nfs4_get_fd(f, O_RDONLY);
339 }
340 spin_unlock(&f->fi_lock);
341 return ret;
342}
343
NeilBrownef0f3392006-04-10 22:55:41 -0700344static int num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800345unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700346
347/*
348 * Open owner state (share locks)
349 */
350
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500351/* hash tables for lock and open owners */
352#define OWNER_HASH_BITS 8
353#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
354#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700355
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500356static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400357{
358 unsigned int ret;
359
360 ret = opaque_hashval(ownername->data, ownername->len);
361 ret += clientid;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500362 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400363}
NeilBrownef0f3392006-04-10 22:55:41 -0700364
NeilBrownef0f3392006-04-10 22:55:41 -0700365/* hash table for nfs4_file */
366#define FILE_HASH_BITS 8
367#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800368
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400369static unsigned int file_hashval(struct inode *ino)
370{
371 /* XXX: why are we hashing on inode pointer, anyway? */
372 return hash_ptr(ino, FILE_HASH_BITS);
373}
374
Jeff Layton89876f82013-04-02 09:01:59 -0400375static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700376
Jeff Layton12659652014-07-10 14:07:28 -0400377static void
378__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400379{
Jeff Layton7214e862014-07-10 14:07:33 -0400380 lockdep_assert_held(&fp->fi_lock);
381
Jeff Layton12659652014-07-10 14:07:28 -0400382 if (access & NFS4_SHARE_ACCESS_WRITE)
383 atomic_inc(&fp->fi_access[O_WRONLY]);
384 if (access & NFS4_SHARE_ACCESS_READ)
385 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400386}
387
Jeff Layton12659652014-07-10 14:07:28 -0400388static __be32
389nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400390{
Jeff Layton7214e862014-07-10 14:07:33 -0400391 lockdep_assert_held(&fp->fi_lock);
392
Jeff Layton12659652014-07-10 14:07:28 -0400393 /* Does this access mode make sense? */
394 if (access & ~NFS4_SHARE_ACCESS_BOTH)
395 return nfserr_inval;
396
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400397 /* Does it conflict with a deny mode already set? */
398 if ((access & fp->fi_share_deny) != 0)
399 return nfserr_share_denied;
400
Jeff Layton12659652014-07-10 14:07:28 -0400401 __nfs4_file_get_access(fp, access);
402 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400403}
404
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400405static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
406{
407 /* Common case is that there is no deny mode. */
408 if (deny) {
409 /* Does this deny mode make sense? */
410 if (deny & ~NFS4_SHARE_DENY_BOTH)
411 return nfserr_inval;
412
413 if ((deny & NFS4_SHARE_DENY_READ) &&
414 atomic_read(&fp->fi_access[O_RDONLY]))
415 return nfserr_share_denied;
416
417 if ((deny & NFS4_SHARE_DENY_WRITE) &&
418 atomic_read(&fp->fi_access[O_WRONLY]))
419 return nfserr_share_denied;
420 }
421 return nfs_ok;
422}
423
J. Bruce Fields998db522010-08-07 09:21:41 -0400424static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400425{
Trond Myklebustde186432014-07-10 14:07:26 -0400426 might_lock(&fp->fi_lock);
427
428 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
429 struct file *f1 = NULL;
430 struct file *f2 = NULL;
431
Jeff Layton6d338b52014-07-10 14:07:29 -0400432 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400433 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400434 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400435 spin_unlock(&fp->fi_lock);
436 if (f1)
437 fput(f1);
438 if (f2)
439 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400440 }
441}
442
Jeff Layton12659652014-07-10 14:07:28 -0400443static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400444{
Jeff Layton12659652014-07-10 14:07:28 -0400445 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
446
447 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400448 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400449 if (access & NFS4_SHARE_ACCESS_READ)
450 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400451}
452
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500453static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct
454kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400455{
456 struct idr *stateids = &cl->cl_stateids;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500457 struct nfs4_stid *stid;
458 int new_id;
459
460 stid = kmem_cache_alloc(slab, GFP_KERNEL);
461 if (!stid)
462 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400463
Jeff Layton398c33a2013-04-29 16:21:20 -0700464 new_id = idr_alloc_cyclic(stateids, stid, 0, 0, GFP_KERNEL);
Tejun Heoebd6c702013-03-13 14:59:37 -0700465 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500466 goto out_free;
467 stid->sc_client = cl;
468 stid->sc_type = 0;
469 stid->sc_stateid.si_opaque.so_id = new_id;
470 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
471 /* Will be incremented before return to client: */
472 stid->sc_stateid.si_generation = 0;
473
J. Bruce Fields996e0932011-10-17 11:14:48 -0400474 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500475 * It shouldn't be a problem to reuse an opaque stateid value.
476 * I don't think it is for 4.1. But with 4.0 I worry that, for
477 * example, a stray write retransmission could be accepted by
478 * the server when it should have been rejected. Therefore,
479 * adopt a trick from the sctp code to attempt to maximize the
480 * amount of time until an id is reused, by ensuring they always
481 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400482 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500483 return stid;
484out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800485 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500486 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400487}
488
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400489static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
490{
491 return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
492}
493
NeilBrown6282cd52014-06-04 17:39:26 +1000494/*
495 * When we recall a delegation, we should be careful not to hand it
496 * out again straight away.
497 * To ensure this we keep a pair of bloom filters ('new' and 'old')
498 * in which the filehandles of recalled delegations are "stored".
499 * If a filehandle appear in either filter, a delegation is blocked.
500 * When a delegation is recalled, the filehandle is stored in the "new"
501 * filter.
502 * Every 30 seconds we swap the filters and clear the "new" one,
503 * unless both are empty of course.
504 *
505 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
506 * low 3 bytes as hash-table indices.
507 *
508 * 'state_lock', which is always held when block_delegations() is called,
509 * is used to manage concurrent access. Testing does not need the lock
510 * except when swapping the two filters.
511 */
512static struct bloom_pair {
513 int entries, old_entries;
514 time_t swap_time;
515 int new; /* index into 'set' */
516 DECLARE_BITMAP(set[2], 256);
517} blocked_delegations;
518
519static int delegation_blocked(struct knfsd_fh *fh)
520{
521 u32 hash;
522 struct bloom_pair *bd = &blocked_delegations;
523
524 if (bd->entries == 0)
525 return 0;
526 if (seconds_since_boot() - bd->swap_time > 30) {
527 spin_lock(&state_lock);
528 if (seconds_since_boot() - bd->swap_time > 30) {
529 bd->entries -= bd->old_entries;
530 bd->old_entries = bd->entries;
531 memset(bd->set[bd->new], 0,
532 sizeof(bd->set[0]));
533 bd->new = 1-bd->new;
534 bd->swap_time = seconds_since_boot();
535 }
536 spin_unlock(&state_lock);
537 }
538 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
539 if (test_bit(hash&255, bd->set[0]) &&
540 test_bit((hash>>8)&255, bd->set[0]) &&
541 test_bit((hash>>16)&255, bd->set[0]))
542 return 1;
543
544 if (test_bit(hash&255, bd->set[1]) &&
545 test_bit((hash>>8)&255, bd->set[1]) &&
546 test_bit((hash>>16)&255, bd->set[1]))
547 return 1;
548
549 return 0;
550}
551
552static void block_delegations(struct knfsd_fh *fh)
553{
554 u32 hash;
555 struct bloom_pair *bd = &blocked_delegations;
556
557 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
558
559 __set_bit(hash&255, bd->set[bd->new]);
560 __set_bit((hash>>8)&255, bd->set[bd->new]);
561 __set_bit((hash>>16)&255, bd->set[bd->new]);
562 if (bd->entries == 0)
563 bd->swap_time = seconds_since_boot();
564 bd->entries += 1;
565}
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567static struct nfs4_delegation *
J. Bruce Fields99c41512013-05-21 16:21:25 -0400568alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 dprintk("NFSD alloc_init_deleg\n");
Meelap Shahc2f1a552007-07-17 04:04:39 -0700573 if (num_delegations > max_delegations)
NeilBrownef0f3392006-04-10 22:55:41 -0700574 return NULL;
NeilBrown6282cd52014-06-04 17:39:26 +1000575 if (delegation_blocked(&current_fh->fh_handle))
576 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400577 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700578 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return dp;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400580 /*
581 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400582 * meaning of seqid 0 isn't meaningful, really, but let's avoid
583 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400584 */
585 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownef0f3392006-04-10 22:55:41 -0700586 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700587 INIT_LIST_HEAD(&dp->dl_perfile);
588 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -0400590 dp->dl_file = NULL;
J. Bruce Fields99c41512013-05-21 16:21:25 -0400591 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
J. Bruce Fields6c02eaa2009-02-02 17:30:51 -0500592 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 dp->dl_time = 0;
594 atomic_set(&dp->dl_count, 1);
J. Bruce Fields57725152012-11-05 15:10:26 -0500595 nfsd4_init_callback(&dp->dl_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return dp;
597}
598
J. Bruce Fields68a33962013-03-21 11:21:50 -0400599static void remove_stid(struct nfs4_stid *s)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500600{
601 struct idr *stateids = &s->sc_client->cl_stateids;
602
603 idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500604}
605
Benny Halevy9857df82013-10-14 13:44:52 +0300606static void nfs4_free_stid(struct kmem_cache *slab, struct nfs4_stid *s)
607{
608 kmem_cache_free(slab, s);
609}
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611void
612nfs4_put_delegation(struct nfs4_delegation *dp)
613{
614 if (atomic_dec_and_test(&dp->dl_count)) {
Benny Halevy9857df82013-10-14 13:44:52 +0300615 nfs4_free_stid(deleg_slab, &dp->dl_stid);
NeilBrownef0f3392006-04-10 22:55:41 -0700616 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618}
619
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500620static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -0500622 if (!fp->fi_lease)
623 return;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500624 if (atomic_dec_and_test(&fp->fi_delegees)) {
625 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
626 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400627 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500628 fp->fi_deleg_file = NULL;
629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400632static void unhash_stid(struct nfs4_stid *s)
633{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500634 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400635}
636
Benny Halevy931ee562014-05-30 09:09:27 -0400637static void
638hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
639{
Benny Halevycdc97502014-05-30 09:09:30 -0400640 lockdep_assert_held(&state_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400641
Benny Halevy3fb87d12014-05-30 09:09:31 -0400642 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400643 list_add(&dp->dl_perfile, &fp->fi_delegations);
644 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
645}
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647/* Called under the state lock. */
648static void
649unhash_delegation(struct nfs4_delegation *dp)
650{
Benny Halevycdc97502014-05-30 09:09:30 -0400651 spin_lock(&state_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400652 list_del_init(&dp->dl_perclnt);
J. Bruce Fields5d926e82011-02-07 16:53:46 -0500653 list_del_init(&dp->dl_perfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 list_del_init(&dp->dl_recall_lru);
Benny Halevycdc97502014-05-30 09:09:30 -0400655 spin_unlock(&state_lock);
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -0500656 if (dp->dl_file) {
657 nfs4_put_deleg_lease(dp->dl_file);
658 put_nfs4_file(dp->dl_file);
659 dp->dl_file = NULL;
660 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400661}
662
663
664
665static void destroy_revoked_delegation(struct nfs4_delegation *dp)
666{
667 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields68a33962013-03-21 11:21:50 -0400668 remove_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 nfs4_put_delegation(dp);
670}
671
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400672static void destroy_delegation(struct nfs4_delegation *dp)
673{
674 unhash_delegation(dp);
675 remove_stid(&dp->dl_stid);
676 nfs4_put_delegation(dp);
677}
678
679static void revoke_delegation(struct nfs4_delegation *dp)
680{
681 struct nfs4_client *clp = dp->dl_stid.sc_client;
682
683 if (clp->cl_minorversion == 0)
684 destroy_delegation(dp);
685 else {
686 unhash_delegation(dp);
687 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
688 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
689 }
690}
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692/*
693 * SETCLIENTID state
694 */
695
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400696static unsigned int clientid_hashval(u32 id)
697{
698 return id & CLIENT_HASH_MASK;
699}
700
701static unsigned int clientstr_hashval(const char *name)
702{
703 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
704}
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400707 * We store the NONE, READ, WRITE, and BOTH bits separately in the
708 * st_{access,deny}_bmap field of the stateid, in order to track not
709 * only what share bits are currently in force, but also what
710 * combinations of share bits previous opens have used. This allows us
711 * to enforce the recommendation of rfc 3530 14.2.19 that the server
712 * return an error if the client attempt to downgrade to a combination
713 * of share bits not explicable by closing some of its previous opens.
714 *
715 * XXX: This enforcement is actually incomplete, since we don't keep
716 * track of access/deny bit combinations; so, e.g., we allow:
717 *
718 * OPEN allow read, deny write
719 * OPEN allow both, deny none
720 * DOWNGRADE allow read, deny none
721 *
722 * which we should reject.
723 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400724static unsigned int
725bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400726 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400727 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400728
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400729 for (i = 1; i < 4; i++) {
730 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400731 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400732 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400733 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400734}
735
Jeff Layton82c5ff12012-05-11 09:45:13 -0400736/* set share access for a given stateid */
737static inline void
738set_access(u32 access, struct nfs4_ol_stateid *stp)
739{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400740 unsigned char mask = 1 << access;
741
742 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
743 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400744}
745
746/* clear share access for a given stateid */
747static inline void
748clear_access(u32 access, struct nfs4_ol_stateid *stp)
749{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400750 unsigned char mask = 1 << access;
751
752 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
753 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400754}
755
756/* test whether a given stateid has access */
757static inline bool
758test_access(u32 access, struct nfs4_ol_stateid *stp)
759{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400760 unsigned char mask = 1 << access;
761
762 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400763}
764
Jeff Laytonce0fc432012-05-11 09:45:14 -0400765/* set share deny for a given stateid */
766static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400767set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400768{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400769 unsigned char mask = 1 << deny;
770
771 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
772 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400773}
774
775/* clear share deny for a given stateid */
776static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400777clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400778{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400779 unsigned char mask = 1 << deny;
780
781 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
782 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400783}
784
785/* test whether a given stateid is denying specific access */
786static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400787test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400788{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400789 unsigned char mask = 1 << deny;
790
791 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400792}
793
794static int nfs4_access_to_omode(u32 access)
795{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400796 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400797 case NFS4_SHARE_ACCESS_READ:
798 return O_RDONLY;
799 case NFS4_SHARE_ACCESS_WRITE:
800 return O_WRONLY;
801 case NFS4_SHARE_ACCESS_BOTH:
802 return O_RDWR;
803 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500804 WARN_ON_ONCE(1);
805 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400806}
807
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400808/*
809 * A stateid that had a deny mode associated with it is being released
810 * or downgraded. Recalculate the deny mode on the file.
811 */
812static void
813recalculate_deny_mode(struct nfs4_file *fp)
814{
815 struct nfs4_ol_stateid *stp;
816
817 spin_lock(&fp->fi_lock);
818 fp->fi_share_deny = 0;
819 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
820 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
821 spin_unlock(&fp->fi_lock);
822}
823
824static void
825reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
826{
827 int i;
828 bool change = false;
829
830 for (i = 1; i < 4; i++) {
831 if ((i & deny) != i) {
832 change = true;
833 clear_deny(i, stp);
834 }
835 }
836
837 /* Recalculate per-file deny mode if there was a change */
838 if (change)
839 recalculate_deny_mode(stp->st_file);
840}
841
Jeff Layton82c5ff12012-05-11 09:45:13 -0400842/* release all access and file references for a given stateid */
843static void
844release_all_access(struct nfs4_ol_stateid *stp)
845{
846 int i;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400847 struct nfs4_file *fp = stp->st_file;
848
849 if (fp && stp->st_deny_bmap != 0)
850 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400851
852 for (i = 1; i < 4; i++) {
853 if (test_access(i, stp))
Jeff Layton12659652014-07-10 14:07:28 -0400854 nfs4_file_put_access(stp->st_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400855 clear_access(i, stp);
856 }
857}
858
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400859static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500860{
Trond Myklebust1d31a252014-07-10 14:07:25 -0400861 struct nfs4_file *fp = stp->st_file;
862
863 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500864 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400865 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500866 list_del(&stp->st_perstateowner);
867}
868
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400869static void close_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500870{
Jeff Layton82c5ff12012-05-11 09:45:13 -0400871 release_all_access(stp);
OGAWA Hirofumia96e5b92011-04-18 11:48:55 -0400872 put_nfs4_file(stp->st_file);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400873 stp->st_file = NULL;
874}
875
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400876static void free_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400877{
J. Bruce Fields68a33962013-03-21 11:21:50 -0400878 remove_stid(&stp->st_stid);
Benny Halevy9857df82013-10-14 13:44:52 +0300879 nfs4_free_stid(stateid_slab, &stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500880}
881
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400882static void __release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500883{
884 struct file *file;
885
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400886 list_del(&stp->st_locks);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500887 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400888 unhash_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500889 file = find_any_file(stp->st_file);
Trond Myklebuste20fcf12014-07-10 14:07:27 -0400890 if (file)
891 filp_close(file, (fl_owner_t)lockowner(stp->st_stateowner));
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400892 close_generic_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500893 free_generic_stateid(stp);
894}
895
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400896static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500897{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400898 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500899
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400900 list_del(&lo->lo_owner.so_strhash);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400901 while (!list_empty(&lo->lo_owner.so_stateids)) {
902 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400903 struct nfs4_ol_stateid, st_perstateowner);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400904 __release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500905 }
906}
907
Trond Myklebust50cc6232014-04-18 14:44:03 -0400908static void nfs4_free_lockowner(struct nfs4_lockowner *lo)
909{
910 kfree(lo->lo_owner.so_owner.data);
911 kmem_cache_free(lockowner_slab, lo);
912}
913
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400914static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500915{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400916 unhash_lockowner(lo);
917 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500918}
919
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400920static void release_lockowner_if_empty(struct nfs4_lockowner *lo)
921{
922 if (list_empty(&lo->lo_owner.so_stateids))
923 release_lockowner(lo);
924}
925
926static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500927{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400928 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500929
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400930 lo = lockowner(stp->st_stateowner);
931 __release_lock_stateid(stp);
932 release_lockowner_if_empty(lo);
933}
934
935static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
936{
937 struct nfs4_ol_stateid *stp;
938
939 while (!list_empty(&open_stp->st_locks)) {
940 stp = list_entry(open_stp->st_locks.next,
941 struct nfs4_ol_stateid, st_locks);
942 release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500943 }
944}
945
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400946static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500947{
948 unhash_generic_stateid(stp);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400949 release_open_stateid_locks(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400950 close_generic_stateid(stp);
951}
952
953static void release_open_stateid(struct nfs4_ol_stateid *stp)
954{
955 unhash_open_stateid(stp);
J. Bruce Fields22839632009-01-11 14:27:17 -0500956 free_generic_stateid(stp);
957}
958
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400959static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500960{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400961 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500962
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400963 list_del(&oo->oo_owner.so_strhash);
964 list_del(&oo->oo_perclient);
965 while (!list_empty(&oo->oo_owner.so_stateids)) {
966 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400967 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500968 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500969 }
970}
971
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400972static void release_last_closed_stateid(struct nfs4_openowner *oo)
973{
974 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
975
976 if (s) {
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400977 free_generic_stateid(s);
978 oo->oo_last_closed_stid = NULL;
979 }
980}
981
Trond Myklebust50cc6232014-04-18 14:44:03 -0400982static void nfs4_free_openowner(struct nfs4_openowner *oo)
983{
984 kfree(oo->oo_owner.so_owner.data);
985 kmem_cache_free(openowner_slab, oo);
986}
987
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400988static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500989{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400990 unhash_openowner(oo);
991 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400992 release_last_closed_stateid(oo);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400993 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500994}
995
Marc Eshel5282fd72009-04-03 08:27:52 +0300996static inline int
997hash_sessionid(struct nfs4_sessionid *sessionid)
998{
999 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1000
1001 return sid->sequence % SESSION_HASH_SIZE;
1002}
1003
Trond Myklebust8f199b82012-03-20 15:11:17 -04001004#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001005static inline void
1006dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1007{
1008 u32 *ptr = (u32 *)(&sessionid->data[0]);
1009 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1010}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001011#else
1012static inline void
1013dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1014{
1015}
1016#endif
1017
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001018/*
1019 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1020 * won't be used for replay.
1021 */
1022void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1023{
1024 struct nfs4_stateowner *so = cstate->replay_owner;
1025
1026 if (nfserr == nfserr_replay_me)
1027 return;
1028
1029 if (!seqid_mutating_err(ntohl(nfserr))) {
1030 cstate->replay_owner = NULL;
1031 return;
1032 }
1033 if (!so)
1034 return;
1035 if (so->so_is_open_owner)
1036 release_last_closed_stateid(openowner(so));
1037 so->so_seqid++;
1038 return;
1039}
Marc Eshel5282fd72009-04-03 08:27:52 +03001040
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001041static void
1042gen_sessionid(struct nfsd4_session *ses)
1043{
1044 struct nfs4_client *clp = ses->se_client;
1045 struct nfsd4_sessionid *sid;
1046
1047 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1048 sid->clientid = clp->cl_clientid;
1049 sid->sequence = current_sessionid++;
1050 sid->reserved = 0;
1051}
1052
1053/*
Andy Adamsona6496372009-08-28 08:45:01 -04001054 * The protocol defines ca_maxresponssize_cached to include the size of
1055 * the rpc header, but all we need to cache is the data starting after
1056 * the end of the initial SEQUENCE operation--the rest we regenerate
1057 * each time. Therefore we can advertise a ca_maxresponssize_cached
1058 * value that is the number of bytes in our cache plus a few additional
1059 * bytes. In order to stay on the safe side, and not promise more than
1060 * we can cache, those additional bytes must be the minimum possible: 24
1061 * bytes of rpc header (xid through accept state, with AUTH_NULL
1062 * verifier), 12 for the compound header (with zero-length tag), and 44
1063 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001064 */
Andy Adamsona6496372009-08-28 08:45:01 -04001065#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1066
Andy Adamson557ce262009-08-28 08:45:04 -04001067static void
1068free_session_slots(struct nfsd4_session *ses)
1069{
1070 int i;
1071
1072 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1073 kfree(ses->se_slots[i]);
1074}
1075
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001076/*
1077 * We don't actually need to cache the rpc and session headers, so we
1078 * can allocate a little less for each slot:
1079 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001080static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001081{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001082 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001083
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001084 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1085 size = 0;
1086 else
1087 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1088 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001089}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001090
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001091/*
1092 * XXX: If we run out of reserved DRC memory we could (up to a point)
1093 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001094 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001095 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001096static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001097{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001098 u32 slotsize = slot_bytes(ca);
1099 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001100 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001101
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001102 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001103 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1104 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001105 num = min_t(int, num, avail / slotsize);
1106 nfsd_drc_mem_used += num * slotsize;
1107 spin_unlock(&nfsd_drc_lock);
1108
1109 return num;
1110}
1111
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001112static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001113{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001114 int slotsize = slot_bytes(ca);
1115
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001116 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001117 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001118 spin_unlock(&nfsd_drc_lock);
1119}
1120
Kinglong Mee60810e52014-01-01 00:35:47 +08001121static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1122 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001123{
Kinglong Mee60810e52014-01-01 00:35:47 +08001124 int numslots = fattrs->maxreqs;
1125 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001126 struct nfsd4_session *new;
1127 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001128
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001129 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001130 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1131 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001132
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001133 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001134 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001135 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001136 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001137 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001138 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001139 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001140 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001141 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001142
1143 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1144 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1145
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001146 return new;
1147out_free:
1148 while (i--)
1149 kfree(new->se_slots[i]);
1150 kfree(new);
1151 return NULL;
1152}
1153
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001154static void free_conn(struct nfsd4_conn *c)
1155{
1156 svc_xprt_put(c->cn_xprt);
1157 kfree(c);
1158}
1159
1160static void nfsd4_conn_lost(struct svc_xpt_user *u)
1161{
1162 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1163 struct nfs4_client *clp = c->cn_session->se_client;
1164
1165 spin_lock(&clp->cl_lock);
1166 if (!list_empty(&c->cn_persession)) {
1167 list_del(&c->cn_persession);
1168 free_conn(c);
1169 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001170 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001171 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001172}
1173
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001174static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001175{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001176 struct nfsd4_conn *conn;
1177
1178 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1179 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001180 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001181 svc_xprt_get(rqstp->rq_xprt);
1182 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001183 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001184 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1185 return conn;
1186}
1187
J. Bruce Fields328ead22010-09-29 16:11:06 -04001188static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1189{
1190 conn->cn_session = ses;
1191 list_add(&conn->cn_persession, &ses->se_conns);
1192}
1193
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001194static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1195{
1196 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001197
1198 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001199 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001200 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001201}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001202
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001203static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001204{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001205 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001206 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001207}
1208
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001209static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001210{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001211 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001212
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001213 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001214 ret = nfsd4_register_conn(conn);
1215 if (ret)
1216 /* oops; xprt is already down: */
1217 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields6a3b1562012-09-12 09:59:17 -04001218 if (conn->cn_flags & NFS4_CDFC4_BACK) {
Weston Andros Adamson24119672012-05-24 15:42:17 -04001219 /* callback channel may be back up */
1220 nfsd4_probe_callback(ses->se_client);
1221 }
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001222}
1223
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001224static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001225{
1226 u32 dir = NFS4_CDFC4_FORE;
1227
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001228 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001229 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001230 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001231}
1232
1233/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001234static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001235{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001236 struct nfs4_client *clp = s->se_client;
1237 struct nfsd4_conn *c;
1238
1239 spin_lock(&clp->cl_lock);
1240 while (!list_empty(&s->se_conns)) {
1241 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1242 list_del_init(&c->cn_persession);
1243 spin_unlock(&clp->cl_lock);
1244
1245 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1246 free_conn(c);
1247
1248 spin_lock(&clp->cl_lock);
1249 }
1250 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001251}
1252
J. Bruce Fields1377b692012-09-11 21:42:40 -04001253static void __free_session(struct nfsd4_session *ses)
1254{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001255 free_session_slots(ses);
1256 kfree(ses);
1257}
1258
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001259static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001260{
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001261 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1262
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001263 lockdep_assert_held(&nn->client_lock);
1264 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001265 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001266 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001267}
Andy Adamson557ce262009-08-28 08:45:04 -04001268
Fengguang Wu135ae822012-11-10 07:20:25 -05001269static 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 -04001270{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001271 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001272 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001273
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001274 new->se_client = clp;
1275 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001276
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001277 INIT_LIST_HEAD(&new->se_conns);
1278
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001279 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001280 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001281 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001282 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001283 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001284 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001285 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001286 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001287 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001288 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001289 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001290 spin_unlock(&nn->client_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001291
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001292 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001293 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001294 /*
1295 * This is a little silly; with sessions there's no real
1296 * use for the callback address. Use the peer address
1297 * as a reasonable default for now, but consider fixing
1298 * the rpc client not to require an address in the
1299 * future:
1300 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001301 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1302 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001303 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001304}
1305
Benny Halevy9089f1b2010-05-12 00:12:26 +03001306/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001307static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001308__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001309{
1310 struct nfsd4_session *elem;
1311 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001312 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001313
1314 dump_sessionid(__func__, sessionid);
1315 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001316 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001317 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001318 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1319 NFS4_MAX_SESSIONID_LEN)) {
1320 return elem;
1321 }
1322 }
1323
1324 dprintk("%s: session not found\n", __func__);
1325 return NULL;
1326}
1327
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001328static struct nfsd4_session *
1329find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1330 __be32 *ret)
1331{
1332 struct nfsd4_session *session;
1333 __be32 status = nfserr_badsession;
1334
1335 session = __find_in_sessionid_hashtbl(sessionid, net);
1336 if (!session)
1337 goto out;
1338 status = nfsd4_get_session_locked(session);
1339 if (status)
1340 session = NULL;
1341out:
1342 *ret = status;
1343 return session;
1344}
1345
Benny Halevy9089f1b2010-05-12 00:12:26 +03001346/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001347static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001348unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001349{
1350 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001351 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001352 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001353 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001354}
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1357static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001358STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001360 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001362 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001363 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return 1;
1365}
1366
1367/*
1368 * XXX Should we use a slab cache ?
1369 * This type of memory management is somewhat inefficient, but we use it
1370 * anyway since SETCLIENTID is not a common operation.
1371 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001372static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
1374 struct nfs4_client *clp;
1375
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001376 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1377 if (clp == NULL)
1378 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001379 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001380 if (clp->cl_name.data == NULL) {
1381 kfree(clp);
1382 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001384 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001385 INIT_LIST_HEAD(&clp->cl_sessions);
1386 idr_init(&clp->cl_stateids);
1387 atomic_set(&clp->cl_refcount, 0);
1388 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1389 INIT_LIST_HEAD(&clp->cl_idhash);
1390 INIT_LIST_HEAD(&clp->cl_openowners);
1391 INIT_LIST_HEAD(&clp->cl_delegations);
1392 INIT_LIST_HEAD(&clp->cl_lru);
1393 INIT_LIST_HEAD(&clp->cl_callbacks);
1394 INIT_LIST_HEAD(&clp->cl_revoked);
1395 spin_lock_init(&clp->cl_lock);
1396 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return clp;
1398}
1399
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001400static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401free_client(struct nfs4_client *clp)
1402{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001403 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001404
1405 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001406 while (!list_empty(&clp->cl_sessions)) {
1407 struct nfsd4_session *ses;
1408 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1409 se_perclnt);
1410 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001411 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1412 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001413 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001414 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001415 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001417 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 kfree(clp);
1419}
1420
Benny Halevy84d38ac2010-05-12 00:13:16 +03001421/* must be called under the client_lock */
1422static inline void
1423unhash_client_locked(struct nfs4_client *clp)
1424{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001425 struct nfsd4_session *ses;
1426
Benny Halevy84d38ac2010-05-12 00:13:16 +03001427 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001428 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001429 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1430 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001431 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001432}
1433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001435destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001437 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001440 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001443 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001444 while (!list_empty(&clp->cl_delegations)) {
1445 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
NeilBrownea1da632005-06-23 22:04:17 -07001446 list_del_init(&dp->dl_perclnt);
Jeff Laytondff13992014-07-08 14:02:49 -04001447 /* Ensure that deleg break won't try to requeue it */
1448 ++dp->dl_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 list_move(&dp->dl_recall_lru, &reaplist);
1450 }
Benny Halevycdc97502014-05-30 09:09:30 -04001451 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 while (!list_empty(&reaplist)) {
1453 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001454 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
Benny Halevy956c4fe2013-10-29 11:39:12 +02001456 list_splice_init(&clp->cl_revoked, &reaplist);
1457 while (!list_empty(&reaplist)) {
1458 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1459 destroy_revoked_delegation(dp);
1460 }
NeilBrownea1da632005-06-23 22:04:17 -07001461 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001462 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1463 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001465 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001466 if (clp->cl_cb_conn.cb_xprt)
1467 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001468 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001469 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001470 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001471 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001472 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001473 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001474 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001475 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1476 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001477 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001480static void expire_client(struct nfs4_client *clp)
1481{
1482 nfsd4_client_record_remove(clp);
1483 destroy_client(clp);
1484}
1485
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001486static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1487{
1488 memcpy(target->cl_verifier.data, source->data,
1489 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001492static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1493{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1495 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1496}
1497
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001498static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001499{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001500 if (source->cr_principal) {
1501 target->cr_principal =
1502 kstrdup(source->cr_principal, GFP_KERNEL);
1503 if (target->cr_principal == NULL)
1504 return -ENOMEM;
1505 } else
1506 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001507 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 target->cr_uid = source->cr_uid;
1509 target->cr_gid = source->cr_gid;
1510 target->cr_group_info = source->cr_group_info;
1511 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001512 target->cr_gss_mech = source->cr_gss_mech;
1513 if (source->cr_gss_mech)
1514 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001515 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516}
1517
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001518static long long
1519compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1520{
1521 long long res;
1522
1523 res = o1->len - o2->len;
1524 if (res)
1525 return res;
1526 return (long long)memcmp(o1->data, o2->data, o1->len);
1527}
1528
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001529static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001530{
NeilBrowna55370a2005-06-23 22:03:52 -07001531 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
1534static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001535same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1536{
1537 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
1539
1540static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001541same_clid(clientid_t *cl1, clientid_t *cl2)
1542{
1543 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
1545
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001546static bool groups_equal(struct group_info *g1, struct group_info *g2)
1547{
1548 int i;
1549
1550 if (g1->ngroups != g2->ngroups)
1551 return false;
1552 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001553 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001554 return false;
1555 return true;
1556}
1557
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001558/*
1559 * RFC 3530 language requires clid_inuse be returned when the
1560 * "principal" associated with a requests differs from that previously
1561 * used. We use uid, gid's, and gss principal string as our best
1562 * approximation. We also don't want to allow non-gss use of a client
1563 * established using gss: in theory cr_principal should catch that
1564 * change, but in practice cr_principal can be null even in the gss case
1565 * since gssd doesn't always pass down a principal string.
1566 */
1567static bool is_gss_cred(struct svc_cred *cr)
1568{
1569 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1570 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1571}
1572
1573
Vivek Trivedi5559b502012-07-24 21:18:20 +05301574static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001575same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1576{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001577 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001578 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1579 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001580 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1581 return false;
1582 if (cr1->cr_principal == cr2->cr_principal)
1583 return true;
1584 if (!cr1->cr_principal || !cr2->cr_principal)
1585 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301586 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588
J. Bruce Fields57266a62013-04-13 14:27:29 -04001589static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1590{
1591 struct svc_cred *cr = &rqstp->rq_cred;
1592 u32 service;
1593
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001594 if (!cr->cr_gss_mech)
1595 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001596 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1597 return service == RPC_GSS_SVC_INTEGRITY ||
1598 service == RPC_GSS_SVC_PRIVACY;
1599}
1600
1601static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1602{
1603 struct svc_cred *cr = &rqstp->rq_cred;
1604
1605 if (!cl->cl_mach_cred)
1606 return true;
1607 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1608 return false;
1609 if (!svc_rqst_integrity_protected(rqstp))
1610 return false;
1611 if (!cr->cr_principal)
1612 return false;
1613 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1614}
1615
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001616static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001617{
1618 static u32 current_clientid = 1;
1619
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001620 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 clp->cl_clientid.cl_id = current_clientid++;
1622}
1623
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001624static void gen_confirm(struct nfs4_client *clp)
1625{
Chuck Leverab4684d2012-03-02 17:13:50 -05001626 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001627 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Jeff Laytonf4199922014-06-17 07:44:11 -04001629 /*
1630 * This is opaque to client, so no need to byte-swap. Use
1631 * __force to keep sparse happy
1632 */
1633 verf[0] = (__force __be32)get_seconds();
1634 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001635 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636}
1637
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001638static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001639{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001640 struct nfs4_stid *ret;
1641
1642 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1643 if (!ret || !ret->sc_type)
1644 return NULL;
1645 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001646}
1647
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001648static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001649{
1650 struct nfs4_stid *s;
1651
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001652 s = find_stateid(cl, t);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001653 if (!s)
1654 return NULL;
1655 if (typemask & s->sc_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001656 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001657 return NULL;
1658}
1659
Jeff Layton2216d442012-11-12 15:00:57 -05001660static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001661 struct svc_rqst *rqstp, nfs4_verifier *verf)
1662{
1663 struct nfs4_client *clp;
1664 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001665 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001666 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001667 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001668
1669 clp = alloc_client(name);
1670 if (clp == NULL)
1671 return NULL;
1672
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001673 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1674 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001675 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001676 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001677 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001678 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001679 }
J. Bruce Fields57725152012-11-05 15:10:26 -05001680 nfsd4_init_callback(&clp->cl_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001681 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001682 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001683 copy_verf(clp, verf);
1684 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001685 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001686 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001687 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001688 return clp;
1689}
1690
NeilBrownfd39ca92005-06-23 22:04:03 -07001691static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001692add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1693{
1694 struct rb_node **new = &(root->rb_node), *parent = NULL;
1695 struct nfs4_client *clp;
1696
1697 while (*new) {
1698 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1699 parent = *new;
1700
1701 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1702 new = &((*new)->rb_left);
1703 else
1704 new = &((*new)->rb_right);
1705 }
1706
1707 rb_link_node(&new_clp->cl_namenode, parent, new);
1708 rb_insert_color(&new_clp->cl_namenode, root);
1709}
1710
1711static struct nfs4_client *
1712find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1713{
1714 long long cmp;
1715 struct rb_node *node = root->rb_node;
1716 struct nfs4_client *clp;
1717
1718 while (node) {
1719 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1720 cmp = compare_blob(&clp->cl_name, name);
1721 if (cmp > 0)
1722 node = node->rb_left;
1723 else if (cmp < 0)
1724 node = node->rb_right;
1725 else
1726 return clp;
1727 }
1728 return NULL;
1729}
1730
1731static void
1732add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
1734 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001735 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001737 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001738 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001740 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001741 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742}
1743
NeilBrownfd39ca92005-06-23 22:04:03 -07001744static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745move_to_confirmed(struct nfs4_client *clp)
1746{
1747 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001748 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001751 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001752 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001753 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001754 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 renew_client(clp);
1756}
1757
1758static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001759find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
1761 struct nfs4_client *clp;
1762 unsigned int idhashval = clientid_hashval(clid->cl_id);
1763
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001764 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001765 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001766 if ((bool)clp->cl_minorversion != sessions)
1767 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001768 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 }
1772 return NULL;
1773}
1774
1775static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001776find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1777{
1778 struct list_head *tbl = nn->conf_id_hashtbl;
1779
1780 return find_client_in_id_table(tbl, clid, sessions);
1781}
1782
1783static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001784find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001786 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001788 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
1790
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001791static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001792{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001793 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001794}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001795
NeilBrown28ce6052005-06-23 22:03:56 -07001796static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001797find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001798{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001799 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001800}
1801
1802static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001803find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001804{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001805 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001806}
1807
NeilBrownfd39ca92005-06-23 22:04:03 -07001808static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001809gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001811 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001812 struct sockaddr *sa = svc_addr(rqstp);
1813 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001814 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Jeff Layton7077ecb2009-08-14 12:57:58 -04001816 /* Currently, we only support tcp and tcp6 for the callback channel */
1817 if (se->se_callback_netid_len == 3 &&
1818 !memcmp(se->se_callback_netid_val, "tcp", 3))
1819 expected_family = AF_INET;
1820 else if (se->se_callback_netid_len == 4 &&
1821 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1822 expected_family = AF_INET6;
1823 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 goto out_err;
1825
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001826 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001827 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001828 (struct sockaddr *)&conn->cb_addr,
1829 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001830
J. Bruce Fields07263f12010-05-31 19:09:40 -04001831 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001833
J. Bruce Fields07263f12010-05-31 19:09:40 -04001834 if (conn->cb_addr.ss_family == AF_INET6)
1835 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001836
J. Bruce Fields07263f12010-05-31 19:09:40 -04001837 conn->cb_prog = se->se_callback_prog;
1838 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001839 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 return;
1841out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001842 conn->cb_addr.ss_family = AF_UNSPEC;
1843 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001844 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 "will not receive delegations\n",
1846 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return;
1849}
1850
Andy Adamson074fe892009-04-03 08:28:15 +03001851/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04001852 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001853 */
Trond Myklebustb6076642014-06-30 11:48:35 -04001854static void
Andy Adamson074fe892009-04-03 08:28:15 +03001855nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1856{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001857 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04001858 struct nfsd4_slot *slot = resp->cstate.slot;
1859 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001860
Andy Adamson557ce262009-08-28 08:45:04 -04001861 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001862
Andy Adamson557ce262009-08-28 08:45:04 -04001863 slot->sl_opcnt = resp->opcnt;
1864 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001865
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001866 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001867 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001868 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001869 return;
1870 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001871 base = resp->cstate.data_offset;
1872 slot->sl_datalen = buf->len - base;
1873 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04001874 WARN("%s: sessions DRC could not cache compound\n", __func__);
1875 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001876}
1877
1878/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001879 * Encode the replay sequence operation from the slot values.
1880 * If cachethis is FALSE encode the uncached rep error on the next
1881 * operation which sets resp->p and increments resp->opcnt for
1882 * nfs4svc_encode_compoundres.
1883 *
Andy Adamson074fe892009-04-03 08:28:15 +03001884 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001885static __be32
1886nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1887 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001888{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001889 struct nfsd4_op *op;
1890 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001891
Andy Adamsonabfabf82009-07-23 19:02:18 -04001892 /* Encode the replayed sequence operation */
1893 op = &args->ops[resp->opcnt - 1];
1894 nfsd4_encode_operation(resp, op);
1895
1896 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001897 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001898 op = &args->ops[resp->opcnt++];
1899 op->status = nfserr_retry_uncached_rep;
1900 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001901 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001902 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001903}
1904
1905/*
Andy Adamson557ce262009-08-28 08:45:04 -04001906 * The sequence operation is not cached because we can use the slot and
1907 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001908 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04001909static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001910nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1911 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001912{
Andy Adamson557ce262009-08-28 08:45:04 -04001913 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001914 struct xdr_stream *xdr = &resp->xdr;
1915 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03001916 __be32 status;
1917
Andy Adamson557ce262009-08-28 08:45:04 -04001918 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001919
Andy Adamsonabfabf82009-07-23 19:02:18 -04001920 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04001921 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04001922 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001923
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001924 p = xdr_reserve_space(xdr, slot->sl_datalen);
1925 if (!p) {
1926 WARN_ON_ONCE(1);
1927 return nfserr_serverfault;
1928 }
1929 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1930 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03001931
Andy Adamson557ce262009-08-28 08:45:04 -04001932 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001933 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001934}
1935
Andy Adamson0733d212009-04-03 08:28:01 +03001936/*
1937 * Set the exchange_id flags returned by the server.
1938 */
1939static void
1940nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1941{
1942 /* pNFS is not supported */
1943 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1944
1945 /* Referrals are supported, Migration is not. */
1946 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1947
1948 /* set the wire flags to return to client. */
1949 clid->flags = new->cl_exchange_flags;
1950}
1951
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001952static bool client_has_state(struct nfs4_client *clp)
1953{
1954 /*
1955 * Note clp->cl_openowners check isn't quite right: there's no
1956 * need to count owners without stateid's.
1957 *
1958 * Also note we should probably be using this in 4.0 case too.
1959 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04001960 return !list_empty(&clp->cl_openowners)
1961 || !list_empty(&clp->cl_delegations)
1962 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001963}
1964
Al Virob37ad282006-10-19 23:28:59 -07001965__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001966nfsd4_exchange_id(struct svc_rqst *rqstp,
1967 struct nfsd4_compound_state *cstate,
1968 struct nfsd4_exchange_id *exid)
1969{
Andy Adamson0733d212009-04-03 08:28:01 +03001970 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001971 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04001972 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001973 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001974 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001975 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001976 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03001977
Jeff Layton363168b2009-08-14 12:57:56 -04001978 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001979 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001980 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001981 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001982 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03001983
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001984 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03001985 return nfserr_inval;
1986
Andy Adamson0733d212009-04-03 08:28:01 +03001987 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04001988 case SP4_MACH_CRED:
1989 if (!svc_rqst_integrity_protected(rqstp))
1990 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03001991 case SP4_NONE:
1992 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001993 default: /* checked by xdr code */
1994 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03001995 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04001996 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03001997 }
1998
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001999 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002000 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002001 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002002 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002003 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2004 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2005
J. Bruce Fields136e6582012-05-12 20:37:23 -04002006 if (update) {
2007 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002008 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002009 goto out;
2010 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002011 if (!mach_creds_match(conf, rqstp)) {
2012 status = nfserr_wrong_cred;
2013 goto out;
2014 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002015 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002016 status = nfserr_perm;
2017 goto out;
2018 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002019 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002020 status = nfserr_not_same;
2021 goto out;
2022 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002023 /* case 6 */
2024 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2025 new = conf;
2026 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002027 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002028 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002029 if (client_has_state(conf)) {
2030 status = nfserr_clid_inuse;
2031 goto out;
2032 }
Andy Adamson0733d212009-04-03 08:28:01 +03002033 expire_client(conf);
2034 goto out_new;
2035 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002036 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002037 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002038 new = conf;
2039 goto out_copy;
2040 }
2041 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04002042 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002043 }
2044
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002045 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002046 status = nfserr_noent;
2047 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002048 }
2049
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002050 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002051 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03002052 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002053
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002054 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002055out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05002056 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03002057 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04002058 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03002059 goto out;
2060 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002061 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002062 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002063
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002064 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002065 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03002066out_copy:
2067 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2068 exid->clientid.cl_id = new->cl_clientid.cl_id;
2069
J. Bruce Fields778df3f2012-05-25 21:40:23 -04002070 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03002071 nfsd4_set_ex_flags(new, exid);
2072
2073 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04002074 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002075 status = nfs_ok;
2076
2077out:
2078 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03002079 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002080}
2081
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002082static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002083check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002084{
Andy Adamson88e588d2009-07-23 19:02:15 -04002085 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2086 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002087
2088 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002089 if (slot_inuse) {
2090 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002091 return nfserr_jukebox;
2092 else
2093 return nfserr_seq_misordered;
2094 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002095 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002096 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002097 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002098 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002099 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002100 return nfserr_seq_misordered;
2101}
2102
Andy Adamson49557cc2009-07-23 19:02:16 -04002103/*
2104 * Cache the create session result into the create session single DRC
2105 * slot cache by saving the xdr structure. sl_seqid has been set.
2106 * Do this for solo or embedded create session operations.
2107 */
2108static void
2109nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002110 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002111{
2112 slot->sl_status = nfserr;
2113 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2114}
2115
2116static __be32
2117nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2118 struct nfsd4_clid_slot *slot)
2119{
2120 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2121 return slot->sl_status;
2122}
2123
Mi Jinlong1b74c252011-07-14 14:50:17 +08002124#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2125 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2126 1 + /* MIN tag is length with zero, only length */ \
2127 3 + /* version, opcount, opcode */ \
2128 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2129 /* seqid, slotID, slotID, cache */ \
2130 4 ) * sizeof(__be32))
2131
2132#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2133 2 + /* verifier: AUTH_NULL, length 0 */\
2134 1 + /* status */ \
2135 1 + /* MIN tag is length with zero, only length */ \
2136 3 + /* opcount, opcode, opstatus*/ \
2137 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2138 /* seqid, slotID, slotID, slotID, status */ \
2139 5 ) * sizeof(__be32))
2140
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002141static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002142{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002143 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2144
J. Bruce Fields373cd402013-04-08 15:42:12 -04002145 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2146 return nfserr_toosmall;
2147 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2148 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002149 ca->headerpadsz = 0;
2150 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2151 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2152 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2153 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2154 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2155 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2156 /*
2157 * Note decreasing slot size below client's request may make it
2158 * difficult for client to function correctly, whereas
2159 * decreasing the number of slots will (just?) affect
2160 * performance. When short on memory we therefore prefer to
2161 * decrease number of slots instead of their size. Clients that
2162 * request larger slots than they need will get poor results:
2163 */
2164 ca->maxreqs = nfsd4_get_drc_mem(ca);
2165 if (!ca->maxreqs)
2166 return nfserr_jukebox;
2167
J. Bruce Fields373cd402013-04-08 15:42:12 -04002168 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002169}
2170
Kinglong Mee8a891632013-12-23 18:11:02 +08002171#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2172 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2173#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2174 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2175
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002176static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2177{
2178 ca->headerpadsz = 0;
2179
2180 /*
2181 * These RPC_MAX_HEADER macros are overkill, especially since we
2182 * don't even do gss on the backchannel yet. But this is still
2183 * less than 1k. Tighten up this estimate in the unlikely event
2184 * it turns out to be a problem for some client:
2185 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002186 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002187 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002188 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002189 return nfserr_toosmall;
2190 ca->maxresp_cached = 0;
2191 if (ca->maxops < 2)
2192 return nfserr_toosmall;
2193
2194 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002195}
2196
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002197static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2198{
2199 switch (cbs->flavor) {
2200 case RPC_AUTH_NULL:
2201 case RPC_AUTH_UNIX:
2202 return nfs_ok;
2203 default:
2204 /*
2205 * GSS case: the spec doesn't allow us to return this
2206 * error. But it also doesn't allow us not to support
2207 * GSS.
2208 * I'd rather this fail hard than return some error the
2209 * client might think it can already handle:
2210 */
2211 return nfserr_encr_alg_unsupp;
2212 }
2213}
2214
Andy Adamson069b6ad2009-04-03 08:27:58 +03002215__be32
2216nfsd4_create_session(struct svc_rqst *rqstp,
2217 struct nfsd4_compound_state *cstate,
2218 struct nfsd4_create_session *cr_ses)
2219{
Jeff Layton363168b2009-08-14 12:57:56 -04002220 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002221 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002222 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002223 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002224 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002225 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002226 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002227
Mi Jinlonga62573d2011-03-23 17:57:07 +08002228 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2229 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002230 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2231 if (status)
2232 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002233 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002234 if (status)
2235 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002236 status = check_backchannel_attrs(&cr_ses->back_channel);
2237 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002238 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002239 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002240 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002241 if (!new)
2242 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002243 conn = alloc_conn_from_crses(rqstp, cr_ses);
2244 if (!conn)
2245 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002246
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002247 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002248 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002249 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002250 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002251
2252 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002253 status = nfserr_wrong_cred;
2254 if (!mach_creds_match(conf, rqstp))
2255 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002256 cs_slot = &conf->cl_cs_slot;
2257 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002258 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002259 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002260 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002261 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002262 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002263 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002264 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002265 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002266 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002267 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002268 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002269 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002270 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002271 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002272 status = nfserr_wrong_cred;
2273 if (!mach_creds_match(unconf, rqstp))
2274 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002275 cs_slot = &unconf->cl_cs_slot;
2276 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002277 if (status) {
2278 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002279 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002280 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002281 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002282 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002283 if (old) {
2284 status = mark_client_expired(old);
2285 if (status)
2286 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002287 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002288 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002289 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002290 conf = unconf;
2291 } else {
2292 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002293 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002294 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002295 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002296 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002297 * We do not support RDMA or persistent sessions
2298 */
2299 cr_ses->flags &= ~SESSION4_PERSIST;
2300 cr_ses->flags &= ~SESSION4_RDMA;
2301
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002302 init_session(rqstp, new, conf, cr_ses);
2303 nfsd4_init_conn(rqstp, conn, new);
2304
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002305 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002306 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002307 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002308 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002309
Andy Adamson49557cc2009-07-23 19:02:16 -04002310 /* cache solo and embedded create sessions under the state lock */
2311 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002312 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002313 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002314out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08002315 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002316 free_conn(conn);
2317out_free_session:
2318 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002319out_release_drc_mem:
2320 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002321 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002322}
2323
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002324static __be32 nfsd4_map_bcts_dir(u32 *dir)
2325{
2326 switch (*dir) {
2327 case NFS4_CDFC4_FORE:
2328 case NFS4_CDFC4_BACK:
2329 return nfs_ok;
2330 case NFS4_CDFC4_FORE_OR_BOTH:
2331 case NFS4_CDFC4_BACK_OR_BOTH:
2332 *dir = NFS4_CDFC4_BOTH;
2333 return nfs_ok;
2334 };
2335 return nfserr_inval;
2336}
2337
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002338__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2339{
2340 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002341 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002342 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002343
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002344 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2345 if (status)
2346 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002347 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002348 session->se_cb_prog = bc->bc_cb_program;
2349 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002350 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002351
2352 nfsd4_probe_callback(session->se_client);
2353
2354 return nfs_ok;
2355}
2356
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002357__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2358 struct nfsd4_compound_state *cstate,
2359 struct nfsd4_bind_conn_to_session *bcts)
2360{
2361 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002362 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002363 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002364 struct net *net = SVC_NET(rqstp);
2365 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002366
2367 if (!nfsd4_last_compound_op(rqstp))
2368 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002369 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002370 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002371 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002372 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002373 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002374 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002375 status = nfserr_wrong_cred;
2376 if (!mach_creds_match(session->se_client, rqstp))
2377 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002378 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002379 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002380 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002381 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002382 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002383 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002384 goto out;
2385 nfsd4_init_conn(rqstp, conn, session);
2386 status = nfs_ok;
2387out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002388 nfsd4_put_session(session);
2389out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002390 nfs4_unlock_state();
2391 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002392}
2393
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002394static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2395{
2396 if (!session)
2397 return 0;
2398 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2399}
2400
Andy Adamson069b6ad2009-04-03 08:27:58 +03002401__be32
2402nfsd4_destroy_session(struct svc_rqst *r,
2403 struct nfsd4_compound_state *cstate,
2404 struct nfsd4_destroy_session *sessionid)
2405{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002406 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002407 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002408 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002409 struct net *net = SVC_NET(r);
2410 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002411
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002412 nfs4_lock_state();
2413 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002414 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002415 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002416 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002417 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002418 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002419 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002420 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002421 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002422 if (!ses)
2423 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002424 status = nfserr_wrong_cred;
2425 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002426 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002427 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002428 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002429 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002430 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002431 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002432
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002433 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002434
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002435 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002436 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002437out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002438 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002439out_client_lock:
2440 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002441out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002442 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002443 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002444}
2445
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002446static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002447{
2448 struct nfsd4_conn *c;
2449
2450 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002451 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002452 return c;
2453 }
2454 }
2455 return NULL;
2456}
2457
J. Bruce Fields57266a62013-04-13 14:27:29 -04002458static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002459{
2460 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002461 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002462 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002463 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002464
2465 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002466 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002467 if (c)
2468 goto out_free;
2469 status = nfserr_conn_not_bound_to_session;
2470 if (clp->cl_mach_cred)
2471 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002472 __nfsd4_hash_conn(new, ses);
2473 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002474 ret = nfsd4_register_conn(new);
2475 if (ret)
2476 /* oops; xprt is already down: */
2477 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002478 return nfs_ok;
2479out_free:
2480 spin_unlock(&clp->cl_lock);
2481 free_conn(new);
2482 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002483}
2484
Mi Jinlong868b89c2011-04-27 09:09:58 +08002485static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2486{
2487 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2488
2489 return args->opcnt > session->se_fchannel.maxops;
2490}
2491
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002492static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2493 struct nfsd4_session *session)
2494{
2495 struct xdr_buf *xb = &rqstp->rq_arg;
2496
2497 return xb->len > session->se_fchannel.maxreq_sz;
2498}
2499
Andy Adamson069b6ad2009-04-03 08:27:58 +03002500__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002501nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002502 struct nfsd4_compound_state *cstate,
2503 struct nfsd4_sequence *seq)
2504{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002505 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002506 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002507 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002508 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002509 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002510 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002511 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002512 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002513 struct net *net = SVC_NET(rqstp);
2514 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002515
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002516 if (resp->opcnt != 1)
2517 return nfserr_sequence_pos;
2518
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002519 /*
2520 * Will be either used or freed by nfsd4_sequence_check_conn
2521 * below.
2522 */
2523 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2524 if (!conn)
2525 return nfserr_jukebox;
2526
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002527 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002528 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002529 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002530 goto out_no_session;
2531 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002532
Mi Jinlong868b89c2011-04-27 09:09:58 +08002533 status = nfserr_too_many_ops;
2534 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002535 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002536
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002537 status = nfserr_req_too_big;
2538 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002539 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002540
Benny Halevyb85d4c02009-04-03 08:28:08 +03002541 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002542 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002543 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002544
Andy Adamson557ce262009-08-28 08:45:04 -04002545 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002546 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2547
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002548 /* We do not negotiate the number of slots yet, so set the
2549 * maxslots to the session maxreqs which is used to encode
2550 * sr_highest_slotid and the sr_target_slot id to maxslots */
2551 seq->maxslots = session->se_fchannel.maxreqs;
2552
J. Bruce Fields73e79482012-02-13 16:39:00 -05002553 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2554 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002555 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002556 status = nfserr_seq_misordered;
2557 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002558 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002559 cstate->slot = slot;
2560 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002561 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002562 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002563 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002564 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002565 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002566 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002567 }
2568 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002569 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002570
J. Bruce Fields57266a62013-04-13 14:27:29 -04002571 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002572 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002573 if (status)
2574 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002575
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002576 buflen = (seq->cachethis) ?
2577 session->se_fchannel.maxresp_cached :
2578 session->se_fchannel.maxresp_sz;
2579 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2580 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002581 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002582 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002583 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002584
2585 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002586 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002587 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002588 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002589 if (seq->cachethis)
2590 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002591 else
2592 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002593
2594 cstate->slot = slot;
2595 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002596 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002597
Benny Halevyb85d4c02009-04-03 08:28:08 +03002598out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002599 switch (clp->cl_cb_state) {
2600 case NFSD4_CB_DOWN:
2601 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2602 break;
2603 case NFSD4_CB_FAULT:
2604 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2605 break;
2606 default:
2607 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002608 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002609 if (!list_empty(&clp->cl_revoked))
2610 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002611out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002612 if (conn)
2613 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002614 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002615 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002616out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002617 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002618 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002619}
2620
Trond Myklebustb6076642014-06-30 11:48:35 -04002621void
2622nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2623{
2624 struct nfsd4_compound_state *cs = &resp->cstate;
2625
2626 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002627 if (cs->status != nfserr_replay_cache) {
2628 nfsd4_store_cache_entry(resp);
2629 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2630 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002631 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002632 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002633 } else if (cs->clp)
2634 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002635}
2636
Mi Jinlong345c2842011-10-20 17:51:39 +08002637__be32
2638nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2639{
2640 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002641 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002642 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002643
2644 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002645 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002646 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002647 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002648
2649 if (conf) {
2650 clp = conf;
2651
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002652 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002653 status = nfserr_clientid_busy;
2654 goto out;
2655 }
2656 } else if (unconf)
2657 clp = unconf;
2658 else {
2659 status = nfserr_stale_clientid;
2660 goto out;
2661 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002662 if (!mach_creds_match(clp, rqstp)) {
2663 status = nfserr_wrong_cred;
2664 goto out;
2665 }
Mi Jinlong345c2842011-10-20 17:51:39 +08002666 expire_client(clp);
2667out:
2668 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002669 return status;
2670}
2671
Andy Adamson069b6ad2009-04-03 08:27:58 +03002672__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002673nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2674{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002675 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002676
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002677 if (rc->rca_one_fs) {
2678 if (!cstate->current_fh.fh_dentry)
2679 return nfserr_nofilehandle;
2680 /*
2681 * We don't take advantage of the rca_one_fs case.
2682 * That's OK, it's optional, we can safely ignore it.
2683 */
2684 return nfs_ok;
2685 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002686
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002687 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002688 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002689 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2690 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002691 goto out;
2692
2693 status = nfserr_stale_clientid;
2694 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002695 /*
2696 * The following error isn't really legal.
2697 * But we only get here if the client just explicitly
2698 * destroyed the client. Surely it no longer cares what
2699 * error it gets back on an operation for the dead
2700 * client.
2701 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002702 goto out;
2703
2704 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002705 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002706out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002707 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002708 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002709}
2710
2711__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002712nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2713 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002715 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002717 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002718 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002719 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2720
J. Bruce Fields63db4632012-05-18 21:54:19 -04002721 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002723 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002724 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002725 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002727 if (clp_used_exchangeid(conf))
2728 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002729 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002730 char addr_str[INET6_ADDRSTRLEN];
2731 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2732 sizeof(addr_str));
2733 dprintk("NFSD: setclientid: string in use by client "
2734 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 goto out;
2736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002738 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002739 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002742 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002743 if (new == NULL)
2744 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002745 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002746 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002748 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002749 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002750 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002751 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002752 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2754 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2755 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2756 status = nfs_ok;
2757out:
2758 nfs4_unlock_state();
2759 return status;
2760}
2761
2762
Al Virob37ad282006-10-19 23:28:59 -07002763__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002764nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2765 struct nfsd4_compound_state *cstate,
2766 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767{
NeilBrown21ab45a2005-06-23 22:04:14 -07002768 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2770 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002771 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002772 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002774 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002777
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002778 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002779 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002780 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002781 * We try hard to give out unique clientid's, so if we get an
2782 * attempt to confirm the same clientid with a different cred,
2783 * there's a bug somewhere. Let's charitably assume it's our
2784 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002785 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002786 status = nfserr_serverfault;
2787 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2788 goto out;
2789 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2790 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002791 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002792 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2793 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002795 else /* case 4: client hasn't noticed we rebooted yet? */
2796 status = nfserr_stale_clientid;
2797 goto out;
2798 }
2799 status = nfs_ok;
2800 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002801 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2802 nfsd4_probe_callback(conf);
2803 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002804 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002805 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002806 if (conf) {
2807 status = mark_client_expired(conf);
2808 if (status)
2809 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002810 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002811 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002812 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002813 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 nfs4_unlock_state();
2817 return status;
2818}
2819
J. Bruce Fields32513b42011-10-13 16:00:16 -04002820static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002822 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2823}
2824
2825/* OPEN Share state helper functions */
2826static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
2827{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 unsigned int hashval = file_hashval(ino);
2829
Trond Myklebust950e0112014-06-30 11:48:31 -04002830 lockdep_assert_held(&state_lock);
2831
J. Bruce Fields32513b42011-10-13 16:00:16 -04002832 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04002833 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002834 INIT_LIST_HEAD(&fp->fi_stateids);
2835 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebust950e0112014-06-30 11:48:31 -04002836 ihold(ino);
2837 fp->fi_inode = ino;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002838 fp->fi_had_conflict = false;
2839 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04002840 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002841 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2842 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04002843 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844}
2845
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002846void
NeilBrowne60d4392005-06-23 22:03:01 -07002847nfsd4_free_slabs(void)
2848{
Christoph Hellwigabf11352014-05-21 07:43:03 -07002849 kmem_cache_destroy(openowner_slab);
2850 kmem_cache_destroy(lockowner_slab);
2851 kmem_cache_destroy(file_slab);
2852 kmem_cache_destroy(stateid_slab);
2853 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002854}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
Bryan Schumaker72083392011-11-01 15:24:59 -04002856int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857nfsd4_init_slabs(void)
2858{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002859 openowner_slab = kmem_cache_create("nfsd4_openowners",
2860 sizeof(struct nfs4_openowner), 0, 0, NULL);
2861 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002862 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002863 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002864 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002865 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002866 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07002867 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002868 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002869 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002870 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07002871 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002872 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002873 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002874 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002875 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002876 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002877 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002878 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07002880
2881out_free_stateid_slab:
2882 kmem_cache_destroy(stateid_slab);
2883out_free_file_slab:
2884 kmem_cache_destroy(file_slab);
2885out_free_lockowner_slab:
2886 kmem_cache_destroy(lockowner_slab);
2887out_free_openowner_slab:
2888 kmem_cache_destroy(openowner_slab);
2889out:
NeilBrowne60d4392005-06-23 22:03:01 -07002890 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2891 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892}
2893
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002894static void init_nfs4_replay(struct nfs4_replay *rp)
2895{
2896 rp->rp_status = nfserr_serverfault;
2897 rp->rp_buflen = 0;
2898 rp->rp_buf = rp->rp_ibuf;
2899}
2900
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002901static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
2903 struct nfs4_stateowner *sop;
2904
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002905 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002906 if (!sop)
2907 return NULL;
2908
2909 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2910 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002911 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002912 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002914 sop->so_owner.len = owner->len;
2915
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002916 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002917 sop->so_client = clp;
2918 init_nfs4_replay(&sop->so_replay);
2919 return sop;
2920}
2921
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002922static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002923{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002924 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2925
2926 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002927 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928}
2929
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002930static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04002931alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002932 struct nfsd4_compound_state *cstate)
2933{
Trond Myklebust13d6f662014-06-30 11:48:45 -04002934 struct nfs4_client *clp = cstate->clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002935 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002937 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2938 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002940 oo->oo_owner.so_is_open_owner = 1;
2941 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04002942 oo->oo_flags = NFS4_OO_NEW;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002943 if (nfsd4_has_session(cstate))
2944 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002945 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002946 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002947 INIT_LIST_HEAD(&oo->oo_close_lru);
2948 hash_openowner(oo, clp, strhashval);
2949 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950}
2951
J. Bruce Fields996e0932011-10-17 11:14:48 -04002952static 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 -04002953 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002955 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04002956 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002957 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002958 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002959 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 stp->st_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 stp->st_access_bmap = 0;
2962 stp->st_deny_bmap = 0;
Jeff Layton82c5ff12012-05-11 09:45:13 -04002963 set_access(open->op_share_access, stp);
Jeff Laytonce0fc432012-05-11 09:45:14 -04002964 set_deny(open->op_share_deny, stp);
NeilBrown4c4cd222005-07-07 17:59:27 -07002965 stp->st_openstp = NULL;
Trond Myklebust1d31a252014-07-10 14:07:25 -04002966 spin_lock(&fp->fi_lock);
2967 list_add(&stp->st_perfile, &fp->fi_stateids);
2968 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969}
2970
2971static void
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002972move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973{
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002974 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2975
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002976 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002978 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002979 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980}
2981
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002983same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2984 clientid_t *clid)
2985{
2986 return (sop->so_owner.len == owner->len) &&
2987 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2988 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989}
2990
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002991static struct nfs4_openowner *
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002992find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
2993 bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002995 struct nfs4_stateowner *so;
2996 struct nfs4_openowner *oo;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002997 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002999 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003000 if (!so->so_is_open_owner)
3001 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003002 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
3003 oo = openowner(so);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003004 clp = oo->oo_owner.so_client;
3005 if ((bool)clp->cl_minorversion != sessions)
3006 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04003007 renew_client(oo->oo_owner.so_client);
3008 return oo;
3009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 }
3011 return NULL;
3012}
3013
3014/* search file_hashtbl[] for file */
3015static struct nfs4_file *
Trond Myklebust950e0112014-06-30 11:48:31 -04003016find_file_locked(struct inode *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017{
3018 unsigned int hashval = file_hashval(ino);
3019 struct nfs4_file *fp;
3020
Trond Myklebust950e0112014-06-30 11:48:31 -04003021 lockdep_assert_held(&state_lock);
3022
Jeff Layton89876f82013-04-02 09:01:59 -04003023 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07003024 if (fp->fi_inode == ino) {
3025 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 }
3029 return NULL;
3030}
3031
Trond Myklebust950e0112014-06-30 11:48:31 -04003032static struct nfs4_file *
3033find_file(struct inode *ino)
3034{
3035 struct nfs4_file *fp;
3036
3037 spin_lock(&state_lock);
3038 fp = find_file_locked(ino);
3039 spin_unlock(&state_lock);
3040 return fp;
3041}
3042
3043static struct nfs4_file *
3044find_or_add_file(struct inode *ino, struct nfs4_file *new)
3045{
3046 struct nfs4_file *fp;
3047
3048 spin_lock(&state_lock);
3049 fp = find_file_locked(ino);
3050 if (fp == NULL) {
3051 nfsd4_init_file(new, ino);
3052 fp = new;
3053 }
3054 spin_unlock(&state_lock);
3055
3056 return fp;
3057}
3058
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003059/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 * Called to check deny when READ with all zero stateid or
3061 * WRITE with all zero or all one stateid
3062 */
Al Virob37ad282006-10-19 23:28:59 -07003063static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3065{
3066 struct inode *ino = current_fh->fh_dentry->d_inode;
3067 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003068 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07003071 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003072 return ret;
3073 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003074 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003075 if (fp->fi_share_deny & deny_type)
3076 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003077 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003078 put_nfs4_file(fp);
3079 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080}
3081
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003082static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083{
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003084 struct nfs4_client *clp = dp->dl_stid.sc_client;
3085 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3086
Benny Halevycdc97502014-05-30 09:09:30 -04003087 lockdep_assert_held(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 /* We're assuming the state code never drops its reference
3089 * without first removing the lease. Since we're in this lease
3090 * callback (and since the lease code is serialized by the kernel
3091 * lock) we know the server hasn't removed the lease yet, we know
3092 * it's safe to take a reference: */
3093 atomic_inc(&dp->dl_count);
3094
Jeff Laytondff13992014-07-08 14:02:49 -04003095 /*
3096 * If the dl_time != 0, then we know that it has already been
3097 * queued for a lease break. Don't queue it again.
3098 */
3099 if (dp->dl_time == 0) {
3100 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3101 dp->dl_time = get_seconds();
3102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
NeilBrown6282cd52014-06-04 17:39:26 +10003104 block_delegations(&dp->dl_fh);
3105
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003106 nfsd4_cb_recall(dp);
3107}
3108
Jeff Layton1c8c6012013-06-21 08:58:15 -04003109/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003110static void nfsd_break_deleg_cb(struct file_lock *fl)
3111{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003112 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3113 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003114
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003115 if (!fp) {
3116 WARN(1, "(%p)->fl_owner NULL\n", fl);
3117 return;
3118 }
3119 if (fp->fi_had_conflict) {
3120 WARN(1, "duplicate break on %p\n", fp);
3121 return;
3122 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003123 /*
3124 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003125 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003126 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003127 */
3128 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
Benny Halevycdc97502014-05-30 09:09:30 -04003130 spin_lock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003131 fp->fi_had_conflict = true;
3132 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3133 nfsd_break_one_deleg(dp);
Benny Halevycdc97502014-05-30 09:09:30 -04003134 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135}
3136
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137static
3138int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3139{
3140 if (arg & F_UNLCK)
3141 return lease_modify(onlist, arg);
3142 else
3143 return -EAGAIN;
3144}
3145
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003146static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003147 .lm_break = nfsd_break_deleg_cb,
3148 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149};
3150
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003151static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3152{
3153 if (nfsd4_has_session(cstate))
3154 return nfs_ok;
3155 if (seqid == so->so_seqid - 1)
3156 return nfserr_replay_me;
3157 if (seqid == so->so_seqid)
3158 return nfs_ok;
3159 return nfserr_bad_seqid;
3160}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Jeff Layton4b24ca72014-06-30 11:48:44 -04003162static __be32 lookup_clientid(clientid_t *clid,
3163 struct nfsd4_compound_state *cstate,
3164 struct nfsd_net *nn)
3165{
3166 struct nfs4_client *found;
3167
3168 if (cstate->clp) {
3169 found = cstate->clp;
3170 if (!same_clid(&found->cl_clientid, clid))
3171 return nfserr_stale_clientid;
3172 return nfs_ok;
3173 }
3174
3175 if (STALE_CLIENTID(clid, nn))
3176 return nfserr_stale_clientid;
3177
3178 /*
3179 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3180 * cached already then we know this is for is for v4.0 and "sessions"
3181 * will be false.
3182 */
3183 WARN_ON_ONCE(cstate->session);
3184 found = find_confirmed_client(clid, false, nn);
3185 if (!found)
3186 return nfserr_expired;
3187
3188 /* Cache the nfs4_client in cstate! */
3189 cstate->clp = found;
3190 atomic_inc(&found->cl_refcount);
3191 return nfs_ok;
3192}
3193
Al Virob37ad282006-10-19 23:28:59 -07003194__be32
Andy Adamson66689582009-04-03 08:28:45 +03003195nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003196 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 clientid_t *clientid = &open->op_clientid;
3199 struct nfs4_client *clp = NULL;
3200 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003201 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003202 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003204 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003206 /*
3207 * In case we need it later, after we've already created the
3208 * file and don't want to risk a further failure:
3209 */
3210 open->op_file = nfsd4_alloc_file();
3211 if (open->op_file == NULL)
3212 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
Trond Myklebust2d91e892014-06-30 11:48:46 -04003214 status = lookup_clientid(clientid, cstate, nn);
3215 if (status)
3216 return status;
3217 clp = cstate->clp;
3218
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003219 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003220 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003221 open->op_openowner = oo;
3222 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003223 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003225 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003226 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003227 release_openowner(oo);
3228 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003229 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003230 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003231 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3232 if (status)
3233 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003234 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003235new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003236 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003237 if (oo == NULL)
3238 return nfserr_jukebox;
3239 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003240alloc_stateid:
3241 open->op_stp = nfs4_alloc_stateid(clp);
3242 if (!open->op_stp)
3243 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003244 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245}
3246
Al Virob37ad282006-10-19 23:28:59 -07003247static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003248nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3249{
3250 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3251 return nfserr_openmode;
3252 else
3253 return nfs_ok;
3254}
3255
Daniel Mackc47d8322011-05-16 16:38:14 +02003256static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003257{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003258 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3259}
3260
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003261static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003262{
3263 struct nfs4_stid *ret;
3264
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003265 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003266 if (!ret)
3267 return NULL;
3268 return delegstateid(ret);
3269}
3270
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003271static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3272{
3273 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3274 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3275}
3276
Al Virob37ad282006-10-19 23:28:59 -07003277static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003278nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003279 struct nfs4_delegation **dp)
3280{
3281 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003282 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07003283
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003284 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07003285 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003286 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003287 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07003288 status = nfs4_check_delegmode(*dp, flags);
3289 if (status)
3290 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003291out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003292 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003293 return nfs_ok;
3294 if (status)
3295 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003296 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003297 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003298}
3299
Al Virob37ad282006-10-19 23:28:59 -07003300static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003301nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003303 struct nfs4_ol_stateid *local;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003304 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
Trond Myklebust1d31a252014-07-10 14:07:25 -04003306 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003307 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 /* ignore lock owners */
3309 if (local->st_stateowner->so_is_open_owner == 0)
3310 continue;
3311 /* remember if we have seen this open owner */
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003312 if (local->st_stateowner == &oo->oo_owner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 *stpp = local;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003314 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003317 spin_unlock(&fp->fi_lock);
J. Bruce Fields77eaae82011-09-02 12:08:20 -04003318 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319}
3320
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003321static inline int nfs4_access_to_access(u32 nfs4_access)
3322{
3323 int flags = 0;
3324
3325 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3326 flags |= NFSD_MAY_READ;
3327 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3328 flags |= NFSD_MAY_WRITE;
3329 return flags;
3330}
3331
Al Virob37ad282006-10-19 23:28:59 -07003332static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3334 struct nfsd4_open *open)
3335{
3336 struct iattr iattr = {
3337 .ia_valid = ATTR_SIZE,
3338 .ia_size = 0,
3339 };
3340 if (!open->op_truncate)
3341 return 0;
3342 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003343 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3345}
3346
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003347static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003348 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3349 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003350{
Trond Myklebustde186432014-07-10 14:07:26 -04003351 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003352 __be32 status;
3353 int oflag = nfs4_access_to_omode(open->op_share_access);
3354 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003355 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003356
Trond Myklebustde186432014-07-10 14:07:26 -04003357 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003358
3359 /*
3360 * Are we trying to set a deny mode that would conflict with
3361 * current access?
3362 */
3363 status = nfs4_file_check_deny(fp, open->op_share_deny);
3364 if (status != nfs_ok) {
3365 spin_unlock(&fp->fi_lock);
3366 goto out;
3367 }
3368
3369 /* set access to the file */
3370 status = nfs4_file_get_access(fp, open->op_share_access);
3371 if (status != nfs_ok) {
3372 spin_unlock(&fp->fi_lock);
3373 goto out;
3374 }
3375
3376 /* Set access bits in stateid */
3377 old_access_bmap = stp->st_access_bmap;
3378 set_access(open->op_share_access, stp);
3379
3380 /* Set new deny mask */
3381 old_deny_bmap = stp->st_deny_bmap;
3382 set_deny(open->op_share_deny, stp);
3383 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3384
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003385 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003386 spin_unlock(&fp->fi_lock);
3387 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003388 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003389 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003390 spin_lock(&fp->fi_lock);
3391 if (!fp->fi_fds[oflag]) {
3392 fp->fi_fds[oflag] = filp;
3393 filp = NULL;
3394 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003395 }
Trond Myklebustde186432014-07-10 14:07:26 -04003396 spin_unlock(&fp->fi_lock);
3397 if (filp)
3398 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003399
3400 status = nfsd4_truncate(rqstp, cur_fh, open);
3401 if (status)
3402 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003403out:
3404 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003405out_put_access:
3406 stp->st_access_bmap = old_access_bmap;
3407 nfs4_file_put_access(fp, open->op_share_access);
3408 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3409 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003410}
3411
Al Virob37ad282006-10-19 23:28:59 -07003412static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003413nfs4_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 -07003414{
Al Virob37ad282006-10-19 23:28:59 -07003415 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003416 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003418 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003419 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003420
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003421 /* test and set deny mode */
3422 spin_lock(&fp->fi_lock);
3423 status = nfs4_file_check_deny(fp, open->op_share_deny);
3424 if (status == nfs_ok) {
3425 old_deny_bmap = stp->st_deny_bmap;
3426 set_deny(open->op_share_deny, stp);
3427 fp->fi_share_deny |=
3428 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3429 }
3430 spin_unlock(&fp->fi_lock);
3431
3432 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003435 status = nfsd4_truncate(rqstp, cur_fh, open);
3436 if (status != nfs_ok)
3437 reset_union_bmap_deny(old_deny_bmap, stp);
3438 return status;
3439}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003442nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003444 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445}
3446
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003447/* Should we give out recallable state?: */
3448static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3449{
3450 if (clp->cl_cb_state == NFSD4_CB_UP)
3451 return true;
3452 /*
3453 * In the sessions case, since we don't have to establish a
3454 * separate connection for callbacks, we assume it's OK
3455 * until we hear otherwise:
3456 */
3457 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3458}
3459
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003460static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
3461{
3462 struct file_lock *fl;
3463
3464 fl = locks_alloc_lock();
3465 if (!fl)
3466 return NULL;
3467 locks_init_lock(fl);
3468 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003469 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003470 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3471 fl->fl_end = OFFSET_MAX;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003472 fl->fl_owner = (fl_owner_t)(dp->dl_file);
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003473 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003474 return fl;
3475}
3476
J. Bruce Fields99c41512013-05-21 16:21:25 -04003477static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003478{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003479 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003480 struct file_lock *fl;
3481 int status;
3482
J. Bruce Fields99c41512013-05-21 16:21:25 -04003483 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003484 if (!fl)
3485 return -ENOMEM;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003486 fl->fl_file = find_readable_file(fp);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003487 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003488 if (status)
3489 goto out_free;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003490 fp->fi_lease = fl;
Trond Myklebustde186432014-07-10 14:07:26 -04003491 fp->fi_deleg_file = fl->fl_file;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003492 atomic_set(&fp->fi_delegees, 1);
Benny Halevycdc97502014-05-30 09:09:30 -04003493 spin_lock(&state_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04003494 hash_delegation_locked(dp, fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003495 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003496 return 0;
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003497out_free:
Trond Myklebustde186432014-07-10 14:07:26 -04003498 if (fl->fl_file)
3499 fput(fl->fl_file);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003500 locks_free_lock(fl);
3501 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003502}
3503
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003504static int nfs4_set_delegation(struct nfs4_delegation *dp, struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003505{
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003506 if (fp->fi_had_conflict)
3507 return -EAGAIN;
3508 get_nfs4_file(fp);
3509 dp->dl_file = fp;
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003510 if (!fp->fi_lease)
3511 return nfs4_setlease(dp);
Benny Halevycdc97502014-05-30 09:09:30 -04003512 spin_lock(&state_lock);
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003513 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003514 if (fp->fi_had_conflict) {
Benny Halevycdc97502014-05-30 09:09:30 -04003515 spin_unlock(&state_lock);
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003516 return -EAGAIN;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003517 }
Benny Halevy931ee562014-05-30 09:09:27 -04003518 hash_delegation_locked(dp, fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003519 spin_unlock(&state_lock);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003520 return 0;
3521}
3522
Benny Halevy4aa89132012-02-21 14:16:44 -08003523static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3524{
3525 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3526 if (status == -EAGAIN)
3527 open->op_why_no_deleg = WND4_CONTENTION;
3528 else {
3529 open->op_why_no_deleg = WND4_RESOURCE;
3530 switch (open->op_deleg_want) {
3531 case NFS4_SHARE_WANT_READ_DELEG:
3532 case NFS4_SHARE_WANT_WRITE_DELEG:
3533 case NFS4_SHARE_WANT_ANY_DELEG:
3534 break;
3535 case NFS4_SHARE_WANT_CANCEL:
3536 open->op_why_no_deleg = WND4_CANCELLED;
3537 break;
3538 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003539 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003540 }
3541 }
3542}
3543
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544/*
3545 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003546 *
3547 * Note we don't support write delegations, and won't until the vfs has
3548 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 */
3550static void
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003551nfs4_open_delegation(struct net *net, struct svc_fh *fh,
3552 struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553{
3554 struct nfs4_delegation *dp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003555 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003556 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003557 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003559 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003560 open->op_recall = 0;
3561 switch (open->op_claim_type) {
3562 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003563 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003564 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003565 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3566 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003567 break;
3568 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003569 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003570 /*
3571 * Let's not give out any delegations till everyone's
3572 * had the chance to reclaim theirs....
3573 */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003574 if (locks_in_grace(net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003575 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003576 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003577 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003578 /*
3579 * Also, if the file was opened for write or
3580 * create, there's a good chance the client's
3581 * about to write to it, resulting in an
3582 * immediate recall (since we don't support
3583 * write delegations):
3584 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003585 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003586 goto out_no_deleg;
3587 if (open->op_create == NFS4_OPEN_CREATE)
3588 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003589 break;
3590 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003591 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003592 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003593 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003594 if (dp == NULL)
3595 goto out_no_deleg;
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003596 status = nfs4_set_delegation(dp, stp->st_file);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003597 if (status)
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003598 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003600 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003602 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003603 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003604 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003605 return;
3606out_free:
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003607 destroy_delegation(dp);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003608out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003609 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3610 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003611 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003612 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003613 open->op_recall = 1;
3614 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003615
3616 /* 4.1 client asking for a delegation? */
3617 if (open->op_deleg_want)
3618 nfsd4_open_deleg_none_ext(open, status);
3619 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620}
3621
Benny Halevye27f49c2012-02-21 14:16:54 -08003622static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3623 struct nfs4_delegation *dp)
3624{
3625 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3626 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3627 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3628 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3629 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3630 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3631 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3632 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3633 }
3634 /* Otherwise the client must be confused wanting a delegation
3635 * it already has, therefore we don't return
3636 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3637 */
3638}
3639
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640/*
3641 * called with nfs4_lock_state() held.
3642 */
Al Virob37ad282006-10-19 23:28:59 -07003643__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3645{
Andy Adamson66689582009-04-03 08:28:45 +03003646 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003647 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 struct nfs4_file *fp = NULL;
3649 struct inode *ino = current_fh->fh_dentry->d_inode;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003650 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003651 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003652 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 /*
3655 * Lookup file; if found, lookup stateid and check open request,
3656 * and check for delegations in the process of being recalled.
3657 * If not found, create the nfs4_file struct
3658 */
Trond Myklebust950e0112014-06-30 11:48:31 -04003659 fp = find_or_add_file(ino, open->op_file);
3660 if (fp != open->op_file) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003661 status = nfs4_check_open(fp, open, &stp);
3662 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 goto out;
J. Bruce Fields41d22662013-03-21 15:49:47 -04003664 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003665 if (status)
3666 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003668 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003669 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003670 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003671 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003672 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 }
3674
3675 /*
3676 * OPEN the file, or upgrade an existing OPEN.
3677 * If truncate fails, the OPEN fails.
3678 */
3679 if (stp) {
3680 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003681 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 if (status)
3683 goto out;
3684 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003685 stp = open->op_stp;
3686 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003687 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003688 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3689 if (status) {
3690 release_open_stateid(stp);
3691 goto out;
3692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003694 update_stateid(&stp->st_stid.sc_stateid);
3695 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696
Benny Halevyd24433c2012-02-16 20:57:17 +02003697 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02003698 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3699 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3700 open->op_why_no_deleg = WND4_NOT_WANTED;
3701 goto nodeleg;
3702 }
3703 }
3704
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 /*
3706 * Attempt to hand out a delegation. No error return, because the
3707 * OPEN succeeds even if we fail.
3708 */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003709 nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003710nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 status = nfs_ok;
3712
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003713 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003714 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003716 /* 4.1 client trying to upgrade/downgrade delegation? */
3717 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003718 open->op_deleg_want)
3719 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003720
NeilBrown13cd2182005-06-23 22:03:10 -07003721 if (fp)
3722 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003723 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003724 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 /*
3726 * To finish the open response, we just need to set the rflags.
3727 */
3728 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003729 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003730 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3732
3733 return status;
3734}
3735
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003736void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3737{
3738 if (open->op_openowner) {
3739 struct nfs4_openowner *oo = open->op_openowner;
3740
3741 if (!list_empty(&oo->oo_owner.so_stateids))
3742 list_del_init(&oo->oo_close_lru);
3743 if (oo->oo_flags & NFS4_OO_NEW) {
3744 if (status) {
3745 release_openowner(oo);
3746 open->op_openowner = NULL;
3747 } else
3748 oo->oo_flags &= ~NFS4_OO_NEW;
3749 }
3750 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003751 if (open->op_file)
3752 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003753 if (open->op_stp)
J. Bruce Fieldsef798592012-08-29 10:32:54 -07003754 free_generic_stateid(open->op_stp);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003755}
3756
Al Virob37ad282006-10-19 23:28:59 -07003757__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003758nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3759 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760{
3761 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003762 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003763 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
3765 nfs4_lock_state();
3766 dprintk("process_renew(%08x/%08x): starting\n",
3767 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003768 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003769 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003771 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003773 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003774 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 goto out;
3776 status = nfs_ok;
3777out:
3778 nfs4_unlock_state();
3779 return status;
3780}
3781
NeilBrowna76b4312005-06-23 22:04:01 -07003782static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003783nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07003784{
Jeff Layton33dcc482012-04-10 11:08:48 -04003785 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003786 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003787 return;
3788
NeilBrowna76b4312005-06-23 22:04:01 -07003789 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003790 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003791 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003792 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003793 /*
3794 * Now that every NFSv4 client has had the chance to recover and
3795 * to see the (possibly new, possibly shorter) lease time, we
3796 * can safely set the next grace time to the current lease time:
3797 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03003798 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003799}
3800
NeilBrownfd39ca92005-06-23 22:04:03 -07003801static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003802nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803{
3804 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003805 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 struct nfs4_delegation *dp;
3807 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003808 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003809 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810
3811 nfs4_lock_state();
3812
3813 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003814 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03003815 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003816 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03003817 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 clp = list_entry(pos, struct nfs4_client, cl_lru);
3819 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3820 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003821 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 break;
3823 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003824 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03003825 dprintk("NFSD: client in use (clientid %08x)\n",
3826 clp->cl_clientid.cl_id);
3827 continue;
3828 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003829 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003830 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003831 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03003832 list_for_each_safe(pos, next, &reaplist) {
3833 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 dprintk("NFSD: purging unused client (clientid %08x)\n",
3835 clp->cl_clientid.cl_id);
3836 expire_client(clp);
3837 }
Benny Halevycdc97502014-05-30 09:09:30 -04003838 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003839 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03003841 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3842 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003844 t = dp->dl_time - cutoff;
3845 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 break;
3847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 list_move(&dp->dl_recall_lru, &reaplist);
3849 }
Benny Halevycdc97502014-05-30 09:09:30 -04003850 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 list_for_each_safe(pos, next, &reaplist) {
3852 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003853 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003855 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003856 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3857 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003858 t = oo->oo_time - cutoff;
3859 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 break;
3861 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003862 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04003864 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04003866 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867}
3868
Harvey Harrisona254b242008-02-20 12:49:00 -08003869static struct workqueue_struct *laundry_wq;
3870static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08003871
3872static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003873laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874{
3875 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003876 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3877 work);
3878 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3879 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003881 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003883 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884}
3885
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003886static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003887{
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003888 if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
3889 return nfserr_bad_stateid;
3890 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891}
3892
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003894access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003896 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3897 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3898 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899}
3900
3901static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003902access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003904 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3905 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906}
3907
3908static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003909__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910{
Al Virob37ad282006-10-19 23:28:59 -07003911 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912
J. Bruce Fields02921912010-07-29 15:16:59 -04003913 /* For lock stateid's, we test the parent open, not the lock: */
3914 if (stp->st_openstp)
3915 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003916 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003918 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 goto out;
3920 status = nfs_ok;
3921out:
3922 return status;
3923}
3924
Al Virob37ad282006-10-19 23:28:59 -07003925static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003926check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003928 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003930 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003931 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 * conflicting state; so we must wait out the grace period. */
3933 return nfserr_grace;
3934 } else if (flags & WR_STATE)
3935 return nfs4_share_conflict(current_fh,
3936 NFS4_SHARE_DENY_WRITE);
3937 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3938 return nfs4_share_conflict(current_fh,
3939 NFS4_SHARE_DENY_READ);
3940}
3941
3942/*
3943 * Allow READ/WRITE during grace period on recovered state only for files
3944 * that are not able to provide mandatory locking.
3945 */
3946static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003947grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003949 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950}
3951
J. Bruce Fields81b82962011-08-23 11:03:29 -04003952/* Returns true iff a is later than b: */
3953static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3954{
Jim Rees1a9357f2013-05-17 17:33:00 -04003955 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04003956}
3957
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003958static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05003959{
Andy Adamson66689582009-04-03 08:28:45 +03003960 /*
3961 * When sessions are used the stateid generation number is ignored
3962 * when it is zero.
3963 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04003964 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04003965 return nfs_ok;
3966
3967 if (in->si_generation == ref->si_generation)
3968 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03003969
J. Bruce Fields0836f582008-01-26 19:08:12 -05003970 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003971 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05003972 return nfserr_bad_stateid;
3973 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04003974 * However, we could see a stateid from the past, even from a
3975 * non-buggy client. For example, if the client sends a lock
3976 * while some IO is outstanding, the lock may bump si_generation
3977 * while the IO is still in flight. The client could avoid that
3978 * situation by waiting for responses on all the IO requests,
3979 * but better performance may result in retrying IO that
3980 * receives an old_stateid error if requests are rarely
3981 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05003982 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003983 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05003984}
3985
Chuck Lever7df302f2012-05-29 13:56:37 -04003986static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04003987{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003988 struct nfs4_stid *s;
3989 struct nfs4_ol_stateid *ols;
3990 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003991
Chuck Lever7df302f2012-05-29 13:56:37 -04003992 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3993 return nfserr_bad_stateid;
3994 /* Client debugging aid. */
3995 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
3996 char addr_str[INET6_ADDRSTRLEN];
3997 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
3998 sizeof(addr_str));
3999 pr_warn_ratelimited("NFSD: client %s testing state ID "
4000 "with incorrect client ID\n", addr_str);
4001 return nfserr_bad_stateid;
4002 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004003 s = find_stateid(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004004 if (!s)
Chuck Lever7df302f2012-05-29 13:56:37 -04004005 return nfserr_bad_stateid;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004006 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004007 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004008 return status;
J. Bruce Fields23340032013-04-09 17:42:28 -04004009 switch (s->sc_type) {
4010 case NFS4_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004011 return nfs_ok;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004012 case NFS4_REVOKED_DELEG_STID:
4013 return nfserr_deleg_revoked;
J. Bruce Fields23340032013-04-09 17:42:28 -04004014 case NFS4_OPEN_STID:
4015 case NFS4_LOCK_STID:
4016 ols = openlockstateid(s);
4017 if (ols->st_stateowner->so_is_open_owner
4018 && !(openowner(ols->st_stateowner)->oo_flags
4019 & NFS4_OO_CONFIRMED))
4020 return nfserr_bad_stateid;
4021 return nfs_ok;
4022 default:
4023 printk("unknown stateid type %x\n", s->sc_type);
4024 case NFS4_CLOSED_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004025 return nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004026 }
Bryan Schumaker17456802011-07-13 10:50:48 -04004027}
4028
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004029static __be32
4030nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4031 stateid_t *stateid, unsigned char typemask,
4032 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004033{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004034 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004035
4036 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4037 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004038 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004039 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004040 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004041 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004042 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004043 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004044 if (status)
4045 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004046 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004047 if (!*s)
4048 return nfserr_bad_stateid;
4049 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004050}
4051
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052/*
4053* Checks for stateid operations
4054*/
Al Virob37ad282006-10-19 23:28:59 -07004055__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004056nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004057 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004059 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004060 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004062 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004064 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004065 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004066 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 if (filpp)
4069 *filpp = NULL;
4070
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004071 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 return nfserr_grace;
4073
4074 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004075 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076
Trond Myklebust14bcab12014-04-18 14:44:07 -04004077 nfs4_lock_state();
4078
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004079 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004080 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004081 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004082 if (status)
Trond Myklebust14bcab12014-04-18 14:44:07 -04004083 goto out;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004084 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4085 if (status)
4086 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004087 switch (s->sc_type) {
4088 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004089 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004090 status = nfs4_check_delegmode(dp, flags);
4091 if (status)
4092 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004093 if (filpp) {
Trond Myklebust14bcab12014-04-18 14:44:07 -04004094 file = dp->dl_file->fi_deleg_file;
4095 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004096 WARN_ON_ONCE(1);
4097 status = nfserr_serverfault;
4098 goto out;
4099 }
Trond Myklebustde186432014-07-10 14:07:26 -04004100 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004101 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004102 break;
4103 case NFS4_OPEN_STID:
4104 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004105 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004106 status = nfs4_check_fh(current_fh, stp);
4107 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004109 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004110 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004112 status = nfs4_check_openmode(stp, flags);
4113 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004115 if (filpp) {
4116 if (flags & RD_STATE)
Trond Myklebust14bcab12014-04-18 14:44:07 -04004117 file = find_readable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004118 else
Trond Myklebust14bcab12014-04-18 14:44:07 -04004119 file = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004120 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004121 break;
4122 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004123 status = nfserr_bad_stateid;
4124 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 }
4126 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004127 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004128 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129out:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004130 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 return status;
4132}
4133
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004134static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004135nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004136{
J. Bruce Fieldsa1b8ff42014-05-20 15:55:21 -04004137 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4138
4139 if (check_for_locks(stp->st_file, lo))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004140 return nfserr_locks_held;
Jeff Laytonc53530d2014-06-30 11:48:39 -04004141 release_lockowner_if_empty(lo);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004142 return nfs_ok;
4143}
4144
4145/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004146 * Test if the stateid is valid
4147 */
4148__be32
4149nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4150 struct nfsd4_test_stateid *test_stateid)
4151{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004152 struct nfsd4_test_stateid_id *stateid;
4153 struct nfs4_client *cl = cstate->session->se_client;
4154
4155 nfs4_lock_state();
4156 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004157 stateid->ts_id_status =
4158 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004159 nfs4_unlock_state();
4160
Bryan Schumaker17456802011-07-13 10:50:48 -04004161 return nfs_ok;
4162}
4163
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004164__be32
4165nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4166 struct nfsd4_free_stateid *free_stateid)
4167{
4168 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004169 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004170 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004171 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004172 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004173
4174 nfs4_lock_state();
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004175 s = find_stateid(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004176 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004177 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004178 switch (s->sc_type) {
4179 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004180 ret = nfserr_locks_held;
4181 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004182 case NFS4_OPEN_STID:
4183 case NFS4_LOCK_STID:
4184 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4185 if (ret)
4186 goto out;
4187 if (s->sc_type == NFS4_LOCK_STID)
4188 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4189 else
4190 ret = nfserr_locks_held;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004191 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004192 case NFS4_REVOKED_DELEG_STID:
4193 dp = delegstateid(s);
4194 destroy_revoked_delegation(dp);
4195 ret = nfs_ok;
4196 break;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004197 default:
4198 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004199 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004200out:
4201 nfs4_unlock_state();
4202 return ret;
4203}
4204
NeilBrown4c4cd222005-07-07 17:59:27 -07004205static inline int
4206setlkflg (int type)
4207{
4208 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4209 RD_STATE : WR_STATE;
4210}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004212static __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 -04004213{
4214 struct svc_fh *current_fh = &cstate->current_fh;
4215 struct nfs4_stateowner *sop = stp->st_stateowner;
4216 __be32 status;
4217
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004218 status = nfsd4_check_seqid(cstate, sop, seqid);
4219 if (status)
4220 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004221 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4222 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004223 /*
4224 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004225 * nfserr_replay_me from the previous step, and
4226 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004227 */
4228 return nfserr_bad_stateid;
4229 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4230 if (status)
4231 return status;
4232 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004233}
4234
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235/*
4236 * Checks for sequence id mutating operations.
4237 */
Al Virob37ad282006-10-19 23:28:59 -07004238static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004239nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004240 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004241 struct nfs4_ol_stateid **stpp,
4242 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004244 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004245 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004246 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004248 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4249 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004250
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004252 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004253 if (status)
4254 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004255 stp = openlockstateid(s);
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04004256 if (!nfsd4_has_session(cstate))
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004257 cstate->replay_owner = stp->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004259 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4260 if (!status)
4261 *stpp = stp;
4262 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004263}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004264
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004265static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4266 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004267{
4268 __be32 status;
4269 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004271 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004272 NFS4_OPEN_STID, stpp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004273 if (status)
4274 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004275 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004276 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07004277 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004278 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279}
4280
Al Virob37ad282006-10-19 23:28:59 -07004281__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004282nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004283 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284{
Al Virob37ad282006-10-19 23:28:59 -07004285 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004286 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004287 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004288 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289
Al Viroa6a9f182013-09-16 10:57:01 -04004290 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4291 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004293 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004294 if (status)
4295 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296
4297 nfs4_lock_state();
4298
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004299 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004300 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004301 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004302 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004303 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004304 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004305 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004306 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004307 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004308 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004309 update_stateid(&stp->st_stid.sc_stateid);
4310 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004311 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004312 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004313
Jeff Layton2a4317c2012-03-21 16:42:43 -04004314 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004315 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004317 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004318 if (!cstate->replay_owner)
4319 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 return status;
4321}
4322
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004323static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004325 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004326 return;
Jeff Layton12659652014-07-10 14:07:28 -04004327 nfs4_file_put_access(stp->st_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004328 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004329}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004330
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004331static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4332{
4333 switch (to_access) {
4334 case NFS4_SHARE_ACCESS_READ:
4335 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4336 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4337 break;
4338 case NFS4_SHARE_ACCESS_WRITE:
4339 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4340 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4341 break;
4342 case NFS4_SHARE_ACCESS_BOTH:
4343 break;
4344 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004345 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 }
4347}
4348
Al Virob37ad282006-10-19 23:28:59 -07004349__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004350nfsd4_open_downgrade(struct svc_rqst *rqstp,
4351 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004352 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353{
Al Virob37ad282006-10-19 23:28:59 -07004354 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004355 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004356 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
Al Viroa6a9f182013-09-16 10:57:01 -04004358 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4359 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004361 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004362 if (od->od_deleg_want)
4363 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4364 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365
4366 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004367 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004368 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004369 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004372 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004373 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 stp->st_access_bmap, od->od_share_access);
4375 goto out;
4376 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004377 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004378 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 stp->st_deny_bmap, od->od_share_deny);
4380 goto out;
4381 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004382 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383
Jeff Laytonce0fc432012-05-11 09:45:14 -04004384 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004386 update_stateid(&stp->st_stid.sc_stateid);
4387 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 status = nfs_ok;
4389out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004390 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004391 if (!cstate->replay_owner)
4392 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 return status;
4394}
4395
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004396static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4397{
Trond Myklebustacf92952014-06-30 11:48:37 -04004398 struct nfs4_client *clp = s->st_stid.sc_client;
4399 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4400
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004401 s->st_stid.sc_type = NFS4_CLOSED_STID;
Trond Myklebustacf92952014-06-30 11:48:37 -04004402 unhash_open_stateid(s);
4403
4404 if (clp->cl_minorversion) {
4405 free_generic_stateid(s);
4406 if (list_empty(&oo->oo_owner.so_stateids))
4407 release_openowner(oo);
4408 } else {
4409 oo->oo_last_closed_stid = s;
4410 /*
4411 * In the 4.0 case we need to keep the owners around a
4412 * little while to handle CLOSE replay.
4413 */
4414 if (list_empty(&oo->oo_owner.so_stateids))
4415 move_to_close_lru(oo, clp->net);
4416 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004417}
4418
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419/*
4420 * nfs4_unlock_state() called after encode
4421 */
Al Virob37ad282006-10-19 23:28:59 -07004422__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004423nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004424 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425{
Al Virob37ad282006-10-19 23:28:59 -07004426 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004427 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004428 struct net *net = SVC_NET(rqstp);
4429 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430
Al Viroa6a9f182013-09-16 10:57:01 -04004431 dprintk("NFSD: nfsd4_close on file %pd\n",
4432 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433
4434 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004435 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4436 &close->cl_stateid,
4437 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004438 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004439 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004440 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004442 update_stateid(&stp->st_stid.sc_stateid);
4443 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004445 nfsd4_close_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004447 if (!cstate->replay_owner)
4448 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 return status;
4450}
4451
Al Virob37ad282006-10-19 23:28:59 -07004452__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004453nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4454 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004456 struct nfs4_delegation *dp;
4457 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004458 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004459 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004460 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004462 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004463 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
4465 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004466 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004467 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004468 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004469 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004470 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004471 if (status)
4472 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004473
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004474 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004476 nfs4_unlock_state();
4477
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 return status;
4479}
4480
4481
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483
Benny Halevy87df4de2008-12-15 19:42:03 +02004484static inline u64
4485end_offset(u64 start, u64 len)
4486{
4487 u64 end;
4488
4489 end = start + len;
4490 return end >= start ? end: NFS4_MAX_UINT64;
4491}
4492
4493/* last octet in a range */
4494static inline u64
4495last_byte_offset(u64 start, u64 len)
4496{
4497 u64 end;
4498
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004499 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004500 end = start + len;
4501 return end > start ? end - 1: NFS4_MAX_UINT64;
4502}
4503
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504/*
4505 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4506 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4507 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4508 * locking, this prevents us from being completely protocol-compliant. The
4509 * real solution to this problem is to start using unsigned file offsets in
4510 * the VFS, but this is a very deep change!
4511 */
4512static inline void
4513nfs4_transform_lock_offset(struct file_lock *lock)
4514{
4515 if (lock->fl_start < 0)
4516 lock->fl_start = OFFSET_MAX;
4517 if (lock->fl_end < 0)
4518 lock->fl_end = OFFSET_MAX;
4519}
4520
NeilBrownd5b90262006-04-10 22:55:22 -07004521/* Hack!: For now, we're defining this just so we can use a pointer to it
4522 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004523static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004524};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525
4526static inline void
4527nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4528{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004529 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530
NeilBrownd5b90262006-04-10 22:55:22 -07004531 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004532 lo = (struct nfs4_lockowner *) fl->fl_owner;
4533 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4534 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004535 if (!deny->ld_owner.data)
4536 /* We just don't care that much */
4537 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004538 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4539 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004540 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004541nevermind:
4542 deny->ld_owner.len = 0;
4543 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004544 deny->ld_clientid.cl_boot = 0;
4545 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 }
4547 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004548 deny->ld_length = NFS4_MAX_UINT64;
4549 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4551 deny->ld_type = NFS4_READ_LT;
4552 if (fl->fl_type != F_RDLCK)
4553 deny->ld_type = NFS4_WRITE_LT;
4554}
4555
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004556static struct nfs4_lockowner *
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004557find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4558 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559{
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004560 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4561 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004563 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4564 if (so->so_is_open_owner)
4565 continue;
4566 if (!same_owner_str(so, owner, clid))
4567 continue;
4568 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 }
4570 return NULL;
4571}
4572
4573/*
4574 * Alloc a lock owner structure.
4575 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004576 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004578 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004580static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004581alloc_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 -04004582 struct nfs4_lockowner *lo;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004583 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004585 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4586 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004588 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4589 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07004590 /* It is the openowner seqid that will be incremented in encode in the
4591 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004592 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004593 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004594 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595}
4596
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004597static struct nfs4_ol_stateid *
4598alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004600 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004601 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602
J. Bruce Fields996e0932011-10-17 11:14:48 -04004603 stp = nfs4_alloc_stateid(clp);
NeilBrown5ac049a2005-06-23 22:03:03 -07004604 if (stp == NULL)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04004605 return NULL;
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004606 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004607 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4608 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004609 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 stp->st_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004611 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004613 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004614 list_add(&stp->st_locks, &open_stp->st_locks);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004615 spin_lock(&fp->fi_lock);
4616 list_add(&stp->st_perfile, &fp->fi_stateids);
4617 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 return stp;
4619}
4620
Jeff Laytonc53530d2014-06-30 11:48:39 -04004621static struct nfs4_ol_stateid *
4622find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4623{
4624 struct nfs4_ol_stateid *lst;
4625
4626 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
4627 if (lst->st_file == fp)
4628 return lst;
4629 }
4630 return NULL;
4631}
4632
4633
NeilBrownfd39ca92005-06-23 22:04:03 -07004634static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635check_lock_length(u64 offset, u64 length)
4636{
Benny Halevy87df4de2008-12-15 19:42:03 +02004637 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 LOFF_OVERFLOW(offset, length)));
4639}
4640
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004641static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004642{
4643 struct nfs4_file *fp = lock_stp->st_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004644
Jeff Layton7214e862014-07-10 14:07:33 -04004645 lockdep_assert_held(&fp->fi_lock);
4646
Jeff Layton82c5ff12012-05-11 09:45:13 -04004647 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004648 return;
Jeff Layton12659652014-07-10 14:07:28 -04004649 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004650 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004651}
4652
J. Bruce Fields2355c592012-04-25 16:56:22 -04004653static __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 -04004654{
4655 struct nfs4_file *fi = ost->st_file;
4656 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4657 struct nfs4_client *cl = oo->oo_owner.so_client;
4658 struct nfs4_lockowner *lo;
4659 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004660 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004661
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004662 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004663 if (!lo) {
4664 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4665 &lock->v.new.owner);
4666 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4667 if (lo == NULL)
4668 return nfserr_jukebox;
4669 } else {
4670 /* with an existing lockowner, seqids must be the same */
4671 if (!cstate->minorversion &&
4672 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004673 return nfserr_bad_seqid;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004674 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004675
4676 *lst = find_lock_stateid(lo, fi);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004677 if (*lst == NULL) {
Jeff Laytonc53530d2014-06-30 11:48:39 -04004678 *lst = alloc_init_lock_stateid(lo, fi, ost);
4679 if (*lst == NULL) {
4680 release_lockowner_if_empty(lo);
4681 return nfserr_jukebox;
4682 }
4683 *new = true;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004684 }
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004685 return nfs_ok;
4686}
4687
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688/*
4689 * LOCK operation
4690 */
Al Virob37ad282006-10-19 23:28:59 -07004691__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004692nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004693 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004695 struct nfs4_openowner *open_sop = NULL;
4696 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004697 struct nfs4_ol_stateid *lock_stp;
Jeff Layton7214e862014-07-10 14:07:33 -04004698 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004699 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004700 struct file_lock *file_lock = NULL;
4701 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004702 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004703 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004704 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004705 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004706 struct net *net = SVC_NET(rqstp);
4707 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
4709 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4710 (long long) lock->lk_offset,
4711 (long long) lock->lk_length);
4712
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 if (check_lock_length(lock->lk_offset, lock->lk_length))
4714 return nfserr_inval;
4715
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004716 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004717 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004718 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4719 return status;
4720 }
4721
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 nfs4_lock_state();
4723
4724 if (lock->lk_is_new) {
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004725 struct nfs4_ol_stateid *open_stp = NULL;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004726
4727 if (nfsd4_has_session(cstate))
4728 /* See rfc 5661 18.10.3: given clientid is ignored: */
4729 memcpy(&lock->v.new.clientid,
4730 &cstate->session->se_client->cl_clientid,
4731 sizeof(clientid_t));
4732
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004734 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004738 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 lock->lk_new_open_seqid,
4740 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004741 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07004742 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004744 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004745 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004746 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004747 &lock->v.new.clientid))
4748 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004749 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4750 &lock_stp, &new_state);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004751 } else
Benny Halevydd453df2009-04-03 08:28:41 +03004752 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004753 lock->lk_old_lock_seqid,
4754 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004755 NFS4_LOCK_STID, &lock_stp, nn);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004756 if (status)
4757 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004758 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004760 lkflg = setlkflg(lock->lk_type);
4761 status = nfs4_check_openmode(lock_stp, lkflg);
4762 if (status)
4763 goto out;
4764
NeilBrown0dd395d2005-07-07 17:59:15 -07004765 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004766 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004767 goto out;
4768 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004769 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004770 goto out;
4771
Jeff Layton21179d82012-08-21 08:03:32 -04004772 file_lock = locks_alloc_lock();
4773 if (!file_lock) {
4774 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4775 status = nfserr_jukebox;
4776 goto out;
4777 }
4778
Jeff Layton7214e862014-07-10 14:07:33 -04004779 fp = lock_stp->st_file;
Jeff Layton21179d82012-08-21 08:03:32 -04004780 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 switch (lock->lk_type) {
4782 case NFS4_READ_LT:
4783 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004784 spin_lock(&fp->fi_lock);
4785 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004786 if (filp)
4787 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04004788 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004789 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004790 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 case NFS4_WRITE_LT:
4792 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04004793 spin_lock(&fp->fi_lock);
4794 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004795 if (filp)
4796 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04004797 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04004798 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004799 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 default:
4801 status = nfserr_inval;
4802 goto out;
4803 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004804 if (!filp) {
4805 status = nfserr_openmode;
4806 goto out;
4807 }
Jeff Layton21179d82012-08-21 08:03:32 -04004808 file_lock->fl_owner = (fl_owner_t)lock_sop;
4809 file_lock->fl_pid = current->tgid;
4810 file_lock->fl_file = filp;
4811 file_lock->fl_flags = FL_POSIX;
4812 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4813 file_lock->fl_start = lock->lk_offset;
4814 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4815 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816
Jeff Layton21179d82012-08-21 08:03:32 -04004817 conflock = locks_alloc_lock();
4818 if (!conflock) {
4819 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4820 status = nfserr_jukebox;
4821 goto out;
4822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823
Jeff Layton21179d82012-08-21 08:03:32 -04004824 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004825 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004827 update_stateid(&lock_stp->st_stid.sc_stateid);
4828 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004830 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004831 break;
4832 case (EAGAIN): /* conflock holds conflicting lock */
4833 status = nfserr_denied;
4834 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04004835 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004836 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 case (EDEADLK):
4838 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004839 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004840 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004841 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004842 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004843 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845out:
Trond Myklebustde186432014-07-10 14:07:26 -04004846 if (filp)
4847 fput(filp);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004848 if (status && new_state)
Jeff Laytonc53530d2014-06-30 11:48:39 -04004849 release_lock_stateid(lock_stp);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004850 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004851 if (!cstate->replay_owner)
4852 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004853 if (file_lock)
4854 locks_free_lock(file_lock);
4855 if (conflock)
4856 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 return status;
4858}
4859
4860/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004861 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4862 * so we do a temporary open here just to get an open file to pass to
4863 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4864 * inode operation.)
4865 */
Al Viro04da6e92012-04-13 00:00:04 -04004866static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004867{
4868 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04004869 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4870 if (!err) {
4871 err = nfserrno(vfs_test_lock(file, lock));
4872 nfsd_close(file);
4873 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004874 return err;
4875}
4876
4877/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 * LOCKT operation
4879 */
Al Virob37ad282006-10-19 23:28:59 -07004880__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004881nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4882 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883{
Jeff Layton21179d82012-08-21 08:03:32 -04004884 struct file_lock *file_lock = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004885 struct nfs4_lockowner *lo;
Al Virob37ad282006-10-19 23:28:59 -07004886 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004887 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004889 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 return nfserr_grace;
4891
4892 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4893 return nfserr_inval;
4894
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 nfs4_lock_state();
4896
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004897 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004898 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004899 if (status)
4900 goto out;
4901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004903 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905
Jeff Layton21179d82012-08-21 08:03:32 -04004906 file_lock = locks_alloc_lock();
4907 if (!file_lock) {
4908 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4909 status = nfserr_jukebox;
4910 goto out;
4911 }
4912 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 switch (lockt->lt_type) {
4914 case NFS4_READ_LT:
4915 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004916 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 break;
4918 case NFS4_WRITE_LT:
4919 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004920 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 break;
4922 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04004923 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 status = nfserr_inval;
4925 goto out;
4926 }
4927
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004928 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004929 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04004930 file_lock->fl_owner = (fl_owner_t)lo;
4931 file_lock->fl_pid = current->tgid;
4932 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933
Jeff Layton21179d82012-08-21 08:03:32 -04004934 file_lock->fl_start = lockt->lt_offset;
4935 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936
Jeff Layton21179d82012-08-21 08:03:32 -04004937 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938
Jeff Layton21179d82012-08-21 08:03:32 -04004939 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04004940 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05004941 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04004942
Jeff Layton21179d82012-08-21 08:03:32 -04004943 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04004945 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 }
4947out:
4948 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004949 if (file_lock)
4950 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 return status;
4952}
4953
Al Virob37ad282006-10-19 23:28:59 -07004954__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004955nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004956 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004958 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004960 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004961 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07004962 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004963 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4964
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4966 (long long) locku->lu_offset,
4967 (long long) locku->lu_length);
4968
4969 if (check_lock_length(locku->lu_offset, locku->lu_length))
4970 return nfserr_inval;
4971
4972 nfs4_lock_state();
4973
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004974 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004975 &locku->lu_stateid, NFS4_LOCK_STID,
4976 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004977 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004979 filp = find_any_file(stp->st_file);
4980 if (!filp) {
4981 status = nfserr_lock_range;
4982 goto out;
4983 }
Jeff Layton21179d82012-08-21 08:03:32 -04004984 file_lock = locks_alloc_lock();
4985 if (!file_lock) {
4986 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4987 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04004988 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04004989 }
4990 locks_init_lock(file_lock);
4991 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04004992 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04004993 file_lock->fl_pid = current->tgid;
4994 file_lock->fl_file = filp;
4995 file_lock->fl_flags = FL_POSIX;
4996 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4997 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998
Jeff Layton21179d82012-08-21 08:03:32 -04004999 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5000 locku->lu_length);
5001 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002
Jeff Layton21179d82012-08-21 08:03:32 -04005003 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005004 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005005 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 goto out_nfserr;
5007 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005008 update_stateid(&stp->st_stid.sc_stateid);
5009 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005010fput:
5011 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005013 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04005014 if (!cstate->replay_owner)
5015 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005016 if (file_lock)
5017 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 return status;
5019
5020out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005021 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005022 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023}
5024
5025/*
5026 * returns
5027 * 1: locks held by lockowner
5028 * 0: no locks held by lockowner
5029 */
5030static int
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005031check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032{
5033 struct file_lock **flpp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005034 struct inode *inode = filp->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 int status = 0;
5036
Jeff Layton1c8c6012013-06-21 08:58:15 -04005037 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005039 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 status = 1;
5041 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 }
5044out:
Jeff Layton1c8c6012013-06-21 08:58:15 -04005045 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 return status;
5047}
5048
Al Virob37ad282006-10-19 23:28:59 -07005049__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005050nfsd4_release_lockowner(struct svc_rqst *rqstp,
5051 struct nfsd4_compound_state *cstate,
5052 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053{
5054 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Laytonfd449072014-06-30 11:48:41 -04005055 struct nfs4_stateowner *sop = NULL, *tmp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005056 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005057 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 struct xdr_netobj *owner = &rlockowner->rl_owner;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005059 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07005060 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005061 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062
5063 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5064 clid->cl_boot, clid->cl_id);
5065
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 nfs4_lock_state();
5067
Jeff Layton4b24ca72014-06-30 11:48:44 -04005068 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005069 if (status)
5070 goto out;
5071
NeilBrown3e9e3db2005-06-23 22:04:20 -07005072 status = nfserr_locks_held;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05005073
Jeff Laytonfd449072014-06-30 11:48:41 -04005074 /* Find the matching lock stateowner */
5075 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
5076 if (tmp->so_is_open_owner)
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005077 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005078 if (same_owner_str(tmp, owner, clid)) {
5079 sop = tmp;
5080 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07005082 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005083
5084 /* No matching owner found, maybe a replay? Just declare victory... */
5085 if (!sop) {
5086 status = nfs_ok;
5087 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005089
5090 lo = lockowner(sop);
5091 /* see if there are still any locks associated with it */
5092 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5093 if (check_for_locks(stp->st_file, lo))
5094 goto out;
5095 }
5096
5097 status = nfs_ok;
5098 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099out:
5100 nfs4_unlock_state();
5101 return status;
5102}
5103
5104static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005105alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106{
NeilBrowna55370a2005-06-23 22:03:52 -07005107 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108}
5109
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005110bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005111nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005112{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005113 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005114
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005115 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005116 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005117}
5118
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119/*
5120 * failure => all reset bets are off, nfserr_no_grace...
5121 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005122struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005123nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124{
5125 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005126 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127
NeilBrowna55370a2005-06-23 22:03:52 -07005128 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5129 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005130 if (crp) {
5131 strhashval = clientstr_hashval(name);
5132 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005133 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005134 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005135 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005136 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005137 }
5138 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139}
5140
Jeff Layton2a4317c2012-03-21 16:42:43 -04005141void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005142nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005143{
5144 list_del(&crp->cr_strhash);
5145 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005146 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005147}
5148
5149void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005150nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151{
5152 struct nfs4_client_reclaim *crp = NULL;
5153 int i;
5154
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005156 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5157 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005159 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 }
5161 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005162 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163}
5164
5165/*
5166 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005167struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005168nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169{
5170 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 struct nfs4_client_reclaim *crp = NULL;
5172
Jeff Layton278c9312012-11-12 15:00:52 -05005173 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174
Jeff Layton278c9312012-11-12 15:00:52 -05005175 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005176 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005177 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 return crp;
5179 }
5180 }
5181 return NULL;
5182}
5183
5184/*
5185* Called from OPEN. Look for clientid in reclaim list.
5186*/
Al Virob37ad282006-10-19 23:28:59 -07005187__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005188nfs4_check_open_reclaim(clientid_t *clid,
5189 struct nfsd4_compound_state *cstate,
5190 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005192 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005193
5194 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005195 status = lookup_clientid(clid, cstate, nn);
5196 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005197 return nfserr_reclaim_bad;
5198
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005199 if (nfsd4_client_record_check(cstate->clp))
5200 return nfserr_reclaim_bad;
5201
5202 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203}
5204
Bryan Schumaker65178db2011-11-01 13:35:21 -04005205#ifdef CONFIG_NFSD_FAULT_INJECTION
5206
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005207u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5208{
J. Bruce Fields221a6872013-04-01 22:23:49 -04005209 if (mark_client_expired(clp))
5210 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005211 expire_client(clp);
5212 return 1;
5213}
5214
Bryan Schumaker184c1842012-11-29 11:40:44 -05005215u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5216{
5217 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005218 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005219 printk(KERN_INFO "NFS Client: %s\n", buf);
5220 return 1;
5221}
5222
5223static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5224 const char *type)
5225{
5226 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005227 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005228 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5229}
5230
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005231static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5232 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005233{
5234 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005235 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005236 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005237 u64 count = 0;
5238
5239 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005240 list_for_each_entry_safe(stp, st_next,
5241 &oop->oo_owner.so_stateids, st_perstateowner) {
5242 list_for_each_entry_safe(lst, lst_next,
5243 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005244 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005245 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005246 if (++count == max)
5247 return count;
5248 }
5249 }
5250 }
5251
5252 return count;
5253}
5254
5255u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5256{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005257 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005258}
5259
Bryan Schumaker184c1842012-11-29 11:40:44 -05005260u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5261{
5262 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5263 nfsd_print_count(clp, count, "locked files");
5264 return count;
5265}
5266
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005267static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5268{
5269 struct nfs4_openowner *oop, *next;
5270 u64 count = 0;
5271
5272 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5273 if (func)
5274 func(oop);
5275 if (++count == max)
5276 break;
5277 }
5278
5279 return count;
5280}
5281
5282u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5283{
5284 return nfsd_foreach_client_open(clp, max, release_openowner);
5285}
5286
Bryan Schumaker184c1842012-11-29 11:40:44 -05005287u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5288{
5289 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5290 nfsd_print_count(clp, count, "open files");
5291 return count;
5292}
5293
Bryan Schumaker269de302012-11-29 11:40:42 -05005294static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5295 struct list_head *victims)
5296{
5297 struct nfs4_delegation *dp, *next;
5298 u64 count = 0;
5299
Benny Halevycdc97502014-05-30 09:09:30 -04005300 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005301 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005302 if (victims) {
5303 /*
5304 * It's not safe to mess with delegations that have a
5305 * non-zero dl_time. They might have already been broken
5306 * and could be processed by the laundromat outside of
5307 * the state_lock. Just leave them be.
5308 */
5309 if (dp->dl_time != 0)
5310 continue;
5311
5312 /*
5313 * Increment dl_time to ensure that delegation breaks
5314 * don't monkey with it now that we are.
5315 */
5316 ++dp->dl_time;
Bryan Schumaker269de302012-11-29 11:40:42 -05005317 list_move(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005318 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005319 if (++count == max)
5320 break;
5321 }
5322 return count;
5323}
5324
5325u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5326{
5327 struct nfs4_delegation *dp, *next;
5328 LIST_HEAD(victims);
5329 u64 count;
5330
Benny Halevycdc97502014-05-30 09:09:30 -04005331 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005332 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005333 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005334
5335 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005336 revoke_delegation(dp);
Bryan Schumaker269de302012-11-29 11:40:42 -05005337
5338 return count;
5339}
5340
5341u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5342{
Jeff Laytondff13992014-07-08 14:02:49 -04005343 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005344 LIST_HEAD(victims);
5345 u64 count;
5346
Benny Halevycdc97502014-05-30 09:09:30 -04005347 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005348 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005349 while (!list_empty(&victims)) {
5350 dp = list_first_entry(&victims, struct nfs4_delegation,
5351 dl_recall_lru);
5352 list_del_init(&dp->dl_recall_lru);
5353 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005354 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005355 }
Benny Halevycdc97502014-05-30 09:09:30 -04005356 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005357
5358 return count;
5359}
5360
Bryan Schumaker184c1842012-11-29 11:40:44 -05005361u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5362{
5363 u64 count = 0;
5364
Benny Halevycdc97502014-05-30 09:09:30 -04005365 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005366 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005367 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005368
5369 nfsd_print_count(clp, count, "delegations");
5370 return count;
5371}
5372
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005373u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005374{
5375 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005376 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005377 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005378
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005379 if (!nfsd_netns_ready(nn))
5380 return 0;
5381
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005382 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005383 count += func(clp, max - count);
5384 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005385 break;
5386 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005387
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005388 return count;
5389}
5390
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005391struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5392{
5393 struct nfs4_client *clp;
5394 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5395
5396 if (!nfsd_netns_ready(nn))
5397 return NULL;
5398
5399 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5400 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5401 return clp;
5402 }
5403 return NULL;
5404}
5405
Bryan Schumaker65178db2011-11-01 13:35:21 -04005406#endif /* CONFIG_NFSD_FAULT_INJECTION */
5407
Meelap Shahc2f1a552007-07-17 04:04:39 -07005408/*
5409 * Since the lifetime of a delegation isn't limited to that of an open, a
5410 * client may quite reasonably hang on to a delegation as long as it has
5411 * the inode cached. This becomes an obvious problem the first time a
5412 * client's inode cache approaches the size of the server's total memory.
5413 *
5414 * For now we avoid this problem by imposing a hard limit on the number
5415 * of delegations, which varies according to the server's memory size.
5416 */
5417static void
5418set_max_delegations(void)
5419{
5420 /*
5421 * Allow at most 4 delegations per megabyte of RAM. Quick
5422 * estimates suggest that in the worst case (where every delegation
5423 * is for a different inode), a delegation could take about 1.5K,
5424 * giving a worst case usage of about 6% of memory.
5425 */
5426 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5427}
5428
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005429static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005430{
5431 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5432 int i;
5433
5434 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5435 CLIENT_HASH_SIZE, GFP_KERNEL);
5436 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005437 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005438 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5439 CLIENT_HASH_SIZE, GFP_KERNEL);
5440 if (!nn->unconf_id_hashtbl)
5441 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005442 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5443 OWNER_HASH_SIZE, GFP_KERNEL);
5444 if (!nn->ownerstr_hashtbl)
5445 goto err_ownerstr;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005446 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5447 SESSION_HASH_SIZE, GFP_KERNEL);
5448 if (!nn->sessionid_hashtbl)
5449 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005450
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005451 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005452 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005453 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005454 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005455 for (i = 0; i < OWNER_HASH_SIZE; i++)
5456 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005457 for (i = 0; i < SESSION_HASH_SIZE; i++)
5458 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005459 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005460 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005461 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005462 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005463 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005464 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005465
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005466 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005467 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005468
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005469 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005470
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005471err_sessionid:
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03005472 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005473err_ownerstr:
5474 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005475err_unconf_id:
5476 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005477err:
5478 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005479}
5480
5481static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005482nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005483{
5484 int i;
5485 struct nfs4_client *clp = NULL;
5486 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5487
5488 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5489 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5490 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5491 destroy_client(clp);
5492 }
5493 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005494
Kinglong Mee2b905632014-03-26 22:09:30 +08005495 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5496 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5497 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5498 destroy_client(clp);
5499 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005500 }
5501
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005502 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005503 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005504 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005505 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005506 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005507}
5508
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005509int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005510nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005511{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005512 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005513 int ret;
5514
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005515 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005516 if (ret)
5517 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005518 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005519 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005520 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005521 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005522 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005523 nn->nfsd4_grace, net);
5524 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005525 return 0;
5526}
5527
5528/* initialization to perform when the nfsd service is started: */
5529
5530int
5531nfs4_state_start(void)
5532{
5533 int ret;
5534
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005535 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005536 if (ret)
5537 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005538 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005539 if (laundry_wq == NULL) {
5540 ret = -ENOMEM;
5541 goto out_recovery;
5542 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005543 ret = nfsd4_create_callback_queue();
5544 if (ret)
5545 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005546
Meelap Shahc2f1a552007-07-17 04:04:39 -07005547 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005548
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005549 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005550
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005551out_free_laundry:
5552 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005553out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005554 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555}
5556
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005557void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005558nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005562 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005564 cancel_delayed_work_sync(&nn->laundromat_work);
5565 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005566
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005567 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04005569 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005570 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5572 list_move(&dp->dl_recall_lru, &reaplist);
5573 }
Benny Halevycdc97502014-05-30 09:09:30 -04005574 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 list_for_each_safe(pos, next, &reaplist) {
5576 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005577 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 }
5579
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005580 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005581 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005582 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583}
5584
5585void
5586nfs4_state_shutdown(void)
5587{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005588 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005589 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005591
5592static void
5593get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5594{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005595 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5596 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005597}
5598
5599static void
5600put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5601{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005602 if (cstate->minorversion) {
5603 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5604 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5605 }
5606}
5607
5608void
5609clear_current_stateid(struct nfsd4_compound_state *cstate)
5610{
5611 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005612}
5613
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005614/*
5615 * functions to set current state id
5616 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005617void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005618nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5619{
5620 put_stateid(cstate, &odp->od_stateid);
5621}
5622
5623void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005624nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5625{
5626 put_stateid(cstate, &open->op_stateid);
5627}
5628
5629void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005630nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5631{
5632 put_stateid(cstate, &close->cl_stateid);
5633}
5634
5635void
5636nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5637{
5638 put_stateid(cstate, &lock->lk_resp_stateid);
5639}
5640
5641/*
5642 * functions to consume current state id
5643 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005644
5645void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005646nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5647{
5648 get_stateid(cstate, &odp->od_stateid);
5649}
5650
5651void
5652nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5653{
5654 get_stateid(cstate, &drp->dr_stateid);
5655}
5656
5657void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005658nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5659{
5660 get_stateid(cstate, &fsp->fr_stateid);
5661}
5662
5663void
5664nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5665{
5666 get_stateid(cstate, &setattr->sa_stateid);
5667}
5668
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005669void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005670nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5671{
5672 get_stateid(cstate, &close->cl_stateid);
5673}
5674
5675void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005676nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005677{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005678 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005679}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01005680
5681void
5682nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5683{
5684 get_stateid(cstate, &read->rd_stateid);
5685}
5686
5687void
5688nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5689{
5690 get_stateid(cstate, &write->wr_stateid);
5691}