blob: 0a54fc95646346842a0c9e209fcc613c9b4bda57 [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 Halevycdc975052014-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 Halevycdc975052014-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 Halevycdc975052014-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 Layton12659652014-07-10 14:07:28 -0400380 if (access & NFS4_SHARE_ACCESS_WRITE)
381 atomic_inc(&fp->fi_access[O_WRONLY]);
382 if (access & NFS4_SHARE_ACCESS_READ)
383 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400384}
385
Jeff Layton12659652014-07-10 14:07:28 -0400386static __be32
387nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400388{
Jeff Layton12659652014-07-10 14:07:28 -0400389 /* Does this access mode make sense? */
390 if (access & ~NFS4_SHARE_ACCESS_BOTH)
391 return nfserr_inval;
392
393 __nfs4_file_get_access(fp, access);
394 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400395}
396
J. Bruce Fields998db522010-08-07 09:21:41 -0400397static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400398{
Trond Myklebustde186432014-07-10 14:07:26 -0400399 might_lock(&fp->fi_lock);
400
401 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
402 struct file *f1 = NULL;
403 struct file *f2 = NULL;
404
Jeff Layton6d338b52014-07-10 14:07:29 -0400405 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400406 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400407 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400408 spin_unlock(&fp->fi_lock);
409 if (f1)
410 fput(f1);
411 if (f2)
412 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400413 }
414}
415
Jeff Layton12659652014-07-10 14:07:28 -0400416static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400417{
Jeff Layton12659652014-07-10 14:07:28 -0400418 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
419
420 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400421 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400422 if (access & NFS4_SHARE_ACCESS_READ)
423 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400424}
425
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500426static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct
427kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400428{
429 struct idr *stateids = &cl->cl_stateids;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500430 struct nfs4_stid *stid;
431 int new_id;
432
433 stid = kmem_cache_alloc(slab, GFP_KERNEL);
434 if (!stid)
435 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400436
Jeff Layton398c33a2013-04-29 16:21:20 -0700437 new_id = idr_alloc_cyclic(stateids, stid, 0, 0, GFP_KERNEL);
Tejun Heoebd6c702013-03-13 14:59:37 -0700438 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500439 goto out_free;
440 stid->sc_client = cl;
441 stid->sc_type = 0;
442 stid->sc_stateid.si_opaque.so_id = new_id;
443 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
444 /* Will be incremented before return to client: */
445 stid->sc_stateid.si_generation = 0;
446
J. Bruce Fields996e0932011-10-17 11:14:48 -0400447 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500448 * It shouldn't be a problem to reuse an opaque stateid value.
449 * I don't think it is for 4.1. But with 4.0 I worry that, for
450 * example, a stray write retransmission could be accepted by
451 * the server when it should have been rejected. Therefore,
452 * adopt a trick from the sctp code to attempt to maximize the
453 * amount of time until an id is reused, by ensuring they always
454 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400455 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500456 return stid;
457out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800458 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500459 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400460}
461
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400462static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
463{
464 return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
465}
466
NeilBrown6282cd52014-06-04 17:39:26 +1000467/*
468 * When we recall a delegation, we should be careful not to hand it
469 * out again straight away.
470 * To ensure this we keep a pair of bloom filters ('new' and 'old')
471 * in which the filehandles of recalled delegations are "stored".
472 * If a filehandle appear in either filter, a delegation is blocked.
473 * When a delegation is recalled, the filehandle is stored in the "new"
474 * filter.
475 * Every 30 seconds we swap the filters and clear the "new" one,
476 * unless both are empty of course.
477 *
478 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
479 * low 3 bytes as hash-table indices.
480 *
481 * 'state_lock', which is always held when block_delegations() is called,
482 * is used to manage concurrent access. Testing does not need the lock
483 * except when swapping the two filters.
484 */
485static struct bloom_pair {
486 int entries, old_entries;
487 time_t swap_time;
488 int new; /* index into 'set' */
489 DECLARE_BITMAP(set[2], 256);
490} blocked_delegations;
491
492static int delegation_blocked(struct knfsd_fh *fh)
493{
494 u32 hash;
495 struct bloom_pair *bd = &blocked_delegations;
496
497 if (bd->entries == 0)
498 return 0;
499 if (seconds_since_boot() - bd->swap_time > 30) {
500 spin_lock(&state_lock);
501 if (seconds_since_boot() - bd->swap_time > 30) {
502 bd->entries -= bd->old_entries;
503 bd->old_entries = bd->entries;
504 memset(bd->set[bd->new], 0,
505 sizeof(bd->set[0]));
506 bd->new = 1-bd->new;
507 bd->swap_time = seconds_since_boot();
508 }
509 spin_unlock(&state_lock);
510 }
511 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
512 if (test_bit(hash&255, bd->set[0]) &&
513 test_bit((hash>>8)&255, bd->set[0]) &&
514 test_bit((hash>>16)&255, bd->set[0]))
515 return 1;
516
517 if (test_bit(hash&255, bd->set[1]) &&
518 test_bit((hash>>8)&255, bd->set[1]) &&
519 test_bit((hash>>16)&255, bd->set[1]))
520 return 1;
521
522 return 0;
523}
524
525static void block_delegations(struct knfsd_fh *fh)
526{
527 u32 hash;
528 struct bloom_pair *bd = &blocked_delegations;
529
530 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
531
532 __set_bit(hash&255, bd->set[bd->new]);
533 __set_bit((hash>>8)&255, bd->set[bd->new]);
534 __set_bit((hash>>16)&255, bd->set[bd->new]);
535 if (bd->entries == 0)
536 bd->swap_time = seconds_since_boot();
537 bd->entries += 1;
538}
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540static struct nfs4_delegation *
J. Bruce Fields99c41512013-05-21 16:21:25 -0400541alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 dprintk("NFSD alloc_init_deleg\n");
Meelap Shahc2f1a552007-07-17 04:04:39 -0700546 if (num_delegations > max_delegations)
NeilBrownef0f3392006-04-10 22:55:41 -0700547 return NULL;
NeilBrown6282cd52014-06-04 17:39:26 +1000548 if (delegation_blocked(&current_fh->fh_handle))
549 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400550 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700551 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return dp;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400553 /*
554 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400555 * meaning of seqid 0 isn't meaningful, really, but let's avoid
556 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400557 */
558 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownef0f3392006-04-10 22:55:41 -0700559 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700560 INIT_LIST_HEAD(&dp->dl_perfile);
561 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -0400563 dp->dl_file = NULL;
J. Bruce Fields99c41512013-05-21 16:21:25 -0400564 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
J. Bruce Fields6c02eaa2009-02-02 17:30:51 -0500565 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 dp->dl_time = 0;
567 atomic_set(&dp->dl_count, 1);
J. Bruce Fields57725152012-11-05 15:10:26 -0500568 nfsd4_init_callback(&dp->dl_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return dp;
570}
571
J. Bruce Fields68a33962013-03-21 11:21:50 -0400572static void remove_stid(struct nfs4_stid *s)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500573{
574 struct idr *stateids = &s->sc_client->cl_stateids;
575
576 idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500577}
578
Benny Halevy9857df82013-10-14 13:44:52 +0300579static void nfs4_free_stid(struct kmem_cache *slab, struct nfs4_stid *s)
580{
581 kmem_cache_free(slab, s);
582}
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584void
585nfs4_put_delegation(struct nfs4_delegation *dp)
586{
587 if (atomic_dec_and_test(&dp->dl_count)) {
Benny Halevy9857df82013-10-14 13:44:52 +0300588 nfs4_free_stid(deleg_slab, &dp->dl_stid);
NeilBrownef0f3392006-04-10 22:55:41 -0700589 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591}
592
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500593static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -0500595 if (!fp->fi_lease)
596 return;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500597 if (atomic_dec_and_test(&fp->fi_delegees)) {
598 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
599 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400600 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500601 fp->fi_deleg_file = NULL;
602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400605static void unhash_stid(struct nfs4_stid *s)
606{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500607 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400608}
609
Benny Halevy931ee562014-05-30 09:09:27 -0400610static void
611hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
612{
Benny Halevycdc975052014-05-30 09:09:30 -0400613 lockdep_assert_held(&state_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400614
Benny Halevy3fb87d12014-05-30 09:09:31 -0400615 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400616 list_add(&dp->dl_perfile, &fp->fi_delegations);
617 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
618}
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620/* Called under the state lock. */
621static void
622unhash_delegation(struct nfs4_delegation *dp)
623{
Benny Halevycdc975052014-05-30 09:09:30 -0400624 spin_lock(&state_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400625 list_del_init(&dp->dl_perclnt);
J. Bruce Fields5d926e82011-02-07 16:53:46 -0500626 list_del_init(&dp->dl_perfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 list_del_init(&dp->dl_recall_lru);
Benny Halevycdc975052014-05-30 09:09:30 -0400628 spin_unlock(&state_lock);
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -0500629 if (dp->dl_file) {
630 nfs4_put_deleg_lease(dp->dl_file);
631 put_nfs4_file(dp->dl_file);
632 dp->dl_file = NULL;
633 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400634}
635
636
637
638static void destroy_revoked_delegation(struct nfs4_delegation *dp)
639{
640 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields68a33962013-03-21 11:21:50 -0400641 remove_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 nfs4_put_delegation(dp);
643}
644
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400645static void destroy_delegation(struct nfs4_delegation *dp)
646{
647 unhash_delegation(dp);
648 remove_stid(&dp->dl_stid);
649 nfs4_put_delegation(dp);
650}
651
652static void revoke_delegation(struct nfs4_delegation *dp)
653{
654 struct nfs4_client *clp = dp->dl_stid.sc_client;
655
656 if (clp->cl_minorversion == 0)
657 destroy_delegation(dp);
658 else {
659 unhash_delegation(dp);
660 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
661 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
662 }
663}
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665/*
666 * SETCLIENTID state
667 */
668
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400669static unsigned int clientid_hashval(u32 id)
670{
671 return id & CLIENT_HASH_MASK;
672}
673
674static unsigned int clientstr_hashval(const char *name)
675{
676 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
677}
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400680 * We store the NONE, READ, WRITE, and BOTH bits separately in the
681 * st_{access,deny}_bmap field of the stateid, in order to track not
682 * only what share bits are currently in force, but also what
683 * combinations of share bits previous opens have used. This allows us
684 * to enforce the recommendation of rfc 3530 14.2.19 that the server
685 * return an error if the client attempt to downgrade to a combination
686 * of share bits not explicable by closing some of its previous opens.
687 *
688 * XXX: This enforcement is actually incomplete, since we don't keep
689 * track of access/deny bit combinations; so, e.g., we allow:
690 *
691 * OPEN allow read, deny write
692 * OPEN allow both, deny none
693 * DOWNGRADE allow read, deny none
694 *
695 * which we should reject.
696 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400697static unsigned int
698bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400699 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400700 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400701
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400702 for (i = 1; i < 4; i++) {
703 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400704 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400705 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400706 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400707}
708
Jeff Layton3a328612012-05-11 09:45:12 -0400709static bool
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400710test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400711 unsigned int access, deny;
712
Jeff Layton5ae037e2012-05-11 09:45:11 -0400713 access = bmap_to_share_mode(stp->st_access_bmap);
714 deny = bmap_to_share_mode(stp->st_deny_bmap);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400715 if ((access & open->op_share_deny) || (deny & open->op_share_access))
Jeff Layton3a328612012-05-11 09:45:12 -0400716 return false;
717 return true;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400718}
719
Jeff Layton82c5ff12012-05-11 09:45:13 -0400720/* set share access for a given stateid */
721static inline void
722set_access(u32 access, struct nfs4_ol_stateid *stp)
723{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400724 unsigned char mask = 1 << access;
725
726 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
727 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400728}
729
730/* clear share access for a given stateid */
731static inline void
732clear_access(u32 access, struct nfs4_ol_stateid *stp)
733{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400734 unsigned char mask = 1 << access;
735
736 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
737 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400738}
739
740/* test whether a given stateid has access */
741static inline bool
742test_access(u32 access, struct nfs4_ol_stateid *stp)
743{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400744 unsigned char mask = 1 << access;
745
746 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400747}
748
Jeff Laytonce0fc432012-05-11 09:45:14 -0400749/* set share deny for a given stateid */
750static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400751set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400752{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400753 unsigned char mask = 1 << deny;
754
755 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
756 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400757}
758
759/* clear share deny for a given stateid */
760static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400761clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400762{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400763 unsigned char mask = 1 << deny;
764
765 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
766 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400767}
768
769/* test whether a given stateid is denying specific access */
770static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400771test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400772{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400773 unsigned char mask = 1 << deny;
774
775 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400776}
777
778static int nfs4_access_to_omode(u32 access)
779{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400780 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400781 case NFS4_SHARE_ACCESS_READ:
782 return O_RDONLY;
783 case NFS4_SHARE_ACCESS_WRITE:
784 return O_WRONLY;
785 case NFS4_SHARE_ACCESS_BOTH:
786 return O_RDWR;
787 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500788 WARN_ON_ONCE(1);
789 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400790}
791
Jeff Layton82c5ff12012-05-11 09:45:13 -0400792/* release all access and file references for a given stateid */
793static void
794release_all_access(struct nfs4_ol_stateid *stp)
795{
796 int i;
797
798 for (i = 1; i < 4; i++) {
799 if (test_access(i, stp))
Jeff Layton12659652014-07-10 14:07:28 -0400800 nfs4_file_put_access(stp->st_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400801 clear_access(i, stp);
802 }
803}
804
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400805static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500806{
Trond Myklebust1d31a252014-07-10 14:07:25 -0400807 struct nfs4_file *fp = stp->st_file;
808
809 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500810 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400811 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500812 list_del(&stp->st_perstateowner);
813}
814
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400815static void close_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500816{
Jeff Layton82c5ff12012-05-11 09:45:13 -0400817 release_all_access(stp);
OGAWA Hirofumia96e5b92011-04-18 11:48:55 -0400818 put_nfs4_file(stp->st_file);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400819 stp->st_file = NULL;
820}
821
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400822static void free_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400823{
J. Bruce Fields68a33962013-03-21 11:21:50 -0400824 remove_stid(&stp->st_stid);
Benny Halevy9857df82013-10-14 13:44:52 +0300825 nfs4_free_stid(stateid_slab, &stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500826}
827
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400828static void __release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500829{
830 struct file *file;
831
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400832 list_del(&stp->st_locks);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500833 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400834 unhash_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500835 file = find_any_file(stp->st_file);
Trond Myklebuste20fcf12014-07-10 14:07:27 -0400836 if (file)
837 filp_close(file, (fl_owner_t)lockowner(stp->st_stateowner));
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400838 close_generic_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500839 free_generic_stateid(stp);
840}
841
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400842static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500843{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400844 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500845
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400846 list_del(&lo->lo_owner.so_strhash);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400847 while (!list_empty(&lo->lo_owner.so_stateids)) {
848 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400849 struct nfs4_ol_stateid, st_perstateowner);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400850 __release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500851 }
852}
853
Trond Myklebust50cc6232014-04-18 14:44:03 -0400854static void nfs4_free_lockowner(struct nfs4_lockowner *lo)
855{
856 kfree(lo->lo_owner.so_owner.data);
857 kmem_cache_free(lockowner_slab, lo);
858}
859
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400860static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500861{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400862 unhash_lockowner(lo);
863 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500864}
865
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400866static void release_lockowner_if_empty(struct nfs4_lockowner *lo)
867{
868 if (list_empty(&lo->lo_owner.so_stateids))
869 release_lockowner(lo);
870}
871
872static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500873{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400874 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500875
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400876 lo = lockowner(stp->st_stateowner);
877 __release_lock_stateid(stp);
878 release_lockowner_if_empty(lo);
879}
880
881static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
882{
883 struct nfs4_ol_stateid *stp;
884
885 while (!list_empty(&open_stp->st_locks)) {
886 stp = list_entry(open_stp->st_locks.next,
887 struct nfs4_ol_stateid, st_locks);
888 release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500889 }
890}
891
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400892static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500893{
894 unhash_generic_stateid(stp);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400895 release_open_stateid_locks(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400896 close_generic_stateid(stp);
897}
898
899static void release_open_stateid(struct nfs4_ol_stateid *stp)
900{
901 unhash_open_stateid(stp);
J. Bruce Fields22839632009-01-11 14:27:17 -0500902 free_generic_stateid(stp);
903}
904
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400905static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500906{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400907 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500908
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400909 list_del(&oo->oo_owner.so_strhash);
910 list_del(&oo->oo_perclient);
911 while (!list_empty(&oo->oo_owner.so_stateids)) {
912 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400913 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500914 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500915 }
916}
917
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400918static void release_last_closed_stateid(struct nfs4_openowner *oo)
919{
920 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
921
922 if (s) {
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400923 free_generic_stateid(s);
924 oo->oo_last_closed_stid = NULL;
925 }
926}
927
Trond Myklebust50cc6232014-04-18 14:44:03 -0400928static void nfs4_free_openowner(struct nfs4_openowner *oo)
929{
930 kfree(oo->oo_owner.so_owner.data);
931 kmem_cache_free(openowner_slab, oo);
932}
933
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400934static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500935{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400936 unhash_openowner(oo);
937 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400938 release_last_closed_stateid(oo);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400939 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500940}
941
Marc Eshel5282fd72009-04-03 08:27:52 +0300942static inline int
943hash_sessionid(struct nfs4_sessionid *sessionid)
944{
945 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
946
947 return sid->sequence % SESSION_HASH_SIZE;
948}
949
Trond Myklebust8f199b82012-03-20 15:11:17 -0400950#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +0300951static inline void
952dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
953{
954 u32 *ptr = (u32 *)(&sessionid->data[0]);
955 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
956}
Trond Myklebust8f199b82012-03-20 15:11:17 -0400957#else
958static inline void
959dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
960{
961}
962#endif
963
J. Bruce Fields9411b1d2013-04-01 16:37:12 -0400964/*
965 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
966 * won't be used for replay.
967 */
968void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
969{
970 struct nfs4_stateowner *so = cstate->replay_owner;
971
972 if (nfserr == nfserr_replay_me)
973 return;
974
975 if (!seqid_mutating_err(ntohl(nfserr))) {
976 cstate->replay_owner = NULL;
977 return;
978 }
979 if (!so)
980 return;
981 if (so->so_is_open_owner)
982 release_last_closed_stateid(openowner(so));
983 so->so_seqid++;
984 return;
985}
Marc Eshel5282fd72009-04-03 08:27:52 +0300986
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300987static void
988gen_sessionid(struct nfsd4_session *ses)
989{
990 struct nfs4_client *clp = ses->se_client;
991 struct nfsd4_sessionid *sid;
992
993 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
994 sid->clientid = clp->cl_clientid;
995 sid->sequence = current_sessionid++;
996 sid->reserved = 0;
997}
998
999/*
Andy Adamsona649637c72009-08-28 08:45:01 -04001000 * The protocol defines ca_maxresponssize_cached to include the size of
1001 * the rpc header, but all we need to cache is the data starting after
1002 * the end of the initial SEQUENCE operation--the rest we regenerate
1003 * each time. Therefore we can advertise a ca_maxresponssize_cached
1004 * value that is the number of bytes in our cache plus a few additional
1005 * bytes. In order to stay on the safe side, and not promise more than
1006 * we can cache, those additional bytes must be the minimum possible: 24
1007 * bytes of rpc header (xid through accept state, with AUTH_NULL
1008 * verifier), 12 for the compound header (with zero-length tag), and 44
1009 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001010 */
Andy Adamsona649637c72009-08-28 08:45:01 -04001011#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1012
Andy Adamson557ce262009-08-28 08:45:04 -04001013static void
1014free_session_slots(struct nfsd4_session *ses)
1015{
1016 int i;
1017
1018 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1019 kfree(ses->se_slots[i]);
1020}
1021
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001022/*
1023 * We don't actually need to cache the rpc and session headers, so we
1024 * can allocate a little less for each slot:
1025 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001026static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001027{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001028 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001029
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001030 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1031 size = 0;
1032 else
1033 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1034 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001035}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001036
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001037/*
1038 * XXX: If we run out of reserved DRC memory we could (up to a point)
1039 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001040 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001041 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001042static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001043{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001044 u32 slotsize = slot_bytes(ca);
1045 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001046 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001047
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001048 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001049 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1050 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001051 num = min_t(int, num, avail / slotsize);
1052 nfsd_drc_mem_used += num * slotsize;
1053 spin_unlock(&nfsd_drc_lock);
1054
1055 return num;
1056}
1057
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001058static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001059{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001060 int slotsize = slot_bytes(ca);
1061
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001062 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001063 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001064 spin_unlock(&nfsd_drc_lock);
1065}
1066
Kinglong Mee60810e52014-01-01 00:35:47 +08001067static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1068 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001069{
Kinglong Mee60810e52014-01-01 00:35:47 +08001070 int numslots = fattrs->maxreqs;
1071 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001072 struct nfsd4_session *new;
1073 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001074
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001075 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001076 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1077 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001078
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001079 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001080 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001081 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001082 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001083 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001084 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001085 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001086 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001087 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001088
1089 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1090 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1091
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001092 return new;
1093out_free:
1094 while (i--)
1095 kfree(new->se_slots[i]);
1096 kfree(new);
1097 return NULL;
1098}
1099
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001100static void free_conn(struct nfsd4_conn *c)
1101{
1102 svc_xprt_put(c->cn_xprt);
1103 kfree(c);
1104}
1105
1106static void nfsd4_conn_lost(struct svc_xpt_user *u)
1107{
1108 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1109 struct nfs4_client *clp = c->cn_session->se_client;
1110
1111 spin_lock(&clp->cl_lock);
1112 if (!list_empty(&c->cn_persession)) {
1113 list_del(&c->cn_persession);
1114 free_conn(c);
1115 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001116 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001117 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001118}
1119
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001120static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001121{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001122 struct nfsd4_conn *conn;
1123
1124 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1125 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001126 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001127 svc_xprt_get(rqstp->rq_xprt);
1128 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001129 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001130 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1131 return conn;
1132}
1133
J. Bruce Fields328ead22010-09-29 16:11:06 -04001134static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1135{
1136 conn->cn_session = ses;
1137 list_add(&conn->cn_persession, &ses->se_conns);
1138}
1139
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001140static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1141{
1142 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001143
1144 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001145 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001146 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001147}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001148
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001149static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001150{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001151 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001152 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001153}
1154
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001155static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001156{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001157 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001158
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001159 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001160 ret = nfsd4_register_conn(conn);
1161 if (ret)
1162 /* oops; xprt is already down: */
1163 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields6a3b1562012-09-12 09:59:17 -04001164 if (conn->cn_flags & NFS4_CDFC4_BACK) {
Weston Andros Adamson24119672012-05-24 15:42:17 -04001165 /* callback channel may be back up */
1166 nfsd4_probe_callback(ses->se_client);
1167 }
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001168}
1169
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001170static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001171{
1172 u32 dir = NFS4_CDFC4_FORE;
1173
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001174 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001175 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001176 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001177}
1178
1179/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001180static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001181{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001182 struct nfs4_client *clp = s->se_client;
1183 struct nfsd4_conn *c;
1184
1185 spin_lock(&clp->cl_lock);
1186 while (!list_empty(&s->se_conns)) {
1187 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1188 list_del_init(&c->cn_persession);
1189 spin_unlock(&clp->cl_lock);
1190
1191 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1192 free_conn(c);
1193
1194 spin_lock(&clp->cl_lock);
1195 }
1196 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001197}
1198
J. Bruce Fields1377b692012-09-11 21:42:40 -04001199static void __free_session(struct nfsd4_session *ses)
1200{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001201 free_session_slots(ses);
1202 kfree(ses);
1203}
1204
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001205static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001206{
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001207 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1208
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001209 lockdep_assert_held(&nn->client_lock);
1210 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001211 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001212 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001213}
Andy Adamson557ce262009-08-28 08:45:04 -04001214
Fengguang Wu135ae822012-11-10 07:20:25 -05001215static 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 -04001216{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001217 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001218 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001219
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001220 new->se_client = clp;
1221 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001222
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001223 INIT_LIST_HEAD(&new->se_conns);
1224
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001225 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001226 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001227 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001228 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001229 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001230 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001231 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001232 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001233 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001234 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001235 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001236 spin_unlock(&nn->client_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001237
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001238 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001239 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001240 /*
1241 * This is a little silly; with sessions there's no real
1242 * use for the callback address. Use the peer address
1243 * as a reasonable default for now, but consider fixing
1244 * the rpc client not to require an address in the
1245 * future:
1246 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001247 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1248 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001249 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001250}
1251
Benny Halevy9089f1b2010-05-12 00:12:26 +03001252/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001253static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001254__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001255{
1256 struct nfsd4_session *elem;
1257 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001258 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001259
1260 dump_sessionid(__func__, sessionid);
1261 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001262 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001263 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001264 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1265 NFS4_MAX_SESSIONID_LEN)) {
1266 return elem;
1267 }
1268 }
1269
1270 dprintk("%s: session not found\n", __func__);
1271 return NULL;
1272}
1273
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001274static struct nfsd4_session *
1275find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1276 __be32 *ret)
1277{
1278 struct nfsd4_session *session;
1279 __be32 status = nfserr_badsession;
1280
1281 session = __find_in_sessionid_hashtbl(sessionid, net);
1282 if (!session)
1283 goto out;
1284 status = nfsd4_get_session_locked(session);
1285 if (status)
1286 session = NULL;
1287out:
1288 *ret = status;
1289 return session;
1290}
1291
Benny Halevy9089f1b2010-05-12 00:12:26 +03001292/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001293static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001294unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001295{
1296 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001297 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001298 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001299 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001300}
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1303static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001304STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001306 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001308 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001309 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return 1;
1311}
1312
1313/*
1314 * XXX Should we use a slab cache ?
1315 * This type of memory management is somewhat inefficient, but we use it
1316 * anyway since SETCLIENTID is not a common operation.
1317 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001318static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 struct nfs4_client *clp;
1321
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001322 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1323 if (clp == NULL)
1324 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001325 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001326 if (clp->cl_name.data == NULL) {
1327 kfree(clp);
1328 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001330 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001331 INIT_LIST_HEAD(&clp->cl_sessions);
1332 idr_init(&clp->cl_stateids);
1333 atomic_set(&clp->cl_refcount, 0);
1334 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1335 INIT_LIST_HEAD(&clp->cl_idhash);
1336 INIT_LIST_HEAD(&clp->cl_openowners);
1337 INIT_LIST_HEAD(&clp->cl_delegations);
1338 INIT_LIST_HEAD(&clp->cl_lru);
1339 INIT_LIST_HEAD(&clp->cl_callbacks);
1340 INIT_LIST_HEAD(&clp->cl_revoked);
1341 spin_lock_init(&clp->cl_lock);
1342 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return clp;
1344}
1345
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001346static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347free_client(struct nfs4_client *clp)
1348{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001349 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001350
1351 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001352 while (!list_empty(&clp->cl_sessions)) {
1353 struct nfsd4_session *ses;
1354 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1355 se_perclnt);
1356 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001357 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1358 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001359 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001360 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001361 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001363 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 kfree(clp);
1365}
1366
Benny Halevy84d38ac2010-05-12 00:13:16 +03001367/* must be called under the client_lock */
1368static inline void
1369unhash_client_locked(struct nfs4_client *clp)
1370{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001371 struct nfsd4_session *ses;
1372
Benny Halevy84d38ac2010-05-12 00:13:16 +03001373 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001374 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001375 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1376 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001377 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001378}
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001381destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001383 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001386 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04001389 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001390 while (!list_empty(&clp->cl_delegations)) {
1391 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
NeilBrownea1da632005-06-23 22:04:17 -07001392 list_del_init(&dp->dl_perclnt);
Jeff Laytondff13992014-07-08 14:02:49 -04001393 /* Ensure that deleg break won't try to requeue it */
1394 ++dp->dl_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 list_move(&dp->dl_recall_lru, &reaplist);
1396 }
Benny Halevycdc975052014-05-30 09:09:30 -04001397 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 while (!list_empty(&reaplist)) {
1399 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001400 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
Benny Halevy956c4fe2013-10-29 11:39:12 +02001402 list_splice_init(&clp->cl_revoked, &reaplist);
1403 while (!list_empty(&reaplist)) {
1404 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1405 destroy_revoked_delegation(dp);
1406 }
NeilBrownea1da632005-06-23 22:04:17 -07001407 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001408 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1409 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001411 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001412 if (clp->cl_cb_conn.cb_xprt)
1413 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001414 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001415 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001416 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001417 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001418 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001419 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001420 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001421 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1422 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001423 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001426static void expire_client(struct nfs4_client *clp)
1427{
1428 nfsd4_client_record_remove(clp);
1429 destroy_client(clp);
1430}
1431
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001432static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1433{
1434 memcpy(target->cl_verifier.data, source->data,
1435 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
1437
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001438static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1439{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1441 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1442}
1443
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001444static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001445{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001446 if (source->cr_principal) {
1447 target->cr_principal =
1448 kstrdup(source->cr_principal, GFP_KERNEL);
1449 if (target->cr_principal == NULL)
1450 return -ENOMEM;
1451 } else
1452 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001453 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 target->cr_uid = source->cr_uid;
1455 target->cr_gid = source->cr_gid;
1456 target->cr_group_info = source->cr_group_info;
1457 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001458 target->cr_gss_mech = source->cr_gss_mech;
1459 if (source->cr_gss_mech)
1460 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001461 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001464static long long
1465compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1466{
1467 long long res;
1468
1469 res = o1->len - o2->len;
1470 if (res)
1471 return res;
1472 return (long long)memcmp(o1->data, o2->data, o1->len);
1473}
1474
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001475static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001476{
NeilBrowna55370a2005-06-23 22:03:52 -07001477 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
1480static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001481same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1482{
1483 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
1485
1486static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001487same_clid(clientid_t *cl1, clientid_t *cl2)
1488{
1489 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001492static bool groups_equal(struct group_info *g1, struct group_info *g2)
1493{
1494 int i;
1495
1496 if (g1->ngroups != g2->ngroups)
1497 return false;
1498 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001499 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001500 return false;
1501 return true;
1502}
1503
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001504/*
1505 * RFC 3530 language requires clid_inuse be returned when the
1506 * "principal" associated with a requests differs from that previously
1507 * used. We use uid, gid's, and gss principal string as our best
1508 * approximation. We also don't want to allow non-gss use of a client
1509 * established using gss: in theory cr_principal should catch that
1510 * change, but in practice cr_principal can be null even in the gss case
1511 * since gssd doesn't always pass down a principal string.
1512 */
1513static bool is_gss_cred(struct svc_cred *cr)
1514{
1515 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1516 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1517}
1518
1519
Vivek Trivedi5559b502012-07-24 21:18:20 +05301520static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001521same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1522{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001523 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001524 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1525 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001526 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1527 return false;
1528 if (cr1->cr_principal == cr2->cr_principal)
1529 return true;
1530 if (!cr1->cr_principal || !cr2->cr_principal)
1531 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301532 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
J. Bruce Fields57266a62013-04-13 14:27:29 -04001535static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1536{
1537 struct svc_cred *cr = &rqstp->rq_cred;
1538 u32 service;
1539
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001540 if (!cr->cr_gss_mech)
1541 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001542 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1543 return service == RPC_GSS_SVC_INTEGRITY ||
1544 service == RPC_GSS_SVC_PRIVACY;
1545}
1546
1547static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1548{
1549 struct svc_cred *cr = &rqstp->rq_cred;
1550
1551 if (!cl->cl_mach_cred)
1552 return true;
1553 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1554 return false;
1555 if (!svc_rqst_integrity_protected(rqstp))
1556 return false;
1557 if (!cr->cr_principal)
1558 return false;
1559 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1560}
1561
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001562static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001563{
1564 static u32 current_clientid = 1;
1565
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001566 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 clp->cl_clientid.cl_id = current_clientid++;
1568}
1569
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001570static void gen_confirm(struct nfs4_client *clp)
1571{
Chuck Leverab4684d2012-03-02 17:13:50 -05001572 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001573 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Jeff Laytonf4199922014-06-17 07:44:11 -04001575 /*
1576 * This is opaque to client, so no need to byte-swap. Use
1577 * __force to keep sparse happy
1578 */
1579 verf[0] = (__force __be32)get_seconds();
1580 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001581 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001584static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001585{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001586 struct nfs4_stid *ret;
1587
1588 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1589 if (!ret || !ret->sc_type)
1590 return NULL;
1591 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001592}
1593
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001594static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001595{
1596 struct nfs4_stid *s;
1597
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001598 s = find_stateid(cl, t);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001599 if (!s)
1600 return NULL;
1601 if (typemask & s->sc_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001602 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001603 return NULL;
1604}
1605
Jeff Layton2216d442012-11-12 15:00:57 -05001606static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001607 struct svc_rqst *rqstp, nfs4_verifier *verf)
1608{
1609 struct nfs4_client *clp;
1610 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001611 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001612 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001613 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001614
1615 clp = alloc_client(name);
1616 if (clp == NULL)
1617 return NULL;
1618
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001619 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1620 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001621 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001622 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001623 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001624 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001625 }
J. Bruce Fields57725152012-11-05 15:10:26 -05001626 nfsd4_init_callback(&clp->cl_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001627 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001628 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001629 copy_verf(clp, verf);
1630 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001631 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001632 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001633 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001634 return clp;
1635}
1636
NeilBrownfd39ca92005-06-23 22:04:03 -07001637static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001638add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1639{
1640 struct rb_node **new = &(root->rb_node), *parent = NULL;
1641 struct nfs4_client *clp;
1642
1643 while (*new) {
1644 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1645 parent = *new;
1646
1647 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1648 new = &((*new)->rb_left);
1649 else
1650 new = &((*new)->rb_right);
1651 }
1652
1653 rb_link_node(&new_clp->cl_namenode, parent, new);
1654 rb_insert_color(&new_clp->cl_namenode, root);
1655}
1656
1657static struct nfs4_client *
1658find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1659{
1660 long long cmp;
1661 struct rb_node *node = root->rb_node;
1662 struct nfs4_client *clp;
1663
1664 while (node) {
1665 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1666 cmp = compare_blob(&clp->cl_name, name);
1667 if (cmp > 0)
1668 node = node->rb_left;
1669 else if (cmp < 0)
1670 node = node->rb_right;
1671 else
1672 return clp;
1673 }
1674 return NULL;
1675}
1676
1677static void
1678add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
1680 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001681 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001683 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001684 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001686 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001687 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
1689
NeilBrownfd39ca92005-06-23 22:04:03 -07001690static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691move_to_confirmed(struct nfs4_client *clp)
1692{
1693 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001694 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001697 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001698 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001699 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001700 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 renew_client(clp);
1702}
1703
1704static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001705find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
1707 struct nfs4_client *clp;
1708 unsigned int idhashval = clientid_hashval(clid->cl_id);
1709
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001710 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001711 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001712 if ((bool)clp->cl_minorversion != sessions)
1713 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001714 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 }
1718 return NULL;
1719}
1720
1721static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001722find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1723{
1724 struct list_head *tbl = nn->conf_id_hashtbl;
1725
1726 return find_client_in_id_table(tbl, clid, sessions);
1727}
1728
1729static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001730find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001732 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001734 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735}
1736
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001737static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001738{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001739 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001740}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001741
NeilBrown28ce6052005-06-23 22:03:56 -07001742static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001743find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001744{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001745 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001746}
1747
1748static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001749find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001750{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001751 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001752}
1753
NeilBrownfd39ca92005-06-23 22:04:03 -07001754static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001755gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001757 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001758 struct sockaddr *sa = svc_addr(rqstp);
1759 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001760 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Jeff Layton7077ecb2009-08-14 12:57:58 -04001762 /* Currently, we only support tcp and tcp6 for the callback channel */
1763 if (se->se_callback_netid_len == 3 &&
1764 !memcmp(se->se_callback_netid_val, "tcp", 3))
1765 expected_family = AF_INET;
1766 else if (se->se_callback_netid_len == 4 &&
1767 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1768 expected_family = AF_INET6;
1769 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 goto out_err;
1771
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001772 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001773 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001774 (struct sockaddr *)&conn->cb_addr,
1775 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001776
J. Bruce Fields07263f12010-05-31 19:09:40 -04001777 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001779
J. Bruce Fields07263f12010-05-31 19:09:40 -04001780 if (conn->cb_addr.ss_family == AF_INET6)
1781 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001782
J. Bruce Fields07263f12010-05-31 19:09:40 -04001783 conn->cb_prog = se->se_callback_prog;
1784 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001785 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 return;
1787out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001788 conn->cb_addr.ss_family = AF_UNSPEC;
1789 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001790 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 "will not receive delegations\n",
1792 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return;
1795}
1796
Andy Adamson074fe892009-04-03 08:28:15 +03001797/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04001798 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001799 */
Trond Myklebustb6076642014-06-30 11:48:35 -04001800static void
Andy Adamson074fe892009-04-03 08:28:15 +03001801nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1802{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001803 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04001804 struct nfsd4_slot *slot = resp->cstate.slot;
1805 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001806
Andy Adamson557ce262009-08-28 08:45:04 -04001807 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001808
Andy Adamson557ce262009-08-28 08:45:04 -04001809 slot->sl_opcnt = resp->opcnt;
1810 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001811
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001812 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001813 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001814 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001815 return;
1816 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001817 base = resp->cstate.data_offset;
1818 slot->sl_datalen = buf->len - base;
1819 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04001820 WARN("%s: sessions DRC could not cache compound\n", __func__);
1821 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001822}
1823
1824/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001825 * Encode the replay sequence operation from the slot values.
1826 * If cachethis is FALSE encode the uncached rep error on the next
1827 * operation which sets resp->p and increments resp->opcnt for
1828 * nfs4svc_encode_compoundres.
1829 *
Andy Adamson074fe892009-04-03 08:28:15 +03001830 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001831static __be32
1832nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1833 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001834{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001835 struct nfsd4_op *op;
1836 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001837
Andy Adamsonabfabf82009-07-23 19:02:18 -04001838 /* Encode the replayed sequence operation */
1839 op = &args->ops[resp->opcnt - 1];
1840 nfsd4_encode_operation(resp, op);
1841
1842 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001843 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001844 op = &args->ops[resp->opcnt++];
1845 op->status = nfserr_retry_uncached_rep;
1846 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001847 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001848 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001849}
1850
1851/*
Andy Adamson557ce262009-08-28 08:45:04 -04001852 * The sequence operation is not cached because we can use the slot and
1853 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001854 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04001855static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001856nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1857 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001858{
Andy Adamson557ce262009-08-28 08:45:04 -04001859 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001860 struct xdr_stream *xdr = &resp->xdr;
1861 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03001862 __be32 status;
1863
Andy Adamson557ce262009-08-28 08:45:04 -04001864 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001865
Andy Adamsonabfabf82009-07-23 19:02:18 -04001866 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04001867 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04001868 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001869
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001870 p = xdr_reserve_space(xdr, slot->sl_datalen);
1871 if (!p) {
1872 WARN_ON_ONCE(1);
1873 return nfserr_serverfault;
1874 }
1875 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1876 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03001877
Andy Adamson557ce262009-08-28 08:45:04 -04001878 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001879 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001880}
1881
Andy Adamson0733d212009-04-03 08:28:01 +03001882/*
1883 * Set the exchange_id flags returned by the server.
1884 */
1885static void
1886nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1887{
1888 /* pNFS is not supported */
1889 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1890
1891 /* Referrals are supported, Migration is not. */
1892 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1893
1894 /* set the wire flags to return to client. */
1895 clid->flags = new->cl_exchange_flags;
1896}
1897
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001898static bool client_has_state(struct nfs4_client *clp)
1899{
1900 /*
1901 * Note clp->cl_openowners check isn't quite right: there's no
1902 * need to count owners without stateid's.
1903 *
1904 * Also note we should probably be using this in 4.0 case too.
1905 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04001906 return !list_empty(&clp->cl_openowners)
1907 || !list_empty(&clp->cl_delegations)
1908 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001909}
1910
Al Virob37ad282006-10-19 23:28:59 -07001911__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001912nfsd4_exchange_id(struct svc_rqst *rqstp,
1913 struct nfsd4_compound_state *cstate,
1914 struct nfsd4_exchange_id *exid)
1915{
Andy Adamson0733d212009-04-03 08:28:01 +03001916 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001917 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04001918 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001919 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001920 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001921 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001922 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03001923
Jeff Layton363168b2009-08-14 12:57:56 -04001924 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001925 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001926 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001927 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001928 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03001929
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001930 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03001931 return nfserr_inval;
1932
Andy Adamson0733d212009-04-03 08:28:01 +03001933 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04001934 case SP4_MACH_CRED:
1935 if (!svc_rqst_integrity_protected(rqstp))
1936 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03001937 case SP4_NONE:
1938 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001939 default: /* checked by xdr code */
1940 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03001941 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04001942 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03001943 }
1944
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001945 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03001946 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001947 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03001948 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001949 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
1950 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
1951
J. Bruce Fields136e6582012-05-12 20:37:23 -04001952 if (update) {
1953 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001954 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03001955 goto out;
1956 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04001957 if (!mach_creds_match(conf, rqstp)) {
1958 status = nfserr_wrong_cred;
1959 goto out;
1960 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001961 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03001962 status = nfserr_perm;
1963 goto out;
1964 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001965 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03001966 status = nfserr_not_same;
1967 goto out;
1968 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001969 /* case 6 */
1970 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1971 new = conf;
1972 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03001973 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001974 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001975 if (client_has_state(conf)) {
1976 status = nfserr_clid_inuse;
1977 goto out;
1978 }
Andy Adamson0733d212009-04-03 08:28:01 +03001979 expire_client(conf);
1980 goto out_new;
1981 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001982 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04001983 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04001984 new = conf;
1985 goto out_copy;
1986 }
1987 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04001988 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001989 }
1990
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001991 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001992 status = nfserr_noent;
1993 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001994 }
1995
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001996 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001997 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03001998 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03001999
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002000 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002001out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05002002 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03002003 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04002004 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03002005 goto out;
2006 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002007 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002008 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002009
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002010 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002011 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03002012out_copy:
2013 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2014 exid->clientid.cl_id = new->cl_clientid.cl_id;
2015
J. Bruce Fields778df3f2012-05-25 21:40:23 -04002016 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03002017 nfsd4_set_ex_flags(new, exid);
2018
2019 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04002020 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002021 status = nfs_ok;
2022
2023out:
2024 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03002025 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002026}
2027
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002028static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002029check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002030{
Andy Adamson88e588d2009-07-23 19:02:15 -04002031 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2032 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002033
2034 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002035 if (slot_inuse) {
2036 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002037 return nfserr_jukebox;
2038 else
2039 return nfserr_seq_misordered;
2040 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002041 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002042 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002043 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002044 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002045 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002046 return nfserr_seq_misordered;
2047}
2048
Andy Adamson49557cc2009-07-23 19:02:16 -04002049/*
2050 * Cache the create session result into the create session single DRC
2051 * slot cache by saving the xdr structure. sl_seqid has been set.
2052 * Do this for solo or embedded create session operations.
2053 */
2054static void
2055nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002056 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002057{
2058 slot->sl_status = nfserr;
2059 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2060}
2061
2062static __be32
2063nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2064 struct nfsd4_clid_slot *slot)
2065{
2066 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2067 return slot->sl_status;
2068}
2069
Mi Jinlong1b74c252011-07-14 14:50:17 +08002070#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2071 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2072 1 + /* MIN tag is length with zero, only length */ \
2073 3 + /* version, opcount, opcode */ \
2074 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2075 /* seqid, slotID, slotID, cache */ \
2076 4 ) * sizeof(__be32))
2077
2078#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2079 2 + /* verifier: AUTH_NULL, length 0 */\
2080 1 + /* status */ \
2081 1 + /* MIN tag is length with zero, only length */ \
2082 3 + /* opcount, opcode, opstatus*/ \
2083 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2084 /* seqid, slotID, slotID, slotID, status */ \
2085 5 ) * sizeof(__be32))
2086
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002087static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002088{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002089 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2090
J. Bruce Fields373cd402013-04-08 15:42:12 -04002091 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2092 return nfserr_toosmall;
2093 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2094 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002095 ca->headerpadsz = 0;
2096 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2097 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2098 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2099 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2100 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2101 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2102 /*
2103 * Note decreasing slot size below client's request may make it
2104 * difficult for client to function correctly, whereas
2105 * decreasing the number of slots will (just?) affect
2106 * performance. When short on memory we therefore prefer to
2107 * decrease number of slots instead of their size. Clients that
2108 * request larger slots than they need will get poor results:
2109 */
2110 ca->maxreqs = nfsd4_get_drc_mem(ca);
2111 if (!ca->maxreqs)
2112 return nfserr_jukebox;
2113
J. Bruce Fields373cd402013-04-08 15:42:12 -04002114 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002115}
2116
Kinglong Mee8a891632013-12-23 18:11:02 +08002117#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2118 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2119#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2120 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2121
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002122static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2123{
2124 ca->headerpadsz = 0;
2125
2126 /*
2127 * These RPC_MAX_HEADER macros are overkill, especially since we
2128 * don't even do gss on the backchannel yet. But this is still
2129 * less than 1k. Tighten up this estimate in the unlikely event
2130 * it turns out to be a problem for some client:
2131 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002132 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002133 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002134 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002135 return nfserr_toosmall;
2136 ca->maxresp_cached = 0;
2137 if (ca->maxops < 2)
2138 return nfserr_toosmall;
2139
2140 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002141}
2142
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002143static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2144{
2145 switch (cbs->flavor) {
2146 case RPC_AUTH_NULL:
2147 case RPC_AUTH_UNIX:
2148 return nfs_ok;
2149 default:
2150 /*
2151 * GSS case: the spec doesn't allow us to return this
2152 * error. But it also doesn't allow us not to support
2153 * GSS.
2154 * I'd rather this fail hard than return some error the
2155 * client might think it can already handle:
2156 */
2157 return nfserr_encr_alg_unsupp;
2158 }
2159}
2160
Andy Adamson069b6ad2009-04-03 08:27:58 +03002161__be32
2162nfsd4_create_session(struct svc_rqst *rqstp,
2163 struct nfsd4_compound_state *cstate,
2164 struct nfsd4_create_session *cr_ses)
2165{
Jeff Layton363168b2009-08-14 12:57:56 -04002166 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002167 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04002168 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002169 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002170 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002171 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002172 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002173
Mi Jinlonga62573d2011-03-23 17:57:07 +08002174 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2175 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002176 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2177 if (status)
2178 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002179 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002180 if (status)
2181 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002182 status = check_backchannel_attrs(&cr_ses->back_channel);
2183 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002184 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002185 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002186 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002187 if (!new)
2188 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002189 conn = alloc_conn_from_crses(rqstp, cr_ses);
2190 if (!conn)
2191 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002192
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002193 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002194 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002195 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002196 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002197
2198 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002199 status = nfserr_wrong_cred;
2200 if (!mach_creds_match(conf, rqstp))
2201 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002202 cs_slot = &conf->cl_cs_slot;
2203 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002204 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002205 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002206 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002207 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002208 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002209 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002210 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002211 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002212 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002213 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002214 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002215 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002216 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002217 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002218 status = nfserr_wrong_cred;
2219 if (!mach_creds_match(unconf, rqstp))
2220 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002221 cs_slot = &unconf->cl_cs_slot;
2222 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002223 if (status) {
2224 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002225 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002226 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002227 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002228 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002229 if (old) {
2230 status = mark_client_expired(old);
2231 if (status)
2232 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002233 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002234 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002235 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002236 conf = unconf;
2237 } else {
2238 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002239 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002240 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002241 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002242 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002243 * We do not support RDMA or persistent sessions
2244 */
2245 cr_ses->flags &= ~SESSION4_PERSIST;
2246 cr_ses->flags &= ~SESSION4_RDMA;
2247
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002248 init_session(rqstp, new, conf, cr_ses);
2249 nfsd4_init_conn(rqstp, conn, new);
2250
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04002251 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002252 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002253 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002254 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002255
Andy Adamson49557cc2009-07-23 19:02:16 -04002256 /* cache solo and embedded create sessions under the state lock */
2257 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002258 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002259 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002260out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08002261 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002262 free_conn(conn);
2263out_free_session:
2264 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002265out_release_drc_mem:
2266 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002267 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002268}
2269
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002270static __be32 nfsd4_map_bcts_dir(u32 *dir)
2271{
2272 switch (*dir) {
2273 case NFS4_CDFC4_FORE:
2274 case NFS4_CDFC4_BACK:
2275 return nfs_ok;
2276 case NFS4_CDFC4_FORE_OR_BOTH:
2277 case NFS4_CDFC4_BACK_OR_BOTH:
2278 *dir = NFS4_CDFC4_BOTH;
2279 return nfs_ok;
2280 };
2281 return nfserr_inval;
2282}
2283
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002284__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2285{
2286 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002287 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002288 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002289
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002290 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2291 if (status)
2292 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002293 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002294 session->se_cb_prog = bc->bc_cb_program;
2295 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002296 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002297
2298 nfsd4_probe_callback(session->se_client);
2299
2300 return nfs_ok;
2301}
2302
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002303__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2304 struct nfsd4_compound_state *cstate,
2305 struct nfsd4_bind_conn_to_session *bcts)
2306{
2307 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002308 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002309 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002310 struct net *net = SVC_NET(rqstp);
2311 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002312
2313 if (!nfsd4_last_compound_op(rqstp))
2314 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002315 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002316 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002317 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002318 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002319 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002320 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002321 status = nfserr_wrong_cred;
2322 if (!mach_creds_match(session->se_client, rqstp))
2323 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002324 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002325 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002326 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002327 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002328 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002329 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002330 goto out;
2331 nfsd4_init_conn(rqstp, conn, session);
2332 status = nfs_ok;
2333out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002334 nfsd4_put_session(session);
2335out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002336 nfs4_unlock_state();
2337 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002338}
2339
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002340static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2341{
2342 if (!session)
2343 return 0;
2344 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2345}
2346
Andy Adamson069b6ad2009-04-03 08:27:58 +03002347__be32
2348nfsd4_destroy_session(struct svc_rqst *r,
2349 struct nfsd4_compound_state *cstate,
2350 struct nfsd4_destroy_session *sessionid)
2351{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002352 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002353 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002354 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002355 struct net *net = SVC_NET(r);
2356 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002357
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002358 nfs4_lock_state();
2359 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002360 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002361 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002362 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002363 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002364 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002365 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002366 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002367 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002368 if (!ses)
2369 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002370 status = nfserr_wrong_cred;
2371 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002372 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002373 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002374 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002375 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002376 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002377 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002378
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002379 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002380
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002381 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002382 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002383out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002384 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002385out_client_lock:
2386 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002387out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002388 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002389 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002390}
2391
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002392static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002393{
2394 struct nfsd4_conn *c;
2395
2396 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002397 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002398 return c;
2399 }
2400 }
2401 return NULL;
2402}
2403
J. Bruce Fields57266a62013-04-13 14:27:29 -04002404static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002405{
2406 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002407 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002408 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002409 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002410
2411 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002412 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002413 if (c)
2414 goto out_free;
2415 status = nfserr_conn_not_bound_to_session;
2416 if (clp->cl_mach_cred)
2417 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002418 __nfsd4_hash_conn(new, ses);
2419 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002420 ret = nfsd4_register_conn(new);
2421 if (ret)
2422 /* oops; xprt is already down: */
2423 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002424 return nfs_ok;
2425out_free:
2426 spin_unlock(&clp->cl_lock);
2427 free_conn(new);
2428 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002429}
2430
Mi Jinlong868b89c2011-04-27 09:09:58 +08002431static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2432{
2433 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2434
2435 return args->opcnt > session->se_fchannel.maxops;
2436}
2437
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002438static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2439 struct nfsd4_session *session)
2440{
2441 struct xdr_buf *xb = &rqstp->rq_arg;
2442
2443 return xb->len > session->se_fchannel.maxreq_sz;
2444}
2445
Andy Adamson069b6ad2009-04-03 08:27:58 +03002446__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002447nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002448 struct nfsd4_compound_state *cstate,
2449 struct nfsd4_sequence *seq)
2450{
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002451 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002452 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002453 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002454 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002455 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002456 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002457 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002458 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002459 struct net *net = SVC_NET(rqstp);
2460 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002461
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002462 if (resp->opcnt != 1)
2463 return nfserr_sequence_pos;
2464
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002465 /*
2466 * Will be either used or freed by nfsd4_sequence_check_conn
2467 * below.
2468 */
2469 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2470 if (!conn)
2471 return nfserr_jukebox;
2472
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002473 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002474 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002475 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002476 goto out_no_session;
2477 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002478
Mi Jinlong868b89c2011-04-27 09:09:58 +08002479 status = nfserr_too_many_ops;
2480 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002481 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002482
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002483 status = nfserr_req_too_big;
2484 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002485 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002486
Benny Halevyb85d4c02009-04-03 08:28:08 +03002487 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002488 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002489 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002490
Andy Adamson557ce262009-08-28 08:45:04 -04002491 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002492 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2493
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002494 /* We do not negotiate the number of slots yet, so set the
2495 * maxslots to the session maxreqs which is used to encode
2496 * sr_highest_slotid and the sr_target_slot id to maxslots */
2497 seq->maxslots = session->se_fchannel.maxreqs;
2498
J. Bruce Fields73e79482012-02-13 16:39:00 -05002499 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2500 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002501 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002502 status = nfserr_seq_misordered;
2503 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002504 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002505 cstate->slot = slot;
2506 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002507 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002508 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002509 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002510 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002511 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002512 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002513 }
2514 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002515 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002516
J. Bruce Fields57266a62013-04-13 14:27:29 -04002517 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002518 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002519 if (status)
2520 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002521
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002522 buflen = (seq->cachethis) ?
2523 session->se_fchannel.maxresp_cached :
2524 session->se_fchannel.maxresp_sz;
2525 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2526 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002527 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002528 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002529 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002530
2531 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002532 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002533 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002534 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002535 if (seq->cachethis)
2536 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002537 else
2538 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002539
2540 cstate->slot = slot;
2541 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002542 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002543
Benny Halevyb85d4c02009-04-03 08:28:08 +03002544out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002545 switch (clp->cl_cb_state) {
2546 case NFSD4_CB_DOWN:
2547 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2548 break;
2549 case NFSD4_CB_FAULT:
2550 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2551 break;
2552 default:
2553 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002554 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002555 if (!list_empty(&clp->cl_revoked))
2556 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002557out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002558 if (conn)
2559 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002560 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002561 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002562out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002563 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002564 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002565}
2566
Trond Myklebustb6076642014-06-30 11:48:35 -04002567void
2568nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2569{
2570 struct nfsd4_compound_state *cs = &resp->cstate;
2571
2572 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002573 if (cs->status != nfserr_replay_cache) {
2574 nfsd4_store_cache_entry(resp);
2575 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2576 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002577 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002578 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002579 } else if (cs->clp)
2580 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002581}
2582
Mi Jinlong345c2842011-10-20 17:51:39 +08002583__be32
2584nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2585{
2586 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002587 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002588 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002589
2590 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002591 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002592 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002593 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002594
2595 if (conf) {
2596 clp = conf;
2597
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002598 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002599 status = nfserr_clientid_busy;
2600 goto out;
2601 }
2602 } else if (unconf)
2603 clp = unconf;
2604 else {
2605 status = nfserr_stale_clientid;
2606 goto out;
2607 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002608 if (!mach_creds_match(clp, rqstp)) {
2609 status = nfserr_wrong_cred;
2610 goto out;
2611 }
Mi Jinlong345c2842011-10-20 17:51:39 +08002612 expire_client(clp);
2613out:
2614 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002615 return status;
2616}
2617
Andy Adamson069b6ad2009-04-03 08:27:58 +03002618__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002619nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2620{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002621 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002622
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002623 if (rc->rca_one_fs) {
2624 if (!cstate->current_fh.fh_dentry)
2625 return nfserr_nofilehandle;
2626 /*
2627 * We don't take advantage of the rca_one_fs case.
2628 * That's OK, it's optional, we can safely ignore it.
2629 */
2630 return nfs_ok;
2631 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002632
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002633 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002634 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002635 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2636 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002637 goto out;
2638
2639 status = nfserr_stale_clientid;
2640 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002641 /*
2642 * The following error isn't really legal.
2643 * But we only get here if the client just explicitly
2644 * destroyed the client. Surely it no longer cares what
2645 * error it gets back on an operation for the dead
2646 * client.
2647 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002648 goto out;
2649
2650 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002651 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002652out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002653 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002654 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002655}
2656
2657__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002658nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2659 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002661 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002663 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002664 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002665 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2666
J. Bruce Fields63db4632012-05-18 21:54:19 -04002667 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002669 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002670 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002671 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002673 if (clp_used_exchangeid(conf))
2674 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002675 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002676 char addr_str[INET6_ADDRSTRLEN];
2677 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2678 sizeof(addr_str));
2679 dprintk("NFSD: setclientid: string in use by client "
2680 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 goto out;
2682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002684 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002685 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002688 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002689 if (new == NULL)
2690 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002691 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002692 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002694 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002695 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002696 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002697 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002698 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2700 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2701 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2702 status = nfs_ok;
2703out:
2704 nfs4_unlock_state();
2705 return status;
2706}
2707
2708
Al Virob37ad282006-10-19 23:28:59 -07002709__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002710nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2711 struct nfsd4_compound_state *cstate,
2712 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713{
NeilBrown21ab45a2005-06-23 22:04:14 -07002714 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2716 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002717 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002718 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002720 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002723
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002724 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002725 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002726 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002727 * We try hard to give out unique clientid's, so if we get an
2728 * attempt to confirm the same clientid with a different cred,
2729 * there's a bug somewhere. Let's charitably assume it's our
2730 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002731 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002732 status = nfserr_serverfault;
2733 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2734 goto out;
2735 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2736 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002737 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002738 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2739 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002741 else /* case 4: client hasn't noticed we rebooted yet? */
2742 status = nfserr_stale_clientid;
2743 goto out;
2744 }
2745 status = nfs_ok;
2746 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002747 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2748 nfsd4_probe_callback(conf);
2749 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002750 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002751 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002752 if (conf) {
2753 status = mark_client_expired(conf);
2754 if (status)
2755 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002756 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002757 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002758 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002759 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 nfs4_unlock_state();
2763 return status;
2764}
2765
J. Bruce Fields32513b42011-10-13 16:00:16 -04002766static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002768 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2769}
2770
2771/* OPEN Share state helper functions */
2772static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
2773{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 unsigned int hashval = file_hashval(ino);
2775
Trond Myklebust950e0112014-06-30 11:48:31 -04002776 lockdep_assert_held(&state_lock);
2777
J. Bruce Fields32513b42011-10-13 16:00:16 -04002778 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04002779 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002780 INIT_LIST_HEAD(&fp->fi_stateids);
2781 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebust950e0112014-06-30 11:48:31 -04002782 ihold(ino);
2783 fp->fi_inode = ino;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002784 fp->fi_had_conflict = false;
2785 fp->fi_lease = NULL;
2786 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2787 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04002788 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789}
2790
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002791void
NeilBrowne60d4392005-06-23 22:03:01 -07002792nfsd4_free_slabs(void)
2793{
Christoph Hellwigabf11352014-05-21 07:43:03 -07002794 kmem_cache_destroy(openowner_slab);
2795 kmem_cache_destroy(lockowner_slab);
2796 kmem_cache_destroy(file_slab);
2797 kmem_cache_destroy(stateid_slab);
2798 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002799}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
Bryan Schumaker72083392011-11-01 15:24:59 -04002801int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802nfsd4_init_slabs(void)
2803{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002804 openowner_slab = kmem_cache_create("nfsd4_openowners",
2805 sizeof(struct nfs4_openowner), 0, 0, NULL);
2806 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002807 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002808 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002809 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002810 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002811 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07002812 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002813 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002814 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002815 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07002816 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002817 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002818 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002819 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002820 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002821 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002822 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002823 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07002825
2826out_free_stateid_slab:
2827 kmem_cache_destroy(stateid_slab);
2828out_free_file_slab:
2829 kmem_cache_destroy(file_slab);
2830out_free_lockowner_slab:
2831 kmem_cache_destroy(lockowner_slab);
2832out_free_openowner_slab:
2833 kmem_cache_destroy(openowner_slab);
2834out:
NeilBrowne60d4392005-06-23 22:03:01 -07002835 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2836 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837}
2838
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002839static void init_nfs4_replay(struct nfs4_replay *rp)
2840{
2841 rp->rp_status = nfserr_serverfault;
2842 rp->rp_buflen = 0;
2843 rp->rp_buf = rp->rp_ibuf;
2844}
2845
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002846static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847{
2848 struct nfs4_stateowner *sop;
2849
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002850 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002851 if (!sop)
2852 return NULL;
2853
2854 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2855 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002856 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002857 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002859 sop->so_owner.len = owner->len;
2860
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002861 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002862 sop->so_client = clp;
2863 init_nfs4_replay(&sop->so_replay);
2864 return sop;
2865}
2866
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002867static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002868{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002869 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2870
2871 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002872 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873}
2874
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002875static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04002876alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002877 struct nfsd4_compound_state *cstate)
2878{
Trond Myklebust13d6f662014-06-30 11:48:45 -04002879 struct nfs4_client *clp = cstate->clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002880 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002882 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2883 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002885 oo->oo_owner.so_is_open_owner = 1;
2886 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04002887 oo->oo_flags = NFS4_OO_NEW;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002888 if (nfsd4_has_session(cstate))
2889 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002890 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002891 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002892 INIT_LIST_HEAD(&oo->oo_close_lru);
2893 hash_openowner(oo, clp, strhashval);
2894 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895}
2896
J. Bruce Fields996e0932011-10-17 11:14:48 -04002897static 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 -04002898 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002900 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04002901 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002902 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002903 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002904 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 stp->st_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 stp->st_access_bmap = 0;
2907 stp->st_deny_bmap = 0;
Jeff Layton82c5ff12012-05-11 09:45:13 -04002908 set_access(open->op_share_access, stp);
Jeff Laytonce0fc432012-05-11 09:45:14 -04002909 set_deny(open->op_share_deny, stp);
NeilBrown4c4cd222005-07-07 17:59:27 -07002910 stp->st_openstp = NULL;
Trond Myklebust1d31a252014-07-10 14:07:25 -04002911 spin_lock(&fp->fi_lock);
2912 list_add(&stp->st_perfile, &fp->fi_stateids);
2913 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914}
2915
2916static void
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002917move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002919 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2920
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002921 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002923 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002924 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925}
2926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002928same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2929 clientid_t *clid)
2930{
2931 return (sop->so_owner.len == owner->len) &&
2932 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2933 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934}
2935
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002936static struct nfs4_openowner *
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002937find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
2938 bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002940 struct nfs4_stateowner *so;
2941 struct nfs4_openowner *oo;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002942 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002944 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05002945 if (!so->so_is_open_owner)
2946 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002947 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
2948 oo = openowner(so);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002949 clp = oo->oo_owner.so_client;
2950 if ((bool)clp->cl_minorversion != sessions)
2951 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002952 renew_client(oo->oo_owner.so_client);
2953 return oo;
2954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 }
2956 return NULL;
2957}
2958
2959/* search file_hashtbl[] for file */
2960static struct nfs4_file *
Trond Myklebust950e0112014-06-30 11:48:31 -04002961find_file_locked(struct inode *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962{
2963 unsigned int hashval = file_hashval(ino);
2964 struct nfs4_file *fp;
2965
Trond Myklebust950e0112014-06-30 11:48:31 -04002966 lockdep_assert_held(&state_lock);
2967
Jeff Layton89876f82013-04-02 09:01:59 -04002968 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07002969 if (fp->fi_inode == ino) {
2970 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07002972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 }
2974 return NULL;
2975}
2976
Trond Myklebust950e0112014-06-30 11:48:31 -04002977static struct nfs4_file *
2978find_file(struct inode *ino)
2979{
2980 struct nfs4_file *fp;
2981
2982 spin_lock(&state_lock);
2983 fp = find_file_locked(ino);
2984 spin_unlock(&state_lock);
2985 return fp;
2986}
2987
2988static struct nfs4_file *
2989find_or_add_file(struct inode *ino, struct nfs4_file *new)
2990{
2991 struct nfs4_file *fp;
2992
2993 spin_lock(&state_lock);
2994 fp = find_file_locked(ino);
2995 if (fp == NULL) {
2996 nfsd4_init_file(new, ino);
2997 fp = new;
2998 }
2999 spin_unlock(&state_lock);
3000
3001 return fp;
3002}
3003
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003004/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 * Called to check deny when READ with all zero stateid or
3006 * WRITE with all zero or all one stateid
3007 */
Al Virob37ad282006-10-19 23:28:59 -07003008static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3010{
3011 struct inode *ino = current_fh->fh_dentry->d_inode;
3012 struct nfs4_file *fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003013 struct nfs4_ol_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07003014 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07003017 if (!fp)
3018 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07003019 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 /* Search for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003021 spin_lock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003022 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
Jeff Laytonce0fc432012-05-11 09:45:14 -04003023 if (test_deny(deny_type, stp) ||
3024 test_deny(NFS4_SHARE_DENY_BOTH, stp))
NeilBrown13cd2182005-06-23 22:03:10 -07003025 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 }
NeilBrown13cd2182005-06-23 22:03:10 -07003027 ret = nfs_ok;
3028out:
Trond Myklebust1d31a252014-07-10 14:07:25 -04003029 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003030 put_nfs4_file(fp);
3031 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032}
3033
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003034static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035{
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003036 struct nfs4_client *clp = dp->dl_stid.sc_client;
3037 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3038
Benny Halevycdc975052014-05-30 09:09:30 -04003039 lockdep_assert_held(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 /* We're assuming the state code never drops its reference
3041 * without first removing the lease. Since we're in this lease
3042 * callback (and since the lease code is serialized by the kernel
3043 * lock) we know the server hasn't removed the lease yet, we know
3044 * it's safe to take a reference: */
3045 atomic_inc(&dp->dl_count);
3046
Jeff Laytondff13992014-07-08 14:02:49 -04003047 /*
3048 * If the dl_time != 0, then we know that it has already been
3049 * queued for a lease break. Don't queue it again.
3050 */
3051 if (dp->dl_time == 0) {
3052 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3053 dp->dl_time = get_seconds();
3054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
NeilBrown6282cd52014-06-04 17:39:26 +10003056 block_delegations(&dp->dl_fh);
3057
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003058 nfsd4_cb_recall(dp);
3059}
3060
Jeff Layton1c8c6012013-06-21 08:58:15 -04003061/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003062static void nfsd_break_deleg_cb(struct file_lock *fl)
3063{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003064 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3065 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003066
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003067 if (!fp) {
3068 WARN(1, "(%p)->fl_owner NULL\n", fl);
3069 return;
3070 }
3071 if (fp->fi_had_conflict) {
3072 WARN(1, "duplicate break on %p\n", fp);
3073 return;
3074 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003075 /*
3076 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003077 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003078 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003079 */
3080 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
Benny Halevycdc975052014-05-30 09:09:30 -04003082 spin_lock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003083 fp->fi_had_conflict = true;
3084 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3085 nfsd_break_one_deleg(dp);
Benny Halevycdc975052014-05-30 09:09:30 -04003086 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087}
3088
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089static
3090int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3091{
3092 if (arg & F_UNLCK)
3093 return lease_modify(onlist, arg);
3094 else
3095 return -EAGAIN;
3096}
3097
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003098static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003099 .lm_break = nfsd_break_deleg_cb,
3100 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101};
3102
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003103static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3104{
3105 if (nfsd4_has_session(cstate))
3106 return nfs_ok;
3107 if (seqid == so->so_seqid - 1)
3108 return nfserr_replay_me;
3109 if (seqid == so->so_seqid)
3110 return nfs_ok;
3111 return nfserr_bad_seqid;
3112}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
Jeff Layton4b24ca72014-06-30 11:48:44 -04003114static __be32 lookup_clientid(clientid_t *clid,
3115 struct nfsd4_compound_state *cstate,
3116 struct nfsd_net *nn)
3117{
3118 struct nfs4_client *found;
3119
3120 if (cstate->clp) {
3121 found = cstate->clp;
3122 if (!same_clid(&found->cl_clientid, clid))
3123 return nfserr_stale_clientid;
3124 return nfs_ok;
3125 }
3126
3127 if (STALE_CLIENTID(clid, nn))
3128 return nfserr_stale_clientid;
3129
3130 /*
3131 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3132 * cached already then we know this is for is for v4.0 and "sessions"
3133 * will be false.
3134 */
3135 WARN_ON_ONCE(cstate->session);
3136 found = find_confirmed_client(clid, false, nn);
3137 if (!found)
3138 return nfserr_expired;
3139
3140 /* Cache the nfs4_client in cstate! */
3141 cstate->clp = found;
3142 atomic_inc(&found->cl_refcount);
3143 return nfs_ok;
3144}
3145
Al Virob37ad282006-10-19 23:28:59 -07003146__be32
Andy Adamson66689582009-04-03 08:28:45 +03003147nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003148 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 clientid_t *clientid = &open->op_clientid;
3151 struct nfs4_client *clp = NULL;
3152 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003153 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003154 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003156 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003158 /*
3159 * In case we need it later, after we've already created the
3160 * file and don't want to risk a further failure:
3161 */
3162 open->op_file = nfsd4_alloc_file();
3163 if (open->op_file == NULL)
3164 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Trond Myklebust2d91e892014-06-30 11:48:46 -04003166 status = lookup_clientid(clientid, cstate, nn);
3167 if (status)
3168 return status;
3169 clp = cstate->clp;
3170
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003171 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003172 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003173 open->op_openowner = oo;
3174 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003175 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003177 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003178 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003179 release_openowner(oo);
3180 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003181 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003182 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003183 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3184 if (status)
3185 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003186 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003187new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003188 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003189 if (oo == NULL)
3190 return nfserr_jukebox;
3191 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003192alloc_stateid:
3193 open->op_stp = nfs4_alloc_stateid(clp);
3194 if (!open->op_stp)
3195 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003196 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197}
3198
Al Virob37ad282006-10-19 23:28:59 -07003199static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003200nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3201{
3202 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3203 return nfserr_openmode;
3204 else
3205 return nfs_ok;
3206}
3207
Daniel Mackc47d8322011-05-16 16:38:14 +02003208static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003209{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003210 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3211}
3212
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003213static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003214{
3215 struct nfs4_stid *ret;
3216
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003217 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003218 if (!ret)
3219 return NULL;
3220 return delegstateid(ret);
3221}
3222
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003223static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3224{
3225 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3226 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3227}
3228
Al Virob37ad282006-10-19 23:28:59 -07003229static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003230nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003231 struct nfs4_delegation **dp)
3232{
3233 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003234 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07003235
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003236 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07003237 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003238 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003239 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07003240 status = nfs4_check_delegmode(*dp, flags);
3241 if (status)
3242 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003243out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003244 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003245 return nfs_ok;
3246 if (status)
3247 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003248 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003249 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003250}
3251
Al Virob37ad282006-10-19 23:28:59 -07003252static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003253nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003255 struct nfs4_ol_stateid *local;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003256 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
Trond Myklebust1d31a252014-07-10 14:07:25 -04003258 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003259 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 /* ignore lock owners */
3261 if (local->st_stateowner->so_is_open_owner == 0)
3262 continue;
3263 /* remember if we have seen this open owner */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003264 if (local->st_stateowner == &oo->oo_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 *stpp = local;
3266 /* check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003267 if (!test_share(local, open)) {
3268 spin_unlock(&fp->fi_lock);
J. Bruce Fields77eaae82011-09-02 12:08:20 -04003269 return nfserr_share_denied;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003272 spin_unlock(&fp->fi_lock);
J. Bruce Fields77eaae82011-09-02 12:08:20 -04003273 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274}
3275
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003276static inline int nfs4_access_to_access(u32 nfs4_access)
3277{
3278 int flags = 0;
3279
3280 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3281 flags |= NFSD_MAY_READ;
3282 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3283 flags |= NFSD_MAY_WRITE;
3284 return flags;
3285}
3286
Al Virob37ad282006-10-19 23:28:59 -07003287static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3289 struct nfsd4_open *open)
3290{
3291 struct iattr iattr = {
3292 .ia_valid = ATTR_SIZE,
3293 .ia_size = 0,
3294 };
3295 if (!open->op_truncate)
3296 return 0;
3297 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003298 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3300}
3301
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003302static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003303 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3304 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003305{
Trond Myklebustde186432014-07-10 14:07:26 -04003306 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003307 __be32 status;
3308 int oflag = nfs4_access_to_omode(open->op_share_access);
3309 int access = nfs4_access_to_access(open->op_share_access);
3310
Trond Myklebustde186432014-07-10 14:07:26 -04003311 spin_lock(&fp->fi_lock);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003312 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003313 spin_unlock(&fp->fi_lock);
3314 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003315 if (status)
3316 goto out;
Trond Myklebustde186432014-07-10 14:07:26 -04003317 spin_lock(&fp->fi_lock);
3318 if (!fp->fi_fds[oflag]) {
3319 fp->fi_fds[oflag] = filp;
3320 filp = NULL;
3321 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003322 }
Jeff Layton12659652014-07-10 14:07:28 -04003323 status = nfs4_file_get_access(fp, open->op_share_access);
Trond Myklebustde186432014-07-10 14:07:26 -04003324 spin_unlock(&fp->fi_lock);
3325 if (filp)
3326 fput(filp);
Jeff Layton12659652014-07-10 14:07:28 -04003327 if (status)
3328 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003329
3330 status = nfsd4_truncate(rqstp, cur_fh, open);
3331 if (status)
3332 goto out_put_access;
3333
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003334 /* Set access and deny bits in stateid */
3335 set_access(open->op_share_access, stp);
3336 set_deny(open->op_share_deny, stp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003337 return nfs_ok;
3338
3339out_put_access:
Jeff Layton12659652014-07-10 14:07:28 -04003340 nfs4_file_put_access(fp, open->op_share_access);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003341out:
3342 return status;
3343}
3344
Al Virob37ad282006-10-19 23:28:59 -07003345static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003346nfs4_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 -07003347{
Al Virob37ad282006-10-19 23:28:59 -07003348 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003350 if (!test_access(open->op_share_access, stp))
3351 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003352 else
3353 status = nfsd4_truncate(rqstp, cur_fh, open);
3354
3355 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 return nfs_ok;
3358}
3359
3360
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003362nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003364 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365}
3366
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003367/* Should we give out recallable state?: */
3368static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3369{
3370 if (clp->cl_cb_state == NFSD4_CB_UP)
3371 return true;
3372 /*
3373 * In the sessions case, since we don't have to establish a
3374 * separate connection for callbacks, we assume it's OK
3375 * until we hear otherwise:
3376 */
3377 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3378}
3379
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003380static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
3381{
3382 struct file_lock *fl;
3383
3384 fl = locks_alloc_lock();
3385 if (!fl)
3386 return NULL;
3387 locks_init_lock(fl);
3388 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003389 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003390 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3391 fl->fl_end = OFFSET_MAX;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003392 fl->fl_owner = (fl_owner_t)(dp->dl_file);
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003393 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003394 return fl;
3395}
3396
J. Bruce Fields99c41512013-05-21 16:21:25 -04003397static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003398{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003399 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003400 struct file_lock *fl;
3401 int status;
3402
J. Bruce Fields99c41512013-05-21 16:21:25 -04003403 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003404 if (!fl)
3405 return -ENOMEM;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003406 fl->fl_file = find_readable_file(fp);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003407 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003408 if (status)
3409 goto out_free;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003410 fp->fi_lease = fl;
Trond Myklebustde186432014-07-10 14:07:26 -04003411 fp->fi_deleg_file = fl->fl_file;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003412 atomic_set(&fp->fi_delegees, 1);
Benny Halevycdc975052014-05-30 09:09:30 -04003413 spin_lock(&state_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04003414 hash_delegation_locked(dp, fp);
Benny Halevycdc975052014-05-30 09:09:30 -04003415 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003416 return 0;
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003417out_free:
Trond Myklebustde186432014-07-10 14:07:26 -04003418 if (fl->fl_file)
3419 fput(fl->fl_file);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003420 locks_free_lock(fl);
3421 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003422}
3423
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003424static int nfs4_set_delegation(struct nfs4_delegation *dp, struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003425{
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003426 if (fp->fi_had_conflict)
3427 return -EAGAIN;
3428 get_nfs4_file(fp);
3429 dp->dl_file = fp;
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003430 if (!fp->fi_lease)
3431 return nfs4_setlease(dp);
Benny Halevycdc975052014-05-30 09:09:30 -04003432 spin_lock(&state_lock);
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003433 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003434 if (fp->fi_had_conflict) {
Benny Halevycdc975052014-05-30 09:09:30 -04003435 spin_unlock(&state_lock);
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003436 return -EAGAIN;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003437 }
Benny Halevy931ee562014-05-30 09:09:27 -04003438 hash_delegation_locked(dp, fp);
Benny Halevycdc975052014-05-30 09:09:30 -04003439 spin_unlock(&state_lock);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003440 return 0;
3441}
3442
Benny Halevy4aa89132012-02-21 14:16:44 -08003443static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3444{
3445 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3446 if (status == -EAGAIN)
3447 open->op_why_no_deleg = WND4_CONTENTION;
3448 else {
3449 open->op_why_no_deleg = WND4_RESOURCE;
3450 switch (open->op_deleg_want) {
3451 case NFS4_SHARE_WANT_READ_DELEG:
3452 case NFS4_SHARE_WANT_WRITE_DELEG:
3453 case NFS4_SHARE_WANT_ANY_DELEG:
3454 break;
3455 case NFS4_SHARE_WANT_CANCEL:
3456 open->op_why_no_deleg = WND4_CANCELLED;
3457 break;
3458 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003459 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003460 }
3461 }
3462}
3463
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464/*
3465 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003466 *
3467 * Note we don't support write delegations, and won't until the vfs has
3468 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 */
3470static void
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003471nfs4_open_delegation(struct net *net, struct svc_fh *fh,
3472 struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473{
3474 struct nfs4_delegation *dp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003475 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003476 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003477 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003479 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003480 open->op_recall = 0;
3481 switch (open->op_claim_type) {
3482 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003483 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003484 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003485 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3486 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003487 break;
3488 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003489 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003490 /*
3491 * Let's not give out any delegations till everyone's
3492 * had the chance to reclaim theirs....
3493 */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003494 if (locks_in_grace(net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003495 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003496 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003497 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003498 /*
3499 * Also, if the file was opened for write or
3500 * create, there's a good chance the client's
3501 * about to write to it, resulting in an
3502 * immediate recall (since we don't support
3503 * write delegations):
3504 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003505 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003506 goto out_no_deleg;
3507 if (open->op_create == NFS4_OPEN_CREATE)
3508 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003509 break;
3510 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003511 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003512 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003513 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003514 if (dp == NULL)
3515 goto out_no_deleg;
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003516 status = nfs4_set_delegation(dp, stp->st_file);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003517 if (status)
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003518 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003520 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003522 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003523 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003524 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003525 return;
3526out_free:
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003527 destroy_delegation(dp);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003528out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003529 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3530 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003531 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003532 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003533 open->op_recall = 1;
3534 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003535
3536 /* 4.1 client asking for a delegation? */
3537 if (open->op_deleg_want)
3538 nfsd4_open_deleg_none_ext(open, status);
3539 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540}
3541
Benny Halevye27f49c2012-02-21 14:16:54 -08003542static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3543 struct nfs4_delegation *dp)
3544{
3545 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3546 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3547 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3548 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3549 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3550 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3551 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3552 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3553 }
3554 /* Otherwise the client must be confused wanting a delegation
3555 * it already has, therefore we don't return
3556 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3557 */
3558}
3559
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560/*
3561 * called with nfs4_lock_state() held.
3562 */
Al Virob37ad282006-10-19 23:28:59 -07003563__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3565{
Andy Adamson66689582009-04-03 08:28:45 +03003566 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003567 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 struct nfs4_file *fp = NULL;
3569 struct inode *ino = current_fh->fh_dentry->d_inode;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003570 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003571 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003572 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 /*
3575 * Lookup file; if found, lookup stateid and check open request,
3576 * and check for delegations in the process of being recalled.
3577 * If not found, create the nfs4_file struct
3578 */
Trond Myklebust950e0112014-06-30 11:48:31 -04003579 fp = find_or_add_file(ino, open->op_file);
3580 if (fp != open->op_file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 if ((status = nfs4_check_open(fp, open, &stp)))
3582 goto out;
J. Bruce Fields41d22662013-03-21 15:49:47 -04003583 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003584 if (status)
3585 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003587 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003588 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003589 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003590 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003591 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 }
3593
3594 /*
3595 * OPEN the file, or upgrade an existing OPEN.
3596 * If truncate fails, the OPEN fails.
3597 */
3598 if (stp) {
3599 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003600 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 if (status)
3602 goto out;
3603 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003604 stp = open->op_stp;
3605 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003606 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003607 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3608 if (status) {
3609 release_open_stateid(stp);
3610 goto out;
3611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003613 update_stateid(&stp->st_stid.sc_stateid);
3614 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
Benny Halevyd24433c2012-02-16 20:57:17 +02003616 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02003617 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3618 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3619 open->op_why_no_deleg = WND4_NOT_WANTED;
3620 goto nodeleg;
3621 }
3622 }
3623
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 /*
3625 * Attempt to hand out a delegation. No error return, because the
3626 * OPEN succeeds even if we fail.
3627 */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003628 nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003629nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 status = nfs_ok;
3631
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003632 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003633 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003635 /* 4.1 client trying to upgrade/downgrade delegation? */
3636 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003637 open->op_deleg_want)
3638 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003639
NeilBrown13cd2182005-06-23 22:03:10 -07003640 if (fp)
3641 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003642 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003643 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 /*
3645 * To finish the open response, we just need to set the rflags.
3646 */
3647 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003648 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003649 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3651
3652 return status;
3653}
3654
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003655void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3656{
3657 if (open->op_openowner) {
3658 struct nfs4_openowner *oo = open->op_openowner;
3659
3660 if (!list_empty(&oo->oo_owner.so_stateids))
3661 list_del_init(&oo->oo_close_lru);
3662 if (oo->oo_flags & NFS4_OO_NEW) {
3663 if (status) {
3664 release_openowner(oo);
3665 open->op_openowner = NULL;
3666 } else
3667 oo->oo_flags &= ~NFS4_OO_NEW;
3668 }
3669 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003670 if (open->op_file)
3671 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003672 if (open->op_stp)
J. Bruce Fieldsef798592012-08-29 10:32:54 -07003673 free_generic_stateid(open->op_stp);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003674}
3675
Al Virob37ad282006-10-19 23:28:59 -07003676__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003677nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3678 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679{
3680 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003681 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003682 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
3684 nfs4_lock_state();
3685 dprintk("process_renew(%08x/%08x): starting\n",
3686 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003687 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003688 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003690 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003692 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003693 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 goto out;
3695 status = nfs_ok;
3696out:
3697 nfs4_unlock_state();
3698 return status;
3699}
3700
NeilBrowna76b4312005-06-23 22:04:01 -07003701static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003702nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07003703{
Jeff Layton33dcc482012-04-10 11:08:48 -04003704 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003705 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003706 return;
3707
NeilBrowna76b4312005-06-23 22:04:01 -07003708 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003709 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003710 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003711 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003712 /*
3713 * Now that every NFSv4 client has had the chance to recover and
3714 * to see the (possibly new, possibly shorter) lease time, we
3715 * can safely set the next grace time to the current lease time:
3716 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03003717 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003718}
3719
NeilBrownfd39ca92005-06-23 22:04:03 -07003720static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003721nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722{
3723 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003724 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 struct nfs4_delegation *dp;
3726 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003727 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003728 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
3730 nfs4_lock_state();
3731
3732 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003733 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03003734 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003735 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03003736 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 clp = list_entry(pos, struct nfs4_client, cl_lru);
3738 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3739 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003740 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 break;
3742 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003743 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03003744 dprintk("NFSD: client in use (clientid %08x)\n",
3745 clp->cl_clientid.cl_id);
3746 continue;
3747 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003748 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003749 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003750 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03003751 list_for_each_safe(pos, next, &reaplist) {
3752 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 dprintk("NFSD: purging unused client (clientid %08x)\n",
3754 clp->cl_clientid.cl_id);
3755 expire_client(clp);
3756 }
Benny Halevycdc975052014-05-30 09:09:30 -04003757 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003758 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03003760 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3761 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003763 t = dp->dl_time - cutoff;
3764 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 break;
3766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 list_move(&dp->dl_recall_lru, &reaplist);
3768 }
Benny Halevycdc975052014-05-30 09:09:30 -04003769 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 list_for_each_safe(pos, next, &reaplist) {
3771 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003772 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003774 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003775 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3776 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003777 t = oo->oo_time - cutoff;
3778 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 break;
3780 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003781 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04003783 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04003785 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786}
3787
Harvey Harrisona254b242008-02-20 12:49:00 -08003788static struct workqueue_struct *laundry_wq;
3789static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08003790
3791static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003792laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793{
3794 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003795 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3796 work);
3797 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3798 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003800 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003802 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803}
3804
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003805static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003806{
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003807 if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
3808 return nfserr_bad_stateid;
3809 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810}
3811
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003813access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003815 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3816 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3817 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818}
3819
3820static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003821access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003823 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3824 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825}
3826
3827static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003828__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829{
Al Virob37ad282006-10-19 23:28:59 -07003830 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831
J. Bruce Fields02921912010-07-29 15:16:59 -04003832 /* For lock stateid's, we test the parent open, not the lock: */
3833 if (stp->st_openstp)
3834 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003835 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003837 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 goto out;
3839 status = nfs_ok;
3840out:
3841 return status;
3842}
3843
Al Virob37ad282006-10-19 23:28:59 -07003844static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003845check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003847 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003849 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003850 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 * conflicting state; so we must wait out the grace period. */
3852 return nfserr_grace;
3853 } else if (flags & WR_STATE)
3854 return nfs4_share_conflict(current_fh,
3855 NFS4_SHARE_DENY_WRITE);
3856 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3857 return nfs4_share_conflict(current_fh,
3858 NFS4_SHARE_DENY_READ);
3859}
3860
3861/*
3862 * Allow READ/WRITE during grace period on recovered state only for files
3863 * that are not able to provide mandatory locking.
3864 */
3865static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003866grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003868 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869}
3870
J. Bruce Fields81b82962011-08-23 11:03:29 -04003871/* Returns true iff a is later than b: */
3872static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3873{
Jim Rees1a9357f2013-05-17 17:33:00 -04003874 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04003875}
3876
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003877static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05003878{
Andy Adamson66689582009-04-03 08:28:45 +03003879 /*
3880 * When sessions are used the stateid generation number is ignored
3881 * when it is zero.
3882 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04003883 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04003884 return nfs_ok;
3885
3886 if (in->si_generation == ref->si_generation)
3887 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03003888
J. Bruce Fields0836f582008-01-26 19:08:12 -05003889 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003890 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05003891 return nfserr_bad_stateid;
3892 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04003893 * However, we could see a stateid from the past, even from a
3894 * non-buggy client. For example, if the client sends a lock
3895 * while some IO is outstanding, the lock may bump si_generation
3896 * while the IO is still in flight. The client could avoid that
3897 * situation by waiting for responses on all the IO requests,
3898 * but better performance may result in retrying IO that
3899 * receives an old_stateid error if requests are rarely
3900 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05003901 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003902 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05003903}
3904
Chuck Lever7df302f2012-05-29 13:56:37 -04003905static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04003906{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003907 struct nfs4_stid *s;
3908 struct nfs4_ol_stateid *ols;
3909 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003910
Chuck Lever7df302f2012-05-29 13:56:37 -04003911 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3912 return nfserr_bad_stateid;
3913 /* Client debugging aid. */
3914 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
3915 char addr_str[INET6_ADDRSTRLEN];
3916 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
3917 sizeof(addr_str));
3918 pr_warn_ratelimited("NFSD: client %s testing state ID "
3919 "with incorrect client ID\n", addr_str);
3920 return nfserr_bad_stateid;
3921 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003922 s = find_stateid(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003923 if (!s)
Chuck Lever7df302f2012-05-29 13:56:37 -04003924 return nfserr_bad_stateid;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04003925 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04003926 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003927 return status;
J. Bruce Fields23340032013-04-09 17:42:28 -04003928 switch (s->sc_type) {
3929 case NFS4_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003930 return nfs_ok;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003931 case NFS4_REVOKED_DELEG_STID:
3932 return nfserr_deleg_revoked;
J. Bruce Fields23340032013-04-09 17:42:28 -04003933 case NFS4_OPEN_STID:
3934 case NFS4_LOCK_STID:
3935 ols = openlockstateid(s);
3936 if (ols->st_stateowner->so_is_open_owner
3937 && !(openowner(ols->st_stateowner)->oo_flags
3938 & NFS4_OO_CONFIRMED))
3939 return nfserr_bad_stateid;
3940 return nfs_ok;
3941 default:
3942 printk("unknown stateid type %x\n", s->sc_type);
3943 case NFS4_CLOSED_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003944 return nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04003945 }
Bryan Schumaker17456802011-07-13 10:50:48 -04003946}
3947
Trond Myklebust2dd6e452014-06-30 11:48:43 -04003948static __be32
3949nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
3950 stateid_t *stateid, unsigned char typemask,
3951 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003952{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04003953 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003954
3955 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3956 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003957 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04003958 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04003959 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04003960 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003961 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04003962 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04003963 if (status)
3964 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003965 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003966 if (!*s)
3967 return nfserr_bad_stateid;
3968 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003969}
3970
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971/*
3972* Checks for stateid operations
3973*/
Al Virob37ad282006-10-19 23:28:59 -07003974__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003975nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03003976 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977{
J. Bruce Fields69064a22011-09-09 11:54:57 -04003978 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003979 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03003981 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003983 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04003984 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003985 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 if (filpp)
3988 *filpp = NULL;
3989
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003990 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 return nfserr_grace;
3992
3993 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003994 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
Trond Myklebust14bcab12014-04-18 14:44:07 -04003996 nfs4_lock_state();
3997
Trond Myklebust2dd6e452014-06-30 11:48:43 -04003998 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003999 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004000 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004001 if (status)
Trond Myklebust14bcab12014-04-18 14:44:07 -04004002 goto out;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004003 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4004 if (status)
4005 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004006 switch (s->sc_type) {
4007 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004008 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004009 status = nfs4_check_delegmode(dp, flags);
4010 if (status)
4011 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004012 if (filpp) {
Trond Myklebust14bcab12014-04-18 14:44:07 -04004013 file = dp->dl_file->fi_deleg_file;
4014 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004015 WARN_ON_ONCE(1);
4016 status = nfserr_serverfault;
4017 goto out;
4018 }
Trond Myklebustde186432014-07-10 14:07:26 -04004019 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004020 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004021 break;
4022 case NFS4_OPEN_STID:
4023 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004024 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004025 status = nfs4_check_fh(current_fh, stp);
4026 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004028 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004029 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004031 status = nfs4_check_openmode(stp, flags);
4032 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004034 if (filpp) {
4035 if (flags & RD_STATE)
Trond Myklebust14bcab12014-04-18 14:44:07 -04004036 file = find_readable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004037 else
Trond Myklebust14bcab12014-04-18 14:44:07 -04004038 file = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004039 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004040 break;
4041 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004042 status = nfserr_bad_stateid;
4043 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 }
4045 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004046 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004047 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048out:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004049 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 return status;
4051}
4052
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004053static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004054nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004055{
J. Bruce Fieldsa1b8ff42014-05-20 15:55:21 -04004056 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4057
4058 if (check_for_locks(stp->st_file, lo))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004059 return nfserr_locks_held;
Jeff Laytonc53530d2014-06-30 11:48:39 -04004060 release_lockowner_if_empty(lo);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004061 return nfs_ok;
4062}
4063
4064/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004065 * Test if the stateid is valid
4066 */
4067__be32
4068nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4069 struct nfsd4_test_stateid *test_stateid)
4070{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004071 struct nfsd4_test_stateid_id *stateid;
4072 struct nfs4_client *cl = cstate->session->se_client;
4073
4074 nfs4_lock_state();
4075 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004076 stateid->ts_id_status =
4077 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004078 nfs4_unlock_state();
4079
Bryan Schumaker17456802011-07-13 10:50:48 -04004080 return nfs_ok;
4081}
4082
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004083__be32
4084nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4085 struct nfsd4_free_stateid *free_stateid)
4086{
4087 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004088 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004089 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004090 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004091 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004092
4093 nfs4_lock_state();
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004094 s = find_stateid(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004095 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004096 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004097 switch (s->sc_type) {
4098 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004099 ret = nfserr_locks_held;
4100 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004101 case NFS4_OPEN_STID:
4102 case NFS4_LOCK_STID:
4103 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4104 if (ret)
4105 goto out;
4106 if (s->sc_type == NFS4_LOCK_STID)
4107 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4108 else
4109 ret = nfserr_locks_held;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004110 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004111 case NFS4_REVOKED_DELEG_STID:
4112 dp = delegstateid(s);
4113 destroy_revoked_delegation(dp);
4114 ret = nfs_ok;
4115 break;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004116 default:
4117 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004118 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004119out:
4120 nfs4_unlock_state();
4121 return ret;
4122}
4123
NeilBrown4c4cd222005-07-07 17:59:27 -07004124static inline int
4125setlkflg (int type)
4126{
4127 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4128 RD_STATE : WR_STATE;
4129}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004131static __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 -04004132{
4133 struct svc_fh *current_fh = &cstate->current_fh;
4134 struct nfs4_stateowner *sop = stp->st_stateowner;
4135 __be32 status;
4136
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004137 status = nfsd4_check_seqid(cstate, sop, seqid);
4138 if (status)
4139 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004140 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4141 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004142 /*
4143 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004144 * nfserr_replay_me from the previous step, and
4145 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004146 */
4147 return nfserr_bad_stateid;
4148 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4149 if (status)
4150 return status;
4151 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004152}
4153
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154/*
4155 * Checks for sequence id mutating operations.
4156 */
Al Virob37ad282006-10-19 23:28:59 -07004157static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004158nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004159 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004160 struct nfs4_ol_stateid **stpp,
4161 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004163 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004164 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004165 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004167 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4168 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004169
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004171 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004172 if (status)
4173 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004174 stp = openlockstateid(s);
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04004175 if (!nfsd4_has_session(cstate))
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004176 cstate->replay_owner = stp->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004178 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4179 if (!status)
4180 *stpp = stp;
4181 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004182}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004183
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004184static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4185 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004186{
4187 __be32 status;
4188 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004190 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004191 NFS4_OPEN_STID, stpp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004192 if (status)
4193 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004194 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004195 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07004196 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004197 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198}
4199
Al Virob37ad282006-10-19 23:28:59 -07004200__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004201nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004202 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203{
Al Virob37ad282006-10-19 23:28:59 -07004204 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004205 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004206 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004207 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208
Al Viroa6a9f182013-09-16 10:57:01 -04004209 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4210 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004212 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004213 if (status)
4214 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
4216 nfs4_lock_state();
4217
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004218 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004219 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004220 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004221 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004222 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004223 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004224 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004225 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004226 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004227 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004228 update_stateid(&stp->st_stid.sc_stateid);
4229 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004230 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004231 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004232
Jeff Layton2a4317c2012-03-21 16:42:43 -04004233 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004234 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004236 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004237 if (!cstate->replay_owner)
4238 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 return status;
4240}
4241
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004242static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004244 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004245 return;
Jeff Layton12659652014-07-10 14:07:28 -04004246 nfs4_file_put_access(stp->st_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004247 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004248}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004249
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004250static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4251{
4252 switch (to_access) {
4253 case NFS4_SHARE_ACCESS_READ:
4254 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4255 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4256 break;
4257 case NFS4_SHARE_ACCESS_WRITE:
4258 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4259 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4260 break;
4261 case NFS4_SHARE_ACCESS_BOTH:
4262 break;
4263 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004264 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 }
4266}
4267
4268static void
Jeff Laytonce0fc432012-05-11 09:45:14 -04004269reset_union_bmap_deny(unsigned long deny, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270{
4271 int i;
4272 for (i = 0; i < 4; i++) {
4273 if ((i & deny) != i)
Jeff Laytonce0fc432012-05-11 09:45:14 -04004274 clear_deny(i, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 }
4276}
4277
Al Virob37ad282006-10-19 23:28:59 -07004278__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004279nfsd4_open_downgrade(struct svc_rqst *rqstp,
4280 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004281 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282{
Al Virob37ad282006-10-19 23:28:59 -07004283 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004284 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004285 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
Al Viroa6a9f182013-09-16 10:57:01 -04004287 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4288 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004290 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004291 if (od->od_deleg_want)
4292 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4293 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
4295 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004296 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004297 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004298 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004301 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004302 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 stp->st_access_bmap, od->od_share_access);
4304 goto out;
4305 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004306 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004307 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 stp->st_deny_bmap, od->od_share_deny);
4309 goto out;
4310 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004311 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312
Jeff Laytonce0fc432012-05-11 09:45:14 -04004313 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004315 update_stateid(&stp->st_stid.sc_stateid);
4316 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 status = nfs_ok;
4318out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004319 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004320 if (!cstate->replay_owner)
4321 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 return status;
4323}
4324
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004325static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4326{
Trond Myklebustacf92952014-06-30 11:48:37 -04004327 struct nfs4_client *clp = s->st_stid.sc_client;
4328 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4329
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004330 s->st_stid.sc_type = NFS4_CLOSED_STID;
Trond Myklebustacf92952014-06-30 11:48:37 -04004331 unhash_open_stateid(s);
4332
4333 if (clp->cl_minorversion) {
4334 free_generic_stateid(s);
4335 if (list_empty(&oo->oo_owner.so_stateids))
4336 release_openowner(oo);
4337 } else {
4338 oo->oo_last_closed_stid = s;
4339 /*
4340 * In the 4.0 case we need to keep the owners around a
4341 * little while to handle CLOSE replay.
4342 */
4343 if (list_empty(&oo->oo_owner.so_stateids))
4344 move_to_close_lru(oo, clp->net);
4345 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004346}
4347
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348/*
4349 * nfs4_unlock_state() called after encode
4350 */
Al Virob37ad282006-10-19 23:28:59 -07004351__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004352nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004353 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354{
Al Virob37ad282006-10-19 23:28:59 -07004355 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004356 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004357 struct net *net = SVC_NET(rqstp);
4358 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
Al Viroa6a9f182013-09-16 10:57:01 -04004360 dprintk("NFSD: nfsd4_close on file %pd\n",
4361 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362
4363 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004364 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4365 &close->cl_stateid,
4366 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004367 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004368 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004369 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004371 update_stateid(&stp->st_stid.sc_stateid);
4372 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004374 nfsd4_close_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004376 if (!cstate->replay_owner)
4377 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 return status;
4379}
4380
Al Virob37ad282006-10-19 23:28:59 -07004381__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004382nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4383 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004385 struct nfs4_delegation *dp;
4386 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004387 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004388 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004389 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004391 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004392 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
4394 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004395 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004396 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004397 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004398 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004399 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004400 if (status)
4401 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004402
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004403 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004405 nfs4_unlock_state();
4406
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 return status;
4408}
4409
4410
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412
Benny Halevy87df4de2008-12-15 19:42:03 +02004413static inline u64
4414end_offset(u64 start, u64 len)
4415{
4416 u64 end;
4417
4418 end = start + len;
4419 return end >= start ? end: NFS4_MAX_UINT64;
4420}
4421
4422/* last octet in a range */
4423static inline u64
4424last_byte_offset(u64 start, u64 len)
4425{
4426 u64 end;
4427
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004428 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004429 end = start + len;
4430 return end > start ? end - 1: NFS4_MAX_UINT64;
4431}
4432
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433/*
4434 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4435 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4436 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4437 * locking, this prevents us from being completely protocol-compliant. The
4438 * real solution to this problem is to start using unsigned file offsets in
4439 * the VFS, but this is a very deep change!
4440 */
4441static inline void
4442nfs4_transform_lock_offset(struct file_lock *lock)
4443{
4444 if (lock->fl_start < 0)
4445 lock->fl_start = OFFSET_MAX;
4446 if (lock->fl_end < 0)
4447 lock->fl_end = OFFSET_MAX;
4448}
4449
NeilBrownd5b90262006-04-10 22:55:22 -07004450/* Hack!: For now, we're defining this just so we can use a pointer to it
4451 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004452static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004453};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454
4455static inline void
4456nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4457{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004458 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459
NeilBrownd5b90262006-04-10 22:55:22 -07004460 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004461 lo = (struct nfs4_lockowner *) fl->fl_owner;
4462 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4463 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004464 if (!deny->ld_owner.data)
4465 /* We just don't care that much */
4466 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004467 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4468 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004469 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004470nevermind:
4471 deny->ld_owner.len = 0;
4472 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004473 deny->ld_clientid.cl_boot = 0;
4474 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 }
4476 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004477 deny->ld_length = NFS4_MAX_UINT64;
4478 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4480 deny->ld_type = NFS4_READ_LT;
4481 if (fl->fl_type != F_RDLCK)
4482 deny->ld_type = NFS4_WRITE_LT;
4483}
4484
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004485static struct nfs4_lockowner *
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004486find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4487 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488{
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004489 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4490 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004492 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4493 if (so->so_is_open_owner)
4494 continue;
4495 if (!same_owner_str(so, owner, clid))
4496 continue;
4497 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 }
4499 return NULL;
4500}
4501
4502/*
4503 * Alloc a lock owner structure.
4504 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004505 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004507 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004509static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004510alloc_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 -04004511 struct nfs4_lockowner *lo;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004512 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004514 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4515 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004517 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4518 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07004519 /* It is the openowner seqid that will be incremented in encode in the
4520 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004521 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004522 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004523 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524}
4525
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004526static struct nfs4_ol_stateid *
4527alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004529 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004530 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531
J. Bruce Fields996e0932011-10-17 11:14:48 -04004532 stp = nfs4_alloc_stateid(clp);
NeilBrown5ac049a2005-06-23 22:03:03 -07004533 if (stp == NULL)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04004534 return NULL;
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004535 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004536 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4537 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004538 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 stp->st_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004540 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004542 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004543 list_add(&stp->st_locks, &open_stp->st_locks);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004544 spin_lock(&fp->fi_lock);
4545 list_add(&stp->st_perfile, &fp->fi_stateids);
4546 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 return stp;
4548}
4549
Jeff Laytonc53530d2014-06-30 11:48:39 -04004550static struct nfs4_ol_stateid *
4551find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4552{
4553 struct nfs4_ol_stateid *lst;
4554
4555 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
4556 if (lst->st_file == fp)
4557 return lst;
4558 }
4559 return NULL;
4560}
4561
4562
NeilBrownfd39ca92005-06-23 22:04:03 -07004563static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564check_lock_length(u64 offset, u64 length)
4565{
Benny Halevy87df4de2008-12-15 19:42:03 +02004566 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 LOFF_OVERFLOW(offset, length)));
4568}
4569
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004570static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004571{
4572 struct nfs4_file *fp = lock_stp->st_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004573
Jeff Layton82c5ff12012-05-11 09:45:13 -04004574 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004575 return;
Jeff Layton12659652014-07-10 14:07:28 -04004576 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004577 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004578}
4579
J. Bruce Fields2355c592012-04-25 16:56:22 -04004580static __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 -04004581{
4582 struct nfs4_file *fi = ost->st_file;
4583 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4584 struct nfs4_client *cl = oo->oo_owner.so_client;
4585 struct nfs4_lockowner *lo;
4586 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004587 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004588
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004589 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004590 if (!lo) {
4591 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4592 &lock->v.new.owner);
4593 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4594 if (lo == NULL)
4595 return nfserr_jukebox;
4596 } else {
4597 /* with an existing lockowner, seqids must be the same */
4598 if (!cstate->minorversion &&
4599 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004600 return nfserr_bad_seqid;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004601 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004602
4603 *lst = find_lock_stateid(lo, fi);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004604 if (*lst == NULL) {
Jeff Laytonc53530d2014-06-30 11:48:39 -04004605 *lst = alloc_init_lock_stateid(lo, fi, ost);
4606 if (*lst == NULL) {
4607 release_lockowner_if_empty(lo);
4608 return nfserr_jukebox;
4609 }
4610 *new = true;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004611 }
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004612 return nfs_ok;
4613}
4614
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615/*
4616 * LOCK operation
4617 */
Al Virob37ad282006-10-19 23:28:59 -07004618__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004619nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004620 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004622 struct nfs4_openowner *open_sop = NULL;
4623 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004624 struct nfs4_ol_stateid *lock_stp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004625 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004626 struct file_lock *file_lock = NULL;
4627 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004628 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004629 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004630 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004631 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004632 struct net *net = SVC_NET(rqstp);
4633 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634
4635 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4636 (long long) lock->lk_offset,
4637 (long long) lock->lk_length);
4638
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 if (check_lock_length(lock->lk_offset, lock->lk_length))
4640 return nfserr_inval;
4641
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004642 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004643 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004644 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4645 return status;
4646 }
4647
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 nfs4_lock_state();
4649
4650 if (lock->lk_is_new) {
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004651 struct nfs4_ol_stateid *open_stp = NULL;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004652
4653 if (nfsd4_has_session(cstate))
4654 /* See rfc 5661 18.10.3: given clientid is ignored: */
4655 memcpy(&lock->v.new.clientid,
4656 &cstate->session->se_client->cl_clientid,
4657 sizeof(clientid_t));
4658
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004660 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004664 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 lock->lk_new_open_seqid,
4666 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004667 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07004668 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004670 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004671 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004672 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004673 &lock->v.new.clientid))
4674 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004675 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4676 &lock_stp, &new_state);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004677 } else
Benny Halevydd453df2009-04-03 08:28:41 +03004678 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004679 lock->lk_old_lock_seqid,
4680 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004681 NFS4_LOCK_STID, &lock_stp, nn);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004682 if (status)
4683 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004684 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004686 lkflg = setlkflg(lock->lk_type);
4687 status = nfs4_check_openmode(lock_stp, lkflg);
4688 if (status)
4689 goto out;
4690
NeilBrown0dd395d2005-07-07 17:59:15 -07004691 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004692 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004693 goto out;
4694 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004695 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004696 goto out;
4697
Jeff Layton21179d82012-08-21 08:03:32 -04004698 file_lock = locks_alloc_lock();
4699 if (!file_lock) {
4700 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4701 status = nfserr_jukebox;
4702 goto out;
4703 }
4704
4705 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 switch (lock->lk_type) {
4707 case NFS4_READ_LT:
4708 case NFS4_READW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004709 filp = find_readable_file(lock_stp->st_file);
4710 if (filp)
4711 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton21179d82012-08-21 08:03:32 -04004712 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004713 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 case NFS4_WRITE_LT:
4715 case NFS4_WRITEW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004716 filp = find_writeable_file(lock_stp->st_file);
4717 if (filp)
4718 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton21179d82012-08-21 08:03:32 -04004719 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004720 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 default:
4722 status = nfserr_inval;
4723 goto out;
4724 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004725 if (!filp) {
4726 status = nfserr_openmode;
4727 goto out;
4728 }
Jeff Layton21179d82012-08-21 08:03:32 -04004729 file_lock->fl_owner = (fl_owner_t)lock_sop;
4730 file_lock->fl_pid = current->tgid;
4731 file_lock->fl_file = filp;
4732 file_lock->fl_flags = FL_POSIX;
4733 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4734 file_lock->fl_start = lock->lk_offset;
4735 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4736 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737
Jeff Layton21179d82012-08-21 08:03:32 -04004738 conflock = locks_alloc_lock();
4739 if (!conflock) {
4740 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4741 status = nfserr_jukebox;
4742 goto out;
4743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744
Jeff Layton21179d82012-08-21 08:03:32 -04004745 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004746 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004748 update_stateid(&lock_stp->st_stid.sc_stateid);
4749 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004751 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004752 break;
4753 case (EAGAIN): /* conflock holds conflicting lock */
4754 status = nfserr_denied;
4755 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04004756 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004757 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 case (EDEADLK):
4759 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004760 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004761 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004762 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004763 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004764 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766out:
Trond Myklebustde186432014-07-10 14:07:26 -04004767 if (filp)
4768 fput(filp);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004769 if (status && new_state)
Jeff Laytonc53530d2014-06-30 11:48:39 -04004770 release_lock_stateid(lock_stp);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004771 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004772 if (!cstate->replay_owner)
4773 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004774 if (file_lock)
4775 locks_free_lock(file_lock);
4776 if (conflock)
4777 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 return status;
4779}
4780
4781/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004782 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4783 * so we do a temporary open here just to get an open file to pass to
4784 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4785 * inode operation.)
4786 */
Al Viro04da6e92012-04-13 00:00:04 -04004787static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004788{
4789 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04004790 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4791 if (!err) {
4792 err = nfserrno(vfs_test_lock(file, lock));
4793 nfsd_close(file);
4794 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004795 return err;
4796}
4797
4798/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 * LOCKT operation
4800 */
Al Virob37ad282006-10-19 23:28:59 -07004801__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004802nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4803 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804{
Jeff Layton21179d82012-08-21 08:03:32 -04004805 struct file_lock *file_lock = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004806 struct nfs4_lockowner *lo;
Al Virob37ad282006-10-19 23:28:59 -07004807 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004808 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004810 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 return nfserr_grace;
4812
4813 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4814 return nfserr_inval;
4815
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 nfs4_lock_state();
4817
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004818 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004819 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004820 if (status)
4821 goto out;
4822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004824 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826
Jeff Layton21179d82012-08-21 08:03:32 -04004827 file_lock = locks_alloc_lock();
4828 if (!file_lock) {
4829 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4830 status = nfserr_jukebox;
4831 goto out;
4832 }
4833 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 switch (lockt->lt_type) {
4835 case NFS4_READ_LT:
4836 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004837 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 break;
4839 case NFS4_WRITE_LT:
4840 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004841 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 break;
4843 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04004844 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 status = nfserr_inval;
4846 goto out;
4847 }
4848
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004849 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004850 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04004851 file_lock->fl_owner = (fl_owner_t)lo;
4852 file_lock->fl_pid = current->tgid;
4853 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854
Jeff Layton21179d82012-08-21 08:03:32 -04004855 file_lock->fl_start = lockt->lt_offset;
4856 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857
Jeff Layton21179d82012-08-21 08:03:32 -04004858 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859
Jeff Layton21179d82012-08-21 08:03:32 -04004860 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04004861 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05004862 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04004863
Jeff Layton21179d82012-08-21 08:03:32 -04004864 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04004866 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 }
4868out:
4869 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004870 if (file_lock)
4871 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 return status;
4873}
4874
Al Virob37ad282006-10-19 23:28:59 -07004875__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004876nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004877 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004879 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004881 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004882 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07004883 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004884 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4885
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4887 (long long) locku->lu_offset,
4888 (long long) locku->lu_length);
4889
4890 if (check_lock_length(locku->lu_offset, locku->lu_length))
4891 return nfserr_inval;
4892
4893 nfs4_lock_state();
4894
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004895 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004896 &locku->lu_stateid, NFS4_LOCK_STID,
4897 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004898 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004900 filp = find_any_file(stp->st_file);
4901 if (!filp) {
4902 status = nfserr_lock_range;
4903 goto out;
4904 }
Jeff Layton21179d82012-08-21 08:03:32 -04004905 file_lock = locks_alloc_lock();
4906 if (!file_lock) {
4907 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4908 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04004909 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04004910 }
4911 locks_init_lock(file_lock);
4912 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04004913 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04004914 file_lock->fl_pid = current->tgid;
4915 file_lock->fl_file = filp;
4916 file_lock->fl_flags = FL_POSIX;
4917 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4918 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919
Jeff Layton21179d82012-08-21 08:03:32 -04004920 file_lock->fl_end = last_byte_offset(locku->lu_offset,
4921 locku->lu_length);
4922 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923
Jeff Layton21179d82012-08-21 08:03:32 -04004924 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07004925 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05004926 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 goto out_nfserr;
4928 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004929 update_stateid(&stp->st_stid.sc_stateid);
4930 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04004931fput:
4932 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004934 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04004935 if (!cstate->replay_owner)
4936 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004937 if (file_lock)
4938 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 return status;
4940
4941out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07004942 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04004943 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944}
4945
4946/*
4947 * returns
4948 * 1: locks held by lockowner
4949 * 0: no locks held by lockowner
4950 */
4951static int
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004952check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953{
4954 struct file_lock **flpp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004955 struct inode *inode = filp->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 int status = 0;
4957
Jeff Layton1c8c6012013-06-21 08:58:15 -04004958 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004960 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 status = 1;
4962 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 }
4965out:
Jeff Layton1c8c6012013-06-21 08:58:15 -04004966 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 return status;
4968}
4969
Al Virob37ad282006-10-19 23:28:59 -07004970__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004971nfsd4_release_lockowner(struct svc_rqst *rqstp,
4972 struct nfsd4_compound_state *cstate,
4973 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974{
4975 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Laytonfd449072014-06-30 11:48:41 -04004976 struct nfs4_stateowner *sop = NULL, *tmp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004977 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004978 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 struct xdr_netobj *owner = &rlockowner->rl_owner;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004980 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07004981 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004982 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983
4984 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4985 clid->cl_boot, clid->cl_id);
4986
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 nfs4_lock_state();
4988
Jeff Layton4b24ca72014-06-30 11:48:44 -04004989 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004990 if (status)
4991 goto out;
4992
NeilBrown3e9e3db2005-06-23 22:04:20 -07004993 status = nfserr_locks_held;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05004994
Jeff Laytonfd449072014-06-30 11:48:41 -04004995 /* Find the matching lock stateowner */
4996 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
4997 if (tmp->so_is_open_owner)
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004998 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04004999 if (same_owner_str(tmp, owner, clid)) {
5000 sop = tmp;
5001 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07005003 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005004
5005 /* No matching owner found, maybe a replay? Just declare victory... */
5006 if (!sop) {
5007 status = nfs_ok;
5008 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005010
5011 lo = lockowner(sop);
5012 /* see if there are still any locks associated with it */
5013 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5014 if (check_for_locks(stp->st_file, lo))
5015 goto out;
5016 }
5017
5018 status = nfs_ok;
5019 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020out:
5021 nfs4_unlock_state();
5022 return status;
5023}
5024
5025static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005026alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027{
NeilBrowna55370a2005-06-23 22:03:52 -07005028 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029}
5030
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005031bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005032nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005033{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005034 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005035
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005036 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005037 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005038}
5039
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040/*
5041 * failure => all reset bets are off, nfserr_no_grace...
5042 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005043struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005044nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045{
5046 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005047 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048
NeilBrowna55370a2005-06-23 22:03:52 -07005049 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5050 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005051 if (crp) {
5052 strhashval = clientstr_hashval(name);
5053 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005054 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005055 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005056 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005057 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005058 }
5059 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060}
5061
Jeff Layton2a4317c2012-03-21 16:42:43 -04005062void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005063nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005064{
5065 list_del(&crp->cr_strhash);
5066 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005067 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005068}
5069
5070void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005071nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072{
5073 struct nfs4_client_reclaim *crp = NULL;
5074 int i;
5075
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005077 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5078 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005080 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 }
5082 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005083 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084}
5085
5086/*
5087 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005088struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005089nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090{
5091 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 struct nfs4_client_reclaim *crp = NULL;
5093
Jeff Layton278c9312012-11-12 15:00:52 -05005094 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
Jeff Layton278c9312012-11-12 15:00:52 -05005096 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005097 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005098 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 return crp;
5100 }
5101 }
5102 return NULL;
5103}
5104
5105/*
5106* Called from OPEN. Look for clientid in reclaim list.
5107*/
Al Virob37ad282006-10-19 23:28:59 -07005108__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005109nfs4_check_open_reclaim(clientid_t *clid,
5110 struct nfsd4_compound_state *cstate,
5111 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005113 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005114
5115 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005116 status = lookup_clientid(clid, cstate, nn);
5117 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005118 return nfserr_reclaim_bad;
5119
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005120 if (nfsd4_client_record_check(cstate->clp))
5121 return nfserr_reclaim_bad;
5122
5123 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124}
5125
Bryan Schumaker65178db2011-11-01 13:35:21 -04005126#ifdef CONFIG_NFSD_FAULT_INJECTION
5127
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005128u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5129{
J. Bruce Fields221a6872013-04-01 22:23:49 -04005130 if (mark_client_expired(clp))
5131 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005132 expire_client(clp);
5133 return 1;
5134}
5135
Bryan Schumaker184c1842012-11-29 11:40:44 -05005136u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5137{
5138 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005139 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005140 printk(KERN_INFO "NFS Client: %s\n", buf);
5141 return 1;
5142}
5143
5144static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5145 const char *type)
5146{
5147 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005148 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005149 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5150}
5151
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005152static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5153 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005154{
5155 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005156 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005157 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005158 u64 count = 0;
5159
5160 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005161 list_for_each_entry_safe(stp, st_next,
5162 &oop->oo_owner.so_stateids, st_perstateowner) {
5163 list_for_each_entry_safe(lst, lst_next,
5164 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005165 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005166 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005167 if (++count == max)
5168 return count;
5169 }
5170 }
5171 }
5172
5173 return count;
5174}
5175
5176u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5177{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005178 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005179}
5180
Bryan Schumaker184c1842012-11-29 11:40:44 -05005181u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5182{
5183 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5184 nfsd_print_count(clp, count, "locked files");
5185 return count;
5186}
5187
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005188static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5189{
5190 struct nfs4_openowner *oop, *next;
5191 u64 count = 0;
5192
5193 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5194 if (func)
5195 func(oop);
5196 if (++count == max)
5197 break;
5198 }
5199
5200 return count;
5201}
5202
5203u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5204{
5205 return nfsd_foreach_client_open(clp, max, release_openowner);
5206}
5207
Bryan Schumaker184c1842012-11-29 11:40:44 -05005208u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5209{
5210 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5211 nfsd_print_count(clp, count, "open files");
5212 return count;
5213}
5214
Bryan Schumaker269de302012-11-29 11:40:42 -05005215static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5216 struct list_head *victims)
5217{
5218 struct nfs4_delegation *dp, *next;
5219 u64 count = 0;
5220
Benny Halevycdc975052014-05-30 09:09:30 -04005221 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005222 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005223 if (victims) {
5224 /*
5225 * It's not safe to mess with delegations that have a
5226 * non-zero dl_time. They might have already been broken
5227 * and could be processed by the laundromat outside of
5228 * the state_lock. Just leave them be.
5229 */
5230 if (dp->dl_time != 0)
5231 continue;
5232
5233 /*
5234 * Increment dl_time to ensure that delegation breaks
5235 * don't monkey with it now that we are.
5236 */
5237 ++dp->dl_time;
Bryan Schumaker269de302012-11-29 11:40:42 -05005238 list_move(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005239 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005240 if (++count == max)
5241 break;
5242 }
5243 return count;
5244}
5245
5246u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5247{
5248 struct nfs4_delegation *dp, *next;
5249 LIST_HEAD(victims);
5250 u64 count;
5251
Benny Halevycdc975052014-05-30 09:09:30 -04005252 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005253 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc975052014-05-30 09:09:30 -04005254 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005255
5256 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005257 revoke_delegation(dp);
Bryan Schumaker269de302012-11-29 11:40:42 -05005258
5259 return count;
5260}
5261
5262u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5263{
Jeff Laytondff13992014-07-08 14:02:49 -04005264 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005265 LIST_HEAD(victims);
5266 u64 count;
5267
Benny Halevycdc975052014-05-30 09:09:30 -04005268 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005269 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005270 while (!list_empty(&victims)) {
5271 dp = list_first_entry(&victims, struct nfs4_delegation,
5272 dl_recall_lru);
5273 list_del_init(&dp->dl_recall_lru);
5274 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005275 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005276 }
Benny Halevycdc975052014-05-30 09:09:30 -04005277 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005278
5279 return count;
5280}
5281
Bryan Schumaker184c1842012-11-29 11:40:44 -05005282u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5283{
5284 u64 count = 0;
5285
Benny Halevycdc975052014-05-30 09:09:30 -04005286 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005287 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc975052014-05-30 09:09:30 -04005288 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005289
5290 nfsd_print_count(clp, count, "delegations");
5291 return count;
5292}
5293
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005294u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005295{
5296 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005297 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005298 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005299
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005300 if (!nfsd_netns_ready(nn))
5301 return 0;
5302
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005303 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005304 count += func(clp, max - count);
5305 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005306 break;
5307 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005308
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005309 return count;
5310}
5311
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005312struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5313{
5314 struct nfs4_client *clp;
5315 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5316
5317 if (!nfsd_netns_ready(nn))
5318 return NULL;
5319
5320 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5321 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5322 return clp;
5323 }
5324 return NULL;
5325}
5326
Bryan Schumaker65178db2011-11-01 13:35:21 -04005327#endif /* CONFIG_NFSD_FAULT_INJECTION */
5328
Meelap Shahc2f1a552007-07-17 04:04:39 -07005329/*
5330 * Since the lifetime of a delegation isn't limited to that of an open, a
5331 * client may quite reasonably hang on to a delegation as long as it has
5332 * the inode cached. This becomes an obvious problem the first time a
5333 * client's inode cache approaches the size of the server's total memory.
5334 *
5335 * For now we avoid this problem by imposing a hard limit on the number
5336 * of delegations, which varies according to the server's memory size.
5337 */
5338static void
5339set_max_delegations(void)
5340{
5341 /*
5342 * Allow at most 4 delegations per megabyte of RAM. Quick
5343 * estimates suggest that in the worst case (where every delegation
5344 * is for a different inode), a delegation could take about 1.5K,
5345 * giving a worst case usage of about 6% of memory.
5346 */
5347 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5348}
5349
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005350static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005351{
5352 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5353 int i;
5354
5355 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5356 CLIENT_HASH_SIZE, GFP_KERNEL);
5357 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005358 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005359 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5360 CLIENT_HASH_SIZE, GFP_KERNEL);
5361 if (!nn->unconf_id_hashtbl)
5362 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005363 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5364 OWNER_HASH_SIZE, GFP_KERNEL);
5365 if (!nn->ownerstr_hashtbl)
5366 goto err_ownerstr;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005367 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5368 SESSION_HASH_SIZE, GFP_KERNEL);
5369 if (!nn->sessionid_hashtbl)
5370 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005371
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005372 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005373 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005374 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005375 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005376 for (i = 0; i < OWNER_HASH_SIZE; i++)
5377 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005378 for (i = 0; i < SESSION_HASH_SIZE; i++)
5379 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005380 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005381 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005382 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005383 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005384 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005385 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005386
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005387 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005388 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005389
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005390 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005391
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005392err_sessionid:
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03005393 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005394err_ownerstr:
5395 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005396err_unconf_id:
5397 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005398err:
5399 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005400}
5401
5402static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005403nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005404{
5405 int i;
5406 struct nfs4_client *clp = NULL;
5407 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5408
5409 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5410 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5411 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5412 destroy_client(clp);
5413 }
5414 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005415
Kinglong Mee2b905632014-03-26 22:09:30 +08005416 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5417 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5418 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5419 destroy_client(clp);
5420 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005421 }
5422
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005423 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005424 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005425 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005426 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005427 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005428}
5429
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005430int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005431nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005432{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005433 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005434 int ret;
5435
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005436 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005437 if (ret)
5438 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005439 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005440 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005441 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005442 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005443 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005444 nn->nfsd4_grace, net);
5445 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005446 return 0;
5447}
5448
5449/* initialization to perform when the nfsd service is started: */
5450
5451int
5452nfs4_state_start(void)
5453{
5454 int ret;
5455
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005456 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005457 if (ret)
5458 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005459 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005460 if (laundry_wq == NULL) {
5461 ret = -ENOMEM;
5462 goto out_recovery;
5463 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005464 ret = nfsd4_create_callback_queue();
5465 if (ret)
5466 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005467
Meelap Shahc2f1a552007-07-17 04:04:39 -07005468 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005469
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005470 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005471
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005472out_free_laundry:
5473 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005474out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005475 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476}
5477
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005478void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005479nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005483 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005485 cancel_delayed_work_sync(&nn->laundromat_work);
5486 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005487
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005488 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04005490 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005491 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5493 list_move(&dp->dl_recall_lru, &reaplist);
5494 }
Benny Halevycdc975052014-05-30 09:09:30 -04005495 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 list_for_each_safe(pos, next, &reaplist) {
5497 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005498 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 }
5500
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005501 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005502 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005503 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504}
5505
5506void
5507nfs4_state_shutdown(void)
5508{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005509 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005510 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005512
5513static void
5514get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5515{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005516 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5517 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005518}
5519
5520static void
5521put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5522{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005523 if (cstate->minorversion) {
5524 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5525 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5526 }
5527}
5528
5529void
5530clear_current_stateid(struct nfsd4_compound_state *cstate)
5531{
5532 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005533}
5534
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005535/*
5536 * functions to set current state id
5537 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005538void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005539nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5540{
5541 put_stateid(cstate, &odp->od_stateid);
5542}
5543
5544void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005545nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5546{
5547 put_stateid(cstate, &open->op_stateid);
5548}
5549
5550void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005551nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5552{
5553 put_stateid(cstate, &close->cl_stateid);
5554}
5555
5556void
5557nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5558{
5559 put_stateid(cstate, &lock->lk_resp_stateid);
5560}
5561
5562/*
5563 * functions to consume current state id
5564 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005565
5566void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005567nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5568{
5569 get_stateid(cstate, &odp->od_stateid);
5570}
5571
5572void
5573nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5574{
5575 get_stateid(cstate, &drp->dr_stateid);
5576}
5577
5578void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005579nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5580{
5581 get_stateid(cstate, &fsp->fr_stateid);
5582}
5583
5584void
5585nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5586{
5587 get_stateid(cstate, &setattr->sa_stateid);
5588}
5589
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005590void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005591nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5592{
5593 get_stateid(cstate, &close->cl_stateid);
5594}
5595
5596void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005597nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005598{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005599 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005600}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01005601
5602void
5603nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5604{
5605 get_stateid(cstate, &read->rd_stateid);
5606}
5607
5608void
5609nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5610{
5611 get_stateid(cstate, &write->wr_stateid);
5612}