blob: c4bb7f2b29d94d2dc58e7e90f4f18e94a3caab0f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Chuck Lever7df302f2012-05-29 13:56:37 -040041#include <linux/ratelimit.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050042#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton59766872013-02-04 12:50:00 -050043#include <linux/sunrpc/addr.h>
NeilBrown6282cd52014-06-04 17:39:26 +100044#include <linux/hash.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020045#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040046#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050047#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040048#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040050#include "netns.h"
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define NFSDDBG_FACILITY NFSDDBG_PROC
53
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050054#define all_ones {{~0,~0},~0}
55static const stateid_t one_stateid = {
56 .si_generation = ~0,
57 .si_opaque = all_ones,
58};
59static const stateid_t zero_stateid = {
60 /* all fields zero */
61};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010062static const stateid_t currentstateid = {
63 .si_generation = 1,
64};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050065
Andy Adamsonec6b5d72009-04-03 08:28:28 +030066static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070067
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050068#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
69#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010070#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040073static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040074static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
J. Bruce Fields8b671b82009-02-22 14:51:34 -080076/* Locking: */
77
78/* Currently used for almost all code touching nfsv4 state: */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080079static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
J. Bruce Fields8b671b82009-02-22 14:51:34 -080081/*
82 * Currently used for the del_recall_lru and file hash table. In an
83 * effort to decrease the scope of the client_mutex, this spinlock may
84 * eventually cover more:
85 */
Benny Halevycdc97502014-05-30 09:09:30 -040086static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080087
Jeff Laytonb401be222014-07-29 21:34:33 -040088/*
89 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
90 * the refcount on the open stateid to drop.
91 */
92static DECLARE_WAIT_QUEUE_HEAD(close_wq);
93
Christoph Hellwigabf11352014-05-21 07:43:03 -070094static struct kmem_cache *openowner_slab;
95static struct kmem_cache *lockowner_slab;
96static struct kmem_cache *file_slab;
97static struct kmem_cache *stateid_slab;
98static struct kmem_cache *deleg_slab;
NeilBrowne60d4392005-06-23 22:03:01 -070099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100void
101nfs4_lock_state(void)
102{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800103 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400106static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800107
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400108static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800109{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400110 return ses->se_flags & NFS4_SESSION_DEAD;
111}
112
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400113static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
114{
115 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400116 return nfserr_jukebox;
117 ses->se_flags |= NFS4_SESSION_DEAD;
118 return nfs_ok;
119}
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121void
122nfs4_unlock_state(void)
123{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800124 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
J. Bruce Fields221a6872013-04-01 22:23:49 -0400127static bool is_client_expired(struct nfs4_client *clp)
128{
129 return clp->cl_time == 0;
130}
131
132static __be32 mark_client_expired_locked(struct nfs4_client *clp)
133{
134 if (atomic_read(&clp->cl_refcount))
135 return nfserr_jukebox;
136 clp->cl_time = 0;
137 return nfs_ok;
138}
139
140static __be32 mark_client_expired(struct nfs4_client *clp)
141{
142 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
143 __be32 ret;
144
145 spin_lock(&nn->client_lock);
146 ret = mark_client_expired_locked(clp);
147 spin_unlock(&nn->client_lock);
148 return ret;
149}
150
151static __be32 get_client_locked(struct nfs4_client *clp)
152{
153 if (is_client_expired(clp))
154 return nfserr_expired;
155 atomic_inc(&clp->cl_refcount);
156 return nfs_ok;
157}
158
159/* must be called under the client_lock */
160static inline void
161renew_client_locked(struct nfs4_client *clp)
162{
163 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
164
165 if (is_client_expired(clp)) {
166 WARN_ON(1);
167 printk("%s: client (clientid %08x/%08x) already expired\n",
168 __func__,
169 clp->cl_clientid.cl_boot,
170 clp->cl_clientid.cl_id);
171 return;
172 }
173
174 dprintk("renewing client (clientid %08x/%08x)\n",
175 clp->cl_clientid.cl_boot,
176 clp->cl_clientid.cl_id);
177 list_move_tail(&clp->cl_lru, &nn->client_lru);
178 clp->cl_time = get_seconds();
179}
180
181static inline void
182renew_client(struct nfs4_client *clp)
183{
184 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
185
186 spin_lock(&nn->client_lock);
187 renew_client_locked(clp);
188 spin_unlock(&nn->client_lock);
189}
190
Fengguang Wuba138432013-04-16 22:14:15 -0400191static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400192{
193 if (!atomic_dec_and_test(&clp->cl_refcount))
194 return;
195 if (!is_client_expired(clp))
196 renew_client_locked(clp);
197}
198
Jeff Layton4b24ca72014-06-30 11:48:44 -0400199static void put_client_renew(struct nfs4_client *clp)
200{
201 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
202
Jeff Laytond6c249b2014-07-08 14:02:50 -0400203 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
204 return;
205 if (!is_client_expired(clp))
206 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400207 spin_unlock(&nn->client_lock);
208}
209
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400210static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
211{
212 __be32 status;
213
214 if (is_session_dead(ses))
215 return nfserr_badsession;
216 status = get_client_locked(ses->se_client);
217 if (status)
218 return status;
219 atomic_inc(&ses->se_ref);
220 return nfs_ok;
221}
222
223static void nfsd4_put_session_locked(struct nfsd4_session *ses)
224{
225 struct nfs4_client *clp = ses->se_client;
226
227 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
228 free_session(ses);
229 put_client_renew_locked(clp);
230}
231
232static void nfsd4_put_session(struct nfsd4_session *ses)
233{
234 struct nfs4_client *clp = ses->se_client;
235 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
236
237 spin_lock(&nn->client_lock);
238 nfsd4_put_session_locked(ses);
239 spin_unlock(&nn->client_lock);
240}
241
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400242static int
243same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
244 clientid_t *clid)
245{
246 return (sop->so_owner.len == owner->len) &&
247 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
248 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
249}
250
251static struct nfs4_openowner *
252find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
253 bool sessions, struct nfsd_net *nn)
254{
255 struct nfs4_stateowner *so;
256 struct nfs4_openowner *oo;
257 struct nfs4_client *clp;
258
259 lockdep_assert_held(&nn->client_lock);
260
261 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
262 if (!so->so_is_open_owner)
263 continue;
264 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
265 oo = openowner(so);
266 clp = oo->oo_owner.so_client;
267 if ((bool)clp->cl_minorversion != sessions)
268 break;
269 renew_client_locked(clp);
270 atomic_inc(&so->so_count);
271 return oo;
272 }
273 }
274 return NULL;
275}
276
277static struct nfs4_openowner *
278find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
279 bool sessions, struct nfsd_net *nn)
280{
281 struct nfs4_openowner *oo;
282
283 spin_lock(&nn->client_lock);
284 oo = find_openstateowner_str_locked(hashval, open, sessions, nn);
285 spin_unlock(&nn->client_lock);
286 return oo;
287}
288
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290static inline u32
291opaque_hashval(const void *ptr, int nbytes)
292{
293 unsigned char *cptr = (unsigned char *) ptr;
294
295 u32 x = 0;
296 while (nbytes--) {
297 x *= 37;
298 x += *cptr++;
299 }
300 return x;
301}
302
J. Bruce Fields32513b42011-10-13 16:00:16 -0400303static void nfsd4_free_file(struct nfs4_file *f)
304{
305 kmem_cache_free(file_slab, f);
306}
307
NeilBrown13cd2182005-06-23 22:03:10 -0700308static inline void
309put_nfs4_file(struct nfs4_file *fi)
310{
Jeff Layton02e12152014-07-16 10:31:57 -0400311 might_lock(&state_lock);
312
Benny Halevycdc97502014-05-30 09:09:30 -0400313 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton89876f82013-04-02 09:01:59 -0400314 hlist_del(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400315 spin_unlock(&state_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400316 nfsd4_free_file(fi);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800317 }
NeilBrown13cd2182005-06-23 22:03:10 -0700318}
319
320static inline void
321get_nfs4_file(struct nfs4_file *fi)
322{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800323 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700324}
325
Trond Myklebustde186432014-07-10 14:07:26 -0400326static struct file *
327__nfs4_get_fd(struct nfs4_file *f, int oflag)
328{
329 if (f->fi_fds[oflag])
330 return get_file(f->fi_fds[oflag]);
331 return NULL;
332}
333
334static struct file *
335find_writeable_file_locked(struct nfs4_file *f)
336{
337 struct file *ret;
338
339 lockdep_assert_held(&f->fi_lock);
340
341 ret = __nfs4_get_fd(f, O_WRONLY);
342 if (!ret)
343 ret = __nfs4_get_fd(f, O_RDWR);
344 return ret;
345}
346
347static struct file *
348find_writeable_file(struct nfs4_file *f)
349{
350 struct file *ret;
351
352 spin_lock(&f->fi_lock);
353 ret = find_writeable_file_locked(f);
354 spin_unlock(&f->fi_lock);
355
356 return ret;
357}
358
359static struct file *find_readable_file_locked(struct nfs4_file *f)
360{
361 struct file *ret;
362
363 lockdep_assert_held(&f->fi_lock);
364
365 ret = __nfs4_get_fd(f, O_RDONLY);
366 if (!ret)
367 ret = __nfs4_get_fd(f, O_RDWR);
368 return ret;
369}
370
371static struct file *
372find_readable_file(struct nfs4_file *f)
373{
374 struct file *ret;
375
376 spin_lock(&f->fi_lock);
377 ret = find_readable_file_locked(f);
378 spin_unlock(&f->fi_lock);
379
380 return ret;
381}
382
383static struct file *
384find_any_file(struct nfs4_file *f)
385{
386 struct file *ret;
387
388 spin_lock(&f->fi_lock);
389 ret = __nfs4_get_fd(f, O_RDWR);
390 if (!ret) {
391 ret = __nfs4_get_fd(f, O_WRONLY);
392 if (!ret)
393 ret = __nfs4_get_fd(f, O_RDONLY);
394 }
395 spin_unlock(&f->fi_lock);
396 return ret;
397}
398
Trond Myklebust02a35082014-07-25 07:34:22 -0400399static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800400unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700401
402/*
403 * Open owner state (share locks)
404 */
405
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500406/* hash tables for lock and open owners */
407#define OWNER_HASH_BITS 8
408#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
409#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700410
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500411static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400412{
413 unsigned int ret;
414
415 ret = opaque_hashval(ownername->data, ownername->len);
416 ret += clientid;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500417 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400418}
NeilBrownef0f3392006-04-10 22:55:41 -0700419
NeilBrownef0f3392006-04-10 22:55:41 -0700420/* hash table for nfs4_file */
421#define FILE_HASH_BITS 8
422#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800423
Trond Myklebustca943212014-07-23 16:17:39 -0400424static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400425{
Trond Myklebustca943212014-07-23 16:17:39 -0400426 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
427}
428
429static unsigned int file_hashval(struct knfsd_fh *fh)
430{
431 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
432}
433
434static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
435{
436 return fh1->fh_size == fh2->fh_size &&
437 !memcmp(fh1->fh_base.fh_pad,
438 fh2->fh_base.fh_pad,
439 fh1->fh_size);
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400440}
441
Jeff Layton89876f82013-04-02 09:01:59 -0400442static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700443
Jeff Layton12659652014-07-10 14:07:28 -0400444static void
445__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400446{
Jeff Layton7214e862014-07-10 14:07:33 -0400447 lockdep_assert_held(&fp->fi_lock);
448
Jeff Layton12659652014-07-10 14:07:28 -0400449 if (access & NFS4_SHARE_ACCESS_WRITE)
450 atomic_inc(&fp->fi_access[O_WRONLY]);
451 if (access & NFS4_SHARE_ACCESS_READ)
452 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400453}
454
Jeff Layton12659652014-07-10 14:07:28 -0400455static __be32
456nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400457{
Jeff Layton7214e862014-07-10 14:07:33 -0400458 lockdep_assert_held(&fp->fi_lock);
459
Jeff Layton12659652014-07-10 14:07:28 -0400460 /* Does this access mode make sense? */
461 if (access & ~NFS4_SHARE_ACCESS_BOTH)
462 return nfserr_inval;
463
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400464 /* Does it conflict with a deny mode already set? */
465 if ((access & fp->fi_share_deny) != 0)
466 return nfserr_share_denied;
467
Jeff Layton12659652014-07-10 14:07:28 -0400468 __nfs4_file_get_access(fp, access);
469 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400470}
471
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400472static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
473{
474 /* Common case is that there is no deny mode. */
475 if (deny) {
476 /* Does this deny mode make sense? */
477 if (deny & ~NFS4_SHARE_DENY_BOTH)
478 return nfserr_inval;
479
480 if ((deny & NFS4_SHARE_DENY_READ) &&
481 atomic_read(&fp->fi_access[O_RDONLY]))
482 return nfserr_share_denied;
483
484 if ((deny & NFS4_SHARE_DENY_WRITE) &&
485 atomic_read(&fp->fi_access[O_WRONLY]))
486 return nfserr_share_denied;
487 }
488 return nfs_ok;
489}
490
J. Bruce Fields998db522010-08-07 09:21:41 -0400491static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400492{
Trond Myklebustde186432014-07-10 14:07:26 -0400493 might_lock(&fp->fi_lock);
494
495 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
496 struct file *f1 = NULL;
497 struct file *f2 = NULL;
498
Jeff Layton6d338b52014-07-10 14:07:29 -0400499 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400500 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400501 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400502 spin_unlock(&fp->fi_lock);
503 if (f1)
504 fput(f1);
505 if (f2)
506 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400507 }
508}
509
Jeff Layton12659652014-07-10 14:07:28 -0400510static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400511{
Jeff Layton12659652014-07-10 14:07:28 -0400512 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
513
514 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400515 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400516 if (access & NFS4_SHARE_ACCESS_READ)
517 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400518}
519
Trond Myklebust60116952014-07-29 21:34:06 -0400520static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
521 struct kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400522{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500523 struct nfs4_stid *stid;
524 int new_id;
525
Trond Myklebustf8338832014-07-25 07:34:19 -0400526 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500527 if (!stid)
528 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400529
Jeff Layton4770d722014-07-29 21:34:10 -0400530 idr_preload(GFP_KERNEL);
531 spin_lock(&cl->cl_lock);
532 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
533 spin_unlock(&cl->cl_lock);
534 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700535 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500536 goto out_free;
537 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500538 stid->sc_stateid.si_opaque.so_id = new_id;
539 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
540 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400541 atomic_set(&stid->sc_count, 1);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500542
J. Bruce Fields996e0932011-10-17 11:14:48 -0400543 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500544 * It shouldn't be a problem to reuse an opaque stateid value.
545 * I don't think it is for 4.1. But with 4.0 I worry that, for
546 * example, a stray write retransmission could be accepted by
547 * the server when it should have been rejected. Therefore,
548 * adopt a trick from the sctp code to attempt to maximize the
549 * amount of time until an id is reused, by ensuring they always
550 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400551 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500552 return stid;
553out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800554 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500555 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400556}
557
Jeff Laytonb49e0842014-07-29 21:34:11 -0400558static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400559{
Trond Myklebust60116952014-07-29 21:34:06 -0400560 struct nfs4_stid *stid;
561 struct nfs4_ol_stateid *stp;
562
563 stid = nfs4_alloc_stid(clp, stateid_slab);
564 if (!stid)
565 return NULL;
566
567 stp = openlockstateid(stid);
568 stp->st_stid.sc_free = nfs4_free_ol_stateid;
569 return stp;
570}
571
572static void nfs4_free_deleg(struct nfs4_stid *stid)
573{
Trond Myklebust60116952014-07-29 21:34:06 -0400574 kmem_cache_free(deleg_slab, stid);
575 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400576}
577
NeilBrown6282cd52014-06-04 17:39:26 +1000578/*
579 * When we recall a delegation, we should be careful not to hand it
580 * out again straight away.
581 * To ensure this we keep a pair of bloom filters ('new' and 'old')
582 * in which the filehandles of recalled delegations are "stored".
583 * If a filehandle appear in either filter, a delegation is blocked.
584 * When a delegation is recalled, the filehandle is stored in the "new"
585 * filter.
586 * Every 30 seconds we swap the filters and clear the "new" one,
587 * unless both are empty of course.
588 *
589 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
590 * low 3 bytes as hash-table indices.
591 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400592 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000593 * is used to manage concurrent access. Testing does not need the lock
594 * except when swapping the two filters.
595 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400596static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000597static struct bloom_pair {
598 int entries, old_entries;
599 time_t swap_time;
600 int new; /* index into 'set' */
601 DECLARE_BITMAP(set[2], 256);
602} blocked_delegations;
603
604static int delegation_blocked(struct knfsd_fh *fh)
605{
606 u32 hash;
607 struct bloom_pair *bd = &blocked_delegations;
608
609 if (bd->entries == 0)
610 return 0;
611 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400612 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000613 if (seconds_since_boot() - bd->swap_time > 30) {
614 bd->entries -= bd->old_entries;
615 bd->old_entries = bd->entries;
616 memset(bd->set[bd->new], 0,
617 sizeof(bd->set[0]));
618 bd->new = 1-bd->new;
619 bd->swap_time = seconds_since_boot();
620 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400621 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000622 }
623 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
624 if (test_bit(hash&255, bd->set[0]) &&
625 test_bit((hash>>8)&255, bd->set[0]) &&
626 test_bit((hash>>16)&255, bd->set[0]))
627 return 1;
628
629 if (test_bit(hash&255, bd->set[1]) &&
630 test_bit((hash>>8)&255, bd->set[1]) &&
631 test_bit((hash>>16)&255, bd->set[1]))
632 return 1;
633
634 return 0;
635}
636
637static void block_delegations(struct knfsd_fh *fh)
638{
639 u32 hash;
640 struct bloom_pair *bd = &blocked_delegations;
641
642 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
643
Jeff Laytonf54fe962014-07-25 07:34:26 -0400644 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000645 __set_bit(hash&255, bd->set[bd->new]);
646 __set_bit((hash>>8)&255, bd->set[bd->new]);
647 __set_bit((hash>>16)&255, bd->set[bd->new]);
648 if (bd->entries == 0)
649 bd->swap_time = seconds_since_boot();
650 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400651 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000652}
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654static struct nfs4_delegation *
Jeff Laytonf9416e22014-07-25 07:34:23 -0400655alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400658 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400661 n = atomic_long_inc_return(&num_delegations);
662 if (n < 0 || n > max_delegations)
663 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000664 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400665 goto out_dec;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400666 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700667 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400668 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400669
670 dp->dl_stid.sc_free = nfs4_free_deleg;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400671 /*
672 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400673 * meaning of seqid 0 isn't meaningful, really, but let's avoid
674 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400675 */
676 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700677 INIT_LIST_HEAD(&dp->dl_perfile);
678 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400680 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Jeff Layton02e12152014-07-16 10:31:57 -0400681 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400683out_dec:
684 atomic_long_dec(&num_delegations);
685 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
688void
Trond Myklebust60116952014-07-29 21:34:06 -0400689nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Trond Myklebust11b91642014-07-29 21:34:08 -0400691 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400692 struct nfs4_client *clp = s->sc_client;
693
Jeff Layton4770d722014-07-29 21:34:10 -0400694 might_lock(&clp->cl_lock);
695
Jeff Laytonb401be222014-07-29 21:34:33 -0400696 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
697 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400698 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400699 }
Trond Myklebust60116952014-07-29 21:34:06 -0400700 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400701 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400702 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400703 if (fp)
704 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500707static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Jeff Layton417c6622014-07-21 09:34:57 -0400709 lockdep_assert_held(&state_lock);
710
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -0500711 if (!fp->fi_lease)
712 return;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500713 if (atomic_dec_and_test(&fp->fi_delegees)) {
714 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
715 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400716 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500717 fp->fi_deleg_file = NULL;
718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400721static void unhash_stid(struct nfs4_stid *s)
722{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500723 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400724}
725
Benny Halevy931ee562014-05-30 09:09:27 -0400726static void
727hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
728{
Benny Halevycdc97502014-05-30 09:09:30 -0400729 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400730 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400731
Trond Myklebust67cb1272014-07-29 21:34:17 -0400732 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400733 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400734 list_add(&dp->dl_perfile, &fp->fi_delegations);
735 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
736}
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738static void
Jeff Layton42690672014-07-25 07:34:20 -0400739unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Trond Myklebust11b91642014-07-29 21:34:08 -0400741 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400742
Jeff Layton42690672014-07-25 07:34:20 -0400743 lockdep_assert_held(&state_lock);
744
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400745 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400746 /* Ensure that deleg break won't try to requeue it */
747 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400748 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400749 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400751 list_del_init(&dp->dl_perfile);
752 spin_unlock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400753 if (fp)
Trond Myklebustf8338832014-07-25 07:34:19 -0400754 nfs4_put_deleg_lease(fp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400755}
756
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400757static void destroy_delegation(struct nfs4_delegation *dp)
758{
Jeff Layton42690672014-07-25 07:34:20 -0400759 spin_lock(&state_lock);
760 unhash_delegation_locked(dp);
761 spin_unlock(&state_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400762 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400763}
764
765static void revoke_delegation(struct nfs4_delegation *dp)
766{
767 struct nfs4_client *clp = dp->dl_stid.sc_client;
768
Jeff Layton2d4a5322014-07-25 07:34:21 -0400769 WARN_ON(!list_empty(&dp->dl_recall_lru));
770
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400771 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400772 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400773 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400774 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400775 spin_lock(&clp->cl_lock);
776 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
777 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400778 }
779}
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781/*
782 * SETCLIENTID state
783 */
784
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400785static unsigned int clientid_hashval(u32 id)
786{
787 return id & CLIENT_HASH_MASK;
788}
789
790static unsigned int clientstr_hashval(const char *name)
791{
792 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
793}
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400796 * We store the NONE, READ, WRITE, and BOTH bits separately in the
797 * st_{access,deny}_bmap field of the stateid, in order to track not
798 * only what share bits are currently in force, but also what
799 * combinations of share bits previous opens have used. This allows us
800 * to enforce the recommendation of rfc 3530 14.2.19 that the server
801 * return an error if the client attempt to downgrade to a combination
802 * of share bits not explicable by closing some of its previous opens.
803 *
804 * XXX: This enforcement is actually incomplete, since we don't keep
805 * track of access/deny bit combinations; so, e.g., we allow:
806 *
807 * OPEN allow read, deny write
808 * OPEN allow both, deny none
809 * DOWNGRADE allow read, deny none
810 *
811 * which we should reject.
812 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400813static unsigned int
814bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400815 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400816 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400817
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400818 for (i = 1; i < 4; i++) {
819 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400820 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400821 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400822 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400823}
824
Jeff Layton82c5ff12012-05-11 09:45:13 -0400825/* set share access for a given stateid */
826static inline void
827set_access(u32 access, struct nfs4_ol_stateid *stp)
828{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400829 unsigned char mask = 1 << access;
830
831 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
832 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400833}
834
835/* clear share access for a given stateid */
836static inline void
837clear_access(u32 access, struct nfs4_ol_stateid *stp)
838{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400839 unsigned char mask = 1 << access;
840
841 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
842 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400843}
844
845/* test whether a given stateid has access */
846static inline bool
847test_access(u32 access, struct nfs4_ol_stateid *stp)
848{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400849 unsigned char mask = 1 << access;
850
851 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400852}
853
Jeff Laytonce0fc432012-05-11 09:45:14 -0400854/* set share deny for a given stateid */
855static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400856set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400857{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400858 unsigned char mask = 1 << deny;
859
860 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
861 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400862}
863
864/* clear share deny for a given stateid */
865static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400866clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400867{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400868 unsigned char mask = 1 << deny;
869
870 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
871 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400872}
873
874/* test whether a given stateid is denying specific access */
875static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400876test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400877{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400878 unsigned char mask = 1 << deny;
879
880 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400881}
882
883static int nfs4_access_to_omode(u32 access)
884{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400885 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400886 case NFS4_SHARE_ACCESS_READ:
887 return O_RDONLY;
888 case NFS4_SHARE_ACCESS_WRITE:
889 return O_WRONLY;
890 case NFS4_SHARE_ACCESS_BOTH:
891 return O_RDWR;
892 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500893 WARN_ON_ONCE(1);
894 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400895}
896
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400897/*
898 * A stateid that had a deny mode associated with it is being released
899 * or downgraded. Recalculate the deny mode on the file.
900 */
901static void
902recalculate_deny_mode(struct nfs4_file *fp)
903{
904 struct nfs4_ol_stateid *stp;
905
906 spin_lock(&fp->fi_lock);
907 fp->fi_share_deny = 0;
908 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
909 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
910 spin_unlock(&fp->fi_lock);
911}
912
913static void
914reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
915{
916 int i;
917 bool change = false;
918
919 for (i = 1; i < 4; i++) {
920 if ((i & deny) != i) {
921 change = true;
922 clear_deny(i, stp);
923 }
924 }
925
926 /* Recalculate per-file deny mode if there was a change */
927 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -0400928 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400929}
930
Jeff Layton82c5ff12012-05-11 09:45:13 -0400931/* release all access and file references for a given stateid */
932static void
933release_all_access(struct nfs4_ol_stateid *stp)
934{
935 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -0400936 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400937
938 if (fp && stp->st_deny_bmap != 0)
939 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400940
941 for (i = 1; i < 4; i++) {
942 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -0400943 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400944 clear_access(i, stp);
945 }
946}
947
Jeff Layton6b180f02014-07-29 21:34:26 -0400948static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
949{
950 if (!atomic_dec_and_test(&sop->so_count))
951 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -0400952 sop->so_ops->so_unhash(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -0400953 kfree(sop->so_owner.data);
954 sop->so_ops->so_free(sop);
955}
956
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400957static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500958{
Trond Myklebust11b91642014-07-29 21:34:08 -0400959 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -0400960
Jeff Layton1c755dc2014-07-29 21:34:12 -0400961 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
962
Trond Myklebust1d31a252014-07-10 14:07:25 -0400963 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500964 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400965 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500966 list_del(&stp->st_perstateowner);
967}
968
Trond Myklebust60116952014-07-29 21:34:06 -0400969static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500970{
Trond Myklebust60116952014-07-29 21:34:06 -0400971 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400972
Trond Myklebust60116952014-07-29 21:34:06 -0400973 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -0400974 if (stp->st_stateowner)
975 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -0400976 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500977}
978
Jeff Laytonb49e0842014-07-29 21:34:11 -0400979static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500980{
Jeff Laytonb49e0842014-07-29 21:34:11 -0400981 struct nfs4_ol_stateid *stp = openlockstateid(stid);
982 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500983 struct file *file;
984
Jeff Laytonb49e0842014-07-29 21:34:11 -0400985 file = find_any_file(stp->st_stid.sc_file);
986 if (file)
987 filp_close(file, (fl_owner_t)lo);
988 nfs4_free_ol_stateid(stid);
989}
990
Jeff Layton5adfd882014-07-29 21:34:31 -0400991static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -0400992{
Jeff Layton1c755dc2014-07-29 21:34:12 -0400993 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
994
995 spin_lock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400996 list_del(&stp->st_locks);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500997 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400998 unhash_stid(&stp->st_stid);
Jeff Layton1c755dc2014-07-29 21:34:12 -0400999 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04001000 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001001}
1002
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001003static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001004{
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001005 list_del_init(&lo->lo_owner.so_strhash);
1006}
1007
1008static void release_lockowner_stateids(struct nfs4_lockowner *lo)
1009{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001010 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001011
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001012 while (!list_empty(&lo->lo_owner.so_stateids)) {
1013 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001014 struct nfs4_ol_stateid, st_perstateowner);
Jeff Layton5adfd882014-07-29 21:34:31 -04001015 release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001016 }
1017}
1018
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001019static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001020{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001021 unhash_lockowner(lo);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001022 release_lockowner_stateids(lo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001023 nfs4_put_stateowner(&lo->lo_owner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001024}
1025
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001026static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
Jeff Layton1c755dc2014-07-29 21:34:12 -04001027 __releases(&open_stp->st_stateowner->so_client->cl_lock)
1028 __acquires(&open_stp->st_stateowner->so_client->cl_lock)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001029{
1030 struct nfs4_ol_stateid *stp;
1031
1032 while (!list_empty(&open_stp->st_locks)) {
1033 stp = list_entry(open_stp->st_locks.next,
1034 struct nfs4_ol_stateid, st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001035 spin_unlock(&open_stp->st_stateowner->so_client->cl_lock);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001036 release_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001037 spin_lock(&open_stp->st_stateowner->so_client->cl_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001038 }
1039}
1040
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001041static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -05001042{
Jeff Layton1c755dc2014-07-29 21:34:12 -04001043 spin_lock(&stp->st_stateowner->so_client->cl_lock);
J. Bruce Fields22839632009-01-11 14:27:17 -05001044 unhash_generic_stateid(stp);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001045 release_open_stateid_locks(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001046 spin_unlock(&stp->st_stateowner->so_client->cl_lock);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001047}
1048
1049static void release_open_stateid(struct nfs4_ol_stateid *stp)
1050{
1051 unhash_open_stateid(stp);
Trond Myklebust60116952014-07-29 21:34:06 -04001052 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields22839632009-01-11 14:27:17 -05001053}
1054
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001055static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001056{
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001057 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1058 nfsd_net_id);
1059
1060 lockdep_assert_held(&nn->client_lock);
1061
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001062 list_del_init(&oo->oo_owner.so_strhash);
1063 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001064}
1065
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001066static void release_last_closed_stateid(struct nfs4_openowner *oo)
1067{
1068 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
1069
1070 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001071 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001072 oo->oo_last_closed_stid = NULL;
Jeff Laytond3134b12014-07-29 21:34:32 -04001073 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001074 }
1075}
1076
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001077static void release_openowner_stateids(struct nfs4_openowner *oo)
1078{
1079 struct nfs4_ol_stateid *stp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001080 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1081 nfsd_net_id);
1082
1083 lockdep_assert_held(&nn->client_lock);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001084
1085 while (!list_empty(&oo->oo_owner.so_stateids)) {
1086 stp = list_first_entry(&oo->oo_owner.so_stateids,
1087 struct nfs4_ol_stateid, st_perstateowner);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001088 spin_unlock(&nn->client_lock);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001089 release_open_stateid(stp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001090 spin_lock(&nn->client_lock);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001091 }
1092}
1093
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001094static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001095{
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001096 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1097 nfsd_net_id);
1098
1099 spin_lock(&nn->client_lock);
1100 unhash_openowner_locked(oo);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001101 release_openowner_stateids(oo);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001102 spin_unlock(&nn->client_lock);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001103 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001104 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001105}
1106
Marc Eshel5282fd72009-04-03 08:27:52 +03001107static inline int
1108hash_sessionid(struct nfs4_sessionid *sessionid)
1109{
1110 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1111
1112 return sid->sequence % SESSION_HASH_SIZE;
1113}
1114
Trond Myklebust8f199b82012-03-20 15:11:17 -04001115#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001116static inline void
1117dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1118{
1119 u32 *ptr = (u32 *)(&sessionid->data[0]);
1120 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1121}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001122#else
1123static inline void
1124dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1125{
1126}
1127#endif
1128
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001129/*
1130 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1131 * won't be used for replay.
1132 */
1133void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1134{
1135 struct nfs4_stateowner *so = cstate->replay_owner;
1136
1137 if (nfserr == nfserr_replay_me)
1138 return;
1139
1140 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001141 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001142 return;
1143 }
1144 if (!so)
1145 return;
1146 if (so->so_is_open_owner)
1147 release_last_closed_stateid(openowner(so));
1148 so->so_seqid++;
1149 return;
1150}
Marc Eshel5282fd72009-04-03 08:27:52 +03001151
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001152static void
1153gen_sessionid(struct nfsd4_session *ses)
1154{
1155 struct nfs4_client *clp = ses->se_client;
1156 struct nfsd4_sessionid *sid;
1157
1158 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1159 sid->clientid = clp->cl_clientid;
1160 sid->sequence = current_sessionid++;
1161 sid->reserved = 0;
1162}
1163
1164/*
Andy Adamsona6496372009-08-28 08:45:01 -04001165 * The protocol defines ca_maxresponssize_cached to include the size of
1166 * the rpc header, but all we need to cache is the data starting after
1167 * the end of the initial SEQUENCE operation--the rest we regenerate
1168 * each time. Therefore we can advertise a ca_maxresponssize_cached
1169 * value that is the number of bytes in our cache plus a few additional
1170 * bytes. In order to stay on the safe side, and not promise more than
1171 * we can cache, those additional bytes must be the minimum possible: 24
1172 * bytes of rpc header (xid through accept state, with AUTH_NULL
1173 * verifier), 12 for the compound header (with zero-length tag), and 44
1174 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001175 */
Andy Adamsona6496372009-08-28 08:45:01 -04001176#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1177
Andy Adamson557ce262009-08-28 08:45:04 -04001178static void
1179free_session_slots(struct nfsd4_session *ses)
1180{
1181 int i;
1182
1183 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1184 kfree(ses->se_slots[i]);
1185}
1186
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001187/*
1188 * We don't actually need to cache the rpc and session headers, so we
1189 * can allocate a little less for each slot:
1190 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001191static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001192{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001193 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001194
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001195 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1196 size = 0;
1197 else
1198 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1199 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001200}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001201
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001202/*
1203 * XXX: If we run out of reserved DRC memory we could (up to a point)
1204 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001205 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001206 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001207static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001208{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001209 u32 slotsize = slot_bytes(ca);
1210 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001211 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001212
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001213 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001214 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1215 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001216 num = min_t(int, num, avail / slotsize);
1217 nfsd_drc_mem_used += num * slotsize;
1218 spin_unlock(&nfsd_drc_lock);
1219
1220 return num;
1221}
1222
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001223static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001224{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001225 int slotsize = slot_bytes(ca);
1226
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001227 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001228 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001229 spin_unlock(&nfsd_drc_lock);
1230}
1231
Kinglong Mee60810e52014-01-01 00:35:47 +08001232static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1233 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001234{
Kinglong Mee60810e52014-01-01 00:35:47 +08001235 int numslots = fattrs->maxreqs;
1236 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001237 struct nfsd4_session *new;
1238 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001239
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001240 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001241 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1242 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001243
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001244 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001245 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001246 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001247 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001248 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001249 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001250 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001251 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001252 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001253
1254 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1255 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1256
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001257 return new;
1258out_free:
1259 while (i--)
1260 kfree(new->se_slots[i]);
1261 kfree(new);
1262 return NULL;
1263}
1264
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001265static void free_conn(struct nfsd4_conn *c)
1266{
1267 svc_xprt_put(c->cn_xprt);
1268 kfree(c);
1269}
1270
1271static void nfsd4_conn_lost(struct svc_xpt_user *u)
1272{
1273 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1274 struct nfs4_client *clp = c->cn_session->se_client;
1275
1276 spin_lock(&clp->cl_lock);
1277 if (!list_empty(&c->cn_persession)) {
1278 list_del(&c->cn_persession);
1279 free_conn(c);
1280 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001281 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001282 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001283}
1284
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001285static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001286{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001287 struct nfsd4_conn *conn;
1288
1289 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1290 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001291 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001292 svc_xprt_get(rqstp->rq_xprt);
1293 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001294 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001295 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1296 return conn;
1297}
1298
J. Bruce Fields328ead22010-09-29 16:11:06 -04001299static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1300{
1301 conn->cn_session = ses;
1302 list_add(&conn->cn_persession, &ses->se_conns);
1303}
1304
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001305static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1306{
1307 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001308
1309 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001310 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001311 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001312}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001313
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001314static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001315{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001316 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001317 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001318}
1319
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001320static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001321{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001322 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001323
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001324 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001325 ret = nfsd4_register_conn(conn);
1326 if (ret)
1327 /* oops; xprt is already down: */
1328 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001329 /* We may have gained or lost a callback channel: */
1330 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001331}
1332
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001333static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001334{
1335 u32 dir = NFS4_CDFC4_FORE;
1336
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001337 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001338 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001339 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001340}
1341
1342/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001343static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001344{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001345 struct nfs4_client *clp = s->se_client;
1346 struct nfsd4_conn *c;
1347
1348 spin_lock(&clp->cl_lock);
1349 while (!list_empty(&s->se_conns)) {
1350 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1351 list_del_init(&c->cn_persession);
1352 spin_unlock(&clp->cl_lock);
1353
1354 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1355 free_conn(c);
1356
1357 spin_lock(&clp->cl_lock);
1358 }
1359 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001360}
1361
J. Bruce Fields1377b692012-09-11 21:42:40 -04001362static void __free_session(struct nfsd4_session *ses)
1363{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001364 free_session_slots(ses);
1365 kfree(ses);
1366}
1367
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001368static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001369{
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001370 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1371
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001372 lockdep_assert_held(&nn->client_lock);
1373 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001374 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001375 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001376}
Andy Adamson557ce262009-08-28 08:45:04 -04001377
Fengguang Wu135ae822012-11-10 07:20:25 -05001378static 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 -04001379{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001380 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001381 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001382
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001383 new->se_client = clp;
1384 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001385
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001386 INIT_LIST_HEAD(&new->se_conns);
1387
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001388 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001389 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001390 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001391 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001392 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001393 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001394 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001395 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001396 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001397 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001398 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001399 spin_unlock(&nn->client_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001400
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001401 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001402 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001403 /*
1404 * This is a little silly; with sessions there's no real
1405 * use for the callback address. Use the peer address
1406 * as a reasonable default for now, but consider fixing
1407 * the rpc client not to require an address in the
1408 * future:
1409 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001410 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1411 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001412 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001413}
1414
Benny Halevy9089f1b2010-05-12 00:12:26 +03001415/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001416static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001417__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001418{
1419 struct nfsd4_session *elem;
1420 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001421 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001422
1423 dump_sessionid(__func__, sessionid);
1424 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001425 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001426 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001427 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1428 NFS4_MAX_SESSIONID_LEN)) {
1429 return elem;
1430 }
1431 }
1432
1433 dprintk("%s: session not found\n", __func__);
1434 return NULL;
1435}
1436
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001437static struct nfsd4_session *
1438find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1439 __be32 *ret)
1440{
1441 struct nfsd4_session *session;
1442 __be32 status = nfserr_badsession;
1443
1444 session = __find_in_sessionid_hashtbl(sessionid, net);
1445 if (!session)
1446 goto out;
1447 status = nfsd4_get_session_locked(session);
1448 if (status)
1449 session = NULL;
1450out:
1451 *ret = status;
1452 return session;
1453}
1454
Benny Halevy9089f1b2010-05-12 00:12:26 +03001455/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001456static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001457unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001458{
1459 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001460 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001461 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001462 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001463}
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1466static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001467STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001469 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001471 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001472 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 return 1;
1474}
1475
1476/*
1477 * XXX Should we use a slab cache ?
1478 * This type of memory management is somewhat inefficient, but we use it
1479 * anyway since SETCLIENTID is not a common operation.
1480 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001481static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
1483 struct nfs4_client *clp;
1484
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001485 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1486 if (clp == NULL)
1487 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001488 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001489 if (clp->cl_name.data == NULL) {
1490 kfree(clp);
1491 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001493 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001494 INIT_LIST_HEAD(&clp->cl_sessions);
1495 idr_init(&clp->cl_stateids);
1496 atomic_set(&clp->cl_refcount, 0);
1497 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1498 INIT_LIST_HEAD(&clp->cl_idhash);
1499 INIT_LIST_HEAD(&clp->cl_openowners);
1500 INIT_LIST_HEAD(&clp->cl_delegations);
1501 INIT_LIST_HEAD(&clp->cl_lru);
1502 INIT_LIST_HEAD(&clp->cl_callbacks);
1503 INIT_LIST_HEAD(&clp->cl_revoked);
1504 spin_lock_init(&clp->cl_lock);
1505 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return clp;
1507}
1508
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001509static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510free_client(struct nfs4_client *clp)
1511{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001512 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001513
1514 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001515 while (!list_empty(&clp->cl_sessions)) {
1516 struct nfsd4_session *ses;
1517 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1518 se_perclnt);
1519 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001520 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1521 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001522 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001523 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001524 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001526 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 kfree(clp);
1528}
1529
Benny Halevy84d38ac2010-05-12 00:13:16 +03001530/* must be called under the client_lock */
1531static inline void
1532unhash_client_locked(struct nfs4_client *clp)
1533{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001534 struct nfsd4_session *ses;
1535
Benny Halevy84d38ac2010-05-12 00:13:16 +03001536 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001537 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001538 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1539 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001540 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001541}
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001544destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001546 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001549 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001552 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001553 while (!list_empty(&clp->cl_delegations)) {
1554 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001555 unhash_delegation_locked(dp);
1556 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
Benny Halevycdc97502014-05-30 09:09:30 -04001558 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 while (!list_empty(&reaplist)) {
1560 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001561 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001562 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001564 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001565 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001566 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001567 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001568 }
NeilBrownea1da632005-06-23 22:04:17 -07001569 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001570 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Jeff Laytond3134b12014-07-29 21:34:32 -04001571 atomic_inc(&oo->oo_owner.so_count);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001572 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001574 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001575 if (clp->cl_cb_conn.cb_xprt)
1576 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001577 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001578 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001579 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001580 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001581 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001582 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001583 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001584 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1585 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001586 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001589static void expire_client(struct nfs4_client *clp)
1590{
1591 nfsd4_client_record_remove(clp);
1592 destroy_client(clp);
1593}
1594
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001595static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1596{
1597 memcpy(target->cl_verifier.data, source->data,
1598 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001601static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1602{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1604 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1605}
1606
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001607static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001608{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001609 if (source->cr_principal) {
1610 target->cr_principal =
1611 kstrdup(source->cr_principal, GFP_KERNEL);
1612 if (target->cr_principal == NULL)
1613 return -ENOMEM;
1614 } else
1615 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001616 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 target->cr_uid = source->cr_uid;
1618 target->cr_gid = source->cr_gid;
1619 target->cr_group_info = source->cr_group_info;
1620 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001621 target->cr_gss_mech = source->cr_gss_mech;
1622 if (source->cr_gss_mech)
1623 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001624 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001627static long long
1628compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1629{
1630 long long res;
1631
1632 res = o1->len - o2->len;
1633 if (res)
1634 return res;
1635 return (long long)memcmp(o1->data, o2->data, o1->len);
1636}
1637
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001638static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001639{
NeilBrowna55370a2005-06-23 22:03:52 -07001640 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642
1643static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001644same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1645{
1646 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
1649static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001650same_clid(clientid_t *cl1, clientid_t *cl2)
1651{
1652 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653}
1654
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001655static bool groups_equal(struct group_info *g1, struct group_info *g2)
1656{
1657 int i;
1658
1659 if (g1->ngroups != g2->ngroups)
1660 return false;
1661 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001662 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001663 return false;
1664 return true;
1665}
1666
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001667/*
1668 * RFC 3530 language requires clid_inuse be returned when the
1669 * "principal" associated with a requests differs from that previously
1670 * used. We use uid, gid's, and gss principal string as our best
1671 * approximation. We also don't want to allow non-gss use of a client
1672 * established using gss: in theory cr_principal should catch that
1673 * change, but in practice cr_principal can be null even in the gss case
1674 * since gssd doesn't always pass down a principal string.
1675 */
1676static bool is_gss_cred(struct svc_cred *cr)
1677{
1678 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1679 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1680}
1681
1682
Vivek Trivedi5559b502012-07-24 21:18:20 +05301683static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001684same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1685{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001686 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001687 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1688 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001689 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1690 return false;
1691 if (cr1->cr_principal == cr2->cr_principal)
1692 return true;
1693 if (!cr1->cr_principal || !cr2->cr_principal)
1694 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301695 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697
J. Bruce Fields57266a62013-04-13 14:27:29 -04001698static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1699{
1700 struct svc_cred *cr = &rqstp->rq_cred;
1701 u32 service;
1702
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001703 if (!cr->cr_gss_mech)
1704 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001705 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1706 return service == RPC_GSS_SVC_INTEGRITY ||
1707 service == RPC_GSS_SVC_PRIVACY;
1708}
1709
1710static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1711{
1712 struct svc_cred *cr = &rqstp->rq_cred;
1713
1714 if (!cl->cl_mach_cred)
1715 return true;
1716 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1717 return false;
1718 if (!svc_rqst_integrity_protected(rqstp))
1719 return false;
1720 if (!cr->cr_principal)
1721 return false;
1722 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1723}
1724
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001725static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001726{
1727 static u32 current_clientid = 1;
1728
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001729 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 clp->cl_clientid.cl_id = current_clientid++;
1731}
1732
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001733static void gen_confirm(struct nfs4_client *clp)
1734{
Chuck Leverab4684d2012-03-02 17:13:50 -05001735 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001736 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Jeff Laytonf4199922014-06-17 07:44:11 -04001738 /*
1739 * This is opaque to client, so no need to byte-swap. Use
1740 * __force to keep sparse happy
1741 */
1742 verf[0] = (__force __be32)get_seconds();
1743 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001744 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
1746
Jeff Layton4770d722014-07-29 21:34:10 -04001747static struct nfs4_stid *
1748find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001749{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001750 struct nfs4_stid *ret;
1751
1752 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1753 if (!ret || !ret->sc_type)
1754 return NULL;
1755 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001756}
1757
Jeff Layton4770d722014-07-29 21:34:10 -04001758static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001759find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001760{
1761 struct nfs4_stid *s;
1762
Jeff Layton4770d722014-07-29 21:34:10 -04001763 spin_lock(&cl->cl_lock);
1764 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001765 if (s != NULL) {
1766 if (typemask & s->sc_type)
1767 atomic_inc(&s->sc_count);
1768 else
1769 s = NULL;
1770 }
Jeff Layton4770d722014-07-29 21:34:10 -04001771 spin_unlock(&cl->cl_lock);
1772 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001773}
1774
Jeff Layton2216d442012-11-12 15:00:57 -05001775static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001776 struct svc_rqst *rqstp, nfs4_verifier *verf)
1777{
1778 struct nfs4_client *clp;
1779 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001780 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001781 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001782 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001783
1784 clp = alloc_client(name);
1785 if (clp == NULL)
1786 return NULL;
1787
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001788 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1789 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001790 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001791 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001792 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001793 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001794 }
Jeff Layton02e12152014-07-16 10:31:57 -04001795 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001796 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001797 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001798 copy_verf(clp, verf);
1799 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001800 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001801 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001802 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001803 return clp;
1804}
1805
NeilBrownfd39ca92005-06-23 22:04:03 -07001806static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001807add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1808{
1809 struct rb_node **new = &(root->rb_node), *parent = NULL;
1810 struct nfs4_client *clp;
1811
1812 while (*new) {
1813 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1814 parent = *new;
1815
1816 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1817 new = &((*new)->rb_left);
1818 else
1819 new = &((*new)->rb_right);
1820 }
1821
1822 rb_link_node(&new_clp->cl_namenode, parent, new);
1823 rb_insert_color(&new_clp->cl_namenode, root);
1824}
1825
1826static struct nfs4_client *
1827find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1828{
1829 long long cmp;
1830 struct rb_node *node = root->rb_node;
1831 struct nfs4_client *clp;
1832
1833 while (node) {
1834 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1835 cmp = compare_blob(&clp->cl_name, name);
1836 if (cmp > 0)
1837 node = node->rb_left;
1838 else if (cmp < 0)
1839 node = node->rb_right;
1840 else
1841 return clp;
1842 }
1843 return NULL;
1844}
1845
1846static void
1847add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
1849 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001850 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001852 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001853 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001855 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001856 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857}
1858
NeilBrownfd39ca92005-06-23 22:04:03 -07001859static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860move_to_confirmed(struct nfs4_client *clp)
1861{
1862 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001863 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001866 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001867 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001868 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001869 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 renew_client(clp);
1871}
1872
1873static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001874find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
1876 struct nfs4_client *clp;
1877 unsigned int idhashval = clientid_hashval(clid->cl_id);
1878
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001879 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001880 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001881 if ((bool)clp->cl_minorversion != sessions)
1882 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001883 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
1887 return NULL;
1888}
1889
1890static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001891find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1892{
1893 struct list_head *tbl = nn->conf_id_hashtbl;
1894
1895 return find_client_in_id_table(tbl, clid, sessions);
1896}
1897
1898static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001899find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001901 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001903 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904}
1905
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001906static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001907{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001908 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001909}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001910
NeilBrown28ce6052005-06-23 22:03:56 -07001911static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001912find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001913{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001914 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001915}
1916
1917static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001918find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001919{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001920 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001921}
1922
NeilBrownfd39ca92005-06-23 22:04:03 -07001923static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001924gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001926 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001927 struct sockaddr *sa = svc_addr(rqstp);
1928 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001929 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Jeff Layton7077ecb2009-08-14 12:57:58 -04001931 /* Currently, we only support tcp and tcp6 for the callback channel */
1932 if (se->se_callback_netid_len == 3 &&
1933 !memcmp(se->se_callback_netid_val, "tcp", 3))
1934 expected_family = AF_INET;
1935 else if (se->se_callback_netid_len == 4 &&
1936 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1937 expected_family = AF_INET6;
1938 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 goto out_err;
1940
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001941 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001942 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001943 (struct sockaddr *)&conn->cb_addr,
1944 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001945
J. Bruce Fields07263f12010-05-31 19:09:40 -04001946 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001948
J. Bruce Fields07263f12010-05-31 19:09:40 -04001949 if (conn->cb_addr.ss_family == AF_INET6)
1950 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001951
J. Bruce Fields07263f12010-05-31 19:09:40 -04001952 conn->cb_prog = se->se_callback_prog;
1953 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001954 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 return;
1956out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001957 conn->cb_addr.ss_family = AF_UNSPEC;
1958 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001959 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 "will not receive delegations\n",
1961 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 return;
1964}
1965
Andy Adamson074fe892009-04-03 08:28:15 +03001966/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04001967 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001968 */
Trond Myklebustb6076642014-06-30 11:48:35 -04001969static void
Andy Adamson074fe892009-04-03 08:28:15 +03001970nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1971{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001972 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04001973 struct nfsd4_slot *slot = resp->cstate.slot;
1974 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001975
Andy Adamson557ce262009-08-28 08:45:04 -04001976 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001977
Andy Adamson557ce262009-08-28 08:45:04 -04001978 slot->sl_opcnt = resp->opcnt;
1979 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001980
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001981 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001982 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001983 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001984 return;
1985 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001986 base = resp->cstate.data_offset;
1987 slot->sl_datalen = buf->len - base;
1988 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04001989 WARN("%s: sessions DRC could not cache compound\n", __func__);
1990 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001991}
1992
1993/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001994 * Encode the replay sequence operation from the slot values.
1995 * If cachethis is FALSE encode the uncached rep error on the next
1996 * operation which sets resp->p and increments resp->opcnt for
1997 * nfs4svc_encode_compoundres.
1998 *
Andy Adamson074fe892009-04-03 08:28:15 +03001999 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002000static __be32
2001nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2002 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002003{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002004 struct nfsd4_op *op;
2005 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002006
Andy Adamsonabfabf82009-07-23 19:02:18 -04002007 /* Encode the replayed sequence operation */
2008 op = &args->ops[resp->opcnt - 1];
2009 nfsd4_encode_operation(resp, op);
2010
2011 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002012 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002013 op = &args->ops[resp->opcnt++];
2014 op->status = nfserr_retry_uncached_rep;
2015 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002016 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002017 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002018}
2019
2020/*
Andy Adamson557ce262009-08-28 08:45:04 -04002021 * The sequence operation is not cached because we can use the slot and
2022 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002023 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002024static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002025nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2026 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002027{
Andy Adamson557ce262009-08-28 08:45:04 -04002028 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002029 struct xdr_stream *xdr = &resp->xdr;
2030 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002031 __be32 status;
2032
Andy Adamson557ce262009-08-28 08:45:04 -04002033 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002034
Andy Adamsonabfabf82009-07-23 19:02:18 -04002035 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002036 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002037 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002038
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002039 p = xdr_reserve_space(xdr, slot->sl_datalen);
2040 if (!p) {
2041 WARN_ON_ONCE(1);
2042 return nfserr_serverfault;
2043 }
2044 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2045 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002046
Andy Adamson557ce262009-08-28 08:45:04 -04002047 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002048 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002049}
2050
Andy Adamson0733d212009-04-03 08:28:01 +03002051/*
2052 * Set the exchange_id flags returned by the server.
2053 */
2054static void
2055nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2056{
2057 /* pNFS is not supported */
2058 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2059
2060 /* Referrals are supported, Migration is not. */
2061 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2062
2063 /* set the wire flags to return to client. */
2064 clid->flags = new->cl_exchange_flags;
2065}
2066
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002067static bool client_has_state(struct nfs4_client *clp)
2068{
2069 /*
2070 * Note clp->cl_openowners check isn't quite right: there's no
2071 * need to count owners without stateid's.
2072 *
2073 * Also note we should probably be using this in 4.0 case too.
2074 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002075 return !list_empty(&clp->cl_openowners)
2076 || !list_empty(&clp->cl_delegations)
2077 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002078}
2079
Al Virob37ad282006-10-19 23:28:59 -07002080__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002081nfsd4_exchange_id(struct svc_rqst *rqstp,
2082 struct nfsd4_compound_state *cstate,
2083 struct nfsd4_exchange_id *exid)
2084{
Andy Adamson0733d212009-04-03 08:28:01 +03002085 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002086 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002087 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002088 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002089 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002090 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002091 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002092
Jeff Layton363168b2009-08-14 12:57:56 -04002093 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002094 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002095 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002096 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002097 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002098
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002099 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002100 return nfserr_inval;
2101
Andy Adamson0733d212009-04-03 08:28:01 +03002102 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002103 case SP4_MACH_CRED:
2104 if (!svc_rqst_integrity_protected(rqstp))
2105 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002106 case SP4_NONE:
2107 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002108 default: /* checked by xdr code */
2109 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002110 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002111 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002112 }
2113
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002114 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002115 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002116 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002117 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002118 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2119 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2120
J. Bruce Fields136e6582012-05-12 20:37:23 -04002121 if (update) {
2122 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002123 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002124 goto out;
2125 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002126 if (!mach_creds_match(conf, rqstp)) {
2127 status = nfserr_wrong_cred;
2128 goto out;
2129 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002130 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002131 status = nfserr_perm;
2132 goto out;
2133 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002134 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002135 status = nfserr_not_same;
2136 goto out;
2137 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002138 /* case 6 */
2139 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2140 new = conf;
2141 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002142 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002143 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002144 if (client_has_state(conf)) {
2145 status = nfserr_clid_inuse;
2146 goto out;
2147 }
Andy Adamson0733d212009-04-03 08:28:01 +03002148 expire_client(conf);
2149 goto out_new;
2150 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002151 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002152 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002153 new = conf;
2154 goto out_copy;
2155 }
2156 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04002157 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002158 }
2159
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002160 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002161 status = nfserr_noent;
2162 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002163 }
2164
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002165 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002166 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03002167 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002168
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002169 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002170out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05002171 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03002172 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04002173 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03002174 goto out;
2175 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002176 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002177 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002178
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002179 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002180 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03002181out_copy:
2182 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2183 exid->clientid.cl_id = new->cl_clientid.cl_id;
2184
J. Bruce Fields778df3f2012-05-25 21:40:23 -04002185 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03002186 nfsd4_set_ex_flags(new, exid);
2187
2188 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04002189 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002190 status = nfs_ok;
2191
2192out:
2193 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03002194 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002195}
2196
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002197static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002198check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002199{
Andy Adamson88e588d2009-07-23 19:02:15 -04002200 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2201 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002202
2203 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002204 if (slot_inuse) {
2205 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002206 return nfserr_jukebox;
2207 else
2208 return nfserr_seq_misordered;
2209 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002210 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002211 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002212 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002213 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002214 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002215 return nfserr_seq_misordered;
2216}
2217
Andy Adamson49557cc2009-07-23 19:02:16 -04002218/*
2219 * Cache the create session result into the create session single DRC
2220 * slot cache by saving the xdr structure. sl_seqid has been set.
2221 * Do this for solo or embedded create session operations.
2222 */
2223static void
2224nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002225 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002226{
2227 slot->sl_status = nfserr;
2228 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2229}
2230
2231static __be32
2232nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2233 struct nfsd4_clid_slot *slot)
2234{
2235 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2236 return slot->sl_status;
2237}
2238
Mi Jinlong1b74c252011-07-14 14:50:17 +08002239#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2240 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2241 1 + /* MIN tag is length with zero, only length */ \
2242 3 + /* version, opcount, opcode */ \
2243 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2244 /* seqid, slotID, slotID, cache */ \
2245 4 ) * sizeof(__be32))
2246
2247#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2248 2 + /* verifier: AUTH_NULL, length 0 */\
2249 1 + /* status */ \
2250 1 + /* MIN tag is length with zero, only length */ \
2251 3 + /* opcount, opcode, opstatus*/ \
2252 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2253 /* seqid, slotID, slotID, slotID, status */ \
2254 5 ) * sizeof(__be32))
2255
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002256static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002257{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002258 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2259
J. Bruce Fields373cd402013-04-08 15:42:12 -04002260 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2261 return nfserr_toosmall;
2262 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2263 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002264 ca->headerpadsz = 0;
2265 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2266 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2267 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2268 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2269 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2270 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2271 /*
2272 * Note decreasing slot size below client's request may make it
2273 * difficult for client to function correctly, whereas
2274 * decreasing the number of slots will (just?) affect
2275 * performance. When short on memory we therefore prefer to
2276 * decrease number of slots instead of their size. Clients that
2277 * request larger slots than they need will get poor results:
2278 */
2279 ca->maxreqs = nfsd4_get_drc_mem(ca);
2280 if (!ca->maxreqs)
2281 return nfserr_jukebox;
2282
J. Bruce Fields373cd402013-04-08 15:42:12 -04002283 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002284}
2285
Kinglong Mee8a891632013-12-23 18:11:02 +08002286#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2287 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2288#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2289 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2290
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002291static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2292{
2293 ca->headerpadsz = 0;
2294
2295 /*
2296 * These RPC_MAX_HEADER macros are overkill, especially since we
2297 * don't even do gss on the backchannel yet. But this is still
2298 * less than 1k. Tighten up this estimate in the unlikely event
2299 * it turns out to be a problem for some client:
2300 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002301 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002302 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002303 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002304 return nfserr_toosmall;
2305 ca->maxresp_cached = 0;
2306 if (ca->maxops < 2)
2307 return nfserr_toosmall;
2308
2309 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002310}
2311
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002312static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2313{
2314 switch (cbs->flavor) {
2315 case RPC_AUTH_NULL:
2316 case RPC_AUTH_UNIX:
2317 return nfs_ok;
2318 default:
2319 /*
2320 * GSS case: the spec doesn't allow us to return this
2321 * error. But it also doesn't allow us not to support
2322 * GSS.
2323 * I'd rather this fail hard than return some error the
2324 * client might think it can already handle:
2325 */
2326 return nfserr_encr_alg_unsupp;
2327 }
2328}
2329
Andy Adamson069b6ad2009-04-03 08:27:58 +03002330__be32
2331nfsd4_create_session(struct svc_rqst *rqstp,
2332 struct nfsd4_compound_state *cstate,
2333 struct nfsd4_create_session *cr_ses)
2334{
Jeff Layton363168b2009-08-14 12:57:56 -04002335 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002336 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002337 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002338 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002339 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002340 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002341 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002342
Mi Jinlonga62573d2011-03-23 17:57:07 +08002343 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2344 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002345 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2346 if (status)
2347 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002348 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002349 if (status)
2350 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002351 status = check_backchannel_attrs(&cr_ses->back_channel);
2352 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002353 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002354 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002355 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002356 if (!new)
2357 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002358 conn = alloc_conn_from_crses(rqstp, cr_ses);
2359 if (!conn)
2360 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002361
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002362 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002363 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002364 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002365 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002366
2367 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002368 status = nfserr_wrong_cred;
2369 if (!mach_creds_match(conf, rqstp))
2370 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002371 cs_slot = &conf->cl_cs_slot;
2372 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002373 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002374 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002375 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002376 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002377 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002378 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002379 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002380 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002381 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002382 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002383 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002384 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002385 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002386 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002387 status = nfserr_wrong_cred;
2388 if (!mach_creds_match(unconf, rqstp))
2389 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002390 cs_slot = &unconf->cl_cs_slot;
2391 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002392 if (status) {
2393 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002394 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002395 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002396 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002397 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002398 if (old) {
2399 status = mark_client_expired(old);
2400 if (status)
2401 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002402 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002403 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002404 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002405 conf = unconf;
2406 } else {
2407 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002408 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002409 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002410 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002411 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002412 * We do not support RDMA or persistent sessions
2413 */
2414 cr_ses->flags &= ~SESSION4_PERSIST;
2415 cr_ses->flags &= ~SESSION4_RDMA;
2416
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002417 init_session(rqstp, new, conf, cr_ses);
2418 nfsd4_init_conn(rqstp, conn, new);
2419
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002420 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002421 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002422 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002423 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002424
Andy Adamson49557cc2009-07-23 19:02:16 -04002425 /* cache solo and embedded create sessions under the state lock */
2426 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002427 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002428 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002429out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08002430 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002431 free_conn(conn);
2432out_free_session:
2433 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002434out_release_drc_mem:
2435 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002436 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002437}
2438
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002439static __be32 nfsd4_map_bcts_dir(u32 *dir)
2440{
2441 switch (*dir) {
2442 case NFS4_CDFC4_FORE:
2443 case NFS4_CDFC4_BACK:
2444 return nfs_ok;
2445 case NFS4_CDFC4_FORE_OR_BOTH:
2446 case NFS4_CDFC4_BACK_OR_BOTH:
2447 *dir = NFS4_CDFC4_BOTH;
2448 return nfs_ok;
2449 };
2450 return nfserr_inval;
2451}
2452
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002453__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2454{
2455 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002456 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002457 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002458
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002459 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2460 if (status)
2461 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002462 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002463 session->se_cb_prog = bc->bc_cb_program;
2464 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002465 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002466
2467 nfsd4_probe_callback(session->se_client);
2468
2469 return nfs_ok;
2470}
2471
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002472__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2473 struct nfsd4_compound_state *cstate,
2474 struct nfsd4_bind_conn_to_session *bcts)
2475{
2476 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002477 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002478 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002479 struct net *net = SVC_NET(rqstp);
2480 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002481
2482 if (!nfsd4_last_compound_op(rqstp))
2483 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002484 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002485 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002486 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002487 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002488 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002489 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002490 status = nfserr_wrong_cred;
2491 if (!mach_creds_match(session->se_client, rqstp))
2492 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002493 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002494 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002495 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002496 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002497 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002498 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002499 goto out;
2500 nfsd4_init_conn(rqstp, conn, session);
2501 status = nfs_ok;
2502out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002503 nfsd4_put_session(session);
2504out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002505 nfs4_unlock_state();
2506 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002507}
2508
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002509static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2510{
2511 if (!session)
2512 return 0;
2513 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2514}
2515
Andy Adamson069b6ad2009-04-03 08:27:58 +03002516__be32
2517nfsd4_destroy_session(struct svc_rqst *r,
2518 struct nfsd4_compound_state *cstate,
2519 struct nfsd4_destroy_session *sessionid)
2520{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002521 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002522 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002523 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002524 struct net *net = SVC_NET(r);
2525 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002526
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002527 nfs4_lock_state();
2528 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002529 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002530 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002531 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002532 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002533 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002534 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002535 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002536 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002537 if (!ses)
2538 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002539 status = nfserr_wrong_cred;
2540 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002541 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002542 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002543 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002544 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002545 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002546 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002547
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002548 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002549
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002550 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002551 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002552out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002553 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002554out_client_lock:
2555 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002556out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002557 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002558 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002559}
2560
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002561static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002562{
2563 struct nfsd4_conn *c;
2564
2565 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002566 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002567 return c;
2568 }
2569 }
2570 return NULL;
2571}
2572
J. Bruce Fields57266a62013-04-13 14:27:29 -04002573static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002574{
2575 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002576 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002577 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002578 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002579
2580 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002581 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002582 if (c)
2583 goto out_free;
2584 status = nfserr_conn_not_bound_to_session;
2585 if (clp->cl_mach_cred)
2586 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002587 __nfsd4_hash_conn(new, ses);
2588 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002589 ret = nfsd4_register_conn(new);
2590 if (ret)
2591 /* oops; xprt is already down: */
2592 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002593 return nfs_ok;
2594out_free:
2595 spin_unlock(&clp->cl_lock);
2596 free_conn(new);
2597 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002598}
2599
Mi Jinlong868b89c2011-04-27 09:09:58 +08002600static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2601{
2602 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2603
2604 return args->opcnt > session->se_fchannel.maxops;
2605}
2606
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002607static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2608 struct nfsd4_session *session)
2609{
2610 struct xdr_buf *xb = &rqstp->rq_arg;
2611
2612 return xb->len > session->se_fchannel.maxreq_sz;
2613}
2614
Andy Adamson069b6ad2009-04-03 08:27:58 +03002615__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002616nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002617 struct nfsd4_compound_state *cstate,
2618 struct nfsd4_sequence *seq)
2619{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002620 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002621 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002622 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002623 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002624 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002625 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002626 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002627 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002628 struct net *net = SVC_NET(rqstp);
2629 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002630
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002631 if (resp->opcnt != 1)
2632 return nfserr_sequence_pos;
2633
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002634 /*
2635 * Will be either used or freed by nfsd4_sequence_check_conn
2636 * below.
2637 */
2638 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2639 if (!conn)
2640 return nfserr_jukebox;
2641
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002642 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002643 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002644 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002645 goto out_no_session;
2646 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002647
Mi Jinlong868b89c2011-04-27 09:09:58 +08002648 status = nfserr_too_many_ops;
2649 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002650 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002651
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002652 status = nfserr_req_too_big;
2653 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002654 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002655
Benny Halevyb85d4c02009-04-03 08:28:08 +03002656 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002657 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002658 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002659
Andy Adamson557ce262009-08-28 08:45:04 -04002660 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002661 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2662
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002663 /* We do not negotiate the number of slots yet, so set the
2664 * maxslots to the session maxreqs which is used to encode
2665 * sr_highest_slotid and the sr_target_slot id to maxslots */
2666 seq->maxslots = session->se_fchannel.maxreqs;
2667
J. Bruce Fields73e79482012-02-13 16:39:00 -05002668 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2669 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002670 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002671 status = nfserr_seq_misordered;
2672 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002673 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002674 cstate->slot = slot;
2675 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002676 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002677 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002678 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002679 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002680 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002681 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002682 }
2683 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002684 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002685
J. Bruce Fields57266a62013-04-13 14:27:29 -04002686 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002687 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002688 if (status)
2689 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002690
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002691 buflen = (seq->cachethis) ?
2692 session->se_fchannel.maxresp_cached :
2693 session->se_fchannel.maxresp_sz;
2694 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2695 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002696 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002697 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002698 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002699
2700 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002701 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002702 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002703 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002704 if (seq->cachethis)
2705 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002706 else
2707 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002708
2709 cstate->slot = slot;
2710 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002711 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002712
Benny Halevyb85d4c02009-04-03 08:28:08 +03002713out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002714 switch (clp->cl_cb_state) {
2715 case NFSD4_CB_DOWN:
2716 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2717 break;
2718 case NFSD4_CB_FAULT:
2719 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2720 break;
2721 default:
2722 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002723 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002724 if (!list_empty(&clp->cl_revoked))
2725 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002726out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002727 if (conn)
2728 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002729 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002730 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002731out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002732 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002733 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002734}
2735
Trond Myklebustb6076642014-06-30 11:48:35 -04002736void
2737nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2738{
2739 struct nfsd4_compound_state *cs = &resp->cstate;
2740
2741 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002742 if (cs->status != nfserr_replay_cache) {
2743 nfsd4_store_cache_entry(resp);
2744 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2745 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002746 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002747 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002748 } else if (cs->clp)
2749 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002750}
2751
Mi Jinlong345c2842011-10-20 17:51:39 +08002752__be32
2753nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2754{
2755 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002756 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002757 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002758
2759 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002760 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002761 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002762 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002763
2764 if (conf) {
2765 clp = conf;
2766
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002767 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002768 status = nfserr_clientid_busy;
2769 goto out;
2770 }
2771 } else if (unconf)
2772 clp = unconf;
2773 else {
2774 status = nfserr_stale_clientid;
2775 goto out;
2776 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002777 if (!mach_creds_match(clp, rqstp)) {
2778 status = nfserr_wrong_cred;
2779 goto out;
2780 }
Mi Jinlong345c2842011-10-20 17:51:39 +08002781 expire_client(clp);
2782out:
2783 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002784 return status;
2785}
2786
Andy Adamson069b6ad2009-04-03 08:27:58 +03002787__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002788nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2789{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002790 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002791
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002792 if (rc->rca_one_fs) {
2793 if (!cstate->current_fh.fh_dentry)
2794 return nfserr_nofilehandle;
2795 /*
2796 * We don't take advantage of the rca_one_fs case.
2797 * That's OK, it's optional, we can safely ignore it.
2798 */
2799 return nfs_ok;
2800 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002801
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002802 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002803 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002804 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2805 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002806 goto out;
2807
2808 status = nfserr_stale_clientid;
2809 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002810 /*
2811 * The following error isn't really legal.
2812 * But we only get here if the client just explicitly
2813 * destroyed the client. Surely it no longer cares what
2814 * error it gets back on an operation for the dead
2815 * client.
2816 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002817 goto out;
2818
2819 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002820 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002821out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002822 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002823 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002824}
2825
2826__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002827nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2828 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002830 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002832 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002833 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002834 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2835
J. Bruce Fields63db4632012-05-18 21:54:19 -04002836 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002838 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002839 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002840 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002842 if (clp_used_exchangeid(conf))
2843 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002844 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002845 char addr_str[INET6_ADDRSTRLEN];
2846 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2847 sizeof(addr_str));
2848 dprintk("NFSD: setclientid: string in use by client "
2849 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 goto out;
2851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002853 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002854 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002857 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002858 if (new == NULL)
2859 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002860 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002861 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002863 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002864 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002865 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002866 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002867 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2869 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2870 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2871 status = nfs_ok;
2872out:
2873 nfs4_unlock_state();
2874 return status;
2875}
2876
2877
Al Virob37ad282006-10-19 23:28:59 -07002878__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002879nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2880 struct nfsd4_compound_state *cstate,
2881 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882{
NeilBrown21ab45a2005-06-23 22:04:14 -07002883 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2885 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002886 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002887 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002889 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002892
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002893 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002894 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002895 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002896 * We try hard to give out unique clientid's, so if we get an
2897 * attempt to confirm the same clientid with a different cred,
2898 * there's a bug somewhere. Let's charitably assume it's our
2899 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002900 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002901 status = nfserr_serverfault;
2902 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2903 goto out;
2904 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2905 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002906 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002907 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2908 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002910 else /* case 4: client hasn't noticed we rebooted yet? */
2911 status = nfserr_stale_clientid;
2912 goto out;
2913 }
2914 status = nfs_ok;
2915 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002916 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2917 nfsd4_probe_callback(conf);
2918 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002919 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002920 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002921 if (conf) {
2922 status = mark_client_expired(conf);
2923 if (status)
2924 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002925 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002926 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002927 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002928 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 nfs4_unlock_state();
2932 return status;
2933}
2934
J. Bruce Fields32513b42011-10-13 16:00:16 -04002935static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002937 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2938}
2939
2940/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04002941static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04002942{
Trond Myklebustca943212014-07-23 16:17:39 -04002943 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Trond Myklebust950e0112014-06-30 11:48:31 -04002945 lockdep_assert_held(&state_lock);
2946
J. Bruce Fields32513b42011-10-13 16:00:16 -04002947 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04002948 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002949 INIT_LIST_HEAD(&fp->fi_stateids);
2950 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04002951 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002952 fp->fi_had_conflict = false;
2953 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04002954 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002955 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2956 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04002957 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958}
2959
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002960void
NeilBrowne60d4392005-06-23 22:03:01 -07002961nfsd4_free_slabs(void)
2962{
Christoph Hellwigabf11352014-05-21 07:43:03 -07002963 kmem_cache_destroy(openowner_slab);
2964 kmem_cache_destroy(lockowner_slab);
2965 kmem_cache_destroy(file_slab);
2966 kmem_cache_destroy(stateid_slab);
2967 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002968}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
Bryan Schumaker72083392011-11-01 15:24:59 -04002970int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971nfsd4_init_slabs(void)
2972{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002973 openowner_slab = kmem_cache_create("nfsd4_openowners",
2974 sizeof(struct nfs4_openowner), 0, 0, NULL);
2975 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002976 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002977 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002978 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002979 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002980 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07002981 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002982 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002983 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002984 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07002985 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002986 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002987 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002988 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002989 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002990 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002991 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002992 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07002994
2995out_free_stateid_slab:
2996 kmem_cache_destroy(stateid_slab);
2997out_free_file_slab:
2998 kmem_cache_destroy(file_slab);
2999out_free_lockowner_slab:
3000 kmem_cache_destroy(lockowner_slab);
3001out_free_openowner_slab:
3002 kmem_cache_destroy(openowner_slab);
3003out:
NeilBrowne60d4392005-06-23 22:03:01 -07003004 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3005 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006}
3007
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003008static void init_nfs4_replay(struct nfs4_replay *rp)
3009{
3010 rp->rp_status = nfserr_serverfault;
3011 rp->rp_buflen = 0;
3012 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003013 mutex_init(&rp->rp_mutex);
3014}
3015
3016static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3017 struct nfs4_stateowner *so)
3018{
3019 if (!nfsd4_has_session(cstate)) {
3020 mutex_lock(&so->so_replay.rp_mutex);
3021 cstate->replay_owner = so;
3022 atomic_inc(&so->so_count);
3023 }
3024}
3025
3026void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3027{
3028 struct nfs4_stateowner *so = cstate->replay_owner;
3029
3030 if (so != NULL) {
3031 cstate->replay_owner = NULL;
3032 mutex_unlock(&so->so_replay.rp_mutex);
3033 nfs4_put_stateowner(so);
3034 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003035}
3036
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003037static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
3039 struct nfs4_stateowner *sop;
3040
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003041 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003042 if (!sop)
3043 return NULL;
3044
3045 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3046 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003047 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003048 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003050 sop->so_owner.len = owner->len;
3051
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003052 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003053 sop->so_client = clp;
3054 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003055 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003056 return sop;
3057}
3058
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003059static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003060{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003061 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3062
3063 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003064 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065}
3066
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003067static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3068{
3069 struct nfs4_openowner *oo = openowner(so);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003070 struct nfsd_net *nn = net_generic(so->so_client->net, nfsd_net_id);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003071
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003072 spin_lock(&nn->client_lock);
3073 unhash_openowner_locked(oo);
3074 spin_unlock(&nn->client_lock);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003075}
3076
Jeff Layton6b180f02014-07-29 21:34:26 -04003077static void nfs4_free_openowner(struct nfs4_stateowner *so)
3078{
3079 struct nfs4_openowner *oo = openowner(so);
3080
3081 kmem_cache_free(openowner_slab, oo);
3082}
3083
3084static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003085 .so_unhash = nfs4_unhash_openowner,
3086 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003087};
3088
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003089static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003090alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003091 struct nfsd4_compound_state *cstate)
3092{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003093 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003094 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3095 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003097 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3098 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003100 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003101 oo->oo_owner.so_is_open_owner = 1;
3102 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003103 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003104 if (nfsd4_has_session(cstate))
3105 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003106 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003107 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003108 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003109 spin_lock(&nn->client_lock);
3110 ret = find_openstateowner_str_locked(strhashval,
3111 open, clp->cl_minorversion, nn);
3112 if (ret == NULL) {
3113 hash_openowner(oo, clp, strhashval);
3114 ret = oo;
3115 } else
3116 nfs4_free_openowner(&oo->oo_owner);
3117 spin_unlock(&nn->client_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003118 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119}
3120
J. Bruce Fields996e0932011-10-17 11:14:48 -04003121static 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 -04003122 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003124 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003125 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003126 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003127 stp->st_stateowner = &oo->oo_owner;
Jeff Laytond3134b12014-07-29 21:34:32 -04003128 atomic_inc(&stp->st_stateowner->so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07003129 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003130 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 stp->st_access_bmap = 0;
3132 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003133 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003134 spin_lock(&oo->oo_owner.so_client->cl_lock);
3135 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003136 spin_lock(&fp->fi_lock);
3137 list_add(&stp->st_perfile, &fp->fi_stateids);
3138 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003139 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140}
3141
Jeff Laytond3134b12014-07-29 21:34:32 -04003142/*
3143 * In the 4.0 case we need to keep the owners around a little while to handle
3144 * CLOSE replay. We still do need to release any file access that is held by
3145 * them before returning however.
3146 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003148move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149{
Jeff Laytond3134b12014-07-29 21:34:32 -04003150 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3151 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3152 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003153
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003154 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
Jeff Laytonb401be222014-07-29 21:34:33 -04003156 /*
3157 * We know that we hold one reference via nfsd4_close, and another
3158 * "persistent" reference for the client. If the refcount is higher
3159 * than 2, then there are still calls in progress that are using this
3160 * stateid. We can't put the sc_file reference until they are finished.
3161 * Wait for the refcount to drop to 2. Since it has been unhashed,
3162 * there should be no danger of the refcount going back up again at
3163 * this point.
3164 */
3165 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3166
Jeff Laytond3134b12014-07-29 21:34:32 -04003167 release_all_access(s);
3168 if (s->st_stid.sc_file) {
3169 put_nfs4_file(s->st_stid.sc_file);
3170 s->st_stid.sc_file = NULL;
3171 }
3172 release_last_closed_stateid(oo);
3173 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003174 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003175 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176}
3177
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178/* search file_hashtbl[] for file */
3179static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003180find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181{
Trond Myklebustca943212014-07-23 16:17:39 -04003182 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 struct nfs4_file *fp;
3184
Trond Myklebust950e0112014-06-30 11:48:31 -04003185 lockdep_assert_held(&state_lock);
3186
Jeff Layton89876f82013-04-02 09:01:59 -04003187 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003188 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003189 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 }
3193 return NULL;
3194}
3195
Trond Myklebust950e0112014-06-30 11:48:31 -04003196static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003197find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003198{
3199 struct nfs4_file *fp;
3200
3201 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003202 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003203 spin_unlock(&state_lock);
3204 return fp;
3205}
3206
3207static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003208find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003209{
3210 struct nfs4_file *fp;
3211
3212 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003213 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003214 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003215 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003216 fp = new;
3217 }
3218 spin_unlock(&state_lock);
3219
3220 return fp;
3221}
3222
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003223/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 * Called to check deny when READ with all zero stateid or
3225 * WRITE with all zero or all one stateid
3226 */
Al Virob37ad282006-10-19 23:28:59 -07003227static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3229{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003231 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
Trond Myklebustca943212014-07-23 16:17:39 -04003233 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003234 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003235 return ret;
3236 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003237 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003238 if (fp->fi_share_deny & deny_type)
3239 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003240 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003241 put_nfs4_file(fp);
3242 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243}
3244
Jeff Layton02e12152014-07-16 10:31:57 -04003245void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246{
Trond Myklebust11b91642014-07-29 21:34:08 -04003247 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3248 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003249
Trond Myklebust11b91642014-07-29 21:34:08 -04003250 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003251
Jeff Layton02e12152014-07-16 10:31:57 -04003252 /*
3253 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003254 * already holding inode->i_lock.
3255 *
Jeff Laytondff13992014-07-08 14:02:49 -04003256 * If the dl_time != 0, then we know that it has already been
3257 * queued for a lease break. Don't queue it again.
3258 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003259 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003260 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003261 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003262 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003263 }
Jeff Layton02e12152014-07-16 10:31:57 -04003264 spin_unlock(&state_lock);
3265}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
Jeff Layton02e12152014-07-16 10:31:57 -04003267static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3268{
3269 /*
3270 * We're assuming the state code never drops its reference
3271 * without first removing the lease. Since we're in this lease
3272 * callback (and since the lease code is serialized by the kernel
3273 * lock) we know the server hasn't removed the lease yet, we know
3274 * it's safe to take a reference.
3275 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003276 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003277 nfsd4_cb_recall(dp);
3278}
3279
Jeff Layton1c8c6012013-06-21 08:58:15 -04003280/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003281static void nfsd_break_deleg_cb(struct file_lock *fl)
3282{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003283 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3284 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003285
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003286 if (!fp) {
3287 WARN(1, "(%p)->fl_owner NULL\n", fl);
3288 return;
3289 }
3290 if (fp->fi_had_conflict) {
3291 WARN(1, "duplicate break on %p\n", fp);
3292 return;
3293 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003294 /*
3295 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003296 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003297 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003298 */
3299 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300
Jeff Layton02e12152014-07-16 10:31:57 -04003301 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003302 fp->fi_had_conflict = true;
3303 /*
3304 * If there are no delegations on the list, then we can't count on this
3305 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3306 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3307 * true should keep any new delegations from being hashed.
3308 */
3309 if (list_empty(&fp->fi_delegations))
3310 fl->fl_break_time = jiffies;
3311 else
3312 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3313 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003314 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315}
3316
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317static
3318int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3319{
3320 if (arg & F_UNLCK)
3321 return lease_modify(onlist, arg);
3322 else
3323 return -EAGAIN;
3324}
3325
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003326static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003327 .lm_break = nfsd_break_deleg_cb,
3328 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329};
3330
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003331static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3332{
3333 if (nfsd4_has_session(cstate))
3334 return nfs_ok;
3335 if (seqid == so->so_seqid - 1)
3336 return nfserr_replay_me;
3337 if (seqid == so->so_seqid)
3338 return nfs_ok;
3339 return nfserr_bad_seqid;
3340}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
Jeff Layton4b24ca72014-06-30 11:48:44 -04003342static __be32 lookup_clientid(clientid_t *clid,
3343 struct nfsd4_compound_state *cstate,
3344 struct nfsd_net *nn)
3345{
3346 struct nfs4_client *found;
3347
3348 if (cstate->clp) {
3349 found = cstate->clp;
3350 if (!same_clid(&found->cl_clientid, clid))
3351 return nfserr_stale_clientid;
3352 return nfs_ok;
3353 }
3354
3355 if (STALE_CLIENTID(clid, nn))
3356 return nfserr_stale_clientid;
3357
3358 /*
3359 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3360 * cached already then we know this is for is for v4.0 and "sessions"
3361 * will be false.
3362 */
3363 WARN_ON_ONCE(cstate->session);
3364 found = find_confirmed_client(clid, false, nn);
3365 if (!found)
3366 return nfserr_expired;
3367
3368 /* Cache the nfs4_client in cstate! */
3369 cstate->clp = found;
3370 atomic_inc(&found->cl_refcount);
3371 return nfs_ok;
3372}
3373
Al Virob37ad282006-10-19 23:28:59 -07003374__be32
Andy Adamson66689582009-04-03 08:28:45 +03003375nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003376 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 clientid_t *clientid = &open->op_clientid;
3379 struct nfs4_client *clp = NULL;
3380 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003381 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003382 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003384 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003386 /*
3387 * In case we need it later, after we've already created the
3388 * file and don't want to risk a further failure:
3389 */
3390 open->op_file = nfsd4_alloc_file();
3391 if (open->op_file == NULL)
3392 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Trond Myklebust2d91e892014-06-30 11:48:46 -04003394 status = lookup_clientid(clientid, cstate, nn);
3395 if (status)
3396 return status;
3397 clp = cstate->clp;
3398
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003399 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003400 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003401 open->op_openowner = oo;
3402 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003403 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003405 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003406 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003407 release_openowner(oo);
3408 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003409 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003410 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003411 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3412 if (status)
3413 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003414 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003415new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003416 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003417 if (oo == NULL)
3418 return nfserr_jukebox;
3419 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003420alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003421 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003422 if (!open->op_stp)
3423 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003424 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425}
3426
Al Virob37ad282006-10-19 23:28:59 -07003427static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003428nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3429{
3430 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3431 return nfserr_openmode;
3432 else
3433 return nfs_ok;
3434}
3435
Daniel Mackc47d8322011-05-16 16:38:14 +02003436static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003437{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003438 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3439}
3440
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003441static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003442{
3443 struct nfs4_stid *ret;
3444
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003445 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003446 if (!ret)
3447 return NULL;
3448 return delegstateid(ret);
3449}
3450
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003451static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3452{
3453 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3454 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3455}
3456
Al Virob37ad282006-10-19 23:28:59 -07003457static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003458nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003459 struct nfs4_delegation **dp)
3460{
3461 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003462 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003463 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003464
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003465 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3466 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003467 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003468 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003469 status = nfs4_check_delegmode(deleg, flags);
3470 if (status) {
3471 nfs4_put_stid(&deleg->dl_stid);
3472 goto out;
3473 }
3474 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003475out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003476 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003477 return nfs_ok;
3478 if (status)
3479 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003480 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003481 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003482}
3483
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003484static struct nfs4_ol_stateid *
3485nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003487 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003488 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
Trond Myklebust1d31a252014-07-10 14:07:25 -04003490 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003491 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 /* ignore lock owners */
3493 if (local->st_stateowner->so_is_open_owner == 0)
3494 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003495 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003496 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003497 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003498 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003501 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003502 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503}
3504
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003505static inline int nfs4_access_to_access(u32 nfs4_access)
3506{
3507 int flags = 0;
3508
3509 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3510 flags |= NFSD_MAY_READ;
3511 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3512 flags |= NFSD_MAY_WRITE;
3513 return flags;
3514}
3515
Al Virob37ad282006-10-19 23:28:59 -07003516static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3518 struct nfsd4_open *open)
3519{
3520 struct iattr iattr = {
3521 .ia_valid = ATTR_SIZE,
3522 .ia_size = 0,
3523 };
3524 if (!open->op_truncate)
3525 return 0;
3526 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003527 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3529}
3530
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003531static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003532 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3533 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003534{
Trond Myklebustde186432014-07-10 14:07:26 -04003535 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003536 __be32 status;
3537 int oflag = nfs4_access_to_omode(open->op_share_access);
3538 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003539 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003540
Trond Myklebustde186432014-07-10 14:07:26 -04003541 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003542
3543 /*
3544 * Are we trying to set a deny mode that would conflict with
3545 * current access?
3546 */
3547 status = nfs4_file_check_deny(fp, open->op_share_deny);
3548 if (status != nfs_ok) {
3549 spin_unlock(&fp->fi_lock);
3550 goto out;
3551 }
3552
3553 /* set access to the file */
3554 status = nfs4_file_get_access(fp, open->op_share_access);
3555 if (status != nfs_ok) {
3556 spin_unlock(&fp->fi_lock);
3557 goto out;
3558 }
3559
3560 /* Set access bits in stateid */
3561 old_access_bmap = stp->st_access_bmap;
3562 set_access(open->op_share_access, stp);
3563
3564 /* Set new deny mask */
3565 old_deny_bmap = stp->st_deny_bmap;
3566 set_deny(open->op_share_deny, stp);
3567 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3568
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003569 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003570 spin_unlock(&fp->fi_lock);
3571 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003572 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003573 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003574 spin_lock(&fp->fi_lock);
3575 if (!fp->fi_fds[oflag]) {
3576 fp->fi_fds[oflag] = filp;
3577 filp = NULL;
3578 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003579 }
Trond Myklebustde186432014-07-10 14:07:26 -04003580 spin_unlock(&fp->fi_lock);
3581 if (filp)
3582 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003583
3584 status = nfsd4_truncate(rqstp, cur_fh, open);
3585 if (status)
3586 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003587out:
3588 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003589out_put_access:
3590 stp->st_access_bmap = old_access_bmap;
3591 nfs4_file_put_access(fp, open->op_share_access);
3592 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3593 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003594}
3595
Al Virob37ad282006-10-19 23:28:59 -07003596static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003597nfs4_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 -07003598{
Al Virob37ad282006-10-19 23:28:59 -07003599 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003600 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003602 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003603 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003604
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003605 /* test and set deny mode */
3606 spin_lock(&fp->fi_lock);
3607 status = nfs4_file_check_deny(fp, open->op_share_deny);
3608 if (status == nfs_ok) {
3609 old_deny_bmap = stp->st_deny_bmap;
3610 set_deny(open->op_share_deny, stp);
3611 fp->fi_share_deny |=
3612 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3613 }
3614 spin_unlock(&fp->fi_lock);
3615
3616 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003619 status = nfsd4_truncate(rqstp, cur_fh, open);
3620 if (status != nfs_ok)
3621 reset_union_bmap_deny(old_deny_bmap, stp);
3622 return status;
3623}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003626nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003628 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629}
3630
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003631/* Should we give out recallable state?: */
3632static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3633{
3634 if (clp->cl_cb_state == NFSD4_CB_UP)
3635 return true;
3636 /*
3637 * In the sessions case, since we don't have to establish a
3638 * separate connection for callbacks, we assume it's OK
3639 * until we hear otherwise:
3640 */
3641 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3642}
3643
Jeff Laytond564fbe2014-07-16 10:31:58 -04003644static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003645{
3646 struct file_lock *fl;
3647
3648 fl = locks_alloc_lock();
3649 if (!fl)
3650 return NULL;
3651 locks_init_lock(fl);
3652 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003653 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003654 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3655 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003656 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003657 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003658 return fl;
3659}
3660
J. Bruce Fields99c41512013-05-21 16:21:25 -04003661static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003662{
Trond Myklebust11b91642014-07-29 21:34:08 -04003663 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003664 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003665 struct file *filp;
3666 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003667
Jeff Laytond564fbe2014-07-16 10:31:58 -04003668 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003669 if (!fl)
3670 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003671 filp = find_readable_file(fp);
3672 if (!filp) {
3673 /* We should always have a readable file here */
3674 WARN_ON_ONCE(1);
3675 return -EBADF;
3676 }
3677 fl->fl_file = filp;
3678 status = vfs_setlease(filp, fl->fl_type, &fl);
3679 if (status) {
3680 locks_free_lock(fl);
3681 goto out_fput;
3682 }
Benny Halevycdc97502014-05-30 09:09:30 -04003683 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003684 spin_lock(&fp->fi_lock);
3685 /* Did the lease get broken before we took the lock? */
3686 status = -EAGAIN;
3687 if (fp->fi_had_conflict)
3688 goto out_unlock;
3689 /* Race breaker */
3690 if (fp->fi_lease) {
3691 status = 0;
3692 atomic_inc(&fp->fi_delegees);
3693 hash_delegation_locked(dp, fp);
3694 goto out_unlock;
3695 }
3696 fp->fi_lease = fl;
3697 fp->fi_deleg_file = filp;
3698 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003699 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003700 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003701 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003702 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003703out_unlock:
3704 spin_unlock(&fp->fi_lock);
3705 spin_unlock(&state_lock);
3706out_fput:
3707 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003708 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003709}
3710
Jeff Layton0b266932014-07-25 07:34:25 -04003711static struct nfs4_delegation *
3712nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3713 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003714{
Jeff Layton0b266932014-07-25 07:34:25 -04003715 int status;
3716 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003717
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003718 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003719 return ERR_PTR(-EAGAIN);
3720
3721 dp = alloc_init_deleg(clp, fh);
3722 if (!dp)
3723 return ERR_PTR(-ENOMEM);
3724
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003725 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003726 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003727 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003728 dp->dl_stid.sc_file = fp;
Jeff Layton417c6622014-07-21 09:34:57 -04003729 if (!fp->fi_lease) {
3730 spin_unlock(&fp->fi_lock);
3731 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003732 status = nfs4_setlease(dp);
3733 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003734 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003735 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003736 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003737 status = -EAGAIN;
3738 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003739 }
Benny Halevy931ee562014-05-30 09:09:27 -04003740 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003741 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003742out_unlock:
3743 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003744 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003745out:
3746 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003747 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003748 return ERR_PTR(status);
3749 }
3750 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003751}
3752
Benny Halevy4aa89132012-02-21 14:16:44 -08003753static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3754{
3755 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3756 if (status == -EAGAIN)
3757 open->op_why_no_deleg = WND4_CONTENTION;
3758 else {
3759 open->op_why_no_deleg = WND4_RESOURCE;
3760 switch (open->op_deleg_want) {
3761 case NFS4_SHARE_WANT_READ_DELEG:
3762 case NFS4_SHARE_WANT_WRITE_DELEG:
3763 case NFS4_SHARE_WANT_ANY_DELEG:
3764 break;
3765 case NFS4_SHARE_WANT_CANCEL:
3766 open->op_why_no_deleg = WND4_CANCELLED;
3767 break;
3768 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003769 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003770 }
3771 }
3772}
3773
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774/*
3775 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003776 *
3777 * Note we don't support write delegations, and won't until the vfs has
3778 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 */
3780static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003781nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3782 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783{
3784 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003785 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3786 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003787 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003788 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003790 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003791 open->op_recall = 0;
3792 switch (open->op_claim_type) {
3793 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003794 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003795 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003796 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3797 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003798 break;
3799 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003800 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003801 /*
3802 * Let's not give out any delegations till everyone's
3803 * had the chance to reclaim theirs....
3804 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003805 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003806 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003807 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003808 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003809 /*
3810 * Also, if the file was opened for write or
3811 * create, there's a good chance the client's
3812 * about to write to it, resulting in an
3813 * immediate recall (since we don't support
3814 * write delegations):
3815 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003816 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003817 goto out_no_deleg;
3818 if (open->op_create == NFS4_OPEN_CREATE)
3819 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003820 break;
3821 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003822 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003823 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003824 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003825 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003826 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003828 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003830 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003831 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003832 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003833 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003834 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003835out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003836 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3837 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003838 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003839 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003840 open->op_recall = 1;
3841 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003842
3843 /* 4.1 client asking for a delegation? */
3844 if (open->op_deleg_want)
3845 nfsd4_open_deleg_none_ext(open, status);
3846 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847}
3848
Benny Halevye27f49c2012-02-21 14:16:54 -08003849static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3850 struct nfs4_delegation *dp)
3851{
3852 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3853 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3854 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3855 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3856 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3857 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3858 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3859 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3860 }
3861 /* Otherwise the client must be confused wanting a delegation
3862 * it already has, therefore we don't return
3863 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3864 */
3865}
3866
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867/*
3868 * called with nfs4_lock_state() held.
3869 */
Al Virob37ad282006-10-19 23:28:59 -07003870__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3872{
Andy Adamson66689582009-04-03 08:28:45 +03003873 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003874 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003876 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003877 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003878 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 /*
3881 * Lookup file; if found, lookup stateid and check open request,
3882 * and check for delegations in the process of being recalled.
3883 * If not found, create the nfs4_file struct
3884 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003885 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04003886 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04003887 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003888 if (status)
3889 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003890 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003892 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003893 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003894 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003895 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003896 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 }
3898
3899 /*
3900 * OPEN the file, or upgrade an existing OPEN.
3901 * If truncate fails, the OPEN fails.
3902 */
3903 if (stp) {
3904 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003905 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 if (status)
3907 goto out;
3908 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003909 stp = open->op_stp;
3910 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003911 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003912 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3913 if (status) {
3914 release_open_stateid(stp);
3915 goto out;
3916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003918 update_stateid(&stp->st_stid.sc_stateid);
3919 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920
Benny Halevyd24433c2012-02-16 20:57:17 +02003921 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02003922 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3923 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3924 open->op_why_no_deleg = WND4_NOT_WANTED;
3925 goto nodeleg;
3926 }
3927 }
3928
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 /*
3930 * Attempt to hand out a delegation. No error return, because the
3931 * OPEN succeeds even if we fail.
3932 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003933 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003934nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 status = nfs_ok;
3936
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003937 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003938 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003940 /* 4.1 client trying to upgrade/downgrade delegation? */
3941 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003942 open->op_deleg_want)
3943 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003944
NeilBrown13cd2182005-06-23 22:03:10 -07003945 if (fp)
3946 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003947 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003948 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 /*
3950 * To finish the open response, we just need to set the rflags.
3951 */
3952 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003953 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003954 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003956 if (dp)
3957 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003958 if (stp)
3959 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960
3961 return status;
3962}
3963
Jeff Layton58fb12e2014-07-29 21:34:27 -04003964void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
3965 struct nfsd4_open *open, __be32 status)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003966{
3967 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04003968 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003969
Jeff Laytond3134b12014-07-29 21:34:32 -04003970 nfsd4_cstate_assign_replay(cstate, so);
3971 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003972 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003973 if (open->op_file)
3974 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003975 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04003976 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003977}
3978
Al Virob37ad282006-10-19 23:28:59 -07003979__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003980nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3981 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982{
3983 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003984 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003985 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986
3987 nfs4_lock_state();
3988 dprintk("process_renew(%08x/%08x): starting\n",
3989 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003990 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003991 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003993 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003995 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003996 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 goto out;
3998 status = nfs_ok;
3999out:
4000 nfs4_unlock_state();
4001 return status;
4002}
4003
NeilBrowna76b4312005-06-23 22:04:01 -07004004static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004005nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004006{
Jeff Layton33dcc482012-04-10 11:08:48 -04004007 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004008 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004009 return;
4010
NeilBrowna76b4312005-06-23 22:04:01 -07004011 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004012 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004013 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004014 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004015 /*
4016 * Now that every NFSv4 client has had the chance to recover and
4017 * to see the (possibly new, possibly shorter) lease time, we
4018 * can safely set the next grace time to the current lease time:
4019 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03004020 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07004021}
4022
NeilBrownfd39ca92005-06-23 22:04:03 -07004023static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004024nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025{
4026 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004027 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 struct nfs4_delegation *dp;
4029 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004030 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004031 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032
4033 nfs4_lock_state();
4034
4035 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004036 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004037 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004038 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004039 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 clp = list_entry(pos, struct nfs4_client, cl_lru);
4041 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4042 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004043 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 break;
4045 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004046 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004047 dprintk("NFSD: client in use (clientid %08x)\n",
4048 clp->cl_clientid.cl_id);
4049 continue;
4050 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004051 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004052 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004053 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004054 list_for_each_safe(pos, next, &reaplist) {
4055 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 dprintk("NFSD: purging unused client (clientid %08x)\n",
4057 clp->cl_clientid.cl_id);
4058 expire_client(clp);
4059 }
Benny Halevycdc97502014-05-30 09:09:30 -04004060 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004061 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004063 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4064 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004066 t = dp->dl_time - cutoff;
4067 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 break;
4069 }
Jeff Layton42690672014-07-25 07:34:20 -04004070 unhash_delegation_locked(dp);
4071 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 }
Benny Halevycdc97502014-05-30 09:09:30 -04004073 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004074 while (!list_empty(&reaplist)) {
4075 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4076 dl_recall_lru);
4077 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004078 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004080 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004081 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
4082 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004083 t = oo->oo_time - cutoff;
4084 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 break;
4086 }
Jeff Laytond3134b12014-07-29 21:34:32 -04004087 release_last_closed_stateid(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04004089 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04004091 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092}
4093
Harvey Harrisona254b242008-02-20 12:49:00 -08004094static struct workqueue_struct *laundry_wq;
4095static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004096
4097static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004098laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099{
4100 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004101 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4102 work);
4103 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4104 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004106 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004108 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109}
4110
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004111static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004112{
Trond Myklebust11b91642014-07-29 21:34:08 -04004113 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004114 return nfserr_bad_stateid;
4115 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116}
4117
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004119access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004121 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4122 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4123 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124}
4125
4126static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004127access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004129 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4130 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131}
4132
4133static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004134__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135{
Al Virob37ad282006-10-19 23:28:59 -07004136 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137
J. Bruce Fields02921912010-07-29 15:16:59 -04004138 /* For lock stateid's, we test the parent open, not the lock: */
4139 if (stp->st_openstp)
4140 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004141 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004143 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 goto out;
4145 status = nfs_ok;
4146out:
4147 return status;
4148}
4149
Al Virob37ad282006-10-19 23:28:59 -07004150static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004151check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004153 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004155 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004156 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 * conflicting state; so we must wait out the grace period. */
4158 return nfserr_grace;
4159 } else if (flags & WR_STATE)
4160 return nfs4_share_conflict(current_fh,
4161 NFS4_SHARE_DENY_WRITE);
4162 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4163 return nfs4_share_conflict(current_fh,
4164 NFS4_SHARE_DENY_READ);
4165}
4166
4167/*
4168 * Allow READ/WRITE during grace period on recovered state only for files
4169 * that are not able to provide mandatory locking.
4170 */
4171static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004172grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004174 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175}
4176
J. Bruce Fields81b82962011-08-23 11:03:29 -04004177/* Returns true iff a is later than b: */
4178static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4179{
Jim Rees1a9357f2013-05-17 17:33:00 -04004180 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004181}
4182
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004183static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004184{
Andy Adamson66689582009-04-03 08:28:45 +03004185 /*
4186 * When sessions are used the stateid generation number is ignored
4187 * when it is zero.
4188 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004189 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004190 return nfs_ok;
4191
4192 if (in->si_generation == ref->si_generation)
4193 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004194
J. Bruce Fields0836f582008-01-26 19:08:12 -05004195 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004196 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004197 return nfserr_bad_stateid;
4198 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004199 * However, we could see a stateid from the past, even from a
4200 * non-buggy client. For example, if the client sends a lock
4201 * while some IO is outstanding, the lock may bump si_generation
4202 * while the IO is still in flight. The client could avoid that
4203 * situation by waiting for responses on all the IO requests,
4204 * but better performance may result in retrying IO that
4205 * receives an old_stateid error if requests are rarely
4206 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004207 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004208 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004209}
4210
Chuck Lever7df302f2012-05-29 13:56:37 -04004211static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004212{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004213 struct nfs4_stid *s;
4214 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004215 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004216
Chuck Lever7df302f2012-05-29 13:56:37 -04004217 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004218 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004219 /* Client debugging aid. */
4220 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4221 char addr_str[INET6_ADDRSTRLEN];
4222 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4223 sizeof(addr_str));
4224 pr_warn_ratelimited("NFSD: client %s testing state ID "
4225 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004226 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004227 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004228 spin_lock(&cl->cl_lock);
4229 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004230 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004231 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004232 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004233 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004234 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004235 switch (s->sc_type) {
4236 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004237 status = nfs_ok;
4238 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004239 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004240 status = nfserr_deleg_revoked;
4241 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004242 case NFS4_OPEN_STID:
4243 case NFS4_LOCK_STID:
4244 ols = openlockstateid(s);
4245 if (ols->st_stateowner->so_is_open_owner
4246 && !(openowner(ols->st_stateowner)->oo_flags
4247 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004248 status = nfserr_bad_stateid;
4249 else
4250 status = nfs_ok;
4251 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004252 default:
4253 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004254 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004255 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004256 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004257 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004258 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004259out_unlock:
4260 spin_unlock(&cl->cl_lock);
4261 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004262}
4263
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004264static __be32
4265nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4266 stateid_t *stateid, unsigned char typemask,
4267 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004268{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004269 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004270
4271 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4272 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004273 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004274 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004275 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004276 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004277 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004278 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004279 if (status)
4280 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004281 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004282 if (!*s)
4283 return nfserr_bad_stateid;
4284 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004285}
4286
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287/*
4288* Checks for stateid operations
4289*/
Al Virob37ad282006-10-19 23:28:59 -07004290__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004291nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004292 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004294 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004295 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004297 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004299 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004300 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004301 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 if (filpp)
4304 *filpp = NULL;
4305
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004306 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 return nfserr_grace;
4308
4309 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004310 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311
Trond Myklebust14bcab12014-04-18 14:44:07 -04004312 nfs4_lock_state();
4313
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004314 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004315 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004316 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004317 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004318 goto unlock_state;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004319 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4320 if (status)
4321 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004322 switch (s->sc_type) {
4323 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004324 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004325 status = nfs4_check_delegmode(dp, flags);
4326 if (status)
4327 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004328 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004329 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004330 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004331 WARN_ON_ONCE(1);
4332 status = nfserr_serverfault;
4333 goto out;
4334 }
Trond Myklebustde186432014-07-10 14:07:26 -04004335 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004336 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004337 break;
4338 case NFS4_OPEN_STID:
4339 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004340 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004341 status = nfs4_check_fh(current_fh, stp);
4342 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004344 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004345 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004347 status = nfs4_check_openmode(stp, flags);
4348 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004350 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004351 struct nfs4_file *fp = stp->st_stid.sc_file;
4352
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004353 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004354 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004355 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004356 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004357 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004358 break;
4359 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004360 status = nfserr_bad_stateid;
4361 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 }
4363 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004364 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004365 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004367 nfs4_put_stid(s);
4368unlock_state:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004369 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 return status;
4371}
4372
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004373static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004374nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004375{
J. Bruce Fieldsa1b8ff42014-05-20 15:55:21 -04004376 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4377
Trond Myklebust11b91642014-07-29 21:34:08 -04004378 if (check_for_locks(stp->st_stid.sc_file, lo))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004379 return nfserr_locks_held;
Jeff Layton5adfd882014-07-29 21:34:31 -04004380 release_lock_stateid(stp);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004381 return nfs_ok;
4382}
4383
4384/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004385 * Test if the stateid is valid
4386 */
4387__be32
4388nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4389 struct nfsd4_test_stateid *test_stateid)
4390{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004391 struct nfsd4_test_stateid_id *stateid;
4392 struct nfs4_client *cl = cstate->session->se_client;
4393
4394 nfs4_lock_state();
4395 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004396 stateid->ts_id_status =
4397 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004398 nfs4_unlock_state();
4399
Bryan Schumaker17456802011-07-13 10:50:48 -04004400 return nfs_ok;
4401}
4402
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004403__be32
4404nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4405 struct nfsd4_free_stateid *free_stateid)
4406{
4407 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004408 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004409 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004410 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004411 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004412
4413 nfs4_lock_state();
Jeff Layton1af71cc2014-07-29 21:34:14 -04004414 spin_lock(&cl->cl_lock);
4415 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004416 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004417 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004418 switch (s->sc_type) {
4419 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004420 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004421 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004422 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004423 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4424 if (ret)
4425 break;
4426 ret = nfserr_locks_held;
4427 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004428 case NFS4_LOCK_STID:
4429 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4430 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004431 break;
4432 spin_unlock(&cl->cl_lock);
4433 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4434 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004435 case NFS4_REVOKED_DELEG_STID:
4436 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004437 list_del_init(&dp->dl_recall_lru);
4438 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004439 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004440 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004441 goto out;
4442 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004443 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004444out_unlock:
4445 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004446out:
4447 nfs4_unlock_state();
4448 return ret;
4449}
4450
NeilBrown4c4cd222005-07-07 17:59:27 -07004451static inline int
4452setlkflg (int type)
4453{
4454 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4455 RD_STATE : WR_STATE;
4456}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004458static __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 -04004459{
4460 struct svc_fh *current_fh = &cstate->current_fh;
4461 struct nfs4_stateowner *sop = stp->st_stateowner;
4462 __be32 status;
4463
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004464 status = nfsd4_check_seqid(cstate, sop, seqid);
4465 if (status)
4466 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004467 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4468 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004469 /*
4470 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004471 * nfserr_replay_me from the previous step, and
4472 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004473 */
4474 return nfserr_bad_stateid;
4475 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4476 if (status)
4477 return status;
4478 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004479}
4480
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481/*
4482 * Checks for sequence id mutating operations.
4483 */
Al Virob37ad282006-10-19 23:28:59 -07004484static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004485nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004486 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004487 struct nfs4_ol_stateid **stpp,
4488 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004490 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004491 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004492 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004494 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4495 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004496
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004498 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004499 if (status)
4500 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004501 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004502 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004504 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004505 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004506 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004507 else
4508 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004509 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004510}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004511
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004512static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4513 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004514{
4515 __be32 status;
4516 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004517 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004519 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004520 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004521 if (status)
4522 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004523 oo = openowner(stp->st_stateowner);
4524 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4525 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004526 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004527 }
4528 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004529 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530}
4531
Al Virob37ad282006-10-19 23:28:59 -07004532__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004533nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004534 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535{
Al Virob37ad282006-10-19 23:28:59 -07004536 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004537 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004538 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004539 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540
Al Viroa6a9f182013-09-16 10:57:01 -04004541 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4542 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004544 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004545 if (status)
4546 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547
4548 nfs4_lock_state();
4549
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004550 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004551 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004552 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004553 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004554 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004555 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004556 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004557 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004558 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004559 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004560 update_stateid(&stp->st_stid.sc_stateid);
4561 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004562 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004563 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004564
Jeff Layton2a4317c2012-03-21 16:42:43 -04004565 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004566 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004567put_stateid:
4568 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004570 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004571 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 return status;
4573}
4574
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004575static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004577 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004578 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004579 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004580 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004581}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004582
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004583static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4584{
4585 switch (to_access) {
4586 case NFS4_SHARE_ACCESS_READ:
4587 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4588 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4589 break;
4590 case NFS4_SHARE_ACCESS_WRITE:
4591 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4592 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4593 break;
4594 case NFS4_SHARE_ACCESS_BOTH:
4595 break;
4596 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004597 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 }
4599}
4600
Al Virob37ad282006-10-19 23:28:59 -07004601__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004602nfsd4_open_downgrade(struct svc_rqst *rqstp,
4603 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004604 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605{
Al Virob37ad282006-10-19 23:28:59 -07004606 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004607 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004608 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609
Al Viroa6a9f182013-09-16 10:57:01 -04004610 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4611 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004613 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004614 if (od->od_deleg_want)
4615 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4616 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617
4618 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004619 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004620 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004621 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004624 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004625 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004627 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004629 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004630 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004632 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004634 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635
Jeff Laytonce0fc432012-05-11 09:45:14 -04004636 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004638 update_stateid(&stp->st_stid.sc_stateid);
4639 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004641put_stateid:
4642 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004644 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004645 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 return status;
4647}
4648
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004649static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4650{
Trond Myklebustacf92952014-06-30 11:48:37 -04004651 struct nfs4_client *clp = s->st_stid.sc_client;
Trond Myklebustacf92952014-06-30 11:48:37 -04004652
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004653 s->st_stid.sc_type = NFS4_CLOSED_STID;
Trond Myklebustacf92952014-06-30 11:48:37 -04004654 unhash_open_stateid(s);
4655
Jeff Laytond3134b12014-07-29 21:34:32 -04004656 if (clp->cl_minorversion)
Trond Myklebust60116952014-07-29 21:34:06 -04004657 nfs4_put_stid(&s->st_stid);
Jeff Laytond3134b12014-07-29 21:34:32 -04004658 else
4659 move_to_close_lru(s, clp->net);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004660}
4661
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662/*
4663 * nfs4_unlock_state() called after encode
4664 */
Al Virob37ad282006-10-19 23:28:59 -07004665__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004666nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004667 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668{
Al Virob37ad282006-10-19 23:28:59 -07004669 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004670 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004671 struct net *net = SVC_NET(rqstp);
4672 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673
Al Viroa6a9f182013-09-16 10:57:01 -04004674 dprintk("NFSD: nfsd4_close on file %pd\n",
4675 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
4677 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004678 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4679 &close->cl_stateid,
4680 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004681 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004682 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004683 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004685 update_stateid(&stp->st_stid.sc_stateid);
4686 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004688 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004689
4690 /* put reference from nfs4_preprocess_seqid_op */
4691 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692out:
Jeff Layton58fb12e2014-07-29 21:34:27 -04004693 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 return status;
4695}
4696
Al Virob37ad282006-10-19 23:28:59 -07004697__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004698nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4699 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004701 struct nfs4_delegation *dp;
4702 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004703 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004704 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004705 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004707 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004708 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709
4710 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004711 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004712 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004713 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004714 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004715 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004716 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004717 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004718
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004719 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004720put_stateid:
4721 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004723 nfs4_unlock_state();
4724
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 return status;
4726}
4727
4728
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730
Benny Halevy87df4de2008-12-15 19:42:03 +02004731static inline u64
4732end_offset(u64 start, u64 len)
4733{
4734 u64 end;
4735
4736 end = start + len;
4737 return end >= start ? end: NFS4_MAX_UINT64;
4738}
4739
4740/* last octet in a range */
4741static inline u64
4742last_byte_offset(u64 start, u64 len)
4743{
4744 u64 end;
4745
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004746 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004747 end = start + len;
4748 return end > start ? end - 1: NFS4_MAX_UINT64;
4749}
4750
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751/*
4752 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4753 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4754 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4755 * locking, this prevents us from being completely protocol-compliant. The
4756 * real solution to this problem is to start using unsigned file offsets in
4757 * the VFS, but this is a very deep change!
4758 */
4759static inline void
4760nfs4_transform_lock_offset(struct file_lock *lock)
4761{
4762 if (lock->fl_start < 0)
4763 lock->fl_start = OFFSET_MAX;
4764 if (lock->fl_end < 0)
4765 lock->fl_end = OFFSET_MAX;
4766}
4767
NeilBrownd5b90262006-04-10 22:55:22 -07004768/* Hack!: For now, we're defining this just so we can use a pointer to it
4769 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004770static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004771};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772
4773static inline void
4774nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4775{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004776 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777
NeilBrownd5b90262006-04-10 22:55:22 -07004778 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004779 lo = (struct nfs4_lockowner *) fl->fl_owner;
4780 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4781 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004782 if (!deny->ld_owner.data)
4783 /* We just don't care that much */
4784 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004785 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4786 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004787 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004788nevermind:
4789 deny->ld_owner.len = 0;
4790 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004791 deny->ld_clientid.cl_boot = 0;
4792 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 }
4794 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004795 deny->ld_length = NFS4_MAX_UINT64;
4796 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4798 deny->ld_type = NFS4_READ_LT;
4799 if (fl->fl_type != F_RDLCK)
4800 deny->ld_type = NFS4_WRITE_LT;
4801}
4802
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004803static struct nfs4_lockowner *
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004804find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4805 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806{
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004807 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4808 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004810 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4811 if (so->so_is_open_owner)
4812 continue;
4813 if (!same_owner_str(so, owner, clid))
4814 continue;
Jeff Layton5db1c032014-07-29 21:34:28 -04004815 atomic_inc(&so->so_count);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004816 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 }
4818 return NULL;
4819}
4820
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004821static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4822{
4823 unhash_lockowner(lockowner(sop));
4824}
4825
Jeff Layton6b180f02014-07-29 21:34:26 -04004826static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4827{
4828 struct nfs4_lockowner *lo = lockowner(sop);
4829
4830 kmem_cache_free(lockowner_slab, lo);
4831}
4832
4833static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004834 .so_unhash = nfs4_unhash_lockowner,
4835 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004836};
4837
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838/*
4839 * Alloc a lock owner structure.
4840 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004841 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004843 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004845static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004846alloc_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 -04004847 struct nfs4_lockowner *lo;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004848 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004850 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4851 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004853 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4854 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04004855 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04004856 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004857 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004858 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859}
4860
Jeff Layton356a95e2014-07-29 21:34:13 -04004861static void
4862init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
4863 struct nfs4_file *fp, struct inode *inode,
4864 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004866 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867
Jeff Layton356a95e2014-07-29 21:34:13 -04004868 lockdep_assert_held(&clp->cl_lock);
4869
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004870 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004871 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004872 stp->st_stateowner = &lo->lo_owner;
Trond Myklebuste4f1dd72014-07-29 21:34:30 -04004873 atomic_inc(&lo->lo_owner.so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07004874 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004875 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04004876 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004877 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004879 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004880 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004881 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004882 spin_lock(&fp->fi_lock);
4883 list_add(&stp->st_perfile, &fp->fi_stateids);
4884 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885}
4886
Jeff Laytonc53530d2014-06-30 11:48:39 -04004887static struct nfs4_ol_stateid *
4888find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4889{
4890 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04004891 struct nfs4_client *clp = lo->lo_owner.so_client;
4892
4893 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004894
4895 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004896 if (lst->st_stid.sc_file == fp) {
4897 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004898 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004899 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004900 }
4901 return NULL;
4902}
4903
Jeff Layton356a95e2014-07-29 21:34:13 -04004904static struct nfs4_ol_stateid *
4905find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
4906 struct inode *inode, struct nfs4_ol_stateid *ost,
4907 bool *new)
4908{
4909 struct nfs4_stid *ns = NULL;
4910 struct nfs4_ol_stateid *lst;
4911 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4912 struct nfs4_client *clp = oo->oo_owner.so_client;
4913
4914 spin_lock(&clp->cl_lock);
4915 lst = find_lock_stateid(lo, fi);
4916 if (lst == NULL) {
4917 spin_unlock(&clp->cl_lock);
4918 ns = nfs4_alloc_stid(clp, stateid_slab);
4919 if (ns == NULL)
4920 return NULL;
4921
4922 spin_lock(&clp->cl_lock);
4923 lst = find_lock_stateid(lo, fi);
4924 if (likely(!lst)) {
4925 lst = openlockstateid(ns);
4926 init_lock_stateid(lst, lo, fi, inode, ost);
4927 ns = NULL;
4928 *new = true;
4929 }
4930 }
4931 spin_unlock(&clp->cl_lock);
4932 if (ns)
4933 nfs4_put_stid(ns);
4934 return lst;
4935}
Jeff Laytonc53530d2014-06-30 11:48:39 -04004936
NeilBrownfd39ca92005-06-23 22:04:03 -07004937static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938check_lock_length(u64 offset, u64 length)
4939{
Benny Halevy87df4de2008-12-15 19:42:03 +02004940 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 LOFF_OVERFLOW(offset, length)));
4942}
4943
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004944static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004945{
Trond Myklebust11b91642014-07-29 21:34:08 -04004946 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004947
Jeff Layton7214e862014-07-10 14:07:33 -04004948 lockdep_assert_held(&fp->fi_lock);
4949
Jeff Layton82c5ff12012-05-11 09:45:13 -04004950 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004951 return;
Jeff Layton12659652014-07-10 14:07:28 -04004952 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004953 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004954}
4955
Jeff Layton356a95e2014-07-29 21:34:13 -04004956static __be32
4957lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
4958 struct nfs4_ol_stateid *ost,
4959 struct nfsd4_lock *lock,
4960 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004961{
Jeff Layton5db1c032014-07-29 21:34:28 -04004962 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04004963 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004964 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4965 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004966 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004967 struct nfs4_lockowner *lo;
4968 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004969 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004970
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004971 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004972 if (!lo) {
4973 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4974 &lock->v.new.owner);
4975 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4976 if (lo == NULL)
4977 return nfserr_jukebox;
4978 } else {
4979 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04004980 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04004981 if (!cstate->minorversion &&
4982 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04004983 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004984 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004985
Jeff Layton356a95e2014-07-29 21:34:13 -04004986 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004987 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04004988 status = nfserr_jukebox;
4989 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004990 }
Jeff Layton5db1c032014-07-29 21:34:28 -04004991 status = nfs_ok;
4992out:
4993 nfs4_put_stateowner(&lo->lo_owner);
4994 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004995}
4996
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997/*
4998 * LOCK operation
4999 */
Al Virob37ad282006-10-19 23:28:59 -07005000__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005001nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005002 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005004 struct nfs4_openowner *open_sop = NULL;
5005 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005006 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005007 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005008 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005009 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005010 struct file_lock *file_lock = NULL;
5011 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005012 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005013 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005014 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005015 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005016 struct net *net = SVC_NET(rqstp);
5017 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018
5019 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5020 (long long) lock->lk_offset,
5021 (long long) lock->lk_length);
5022
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 if (check_lock_length(lock->lk_offset, lock->lk_length))
5024 return nfserr_inval;
5025
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005026 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005027 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005028 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5029 return status;
5030 }
5031
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 nfs4_lock_state();
5033
5034 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005035 if (nfsd4_has_session(cstate))
5036 /* See rfc 5661 18.10.3: given clientid is ignored: */
5037 memcpy(&lock->v.new.clientid,
5038 &cstate->session->se_client->cl_clientid,
5039 sizeof(clientid_t));
5040
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005042 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005046 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 lock->lk_new_open_seqid,
5048 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005049 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005050 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005052 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005053 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005054 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005055 &lock->v.new.clientid))
5056 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005057 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005058 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005059 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005060 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005061 lock->lk_old_lock_seqid,
5062 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005063 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005064 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005065 if (status)
5066 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005067 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005069 lkflg = setlkflg(lock->lk_type);
5070 status = nfs4_check_openmode(lock_stp, lkflg);
5071 if (status)
5072 goto out;
5073
NeilBrown0dd395d2005-07-07 17:59:15 -07005074 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005075 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005076 goto out;
5077 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005078 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005079 goto out;
5080
Jeff Layton21179d82012-08-21 08:03:32 -04005081 file_lock = locks_alloc_lock();
5082 if (!file_lock) {
5083 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5084 status = nfserr_jukebox;
5085 goto out;
5086 }
5087
Trond Myklebust11b91642014-07-29 21:34:08 -04005088 fp = lock_stp->st_stid.sc_file;
Jeff Layton21179d82012-08-21 08:03:32 -04005089 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 switch (lock->lk_type) {
5091 case NFS4_READ_LT:
5092 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005093 spin_lock(&fp->fi_lock);
5094 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005095 if (filp)
5096 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005097 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005098 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005099 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 case NFS4_WRITE_LT:
5101 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005102 spin_lock(&fp->fi_lock);
5103 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005104 if (filp)
5105 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005106 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005107 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005108 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 default:
5110 status = nfserr_inval;
5111 goto out;
5112 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005113 if (!filp) {
5114 status = nfserr_openmode;
5115 goto out;
5116 }
Jeff Layton21179d82012-08-21 08:03:32 -04005117 file_lock->fl_owner = (fl_owner_t)lock_sop;
5118 file_lock->fl_pid = current->tgid;
5119 file_lock->fl_file = filp;
5120 file_lock->fl_flags = FL_POSIX;
5121 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5122 file_lock->fl_start = lock->lk_offset;
5123 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5124 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
Jeff Layton21179d82012-08-21 08:03:32 -04005126 conflock = locks_alloc_lock();
5127 if (!conflock) {
5128 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5129 status = nfserr_jukebox;
5130 goto out;
5131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132
Jeff Layton21179d82012-08-21 08:03:32 -04005133 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005134 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005136 update_stateid(&lock_stp->st_stid.sc_stateid);
5137 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005139 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005140 break;
5141 case (EAGAIN): /* conflock holds conflicting lock */
5142 status = nfserr_denied;
5143 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005144 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005145 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 case (EDEADLK):
5147 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005148 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005149 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005150 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005151 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005152 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154out:
Trond Myklebustde186432014-07-10 14:07:26 -04005155 if (filp)
5156 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005157 if (lock_stp) {
5158 /* Bump seqid manually if the 4.0 replay owner is openowner */
5159 if (cstate->replay_owner &&
5160 cstate->replay_owner != &lock_sop->lo_owner &&
5161 seqid_mutating_err(ntohl(status)))
5162 lock_sop->lo_owner.so_seqid++;
5163
5164 /*
5165 * If this is a new, never-before-used stateid, and we are
5166 * returning an error, then just go ahead and release it.
5167 */
5168 if (status && new)
5169 release_lock_stateid(lock_stp);
5170
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005171 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005172 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005173 if (open_stp)
5174 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005175 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005176 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005177 if (file_lock)
5178 locks_free_lock(file_lock);
5179 if (conflock)
5180 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 return status;
5182}
5183
5184/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005185 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5186 * so we do a temporary open here just to get an open file to pass to
5187 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5188 * inode operation.)
5189 */
Al Viro04da6e92012-04-13 00:00:04 -04005190static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005191{
5192 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005193 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5194 if (!err) {
5195 err = nfserrno(vfs_test_lock(file, lock));
5196 nfsd_close(file);
5197 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005198 return err;
5199}
5200
5201/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 * LOCKT operation
5203 */
Al Virob37ad282006-10-19 23:28:59 -07005204__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005205nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5206 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207{
Jeff Layton21179d82012-08-21 08:03:32 -04005208 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005209 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005210 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005211 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005213 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 return nfserr_grace;
5215
5216 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5217 return nfserr_inval;
5218
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 nfs4_lock_state();
5220
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005221 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005222 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005223 if (status)
5224 goto out;
5225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005227 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229
Jeff Layton21179d82012-08-21 08:03:32 -04005230 file_lock = locks_alloc_lock();
5231 if (!file_lock) {
5232 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5233 status = nfserr_jukebox;
5234 goto out;
5235 }
5236 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 switch (lockt->lt_type) {
5238 case NFS4_READ_LT:
5239 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005240 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 break;
5242 case NFS4_WRITE_LT:
5243 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005244 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 break;
5246 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005247 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 status = nfserr_inval;
5249 goto out;
5250 }
5251
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005252 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005253 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005254 file_lock->fl_owner = (fl_owner_t)lo;
5255 file_lock->fl_pid = current->tgid;
5256 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257
Jeff Layton21179d82012-08-21 08:03:32 -04005258 file_lock->fl_start = lockt->lt_offset;
5259 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260
Jeff Layton21179d82012-08-21 08:03:32 -04005261 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262
Jeff Layton21179d82012-08-21 08:03:32 -04005263 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005264 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005265 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005266
Jeff Layton21179d82012-08-21 08:03:32 -04005267 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005269 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 }
5271out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005272 if (lo)
5273 nfs4_put_stateowner(&lo->lo_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005275 if (file_lock)
5276 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 return status;
5278}
5279
Al Virob37ad282006-10-19 23:28:59 -07005280__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005281nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005282 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005284 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005286 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005287 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005288 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005289 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5290
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5292 (long long) locku->lu_offset,
5293 (long long) locku->lu_length);
5294
5295 if (check_lock_length(locku->lu_offset, locku->lu_length))
5296 return nfserr_inval;
5297
5298 nfs4_lock_state();
5299
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005300 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005301 &locku->lu_stateid, NFS4_LOCK_STID,
5302 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005303 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005305 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005306 if (!filp) {
5307 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005308 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005309 }
Jeff Layton21179d82012-08-21 08:03:32 -04005310 file_lock = locks_alloc_lock();
5311 if (!file_lock) {
5312 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5313 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005314 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005315 }
5316 locks_init_lock(file_lock);
5317 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005318 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005319 file_lock->fl_pid = current->tgid;
5320 file_lock->fl_file = filp;
5321 file_lock->fl_flags = FL_POSIX;
5322 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5323 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324
Jeff Layton21179d82012-08-21 08:03:32 -04005325 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5326 locku->lu_length);
5327 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328
Jeff Layton21179d82012-08-21 08:03:32 -04005329 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005330 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005331 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 goto out_nfserr;
5333 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005334 update_stateid(&stp->st_stid.sc_stateid);
5335 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005336fput:
5337 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005338put_stateid:
5339 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005341 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005342 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005343 if (file_lock)
5344 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 return status;
5346
5347out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005348 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005349 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350}
5351
5352/*
5353 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005354 * true: locks held by lockowner
5355 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005357static bool
5358check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359{
5360 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005361 int status = false;
5362 struct file *filp = find_any_file(fp);
5363 struct inode *inode;
5364
5365 if (!filp) {
5366 /* Any valid lock stateid should have some sort of access */
5367 WARN_ON_ONCE(1);
5368 return status;
5369 }
5370
5371 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372
Jeff Layton1c8c6012013-06-21 08:58:15 -04005373 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005375 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005376 status = true;
5377 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005380 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005381 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 return status;
5383}
5384
Al Virob37ad282006-10-19 23:28:59 -07005385__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005386nfsd4_release_lockowner(struct svc_rqst *rqstp,
5387 struct nfsd4_compound_state *cstate,
5388 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389{
5390 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Laytonfd449072014-06-30 11:48:41 -04005391 struct nfs4_stateowner *sop = NULL, *tmp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005392 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005393 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 struct xdr_netobj *owner = &rlockowner->rl_owner;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005395 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07005396 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005397 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398
5399 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5400 clid->cl_boot, clid->cl_id);
5401
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 nfs4_lock_state();
5403
Jeff Layton4b24ca72014-06-30 11:48:44 -04005404 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005405 if (status)
5406 goto out;
5407
NeilBrown3e9e3db2005-06-23 22:04:20 -07005408 status = nfserr_locks_held;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05005409
Jeff Laytonfd449072014-06-30 11:48:41 -04005410 /* Find the matching lock stateowner */
5411 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
5412 if (tmp->so_is_open_owner)
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005413 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005414 if (same_owner_str(tmp, owner, clid)) {
5415 sop = tmp;
Jeff Layton5adfd882014-07-29 21:34:31 -04005416 atomic_inc(&sop->so_count);
Jeff Laytonfd449072014-06-30 11:48:41 -04005417 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07005419 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005420
5421 /* No matching owner found, maybe a replay? Just declare victory... */
5422 if (!sop) {
5423 status = nfs_ok;
5424 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005426
5427 lo = lockowner(sop);
5428 /* see if there are still any locks associated with it */
5429 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
Jeff Layton5adfd882014-07-29 21:34:31 -04005430 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5431 nfs4_put_stateowner(sop);
Jeff Laytonfd449072014-06-30 11:48:41 -04005432 goto out;
Jeff Layton5adfd882014-07-29 21:34:31 -04005433 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005434 }
5435
5436 status = nfs_ok;
5437 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438out:
5439 nfs4_unlock_state();
5440 return status;
5441}
5442
5443static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005444alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445{
NeilBrowna55370a2005-06-23 22:03:52 -07005446 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447}
5448
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005449bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005450nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005451{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005452 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005453
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005454 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005455 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005456}
5457
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458/*
5459 * failure => all reset bets are off, nfserr_no_grace...
5460 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005461struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005462nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463{
5464 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005465 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466
NeilBrowna55370a2005-06-23 22:03:52 -07005467 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5468 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005469 if (crp) {
5470 strhashval = clientstr_hashval(name);
5471 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005472 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005473 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005474 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005475 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005476 }
5477 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478}
5479
Jeff Layton2a4317c2012-03-21 16:42:43 -04005480void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005481nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005482{
5483 list_del(&crp->cr_strhash);
5484 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005485 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005486}
5487
5488void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005489nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490{
5491 struct nfs4_client_reclaim *crp = NULL;
5492 int i;
5493
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005495 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5496 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005498 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 }
5500 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005501 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502}
5503
5504/*
5505 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005506struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005507nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508{
5509 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 struct nfs4_client_reclaim *crp = NULL;
5511
Jeff Layton278c9312012-11-12 15:00:52 -05005512 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513
Jeff Layton278c9312012-11-12 15:00:52 -05005514 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005515 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005516 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 return crp;
5518 }
5519 }
5520 return NULL;
5521}
5522
5523/*
5524* Called from OPEN. Look for clientid in reclaim list.
5525*/
Al Virob37ad282006-10-19 23:28:59 -07005526__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005527nfs4_check_open_reclaim(clientid_t *clid,
5528 struct nfsd4_compound_state *cstate,
5529 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005531 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005532
5533 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005534 status = lookup_clientid(clid, cstate, nn);
5535 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005536 return nfserr_reclaim_bad;
5537
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005538 if (nfsd4_client_record_check(cstate->clp))
5539 return nfserr_reclaim_bad;
5540
5541 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542}
5543
Bryan Schumaker65178db2011-11-01 13:35:21 -04005544#ifdef CONFIG_NFSD_FAULT_INJECTION
5545
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005546u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5547{
J. Bruce Fields221a6872013-04-01 22:23:49 -04005548 if (mark_client_expired(clp))
5549 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005550 expire_client(clp);
5551 return 1;
5552}
5553
Bryan Schumaker184c1842012-11-29 11:40:44 -05005554u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5555{
5556 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005557 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005558 printk(KERN_INFO "NFS Client: %s\n", buf);
5559 return 1;
5560}
5561
5562static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5563 const char *type)
5564{
5565 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005566 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005567 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5568}
5569
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005570static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5571 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005572{
5573 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005574 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005575 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005576 u64 count = 0;
5577
5578 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005579 list_for_each_entry_safe(stp, st_next,
5580 &oop->oo_owner.so_stateids, st_perstateowner) {
5581 list_for_each_entry_safe(lst, lst_next,
5582 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005583 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005584 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005585 if (++count == max)
5586 return count;
5587 }
5588 }
5589 }
5590
5591 return count;
5592}
5593
5594u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5595{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005596 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005597}
5598
Bryan Schumaker184c1842012-11-29 11:40:44 -05005599u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5600{
5601 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5602 nfsd_print_count(clp, count, "locked files");
5603 return count;
5604}
5605
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005606static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5607{
5608 struct nfs4_openowner *oop, *next;
5609 u64 count = 0;
5610
5611 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5612 if (func)
5613 func(oop);
5614 if (++count == max)
5615 break;
5616 }
5617
5618 return count;
5619}
5620
5621u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5622{
5623 return nfsd_foreach_client_open(clp, max, release_openowner);
5624}
5625
Bryan Schumaker184c1842012-11-29 11:40:44 -05005626u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5627{
5628 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5629 nfsd_print_count(clp, count, "open files");
5630 return count;
5631}
5632
Bryan Schumaker269de302012-11-29 11:40:42 -05005633static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5634 struct list_head *victims)
5635{
5636 struct nfs4_delegation *dp, *next;
5637 u64 count = 0;
5638
Benny Halevycdc97502014-05-30 09:09:30 -04005639 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005640 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005641 if (victims) {
5642 /*
5643 * It's not safe to mess with delegations that have a
5644 * non-zero dl_time. They might have already been broken
5645 * and could be processed by the laundromat outside of
5646 * the state_lock. Just leave them be.
5647 */
5648 if (dp->dl_time != 0)
5649 continue;
5650
Jeff Layton42690672014-07-25 07:34:20 -04005651 unhash_delegation_locked(dp);
5652 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005653 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005654 if (++count == max)
5655 break;
5656 }
5657 return count;
5658}
5659
5660u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5661{
5662 struct nfs4_delegation *dp, *next;
5663 LIST_HEAD(victims);
5664 u64 count;
5665
Benny Halevycdc97502014-05-30 09:09:30 -04005666 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005667 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005668 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005669
Jeff Layton2d4a5322014-07-25 07:34:21 -04005670 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5671 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005672 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005673 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005674
5675 return count;
5676}
5677
5678u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5679{
Jeff Laytondff13992014-07-08 14:02:49 -04005680 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005681 LIST_HEAD(victims);
5682 u64 count;
5683
Benny Halevycdc97502014-05-30 09:09:30 -04005684 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005685 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005686 while (!list_empty(&victims)) {
5687 dp = list_first_entry(&victims, struct nfs4_delegation,
5688 dl_recall_lru);
5689 list_del_init(&dp->dl_recall_lru);
5690 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005691 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005692 }
Benny Halevycdc97502014-05-30 09:09:30 -04005693 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005694
5695 return count;
5696}
5697
Bryan Schumaker184c1842012-11-29 11:40:44 -05005698u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5699{
5700 u64 count = 0;
5701
Benny Halevycdc97502014-05-30 09:09:30 -04005702 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005703 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005704 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005705
5706 nfsd_print_count(clp, count, "delegations");
5707 return count;
5708}
5709
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005710u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005711{
5712 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005713 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005714 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005715
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005716 if (!nfsd_netns_ready(nn))
5717 return 0;
5718
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005719 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005720 count += func(clp, max - count);
5721 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005722 break;
5723 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005724
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005725 return count;
5726}
5727
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005728struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5729{
5730 struct nfs4_client *clp;
5731 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5732
5733 if (!nfsd_netns_ready(nn))
5734 return NULL;
5735
5736 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5737 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5738 return clp;
5739 }
5740 return NULL;
5741}
5742
Bryan Schumaker65178db2011-11-01 13:35:21 -04005743#endif /* CONFIG_NFSD_FAULT_INJECTION */
5744
Meelap Shahc2f1a552007-07-17 04:04:39 -07005745/*
5746 * Since the lifetime of a delegation isn't limited to that of an open, a
5747 * client may quite reasonably hang on to a delegation as long as it has
5748 * the inode cached. This becomes an obvious problem the first time a
5749 * client's inode cache approaches the size of the server's total memory.
5750 *
5751 * For now we avoid this problem by imposing a hard limit on the number
5752 * of delegations, which varies according to the server's memory size.
5753 */
5754static void
5755set_max_delegations(void)
5756{
5757 /*
5758 * Allow at most 4 delegations per megabyte of RAM. Quick
5759 * estimates suggest that in the worst case (where every delegation
5760 * is for a different inode), a delegation could take about 1.5K,
5761 * giving a worst case usage of about 6% of memory.
5762 */
5763 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5764}
5765
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005766static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005767{
5768 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5769 int i;
5770
5771 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5772 CLIENT_HASH_SIZE, GFP_KERNEL);
5773 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005774 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005775 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5776 CLIENT_HASH_SIZE, GFP_KERNEL);
5777 if (!nn->unconf_id_hashtbl)
5778 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005779 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5780 OWNER_HASH_SIZE, GFP_KERNEL);
5781 if (!nn->ownerstr_hashtbl)
5782 goto err_ownerstr;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005783 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5784 SESSION_HASH_SIZE, GFP_KERNEL);
5785 if (!nn->sessionid_hashtbl)
5786 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005787
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005788 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005789 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005790 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005791 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005792 for (i = 0; i < OWNER_HASH_SIZE; i++)
5793 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005794 for (i = 0; i < SESSION_HASH_SIZE; i++)
5795 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005796 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005797 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005798 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005799 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005800 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005801 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005802
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005803 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005804 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005805
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005806 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005807
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005808err_sessionid:
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03005809 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005810err_ownerstr:
5811 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005812err_unconf_id:
5813 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005814err:
5815 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005816}
5817
5818static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005819nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005820{
5821 int i;
5822 struct nfs4_client *clp = NULL;
5823 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5824
5825 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5826 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5827 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5828 destroy_client(clp);
5829 }
5830 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005831
Kinglong Mee2b905632014-03-26 22:09:30 +08005832 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5833 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5834 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5835 destroy_client(clp);
5836 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005837 }
5838
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005839 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005840 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005841 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005842 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005843 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005844}
5845
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005846int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005847nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005848{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005849 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005850 int ret;
5851
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005852 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005853 if (ret)
5854 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005855 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005856 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005857 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005858 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005859 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005860 nn->nfsd4_grace, net);
5861 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005862 return 0;
5863}
5864
5865/* initialization to perform when the nfsd service is started: */
5866
5867int
5868nfs4_state_start(void)
5869{
5870 int ret;
5871
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005872 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005873 if (ret)
5874 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005875 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005876 if (laundry_wq == NULL) {
5877 ret = -ENOMEM;
5878 goto out_recovery;
5879 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005880 ret = nfsd4_create_callback_queue();
5881 if (ret)
5882 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005883
Meelap Shahc2f1a552007-07-17 04:04:39 -07005884 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005885
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005886 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005887
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005888out_free_laundry:
5889 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005890out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005891 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892}
5893
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005894void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005895nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005899 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005901 cancel_delayed_work_sync(&nn->laundromat_work);
5902 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005903
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005904 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04005906 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005907 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005909 unhash_delegation_locked(dp);
5910 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 }
Benny Halevycdc97502014-05-30 09:09:30 -04005912 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 list_for_each_safe(pos, next, &reaplist) {
5914 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005915 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04005916 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 }
5918
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005919 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005920 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005921 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922}
5923
5924void
5925nfs4_state_shutdown(void)
5926{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005927 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005928 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005930
5931static void
5932get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5933{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005934 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5935 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005936}
5937
5938static void
5939put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5940{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005941 if (cstate->minorversion) {
5942 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5943 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5944 }
5945}
5946
5947void
5948clear_current_stateid(struct nfsd4_compound_state *cstate)
5949{
5950 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005951}
5952
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005953/*
5954 * functions to set current state id
5955 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005956void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005957nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5958{
5959 put_stateid(cstate, &odp->od_stateid);
5960}
5961
5962void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005963nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5964{
5965 put_stateid(cstate, &open->op_stateid);
5966}
5967
5968void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005969nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5970{
5971 put_stateid(cstate, &close->cl_stateid);
5972}
5973
5974void
5975nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5976{
5977 put_stateid(cstate, &lock->lk_resp_stateid);
5978}
5979
5980/*
5981 * functions to consume current state id
5982 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005983
5984void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005985nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5986{
5987 get_stateid(cstate, &odp->od_stateid);
5988}
5989
5990void
5991nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5992{
5993 get_stateid(cstate, &drp->dr_stateid);
5994}
5995
5996void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005997nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5998{
5999 get_stateid(cstate, &fsp->fr_stateid);
6000}
6001
6002void
6003nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6004{
6005 get_stateid(cstate, &setattr->sa_stateid);
6006}
6007
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006008void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006009nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6010{
6011 get_stateid(cstate, &close->cl_stateid);
6012}
6013
6014void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006015nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006016{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006017 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006018}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006019
6020void
6021nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6022{
6023 get_stateid(cstate, &read->rd_stateid);
6024}
6025
6026void
6027nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6028{
6029 get_stateid(cstate, &write->wr_stateid);
6030}