blob: c02bad6d7e90c6af15d360eb5580c91fe92c03b2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Chuck Lever7df302f2012-05-29 13:56:37 -040041#include <linux/ratelimit.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050042#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton59766872013-02-04 12:50:00 -050043#include <linux/sunrpc/addr.h>
NeilBrown6282cd52014-06-04 17:39:26 +100044#include <linux/hash.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020045#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040046#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050047#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040048#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040050#include "netns.h"
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define NFSDDBG_FACILITY NFSDDBG_PROC
53
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050054#define all_ones {{~0,~0},~0}
55static const stateid_t one_stateid = {
56 .si_generation = ~0,
57 .si_opaque = all_ones,
58};
59static const stateid_t zero_stateid = {
60 /* all fields zero */
61};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010062static const stateid_t currentstateid = {
63 .si_generation = 1,
64};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050065
Andy Adamsonec6b5d72009-04-03 08:28:28 +030066static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070067
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050068#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
69#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010070#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* forward declarations */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040073static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
J. Bruce Fields8b671b82009-02-22 14:51:34 -080075/* Locking: */
76
77/* Currently used for almost all code touching nfsv4 state: */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080078static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
J. Bruce Fields8b671b82009-02-22 14:51:34 -080080/*
81 * Currently used for the del_recall_lru and file hash table. In an
82 * effort to decrease the scope of the client_mutex, this spinlock may
83 * eventually cover more:
84 */
Benny Halevycdc97502014-05-30 09:09:30 -040085static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080086
Christoph Hellwigabf11352014-05-21 07:43:03 -070087static struct kmem_cache *openowner_slab;
88static struct kmem_cache *lockowner_slab;
89static struct kmem_cache *file_slab;
90static struct kmem_cache *stateid_slab;
91static struct kmem_cache *deleg_slab;
NeilBrowne60d4392005-06-23 22:03:01 -070092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093void
94nfs4_lock_state(void)
95{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080096 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -040099static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800100
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400101static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800102{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400103 return ses->se_flags & NFS4_SESSION_DEAD;
104}
105
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400106static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
107{
108 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400109 return nfserr_jukebox;
110 ses->se_flags |= NFS4_SESSION_DEAD;
111 return nfs_ok;
112}
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114void
115nfs4_unlock_state(void)
116{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800117 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
J. Bruce Fields221a6872013-04-01 22:23:49 -0400120static bool is_client_expired(struct nfs4_client *clp)
121{
122 return clp->cl_time == 0;
123}
124
125static __be32 mark_client_expired_locked(struct nfs4_client *clp)
126{
127 if (atomic_read(&clp->cl_refcount))
128 return nfserr_jukebox;
129 clp->cl_time = 0;
130 return nfs_ok;
131}
132
133static __be32 mark_client_expired(struct nfs4_client *clp)
134{
135 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
136 __be32 ret;
137
138 spin_lock(&nn->client_lock);
139 ret = mark_client_expired_locked(clp);
140 spin_unlock(&nn->client_lock);
141 return ret;
142}
143
144static __be32 get_client_locked(struct nfs4_client *clp)
145{
146 if (is_client_expired(clp))
147 return nfserr_expired;
148 atomic_inc(&clp->cl_refcount);
149 return nfs_ok;
150}
151
152/* must be called under the client_lock */
153static inline void
154renew_client_locked(struct nfs4_client *clp)
155{
156 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
157
158 if (is_client_expired(clp)) {
159 WARN_ON(1);
160 printk("%s: client (clientid %08x/%08x) already expired\n",
161 __func__,
162 clp->cl_clientid.cl_boot,
163 clp->cl_clientid.cl_id);
164 return;
165 }
166
167 dprintk("renewing client (clientid %08x/%08x)\n",
168 clp->cl_clientid.cl_boot,
169 clp->cl_clientid.cl_id);
170 list_move_tail(&clp->cl_lru, &nn->client_lru);
171 clp->cl_time = get_seconds();
172}
173
174static inline void
175renew_client(struct nfs4_client *clp)
176{
177 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
178
179 spin_lock(&nn->client_lock);
180 renew_client_locked(clp);
181 spin_unlock(&nn->client_lock);
182}
183
Fengguang Wuba138432013-04-16 22:14:15 -0400184static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400185{
186 if (!atomic_dec_and_test(&clp->cl_refcount))
187 return;
188 if (!is_client_expired(clp))
189 renew_client_locked(clp);
190}
191
Jeff Layton4b24ca72014-06-30 11:48:44 -0400192static void put_client_renew(struct nfs4_client *clp)
193{
194 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
195
Jeff Laytond6c249b2014-07-08 14:02:50 -0400196 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
197 return;
198 if (!is_client_expired(clp))
199 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400200 spin_unlock(&nn->client_lock);
201}
202
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400203static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
204{
205 __be32 status;
206
207 if (is_session_dead(ses))
208 return nfserr_badsession;
209 status = get_client_locked(ses->se_client);
210 if (status)
211 return status;
212 atomic_inc(&ses->se_ref);
213 return nfs_ok;
214}
215
216static void nfsd4_put_session_locked(struct nfsd4_session *ses)
217{
218 struct nfs4_client *clp = ses->se_client;
219
220 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
221 free_session(ses);
222 put_client_renew_locked(clp);
223}
224
225static void nfsd4_put_session(struct nfsd4_session *ses)
226{
227 struct nfs4_client *clp = ses->se_client;
228 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
229
230 spin_lock(&nn->client_lock);
231 nfsd4_put_session_locked(ses);
232 spin_unlock(&nn->client_lock);
233}
234
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236static inline u32
237opaque_hashval(const void *ptr, int nbytes)
238{
239 unsigned char *cptr = (unsigned char *) ptr;
240
241 u32 x = 0;
242 while (nbytes--) {
243 x *= 37;
244 x += *cptr++;
245 }
246 return x;
247}
248
J. Bruce Fields32513b42011-10-13 16:00:16 -0400249static void nfsd4_free_file(struct nfs4_file *f)
250{
251 kmem_cache_free(file_slab, f);
252}
253
NeilBrown13cd2182005-06-23 22:03:10 -0700254static inline void
255put_nfs4_file(struct nfs4_file *fi)
256{
Benny Halevycdc97502014-05-30 09:09:30 -0400257 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton89876f82013-04-02 09:01:59 -0400258 hlist_del(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400259 spin_unlock(&state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800260 iput(fi->fi_inode);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400261 nfsd4_free_file(fi);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800262 }
NeilBrown13cd2182005-06-23 22:03:10 -0700263}
264
265static inline void
266get_nfs4_file(struct nfs4_file *fi)
267{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800268 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700269}
270
Trond Myklebustde186432014-07-10 14:07:26 -0400271static struct file *
272__nfs4_get_fd(struct nfs4_file *f, int oflag)
273{
274 if (f->fi_fds[oflag])
275 return get_file(f->fi_fds[oflag]);
276 return NULL;
277}
278
279static struct file *
280find_writeable_file_locked(struct nfs4_file *f)
281{
282 struct file *ret;
283
284 lockdep_assert_held(&f->fi_lock);
285
286 ret = __nfs4_get_fd(f, O_WRONLY);
287 if (!ret)
288 ret = __nfs4_get_fd(f, O_RDWR);
289 return ret;
290}
291
292static struct file *
293find_writeable_file(struct nfs4_file *f)
294{
295 struct file *ret;
296
297 spin_lock(&f->fi_lock);
298 ret = find_writeable_file_locked(f);
299 spin_unlock(&f->fi_lock);
300
301 return ret;
302}
303
304static struct file *find_readable_file_locked(struct nfs4_file *f)
305{
306 struct file *ret;
307
308 lockdep_assert_held(&f->fi_lock);
309
310 ret = __nfs4_get_fd(f, O_RDONLY);
311 if (!ret)
312 ret = __nfs4_get_fd(f, O_RDWR);
313 return ret;
314}
315
316static struct file *
317find_readable_file(struct nfs4_file *f)
318{
319 struct file *ret;
320
321 spin_lock(&f->fi_lock);
322 ret = find_readable_file_locked(f);
323 spin_unlock(&f->fi_lock);
324
325 return ret;
326}
327
328static struct file *
329find_any_file(struct nfs4_file *f)
330{
331 struct file *ret;
332
333 spin_lock(&f->fi_lock);
334 ret = __nfs4_get_fd(f, O_RDWR);
335 if (!ret) {
336 ret = __nfs4_get_fd(f, O_WRONLY);
337 if (!ret)
338 ret = __nfs4_get_fd(f, O_RDONLY);
339 }
340 spin_unlock(&f->fi_lock);
341 return ret;
342}
343
NeilBrownef0f3392006-04-10 22:55:41 -0700344static int num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800345unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700346
347/*
348 * Open owner state (share locks)
349 */
350
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500351/* hash tables for lock and open owners */
352#define OWNER_HASH_BITS 8
353#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
354#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700355
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500356static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400357{
358 unsigned int ret;
359
360 ret = opaque_hashval(ownername->data, ownername->len);
361 ret += clientid;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500362 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400363}
NeilBrownef0f3392006-04-10 22:55:41 -0700364
NeilBrownef0f3392006-04-10 22:55:41 -0700365/* hash table for nfs4_file */
366#define FILE_HASH_BITS 8
367#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800368
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400369static unsigned int file_hashval(struct inode *ino)
370{
371 /* XXX: why are we hashing on inode pointer, anyway? */
372 return hash_ptr(ino, FILE_HASH_BITS);
373}
374
Jeff Layton89876f82013-04-02 09:01:59 -0400375static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700376
Jeff Layton12659652014-07-10 14:07:28 -0400377static void
378__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400379{
Jeff 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 Halevycdc97502014-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 Halevycdc97502014-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 Halevycdc97502014-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{
724 __set_bit(access, &stp->st_access_bmap);
725}
726
727/* clear share access for a given stateid */
728static inline void
729clear_access(u32 access, struct nfs4_ol_stateid *stp)
730{
731 __clear_bit(access, &stp->st_access_bmap);
732}
733
734/* test whether a given stateid has access */
735static inline bool
736test_access(u32 access, struct nfs4_ol_stateid *stp)
737{
738 return test_bit(access, &stp->st_access_bmap);
739}
740
Jeff Laytonce0fc432012-05-11 09:45:14 -0400741/* set share deny for a given stateid */
742static inline void
743set_deny(u32 access, struct nfs4_ol_stateid *stp)
744{
745 __set_bit(access, &stp->st_deny_bmap);
746}
747
748/* clear share deny for a given stateid */
749static inline void
750clear_deny(u32 access, struct nfs4_ol_stateid *stp)
751{
752 __clear_bit(access, &stp->st_deny_bmap);
753}
754
755/* test whether a given stateid is denying specific access */
756static inline bool
757test_deny(u32 access, struct nfs4_ol_stateid *stp)
758{
759 return test_bit(access, &stp->st_deny_bmap);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400760}
761
762static int nfs4_access_to_omode(u32 access)
763{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400764 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400765 case NFS4_SHARE_ACCESS_READ:
766 return O_RDONLY;
767 case NFS4_SHARE_ACCESS_WRITE:
768 return O_WRONLY;
769 case NFS4_SHARE_ACCESS_BOTH:
770 return O_RDWR;
771 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500772 WARN_ON_ONCE(1);
773 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400774}
775
Jeff Layton82c5ff12012-05-11 09:45:13 -0400776/* release all access and file references for a given stateid */
777static void
778release_all_access(struct nfs4_ol_stateid *stp)
779{
780 int i;
781
782 for (i = 1; i < 4; i++) {
783 if (test_access(i, stp))
Jeff Layton12659652014-07-10 14:07:28 -0400784 nfs4_file_put_access(stp->st_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400785 clear_access(i, stp);
786 }
787}
788
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400789static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500790{
Trond Myklebust1d31a252014-07-10 14:07:25 -0400791 struct nfs4_file *fp = stp->st_file;
792
793 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500794 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400795 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500796 list_del(&stp->st_perstateowner);
797}
798
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400799static void close_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500800{
Jeff Layton82c5ff12012-05-11 09:45:13 -0400801 release_all_access(stp);
OGAWA Hirofumia96e5b92011-04-18 11:48:55 -0400802 put_nfs4_file(stp->st_file);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400803 stp->st_file = NULL;
804}
805
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400806static void free_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400807{
J. Bruce Fields68a33962013-03-21 11:21:50 -0400808 remove_stid(&stp->st_stid);
Benny Halevy9857df82013-10-14 13:44:52 +0300809 nfs4_free_stid(stateid_slab, &stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500810}
811
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400812static void __release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500813{
814 struct file *file;
815
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400816 list_del(&stp->st_locks);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500817 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400818 unhash_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500819 file = find_any_file(stp->st_file);
Trond Myklebuste20fcf12014-07-10 14:07:27 -0400820 if (file)
821 filp_close(file, (fl_owner_t)lockowner(stp->st_stateowner));
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400822 close_generic_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500823 free_generic_stateid(stp);
824}
825
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400826static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500827{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400828 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500829
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400830 list_del(&lo->lo_owner.so_strhash);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400831 while (!list_empty(&lo->lo_owner.so_stateids)) {
832 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400833 struct nfs4_ol_stateid, st_perstateowner);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400834 __release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500835 }
836}
837
Trond Myklebust50cc6232014-04-18 14:44:03 -0400838static void nfs4_free_lockowner(struct nfs4_lockowner *lo)
839{
840 kfree(lo->lo_owner.so_owner.data);
841 kmem_cache_free(lockowner_slab, lo);
842}
843
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400844static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500845{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400846 unhash_lockowner(lo);
847 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500848}
849
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400850static void release_lockowner_if_empty(struct nfs4_lockowner *lo)
851{
852 if (list_empty(&lo->lo_owner.so_stateids))
853 release_lockowner(lo);
854}
855
856static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500857{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400858 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500859
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400860 lo = lockowner(stp->st_stateowner);
861 __release_lock_stateid(stp);
862 release_lockowner_if_empty(lo);
863}
864
865static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
866{
867 struct nfs4_ol_stateid *stp;
868
869 while (!list_empty(&open_stp->st_locks)) {
870 stp = list_entry(open_stp->st_locks.next,
871 struct nfs4_ol_stateid, st_locks);
872 release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500873 }
874}
875
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400876static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500877{
878 unhash_generic_stateid(stp);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400879 release_open_stateid_locks(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400880 close_generic_stateid(stp);
881}
882
883static void release_open_stateid(struct nfs4_ol_stateid *stp)
884{
885 unhash_open_stateid(stp);
J. Bruce Fields22839632009-01-11 14:27:17 -0500886 free_generic_stateid(stp);
887}
888
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400889static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500890{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400891 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500892
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400893 list_del(&oo->oo_owner.so_strhash);
894 list_del(&oo->oo_perclient);
895 while (!list_empty(&oo->oo_owner.so_stateids)) {
896 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400897 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500898 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500899 }
900}
901
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400902static void release_last_closed_stateid(struct nfs4_openowner *oo)
903{
904 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
905
906 if (s) {
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400907 free_generic_stateid(s);
908 oo->oo_last_closed_stid = NULL;
909 }
910}
911
Trond Myklebust50cc6232014-04-18 14:44:03 -0400912static void nfs4_free_openowner(struct nfs4_openowner *oo)
913{
914 kfree(oo->oo_owner.so_owner.data);
915 kmem_cache_free(openowner_slab, oo);
916}
917
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400918static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500919{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400920 unhash_openowner(oo);
921 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400922 release_last_closed_stateid(oo);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400923 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500924}
925
Marc Eshel5282fd72009-04-03 08:27:52 +0300926static inline int
927hash_sessionid(struct nfs4_sessionid *sessionid)
928{
929 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
930
931 return sid->sequence % SESSION_HASH_SIZE;
932}
933
Trond Myklebust8f199b82012-03-20 15:11:17 -0400934#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +0300935static inline void
936dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
937{
938 u32 *ptr = (u32 *)(&sessionid->data[0]);
939 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
940}
Trond Myklebust8f199b82012-03-20 15:11:17 -0400941#else
942static inline void
943dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
944{
945}
946#endif
947
J. Bruce Fields9411b1d2013-04-01 16:37:12 -0400948/*
949 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
950 * won't be used for replay.
951 */
952void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
953{
954 struct nfs4_stateowner *so = cstate->replay_owner;
955
956 if (nfserr == nfserr_replay_me)
957 return;
958
959 if (!seqid_mutating_err(ntohl(nfserr))) {
960 cstate->replay_owner = NULL;
961 return;
962 }
963 if (!so)
964 return;
965 if (so->so_is_open_owner)
966 release_last_closed_stateid(openowner(so));
967 so->so_seqid++;
968 return;
969}
Marc Eshel5282fd72009-04-03 08:27:52 +0300970
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300971static void
972gen_sessionid(struct nfsd4_session *ses)
973{
974 struct nfs4_client *clp = ses->se_client;
975 struct nfsd4_sessionid *sid;
976
977 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
978 sid->clientid = clp->cl_clientid;
979 sid->sequence = current_sessionid++;
980 sid->reserved = 0;
981}
982
983/*
Andy Adamsona6496372009-08-28 08:45:01 -0400984 * The protocol defines ca_maxresponssize_cached to include the size of
985 * the rpc header, but all we need to cache is the data starting after
986 * the end of the initial SEQUENCE operation--the rest we regenerate
987 * each time. Therefore we can advertise a ca_maxresponssize_cached
988 * value that is the number of bytes in our cache plus a few additional
989 * bytes. In order to stay on the safe side, and not promise more than
990 * we can cache, those additional bytes must be the minimum possible: 24
991 * bytes of rpc header (xid through accept state, with AUTH_NULL
992 * verifier), 12 for the compound header (with zero-length tag), and 44
993 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300994 */
Andy Adamsona6496372009-08-28 08:45:01 -0400995#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
996
Andy Adamson557ce262009-08-28 08:45:04 -0400997static void
998free_session_slots(struct nfsd4_session *ses)
999{
1000 int i;
1001
1002 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1003 kfree(ses->se_slots[i]);
1004}
1005
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001006/*
1007 * We don't actually need to cache the rpc and session headers, so we
1008 * can allocate a little less for each slot:
1009 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001010static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001011{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001012 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001013
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001014 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1015 size = 0;
1016 else
1017 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1018 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001019}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001020
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001021/*
1022 * XXX: If we run out of reserved DRC memory we could (up to a point)
1023 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001024 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001025 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001026static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001027{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001028 u32 slotsize = slot_bytes(ca);
1029 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001030 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001031
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001032 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001033 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1034 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001035 num = min_t(int, num, avail / slotsize);
1036 nfsd_drc_mem_used += num * slotsize;
1037 spin_unlock(&nfsd_drc_lock);
1038
1039 return num;
1040}
1041
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001042static void nfsd4_put_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 int slotsize = slot_bytes(ca);
1045
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001046 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001047 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001048 spin_unlock(&nfsd_drc_lock);
1049}
1050
Kinglong Mee60810e52014-01-01 00:35:47 +08001051static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1052 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001053{
Kinglong Mee60810e52014-01-01 00:35:47 +08001054 int numslots = fattrs->maxreqs;
1055 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001056 struct nfsd4_session *new;
1057 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001058
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001059 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001060 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1061 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001062
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001063 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001064 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001065 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001066 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001067 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001068 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001069 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001070 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001071 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001072
1073 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1074 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1075
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001076 return new;
1077out_free:
1078 while (i--)
1079 kfree(new->se_slots[i]);
1080 kfree(new);
1081 return NULL;
1082}
1083
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001084static void free_conn(struct nfsd4_conn *c)
1085{
1086 svc_xprt_put(c->cn_xprt);
1087 kfree(c);
1088}
1089
1090static void nfsd4_conn_lost(struct svc_xpt_user *u)
1091{
1092 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1093 struct nfs4_client *clp = c->cn_session->se_client;
1094
1095 spin_lock(&clp->cl_lock);
1096 if (!list_empty(&c->cn_persession)) {
1097 list_del(&c->cn_persession);
1098 free_conn(c);
1099 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001100 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001101 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001102}
1103
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001104static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001105{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001106 struct nfsd4_conn *conn;
1107
1108 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1109 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001110 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001111 svc_xprt_get(rqstp->rq_xprt);
1112 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001113 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001114 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1115 return conn;
1116}
1117
J. Bruce Fields328ead22010-09-29 16:11:06 -04001118static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1119{
1120 conn->cn_session = ses;
1121 list_add(&conn->cn_persession, &ses->se_conns);
1122}
1123
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001124static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1125{
1126 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001127
1128 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001129 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001130 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001131}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001132
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001133static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001134{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001135 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001136 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001137}
1138
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001139static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001140{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001141 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001142
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001143 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001144 ret = nfsd4_register_conn(conn);
1145 if (ret)
1146 /* oops; xprt is already down: */
1147 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields6a3b1562012-09-12 09:59:17 -04001148 if (conn->cn_flags & NFS4_CDFC4_BACK) {
Weston Andros Adamson24119672012-05-24 15:42:17 -04001149 /* callback channel may be back up */
1150 nfsd4_probe_callback(ses->se_client);
1151 }
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001152}
1153
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001154static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001155{
1156 u32 dir = NFS4_CDFC4_FORE;
1157
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001158 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001159 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001160 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001161}
1162
1163/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001164static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001165{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001166 struct nfs4_client *clp = s->se_client;
1167 struct nfsd4_conn *c;
1168
1169 spin_lock(&clp->cl_lock);
1170 while (!list_empty(&s->se_conns)) {
1171 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1172 list_del_init(&c->cn_persession);
1173 spin_unlock(&clp->cl_lock);
1174
1175 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1176 free_conn(c);
1177
1178 spin_lock(&clp->cl_lock);
1179 }
1180 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001181}
1182
J. Bruce Fields1377b692012-09-11 21:42:40 -04001183static void __free_session(struct nfsd4_session *ses)
1184{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001185 free_session_slots(ses);
1186 kfree(ses);
1187}
1188
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001189static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001190{
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001191 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1192
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001193 lockdep_assert_held(&nn->client_lock);
1194 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001195 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001196 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001197}
Andy Adamson557ce262009-08-28 08:45:04 -04001198
Fengguang Wu135ae822012-11-10 07:20:25 -05001199static 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 -04001200{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001201 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001202 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001203
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001204 new->se_client = clp;
1205 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001206
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001207 INIT_LIST_HEAD(&new->se_conns);
1208
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001209 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001210 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001211 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001212 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001213 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001214 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001215 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001216 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001217 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001218 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001219 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001220 spin_unlock(&nn->client_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001221
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001222 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001223 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001224 /*
1225 * This is a little silly; with sessions there's no real
1226 * use for the callback address. Use the peer address
1227 * as a reasonable default for now, but consider fixing
1228 * the rpc client not to require an address in the
1229 * future:
1230 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001231 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1232 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001233 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001234}
1235
Benny Halevy9089f1b2010-05-12 00:12:26 +03001236/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001237static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001238__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001239{
1240 struct nfsd4_session *elem;
1241 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001242 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001243
1244 dump_sessionid(__func__, sessionid);
1245 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001246 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001247 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001248 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1249 NFS4_MAX_SESSIONID_LEN)) {
1250 return elem;
1251 }
1252 }
1253
1254 dprintk("%s: session not found\n", __func__);
1255 return NULL;
1256}
1257
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001258static struct nfsd4_session *
1259find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1260 __be32 *ret)
1261{
1262 struct nfsd4_session *session;
1263 __be32 status = nfserr_badsession;
1264
1265 session = __find_in_sessionid_hashtbl(sessionid, net);
1266 if (!session)
1267 goto out;
1268 status = nfsd4_get_session_locked(session);
1269 if (status)
1270 session = NULL;
1271out:
1272 *ret = status;
1273 return session;
1274}
1275
Benny Halevy9089f1b2010-05-12 00:12:26 +03001276/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001277static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001278unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001279{
1280 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001281 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001282 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001283 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001284}
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1287static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001288STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001290 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001292 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001293 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return 1;
1295}
1296
1297/*
1298 * XXX Should we use a slab cache ?
1299 * This type of memory management is somewhat inefficient, but we use it
1300 * anyway since SETCLIENTID is not a common operation.
1301 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001302static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 struct nfs4_client *clp;
1305
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001306 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1307 if (clp == NULL)
1308 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001309 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001310 if (clp->cl_name.data == NULL) {
1311 kfree(clp);
1312 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001314 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001315 INIT_LIST_HEAD(&clp->cl_sessions);
1316 idr_init(&clp->cl_stateids);
1317 atomic_set(&clp->cl_refcount, 0);
1318 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1319 INIT_LIST_HEAD(&clp->cl_idhash);
1320 INIT_LIST_HEAD(&clp->cl_openowners);
1321 INIT_LIST_HEAD(&clp->cl_delegations);
1322 INIT_LIST_HEAD(&clp->cl_lru);
1323 INIT_LIST_HEAD(&clp->cl_callbacks);
1324 INIT_LIST_HEAD(&clp->cl_revoked);
1325 spin_lock_init(&clp->cl_lock);
1326 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 return clp;
1328}
1329
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001330static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331free_client(struct nfs4_client *clp)
1332{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001333 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001334
1335 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001336 while (!list_empty(&clp->cl_sessions)) {
1337 struct nfsd4_session *ses;
1338 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1339 se_perclnt);
1340 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001341 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1342 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001343 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001344 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001345 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001347 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 kfree(clp);
1349}
1350
Benny Halevy84d38ac2010-05-12 00:13:16 +03001351/* must be called under the client_lock */
1352static inline void
1353unhash_client_locked(struct nfs4_client *clp)
1354{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001355 struct nfsd4_session *ses;
1356
Benny Halevy84d38ac2010-05-12 00:13:16 +03001357 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001358 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001359 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1360 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001361 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001362}
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001365destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001367 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001370 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001373 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001374 while (!list_empty(&clp->cl_delegations)) {
1375 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
NeilBrownea1da632005-06-23 22:04:17 -07001376 list_del_init(&dp->dl_perclnt);
Jeff Laytondff13992014-07-08 14:02:49 -04001377 /* Ensure that deleg break won't try to requeue it */
1378 ++dp->dl_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 list_move(&dp->dl_recall_lru, &reaplist);
1380 }
Benny Halevycdc97502014-05-30 09:09:30 -04001381 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 while (!list_empty(&reaplist)) {
1383 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001384 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
Benny Halevy956c4fe2013-10-29 11:39:12 +02001386 list_splice_init(&clp->cl_revoked, &reaplist);
1387 while (!list_empty(&reaplist)) {
1388 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1389 destroy_revoked_delegation(dp);
1390 }
NeilBrownea1da632005-06-23 22:04:17 -07001391 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001392 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1393 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001395 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001396 if (clp->cl_cb_conn.cb_xprt)
1397 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001398 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001399 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001400 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001401 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001402 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001403 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001404 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001405 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1406 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001407 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001410static void expire_client(struct nfs4_client *clp)
1411{
1412 nfsd4_client_record_remove(clp);
1413 destroy_client(clp);
1414}
1415
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001416static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1417{
1418 memcpy(target->cl_verifier.data, source->data,
1419 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001422static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1423{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1425 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1426}
1427
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001428static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001429{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001430 if (source->cr_principal) {
1431 target->cr_principal =
1432 kstrdup(source->cr_principal, GFP_KERNEL);
1433 if (target->cr_principal == NULL)
1434 return -ENOMEM;
1435 } else
1436 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001437 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 target->cr_uid = source->cr_uid;
1439 target->cr_gid = source->cr_gid;
1440 target->cr_group_info = source->cr_group_info;
1441 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001442 target->cr_gss_mech = source->cr_gss_mech;
1443 if (source->cr_gss_mech)
1444 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001445 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001448static long long
1449compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1450{
1451 long long res;
1452
1453 res = o1->len - o2->len;
1454 if (res)
1455 return res;
1456 return (long long)memcmp(o1->data, o2->data, o1->len);
1457}
1458
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001459static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001460{
NeilBrowna55370a2005-06-23 22:03:52 -07001461 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
1464static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001465same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1466{
1467 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
1470static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001471same_clid(clientid_t *cl1, clientid_t *cl2)
1472{
1473 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001476static bool groups_equal(struct group_info *g1, struct group_info *g2)
1477{
1478 int i;
1479
1480 if (g1->ngroups != g2->ngroups)
1481 return false;
1482 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001483 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001484 return false;
1485 return true;
1486}
1487
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001488/*
1489 * RFC 3530 language requires clid_inuse be returned when the
1490 * "principal" associated with a requests differs from that previously
1491 * used. We use uid, gid's, and gss principal string as our best
1492 * approximation. We also don't want to allow non-gss use of a client
1493 * established using gss: in theory cr_principal should catch that
1494 * change, but in practice cr_principal can be null even in the gss case
1495 * since gssd doesn't always pass down a principal string.
1496 */
1497static bool is_gss_cred(struct svc_cred *cr)
1498{
1499 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1500 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1501}
1502
1503
Vivek Trivedi5559b502012-07-24 21:18:20 +05301504static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001505same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1506{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001507 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001508 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1509 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001510 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1511 return false;
1512 if (cr1->cr_principal == cr2->cr_principal)
1513 return true;
1514 if (!cr1->cr_principal || !cr2->cr_principal)
1515 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301516 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
J. Bruce Fields57266a62013-04-13 14:27:29 -04001519static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1520{
1521 struct svc_cred *cr = &rqstp->rq_cred;
1522 u32 service;
1523
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001524 if (!cr->cr_gss_mech)
1525 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001526 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1527 return service == RPC_GSS_SVC_INTEGRITY ||
1528 service == RPC_GSS_SVC_PRIVACY;
1529}
1530
1531static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1532{
1533 struct svc_cred *cr = &rqstp->rq_cred;
1534
1535 if (!cl->cl_mach_cred)
1536 return true;
1537 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1538 return false;
1539 if (!svc_rqst_integrity_protected(rqstp))
1540 return false;
1541 if (!cr->cr_principal)
1542 return false;
1543 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1544}
1545
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001546static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001547{
1548 static u32 current_clientid = 1;
1549
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001550 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 clp->cl_clientid.cl_id = current_clientid++;
1552}
1553
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001554static void gen_confirm(struct nfs4_client *clp)
1555{
Chuck Leverab4684d2012-03-02 17:13:50 -05001556 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001557 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Jeff Laytonf4199922014-06-17 07:44:11 -04001559 /*
1560 * This is opaque to client, so no need to byte-swap. Use
1561 * __force to keep sparse happy
1562 */
1563 verf[0] = (__force __be32)get_seconds();
1564 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001565 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001568static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001569{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001570 struct nfs4_stid *ret;
1571
1572 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1573 if (!ret || !ret->sc_type)
1574 return NULL;
1575 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001576}
1577
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001578static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001579{
1580 struct nfs4_stid *s;
1581
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001582 s = find_stateid(cl, t);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001583 if (!s)
1584 return NULL;
1585 if (typemask & s->sc_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001586 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001587 return NULL;
1588}
1589
Jeff Layton2216d442012-11-12 15:00:57 -05001590static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001591 struct svc_rqst *rqstp, nfs4_verifier *verf)
1592{
1593 struct nfs4_client *clp;
1594 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001595 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001596 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001597 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001598
1599 clp = alloc_client(name);
1600 if (clp == NULL)
1601 return NULL;
1602
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001603 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1604 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001605 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001606 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001607 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001608 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001609 }
J. Bruce Fields57725152012-11-05 15:10:26 -05001610 nfsd4_init_callback(&clp->cl_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001611 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001612 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001613 copy_verf(clp, verf);
1614 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001615 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001616 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001617 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001618 return clp;
1619}
1620
NeilBrownfd39ca92005-06-23 22:04:03 -07001621static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001622add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1623{
1624 struct rb_node **new = &(root->rb_node), *parent = NULL;
1625 struct nfs4_client *clp;
1626
1627 while (*new) {
1628 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1629 parent = *new;
1630
1631 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1632 new = &((*new)->rb_left);
1633 else
1634 new = &((*new)->rb_right);
1635 }
1636
1637 rb_link_node(&new_clp->cl_namenode, parent, new);
1638 rb_insert_color(&new_clp->cl_namenode, root);
1639}
1640
1641static struct nfs4_client *
1642find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1643{
1644 long long cmp;
1645 struct rb_node *node = root->rb_node;
1646 struct nfs4_client *clp;
1647
1648 while (node) {
1649 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1650 cmp = compare_blob(&clp->cl_name, name);
1651 if (cmp > 0)
1652 node = node->rb_left;
1653 else if (cmp < 0)
1654 node = node->rb_right;
1655 else
1656 return clp;
1657 }
1658 return NULL;
1659}
1660
1661static void
1662add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
1664 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001665 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001667 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001668 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001670 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001671 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
1673
NeilBrownfd39ca92005-06-23 22:04:03 -07001674static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675move_to_confirmed(struct nfs4_client *clp)
1676{
1677 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001678 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001681 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001682 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001683 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001684 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 renew_client(clp);
1686}
1687
1688static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001689find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
1691 struct nfs4_client *clp;
1692 unsigned int idhashval = clientid_hashval(clid->cl_id);
1693
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001694 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001695 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001696 if ((bool)clp->cl_minorversion != sessions)
1697 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001698 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 }
1702 return NULL;
1703}
1704
1705static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001706find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1707{
1708 struct list_head *tbl = nn->conf_id_hashtbl;
1709
1710 return find_client_in_id_table(tbl, clid, sessions);
1711}
1712
1713static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001714find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001716 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001718 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719}
1720
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001721static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001722{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001723 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001724}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001725
NeilBrown28ce6052005-06-23 22:03:56 -07001726static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001727find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001728{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001729 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001730}
1731
1732static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001733find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001734{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001735 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001736}
1737
NeilBrownfd39ca92005-06-23 22:04:03 -07001738static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001739gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001741 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001742 struct sockaddr *sa = svc_addr(rqstp);
1743 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001744 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Jeff Layton7077ecb2009-08-14 12:57:58 -04001746 /* Currently, we only support tcp and tcp6 for the callback channel */
1747 if (se->se_callback_netid_len == 3 &&
1748 !memcmp(se->se_callback_netid_val, "tcp", 3))
1749 expected_family = AF_INET;
1750 else if (se->se_callback_netid_len == 4 &&
1751 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1752 expected_family = AF_INET6;
1753 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 goto out_err;
1755
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001756 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001757 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001758 (struct sockaddr *)&conn->cb_addr,
1759 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001760
J. Bruce Fields07263f12010-05-31 19:09:40 -04001761 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001763
J. Bruce Fields07263f12010-05-31 19:09:40 -04001764 if (conn->cb_addr.ss_family == AF_INET6)
1765 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001766
J. Bruce Fields07263f12010-05-31 19:09:40 -04001767 conn->cb_prog = se->se_callback_prog;
1768 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001769 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 return;
1771out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001772 conn->cb_addr.ss_family = AF_UNSPEC;
1773 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001774 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 "will not receive delegations\n",
1776 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 return;
1779}
1780
Andy Adamson074fe892009-04-03 08:28:15 +03001781/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04001782 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001783 */
Trond Myklebustb6076642014-06-30 11:48:35 -04001784static void
Andy Adamson074fe892009-04-03 08:28:15 +03001785nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1786{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001787 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04001788 struct nfsd4_slot *slot = resp->cstate.slot;
1789 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001790
Andy Adamson557ce262009-08-28 08:45:04 -04001791 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001792
Andy Adamson557ce262009-08-28 08:45:04 -04001793 slot->sl_opcnt = resp->opcnt;
1794 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001795
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001796 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001797 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001798 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001799 return;
1800 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001801 base = resp->cstate.data_offset;
1802 slot->sl_datalen = buf->len - base;
1803 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04001804 WARN("%s: sessions DRC could not cache compound\n", __func__);
1805 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001806}
1807
1808/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001809 * Encode the replay sequence operation from the slot values.
1810 * If cachethis is FALSE encode the uncached rep error on the next
1811 * operation which sets resp->p and increments resp->opcnt for
1812 * nfs4svc_encode_compoundres.
1813 *
Andy Adamson074fe892009-04-03 08:28:15 +03001814 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001815static __be32
1816nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1817 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001818{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001819 struct nfsd4_op *op;
1820 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001821
Andy Adamsonabfabf82009-07-23 19:02:18 -04001822 /* Encode the replayed sequence operation */
1823 op = &args->ops[resp->opcnt - 1];
1824 nfsd4_encode_operation(resp, op);
1825
1826 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001827 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001828 op = &args->ops[resp->opcnt++];
1829 op->status = nfserr_retry_uncached_rep;
1830 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001831 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001832 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001833}
1834
1835/*
Andy Adamson557ce262009-08-28 08:45:04 -04001836 * The sequence operation is not cached because we can use the slot and
1837 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001838 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04001839static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001840nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1841 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001842{
Andy Adamson557ce262009-08-28 08:45:04 -04001843 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001844 struct xdr_stream *xdr = &resp->xdr;
1845 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03001846 __be32 status;
1847
Andy Adamson557ce262009-08-28 08:45:04 -04001848 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001849
Andy Adamsonabfabf82009-07-23 19:02:18 -04001850 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04001851 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04001852 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001853
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001854 p = xdr_reserve_space(xdr, slot->sl_datalen);
1855 if (!p) {
1856 WARN_ON_ONCE(1);
1857 return nfserr_serverfault;
1858 }
1859 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1860 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03001861
Andy Adamson557ce262009-08-28 08:45:04 -04001862 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001863 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001864}
1865
Andy Adamson0733d212009-04-03 08:28:01 +03001866/*
1867 * Set the exchange_id flags returned by the server.
1868 */
1869static void
1870nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1871{
1872 /* pNFS is not supported */
1873 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1874
1875 /* Referrals are supported, Migration is not. */
1876 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1877
1878 /* set the wire flags to return to client. */
1879 clid->flags = new->cl_exchange_flags;
1880}
1881
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001882static bool client_has_state(struct nfs4_client *clp)
1883{
1884 /*
1885 * Note clp->cl_openowners check isn't quite right: there's no
1886 * need to count owners without stateid's.
1887 *
1888 * Also note we should probably be using this in 4.0 case too.
1889 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04001890 return !list_empty(&clp->cl_openowners)
1891 || !list_empty(&clp->cl_delegations)
1892 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001893}
1894
Al Virob37ad282006-10-19 23:28:59 -07001895__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001896nfsd4_exchange_id(struct svc_rqst *rqstp,
1897 struct nfsd4_compound_state *cstate,
1898 struct nfsd4_exchange_id *exid)
1899{
Andy Adamson0733d212009-04-03 08:28:01 +03001900 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001901 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04001902 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001903 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001904 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001905 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001906 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03001907
Jeff Layton363168b2009-08-14 12:57:56 -04001908 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001909 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001910 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001911 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001912 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03001913
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001914 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03001915 return nfserr_inval;
1916
Andy Adamson0733d212009-04-03 08:28:01 +03001917 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04001918 case SP4_MACH_CRED:
1919 if (!svc_rqst_integrity_protected(rqstp))
1920 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03001921 case SP4_NONE:
1922 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001923 default: /* checked by xdr code */
1924 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03001925 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04001926 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03001927 }
1928
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001929 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03001930 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001931 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03001932 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001933 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
1934 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
1935
J. Bruce Fields136e6582012-05-12 20:37:23 -04001936 if (update) {
1937 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001938 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03001939 goto out;
1940 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04001941 if (!mach_creds_match(conf, rqstp)) {
1942 status = nfserr_wrong_cred;
1943 goto out;
1944 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001945 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03001946 status = nfserr_perm;
1947 goto out;
1948 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001949 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03001950 status = nfserr_not_same;
1951 goto out;
1952 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001953 /* case 6 */
1954 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1955 new = conf;
1956 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03001957 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001958 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001959 if (client_has_state(conf)) {
1960 status = nfserr_clid_inuse;
1961 goto out;
1962 }
Andy Adamson0733d212009-04-03 08:28:01 +03001963 expire_client(conf);
1964 goto out_new;
1965 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001966 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04001967 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04001968 new = conf;
1969 goto out_copy;
1970 }
1971 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04001972 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001973 }
1974
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001975 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001976 status = nfserr_noent;
1977 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001978 }
1979
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001980 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001981 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03001982 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03001983
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001984 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03001985out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05001986 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03001987 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04001988 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03001989 goto out;
1990 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04001991 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001992 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03001993
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001994 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001995 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03001996out_copy:
1997 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1998 exid->clientid.cl_id = new->cl_clientid.cl_id;
1999
J. Bruce Fields778df3f2012-05-25 21:40:23 -04002000 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03002001 nfsd4_set_ex_flags(new, exid);
2002
2003 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04002004 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002005 status = nfs_ok;
2006
2007out:
2008 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03002009 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002010}
2011
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002012static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002013check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002014{
Andy Adamson88e588d2009-07-23 19:02:15 -04002015 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2016 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002017
2018 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002019 if (slot_inuse) {
2020 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002021 return nfserr_jukebox;
2022 else
2023 return nfserr_seq_misordered;
2024 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002025 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002026 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002027 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002028 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002029 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002030 return nfserr_seq_misordered;
2031}
2032
Andy Adamson49557cc2009-07-23 19:02:16 -04002033/*
2034 * Cache the create session result into the create session single DRC
2035 * slot cache by saving the xdr structure. sl_seqid has been set.
2036 * Do this for solo or embedded create session operations.
2037 */
2038static void
2039nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002040 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002041{
2042 slot->sl_status = nfserr;
2043 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2044}
2045
2046static __be32
2047nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2048 struct nfsd4_clid_slot *slot)
2049{
2050 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2051 return slot->sl_status;
2052}
2053
Mi Jinlong1b74c252011-07-14 14:50:17 +08002054#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2055 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2056 1 + /* MIN tag is length with zero, only length */ \
2057 3 + /* version, opcount, opcode */ \
2058 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2059 /* seqid, slotID, slotID, cache */ \
2060 4 ) * sizeof(__be32))
2061
2062#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2063 2 + /* verifier: AUTH_NULL, length 0 */\
2064 1 + /* status */ \
2065 1 + /* MIN tag is length with zero, only length */ \
2066 3 + /* opcount, opcode, opstatus*/ \
2067 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2068 /* seqid, slotID, slotID, slotID, status */ \
2069 5 ) * sizeof(__be32))
2070
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002071static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002072{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002073 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2074
J. Bruce Fields373cd402013-04-08 15:42:12 -04002075 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2076 return nfserr_toosmall;
2077 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2078 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002079 ca->headerpadsz = 0;
2080 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2081 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2082 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2083 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2084 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2085 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2086 /*
2087 * Note decreasing slot size below client's request may make it
2088 * difficult for client to function correctly, whereas
2089 * decreasing the number of slots will (just?) affect
2090 * performance. When short on memory we therefore prefer to
2091 * decrease number of slots instead of their size. Clients that
2092 * request larger slots than they need will get poor results:
2093 */
2094 ca->maxreqs = nfsd4_get_drc_mem(ca);
2095 if (!ca->maxreqs)
2096 return nfserr_jukebox;
2097
J. Bruce Fields373cd402013-04-08 15:42:12 -04002098 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002099}
2100
Kinglong Mee8a891632013-12-23 18:11:02 +08002101#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2102 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2103#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2104 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2105
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002106static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2107{
2108 ca->headerpadsz = 0;
2109
2110 /*
2111 * These RPC_MAX_HEADER macros are overkill, especially since we
2112 * don't even do gss on the backchannel yet. But this is still
2113 * less than 1k. Tighten up this estimate in the unlikely event
2114 * it turns out to be a problem for some client:
2115 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002116 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002117 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002118 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002119 return nfserr_toosmall;
2120 ca->maxresp_cached = 0;
2121 if (ca->maxops < 2)
2122 return nfserr_toosmall;
2123
2124 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002125}
2126
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002127static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2128{
2129 switch (cbs->flavor) {
2130 case RPC_AUTH_NULL:
2131 case RPC_AUTH_UNIX:
2132 return nfs_ok;
2133 default:
2134 /*
2135 * GSS case: the spec doesn't allow us to return this
2136 * error. But it also doesn't allow us not to support
2137 * GSS.
2138 * I'd rather this fail hard than return some error the
2139 * client might think it can already handle:
2140 */
2141 return nfserr_encr_alg_unsupp;
2142 }
2143}
2144
Andy Adamson069b6ad2009-04-03 08:27:58 +03002145__be32
2146nfsd4_create_session(struct svc_rqst *rqstp,
2147 struct nfsd4_compound_state *cstate,
2148 struct nfsd4_create_session *cr_ses)
2149{
Jeff Layton363168b2009-08-14 12:57:56 -04002150 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002151 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002152 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002153 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002154 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002155 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002156 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002157
Mi Jinlonga62573d2011-03-23 17:57:07 +08002158 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2159 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002160 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2161 if (status)
2162 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002163 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002164 if (status)
2165 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002166 status = check_backchannel_attrs(&cr_ses->back_channel);
2167 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002168 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002169 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002170 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002171 if (!new)
2172 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002173 conn = alloc_conn_from_crses(rqstp, cr_ses);
2174 if (!conn)
2175 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002176
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002177 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002178 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002179 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002180 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002181
2182 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002183 status = nfserr_wrong_cred;
2184 if (!mach_creds_match(conf, rqstp))
2185 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002186 cs_slot = &conf->cl_cs_slot;
2187 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002188 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002189 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002190 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002191 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002192 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002193 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002194 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002195 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002196 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002197 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002198 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002199 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002200 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002201 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002202 status = nfserr_wrong_cred;
2203 if (!mach_creds_match(unconf, rqstp))
2204 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002205 cs_slot = &unconf->cl_cs_slot;
2206 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002207 if (status) {
2208 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002209 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002210 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002211 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002212 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002213 if (old) {
2214 status = mark_client_expired(old);
2215 if (status)
2216 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002217 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002218 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002219 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002220 conf = unconf;
2221 } else {
2222 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002223 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002224 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002225 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002226 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002227 * We do not support RDMA or persistent sessions
2228 */
2229 cr_ses->flags &= ~SESSION4_PERSIST;
2230 cr_ses->flags &= ~SESSION4_RDMA;
2231
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002232 init_session(rqstp, new, conf, cr_ses);
2233 nfsd4_init_conn(rqstp, conn, new);
2234
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002235 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002236 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002237 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002238 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002239
Andy Adamson49557cc2009-07-23 19:02:16 -04002240 /* cache solo and embedded create sessions under the state lock */
2241 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002242 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002243 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002244out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08002245 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002246 free_conn(conn);
2247out_free_session:
2248 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002249out_release_drc_mem:
2250 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002251 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002252}
2253
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002254static __be32 nfsd4_map_bcts_dir(u32 *dir)
2255{
2256 switch (*dir) {
2257 case NFS4_CDFC4_FORE:
2258 case NFS4_CDFC4_BACK:
2259 return nfs_ok;
2260 case NFS4_CDFC4_FORE_OR_BOTH:
2261 case NFS4_CDFC4_BACK_OR_BOTH:
2262 *dir = NFS4_CDFC4_BOTH;
2263 return nfs_ok;
2264 };
2265 return nfserr_inval;
2266}
2267
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002268__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2269{
2270 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002271 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002272 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002273
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002274 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2275 if (status)
2276 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002277 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002278 session->se_cb_prog = bc->bc_cb_program;
2279 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002280 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002281
2282 nfsd4_probe_callback(session->se_client);
2283
2284 return nfs_ok;
2285}
2286
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002287__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2288 struct nfsd4_compound_state *cstate,
2289 struct nfsd4_bind_conn_to_session *bcts)
2290{
2291 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002292 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002293 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002294 struct net *net = SVC_NET(rqstp);
2295 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002296
2297 if (!nfsd4_last_compound_op(rqstp))
2298 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002299 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002300 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002301 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002302 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002303 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002304 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002305 status = nfserr_wrong_cred;
2306 if (!mach_creds_match(session->se_client, rqstp))
2307 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002308 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002309 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002310 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002311 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002312 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002313 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002314 goto out;
2315 nfsd4_init_conn(rqstp, conn, session);
2316 status = nfs_ok;
2317out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002318 nfsd4_put_session(session);
2319out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002320 nfs4_unlock_state();
2321 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002322}
2323
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002324static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2325{
2326 if (!session)
2327 return 0;
2328 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2329}
2330
Andy Adamson069b6ad2009-04-03 08:27:58 +03002331__be32
2332nfsd4_destroy_session(struct svc_rqst *r,
2333 struct nfsd4_compound_state *cstate,
2334 struct nfsd4_destroy_session *sessionid)
2335{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002336 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002337 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002338 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002339 struct net *net = SVC_NET(r);
2340 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002341
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002342 nfs4_lock_state();
2343 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002344 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002345 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002346 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002347 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002348 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002349 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002350 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002351 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002352 if (!ses)
2353 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002354 status = nfserr_wrong_cred;
2355 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002356 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002357 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002358 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002359 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002360 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002361 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002362
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002363 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002364
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002365 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002366 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002367out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002368 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002369out_client_lock:
2370 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002371out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002372 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002373 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002374}
2375
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002376static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002377{
2378 struct nfsd4_conn *c;
2379
2380 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002381 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002382 return c;
2383 }
2384 }
2385 return NULL;
2386}
2387
J. Bruce Fields57266a62013-04-13 14:27:29 -04002388static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002389{
2390 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002391 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002392 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002393 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002394
2395 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002396 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002397 if (c)
2398 goto out_free;
2399 status = nfserr_conn_not_bound_to_session;
2400 if (clp->cl_mach_cred)
2401 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002402 __nfsd4_hash_conn(new, ses);
2403 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002404 ret = nfsd4_register_conn(new);
2405 if (ret)
2406 /* oops; xprt is already down: */
2407 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002408 return nfs_ok;
2409out_free:
2410 spin_unlock(&clp->cl_lock);
2411 free_conn(new);
2412 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002413}
2414
Mi Jinlong868b89c2011-04-27 09:09:58 +08002415static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2416{
2417 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2418
2419 return args->opcnt > session->se_fchannel.maxops;
2420}
2421
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002422static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2423 struct nfsd4_session *session)
2424{
2425 struct xdr_buf *xb = &rqstp->rq_arg;
2426
2427 return xb->len > session->se_fchannel.maxreq_sz;
2428}
2429
Andy Adamson069b6ad2009-04-03 08:27:58 +03002430__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002431nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002432 struct nfsd4_compound_state *cstate,
2433 struct nfsd4_sequence *seq)
2434{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002435 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002436 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002437 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002438 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002439 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002440 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002441 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002442 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002443 struct net *net = SVC_NET(rqstp);
2444 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002445
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002446 if (resp->opcnt != 1)
2447 return nfserr_sequence_pos;
2448
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002449 /*
2450 * Will be either used or freed by nfsd4_sequence_check_conn
2451 * below.
2452 */
2453 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2454 if (!conn)
2455 return nfserr_jukebox;
2456
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002457 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002458 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002459 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002460 goto out_no_session;
2461 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002462
Mi Jinlong868b89c2011-04-27 09:09:58 +08002463 status = nfserr_too_many_ops;
2464 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002465 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002466
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002467 status = nfserr_req_too_big;
2468 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002469 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002470
Benny Halevyb85d4c02009-04-03 08:28:08 +03002471 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002472 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002473 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002474
Andy Adamson557ce262009-08-28 08:45:04 -04002475 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002476 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2477
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002478 /* We do not negotiate the number of slots yet, so set the
2479 * maxslots to the session maxreqs which is used to encode
2480 * sr_highest_slotid and the sr_target_slot id to maxslots */
2481 seq->maxslots = session->se_fchannel.maxreqs;
2482
J. Bruce Fields73e79482012-02-13 16:39:00 -05002483 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2484 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002485 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002486 status = nfserr_seq_misordered;
2487 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002488 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002489 cstate->slot = slot;
2490 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002491 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002492 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002493 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002494 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002495 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002496 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002497 }
2498 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002499 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002500
J. Bruce Fields57266a62013-04-13 14:27:29 -04002501 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002502 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002503 if (status)
2504 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002505
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002506 buflen = (seq->cachethis) ?
2507 session->se_fchannel.maxresp_cached :
2508 session->se_fchannel.maxresp_sz;
2509 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2510 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002511 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002512 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002513 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002514
2515 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002516 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002517 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002518 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002519 if (seq->cachethis)
2520 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002521 else
2522 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002523
2524 cstate->slot = slot;
2525 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002526 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002527
Benny Halevyb85d4c02009-04-03 08:28:08 +03002528out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002529 switch (clp->cl_cb_state) {
2530 case NFSD4_CB_DOWN:
2531 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2532 break;
2533 case NFSD4_CB_FAULT:
2534 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2535 break;
2536 default:
2537 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002538 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002539 if (!list_empty(&clp->cl_revoked))
2540 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002541out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002542 if (conn)
2543 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002544 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002545 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002546out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002547 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002548 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002549}
2550
Trond Myklebustb6076642014-06-30 11:48:35 -04002551void
2552nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2553{
2554 struct nfsd4_compound_state *cs = &resp->cstate;
2555
2556 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002557 if (cs->status != nfserr_replay_cache) {
2558 nfsd4_store_cache_entry(resp);
2559 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2560 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002561 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002562 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002563 } else if (cs->clp)
2564 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002565}
2566
Mi Jinlong345c2842011-10-20 17:51:39 +08002567__be32
2568nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2569{
2570 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002571 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002572 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002573
2574 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002575 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002576 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002577 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002578
2579 if (conf) {
2580 clp = conf;
2581
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002582 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002583 status = nfserr_clientid_busy;
2584 goto out;
2585 }
2586 } else if (unconf)
2587 clp = unconf;
2588 else {
2589 status = nfserr_stale_clientid;
2590 goto out;
2591 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002592 if (!mach_creds_match(clp, rqstp)) {
2593 status = nfserr_wrong_cred;
2594 goto out;
2595 }
Mi Jinlong345c2842011-10-20 17:51:39 +08002596 expire_client(clp);
2597out:
2598 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002599 return status;
2600}
2601
Andy Adamson069b6ad2009-04-03 08:27:58 +03002602__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002603nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2604{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002605 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002606
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002607 if (rc->rca_one_fs) {
2608 if (!cstate->current_fh.fh_dentry)
2609 return nfserr_nofilehandle;
2610 /*
2611 * We don't take advantage of the rca_one_fs case.
2612 * That's OK, it's optional, we can safely ignore it.
2613 */
2614 return nfs_ok;
2615 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002616
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002617 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002618 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002619 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2620 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002621 goto out;
2622
2623 status = nfserr_stale_clientid;
2624 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002625 /*
2626 * The following error isn't really legal.
2627 * But we only get here if the client just explicitly
2628 * destroyed the client. Surely it no longer cares what
2629 * error it gets back on an operation for the dead
2630 * client.
2631 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002632 goto out;
2633
2634 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002635 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002636out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002637 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002638 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002639}
2640
2641__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002642nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2643 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002645 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002647 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002648 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002649 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2650
J. Bruce Fields63db4632012-05-18 21:54:19 -04002651 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002653 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002654 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002655 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002657 if (clp_used_exchangeid(conf))
2658 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002659 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002660 char addr_str[INET6_ADDRSTRLEN];
2661 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2662 sizeof(addr_str));
2663 dprintk("NFSD: setclientid: string in use by client "
2664 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 goto out;
2666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002668 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002669 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002672 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002673 if (new == NULL)
2674 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002675 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002676 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002678 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002679 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002680 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002681 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002682 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2684 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2685 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2686 status = nfs_ok;
2687out:
2688 nfs4_unlock_state();
2689 return status;
2690}
2691
2692
Al Virob37ad282006-10-19 23:28:59 -07002693__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002694nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2695 struct nfsd4_compound_state *cstate,
2696 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697{
NeilBrown21ab45a2005-06-23 22:04:14 -07002698 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2700 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002701 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002702 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002704 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002707
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002708 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002709 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002710 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002711 * We try hard to give out unique clientid's, so if we get an
2712 * attempt to confirm the same clientid with a different cred,
2713 * there's a bug somewhere. Let's charitably assume it's our
2714 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002715 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002716 status = nfserr_serverfault;
2717 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2718 goto out;
2719 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2720 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002721 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002722 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2723 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002725 else /* case 4: client hasn't noticed we rebooted yet? */
2726 status = nfserr_stale_clientid;
2727 goto out;
2728 }
2729 status = nfs_ok;
2730 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002731 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2732 nfsd4_probe_callback(conf);
2733 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002734 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002735 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002736 if (conf) {
2737 status = mark_client_expired(conf);
2738 if (status)
2739 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002740 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002741 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002742 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002743 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 nfs4_unlock_state();
2747 return status;
2748}
2749
J. Bruce Fields32513b42011-10-13 16:00:16 -04002750static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002752 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2753}
2754
2755/* OPEN Share state helper functions */
2756static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
2757{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 unsigned int hashval = file_hashval(ino);
2759
Trond Myklebust950e0112014-06-30 11:48:31 -04002760 lockdep_assert_held(&state_lock);
2761
J. Bruce Fields32513b42011-10-13 16:00:16 -04002762 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04002763 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002764 INIT_LIST_HEAD(&fp->fi_stateids);
2765 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebust950e0112014-06-30 11:48:31 -04002766 ihold(ino);
2767 fp->fi_inode = ino;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002768 fp->fi_had_conflict = false;
2769 fp->fi_lease = NULL;
2770 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2771 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04002772 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773}
2774
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002775void
NeilBrowne60d4392005-06-23 22:03:01 -07002776nfsd4_free_slabs(void)
2777{
Christoph Hellwigabf11352014-05-21 07:43:03 -07002778 kmem_cache_destroy(openowner_slab);
2779 kmem_cache_destroy(lockowner_slab);
2780 kmem_cache_destroy(file_slab);
2781 kmem_cache_destroy(stateid_slab);
2782 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002783}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Bryan Schumaker72083392011-11-01 15:24:59 -04002785int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786nfsd4_init_slabs(void)
2787{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002788 openowner_slab = kmem_cache_create("nfsd4_openowners",
2789 sizeof(struct nfs4_openowner), 0, 0, NULL);
2790 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002791 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002792 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002793 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002794 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002795 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07002796 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002797 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002798 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002799 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07002800 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002801 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002802 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002803 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002804 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002805 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002806 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002807 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07002809
2810out_free_stateid_slab:
2811 kmem_cache_destroy(stateid_slab);
2812out_free_file_slab:
2813 kmem_cache_destroy(file_slab);
2814out_free_lockowner_slab:
2815 kmem_cache_destroy(lockowner_slab);
2816out_free_openowner_slab:
2817 kmem_cache_destroy(openowner_slab);
2818out:
NeilBrowne60d4392005-06-23 22:03:01 -07002819 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2820 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821}
2822
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002823static void init_nfs4_replay(struct nfs4_replay *rp)
2824{
2825 rp->rp_status = nfserr_serverfault;
2826 rp->rp_buflen = 0;
2827 rp->rp_buf = rp->rp_ibuf;
2828}
2829
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002830static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831{
2832 struct nfs4_stateowner *sop;
2833
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002834 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002835 if (!sop)
2836 return NULL;
2837
2838 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2839 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002840 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002841 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002843 sop->so_owner.len = owner->len;
2844
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002845 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002846 sop->so_client = clp;
2847 init_nfs4_replay(&sop->so_replay);
2848 return sop;
2849}
2850
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002851static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002852{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002853 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2854
2855 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002856 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857}
2858
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002859static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04002860alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002861 struct nfsd4_compound_state *cstate)
2862{
Trond Myklebust13d6f662014-06-30 11:48:45 -04002863 struct nfs4_client *clp = cstate->clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002864 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002866 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2867 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002869 oo->oo_owner.so_is_open_owner = 1;
2870 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04002871 oo->oo_flags = NFS4_OO_NEW;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002872 if (nfsd4_has_session(cstate))
2873 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002874 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002875 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002876 INIT_LIST_HEAD(&oo->oo_close_lru);
2877 hash_openowner(oo, clp, strhashval);
2878 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879}
2880
J. Bruce Fields996e0932011-10-17 11:14:48 -04002881static 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 -04002882 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002884 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04002885 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002886 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002887 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002888 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 stp->st_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 stp->st_access_bmap = 0;
2891 stp->st_deny_bmap = 0;
Jeff Layton82c5ff12012-05-11 09:45:13 -04002892 set_access(open->op_share_access, stp);
Jeff Laytonce0fc432012-05-11 09:45:14 -04002893 set_deny(open->op_share_deny, stp);
NeilBrown4c4cd222005-07-07 17:59:27 -07002894 stp->st_openstp = NULL;
Trond Myklebust1d31a252014-07-10 14:07:25 -04002895 spin_lock(&fp->fi_lock);
2896 list_add(&stp->st_perfile, &fp->fi_stateids);
2897 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898}
2899
2900static void
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002901move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002903 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2904
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002905 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002907 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002908 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909}
2910
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002912same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2913 clientid_t *clid)
2914{
2915 return (sop->so_owner.len == owner->len) &&
2916 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2917 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918}
2919
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002920static struct nfs4_openowner *
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002921find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
2922 bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002924 struct nfs4_stateowner *so;
2925 struct nfs4_openowner *oo;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002926 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002928 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05002929 if (!so->so_is_open_owner)
2930 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002931 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
2932 oo = openowner(so);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002933 clp = oo->oo_owner.so_client;
2934 if ((bool)clp->cl_minorversion != sessions)
2935 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002936 renew_client(oo->oo_owner.so_client);
2937 return oo;
2938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 }
2940 return NULL;
2941}
2942
2943/* search file_hashtbl[] for file */
2944static struct nfs4_file *
Trond Myklebust950e0112014-06-30 11:48:31 -04002945find_file_locked(struct inode *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946{
2947 unsigned int hashval = file_hashval(ino);
2948 struct nfs4_file *fp;
2949
Trond Myklebust950e0112014-06-30 11:48:31 -04002950 lockdep_assert_held(&state_lock);
2951
Jeff Layton89876f82013-04-02 09:01:59 -04002952 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07002953 if (fp->fi_inode == ino) {
2954 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07002956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 }
2958 return NULL;
2959}
2960
Trond Myklebust950e0112014-06-30 11:48:31 -04002961static struct nfs4_file *
2962find_file(struct inode *ino)
2963{
2964 struct nfs4_file *fp;
2965
2966 spin_lock(&state_lock);
2967 fp = find_file_locked(ino);
2968 spin_unlock(&state_lock);
2969 return fp;
2970}
2971
2972static struct nfs4_file *
2973find_or_add_file(struct inode *ino, struct nfs4_file *new)
2974{
2975 struct nfs4_file *fp;
2976
2977 spin_lock(&state_lock);
2978 fp = find_file_locked(ino);
2979 if (fp == NULL) {
2980 nfsd4_init_file(new, ino);
2981 fp = new;
2982 }
2983 spin_unlock(&state_lock);
2984
2985 return fp;
2986}
2987
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04002988/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 * Called to check deny when READ with all zero stateid or
2990 * WRITE with all zero or all one stateid
2991 */
Al Virob37ad282006-10-19 23:28:59 -07002992static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2994{
2995 struct inode *ino = current_fh->fh_dentry->d_inode;
2996 struct nfs4_file *fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002997 struct nfs4_ol_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07002998 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07003001 if (!fp)
3002 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07003003 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 /* Search for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003005 spin_lock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003006 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
Jeff Laytonce0fc432012-05-11 09:45:14 -04003007 if (test_deny(deny_type, stp) ||
3008 test_deny(NFS4_SHARE_DENY_BOTH, stp))
NeilBrown13cd2182005-06-23 22:03:10 -07003009 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 }
NeilBrown13cd2182005-06-23 22:03:10 -07003011 ret = nfs_ok;
3012out:
Trond Myklebust1d31a252014-07-10 14:07:25 -04003013 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003014 put_nfs4_file(fp);
3015 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016}
3017
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003018static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003020 struct nfs4_client *clp = dp->dl_stid.sc_client;
3021 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3022
Benny Halevycdc97502014-05-30 09:09:30 -04003023 lockdep_assert_held(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 /* We're assuming the state code never drops its reference
3025 * without first removing the lease. Since we're in this lease
3026 * callback (and since the lease code is serialized by the kernel
3027 * lock) we know the server hasn't removed the lease yet, we know
3028 * it's safe to take a reference: */
3029 atomic_inc(&dp->dl_count);
3030
Jeff Laytondff13992014-07-08 14:02:49 -04003031 /*
3032 * If the dl_time != 0, then we know that it has already been
3033 * queued for a lease break. Don't queue it again.
3034 */
3035 if (dp->dl_time == 0) {
3036 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3037 dp->dl_time = get_seconds();
3038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
NeilBrown6282cd52014-06-04 17:39:26 +10003040 block_delegations(&dp->dl_fh);
3041
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003042 nfsd4_cb_recall(dp);
3043}
3044
Jeff Layton1c8c6012013-06-21 08:58:15 -04003045/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003046static void nfsd_break_deleg_cb(struct file_lock *fl)
3047{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003048 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3049 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003050
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003051 if (!fp) {
3052 WARN(1, "(%p)->fl_owner NULL\n", fl);
3053 return;
3054 }
3055 if (fp->fi_had_conflict) {
3056 WARN(1, "duplicate break on %p\n", fp);
3057 return;
3058 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003059 /*
3060 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003061 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003062 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003063 */
3064 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
Benny Halevycdc97502014-05-30 09:09:30 -04003066 spin_lock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003067 fp->fi_had_conflict = true;
3068 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3069 nfsd_break_one_deleg(dp);
Benny Halevycdc97502014-05-30 09:09:30 -04003070 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071}
3072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073static
3074int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3075{
3076 if (arg & F_UNLCK)
3077 return lease_modify(onlist, arg);
3078 else
3079 return -EAGAIN;
3080}
3081
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003082static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003083 .lm_break = nfsd_break_deleg_cb,
3084 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085};
3086
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003087static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3088{
3089 if (nfsd4_has_session(cstate))
3090 return nfs_ok;
3091 if (seqid == so->so_seqid - 1)
3092 return nfserr_replay_me;
3093 if (seqid == so->so_seqid)
3094 return nfs_ok;
3095 return nfserr_bad_seqid;
3096}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
Jeff Layton4b24ca72014-06-30 11:48:44 -04003098static __be32 lookup_clientid(clientid_t *clid,
3099 struct nfsd4_compound_state *cstate,
3100 struct nfsd_net *nn)
3101{
3102 struct nfs4_client *found;
3103
3104 if (cstate->clp) {
3105 found = cstate->clp;
3106 if (!same_clid(&found->cl_clientid, clid))
3107 return nfserr_stale_clientid;
3108 return nfs_ok;
3109 }
3110
3111 if (STALE_CLIENTID(clid, nn))
3112 return nfserr_stale_clientid;
3113
3114 /*
3115 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3116 * cached already then we know this is for is for v4.0 and "sessions"
3117 * will be false.
3118 */
3119 WARN_ON_ONCE(cstate->session);
3120 found = find_confirmed_client(clid, false, nn);
3121 if (!found)
3122 return nfserr_expired;
3123
3124 /* Cache the nfs4_client in cstate! */
3125 cstate->clp = found;
3126 atomic_inc(&found->cl_refcount);
3127 return nfs_ok;
3128}
3129
Al Virob37ad282006-10-19 23:28:59 -07003130__be32
Andy Adamson66689582009-04-03 08:28:45 +03003131nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003132 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 clientid_t *clientid = &open->op_clientid;
3135 struct nfs4_client *clp = NULL;
3136 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003137 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003138 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003140 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003142 /*
3143 * In case we need it later, after we've already created the
3144 * file and don't want to risk a further failure:
3145 */
3146 open->op_file = nfsd4_alloc_file();
3147 if (open->op_file == NULL)
3148 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Trond Myklebust2d91e892014-06-30 11:48:46 -04003150 status = lookup_clientid(clientid, cstate, nn);
3151 if (status)
3152 return status;
3153 clp = cstate->clp;
3154
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003155 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003156 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003157 open->op_openowner = oo;
3158 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003159 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003161 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003162 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003163 release_openowner(oo);
3164 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003165 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003166 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003167 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3168 if (status)
3169 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003170 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003171new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003172 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003173 if (oo == NULL)
3174 return nfserr_jukebox;
3175 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003176alloc_stateid:
3177 open->op_stp = nfs4_alloc_stateid(clp);
3178 if (!open->op_stp)
3179 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003180 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181}
3182
Al Virob37ad282006-10-19 23:28:59 -07003183static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003184nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3185{
3186 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3187 return nfserr_openmode;
3188 else
3189 return nfs_ok;
3190}
3191
Daniel Mackc47d8322011-05-16 16:38:14 +02003192static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003193{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003194 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3195}
3196
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003197static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003198{
3199 struct nfs4_stid *ret;
3200
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003201 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003202 if (!ret)
3203 return NULL;
3204 return delegstateid(ret);
3205}
3206
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003207static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3208{
3209 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3210 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3211}
3212
Al Virob37ad282006-10-19 23:28:59 -07003213static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003214nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003215 struct nfs4_delegation **dp)
3216{
3217 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003218 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07003219
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003220 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07003221 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003222 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003223 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07003224 status = nfs4_check_delegmode(*dp, flags);
3225 if (status)
3226 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003227out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003228 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003229 return nfs_ok;
3230 if (status)
3231 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003232 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003233 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003234}
3235
Al Virob37ad282006-10-19 23:28:59 -07003236static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003237nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003239 struct nfs4_ol_stateid *local;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003240 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
Trond Myklebust1d31a252014-07-10 14:07:25 -04003242 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003243 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 /* ignore lock owners */
3245 if (local->st_stateowner->so_is_open_owner == 0)
3246 continue;
3247 /* remember if we have seen this open owner */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003248 if (local->st_stateowner == &oo->oo_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 *stpp = local;
3250 /* check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003251 if (!test_share(local, open)) {
3252 spin_unlock(&fp->fi_lock);
J. Bruce Fields77eaae82011-09-02 12:08:20 -04003253 return nfserr_share_denied;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003256 spin_unlock(&fp->fi_lock);
J. Bruce Fields77eaae82011-09-02 12:08:20 -04003257 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258}
3259
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003260static inline int nfs4_access_to_access(u32 nfs4_access)
3261{
3262 int flags = 0;
3263
3264 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3265 flags |= NFSD_MAY_READ;
3266 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3267 flags |= NFSD_MAY_WRITE;
3268 return flags;
3269}
3270
Al Virob37ad282006-10-19 23:28:59 -07003271static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3273 struct nfsd4_open *open)
3274{
3275 struct iattr iattr = {
3276 .ia_valid = ATTR_SIZE,
3277 .ia_size = 0,
3278 };
3279 if (!open->op_truncate)
3280 return 0;
3281 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003282 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3284}
3285
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003286static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3287 struct svc_fh *cur_fh, struct nfsd4_open *open)
3288{
Trond Myklebustde186432014-07-10 14:07:26 -04003289 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003290 __be32 status;
3291 int oflag = nfs4_access_to_omode(open->op_share_access);
3292 int access = nfs4_access_to_access(open->op_share_access);
3293
Trond Myklebustde186432014-07-10 14:07:26 -04003294 spin_lock(&fp->fi_lock);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003295 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003296 spin_unlock(&fp->fi_lock);
3297 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003298 if (status)
3299 goto out;
Trond Myklebustde186432014-07-10 14:07:26 -04003300 spin_lock(&fp->fi_lock);
3301 if (!fp->fi_fds[oflag]) {
3302 fp->fi_fds[oflag] = filp;
3303 filp = NULL;
3304 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003305 }
Jeff Layton12659652014-07-10 14:07:28 -04003306 status = nfs4_file_get_access(fp, open->op_share_access);
Trond Myklebustde186432014-07-10 14:07:26 -04003307 spin_unlock(&fp->fi_lock);
3308 if (filp)
3309 fput(filp);
Jeff Layton12659652014-07-10 14:07:28 -04003310 if (status)
3311 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003312
3313 status = nfsd4_truncate(rqstp, cur_fh, open);
3314 if (status)
3315 goto out_put_access;
3316
3317 return nfs_ok;
3318
3319out_put_access:
Jeff Layton12659652014-07-10 14:07:28 -04003320 nfs4_file_put_access(fp, open->op_share_access);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003321out:
3322 return status;
3323}
3324
Al Virob37ad282006-10-19 23:28:59 -07003325static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003326nfs4_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 -07003327{
J. Bruce Fieldsb6d2f1c2011-10-10 17:44:19 -04003328 u32 op_share_access = open->op_share_access;
Al Virob37ad282006-10-19 23:28:59 -07003329 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003331 if (!test_access(op_share_access, stp))
Casey Bodley0c12eaf2011-07-23 14:58:10 -04003332 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003333 else
3334 status = nfsd4_truncate(rqstp, cur_fh, open);
3335
3336 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 return status;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003338
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 /* remember the open */
Jeff Layton82c5ff12012-05-11 09:45:13 -04003340 set_access(op_share_access, stp);
Jeff Laytonce0fc432012-05-11 09:45:14 -04003341 set_deny(open->op_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 return nfs_ok;
3343}
3344
3345
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003347nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003349 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350}
3351
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003352/* Should we give out recallable state?: */
3353static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3354{
3355 if (clp->cl_cb_state == NFSD4_CB_UP)
3356 return true;
3357 /*
3358 * In the sessions case, since we don't have to establish a
3359 * separate connection for callbacks, we assume it's OK
3360 * until we hear otherwise:
3361 */
3362 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3363}
3364
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003365static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
3366{
3367 struct file_lock *fl;
3368
3369 fl = locks_alloc_lock();
3370 if (!fl)
3371 return NULL;
3372 locks_init_lock(fl);
3373 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003374 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003375 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3376 fl->fl_end = OFFSET_MAX;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003377 fl->fl_owner = (fl_owner_t)(dp->dl_file);
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003378 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003379 return fl;
3380}
3381
J. Bruce Fields99c41512013-05-21 16:21:25 -04003382static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003383{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003384 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003385 struct file_lock *fl;
3386 int status;
3387
J. Bruce Fields99c41512013-05-21 16:21:25 -04003388 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003389 if (!fl)
3390 return -ENOMEM;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003391 fl->fl_file = find_readable_file(fp);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003392 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003393 if (status)
3394 goto out_free;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003395 fp->fi_lease = fl;
Trond Myklebustde186432014-07-10 14:07:26 -04003396 fp->fi_deleg_file = fl->fl_file;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003397 atomic_set(&fp->fi_delegees, 1);
Benny Halevycdc97502014-05-30 09:09:30 -04003398 spin_lock(&state_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04003399 hash_delegation_locked(dp, fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003400 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003401 return 0;
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003402out_free:
Trond Myklebustde186432014-07-10 14:07:26 -04003403 if (fl->fl_file)
3404 fput(fl->fl_file);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003405 locks_free_lock(fl);
3406 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003407}
3408
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003409static int nfs4_set_delegation(struct nfs4_delegation *dp, struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003410{
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003411 if (fp->fi_had_conflict)
3412 return -EAGAIN;
3413 get_nfs4_file(fp);
3414 dp->dl_file = fp;
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003415 if (!fp->fi_lease)
3416 return nfs4_setlease(dp);
Benny Halevycdc97502014-05-30 09:09:30 -04003417 spin_lock(&state_lock);
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003418 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003419 if (fp->fi_had_conflict) {
Benny Halevycdc97502014-05-30 09:09:30 -04003420 spin_unlock(&state_lock);
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003421 return -EAGAIN;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003422 }
Benny Halevy931ee562014-05-30 09:09:27 -04003423 hash_delegation_locked(dp, fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003424 spin_unlock(&state_lock);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003425 return 0;
3426}
3427
Benny Halevy4aa89132012-02-21 14:16:44 -08003428static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3429{
3430 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3431 if (status == -EAGAIN)
3432 open->op_why_no_deleg = WND4_CONTENTION;
3433 else {
3434 open->op_why_no_deleg = WND4_RESOURCE;
3435 switch (open->op_deleg_want) {
3436 case NFS4_SHARE_WANT_READ_DELEG:
3437 case NFS4_SHARE_WANT_WRITE_DELEG:
3438 case NFS4_SHARE_WANT_ANY_DELEG:
3439 break;
3440 case NFS4_SHARE_WANT_CANCEL:
3441 open->op_why_no_deleg = WND4_CANCELLED;
3442 break;
3443 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003444 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003445 }
3446 }
3447}
3448
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449/*
3450 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003451 *
3452 * Note we don't support write delegations, and won't until the vfs has
3453 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 */
3455static void
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003456nfs4_open_delegation(struct net *net, struct svc_fh *fh,
3457 struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458{
3459 struct nfs4_delegation *dp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003460 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003461 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003462 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003464 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003465 open->op_recall = 0;
3466 switch (open->op_claim_type) {
3467 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003468 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003469 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003470 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3471 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003472 break;
3473 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003474 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003475 /*
3476 * Let's not give out any delegations till everyone's
3477 * had the chance to reclaim theirs....
3478 */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003479 if (locks_in_grace(net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003480 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003481 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003482 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003483 /*
3484 * Also, if the file was opened for write or
3485 * create, there's a good chance the client's
3486 * about to write to it, resulting in an
3487 * immediate recall (since we don't support
3488 * write delegations):
3489 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003490 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003491 goto out_no_deleg;
3492 if (open->op_create == NFS4_OPEN_CREATE)
3493 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003494 break;
3495 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003496 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003497 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003498 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003499 if (dp == NULL)
3500 goto out_no_deleg;
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003501 status = nfs4_set_delegation(dp, stp->st_file);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003502 if (status)
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003503 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003505 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003507 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003508 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003509 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003510 return;
3511out_free:
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003512 destroy_delegation(dp);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003513out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003514 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3515 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003516 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003517 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003518 open->op_recall = 1;
3519 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003520
3521 /* 4.1 client asking for a delegation? */
3522 if (open->op_deleg_want)
3523 nfsd4_open_deleg_none_ext(open, status);
3524 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525}
3526
Benny Halevye27f49c2012-02-21 14:16:54 -08003527static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3528 struct nfs4_delegation *dp)
3529{
3530 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3531 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3532 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3533 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3534 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3535 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3536 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3537 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3538 }
3539 /* Otherwise the client must be confused wanting a delegation
3540 * it already has, therefore we don't return
3541 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3542 */
3543}
3544
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545/*
3546 * called with nfs4_lock_state() held.
3547 */
Al Virob37ad282006-10-19 23:28:59 -07003548__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3550{
Andy Adamson66689582009-04-03 08:28:45 +03003551 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003552 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 struct nfs4_file *fp = NULL;
3554 struct inode *ino = current_fh->fh_dentry->d_inode;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003555 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003556 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003557 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 /*
3560 * Lookup file; if found, lookup stateid and check open request,
3561 * and check for delegations in the process of being recalled.
3562 * If not found, create the nfs4_file struct
3563 */
Trond Myklebust950e0112014-06-30 11:48:31 -04003564 fp = find_or_add_file(ino, open->op_file);
3565 if (fp != open->op_file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 if ((status = nfs4_check_open(fp, open, &stp)))
3567 goto out;
J. Bruce Fields41d22662013-03-21 15:49:47 -04003568 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003569 if (status)
3570 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003572 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003573 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003574 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003575 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003576 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 }
3578
3579 /*
3580 * OPEN the file, or upgrade an existing OPEN.
3581 * If truncate fails, the OPEN fails.
3582 */
3583 if (stp) {
3584 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003585 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 if (status)
3587 goto out;
3588 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003589 status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
NeilBrown567d9822005-06-23 22:02:53 -07003590 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 goto out;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003592 stp = open->op_stp;
3593 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003594 init_open_stateid(stp, fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003596 update_stateid(&stp->st_stid.sc_stateid);
3597 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598
Benny Halevyd24433c2012-02-16 20:57:17 +02003599 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02003600 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3601 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3602 open->op_why_no_deleg = WND4_NOT_WANTED;
3603 goto nodeleg;
3604 }
3605 }
3606
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 /*
3608 * Attempt to hand out a delegation. No error return, because the
3609 * OPEN succeeds even if we fail.
3610 */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003611 nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003612nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 status = nfs_ok;
3614
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003615 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003616 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003618 /* 4.1 client trying to upgrade/downgrade delegation? */
3619 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003620 open->op_deleg_want)
3621 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003622
NeilBrown13cd2182005-06-23 22:03:10 -07003623 if (fp)
3624 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003625 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003626 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 /*
3628 * To finish the open response, we just need to set the rflags.
3629 */
3630 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003631 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003632 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3634
3635 return status;
3636}
3637
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003638void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3639{
3640 if (open->op_openowner) {
3641 struct nfs4_openowner *oo = open->op_openowner;
3642
3643 if (!list_empty(&oo->oo_owner.so_stateids))
3644 list_del_init(&oo->oo_close_lru);
3645 if (oo->oo_flags & NFS4_OO_NEW) {
3646 if (status) {
3647 release_openowner(oo);
3648 open->op_openowner = NULL;
3649 } else
3650 oo->oo_flags &= ~NFS4_OO_NEW;
3651 }
3652 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003653 if (open->op_file)
3654 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003655 if (open->op_stp)
J. Bruce Fieldsef798592012-08-29 10:32:54 -07003656 free_generic_stateid(open->op_stp);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003657}
3658
Al Virob37ad282006-10-19 23:28:59 -07003659__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003660nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3661 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662{
3663 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003664 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003665 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666
3667 nfs4_lock_state();
3668 dprintk("process_renew(%08x/%08x): starting\n",
3669 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003670 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003671 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003673 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003675 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003676 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 goto out;
3678 status = nfs_ok;
3679out:
3680 nfs4_unlock_state();
3681 return status;
3682}
3683
NeilBrowna76b4312005-06-23 22:04:01 -07003684static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003685nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07003686{
Jeff Layton33dcc482012-04-10 11:08:48 -04003687 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003688 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003689 return;
3690
NeilBrowna76b4312005-06-23 22:04:01 -07003691 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003692 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003693 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003694 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003695 /*
3696 * Now that every NFSv4 client has had the chance to recover and
3697 * to see the (possibly new, possibly shorter) lease time, we
3698 * can safely set the next grace time to the current lease time:
3699 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03003700 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003701}
3702
NeilBrownfd39ca92005-06-23 22:04:03 -07003703static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003704nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705{
3706 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003707 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 struct nfs4_delegation *dp;
3709 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003710 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003711 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
3713 nfs4_lock_state();
3714
3715 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003716 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03003717 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003718 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03003719 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 clp = list_entry(pos, struct nfs4_client, cl_lru);
3721 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3722 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003723 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 break;
3725 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003726 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03003727 dprintk("NFSD: client in use (clientid %08x)\n",
3728 clp->cl_clientid.cl_id);
3729 continue;
3730 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003731 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003732 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003733 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03003734 list_for_each_safe(pos, next, &reaplist) {
3735 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 dprintk("NFSD: purging unused client (clientid %08x)\n",
3737 clp->cl_clientid.cl_id);
3738 expire_client(clp);
3739 }
Benny Halevycdc97502014-05-30 09:09:30 -04003740 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003741 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03003743 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3744 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003746 t = dp->dl_time - cutoff;
3747 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 break;
3749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 list_move(&dp->dl_recall_lru, &reaplist);
3751 }
Benny Halevycdc97502014-05-30 09:09:30 -04003752 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 list_for_each_safe(pos, next, &reaplist) {
3754 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003755 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003757 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003758 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3759 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003760 t = oo->oo_time - cutoff;
3761 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 break;
3763 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003764 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04003766 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04003768 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769}
3770
Harvey Harrisona254b242008-02-20 12:49:00 -08003771static struct workqueue_struct *laundry_wq;
3772static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08003773
3774static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003775laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776{
3777 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003778 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3779 work);
3780 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3781 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003783 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003785 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786}
3787
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003788static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003789{
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003790 if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
3791 return nfserr_bad_stateid;
3792 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793}
3794
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003796access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003798 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3799 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3800 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801}
3802
3803static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003804access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003806 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3807 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808}
3809
3810static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003811__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812{
Al Virob37ad282006-10-19 23:28:59 -07003813 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
J. Bruce Fields02921912010-07-29 15:16:59 -04003815 /* For lock stateid's, we test the parent open, not the lock: */
3816 if (stp->st_openstp)
3817 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003818 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003820 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 goto out;
3822 status = nfs_ok;
3823out:
3824 return status;
3825}
3826
Al Virob37ad282006-10-19 23:28:59 -07003827static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003828check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003830 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003832 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003833 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 * conflicting state; so we must wait out the grace period. */
3835 return nfserr_grace;
3836 } else if (flags & WR_STATE)
3837 return nfs4_share_conflict(current_fh,
3838 NFS4_SHARE_DENY_WRITE);
3839 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3840 return nfs4_share_conflict(current_fh,
3841 NFS4_SHARE_DENY_READ);
3842}
3843
3844/*
3845 * Allow READ/WRITE during grace period on recovered state only for files
3846 * that are not able to provide mandatory locking.
3847 */
3848static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003849grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003851 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852}
3853
J. Bruce Fields81b82962011-08-23 11:03:29 -04003854/* Returns true iff a is later than b: */
3855static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3856{
Jim Rees1a9357f2013-05-17 17:33:00 -04003857 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04003858}
3859
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003860static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05003861{
Andy Adamson66689582009-04-03 08:28:45 +03003862 /*
3863 * When sessions are used the stateid generation number is ignored
3864 * when it is zero.
3865 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04003866 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04003867 return nfs_ok;
3868
3869 if (in->si_generation == ref->si_generation)
3870 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03003871
J. Bruce Fields0836f582008-01-26 19:08:12 -05003872 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003873 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05003874 return nfserr_bad_stateid;
3875 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04003876 * However, we could see a stateid from the past, even from a
3877 * non-buggy client. For example, if the client sends a lock
3878 * while some IO is outstanding, the lock may bump si_generation
3879 * while the IO is still in flight. The client could avoid that
3880 * situation by waiting for responses on all the IO requests,
3881 * but better performance may result in retrying IO that
3882 * receives an old_stateid error if requests are rarely
3883 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05003884 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003885 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05003886}
3887
Chuck Lever7df302f2012-05-29 13:56:37 -04003888static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04003889{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003890 struct nfs4_stid *s;
3891 struct nfs4_ol_stateid *ols;
3892 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003893
Chuck Lever7df302f2012-05-29 13:56:37 -04003894 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3895 return nfserr_bad_stateid;
3896 /* Client debugging aid. */
3897 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
3898 char addr_str[INET6_ADDRSTRLEN];
3899 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
3900 sizeof(addr_str));
3901 pr_warn_ratelimited("NFSD: client %s testing state ID "
3902 "with incorrect client ID\n", addr_str);
3903 return nfserr_bad_stateid;
3904 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003905 s = find_stateid(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003906 if (!s)
Chuck Lever7df302f2012-05-29 13:56:37 -04003907 return nfserr_bad_stateid;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04003908 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04003909 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003910 return status;
J. Bruce Fields23340032013-04-09 17:42:28 -04003911 switch (s->sc_type) {
3912 case NFS4_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003913 return nfs_ok;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003914 case NFS4_REVOKED_DELEG_STID:
3915 return nfserr_deleg_revoked;
J. Bruce Fields23340032013-04-09 17:42:28 -04003916 case NFS4_OPEN_STID:
3917 case NFS4_LOCK_STID:
3918 ols = openlockstateid(s);
3919 if (ols->st_stateowner->so_is_open_owner
3920 && !(openowner(ols->st_stateowner)->oo_flags
3921 & NFS4_OO_CONFIRMED))
3922 return nfserr_bad_stateid;
3923 return nfs_ok;
3924 default:
3925 printk("unknown stateid type %x\n", s->sc_type);
3926 case NFS4_CLOSED_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003927 return nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04003928 }
Bryan Schumaker17456802011-07-13 10:50:48 -04003929}
3930
Trond Myklebust2dd6e452014-06-30 11:48:43 -04003931static __be32
3932nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
3933 stateid_t *stateid, unsigned char typemask,
3934 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003935{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04003936 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003937
3938 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3939 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003940 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04003941 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04003942 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04003943 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003944 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04003945 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04003946 if (status)
3947 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003948 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003949 if (!*s)
3950 return nfserr_bad_stateid;
3951 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003952}
3953
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954/*
3955* Checks for stateid operations
3956*/
Al Virob37ad282006-10-19 23:28:59 -07003957__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003958nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03003959 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960{
J. Bruce Fields69064a22011-09-09 11:54:57 -04003961 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003962 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03003964 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003966 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04003967 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003968 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 if (filpp)
3971 *filpp = NULL;
3972
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003973 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 return nfserr_grace;
3975
3976 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003977 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978
Trond Myklebust14bcab12014-04-18 14:44:07 -04003979 nfs4_lock_state();
3980
Trond Myklebust2dd6e452014-06-30 11:48:43 -04003981 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003982 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04003983 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003984 if (status)
Trond Myklebust14bcab12014-04-18 14:44:07 -04003985 goto out;
J. Bruce Fields69064a22011-09-09 11:54:57 -04003986 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
3987 if (status)
3988 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003989 switch (s->sc_type) {
3990 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04003991 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08003992 status = nfs4_check_delegmode(dp, flags);
3993 if (status)
3994 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02003995 if (filpp) {
Trond Myklebust14bcab12014-04-18 14:44:07 -04003996 file = dp->dl_file->fi_deleg_file;
3997 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003998 WARN_ON_ONCE(1);
3999 status = nfserr_serverfault;
4000 goto out;
4001 }
Trond Myklebustde186432014-07-10 14:07:26 -04004002 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004003 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004004 break;
4005 case NFS4_OPEN_STID:
4006 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004007 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004008 status = nfs4_check_fh(current_fh, stp);
4009 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004011 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004012 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004014 status = nfs4_check_openmode(stp, flags);
4015 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004017 if (filpp) {
4018 if (flags & RD_STATE)
Trond Myklebust14bcab12014-04-18 14:44:07 -04004019 file = find_readable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004020 else
Trond Myklebust14bcab12014-04-18 14:44:07 -04004021 file = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004022 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004023 break;
4024 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004025 status = nfserr_bad_stateid;
4026 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 }
4028 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004029 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004030 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031out:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004032 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 return status;
4034}
4035
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004036static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004037nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004038{
J. Bruce Fieldsa1b8ff42014-05-20 15:55:21 -04004039 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4040
4041 if (check_for_locks(stp->st_file, lo))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004042 return nfserr_locks_held;
Jeff Laytonc53530d2014-06-30 11:48:39 -04004043 release_lockowner_if_empty(lo);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004044 return nfs_ok;
4045}
4046
4047/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004048 * Test if the stateid is valid
4049 */
4050__be32
4051nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4052 struct nfsd4_test_stateid *test_stateid)
4053{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004054 struct nfsd4_test_stateid_id *stateid;
4055 struct nfs4_client *cl = cstate->session->se_client;
4056
4057 nfs4_lock_state();
4058 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004059 stateid->ts_id_status =
4060 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004061 nfs4_unlock_state();
4062
Bryan Schumaker17456802011-07-13 10:50:48 -04004063 return nfs_ok;
4064}
4065
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004066__be32
4067nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4068 struct nfsd4_free_stateid *free_stateid)
4069{
4070 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004071 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004072 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004073 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004074 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004075
4076 nfs4_lock_state();
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004077 s = find_stateid(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004078 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004079 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004080 switch (s->sc_type) {
4081 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004082 ret = nfserr_locks_held;
4083 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004084 case NFS4_OPEN_STID:
4085 case NFS4_LOCK_STID:
4086 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4087 if (ret)
4088 goto out;
4089 if (s->sc_type == NFS4_LOCK_STID)
4090 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4091 else
4092 ret = nfserr_locks_held;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004093 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004094 case NFS4_REVOKED_DELEG_STID:
4095 dp = delegstateid(s);
4096 destroy_revoked_delegation(dp);
4097 ret = nfs_ok;
4098 break;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004099 default:
4100 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004101 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004102out:
4103 nfs4_unlock_state();
4104 return ret;
4105}
4106
NeilBrown4c4cd222005-07-07 17:59:27 -07004107static inline int
4108setlkflg (int type)
4109{
4110 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4111 RD_STATE : WR_STATE;
4112}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004114static __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 -04004115{
4116 struct svc_fh *current_fh = &cstate->current_fh;
4117 struct nfs4_stateowner *sop = stp->st_stateowner;
4118 __be32 status;
4119
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004120 status = nfsd4_check_seqid(cstate, sop, seqid);
4121 if (status)
4122 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004123 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4124 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004125 /*
4126 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004127 * nfserr_replay_me from the previous step, and
4128 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004129 */
4130 return nfserr_bad_stateid;
4131 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4132 if (status)
4133 return status;
4134 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004135}
4136
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137/*
4138 * Checks for sequence id mutating operations.
4139 */
Al Virob37ad282006-10-19 23:28:59 -07004140static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004141nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004142 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004143 struct nfs4_ol_stateid **stpp,
4144 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004146 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004147 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004148 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004150 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4151 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004152
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004154 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004155 if (status)
4156 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004157 stp = openlockstateid(s);
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04004158 if (!nfsd4_has_session(cstate))
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004159 cstate->replay_owner = stp->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004161 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4162 if (!status)
4163 *stpp = stp;
4164 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004165}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004166
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004167static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4168 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004169{
4170 __be32 status;
4171 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004173 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004174 NFS4_OPEN_STID, stpp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004175 if (status)
4176 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004177 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004178 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07004179 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004180 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181}
4182
Al Virob37ad282006-10-19 23:28:59 -07004183__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004184nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004185 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186{
Al Virob37ad282006-10-19 23:28:59 -07004187 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004188 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004189 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004190 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191
Al Viroa6a9f182013-09-16 10:57:01 -04004192 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4193 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004195 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004196 if (status)
4197 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
4199 nfs4_lock_state();
4200
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004201 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004202 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004203 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004204 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004205 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004206 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004207 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004208 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004209 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004210 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004211 update_stateid(&stp->st_stid.sc_stateid);
4212 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004213 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004214 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004215
Jeff Layton2a4317c2012-03-21 16:42:43 -04004216 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004217 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004219 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004220 if (!cstate->replay_owner)
4221 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 return status;
4223}
4224
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004225static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004227 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004228 return;
Jeff Layton12659652014-07-10 14:07:28 -04004229 nfs4_file_put_access(stp->st_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004230 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004231}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004232
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004233static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4234{
4235 switch (to_access) {
4236 case NFS4_SHARE_ACCESS_READ:
4237 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4238 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4239 break;
4240 case NFS4_SHARE_ACCESS_WRITE:
4241 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4242 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4243 break;
4244 case NFS4_SHARE_ACCESS_BOTH:
4245 break;
4246 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004247 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 }
4249}
4250
4251static void
Jeff Laytonce0fc432012-05-11 09:45:14 -04004252reset_union_bmap_deny(unsigned long deny, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253{
4254 int i;
4255 for (i = 0; i < 4; i++) {
4256 if ((i & deny) != i)
Jeff Laytonce0fc432012-05-11 09:45:14 -04004257 clear_deny(i, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 }
4259}
4260
Al Virob37ad282006-10-19 23:28:59 -07004261__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004262nfsd4_open_downgrade(struct svc_rqst *rqstp,
4263 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004264 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265{
Al Virob37ad282006-10-19 23:28:59 -07004266 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004267 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004268 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269
Al Viroa6a9f182013-09-16 10:57:01 -04004270 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4271 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004273 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004274 if (od->od_deleg_want)
4275 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4276 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
4278 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004279 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004280 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004281 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004284 if (!test_access(od->od_share_access, stp)) {
4285 dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 stp->st_access_bmap, od->od_share_access);
4287 goto out;
4288 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004289 if (!test_deny(od->od_share_deny, stp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
4291 stp->st_deny_bmap, od->od_share_deny);
4292 goto out;
4293 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004294 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295
Jeff Laytonce0fc432012-05-11 09:45:14 -04004296 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004298 update_stateid(&stp->st_stid.sc_stateid);
4299 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 status = nfs_ok;
4301out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004302 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004303 if (!cstate->replay_owner)
4304 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 return status;
4306}
4307
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004308static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4309{
Trond Myklebustacf92952014-06-30 11:48:37 -04004310 struct nfs4_client *clp = s->st_stid.sc_client;
4311 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4312
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004313 s->st_stid.sc_type = NFS4_CLOSED_STID;
Trond Myklebustacf92952014-06-30 11:48:37 -04004314 unhash_open_stateid(s);
4315
4316 if (clp->cl_minorversion) {
4317 free_generic_stateid(s);
4318 if (list_empty(&oo->oo_owner.so_stateids))
4319 release_openowner(oo);
4320 } else {
4321 oo->oo_last_closed_stid = s;
4322 /*
4323 * In the 4.0 case we need to keep the owners around a
4324 * little while to handle CLOSE replay.
4325 */
4326 if (list_empty(&oo->oo_owner.so_stateids))
4327 move_to_close_lru(oo, clp->net);
4328 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004329}
4330
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331/*
4332 * nfs4_unlock_state() called after encode
4333 */
Al Virob37ad282006-10-19 23:28:59 -07004334__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004335nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004336 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337{
Al Virob37ad282006-10-19 23:28:59 -07004338 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004339 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004340 struct net *net = SVC_NET(rqstp);
4341 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
Al Viroa6a9f182013-09-16 10:57:01 -04004343 dprintk("NFSD: nfsd4_close on file %pd\n",
4344 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
4346 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004347 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4348 &close->cl_stateid,
4349 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004350 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004351 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004352 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004354 update_stateid(&stp->st_stid.sc_stateid);
4355 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004357 nfsd4_close_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004359 if (!cstate->replay_owner)
4360 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 return status;
4362}
4363
Al Virob37ad282006-10-19 23:28:59 -07004364__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004365nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4366 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004368 struct nfs4_delegation *dp;
4369 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004370 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004371 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004372 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004374 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004375 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376
4377 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004378 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004379 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004380 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004381 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004382 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004383 if (status)
4384 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004385
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004386 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004388 nfs4_unlock_state();
4389
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 return status;
4391}
4392
4393
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395
Benny Halevy87df4de2008-12-15 19:42:03 +02004396static inline u64
4397end_offset(u64 start, u64 len)
4398{
4399 u64 end;
4400
4401 end = start + len;
4402 return end >= start ? end: NFS4_MAX_UINT64;
4403}
4404
4405/* last octet in a range */
4406static inline u64
4407last_byte_offset(u64 start, u64 len)
4408{
4409 u64 end;
4410
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004411 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004412 end = start + len;
4413 return end > start ? end - 1: NFS4_MAX_UINT64;
4414}
4415
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416/*
4417 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4418 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4419 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4420 * locking, this prevents us from being completely protocol-compliant. The
4421 * real solution to this problem is to start using unsigned file offsets in
4422 * the VFS, but this is a very deep change!
4423 */
4424static inline void
4425nfs4_transform_lock_offset(struct file_lock *lock)
4426{
4427 if (lock->fl_start < 0)
4428 lock->fl_start = OFFSET_MAX;
4429 if (lock->fl_end < 0)
4430 lock->fl_end = OFFSET_MAX;
4431}
4432
NeilBrownd5b90262006-04-10 22:55:22 -07004433/* Hack!: For now, we're defining this just so we can use a pointer to it
4434 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004435static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004436};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
4438static inline void
4439nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4440{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004441 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
NeilBrownd5b90262006-04-10 22:55:22 -07004443 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004444 lo = (struct nfs4_lockowner *) fl->fl_owner;
4445 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4446 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004447 if (!deny->ld_owner.data)
4448 /* We just don't care that much */
4449 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004450 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4451 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004452 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004453nevermind:
4454 deny->ld_owner.len = 0;
4455 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004456 deny->ld_clientid.cl_boot = 0;
4457 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 }
4459 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004460 deny->ld_length = NFS4_MAX_UINT64;
4461 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4463 deny->ld_type = NFS4_READ_LT;
4464 if (fl->fl_type != F_RDLCK)
4465 deny->ld_type = NFS4_WRITE_LT;
4466}
4467
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004468static struct nfs4_lockowner *
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004469find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4470 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471{
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004472 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4473 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004475 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4476 if (so->so_is_open_owner)
4477 continue;
4478 if (!same_owner_str(so, owner, clid))
4479 continue;
4480 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 }
4482 return NULL;
4483}
4484
4485/*
4486 * Alloc a lock owner structure.
4487 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004488 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004490 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004492static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004493alloc_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 -04004494 struct nfs4_lockowner *lo;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004495 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004497 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4498 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004500 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4501 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07004502 /* It is the openowner seqid that will be incremented in encode in the
4503 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004504 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004505 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004506 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507}
4508
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004509static struct nfs4_ol_stateid *
4510alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004512 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004513 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514
J. Bruce Fields996e0932011-10-17 11:14:48 -04004515 stp = nfs4_alloc_stateid(clp);
NeilBrown5ac049a2005-06-23 22:03:03 -07004516 if (stp == NULL)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04004517 return NULL;
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004518 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004519 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4520 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004521 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 stp->st_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004523 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004525 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004526 list_add(&stp->st_locks, &open_stp->st_locks);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004527 spin_lock(&fp->fi_lock);
4528 list_add(&stp->st_perfile, &fp->fi_stateids);
4529 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 return stp;
4531}
4532
Jeff Laytonc53530d2014-06-30 11:48:39 -04004533static struct nfs4_ol_stateid *
4534find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4535{
4536 struct nfs4_ol_stateid *lst;
4537
4538 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
4539 if (lst->st_file == fp)
4540 return lst;
4541 }
4542 return NULL;
4543}
4544
4545
NeilBrownfd39ca92005-06-23 22:04:03 -07004546static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547check_lock_length(u64 offset, u64 length)
4548{
Benny Halevy87df4de2008-12-15 19:42:03 +02004549 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 LOFF_OVERFLOW(offset, length)));
4551}
4552
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004553static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004554{
4555 struct nfs4_file *fp = lock_stp->st_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004556
Jeff Layton82c5ff12012-05-11 09:45:13 -04004557 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004558 return;
Jeff Layton12659652014-07-10 14:07:28 -04004559 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004560 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004561}
4562
J. Bruce Fields2355c592012-04-25 16:56:22 -04004563static __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 -04004564{
4565 struct nfs4_file *fi = ost->st_file;
4566 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4567 struct nfs4_client *cl = oo->oo_owner.so_client;
4568 struct nfs4_lockowner *lo;
4569 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004570 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004571
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004572 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004573 if (!lo) {
4574 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4575 &lock->v.new.owner);
4576 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4577 if (lo == NULL)
4578 return nfserr_jukebox;
4579 } else {
4580 /* with an existing lockowner, seqids must be the same */
4581 if (!cstate->minorversion &&
4582 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004583 return nfserr_bad_seqid;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004584 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004585
4586 *lst = find_lock_stateid(lo, fi);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004587 if (*lst == NULL) {
Jeff Laytonc53530d2014-06-30 11:48:39 -04004588 *lst = alloc_init_lock_stateid(lo, fi, ost);
4589 if (*lst == NULL) {
4590 release_lockowner_if_empty(lo);
4591 return nfserr_jukebox;
4592 }
4593 *new = true;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004594 }
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004595 return nfs_ok;
4596}
4597
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598/*
4599 * LOCK operation
4600 */
Al Virob37ad282006-10-19 23:28:59 -07004601__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004602nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004603 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004605 struct nfs4_openowner *open_sop = NULL;
4606 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004607 struct nfs4_ol_stateid *lock_stp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004608 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004609 struct file_lock *file_lock = NULL;
4610 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004611 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004612 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004613 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004614 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004615 struct net *net = SVC_NET(rqstp);
4616 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617
4618 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4619 (long long) lock->lk_offset,
4620 (long long) lock->lk_length);
4621
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 if (check_lock_length(lock->lk_offset, lock->lk_length))
4623 return nfserr_inval;
4624
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004625 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004626 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004627 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4628 return status;
4629 }
4630
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 nfs4_lock_state();
4632
4633 if (lock->lk_is_new) {
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004634 struct nfs4_ol_stateid *open_stp = NULL;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004635
4636 if (nfsd4_has_session(cstate))
4637 /* See rfc 5661 18.10.3: given clientid is ignored: */
4638 memcpy(&lock->v.new.clientid,
4639 &cstate->session->se_client->cl_clientid,
4640 sizeof(clientid_t));
4641
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004643 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004647 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 lock->lk_new_open_seqid,
4649 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004650 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07004651 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004653 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004654 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004655 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004656 &lock->v.new.clientid))
4657 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004658 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4659 &lock_stp, &new_state);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004660 } else
Benny Halevydd453df2009-04-03 08:28:41 +03004661 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004662 lock->lk_old_lock_seqid,
4663 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004664 NFS4_LOCK_STID, &lock_stp, nn);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004665 if (status)
4666 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004667 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004669 lkflg = setlkflg(lock->lk_type);
4670 status = nfs4_check_openmode(lock_stp, lkflg);
4671 if (status)
4672 goto out;
4673
NeilBrown0dd395d2005-07-07 17:59:15 -07004674 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004675 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004676 goto out;
4677 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004678 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004679 goto out;
4680
Jeff Layton21179d82012-08-21 08:03:32 -04004681 file_lock = locks_alloc_lock();
4682 if (!file_lock) {
4683 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4684 status = nfserr_jukebox;
4685 goto out;
4686 }
4687
4688 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 switch (lock->lk_type) {
4690 case NFS4_READ_LT:
4691 case NFS4_READW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004692 filp = find_readable_file(lock_stp->st_file);
4693 if (filp)
4694 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton21179d82012-08-21 08:03:32 -04004695 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004696 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 case NFS4_WRITE_LT:
4698 case NFS4_WRITEW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004699 filp = find_writeable_file(lock_stp->st_file);
4700 if (filp)
4701 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton21179d82012-08-21 08:03:32 -04004702 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004703 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 default:
4705 status = nfserr_inval;
4706 goto out;
4707 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004708 if (!filp) {
4709 status = nfserr_openmode;
4710 goto out;
4711 }
Jeff Layton21179d82012-08-21 08:03:32 -04004712 file_lock->fl_owner = (fl_owner_t)lock_sop;
4713 file_lock->fl_pid = current->tgid;
4714 file_lock->fl_file = filp;
4715 file_lock->fl_flags = FL_POSIX;
4716 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4717 file_lock->fl_start = lock->lk_offset;
4718 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4719 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720
Jeff Layton21179d82012-08-21 08:03:32 -04004721 conflock = locks_alloc_lock();
4722 if (!conflock) {
4723 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4724 status = nfserr_jukebox;
4725 goto out;
4726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727
Jeff Layton21179d82012-08-21 08:03:32 -04004728 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004729 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004731 update_stateid(&lock_stp->st_stid.sc_stateid);
4732 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004734 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004735 break;
4736 case (EAGAIN): /* conflock holds conflicting lock */
4737 status = nfserr_denied;
4738 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04004739 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004740 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 case (EDEADLK):
4742 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004743 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004744 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004745 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004746 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004747 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749out:
Trond Myklebustde186432014-07-10 14:07:26 -04004750 if (filp)
4751 fput(filp);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004752 if (status && new_state)
Jeff Laytonc53530d2014-06-30 11:48:39 -04004753 release_lock_stateid(lock_stp);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004754 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004755 if (!cstate->replay_owner)
4756 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004757 if (file_lock)
4758 locks_free_lock(file_lock);
4759 if (conflock)
4760 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 return status;
4762}
4763
4764/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004765 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4766 * so we do a temporary open here just to get an open file to pass to
4767 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4768 * inode operation.)
4769 */
Al Viro04da6e92012-04-13 00:00:04 -04004770static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004771{
4772 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04004773 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4774 if (!err) {
4775 err = nfserrno(vfs_test_lock(file, lock));
4776 nfsd_close(file);
4777 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004778 return err;
4779}
4780
4781/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 * LOCKT operation
4783 */
Al Virob37ad282006-10-19 23:28:59 -07004784__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004785nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4786 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787{
Jeff Layton21179d82012-08-21 08:03:32 -04004788 struct file_lock *file_lock = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004789 struct nfs4_lockowner *lo;
Al Virob37ad282006-10-19 23:28:59 -07004790 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004791 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004793 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 return nfserr_grace;
4795
4796 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4797 return nfserr_inval;
4798
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 nfs4_lock_state();
4800
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004801 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004802 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004803 if (status)
4804 goto out;
4805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004807 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
Jeff Layton21179d82012-08-21 08:03:32 -04004810 file_lock = locks_alloc_lock();
4811 if (!file_lock) {
4812 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4813 status = nfserr_jukebox;
4814 goto out;
4815 }
4816 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 switch (lockt->lt_type) {
4818 case NFS4_READ_LT:
4819 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004820 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 break;
4822 case NFS4_WRITE_LT:
4823 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004824 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 break;
4826 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04004827 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 status = nfserr_inval;
4829 goto out;
4830 }
4831
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004832 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004833 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04004834 file_lock->fl_owner = (fl_owner_t)lo;
4835 file_lock->fl_pid = current->tgid;
4836 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837
Jeff Layton21179d82012-08-21 08:03:32 -04004838 file_lock->fl_start = lockt->lt_offset;
4839 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840
Jeff Layton21179d82012-08-21 08:03:32 -04004841 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842
Jeff Layton21179d82012-08-21 08:03:32 -04004843 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04004844 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05004845 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04004846
Jeff Layton21179d82012-08-21 08:03:32 -04004847 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04004849 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 }
4851out:
4852 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004853 if (file_lock)
4854 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 return status;
4856}
4857
Al Virob37ad282006-10-19 23:28:59 -07004858__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004859nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004860 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004862 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004864 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004865 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07004866 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004867 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4868
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4870 (long long) locku->lu_offset,
4871 (long long) locku->lu_length);
4872
4873 if (check_lock_length(locku->lu_offset, locku->lu_length))
4874 return nfserr_inval;
4875
4876 nfs4_lock_state();
4877
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004878 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004879 &locku->lu_stateid, NFS4_LOCK_STID,
4880 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004881 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004883 filp = find_any_file(stp->st_file);
4884 if (!filp) {
4885 status = nfserr_lock_range;
4886 goto out;
4887 }
Jeff Layton21179d82012-08-21 08:03:32 -04004888 file_lock = locks_alloc_lock();
4889 if (!file_lock) {
4890 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4891 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04004892 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04004893 }
4894 locks_init_lock(file_lock);
4895 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04004896 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04004897 file_lock->fl_pid = current->tgid;
4898 file_lock->fl_file = filp;
4899 file_lock->fl_flags = FL_POSIX;
4900 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4901 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902
Jeff Layton21179d82012-08-21 08:03:32 -04004903 file_lock->fl_end = last_byte_offset(locku->lu_offset,
4904 locku->lu_length);
4905 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906
Jeff Layton21179d82012-08-21 08:03:32 -04004907 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07004908 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05004909 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 goto out_nfserr;
4911 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004912 update_stateid(&stp->st_stid.sc_stateid);
4913 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04004914fput:
4915 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004917 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04004918 if (!cstate->replay_owner)
4919 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004920 if (file_lock)
4921 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 return status;
4923
4924out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07004925 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04004926 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927}
4928
4929/*
4930 * returns
4931 * 1: locks held by lockowner
4932 * 0: no locks held by lockowner
4933 */
4934static int
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004935check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936{
4937 struct file_lock **flpp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004938 struct inode *inode = filp->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 int status = 0;
4940
Jeff Layton1c8c6012013-06-21 08:58:15 -04004941 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004943 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 status = 1;
4945 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 }
4948out:
Jeff Layton1c8c6012013-06-21 08:58:15 -04004949 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 return status;
4951}
4952
Al Virob37ad282006-10-19 23:28:59 -07004953__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004954nfsd4_release_lockowner(struct svc_rqst *rqstp,
4955 struct nfsd4_compound_state *cstate,
4956 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957{
4958 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Laytonfd449072014-06-30 11:48:41 -04004959 struct nfs4_stateowner *sop = NULL, *tmp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004960 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004961 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 struct xdr_netobj *owner = &rlockowner->rl_owner;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004963 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07004964 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004965 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966
4967 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4968 clid->cl_boot, clid->cl_id);
4969
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 nfs4_lock_state();
4971
Jeff Layton4b24ca72014-06-30 11:48:44 -04004972 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004973 if (status)
4974 goto out;
4975
NeilBrown3e9e3db2005-06-23 22:04:20 -07004976 status = nfserr_locks_held;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05004977
Jeff Laytonfd449072014-06-30 11:48:41 -04004978 /* Find the matching lock stateowner */
4979 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
4980 if (tmp->so_is_open_owner)
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004981 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04004982 if (same_owner_str(tmp, owner, clid)) {
4983 sop = tmp;
4984 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07004986 }
Jeff Laytonfd449072014-06-30 11:48:41 -04004987
4988 /* No matching owner found, maybe a replay? Just declare victory... */
4989 if (!sop) {
4990 status = nfs_ok;
4991 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 }
Jeff Laytonfd449072014-06-30 11:48:41 -04004993
4994 lo = lockowner(sop);
4995 /* see if there are still any locks associated with it */
4996 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
4997 if (check_for_locks(stp->st_file, lo))
4998 goto out;
4999 }
5000
5001 status = nfs_ok;
5002 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003out:
5004 nfs4_unlock_state();
5005 return status;
5006}
5007
5008static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005009alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010{
NeilBrowna55370a2005-06-23 22:03:52 -07005011 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012}
5013
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005014bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005015nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005016{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005017 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005018
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005019 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005020 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005021}
5022
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023/*
5024 * failure => all reset bets are off, nfserr_no_grace...
5025 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005026struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005027nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028{
5029 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005030 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031
NeilBrowna55370a2005-06-23 22:03:52 -07005032 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5033 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005034 if (crp) {
5035 strhashval = clientstr_hashval(name);
5036 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005037 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005038 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005039 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005040 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005041 }
5042 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043}
5044
Jeff Layton2a4317c2012-03-21 16:42:43 -04005045void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005046nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005047{
5048 list_del(&crp->cr_strhash);
5049 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005050 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005051}
5052
5053void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005054nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055{
5056 struct nfs4_client_reclaim *crp = NULL;
5057 int i;
5058
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005060 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5061 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005063 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 }
5065 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005066 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067}
5068
5069/*
5070 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005071struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005072nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073{
5074 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 struct nfs4_client_reclaim *crp = NULL;
5076
Jeff Layton278c9312012-11-12 15:00:52 -05005077 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078
Jeff Layton278c9312012-11-12 15:00:52 -05005079 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005080 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005081 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 return crp;
5083 }
5084 }
5085 return NULL;
5086}
5087
5088/*
5089* Called from OPEN. Look for clientid in reclaim list.
5090*/
Al Virob37ad282006-10-19 23:28:59 -07005091__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005092nfs4_check_open_reclaim(clientid_t *clid,
5093 struct nfsd4_compound_state *cstate,
5094 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005096 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005097
5098 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005099 status = lookup_clientid(clid, cstate, nn);
5100 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005101 return nfserr_reclaim_bad;
5102
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005103 if (nfsd4_client_record_check(cstate->clp))
5104 return nfserr_reclaim_bad;
5105
5106 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107}
5108
Bryan Schumaker65178db2011-11-01 13:35:21 -04005109#ifdef CONFIG_NFSD_FAULT_INJECTION
5110
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005111u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5112{
J. Bruce Fields221a6872013-04-01 22:23:49 -04005113 if (mark_client_expired(clp))
5114 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005115 expire_client(clp);
5116 return 1;
5117}
5118
Bryan Schumaker184c1842012-11-29 11:40:44 -05005119u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5120{
5121 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005122 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005123 printk(KERN_INFO "NFS Client: %s\n", buf);
5124 return 1;
5125}
5126
5127static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5128 const char *type)
5129{
5130 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005131 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005132 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5133}
5134
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005135static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5136 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005137{
5138 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005139 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005140 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005141 u64 count = 0;
5142
5143 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005144 list_for_each_entry_safe(stp, st_next,
5145 &oop->oo_owner.so_stateids, st_perstateowner) {
5146 list_for_each_entry_safe(lst, lst_next,
5147 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005148 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005149 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005150 if (++count == max)
5151 return count;
5152 }
5153 }
5154 }
5155
5156 return count;
5157}
5158
5159u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5160{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005161 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005162}
5163
Bryan Schumaker184c1842012-11-29 11:40:44 -05005164u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5165{
5166 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5167 nfsd_print_count(clp, count, "locked files");
5168 return count;
5169}
5170
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005171static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5172{
5173 struct nfs4_openowner *oop, *next;
5174 u64 count = 0;
5175
5176 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5177 if (func)
5178 func(oop);
5179 if (++count == max)
5180 break;
5181 }
5182
5183 return count;
5184}
5185
5186u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5187{
5188 return nfsd_foreach_client_open(clp, max, release_openowner);
5189}
5190
Bryan Schumaker184c1842012-11-29 11:40:44 -05005191u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5192{
5193 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5194 nfsd_print_count(clp, count, "open files");
5195 return count;
5196}
5197
Bryan Schumaker269de302012-11-29 11:40:42 -05005198static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5199 struct list_head *victims)
5200{
5201 struct nfs4_delegation *dp, *next;
5202 u64 count = 0;
5203
Benny Halevycdc97502014-05-30 09:09:30 -04005204 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005205 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005206 if (victims) {
5207 /*
5208 * It's not safe to mess with delegations that have a
5209 * non-zero dl_time. They might have already been broken
5210 * and could be processed by the laundromat outside of
5211 * the state_lock. Just leave them be.
5212 */
5213 if (dp->dl_time != 0)
5214 continue;
5215
5216 /*
5217 * Increment dl_time to ensure that delegation breaks
5218 * don't monkey with it now that we are.
5219 */
5220 ++dp->dl_time;
Bryan Schumaker269de302012-11-29 11:40:42 -05005221 list_move(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005222 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005223 if (++count == max)
5224 break;
5225 }
5226 return count;
5227}
5228
5229u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5230{
5231 struct nfs4_delegation *dp, *next;
5232 LIST_HEAD(victims);
5233 u64 count;
5234
Benny Halevycdc97502014-05-30 09:09:30 -04005235 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005236 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005237 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005238
5239 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005240 revoke_delegation(dp);
Bryan Schumaker269de302012-11-29 11:40:42 -05005241
5242 return count;
5243}
5244
5245u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5246{
Jeff Laytondff13992014-07-08 14:02:49 -04005247 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005248 LIST_HEAD(victims);
5249 u64 count;
5250
Benny Halevycdc97502014-05-30 09:09:30 -04005251 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005252 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005253 while (!list_empty(&victims)) {
5254 dp = list_first_entry(&victims, struct nfs4_delegation,
5255 dl_recall_lru);
5256 list_del_init(&dp->dl_recall_lru);
5257 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005258 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005259 }
Benny Halevycdc97502014-05-30 09:09:30 -04005260 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005261
5262 return count;
5263}
5264
Bryan Schumaker184c1842012-11-29 11:40:44 -05005265u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5266{
5267 u64 count = 0;
5268
Benny Halevycdc97502014-05-30 09:09:30 -04005269 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005270 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005271 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005272
5273 nfsd_print_count(clp, count, "delegations");
5274 return count;
5275}
5276
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005277u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005278{
5279 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005280 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005281 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005282
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005283 if (!nfsd_netns_ready(nn))
5284 return 0;
5285
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005286 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005287 count += func(clp, max - count);
5288 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005289 break;
5290 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005291
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005292 return count;
5293}
5294
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005295struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5296{
5297 struct nfs4_client *clp;
5298 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5299
5300 if (!nfsd_netns_ready(nn))
5301 return NULL;
5302
5303 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5304 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5305 return clp;
5306 }
5307 return NULL;
5308}
5309
Bryan Schumaker65178db2011-11-01 13:35:21 -04005310#endif /* CONFIG_NFSD_FAULT_INJECTION */
5311
Meelap Shahc2f1a552007-07-17 04:04:39 -07005312/*
5313 * Since the lifetime of a delegation isn't limited to that of an open, a
5314 * client may quite reasonably hang on to a delegation as long as it has
5315 * the inode cached. This becomes an obvious problem the first time a
5316 * client's inode cache approaches the size of the server's total memory.
5317 *
5318 * For now we avoid this problem by imposing a hard limit on the number
5319 * of delegations, which varies according to the server's memory size.
5320 */
5321static void
5322set_max_delegations(void)
5323{
5324 /*
5325 * Allow at most 4 delegations per megabyte of RAM. Quick
5326 * estimates suggest that in the worst case (where every delegation
5327 * is for a different inode), a delegation could take about 1.5K,
5328 * giving a worst case usage of about 6% of memory.
5329 */
5330 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5331}
5332
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005333static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005334{
5335 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5336 int i;
5337
5338 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5339 CLIENT_HASH_SIZE, GFP_KERNEL);
5340 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005341 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005342 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5343 CLIENT_HASH_SIZE, GFP_KERNEL);
5344 if (!nn->unconf_id_hashtbl)
5345 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005346 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5347 OWNER_HASH_SIZE, GFP_KERNEL);
5348 if (!nn->ownerstr_hashtbl)
5349 goto err_ownerstr;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005350 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5351 SESSION_HASH_SIZE, GFP_KERNEL);
5352 if (!nn->sessionid_hashtbl)
5353 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005354
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005355 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005356 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005357 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005358 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005359 for (i = 0; i < OWNER_HASH_SIZE; i++)
5360 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005361 for (i = 0; i < SESSION_HASH_SIZE; i++)
5362 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005363 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005364 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005365 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005366 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005367 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005368 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005369
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005370 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005371 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005372
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005373 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005374
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005375err_sessionid:
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03005376 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005377err_ownerstr:
5378 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005379err_unconf_id:
5380 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005381err:
5382 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005383}
5384
5385static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005386nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005387{
5388 int i;
5389 struct nfs4_client *clp = NULL;
5390 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5391
5392 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5393 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5394 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5395 destroy_client(clp);
5396 }
5397 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005398
Kinglong Mee2b905632014-03-26 22:09:30 +08005399 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5400 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5401 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5402 destroy_client(clp);
5403 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005404 }
5405
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005406 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005407 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005408 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005409 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005410 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005411}
5412
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005413int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005414nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005415{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005416 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005417 int ret;
5418
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005419 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005420 if (ret)
5421 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005422 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005423 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005424 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005425 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005426 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005427 nn->nfsd4_grace, net);
5428 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005429 return 0;
5430}
5431
5432/* initialization to perform when the nfsd service is started: */
5433
5434int
5435nfs4_state_start(void)
5436{
5437 int ret;
5438
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005439 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005440 if (ret)
5441 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005442 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005443 if (laundry_wq == NULL) {
5444 ret = -ENOMEM;
5445 goto out_recovery;
5446 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005447 ret = nfsd4_create_callback_queue();
5448 if (ret)
5449 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005450
Meelap Shahc2f1a552007-07-17 04:04:39 -07005451 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005452
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005453 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005454
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005455out_free_laundry:
5456 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005457out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005458 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459}
5460
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005461void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005462nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005466 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005468 cancel_delayed_work_sync(&nn->laundromat_work);
5469 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005470
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005471 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04005473 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005474 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5476 list_move(&dp->dl_recall_lru, &reaplist);
5477 }
Benny Halevycdc97502014-05-30 09:09:30 -04005478 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 list_for_each_safe(pos, next, &reaplist) {
5480 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005481 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 }
5483
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005484 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005485 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005486 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487}
5488
5489void
5490nfs4_state_shutdown(void)
5491{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005492 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005493 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005495
5496static void
5497get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5498{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005499 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5500 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005501}
5502
5503static void
5504put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5505{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005506 if (cstate->minorversion) {
5507 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5508 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5509 }
5510}
5511
5512void
5513clear_current_stateid(struct nfsd4_compound_state *cstate)
5514{
5515 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005516}
5517
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005518/*
5519 * functions to set current state id
5520 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005521void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005522nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5523{
5524 put_stateid(cstate, &odp->od_stateid);
5525}
5526
5527void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005528nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5529{
5530 put_stateid(cstate, &open->op_stateid);
5531}
5532
5533void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005534nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5535{
5536 put_stateid(cstate, &close->cl_stateid);
5537}
5538
5539void
5540nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5541{
5542 put_stateid(cstate, &lock->lk_resp_stateid);
5543}
5544
5545/*
5546 * functions to consume current state id
5547 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005548
5549void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005550nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5551{
5552 get_stateid(cstate, &odp->od_stateid);
5553}
5554
5555void
5556nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5557{
5558 get_stateid(cstate, &drp->dr_stateid);
5559}
5560
5561void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005562nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5563{
5564 get_stateid(cstate, &fsp->fr_stateid);
5565}
5566
5567void
5568nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5569{
5570 get_stateid(cstate, &setattr->sa_stateid);
5571}
5572
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005573void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005574nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5575{
5576 get_stateid(cstate, &close->cl_stateid);
5577}
5578
5579void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005580nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005581{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005582 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005583}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01005584
5585void
5586nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5587{
5588 get_stateid(cstate, &read->rd_stateid);
5589}
5590
5591void
5592nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5593{
5594 get_stateid(cstate, &write->wr_stateid);
5595}