blob: 7c15918d20f059bd9db6a758077ed34a43f6d763 [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
Trond Myklebustc58c6612014-07-29 21:34:35 -04001003static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001004{
Trond Myklebustc58c6612014-07-29 21:34:35 -04001005 struct nfsd_net *nn = net_generic(lo->lo_owner.so_client->net,
1006 nfsd_net_id);
1007
1008 lockdep_assert_held(&nn->client_lock);
1009
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001010 list_del_init(&lo->lo_owner.so_strhash);
1011}
1012
1013static void release_lockowner_stateids(struct nfs4_lockowner *lo)
1014{
Trond Myklebustc58c6612014-07-29 21:34:35 -04001015 struct nfsd_net *nn = net_generic(lo->lo_owner.so_client->net,
1016 nfsd_net_id);
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001017 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001018
Trond Myklebustc58c6612014-07-29 21:34:35 -04001019 lockdep_assert_held(&nn->client_lock);
1020
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001021 while (!list_empty(&lo->lo_owner.so_stateids)) {
1022 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04001023 struct nfs4_ol_stateid, st_perstateowner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001024 spin_unlock(&nn->client_lock);
Jeff Layton5adfd882014-07-29 21:34:31 -04001025 release_lock_stateid(stp);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001026 spin_lock(&nn->client_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001027 }
1028}
1029
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001030static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001031{
Trond Myklebustc58c6612014-07-29 21:34:35 -04001032 struct nfsd_net *nn = net_generic(lo->lo_owner.so_client->net,
1033 nfsd_net_id);
1034
1035 spin_lock(&nn->client_lock);
1036 unhash_lockowner_locked(lo);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001037 release_lockowner_stateids(lo);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001038 spin_unlock(&nn->client_lock);
Jeff Layton6b180f02014-07-29 21:34:26 -04001039 nfs4_put_stateowner(&lo->lo_owner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001040}
1041
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001042static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
Jeff Layton1c755dc2014-07-29 21:34:12 -04001043 __releases(&open_stp->st_stateowner->so_client->cl_lock)
1044 __acquires(&open_stp->st_stateowner->so_client->cl_lock)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001045{
1046 struct nfs4_ol_stateid *stp;
1047
1048 while (!list_empty(&open_stp->st_locks)) {
1049 stp = list_entry(open_stp->st_locks.next,
1050 struct nfs4_ol_stateid, st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001051 spin_unlock(&open_stp->st_stateowner->so_client->cl_lock);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001052 release_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001053 spin_lock(&open_stp->st_stateowner->so_client->cl_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001054 }
1055}
1056
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001057static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -05001058{
Jeff Layton1c755dc2014-07-29 21:34:12 -04001059 spin_lock(&stp->st_stateowner->so_client->cl_lock);
J. Bruce Fields22839632009-01-11 14:27:17 -05001060 unhash_generic_stateid(stp);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001061 release_open_stateid_locks(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001062 spin_unlock(&stp->st_stateowner->so_client->cl_lock);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001063}
1064
1065static void release_open_stateid(struct nfs4_ol_stateid *stp)
1066{
1067 unhash_open_stateid(stp);
Trond Myklebust60116952014-07-29 21:34:06 -04001068 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields22839632009-01-11 14:27:17 -05001069}
1070
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001071static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001072{
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001073 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1074 nfsd_net_id);
1075
1076 lockdep_assert_held(&nn->client_lock);
1077
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001078 list_del_init(&oo->oo_owner.so_strhash);
1079 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001080}
1081
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001082static void release_last_closed_stateid(struct nfs4_openowner *oo)
1083{
1084 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
1085
1086 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001087 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001088 oo->oo_last_closed_stid = NULL;
Jeff Laytond3134b12014-07-29 21:34:32 -04001089 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001090 }
1091}
1092
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001093static void release_openowner_stateids(struct nfs4_openowner *oo)
1094{
1095 struct nfs4_ol_stateid *stp;
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 lockdep_assert_held(&nn->client_lock);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001100
1101 while (!list_empty(&oo->oo_owner.so_stateids)) {
1102 stp = list_first_entry(&oo->oo_owner.so_stateids,
1103 struct nfs4_ol_stateid, st_perstateowner);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001104 spin_unlock(&nn->client_lock);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001105 release_open_stateid(stp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001106 spin_lock(&nn->client_lock);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001107 }
1108}
1109
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001110static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001111{
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001112 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1113 nfsd_net_id);
1114
1115 spin_lock(&nn->client_lock);
1116 unhash_openowner_locked(oo);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001117 release_openowner_stateids(oo);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001118 spin_unlock(&nn->client_lock);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001119 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001120 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001121}
1122
Marc Eshel5282fd72009-04-03 08:27:52 +03001123static inline int
1124hash_sessionid(struct nfs4_sessionid *sessionid)
1125{
1126 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1127
1128 return sid->sequence % SESSION_HASH_SIZE;
1129}
1130
Trond Myklebust8f199b82012-03-20 15:11:17 -04001131#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001132static inline void
1133dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1134{
1135 u32 *ptr = (u32 *)(&sessionid->data[0]);
1136 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1137}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001138#else
1139static inline void
1140dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1141{
1142}
1143#endif
1144
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001145/*
1146 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1147 * won't be used for replay.
1148 */
1149void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1150{
1151 struct nfs4_stateowner *so = cstate->replay_owner;
1152
1153 if (nfserr == nfserr_replay_me)
1154 return;
1155
1156 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001157 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001158 return;
1159 }
1160 if (!so)
1161 return;
1162 if (so->so_is_open_owner)
1163 release_last_closed_stateid(openowner(so));
1164 so->so_seqid++;
1165 return;
1166}
Marc Eshel5282fd72009-04-03 08:27:52 +03001167
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001168static void
1169gen_sessionid(struct nfsd4_session *ses)
1170{
1171 struct nfs4_client *clp = ses->se_client;
1172 struct nfsd4_sessionid *sid;
1173
1174 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1175 sid->clientid = clp->cl_clientid;
1176 sid->sequence = current_sessionid++;
1177 sid->reserved = 0;
1178}
1179
1180/*
Andy Adamsona6496372009-08-28 08:45:01 -04001181 * The protocol defines ca_maxresponssize_cached to include the size of
1182 * the rpc header, but all we need to cache is the data starting after
1183 * the end of the initial SEQUENCE operation--the rest we regenerate
1184 * each time. Therefore we can advertise a ca_maxresponssize_cached
1185 * value that is the number of bytes in our cache plus a few additional
1186 * bytes. In order to stay on the safe side, and not promise more than
1187 * we can cache, those additional bytes must be the minimum possible: 24
1188 * bytes of rpc header (xid through accept state, with AUTH_NULL
1189 * verifier), 12 for the compound header (with zero-length tag), and 44
1190 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001191 */
Andy Adamsona6496372009-08-28 08:45:01 -04001192#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1193
Andy Adamson557ce262009-08-28 08:45:04 -04001194static void
1195free_session_slots(struct nfsd4_session *ses)
1196{
1197 int i;
1198
1199 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1200 kfree(ses->se_slots[i]);
1201}
1202
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001203/*
1204 * We don't actually need to cache the rpc and session headers, so we
1205 * can allocate a little less for each slot:
1206 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001207static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001208{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001209 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001210
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001211 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1212 size = 0;
1213 else
1214 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1215 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001216}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001217
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001218/*
1219 * XXX: If we run out of reserved DRC memory we could (up to a point)
1220 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001221 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001222 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001223static u32 nfsd4_get_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 u32 slotsize = slot_bytes(ca);
1226 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001227 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001228
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001229 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001230 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1231 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001232 num = min_t(int, num, avail / slotsize);
1233 nfsd_drc_mem_used += num * slotsize;
1234 spin_unlock(&nfsd_drc_lock);
1235
1236 return num;
1237}
1238
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001239static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001240{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001241 int slotsize = slot_bytes(ca);
1242
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001243 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001244 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001245 spin_unlock(&nfsd_drc_lock);
1246}
1247
Kinglong Mee60810e52014-01-01 00:35:47 +08001248static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1249 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001250{
Kinglong Mee60810e52014-01-01 00:35:47 +08001251 int numslots = fattrs->maxreqs;
1252 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001253 struct nfsd4_session *new;
1254 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001255
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001256 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001257 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1258 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001259
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001260 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001261 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001262 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001263 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001264 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001265 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001266 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001267 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001268 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001269
1270 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1271 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1272
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001273 return new;
1274out_free:
1275 while (i--)
1276 kfree(new->se_slots[i]);
1277 kfree(new);
1278 return NULL;
1279}
1280
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001281static void free_conn(struct nfsd4_conn *c)
1282{
1283 svc_xprt_put(c->cn_xprt);
1284 kfree(c);
1285}
1286
1287static void nfsd4_conn_lost(struct svc_xpt_user *u)
1288{
1289 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1290 struct nfs4_client *clp = c->cn_session->se_client;
1291
1292 spin_lock(&clp->cl_lock);
1293 if (!list_empty(&c->cn_persession)) {
1294 list_del(&c->cn_persession);
1295 free_conn(c);
1296 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001297 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001298 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001299}
1300
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001301static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001302{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001303 struct nfsd4_conn *conn;
1304
1305 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1306 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001307 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001308 svc_xprt_get(rqstp->rq_xprt);
1309 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001310 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001311 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1312 return conn;
1313}
1314
J. Bruce Fields328ead22010-09-29 16:11:06 -04001315static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1316{
1317 conn->cn_session = ses;
1318 list_add(&conn->cn_persession, &ses->se_conns);
1319}
1320
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001321static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1322{
1323 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001324
1325 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001326 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001327 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001328}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001329
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001330static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001331{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001332 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001333 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001334}
1335
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001336static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001337{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001338 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001339
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001340 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001341 ret = nfsd4_register_conn(conn);
1342 if (ret)
1343 /* oops; xprt is already down: */
1344 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001345 /* We may have gained or lost a callback channel: */
1346 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001347}
1348
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001349static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001350{
1351 u32 dir = NFS4_CDFC4_FORE;
1352
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001353 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001354 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001355 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001356}
1357
1358/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001359static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001360{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001361 struct nfs4_client *clp = s->se_client;
1362 struct nfsd4_conn *c;
1363
1364 spin_lock(&clp->cl_lock);
1365 while (!list_empty(&s->se_conns)) {
1366 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1367 list_del_init(&c->cn_persession);
1368 spin_unlock(&clp->cl_lock);
1369
1370 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1371 free_conn(c);
1372
1373 spin_lock(&clp->cl_lock);
1374 }
1375 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001376}
1377
J. Bruce Fields1377b692012-09-11 21:42:40 -04001378static void __free_session(struct nfsd4_session *ses)
1379{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001380 free_session_slots(ses);
1381 kfree(ses);
1382}
1383
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001384static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001385{
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001386 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1387
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001388 lockdep_assert_held(&nn->client_lock);
1389 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001390 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001391 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001392}
Andy Adamson557ce262009-08-28 08:45:04 -04001393
Fengguang Wu135ae822012-11-10 07:20:25 -05001394static 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 -04001395{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001396 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001397 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001398
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001399 new->se_client = clp;
1400 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001401
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001402 INIT_LIST_HEAD(&new->se_conns);
1403
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001404 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001405 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001406 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001407 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001408 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001409 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001410 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001411 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001412 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001413 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001414 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001415 spin_unlock(&nn->client_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001416
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001417 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001418 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001419 /*
1420 * This is a little silly; with sessions there's no real
1421 * use for the callback address. Use the peer address
1422 * as a reasonable default for now, but consider fixing
1423 * the rpc client not to require an address in the
1424 * future:
1425 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001426 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1427 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001428 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001429}
1430
Benny Halevy9089f1b2010-05-12 00:12:26 +03001431/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001432static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001433__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001434{
1435 struct nfsd4_session *elem;
1436 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001437 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001438
1439 dump_sessionid(__func__, sessionid);
1440 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001441 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001442 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001443 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1444 NFS4_MAX_SESSIONID_LEN)) {
1445 return elem;
1446 }
1447 }
1448
1449 dprintk("%s: session not found\n", __func__);
1450 return NULL;
1451}
1452
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001453static struct nfsd4_session *
1454find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1455 __be32 *ret)
1456{
1457 struct nfsd4_session *session;
1458 __be32 status = nfserr_badsession;
1459
1460 session = __find_in_sessionid_hashtbl(sessionid, net);
1461 if (!session)
1462 goto out;
1463 status = nfsd4_get_session_locked(session);
1464 if (status)
1465 session = NULL;
1466out:
1467 *ret = status;
1468 return session;
1469}
1470
Benny Halevy9089f1b2010-05-12 00:12:26 +03001471/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001472static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001473unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001474{
1475 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001476 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001477 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001478 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001479}
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1482static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001483STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001485 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001487 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001488 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return 1;
1490}
1491
1492/*
1493 * XXX Should we use a slab cache ?
1494 * This type of memory management is somewhat inefficient, but we use it
1495 * anyway since SETCLIENTID is not a common operation.
1496 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001497static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
1499 struct nfs4_client *clp;
1500
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001501 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1502 if (clp == NULL)
1503 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001504 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001505 if (clp->cl_name.data == NULL) {
1506 kfree(clp);
1507 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001509 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001510 INIT_LIST_HEAD(&clp->cl_sessions);
1511 idr_init(&clp->cl_stateids);
1512 atomic_set(&clp->cl_refcount, 0);
1513 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1514 INIT_LIST_HEAD(&clp->cl_idhash);
1515 INIT_LIST_HEAD(&clp->cl_openowners);
1516 INIT_LIST_HEAD(&clp->cl_delegations);
1517 INIT_LIST_HEAD(&clp->cl_lru);
1518 INIT_LIST_HEAD(&clp->cl_callbacks);
1519 INIT_LIST_HEAD(&clp->cl_revoked);
1520 spin_lock_init(&clp->cl_lock);
1521 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return clp;
1523}
1524
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001525static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526free_client(struct nfs4_client *clp)
1527{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001528 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001529
1530 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001531 while (!list_empty(&clp->cl_sessions)) {
1532 struct nfsd4_session *ses;
1533 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1534 se_perclnt);
1535 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001536 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1537 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001538 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001539 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001540 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001542 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 kfree(clp);
1544}
1545
Benny Halevy84d38ac2010-05-12 00:13:16 +03001546/* must be called under the client_lock */
1547static inline void
1548unhash_client_locked(struct nfs4_client *clp)
1549{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001550 struct nfsd4_session *ses;
1551
Benny Halevy84d38ac2010-05-12 00:13:16 +03001552 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001553 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001554 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1555 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001556 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001557}
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001560destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001562 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001565 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001568 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001569 while (!list_empty(&clp->cl_delegations)) {
1570 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001571 unhash_delegation_locked(dp);
1572 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
Benny Halevycdc97502014-05-30 09:09:30 -04001574 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 while (!list_empty(&reaplist)) {
1576 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001577 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001578 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001580 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001581 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001582 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001583 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001584 }
NeilBrownea1da632005-06-23 22:04:17 -07001585 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001586 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Jeff Laytond3134b12014-07-29 21:34:32 -04001587 atomic_inc(&oo->oo_owner.so_count);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001588 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001590 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001591 if (clp->cl_cb_conn.cb_xprt)
1592 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001593 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001594 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001595 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001596 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001597 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001598 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001599 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001600 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1601 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001602 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001605static void expire_client(struct nfs4_client *clp)
1606{
1607 nfsd4_client_record_remove(clp);
1608 destroy_client(clp);
1609}
1610
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001611static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1612{
1613 memcpy(target->cl_verifier.data, source->data,
1614 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615}
1616
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001617static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1618{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1620 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1621}
1622
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001623static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001624{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001625 if (source->cr_principal) {
1626 target->cr_principal =
1627 kstrdup(source->cr_principal, GFP_KERNEL);
1628 if (target->cr_principal == NULL)
1629 return -ENOMEM;
1630 } else
1631 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001632 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 target->cr_uid = source->cr_uid;
1634 target->cr_gid = source->cr_gid;
1635 target->cr_group_info = source->cr_group_info;
1636 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001637 target->cr_gss_mech = source->cr_gss_mech;
1638 if (source->cr_gss_mech)
1639 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001640 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001643static long long
1644compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1645{
1646 long long res;
1647
1648 res = o1->len - o2->len;
1649 if (res)
1650 return res;
1651 return (long long)memcmp(o1->data, o2->data, o1->len);
1652}
1653
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001654static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001655{
NeilBrowna55370a2005-06-23 22:03:52 -07001656 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658
1659static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001660same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1661{
1662 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
1665static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001666same_clid(clientid_t *cl1, clientid_t *cl2)
1667{
1668 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
1670
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001671static bool groups_equal(struct group_info *g1, struct group_info *g2)
1672{
1673 int i;
1674
1675 if (g1->ngroups != g2->ngroups)
1676 return false;
1677 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001678 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001679 return false;
1680 return true;
1681}
1682
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001683/*
1684 * RFC 3530 language requires clid_inuse be returned when the
1685 * "principal" associated with a requests differs from that previously
1686 * used. We use uid, gid's, and gss principal string as our best
1687 * approximation. We also don't want to allow non-gss use of a client
1688 * established using gss: in theory cr_principal should catch that
1689 * change, but in practice cr_principal can be null even in the gss case
1690 * since gssd doesn't always pass down a principal string.
1691 */
1692static bool is_gss_cred(struct svc_cred *cr)
1693{
1694 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1695 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1696}
1697
1698
Vivek Trivedi5559b502012-07-24 21:18:20 +05301699static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001700same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1701{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001702 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001703 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1704 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001705 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1706 return false;
1707 if (cr1->cr_principal == cr2->cr_principal)
1708 return true;
1709 if (!cr1->cr_principal || !cr2->cr_principal)
1710 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301711 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
1713
J. Bruce Fields57266a62013-04-13 14:27:29 -04001714static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1715{
1716 struct svc_cred *cr = &rqstp->rq_cred;
1717 u32 service;
1718
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001719 if (!cr->cr_gss_mech)
1720 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001721 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1722 return service == RPC_GSS_SVC_INTEGRITY ||
1723 service == RPC_GSS_SVC_PRIVACY;
1724}
1725
1726static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1727{
1728 struct svc_cred *cr = &rqstp->rq_cred;
1729
1730 if (!cl->cl_mach_cred)
1731 return true;
1732 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1733 return false;
1734 if (!svc_rqst_integrity_protected(rqstp))
1735 return false;
1736 if (!cr->cr_principal)
1737 return false;
1738 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1739}
1740
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001741static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001742{
1743 static u32 current_clientid = 1;
1744
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001745 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 clp->cl_clientid.cl_id = current_clientid++;
1747}
1748
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001749static void gen_confirm(struct nfs4_client *clp)
1750{
Chuck Leverab4684d2012-03-02 17:13:50 -05001751 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001752 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Jeff Laytonf4199922014-06-17 07:44:11 -04001754 /*
1755 * This is opaque to client, so no need to byte-swap. Use
1756 * __force to keep sparse happy
1757 */
1758 verf[0] = (__force __be32)get_seconds();
1759 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001760 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
1762
Jeff Layton4770d722014-07-29 21:34:10 -04001763static struct nfs4_stid *
1764find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001765{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001766 struct nfs4_stid *ret;
1767
1768 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1769 if (!ret || !ret->sc_type)
1770 return NULL;
1771 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001772}
1773
Jeff Layton4770d722014-07-29 21:34:10 -04001774static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001775find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001776{
1777 struct nfs4_stid *s;
1778
Jeff Layton4770d722014-07-29 21:34:10 -04001779 spin_lock(&cl->cl_lock);
1780 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001781 if (s != NULL) {
1782 if (typemask & s->sc_type)
1783 atomic_inc(&s->sc_count);
1784 else
1785 s = NULL;
1786 }
Jeff Layton4770d722014-07-29 21:34:10 -04001787 spin_unlock(&cl->cl_lock);
1788 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001789}
1790
Jeff Layton2216d442012-11-12 15:00:57 -05001791static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001792 struct svc_rqst *rqstp, nfs4_verifier *verf)
1793{
1794 struct nfs4_client *clp;
1795 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001796 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001797 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001798 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001799
1800 clp = alloc_client(name);
1801 if (clp == NULL)
1802 return NULL;
1803
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001804 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1805 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001806 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001807 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001808 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001809 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001810 }
Jeff Layton02e12152014-07-16 10:31:57 -04001811 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001812 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001813 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001814 copy_verf(clp, verf);
1815 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001816 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001817 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001818 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001819 return clp;
1820}
1821
NeilBrownfd39ca92005-06-23 22:04:03 -07001822static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001823add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1824{
1825 struct rb_node **new = &(root->rb_node), *parent = NULL;
1826 struct nfs4_client *clp;
1827
1828 while (*new) {
1829 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1830 parent = *new;
1831
1832 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1833 new = &((*new)->rb_left);
1834 else
1835 new = &((*new)->rb_right);
1836 }
1837
1838 rb_link_node(&new_clp->cl_namenode, parent, new);
1839 rb_insert_color(&new_clp->cl_namenode, root);
1840}
1841
1842static struct nfs4_client *
1843find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1844{
1845 long long cmp;
1846 struct rb_node *node = root->rb_node;
1847 struct nfs4_client *clp;
1848
1849 while (node) {
1850 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1851 cmp = compare_blob(&clp->cl_name, name);
1852 if (cmp > 0)
1853 node = node->rb_left;
1854 else if (cmp < 0)
1855 node = node->rb_right;
1856 else
1857 return clp;
1858 }
1859 return NULL;
1860}
1861
1862static void
1863add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
1865 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001866 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001868 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001869 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001871 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001872 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873}
1874
NeilBrownfd39ca92005-06-23 22:04:03 -07001875static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876move_to_confirmed(struct nfs4_client *clp)
1877{
1878 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001879 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001882 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001883 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001884 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001885 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 renew_client(clp);
1887}
1888
1889static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001890find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
1892 struct nfs4_client *clp;
1893 unsigned int idhashval = clientid_hashval(clid->cl_id);
1894
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001895 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001896 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001897 if ((bool)clp->cl_minorversion != sessions)
1898 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001899 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
1903 return NULL;
1904}
1905
1906static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001907find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1908{
1909 struct list_head *tbl = nn->conf_id_hashtbl;
1910
1911 return find_client_in_id_table(tbl, clid, sessions);
1912}
1913
1914static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001915find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001917 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001919 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920}
1921
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001922static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001923{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001924 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001925}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001926
NeilBrown28ce6052005-06-23 22:03:56 -07001927static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001928find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001929{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001930 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001931}
1932
1933static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001934find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001935{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001936 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001937}
1938
NeilBrownfd39ca92005-06-23 22:04:03 -07001939static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001940gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001942 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001943 struct sockaddr *sa = svc_addr(rqstp);
1944 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001945 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Jeff Layton7077ecb2009-08-14 12:57:58 -04001947 /* Currently, we only support tcp and tcp6 for the callback channel */
1948 if (se->se_callback_netid_len == 3 &&
1949 !memcmp(se->se_callback_netid_val, "tcp", 3))
1950 expected_family = AF_INET;
1951 else if (se->se_callback_netid_len == 4 &&
1952 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1953 expected_family = AF_INET6;
1954 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 goto out_err;
1956
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001957 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001958 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001959 (struct sockaddr *)&conn->cb_addr,
1960 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001961
J. Bruce Fields07263f12010-05-31 19:09:40 -04001962 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001964
J. Bruce Fields07263f12010-05-31 19:09:40 -04001965 if (conn->cb_addr.ss_family == AF_INET6)
1966 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001967
J. Bruce Fields07263f12010-05-31 19:09:40 -04001968 conn->cb_prog = se->se_callback_prog;
1969 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001970 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 return;
1972out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001973 conn->cb_addr.ss_family = AF_UNSPEC;
1974 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001975 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 "will not receive delegations\n",
1977 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 return;
1980}
1981
Andy Adamson074fe892009-04-03 08:28:15 +03001982/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04001983 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001984 */
Trond Myklebustb6076642014-06-30 11:48:35 -04001985static void
Andy Adamson074fe892009-04-03 08:28:15 +03001986nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1987{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001988 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04001989 struct nfsd4_slot *slot = resp->cstate.slot;
1990 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001991
Andy Adamson557ce262009-08-28 08:45:04 -04001992 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001993
Andy Adamson557ce262009-08-28 08:45:04 -04001994 slot->sl_opcnt = resp->opcnt;
1995 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001996
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001997 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001998 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001999 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002000 return;
2001 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002002 base = resp->cstate.data_offset;
2003 slot->sl_datalen = buf->len - base;
2004 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04002005 WARN("%s: sessions DRC could not cache compound\n", __func__);
2006 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002007}
2008
2009/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002010 * Encode the replay sequence operation from the slot values.
2011 * If cachethis is FALSE encode the uncached rep error on the next
2012 * operation which sets resp->p and increments resp->opcnt for
2013 * nfs4svc_encode_compoundres.
2014 *
Andy Adamson074fe892009-04-03 08:28:15 +03002015 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002016static __be32
2017nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2018 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002019{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002020 struct nfsd4_op *op;
2021 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002022
Andy Adamsonabfabf82009-07-23 19:02:18 -04002023 /* Encode the replayed sequence operation */
2024 op = &args->ops[resp->opcnt - 1];
2025 nfsd4_encode_operation(resp, op);
2026
2027 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002028 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002029 op = &args->ops[resp->opcnt++];
2030 op->status = nfserr_retry_uncached_rep;
2031 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002032 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002033 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002034}
2035
2036/*
Andy Adamson557ce262009-08-28 08:45:04 -04002037 * The sequence operation is not cached because we can use the slot and
2038 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002039 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002040static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002041nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2042 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002043{
Andy Adamson557ce262009-08-28 08:45:04 -04002044 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002045 struct xdr_stream *xdr = &resp->xdr;
2046 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002047 __be32 status;
2048
Andy Adamson557ce262009-08-28 08:45:04 -04002049 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002050
Andy Adamsonabfabf82009-07-23 19:02:18 -04002051 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002052 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002053 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002054
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002055 p = xdr_reserve_space(xdr, slot->sl_datalen);
2056 if (!p) {
2057 WARN_ON_ONCE(1);
2058 return nfserr_serverfault;
2059 }
2060 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2061 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002062
Andy Adamson557ce262009-08-28 08:45:04 -04002063 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002064 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002065}
2066
Andy Adamson0733d212009-04-03 08:28:01 +03002067/*
2068 * Set the exchange_id flags returned by the server.
2069 */
2070static void
2071nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2072{
2073 /* pNFS is not supported */
2074 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2075
2076 /* Referrals are supported, Migration is not. */
2077 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2078
2079 /* set the wire flags to return to client. */
2080 clid->flags = new->cl_exchange_flags;
2081}
2082
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002083static bool client_has_state(struct nfs4_client *clp)
2084{
2085 /*
2086 * Note clp->cl_openowners check isn't quite right: there's no
2087 * need to count owners without stateid's.
2088 *
2089 * Also note we should probably be using this in 4.0 case too.
2090 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002091 return !list_empty(&clp->cl_openowners)
2092 || !list_empty(&clp->cl_delegations)
2093 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002094}
2095
Al Virob37ad282006-10-19 23:28:59 -07002096__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002097nfsd4_exchange_id(struct svc_rqst *rqstp,
2098 struct nfsd4_compound_state *cstate,
2099 struct nfsd4_exchange_id *exid)
2100{
Andy Adamson0733d212009-04-03 08:28:01 +03002101 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002102 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002103 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002104 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002105 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002106 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002107 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002108
Jeff Layton363168b2009-08-14 12:57:56 -04002109 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002110 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002111 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002112 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002113 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002114
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002115 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002116 return nfserr_inval;
2117
Andy Adamson0733d212009-04-03 08:28:01 +03002118 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002119 case SP4_MACH_CRED:
2120 if (!svc_rqst_integrity_protected(rqstp))
2121 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002122 case SP4_NONE:
2123 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002124 default: /* checked by xdr code */
2125 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002126 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002127 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002128 }
2129
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002130 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03002131 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002132 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002133 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002134 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2135 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2136
J. Bruce Fields136e6582012-05-12 20:37:23 -04002137 if (update) {
2138 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002139 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002140 goto out;
2141 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002142 if (!mach_creds_match(conf, rqstp)) {
2143 status = nfserr_wrong_cred;
2144 goto out;
2145 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002146 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002147 status = nfserr_perm;
2148 goto out;
2149 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002150 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002151 status = nfserr_not_same;
2152 goto out;
2153 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002154 /* case 6 */
2155 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2156 new = conf;
2157 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002158 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002159 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002160 if (client_has_state(conf)) {
2161 status = nfserr_clid_inuse;
2162 goto out;
2163 }
Andy Adamson0733d212009-04-03 08:28:01 +03002164 expire_client(conf);
2165 goto out_new;
2166 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002167 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002168 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002169 new = conf;
2170 goto out_copy;
2171 }
2172 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04002173 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002174 }
2175
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002176 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002177 status = nfserr_noent;
2178 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002179 }
2180
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002181 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002182 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03002183 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002184
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002185 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002186out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05002187 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03002188 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04002189 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03002190 goto out;
2191 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002192 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002193 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002194
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002195 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002196 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03002197out_copy:
2198 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2199 exid->clientid.cl_id = new->cl_clientid.cl_id;
2200
J. Bruce Fields778df3f2012-05-25 21:40:23 -04002201 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03002202 nfsd4_set_ex_flags(new, exid);
2203
2204 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04002205 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002206 status = nfs_ok;
2207
2208out:
2209 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03002210 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002211}
2212
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002213static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002214check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002215{
Andy Adamson88e588d2009-07-23 19:02:15 -04002216 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2217 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002218
2219 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002220 if (slot_inuse) {
2221 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002222 return nfserr_jukebox;
2223 else
2224 return nfserr_seq_misordered;
2225 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002226 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002227 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002228 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002229 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002230 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002231 return nfserr_seq_misordered;
2232}
2233
Andy Adamson49557cc2009-07-23 19:02:16 -04002234/*
2235 * Cache the create session result into the create session single DRC
2236 * slot cache by saving the xdr structure. sl_seqid has been set.
2237 * Do this for solo or embedded create session operations.
2238 */
2239static void
2240nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002241 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002242{
2243 slot->sl_status = nfserr;
2244 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2245}
2246
2247static __be32
2248nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2249 struct nfsd4_clid_slot *slot)
2250{
2251 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2252 return slot->sl_status;
2253}
2254
Mi Jinlong1b74c252011-07-14 14:50:17 +08002255#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2256 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2257 1 + /* MIN tag is length with zero, only length */ \
2258 3 + /* version, opcount, opcode */ \
2259 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2260 /* seqid, slotID, slotID, cache */ \
2261 4 ) * sizeof(__be32))
2262
2263#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2264 2 + /* verifier: AUTH_NULL, length 0 */\
2265 1 + /* status */ \
2266 1 + /* MIN tag is length with zero, only length */ \
2267 3 + /* opcount, opcode, opstatus*/ \
2268 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2269 /* seqid, slotID, slotID, slotID, status */ \
2270 5 ) * sizeof(__be32))
2271
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002272static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002273{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002274 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2275
J. Bruce Fields373cd402013-04-08 15:42:12 -04002276 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2277 return nfserr_toosmall;
2278 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2279 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002280 ca->headerpadsz = 0;
2281 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2282 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2283 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2284 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2285 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2286 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2287 /*
2288 * Note decreasing slot size below client's request may make it
2289 * difficult for client to function correctly, whereas
2290 * decreasing the number of slots will (just?) affect
2291 * performance. When short on memory we therefore prefer to
2292 * decrease number of slots instead of their size. Clients that
2293 * request larger slots than they need will get poor results:
2294 */
2295 ca->maxreqs = nfsd4_get_drc_mem(ca);
2296 if (!ca->maxreqs)
2297 return nfserr_jukebox;
2298
J. Bruce Fields373cd402013-04-08 15:42:12 -04002299 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002300}
2301
Kinglong Mee8a891632013-12-23 18:11:02 +08002302#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2303 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2304#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2305 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2306
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002307static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2308{
2309 ca->headerpadsz = 0;
2310
2311 /*
2312 * These RPC_MAX_HEADER macros are overkill, especially since we
2313 * don't even do gss on the backchannel yet. But this is still
2314 * less than 1k. Tighten up this estimate in the unlikely event
2315 * it turns out to be a problem for some client:
2316 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002317 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002318 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002319 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002320 return nfserr_toosmall;
2321 ca->maxresp_cached = 0;
2322 if (ca->maxops < 2)
2323 return nfserr_toosmall;
2324
2325 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002326}
2327
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002328static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2329{
2330 switch (cbs->flavor) {
2331 case RPC_AUTH_NULL:
2332 case RPC_AUTH_UNIX:
2333 return nfs_ok;
2334 default:
2335 /*
2336 * GSS case: the spec doesn't allow us to return this
2337 * error. But it also doesn't allow us not to support
2338 * GSS.
2339 * I'd rather this fail hard than return some error the
2340 * client might think it can already handle:
2341 */
2342 return nfserr_encr_alg_unsupp;
2343 }
2344}
2345
Andy Adamson069b6ad2009-04-03 08:27:58 +03002346__be32
2347nfsd4_create_session(struct svc_rqst *rqstp,
2348 struct nfsd4_compound_state *cstate,
2349 struct nfsd4_create_session *cr_ses)
2350{
Jeff Layton363168b2009-08-14 12:57:56 -04002351 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002352 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002353 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002354 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002355 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002356 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002357 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002358
Mi Jinlonga62573d2011-03-23 17:57:07 +08002359 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2360 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002361 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2362 if (status)
2363 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002364 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002365 if (status)
2366 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002367 status = check_backchannel_attrs(&cr_ses->back_channel);
2368 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002369 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002370 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002371 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002372 if (!new)
2373 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002374 conn = alloc_conn_from_crses(rqstp, cr_ses);
2375 if (!conn)
2376 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002377
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002378 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002379 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002380 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002381 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002382
2383 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002384 status = nfserr_wrong_cred;
2385 if (!mach_creds_match(conf, rqstp))
2386 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002387 cs_slot = &conf->cl_cs_slot;
2388 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002389 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002390 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002391 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002392 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002393 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002394 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002395 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002396 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002397 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002398 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002399 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002400 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002401 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002402 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002403 status = nfserr_wrong_cred;
2404 if (!mach_creds_match(unconf, rqstp))
2405 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002406 cs_slot = &unconf->cl_cs_slot;
2407 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002408 if (status) {
2409 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002410 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002411 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002412 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002413 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002414 if (old) {
2415 status = mark_client_expired(old);
2416 if (status)
2417 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002418 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002419 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002420 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002421 conf = unconf;
2422 } else {
2423 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002424 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002425 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002426 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002427 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002428 * We do not support RDMA or persistent sessions
2429 */
2430 cr_ses->flags &= ~SESSION4_PERSIST;
2431 cr_ses->flags &= ~SESSION4_RDMA;
2432
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002433 init_session(rqstp, new, conf, cr_ses);
2434 nfsd4_init_conn(rqstp, conn, new);
2435
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002436 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002437 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002438 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002439 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002440
Andy Adamson49557cc2009-07-23 19:02:16 -04002441 /* cache solo and embedded create sessions under the state lock */
2442 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002443 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002444 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002445out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08002446 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002447 free_conn(conn);
2448out_free_session:
2449 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002450out_release_drc_mem:
2451 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002452 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002453}
2454
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002455static __be32 nfsd4_map_bcts_dir(u32 *dir)
2456{
2457 switch (*dir) {
2458 case NFS4_CDFC4_FORE:
2459 case NFS4_CDFC4_BACK:
2460 return nfs_ok;
2461 case NFS4_CDFC4_FORE_OR_BOTH:
2462 case NFS4_CDFC4_BACK_OR_BOTH:
2463 *dir = NFS4_CDFC4_BOTH;
2464 return nfs_ok;
2465 };
2466 return nfserr_inval;
2467}
2468
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002469__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2470{
2471 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002472 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002473 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002474
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002475 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2476 if (status)
2477 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002478 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002479 session->se_cb_prog = bc->bc_cb_program;
2480 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002481 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002482
2483 nfsd4_probe_callback(session->se_client);
2484
2485 return nfs_ok;
2486}
2487
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002488__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2489 struct nfsd4_compound_state *cstate,
2490 struct nfsd4_bind_conn_to_session *bcts)
2491{
2492 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002493 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002494 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002495 struct net *net = SVC_NET(rqstp);
2496 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002497
2498 if (!nfsd4_last_compound_op(rqstp))
2499 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002500 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002501 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002502 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002503 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002504 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002505 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002506 status = nfserr_wrong_cred;
2507 if (!mach_creds_match(session->se_client, rqstp))
2508 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002509 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002510 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002511 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002512 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002513 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002514 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002515 goto out;
2516 nfsd4_init_conn(rqstp, conn, session);
2517 status = nfs_ok;
2518out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002519 nfsd4_put_session(session);
2520out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002521 nfs4_unlock_state();
2522 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002523}
2524
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002525static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2526{
2527 if (!session)
2528 return 0;
2529 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2530}
2531
Andy Adamson069b6ad2009-04-03 08:27:58 +03002532__be32
2533nfsd4_destroy_session(struct svc_rqst *r,
2534 struct nfsd4_compound_state *cstate,
2535 struct nfsd4_destroy_session *sessionid)
2536{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002537 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002538 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002539 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002540 struct net *net = SVC_NET(r);
2541 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002542
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002543 nfs4_lock_state();
2544 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002545 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002546 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002547 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002548 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002549 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002550 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002551 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002552 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002553 if (!ses)
2554 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002555 status = nfserr_wrong_cred;
2556 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002557 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002558 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002559 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002560 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002561 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002562 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002563
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002564 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002565
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002566 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002567 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002568out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002569 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002570out_client_lock:
2571 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002572out:
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002573 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002574 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002575}
2576
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002577static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002578{
2579 struct nfsd4_conn *c;
2580
2581 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002582 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002583 return c;
2584 }
2585 }
2586 return NULL;
2587}
2588
J. Bruce Fields57266a62013-04-13 14:27:29 -04002589static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002590{
2591 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002592 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002593 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002594 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002595
2596 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002597 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002598 if (c)
2599 goto out_free;
2600 status = nfserr_conn_not_bound_to_session;
2601 if (clp->cl_mach_cred)
2602 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002603 __nfsd4_hash_conn(new, ses);
2604 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002605 ret = nfsd4_register_conn(new);
2606 if (ret)
2607 /* oops; xprt is already down: */
2608 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002609 return nfs_ok;
2610out_free:
2611 spin_unlock(&clp->cl_lock);
2612 free_conn(new);
2613 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002614}
2615
Mi Jinlong868b89c2011-04-27 09:09:58 +08002616static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2617{
2618 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2619
2620 return args->opcnt > session->se_fchannel.maxops;
2621}
2622
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002623static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2624 struct nfsd4_session *session)
2625{
2626 struct xdr_buf *xb = &rqstp->rq_arg;
2627
2628 return xb->len > session->se_fchannel.maxreq_sz;
2629}
2630
Andy Adamson069b6ad2009-04-03 08:27:58 +03002631__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002632nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002633 struct nfsd4_compound_state *cstate,
2634 struct nfsd4_sequence *seq)
2635{
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002636 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002637 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002638 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002639 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002640 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002641 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002642 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002643 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002644 struct net *net = SVC_NET(rqstp);
2645 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002646
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002647 if (resp->opcnt != 1)
2648 return nfserr_sequence_pos;
2649
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002650 /*
2651 * Will be either used or freed by nfsd4_sequence_check_conn
2652 * below.
2653 */
2654 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2655 if (!conn)
2656 return nfserr_jukebox;
2657
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002658 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002659 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002660 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002661 goto out_no_session;
2662 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002663
Mi Jinlong868b89c2011-04-27 09:09:58 +08002664 status = nfserr_too_many_ops;
2665 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002666 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002667
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002668 status = nfserr_req_too_big;
2669 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002670 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002671
Benny Halevyb85d4c02009-04-03 08:28:08 +03002672 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002673 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002674 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002675
Andy Adamson557ce262009-08-28 08:45:04 -04002676 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002677 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2678
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002679 /* We do not negotiate the number of slots yet, so set the
2680 * maxslots to the session maxreqs which is used to encode
2681 * sr_highest_slotid and the sr_target_slot id to maxslots */
2682 seq->maxslots = session->se_fchannel.maxreqs;
2683
J. Bruce Fields73e79482012-02-13 16:39:00 -05002684 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2685 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002686 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002687 status = nfserr_seq_misordered;
2688 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002689 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002690 cstate->slot = slot;
2691 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002692 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002693 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002694 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002695 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002696 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002697 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002698 }
2699 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002700 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002701
J. Bruce Fields57266a62013-04-13 14:27:29 -04002702 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002703 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002704 if (status)
2705 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002706
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002707 buflen = (seq->cachethis) ?
2708 session->se_fchannel.maxresp_cached :
2709 session->se_fchannel.maxresp_sz;
2710 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2711 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002712 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002713 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002714 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002715
2716 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002717 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002718 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002719 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002720 if (seq->cachethis)
2721 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002722 else
2723 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002724
2725 cstate->slot = slot;
2726 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002727 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002728
Benny Halevyb85d4c02009-04-03 08:28:08 +03002729out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002730 switch (clp->cl_cb_state) {
2731 case NFSD4_CB_DOWN:
2732 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2733 break;
2734 case NFSD4_CB_FAULT:
2735 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2736 break;
2737 default:
2738 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002739 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002740 if (!list_empty(&clp->cl_revoked))
2741 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002742out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002743 if (conn)
2744 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002745 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002746 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002747out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002748 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002749 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002750}
2751
Trond Myklebustb6076642014-06-30 11:48:35 -04002752void
2753nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2754{
2755 struct nfsd4_compound_state *cs = &resp->cstate;
2756
2757 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002758 if (cs->status != nfserr_replay_cache) {
2759 nfsd4_store_cache_entry(resp);
2760 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2761 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002762 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002763 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002764 } else if (cs->clp)
2765 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002766}
2767
Mi Jinlong345c2842011-10-20 17:51:39 +08002768__be32
2769nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2770{
2771 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002772 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002773 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002774
2775 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002776 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002777 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002778 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002779
2780 if (conf) {
2781 clp = conf;
2782
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002783 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002784 status = nfserr_clientid_busy;
2785 goto out;
2786 }
2787 } else if (unconf)
2788 clp = unconf;
2789 else {
2790 status = nfserr_stale_clientid;
2791 goto out;
2792 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002793 if (!mach_creds_match(clp, rqstp)) {
2794 status = nfserr_wrong_cred;
2795 goto out;
2796 }
Mi Jinlong345c2842011-10-20 17:51:39 +08002797 expire_client(clp);
2798out:
2799 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002800 return status;
2801}
2802
Andy Adamson069b6ad2009-04-03 08:27:58 +03002803__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002804nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2805{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002806 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002807
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002808 if (rc->rca_one_fs) {
2809 if (!cstate->current_fh.fh_dentry)
2810 return nfserr_nofilehandle;
2811 /*
2812 * We don't take advantage of the rca_one_fs case.
2813 * That's OK, it's optional, we can safely ignore it.
2814 */
2815 return nfs_ok;
2816 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002817
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002818 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002819 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002820 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2821 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002822 goto out;
2823
2824 status = nfserr_stale_clientid;
2825 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002826 /*
2827 * The following error isn't really legal.
2828 * But we only get here if the client just explicitly
2829 * destroyed the client. Surely it no longer cares what
2830 * error it gets back on an operation for the dead
2831 * client.
2832 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002833 goto out;
2834
2835 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002836 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002837out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002838 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002839 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002840}
2841
2842__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002843nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2844 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002846 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002848 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002849 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002850 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2851
J. Bruce Fields63db4632012-05-18 21:54:19 -04002852 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002854 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002855 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002856 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002858 if (clp_used_exchangeid(conf))
2859 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002860 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002861 char addr_str[INET6_ADDRSTRLEN];
2862 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2863 sizeof(addr_str));
2864 dprintk("NFSD: setclientid: string in use by client "
2865 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 goto out;
2867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002869 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002870 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002873 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002874 if (new == NULL)
2875 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002876 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002877 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002879 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002880 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002881 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002882 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002883 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2885 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2886 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2887 status = nfs_ok;
2888out:
2889 nfs4_unlock_state();
2890 return status;
2891}
2892
2893
Al Virob37ad282006-10-19 23:28:59 -07002894__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002895nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2896 struct nfsd4_compound_state *cstate,
2897 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898{
NeilBrown21ab45a2005-06-23 22:04:14 -07002899 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2901 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002902 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002903 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002905 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002908
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002909 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002910 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002911 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002912 * We try hard to give out unique clientid's, so if we get an
2913 * attempt to confirm the same clientid with a different cred,
2914 * there's a bug somewhere. Let's charitably assume it's our
2915 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002916 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002917 status = nfserr_serverfault;
2918 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2919 goto out;
2920 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2921 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002922 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002923 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2924 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002926 else /* case 4: client hasn't noticed we rebooted yet? */
2927 status = nfserr_stale_clientid;
2928 goto out;
2929 }
2930 status = nfs_ok;
2931 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002932 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2933 nfsd4_probe_callback(conf);
2934 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002935 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002936 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002937 if (conf) {
2938 status = mark_client_expired(conf);
2939 if (status)
2940 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002941 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002942 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002943 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002944 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 nfs4_unlock_state();
2948 return status;
2949}
2950
J. Bruce Fields32513b42011-10-13 16:00:16 -04002951static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002953 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2954}
2955
2956/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04002957static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04002958{
Trond Myklebustca943212014-07-23 16:17:39 -04002959 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
Trond Myklebust950e0112014-06-30 11:48:31 -04002961 lockdep_assert_held(&state_lock);
2962
J. Bruce Fields32513b42011-10-13 16:00:16 -04002963 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04002964 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002965 INIT_LIST_HEAD(&fp->fi_stateids);
2966 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04002967 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002968 fp->fi_had_conflict = false;
2969 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04002970 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002971 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2972 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04002973 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974}
2975
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002976void
NeilBrowne60d4392005-06-23 22:03:01 -07002977nfsd4_free_slabs(void)
2978{
Christoph Hellwigabf11352014-05-21 07:43:03 -07002979 kmem_cache_destroy(openowner_slab);
2980 kmem_cache_destroy(lockowner_slab);
2981 kmem_cache_destroy(file_slab);
2982 kmem_cache_destroy(stateid_slab);
2983 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002984}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
Bryan Schumaker72083392011-11-01 15:24:59 -04002986int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987nfsd4_init_slabs(void)
2988{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002989 openowner_slab = kmem_cache_create("nfsd4_openowners",
2990 sizeof(struct nfs4_openowner), 0, 0, NULL);
2991 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002992 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002993 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002994 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002995 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002996 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07002997 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002998 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002999 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003000 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003001 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003002 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003003 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003004 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003005 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003006 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003007 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003008 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003010
3011out_free_stateid_slab:
3012 kmem_cache_destroy(stateid_slab);
3013out_free_file_slab:
3014 kmem_cache_destroy(file_slab);
3015out_free_lockowner_slab:
3016 kmem_cache_destroy(lockowner_slab);
3017out_free_openowner_slab:
3018 kmem_cache_destroy(openowner_slab);
3019out:
NeilBrowne60d4392005-06-23 22:03:01 -07003020 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3021 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022}
3023
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003024static void init_nfs4_replay(struct nfs4_replay *rp)
3025{
3026 rp->rp_status = nfserr_serverfault;
3027 rp->rp_buflen = 0;
3028 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003029 mutex_init(&rp->rp_mutex);
3030}
3031
3032static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3033 struct nfs4_stateowner *so)
3034{
3035 if (!nfsd4_has_session(cstate)) {
3036 mutex_lock(&so->so_replay.rp_mutex);
3037 cstate->replay_owner = so;
3038 atomic_inc(&so->so_count);
3039 }
3040}
3041
3042void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3043{
3044 struct nfs4_stateowner *so = cstate->replay_owner;
3045
3046 if (so != NULL) {
3047 cstate->replay_owner = NULL;
3048 mutex_unlock(&so->so_replay.rp_mutex);
3049 nfs4_put_stateowner(so);
3050 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003051}
3052
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003053static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
3055 struct nfs4_stateowner *sop;
3056
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003057 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003058 if (!sop)
3059 return NULL;
3060
3061 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3062 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003063 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003064 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003066 sop->so_owner.len = owner->len;
3067
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003068 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003069 sop->so_client = clp;
3070 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003071 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003072 return sop;
3073}
3074
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003075static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003076{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003077 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3078
3079 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003080 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081}
3082
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003083static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3084{
3085 struct nfs4_openowner *oo = openowner(so);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003086 struct nfsd_net *nn = net_generic(so->so_client->net, nfsd_net_id);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003087
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003088 spin_lock(&nn->client_lock);
3089 unhash_openowner_locked(oo);
3090 spin_unlock(&nn->client_lock);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003091}
3092
Jeff Layton6b180f02014-07-29 21:34:26 -04003093static void nfs4_free_openowner(struct nfs4_stateowner *so)
3094{
3095 struct nfs4_openowner *oo = openowner(so);
3096
3097 kmem_cache_free(openowner_slab, oo);
3098}
3099
3100static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003101 .so_unhash = nfs4_unhash_openowner,
3102 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003103};
3104
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003105static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003106alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003107 struct nfsd4_compound_state *cstate)
3108{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003109 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003110 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3111 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003113 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3114 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003116 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003117 oo->oo_owner.so_is_open_owner = 1;
3118 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003119 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003120 if (nfsd4_has_session(cstate))
3121 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003122 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003123 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003124 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003125 spin_lock(&nn->client_lock);
3126 ret = find_openstateowner_str_locked(strhashval,
3127 open, clp->cl_minorversion, nn);
3128 if (ret == NULL) {
3129 hash_openowner(oo, clp, strhashval);
3130 ret = oo;
3131 } else
3132 nfs4_free_openowner(&oo->oo_owner);
3133 spin_unlock(&nn->client_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003134 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135}
3136
J. Bruce Fields996e0932011-10-17 11:14:48 -04003137static 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 -04003138 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003140 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003141 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003142 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003143 stp->st_stateowner = &oo->oo_owner;
Jeff Laytond3134b12014-07-29 21:34:32 -04003144 atomic_inc(&stp->st_stateowner->so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07003145 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003146 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 stp->st_access_bmap = 0;
3148 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003149 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003150 spin_lock(&oo->oo_owner.so_client->cl_lock);
3151 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003152 spin_lock(&fp->fi_lock);
3153 list_add(&stp->st_perfile, &fp->fi_stateids);
3154 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003155 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156}
3157
Jeff Laytond3134b12014-07-29 21:34:32 -04003158/*
3159 * In the 4.0 case we need to keep the owners around a little while to handle
3160 * CLOSE replay. We still do need to release any file access that is held by
3161 * them before returning however.
3162 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003164move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165{
Jeff Laytond3134b12014-07-29 21:34:32 -04003166 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3167 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3168 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003169
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003170 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
Jeff Laytonb401be222014-07-29 21:34:33 -04003172 /*
3173 * We know that we hold one reference via nfsd4_close, and another
3174 * "persistent" reference for the client. If the refcount is higher
3175 * than 2, then there are still calls in progress that are using this
3176 * stateid. We can't put the sc_file reference until they are finished.
3177 * Wait for the refcount to drop to 2. Since it has been unhashed,
3178 * there should be no danger of the refcount going back up again at
3179 * this point.
3180 */
3181 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3182
Jeff Laytond3134b12014-07-29 21:34:32 -04003183 release_all_access(s);
3184 if (s->st_stid.sc_file) {
3185 put_nfs4_file(s->st_stid.sc_file);
3186 s->st_stid.sc_file = NULL;
3187 }
3188 release_last_closed_stateid(oo);
3189 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003190 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003191 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192}
3193
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194/* search file_hashtbl[] for file */
3195static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003196find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197{
Trond Myklebustca943212014-07-23 16:17:39 -04003198 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 struct nfs4_file *fp;
3200
Trond Myklebust950e0112014-06-30 11:48:31 -04003201 lockdep_assert_held(&state_lock);
3202
Jeff Layton89876f82013-04-02 09:01:59 -04003203 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003204 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003205 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 }
3209 return NULL;
3210}
3211
Trond Myklebust950e0112014-06-30 11:48:31 -04003212static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003213find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003214{
3215 struct nfs4_file *fp;
3216
3217 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003218 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003219 spin_unlock(&state_lock);
3220 return fp;
3221}
3222
3223static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003224find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003225{
3226 struct nfs4_file *fp;
3227
3228 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003229 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003230 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003231 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003232 fp = new;
3233 }
3234 spin_unlock(&state_lock);
3235
3236 return fp;
3237}
3238
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003239/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 * Called to check deny when READ with all zero stateid or
3241 * WRITE with all zero or all one stateid
3242 */
Al Virob37ad282006-10-19 23:28:59 -07003243static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3245{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003247 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
Trond Myklebustca943212014-07-23 16:17:39 -04003249 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003250 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003251 return ret;
3252 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003253 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003254 if (fp->fi_share_deny & deny_type)
3255 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003256 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003257 put_nfs4_file(fp);
3258 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259}
3260
Jeff Layton02e12152014-07-16 10:31:57 -04003261void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262{
Trond Myklebust11b91642014-07-29 21:34:08 -04003263 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3264 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003265
Trond Myklebust11b91642014-07-29 21:34:08 -04003266 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003267
Jeff Layton02e12152014-07-16 10:31:57 -04003268 /*
3269 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003270 * already holding inode->i_lock.
3271 *
Jeff Laytondff13992014-07-08 14:02:49 -04003272 * If the dl_time != 0, then we know that it has already been
3273 * queued for a lease break. Don't queue it again.
3274 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003275 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003276 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003277 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003278 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003279 }
Jeff Layton02e12152014-07-16 10:31:57 -04003280 spin_unlock(&state_lock);
3281}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Jeff Layton02e12152014-07-16 10:31:57 -04003283static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3284{
3285 /*
3286 * We're assuming the state code never drops its reference
3287 * without first removing the lease. Since we're in this lease
3288 * callback (and since the lease code is serialized by the kernel
3289 * lock) we know the server hasn't removed the lease yet, we know
3290 * it's safe to take a reference.
3291 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003292 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003293 nfsd4_cb_recall(dp);
3294}
3295
Jeff Layton1c8c6012013-06-21 08:58:15 -04003296/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003297static void nfsd_break_deleg_cb(struct file_lock *fl)
3298{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003299 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3300 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003301
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003302 if (!fp) {
3303 WARN(1, "(%p)->fl_owner NULL\n", fl);
3304 return;
3305 }
3306 if (fp->fi_had_conflict) {
3307 WARN(1, "duplicate break on %p\n", fp);
3308 return;
3309 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003310 /*
3311 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003312 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003313 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003314 */
3315 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
Jeff Layton02e12152014-07-16 10:31:57 -04003317 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003318 fp->fi_had_conflict = true;
3319 /*
3320 * If there are no delegations on the list, then we can't count on this
3321 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3322 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3323 * true should keep any new delegations from being hashed.
3324 */
3325 if (list_empty(&fp->fi_delegations))
3326 fl->fl_break_time = jiffies;
3327 else
3328 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3329 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003330 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331}
3332
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333static
3334int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3335{
3336 if (arg & F_UNLCK)
3337 return lease_modify(onlist, arg);
3338 else
3339 return -EAGAIN;
3340}
3341
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003342static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003343 .lm_break = nfsd_break_deleg_cb,
3344 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345};
3346
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003347static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3348{
3349 if (nfsd4_has_session(cstate))
3350 return nfs_ok;
3351 if (seqid == so->so_seqid - 1)
3352 return nfserr_replay_me;
3353 if (seqid == so->so_seqid)
3354 return nfs_ok;
3355 return nfserr_bad_seqid;
3356}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
Jeff Layton4b24ca72014-06-30 11:48:44 -04003358static __be32 lookup_clientid(clientid_t *clid,
3359 struct nfsd4_compound_state *cstate,
3360 struct nfsd_net *nn)
3361{
3362 struct nfs4_client *found;
3363
3364 if (cstate->clp) {
3365 found = cstate->clp;
3366 if (!same_clid(&found->cl_clientid, clid))
3367 return nfserr_stale_clientid;
3368 return nfs_ok;
3369 }
3370
3371 if (STALE_CLIENTID(clid, nn))
3372 return nfserr_stale_clientid;
3373
3374 /*
3375 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3376 * cached already then we know this is for is for v4.0 and "sessions"
3377 * will be false.
3378 */
3379 WARN_ON_ONCE(cstate->session);
3380 found = find_confirmed_client(clid, false, nn);
3381 if (!found)
3382 return nfserr_expired;
3383
3384 /* Cache the nfs4_client in cstate! */
3385 cstate->clp = found;
3386 atomic_inc(&found->cl_refcount);
3387 return nfs_ok;
3388}
3389
Al Virob37ad282006-10-19 23:28:59 -07003390__be32
Andy Adamson66689582009-04-03 08:28:45 +03003391nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003392 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 clientid_t *clientid = &open->op_clientid;
3395 struct nfs4_client *clp = NULL;
3396 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003397 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003398 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003400 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003402 /*
3403 * In case we need it later, after we've already created the
3404 * file and don't want to risk a further failure:
3405 */
3406 open->op_file = nfsd4_alloc_file();
3407 if (open->op_file == NULL)
3408 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409
Trond Myklebust2d91e892014-06-30 11:48:46 -04003410 status = lookup_clientid(clientid, cstate, nn);
3411 if (status)
3412 return status;
3413 clp = cstate->clp;
3414
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003415 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003416 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003417 open->op_openowner = oo;
3418 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003419 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003421 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003422 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003423 release_openowner(oo);
3424 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003425 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003426 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003427 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3428 if (status)
3429 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003430 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003431new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003432 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003433 if (oo == NULL)
3434 return nfserr_jukebox;
3435 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003436alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003437 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003438 if (!open->op_stp)
3439 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003440 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441}
3442
Al Virob37ad282006-10-19 23:28:59 -07003443static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003444nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3445{
3446 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3447 return nfserr_openmode;
3448 else
3449 return nfs_ok;
3450}
3451
Daniel Mackc47d8322011-05-16 16:38:14 +02003452static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003453{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003454 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3455}
3456
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003457static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003458{
3459 struct nfs4_stid *ret;
3460
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003461 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003462 if (!ret)
3463 return NULL;
3464 return delegstateid(ret);
3465}
3466
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003467static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3468{
3469 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3470 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3471}
3472
Al Virob37ad282006-10-19 23:28:59 -07003473static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003474nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003475 struct nfs4_delegation **dp)
3476{
3477 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003478 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003479 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003480
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003481 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3482 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003483 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003484 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003485 status = nfs4_check_delegmode(deleg, flags);
3486 if (status) {
3487 nfs4_put_stid(&deleg->dl_stid);
3488 goto out;
3489 }
3490 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003491out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003492 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003493 return nfs_ok;
3494 if (status)
3495 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003496 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003497 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003498}
3499
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003500static struct nfs4_ol_stateid *
3501nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003503 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003504 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505
Trond Myklebust1d31a252014-07-10 14:07:25 -04003506 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003507 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 /* ignore lock owners */
3509 if (local->st_stateowner->so_is_open_owner == 0)
3510 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003511 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003512 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003513 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003514 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003517 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003518 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519}
3520
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003521static inline int nfs4_access_to_access(u32 nfs4_access)
3522{
3523 int flags = 0;
3524
3525 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3526 flags |= NFSD_MAY_READ;
3527 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3528 flags |= NFSD_MAY_WRITE;
3529 return flags;
3530}
3531
Al Virob37ad282006-10-19 23:28:59 -07003532static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3534 struct nfsd4_open *open)
3535{
3536 struct iattr iattr = {
3537 .ia_valid = ATTR_SIZE,
3538 .ia_size = 0,
3539 };
3540 if (!open->op_truncate)
3541 return 0;
3542 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003543 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3545}
3546
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003547static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003548 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3549 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003550{
Trond Myklebustde186432014-07-10 14:07:26 -04003551 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003552 __be32 status;
3553 int oflag = nfs4_access_to_omode(open->op_share_access);
3554 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003555 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003556
Trond Myklebustde186432014-07-10 14:07:26 -04003557 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003558
3559 /*
3560 * Are we trying to set a deny mode that would conflict with
3561 * current access?
3562 */
3563 status = nfs4_file_check_deny(fp, open->op_share_deny);
3564 if (status != nfs_ok) {
3565 spin_unlock(&fp->fi_lock);
3566 goto out;
3567 }
3568
3569 /* set access to the file */
3570 status = nfs4_file_get_access(fp, open->op_share_access);
3571 if (status != nfs_ok) {
3572 spin_unlock(&fp->fi_lock);
3573 goto out;
3574 }
3575
3576 /* Set access bits in stateid */
3577 old_access_bmap = stp->st_access_bmap;
3578 set_access(open->op_share_access, stp);
3579
3580 /* Set new deny mask */
3581 old_deny_bmap = stp->st_deny_bmap;
3582 set_deny(open->op_share_deny, stp);
3583 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3584
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003585 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003586 spin_unlock(&fp->fi_lock);
3587 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003588 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003589 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003590 spin_lock(&fp->fi_lock);
3591 if (!fp->fi_fds[oflag]) {
3592 fp->fi_fds[oflag] = filp;
3593 filp = NULL;
3594 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003595 }
Trond Myklebustde186432014-07-10 14:07:26 -04003596 spin_unlock(&fp->fi_lock);
3597 if (filp)
3598 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003599
3600 status = nfsd4_truncate(rqstp, cur_fh, open);
3601 if (status)
3602 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003603out:
3604 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003605out_put_access:
3606 stp->st_access_bmap = old_access_bmap;
3607 nfs4_file_put_access(fp, open->op_share_access);
3608 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3609 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003610}
3611
Al Virob37ad282006-10-19 23:28:59 -07003612static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003613nfs4_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 -07003614{
Al Virob37ad282006-10-19 23:28:59 -07003615 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003616 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003618 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003619 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003620
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003621 /* test and set deny mode */
3622 spin_lock(&fp->fi_lock);
3623 status = nfs4_file_check_deny(fp, open->op_share_deny);
3624 if (status == nfs_ok) {
3625 old_deny_bmap = stp->st_deny_bmap;
3626 set_deny(open->op_share_deny, stp);
3627 fp->fi_share_deny |=
3628 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3629 }
3630 spin_unlock(&fp->fi_lock);
3631
3632 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003635 status = nfsd4_truncate(rqstp, cur_fh, open);
3636 if (status != nfs_ok)
3637 reset_union_bmap_deny(old_deny_bmap, stp);
3638 return status;
3639}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003642nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003644 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645}
3646
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003647/* Should we give out recallable state?: */
3648static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3649{
3650 if (clp->cl_cb_state == NFSD4_CB_UP)
3651 return true;
3652 /*
3653 * In the sessions case, since we don't have to establish a
3654 * separate connection for callbacks, we assume it's OK
3655 * until we hear otherwise:
3656 */
3657 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3658}
3659
Jeff Laytond564fbe2014-07-16 10:31:58 -04003660static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003661{
3662 struct file_lock *fl;
3663
3664 fl = locks_alloc_lock();
3665 if (!fl)
3666 return NULL;
3667 locks_init_lock(fl);
3668 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003669 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003670 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3671 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003672 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003673 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003674 return fl;
3675}
3676
J. Bruce Fields99c41512013-05-21 16:21:25 -04003677static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003678{
Trond Myklebust11b91642014-07-29 21:34:08 -04003679 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003680 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003681 struct file *filp;
3682 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003683
Jeff Laytond564fbe2014-07-16 10:31:58 -04003684 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003685 if (!fl)
3686 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003687 filp = find_readable_file(fp);
3688 if (!filp) {
3689 /* We should always have a readable file here */
3690 WARN_ON_ONCE(1);
3691 return -EBADF;
3692 }
3693 fl->fl_file = filp;
3694 status = vfs_setlease(filp, fl->fl_type, &fl);
3695 if (status) {
3696 locks_free_lock(fl);
3697 goto out_fput;
3698 }
Benny Halevycdc97502014-05-30 09:09:30 -04003699 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003700 spin_lock(&fp->fi_lock);
3701 /* Did the lease get broken before we took the lock? */
3702 status = -EAGAIN;
3703 if (fp->fi_had_conflict)
3704 goto out_unlock;
3705 /* Race breaker */
3706 if (fp->fi_lease) {
3707 status = 0;
3708 atomic_inc(&fp->fi_delegees);
3709 hash_delegation_locked(dp, fp);
3710 goto out_unlock;
3711 }
3712 fp->fi_lease = fl;
3713 fp->fi_deleg_file = filp;
3714 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003715 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003716 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003717 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003718 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003719out_unlock:
3720 spin_unlock(&fp->fi_lock);
3721 spin_unlock(&state_lock);
3722out_fput:
3723 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003724 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003725}
3726
Jeff Layton0b266932014-07-25 07:34:25 -04003727static struct nfs4_delegation *
3728nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3729 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003730{
Jeff Layton0b266932014-07-25 07:34:25 -04003731 int status;
3732 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003733
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003734 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003735 return ERR_PTR(-EAGAIN);
3736
3737 dp = alloc_init_deleg(clp, fh);
3738 if (!dp)
3739 return ERR_PTR(-ENOMEM);
3740
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003741 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003742 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003743 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003744 dp->dl_stid.sc_file = fp;
Jeff Layton417c6622014-07-21 09:34:57 -04003745 if (!fp->fi_lease) {
3746 spin_unlock(&fp->fi_lock);
3747 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003748 status = nfs4_setlease(dp);
3749 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003750 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003751 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003752 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003753 status = -EAGAIN;
3754 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003755 }
Benny Halevy931ee562014-05-30 09:09:27 -04003756 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003757 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003758out_unlock:
3759 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003760 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003761out:
3762 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003763 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003764 return ERR_PTR(status);
3765 }
3766 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003767}
3768
Benny Halevy4aa89132012-02-21 14:16:44 -08003769static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3770{
3771 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3772 if (status == -EAGAIN)
3773 open->op_why_no_deleg = WND4_CONTENTION;
3774 else {
3775 open->op_why_no_deleg = WND4_RESOURCE;
3776 switch (open->op_deleg_want) {
3777 case NFS4_SHARE_WANT_READ_DELEG:
3778 case NFS4_SHARE_WANT_WRITE_DELEG:
3779 case NFS4_SHARE_WANT_ANY_DELEG:
3780 break;
3781 case NFS4_SHARE_WANT_CANCEL:
3782 open->op_why_no_deleg = WND4_CANCELLED;
3783 break;
3784 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003785 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003786 }
3787 }
3788}
3789
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790/*
3791 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003792 *
3793 * Note we don't support write delegations, and won't until the vfs has
3794 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 */
3796static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003797nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3798 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799{
3800 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003801 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3802 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003803 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003804 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003806 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003807 open->op_recall = 0;
3808 switch (open->op_claim_type) {
3809 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003810 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003811 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003812 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3813 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003814 break;
3815 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003816 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003817 /*
3818 * Let's not give out any delegations till everyone's
3819 * had the chance to reclaim theirs....
3820 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003821 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003822 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003823 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003824 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003825 /*
3826 * Also, if the file was opened for write or
3827 * create, there's a good chance the client's
3828 * about to write to it, resulting in an
3829 * immediate recall (since we don't support
3830 * write delegations):
3831 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003832 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003833 goto out_no_deleg;
3834 if (open->op_create == NFS4_OPEN_CREATE)
3835 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003836 break;
3837 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003838 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003839 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003840 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003841 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003842 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003844 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003846 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003847 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003848 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003849 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003850 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003851out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003852 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3853 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003854 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003855 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003856 open->op_recall = 1;
3857 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003858
3859 /* 4.1 client asking for a delegation? */
3860 if (open->op_deleg_want)
3861 nfsd4_open_deleg_none_ext(open, status);
3862 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863}
3864
Benny Halevye27f49c2012-02-21 14:16:54 -08003865static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3866 struct nfs4_delegation *dp)
3867{
3868 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3869 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3870 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3871 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3872 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3873 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3874 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3875 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3876 }
3877 /* Otherwise the client must be confused wanting a delegation
3878 * it already has, therefore we don't return
3879 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3880 */
3881}
3882
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883/*
3884 * called with nfs4_lock_state() held.
3885 */
Al Virob37ad282006-10-19 23:28:59 -07003886__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3888{
Andy Adamson66689582009-04-03 08:28:45 +03003889 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003890 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003892 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003893 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003894 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 /*
3897 * Lookup file; if found, lookup stateid and check open request,
3898 * and check for delegations in the process of being recalled.
3899 * If not found, create the nfs4_file struct
3900 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003901 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04003902 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04003903 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003904 if (status)
3905 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003906 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003908 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003909 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003910 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003911 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003912 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 }
3914
3915 /*
3916 * OPEN the file, or upgrade an existing OPEN.
3917 * If truncate fails, the OPEN fails.
3918 */
3919 if (stp) {
3920 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003921 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 if (status)
3923 goto out;
3924 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003925 stp = open->op_stp;
3926 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003927 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003928 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3929 if (status) {
3930 release_open_stateid(stp);
3931 goto out;
3932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003934 update_stateid(&stp->st_stid.sc_stateid);
3935 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936
Benny Halevyd24433c2012-02-16 20:57:17 +02003937 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02003938 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3939 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3940 open->op_why_no_deleg = WND4_NOT_WANTED;
3941 goto nodeleg;
3942 }
3943 }
3944
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 /*
3946 * Attempt to hand out a delegation. No error return, because the
3947 * OPEN succeeds even if we fail.
3948 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003949 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003950nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 status = nfs_ok;
3952
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003953 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003954 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003956 /* 4.1 client trying to upgrade/downgrade delegation? */
3957 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003958 open->op_deleg_want)
3959 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003960
NeilBrown13cd2182005-06-23 22:03:10 -07003961 if (fp)
3962 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003963 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003964 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 /*
3966 * To finish the open response, we just need to set the rflags.
3967 */
3968 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003969 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003970 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003972 if (dp)
3973 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003974 if (stp)
3975 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976
3977 return status;
3978}
3979
Jeff Layton58fb12e2014-07-29 21:34:27 -04003980void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
3981 struct nfsd4_open *open, __be32 status)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003982{
3983 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04003984 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003985
Jeff Laytond3134b12014-07-29 21:34:32 -04003986 nfsd4_cstate_assign_replay(cstate, so);
3987 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003988 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003989 if (open->op_file)
3990 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003991 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04003992 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003993}
3994
Al Virob37ad282006-10-19 23:28:59 -07003995__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003996nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3997 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998{
3999 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004000 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004001 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002
4003 nfs4_lock_state();
4004 dprintk("process_renew(%08x/%08x): starting\n",
4005 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004006 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004007 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004009 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004011 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004012 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 goto out;
4014 status = nfs_ok;
4015out:
4016 nfs4_unlock_state();
4017 return status;
4018}
4019
NeilBrowna76b4312005-06-23 22:04:01 -07004020static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004021nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004022{
Jeff Layton33dcc482012-04-10 11:08:48 -04004023 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004024 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004025 return;
4026
NeilBrowna76b4312005-06-23 22:04:01 -07004027 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004028 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004029 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004030 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004031 /*
4032 * Now that every NFSv4 client has had the chance to recover and
4033 * to see the (possibly new, possibly shorter) lease time, we
4034 * can safely set the next grace time to the current lease time:
4035 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03004036 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07004037}
4038
NeilBrownfd39ca92005-06-23 22:04:03 -07004039static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004040nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041{
4042 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004043 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 struct nfs4_delegation *dp;
4045 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004046 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004047 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048
4049 nfs4_lock_state();
4050
4051 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004052 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004053 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004054 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004055 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 clp = list_entry(pos, struct nfs4_client, cl_lru);
4057 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4058 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004059 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 break;
4061 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004062 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004063 dprintk("NFSD: client in use (clientid %08x)\n",
4064 clp->cl_clientid.cl_id);
4065 continue;
4066 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004067 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004068 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004069 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004070 list_for_each_safe(pos, next, &reaplist) {
4071 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 dprintk("NFSD: purging unused client (clientid %08x)\n",
4073 clp->cl_clientid.cl_id);
4074 expire_client(clp);
4075 }
Benny Halevycdc97502014-05-30 09:09:30 -04004076 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004077 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004079 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4080 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004082 t = dp->dl_time - cutoff;
4083 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 break;
4085 }
Jeff Layton42690672014-07-25 07:34:20 -04004086 unhash_delegation_locked(dp);
4087 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 }
Benny Halevycdc97502014-05-30 09:09:30 -04004089 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004090 while (!list_empty(&reaplist)) {
4091 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4092 dl_recall_lru);
4093 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004094 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004096 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004097 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
4098 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004099 t = oo->oo_time - cutoff;
4100 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 break;
4102 }
Jeff Laytond3134b12014-07-29 21:34:32 -04004103 release_last_closed_stateid(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04004105 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04004107 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108}
4109
Harvey Harrisona254b242008-02-20 12:49:00 -08004110static struct workqueue_struct *laundry_wq;
4111static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004112
4113static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004114laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115{
4116 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004117 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4118 work);
4119 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4120 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004122 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004124 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125}
4126
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004127static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004128{
Trond Myklebust11b91642014-07-29 21:34:08 -04004129 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004130 return nfserr_bad_stateid;
4131 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132}
4133
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004135access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004137 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4138 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4139 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140}
4141
4142static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004143access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004145 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4146 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147}
4148
4149static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004150__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151{
Al Virob37ad282006-10-19 23:28:59 -07004152 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153
J. Bruce Fields02921912010-07-29 15:16:59 -04004154 /* For lock stateid's, we test the parent open, not the lock: */
4155 if (stp->st_openstp)
4156 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004157 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004159 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 goto out;
4161 status = nfs_ok;
4162out:
4163 return status;
4164}
4165
Al Virob37ad282006-10-19 23:28:59 -07004166static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004167check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004169 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004171 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004172 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 * conflicting state; so we must wait out the grace period. */
4174 return nfserr_grace;
4175 } else if (flags & WR_STATE)
4176 return nfs4_share_conflict(current_fh,
4177 NFS4_SHARE_DENY_WRITE);
4178 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4179 return nfs4_share_conflict(current_fh,
4180 NFS4_SHARE_DENY_READ);
4181}
4182
4183/*
4184 * Allow READ/WRITE during grace period on recovered state only for files
4185 * that are not able to provide mandatory locking.
4186 */
4187static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004188grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004190 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191}
4192
J. Bruce Fields81b82962011-08-23 11:03:29 -04004193/* Returns true iff a is later than b: */
4194static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4195{
Jim Rees1a9357f2013-05-17 17:33:00 -04004196 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004197}
4198
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004199static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004200{
Andy Adamson66689582009-04-03 08:28:45 +03004201 /*
4202 * When sessions are used the stateid generation number is ignored
4203 * when it is zero.
4204 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004205 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004206 return nfs_ok;
4207
4208 if (in->si_generation == ref->si_generation)
4209 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004210
J. Bruce Fields0836f582008-01-26 19:08:12 -05004211 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004212 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004213 return nfserr_bad_stateid;
4214 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004215 * However, we could see a stateid from the past, even from a
4216 * non-buggy client. For example, if the client sends a lock
4217 * while some IO is outstanding, the lock may bump si_generation
4218 * while the IO is still in flight. The client could avoid that
4219 * situation by waiting for responses on all the IO requests,
4220 * but better performance may result in retrying IO that
4221 * receives an old_stateid error if requests are rarely
4222 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004223 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004224 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004225}
4226
Chuck Lever7df302f2012-05-29 13:56:37 -04004227static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004228{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004229 struct nfs4_stid *s;
4230 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004231 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004232
Chuck Lever7df302f2012-05-29 13:56:37 -04004233 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004234 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004235 /* Client debugging aid. */
4236 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4237 char addr_str[INET6_ADDRSTRLEN];
4238 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4239 sizeof(addr_str));
4240 pr_warn_ratelimited("NFSD: client %s testing state ID "
4241 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004242 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004243 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004244 spin_lock(&cl->cl_lock);
4245 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004246 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004247 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004248 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004249 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004250 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004251 switch (s->sc_type) {
4252 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004253 status = nfs_ok;
4254 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004255 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004256 status = nfserr_deleg_revoked;
4257 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004258 case NFS4_OPEN_STID:
4259 case NFS4_LOCK_STID:
4260 ols = openlockstateid(s);
4261 if (ols->st_stateowner->so_is_open_owner
4262 && !(openowner(ols->st_stateowner)->oo_flags
4263 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004264 status = nfserr_bad_stateid;
4265 else
4266 status = nfs_ok;
4267 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004268 default:
4269 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004270 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004271 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004272 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004273 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004274 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004275out_unlock:
4276 spin_unlock(&cl->cl_lock);
4277 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004278}
4279
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004280static __be32
4281nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4282 stateid_t *stateid, unsigned char typemask,
4283 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004284{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004285 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004286
4287 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4288 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004289 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004290 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004291 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004292 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004293 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004294 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004295 if (status)
4296 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004297 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004298 if (!*s)
4299 return nfserr_bad_stateid;
4300 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004301}
4302
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303/*
4304* Checks for stateid operations
4305*/
Al Virob37ad282006-10-19 23:28:59 -07004306__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004307nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004308 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004310 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004311 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004313 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004315 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004316 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004317 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 if (filpp)
4320 *filpp = NULL;
4321
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004322 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 return nfserr_grace;
4324
4325 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004326 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
Trond Myklebust14bcab12014-04-18 14:44:07 -04004328 nfs4_lock_state();
4329
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004330 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004331 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004332 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004333 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004334 goto unlock_state;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004335 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4336 if (status)
4337 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004338 switch (s->sc_type) {
4339 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004340 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004341 status = nfs4_check_delegmode(dp, flags);
4342 if (status)
4343 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004344 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004345 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004346 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004347 WARN_ON_ONCE(1);
4348 status = nfserr_serverfault;
4349 goto out;
4350 }
Trond Myklebustde186432014-07-10 14:07:26 -04004351 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004352 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004353 break;
4354 case NFS4_OPEN_STID:
4355 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004356 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004357 status = nfs4_check_fh(current_fh, stp);
4358 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004360 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004361 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004363 status = nfs4_check_openmode(stp, flags);
4364 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004366 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004367 struct nfs4_file *fp = stp->st_stid.sc_file;
4368
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004369 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004370 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004371 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004372 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004373 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004374 break;
4375 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004376 status = nfserr_bad_stateid;
4377 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 }
4379 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004380 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004381 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004383 nfs4_put_stid(s);
4384unlock_state:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004385 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 return status;
4387}
4388
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004389static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004390nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004391{
J. Bruce Fieldsa1b8ff42014-05-20 15:55:21 -04004392 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4393
Trond Myklebust11b91642014-07-29 21:34:08 -04004394 if (check_for_locks(stp->st_stid.sc_file, lo))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004395 return nfserr_locks_held;
Jeff Layton5adfd882014-07-29 21:34:31 -04004396 release_lock_stateid(stp);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004397 return nfs_ok;
4398}
4399
4400/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004401 * Test if the stateid is valid
4402 */
4403__be32
4404nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4405 struct nfsd4_test_stateid *test_stateid)
4406{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004407 struct nfsd4_test_stateid_id *stateid;
4408 struct nfs4_client *cl = cstate->session->se_client;
4409
4410 nfs4_lock_state();
4411 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004412 stateid->ts_id_status =
4413 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004414 nfs4_unlock_state();
4415
Bryan Schumaker17456802011-07-13 10:50:48 -04004416 return nfs_ok;
4417}
4418
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004419__be32
4420nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4421 struct nfsd4_free_stateid *free_stateid)
4422{
4423 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004424 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004425 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004426 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004427 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004428
4429 nfs4_lock_state();
Jeff Layton1af71cc2014-07-29 21:34:14 -04004430 spin_lock(&cl->cl_lock);
4431 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004432 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004433 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004434 switch (s->sc_type) {
4435 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004436 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004437 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004438 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004439 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4440 if (ret)
4441 break;
4442 ret = nfserr_locks_held;
4443 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004444 case NFS4_LOCK_STID:
4445 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4446 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004447 break;
4448 spin_unlock(&cl->cl_lock);
4449 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4450 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004451 case NFS4_REVOKED_DELEG_STID:
4452 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004453 list_del_init(&dp->dl_recall_lru);
4454 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004455 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004456 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004457 goto out;
4458 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004459 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004460out_unlock:
4461 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004462out:
4463 nfs4_unlock_state();
4464 return ret;
4465}
4466
NeilBrown4c4cd222005-07-07 17:59:27 -07004467static inline int
4468setlkflg (int type)
4469{
4470 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4471 RD_STATE : WR_STATE;
4472}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004474static __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 -04004475{
4476 struct svc_fh *current_fh = &cstate->current_fh;
4477 struct nfs4_stateowner *sop = stp->st_stateowner;
4478 __be32 status;
4479
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004480 status = nfsd4_check_seqid(cstate, sop, seqid);
4481 if (status)
4482 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004483 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4484 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004485 /*
4486 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004487 * nfserr_replay_me from the previous step, and
4488 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004489 */
4490 return nfserr_bad_stateid;
4491 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4492 if (status)
4493 return status;
4494 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004495}
4496
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497/*
4498 * Checks for sequence id mutating operations.
4499 */
Al Virob37ad282006-10-19 23:28:59 -07004500static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004501nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004502 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004503 struct nfs4_ol_stateid **stpp,
4504 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004506 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004507 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004508 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004510 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4511 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004512
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004514 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004515 if (status)
4516 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004517 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004518 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004520 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004521 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004522 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004523 else
4524 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004525 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004526}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004527
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004528static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4529 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004530{
4531 __be32 status;
4532 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004533 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004535 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004536 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004537 if (status)
4538 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004539 oo = openowner(stp->st_stateowner);
4540 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4541 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004542 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004543 }
4544 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004545 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546}
4547
Al Virob37ad282006-10-19 23:28:59 -07004548__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004549nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004550 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551{
Al Virob37ad282006-10-19 23:28:59 -07004552 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004553 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004554 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004555 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556
Al Viroa6a9f182013-09-16 10:57:01 -04004557 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4558 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004560 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004561 if (status)
4562 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563
4564 nfs4_lock_state();
4565
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004566 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004567 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004568 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004569 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004570 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004571 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004572 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004573 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004574 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004575 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004576 update_stateid(&stp->st_stid.sc_stateid);
4577 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004578 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004579 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004580
Jeff Layton2a4317c2012-03-21 16:42:43 -04004581 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004582 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004583put_stateid:
4584 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004586 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004587 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 return status;
4589}
4590
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004591static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004593 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004594 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004595 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004596 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004597}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004598
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004599static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4600{
4601 switch (to_access) {
4602 case NFS4_SHARE_ACCESS_READ:
4603 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4604 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4605 break;
4606 case NFS4_SHARE_ACCESS_WRITE:
4607 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4608 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4609 break;
4610 case NFS4_SHARE_ACCESS_BOTH:
4611 break;
4612 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004613 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 }
4615}
4616
Al Virob37ad282006-10-19 23:28:59 -07004617__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004618nfsd4_open_downgrade(struct svc_rqst *rqstp,
4619 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004620 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621{
Al Virob37ad282006-10-19 23:28:59 -07004622 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004623 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004624 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625
Al Viroa6a9f182013-09-16 10:57:01 -04004626 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4627 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004629 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004630 if (od->od_deleg_want)
4631 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4632 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633
4634 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004635 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004636 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004637 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004640 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004641 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004643 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004645 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004646 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004648 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004650 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651
Jeff Laytonce0fc432012-05-11 09:45:14 -04004652 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004654 update_stateid(&stp->st_stid.sc_stateid);
4655 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004657put_stateid:
4658 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004660 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004661 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 return status;
4663}
4664
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004665static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4666{
Trond Myklebustacf92952014-06-30 11:48:37 -04004667 struct nfs4_client *clp = s->st_stid.sc_client;
Trond Myklebustacf92952014-06-30 11:48:37 -04004668
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004669 s->st_stid.sc_type = NFS4_CLOSED_STID;
Trond Myklebustacf92952014-06-30 11:48:37 -04004670 unhash_open_stateid(s);
4671
Jeff Laytond3134b12014-07-29 21:34:32 -04004672 if (clp->cl_minorversion)
Trond Myklebust60116952014-07-29 21:34:06 -04004673 nfs4_put_stid(&s->st_stid);
Jeff Laytond3134b12014-07-29 21:34:32 -04004674 else
4675 move_to_close_lru(s, clp->net);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004676}
4677
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678/*
4679 * nfs4_unlock_state() called after encode
4680 */
Al Virob37ad282006-10-19 23:28:59 -07004681__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004682nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004683 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684{
Al Virob37ad282006-10-19 23:28:59 -07004685 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004686 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004687 struct net *net = SVC_NET(rqstp);
4688 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689
Al Viroa6a9f182013-09-16 10:57:01 -04004690 dprintk("NFSD: nfsd4_close on file %pd\n",
4691 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692
4693 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004694 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4695 &close->cl_stateid,
4696 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004697 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004698 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004699 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004701 update_stateid(&stp->st_stid.sc_stateid);
4702 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004704 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004705
4706 /* put reference from nfs4_preprocess_seqid_op */
4707 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708out:
Jeff Layton58fb12e2014-07-29 21:34:27 -04004709 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 return status;
4711}
4712
Al Virob37ad282006-10-19 23:28:59 -07004713__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004714nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4715 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004717 struct nfs4_delegation *dp;
4718 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004719 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004720 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004721 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004723 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004724 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725
4726 nfs4_lock_state();
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004727 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004728 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004729 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004730 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004731 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004732 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004733 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004734
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004735 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004736put_stateid:
4737 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004739 nfs4_unlock_state();
4740
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 return status;
4742}
4743
4744
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746
Benny Halevy87df4de2008-12-15 19:42:03 +02004747static inline u64
4748end_offset(u64 start, u64 len)
4749{
4750 u64 end;
4751
4752 end = start + len;
4753 return end >= start ? end: NFS4_MAX_UINT64;
4754}
4755
4756/* last octet in a range */
4757static inline u64
4758last_byte_offset(u64 start, u64 len)
4759{
4760 u64 end;
4761
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004762 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004763 end = start + len;
4764 return end > start ? end - 1: NFS4_MAX_UINT64;
4765}
4766
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767/*
4768 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4769 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4770 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4771 * locking, this prevents us from being completely protocol-compliant. The
4772 * real solution to this problem is to start using unsigned file offsets in
4773 * the VFS, but this is a very deep change!
4774 */
4775static inline void
4776nfs4_transform_lock_offset(struct file_lock *lock)
4777{
4778 if (lock->fl_start < 0)
4779 lock->fl_start = OFFSET_MAX;
4780 if (lock->fl_end < 0)
4781 lock->fl_end = OFFSET_MAX;
4782}
4783
NeilBrownd5b90262006-04-10 22:55:22 -07004784/* Hack!: For now, we're defining this just so we can use a pointer to it
4785 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004786static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004787};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788
4789static inline void
4790nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4791{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004792 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
NeilBrownd5b90262006-04-10 22:55:22 -07004794 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004795 lo = (struct nfs4_lockowner *) fl->fl_owner;
4796 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4797 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004798 if (!deny->ld_owner.data)
4799 /* We just don't care that much */
4800 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004801 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4802 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004803 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004804nevermind:
4805 deny->ld_owner.len = 0;
4806 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004807 deny->ld_clientid.cl_boot = 0;
4808 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 }
4810 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004811 deny->ld_length = NFS4_MAX_UINT64;
4812 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4814 deny->ld_type = NFS4_READ_LT;
4815 if (fl->fl_type != F_RDLCK)
4816 deny->ld_type = NFS4_WRITE_LT;
4817}
4818
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004819static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004820find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004821 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822{
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004823 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4824 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004826 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4827 if (so->so_is_open_owner)
4828 continue;
4829 if (!same_owner_str(so, owner, clid))
4830 continue;
Jeff Layton5db1c032014-07-29 21:34:28 -04004831 atomic_inc(&so->so_count);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004832 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 }
4834 return NULL;
4835}
4836
Trond Myklebustc58c6612014-07-29 21:34:35 -04004837static struct nfs4_lockowner *
4838find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4839 struct nfsd_net *nn)
4840{
4841 struct nfs4_lockowner *lo;
4842
4843 spin_lock(&nn->client_lock);
4844 lo = find_lockowner_str_locked(clid, owner, nn);
4845 spin_unlock(&nn->client_lock);
4846 return lo;
4847}
4848
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004849static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4850{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004851 struct nfsd_net *nn = net_generic(sop->so_client->net, nfsd_net_id);
4852
4853 spin_lock(&nn->client_lock);
4854 unhash_lockowner_locked(lockowner(sop));
4855 spin_unlock(&nn->client_lock);
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004856}
4857
Jeff Layton6b180f02014-07-29 21:34:26 -04004858static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4859{
4860 struct nfs4_lockowner *lo = lockowner(sop);
4861
4862 kmem_cache_free(lockowner_slab, lo);
4863}
4864
4865static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004866 .so_unhash = nfs4_unhash_lockowner,
4867 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004868};
4869
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870/*
4871 * Alloc a lock owner structure.
4872 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004873 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004875 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004877static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004878alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
4879 struct nfs4_ol_stateid *open_stp,
4880 struct nfsd4_lock *lock)
4881{
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004882 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004883 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004885 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4886 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004888 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4889 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04004890 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04004891 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustc58c6612014-07-29 21:34:35 -04004892 spin_lock(&nn->client_lock);
4893 ret = find_lockowner_str_locked(&clp->cl_clientid,
4894 &lock->lk_new_owner, nn);
4895 if (ret == NULL) {
4896 list_add(&lo->lo_owner.so_strhash,
4897 &nn->ownerstr_hashtbl[strhashval]);
4898 ret = lo;
4899 } else
4900 nfs4_free_lockowner(&lo->lo_owner);
4901 spin_unlock(&nn->client_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004902 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903}
4904
Jeff Layton356a95e2014-07-29 21:34:13 -04004905static void
4906init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
4907 struct nfs4_file *fp, struct inode *inode,
4908 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004910 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911
Jeff Layton356a95e2014-07-29 21:34:13 -04004912 lockdep_assert_held(&clp->cl_lock);
4913
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004914 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004915 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004916 stp->st_stateowner = &lo->lo_owner;
Trond Myklebuste4f1dd72014-07-29 21:34:30 -04004917 atomic_inc(&lo->lo_owner.so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07004918 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004919 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04004920 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004921 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004923 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004924 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004925 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004926 spin_lock(&fp->fi_lock);
4927 list_add(&stp->st_perfile, &fp->fi_stateids);
4928 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929}
4930
Jeff Laytonc53530d2014-06-30 11:48:39 -04004931static struct nfs4_ol_stateid *
4932find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4933{
4934 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04004935 struct nfs4_client *clp = lo->lo_owner.so_client;
4936
4937 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004938
4939 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004940 if (lst->st_stid.sc_file == fp) {
4941 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004942 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04004943 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004944 }
4945 return NULL;
4946}
4947
Jeff Layton356a95e2014-07-29 21:34:13 -04004948static struct nfs4_ol_stateid *
4949find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
4950 struct inode *inode, struct nfs4_ol_stateid *ost,
4951 bool *new)
4952{
4953 struct nfs4_stid *ns = NULL;
4954 struct nfs4_ol_stateid *lst;
4955 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4956 struct nfs4_client *clp = oo->oo_owner.so_client;
4957
4958 spin_lock(&clp->cl_lock);
4959 lst = find_lock_stateid(lo, fi);
4960 if (lst == NULL) {
4961 spin_unlock(&clp->cl_lock);
4962 ns = nfs4_alloc_stid(clp, stateid_slab);
4963 if (ns == NULL)
4964 return NULL;
4965
4966 spin_lock(&clp->cl_lock);
4967 lst = find_lock_stateid(lo, fi);
4968 if (likely(!lst)) {
4969 lst = openlockstateid(ns);
4970 init_lock_stateid(lst, lo, fi, inode, ost);
4971 ns = NULL;
4972 *new = true;
4973 }
4974 }
4975 spin_unlock(&clp->cl_lock);
4976 if (ns)
4977 nfs4_put_stid(ns);
4978 return lst;
4979}
Jeff Laytonc53530d2014-06-30 11:48:39 -04004980
NeilBrownfd39ca92005-06-23 22:04:03 -07004981static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982check_lock_length(u64 offset, u64 length)
4983{
Benny Halevy87df4de2008-12-15 19:42:03 +02004984 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 LOFF_OVERFLOW(offset, length)));
4986}
4987
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004988static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004989{
Trond Myklebust11b91642014-07-29 21:34:08 -04004990 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004991
Jeff Layton7214e862014-07-10 14:07:33 -04004992 lockdep_assert_held(&fp->fi_lock);
4993
Jeff Layton82c5ff12012-05-11 09:45:13 -04004994 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004995 return;
Jeff Layton12659652014-07-10 14:07:28 -04004996 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004997 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004998}
4999
Jeff Layton356a95e2014-07-29 21:34:13 -04005000static __be32
5001lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5002 struct nfs4_ol_stateid *ost,
5003 struct nfsd4_lock *lock,
5004 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005005{
Jeff Layton5db1c032014-07-29 21:34:28 -04005006 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005007 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005008 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5009 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005010 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005011 struct nfs4_lockowner *lo;
5012 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03005013 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005014
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005015 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005016 if (!lo) {
5017 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
5018 &lock->v.new.owner);
5019 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5020 if (lo == NULL)
5021 return nfserr_jukebox;
5022 } else {
5023 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005024 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005025 if (!cstate->minorversion &&
5026 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005027 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005028 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005029
Jeff Layton356a95e2014-07-29 21:34:13 -04005030 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005031 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005032 status = nfserr_jukebox;
5033 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005034 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005035 status = nfs_ok;
5036out:
5037 nfs4_put_stateowner(&lo->lo_owner);
5038 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005039}
5040
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041/*
5042 * LOCK operation
5043 */
Al Virob37ad282006-10-19 23:28:59 -07005044__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005045nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005046 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005048 struct nfs4_openowner *open_sop = NULL;
5049 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005050 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005051 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005052 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005053 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005054 struct file_lock *file_lock = NULL;
5055 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005056 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005057 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005058 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005059 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005060 struct net *net = SVC_NET(rqstp);
5061 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062
5063 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5064 (long long) lock->lk_offset,
5065 (long long) lock->lk_length);
5066
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 if (check_lock_length(lock->lk_offset, lock->lk_length))
5068 return nfserr_inval;
5069
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005070 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005071 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005072 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5073 return status;
5074 }
5075
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 nfs4_lock_state();
5077
5078 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005079 if (nfsd4_has_session(cstate))
5080 /* See rfc 5661 18.10.3: given clientid is ignored: */
5081 memcpy(&lock->v.new.clientid,
5082 &cstate->session->se_client->cl_clientid,
5083 sizeof(clientid_t));
5084
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005086 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005090 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 lock->lk_new_open_seqid,
5092 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005093 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005094 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005096 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005097 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005098 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005099 &lock->v.new.clientid))
5100 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005101 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005102 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005103 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005104 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005105 lock->lk_old_lock_seqid,
5106 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005107 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005108 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005109 if (status)
5110 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005111 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005113 lkflg = setlkflg(lock->lk_type);
5114 status = nfs4_check_openmode(lock_stp, lkflg);
5115 if (status)
5116 goto out;
5117
NeilBrown0dd395d2005-07-07 17:59:15 -07005118 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005119 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005120 goto out;
5121 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005122 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005123 goto out;
5124
Jeff Layton21179d82012-08-21 08:03:32 -04005125 file_lock = locks_alloc_lock();
5126 if (!file_lock) {
5127 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5128 status = nfserr_jukebox;
5129 goto out;
5130 }
5131
Trond Myklebust11b91642014-07-29 21:34:08 -04005132 fp = lock_stp->st_stid.sc_file;
Jeff Layton21179d82012-08-21 08:03:32 -04005133 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 switch (lock->lk_type) {
5135 case NFS4_READ_LT:
5136 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005137 spin_lock(&fp->fi_lock);
5138 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005139 if (filp)
5140 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005141 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005142 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005143 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 case NFS4_WRITE_LT:
5145 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005146 spin_lock(&fp->fi_lock);
5147 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005148 if (filp)
5149 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005150 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005151 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005152 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 default:
5154 status = nfserr_inval;
5155 goto out;
5156 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005157 if (!filp) {
5158 status = nfserr_openmode;
5159 goto out;
5160 }
Jeff Layton21179d82012-08-21 08:03:32 -04005161 file_lock->fl_owner = (fl_owner_t)lock_sop;
5162 file_lock->fl_pid = current->tgid;
5163 file_lock->fl_file = filp;
5164 file_lock->fl_flags = FL_POSIX;
5165 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5166 file_lock->fl_start = lock->lk_offset;
5167 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5168 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169
Jeff Layton21179d82012-08-21 08:03:32 -04005170 conflock = locks_alloc_lock();
5171 if (!conflock) {
5172 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5173 status = nfserr_jukebox;
5174 goto out;
5175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176
Jeff Layton21179d82012-08-21 08:03:32 -04005177 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005178 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005180 update_stateid(&lock_stp->st_stid.sc_stateid);
5181 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005183 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005184 break;
5185 case (EAGAIN): /* conflock holds conflicting lock */
5186 status = nfserr_denied;
5187 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005188 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005189 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 case (EDEADLK):
5191 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005192 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005193 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005194 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005195 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005196 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198out:
Trond Myklebustde186432014-07-10 14:07:26 -04005199 if (filp)
5200 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005201 if (lock_stp) {
5202 /* Bump seqid manually if the 4.0 replay owner is openowner */
5203 if (cstate->replay_owner &&
5204 cstate->replay_owner != &lock_sop->lo_owner &&
5205 seqid_mutating_err(ntohl(status)))
5206 lock_sop->lo_owner.so_seqid++;
5207
5208 /*
5209 * If this is a new, never-before-used stateid, and we are
5210 * returning an error, then just go ahead and release it.
5211 */
5212 if (status && new)
5213 release_lock_stateid(lock_stp);
5214
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005215 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005216 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005217 if (open_stp)
5218 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005219 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005220 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005221 if (file_lock)
5222 locks_free_lock(file_lock);
5223 if (conflock)
5224 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 return status;
5226}
5227
5228/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005229 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5230 * so we do a temporary open here just to get an open file to pass to
5231 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5232 * inode operation.)
5233 */
Al Viro04da6e92012-04-13 00:00:04 -04005234static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005235{
5236 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005237 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5238 if (!err) {
5239 err = nfserrno(vfs_test_lock(file, lock));
5240 nfsd_close(file);
5241 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005242 return err;
5243}
5244
5245/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 * LOCKT operation
5247 */
Al Virob37ad282006-10-19 23:28:59 -07005248__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005249nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5250 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251{
Jeff Layton21179d82012-08-21 08:03:32 -04005252 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005253 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005254 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005255 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005257 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 return nfserr_grace;
5259
5260 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5261 return nfserr_inval;
5262
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 nfs4_lock_state();
5264
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005265 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005266 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005267 if (status)
5268 goto out;
5269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005271 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273
Jeff Layton21179d82012-08-21 08:03:32 -04005274 file_lock = locks_alloc_lock();
5275 if (!file_lock) {
5276 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5277 status = nfserr_jukebox;
5278 goto out;
5279 }
5280 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 switch (lockt->lt_type) {
5282 case NFS4_READ_LT:
5283 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005284 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 break;
5286 case NFS4_WRITE_LT:
5287 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005288 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 break;
5290 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005291 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 status = nfserr_inval;
5293 goto out;
5294 }
5295
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005296 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005297 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005298 file_lock->fl_owner = (fl_owner_t)lo;
5299 file_lock->fl_pid = current->tgid;
5300 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301
Jeff Layton21179d82012-08-21 08:03:32 -04005302 file_lock->fl_start = lockt->lt_offset;
5303 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304
Jeff Layton21179d82012-08-21 08:03:32 -04005305 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306
Jeff Layton21179d82012-08-21 08:03:32 -04005307 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005308 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005309 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005310
Jeff Layton21179d82012-08-21 08:03:32 -04005311 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005313 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 }
5315out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005316 if (lo)
5317 nfs4_put_stateowner(&lo->lo_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005319 if (file_lock)
5320 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 return status;
5322}
5323
Al Virob37ad282006-10-19 23:28:59 -07005324__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005325nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005326 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005328 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005330 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005331 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005332 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005333 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5334
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5336 (long long) locku->lu_offset,
5337 (long long) locku->lu_length);
5338
5339 if (check_lock_length(locku->lu_offset, locku->lu_length))
5340 return nfserr_inval;
5341
5342 nfs4_lock_state();
5343
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005344 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005345 &locku->lu_stateid, NFS4_LOCK_STID,
5346 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005347 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005349 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005350 if (!filp) {
5351 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005352 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005353 }
Jeff Layton21179d82012-08-21 08:03:32 -04005354 file_lock = locks_alloc_lock();
5355 if (!file_lock) {
5356 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5357 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005358 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005359 }
5360 locks_init_lock(file_lock);
5361 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005362 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005363 file_lock->fl_pid = current->tgid;
5364 file_lock->fl_file = filp;
5365 file_lock->fl_flags = FL_POSIX;
5366 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5367 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368
Jeff Layton21179d82012-08-21 08:03:32 -04005369 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5370 locku->lu_length);
5371 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372
Jeff Layton21179d82012-08-21 08:03:32 -04005373 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005374 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005375 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 goto out_nfserr;
5377 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005378 update_stateid(&stp->st_stid.sc_stateid);
5379 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005380fput:
5381 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005382put_stateid:
5383 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005385 nfsd4_bump_seqid(cstate, status);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005386 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04005387 if (file_lock)
5388 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 return status;
5390
5391out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005392 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005393 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394}
5395
5396/*
5397 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005398 * true: locks held by lockowner
5399 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005401static bool
5402check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403{
5404 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005405 int status = false;
5406 struct file *filp = find_any_file(fp);
5407 struct inode *inode;
5408
5409 if (!filp) {
5410 /* Any valid lock stateid should have some sort of access */
5411 WARN_ON_ONCE(1);
5412 return status;
5413 }
5414
5415 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416
Jeff Layton1c8c6012013-06-21 08:58:15 -04005417 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005419 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005420 status = true;
5421 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005424 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005425 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 return status;
5427}
5428
Al Virob37ad282006-10-19 23:28:59 -07005429__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005430nfsd4_release_lockowner(struct svc_rqst *rqstp,
5431 struct nfsd4_compound_state *cstate,
5432 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433{
5434 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Laytonfd449072014-06-30 11:48:41 -04005435 struct nfs4_stateowner *sop = NULL, *tmp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005436 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005437 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 struct xdr_netobj *owner = &rlockowner->rl_owner;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005439 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07005440 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005441 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005442 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443
5444 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5445 clid->cl_boot, clid->cl_id);
5446
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 nfs4_lock_state();
5448
Jeff Layton4b24ca72014-06-30 11:48:44 -04005449 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005450 if (status)
5451 goto out;
5452
NeilBrown3e9e3db2005-06-23 22:04:20 -07005453 status = nfserr_locks_held;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05005454
Jeff Laytonfd449072014-06-30 11:48:41 -04005455 /* Find the matching lock stateowner */
Trond Myklebustc58c6612014-07-29 21:34:35 -04005456 spin_lock(&nn->client_lock);
Jeff Laytonfd449072014-06-30 11:48:41 -04005457 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
5458 if (tmp->so_is_open_owner)
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005459 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005460 if (same_owner_str(tmp, owner, clid)) {
5461 sop = tmp;
Jeff Layton5adfd882014-07-29 21:34:31 -04005462 atomic_inc(&sop->so_count);
Jeff Laytonfd449072014-06-30 11:48:41 -04005463 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07005465 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005466 spin_unlock(&nn->client_lock);
Jeff Laytonfd449072014-06-30 11:48:41 -04005467
5468 /* No matching owner found, maybe a replay? Just declare victory... */
5469 if (!sop) {
5470 status = nfs_ok;
5471 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005473
5474 lo = lockowner(sop);
5475 /* see if there are still any locks associated with it */
Trond Myklebustc58c6612014-07-29 21:34:35 -04005476 clp = cstate->clp;
5477 spin_lock(&clp->cl_lock);
Jeff Laytonfd449072014-06-30 11:48:41 -04005478 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
Jeff Layton5adfd882014-07-29 21:34:31 -04005479 if (check_for_locks(stp->st_stid.sc_file, lo)) {
Trond Myklebustc58c6612014-07-29 21:34:35 -04005480 spin_unlock(&clp->cl_lock);
Jeff Laytonfd449072014-06-30 11:48:41 -04005481 goto out;
Jeff Layton5adfd882014-07-29 21:34:31 -04005482 }
Jeff Laytonfd449072014-06-30 11:48:41 -04005483 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005484 spin_unlock(&clp->cl_lock);
Jeff Laytonfd449072014-06-30 11:48:41 -04005485
5486 status = nfs_ok;
Trond Myklebustc58c6612014-07-29 21:34:35 -04005487 sop = NULL;
Jeff Laytonfd449072014-06-30 11:48:41 -04005488 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489out:
Trond Myklebustc58c6612014-07-29 21:34:35 -04005490 if (sop)
5491 nfs4_put_stateowner(sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 nfs4_unlock_state();
5493 return status;
5494}
5495
5496static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005497alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498{
NeilBrowna55370a2005-06-23 22:03:52 -07005499 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500}
5501
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005502bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005503nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005504{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005505 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005506
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005507 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005508 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005509}
5510
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511/*
5512 * failure => all reset bets are off, nfserr_no_grace...
5513 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005514struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005515nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516{
5517 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005518 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519
NeilBrowna55370a2005-06-23 22:03:52 -07005520 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5521 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005522 if (crp) {
5523 strhashval = clientstr_hashval(name);
5524 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005525 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005526 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005527 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005528 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005529 }
5530 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531}
5532
Jeff Layton2a4317c2012-03-21 16:42:43 -04005533void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005534nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005535{
5536 list_del(&crp->cr_strhash);
5537 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005538 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005539}
5540
5541void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005542nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543{
5544 struct nfs4_client_reclaim *crp = NULL;
5545 int i;
5546
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005548 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5549 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005551 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 }
5553 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005554 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555}
5556
5557/*
5558 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005559struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005560nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561{
5562 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 struct nfs4_client_reclaim *crp = NULL;
5564
Jeff Layton278c9312012-11-12 15:00:52 -05005565 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
Jeff Layton278c9312012-11-12 15:00:52 -05005567 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005568 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005569 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 return crp;
5571 }
5572 }
5573 return NULL;
5574}
5575
5576/*
5577* Called from OPEN. Look for clientid in reclaim list.
5578*/
Al Virob37ad282006-10-19 23:28:59 -07005579__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005580nfs4_check_open_reclaim(clientid_t *clid,
5581 struct nfsd4_compound_state *cstate,
5582 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005584 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005585
5586 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005587 status = lookup_clientid(clid, cstate, nn);
5588 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005589 return nfserr_reclaim_bad;
5590
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005591 if (nfsd4_client_record_check(cstate->clp))
5592 return nfserr_reclaim_bad;
5593
5594 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595}
5596
Bryan Schumaker65178db2011-11-01 13:35:21 -04005597#ifdef CONFIG_NFSD_FAULT_INJECTION
5598
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005599u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5600{
J. Bruce Fields221a6872013-04-01 22:23:49 -04005601 if (mark_client_expired(clp))
5602 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005603 expire_client(clp);
5604 return 1;
5605}
5606
Bryan Schumaker184c1842012-11-29 11:40:44 -05005607u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5608{
5609 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005610 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005611 printk(KERN_INFO "NFS Client: %s\n", buf);
5612 return 1;
5613}
5614
5615static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5616 const char *type)
5617{
5618 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005619 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005620 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5621}
5622
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005623static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5624 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005625{
5626 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005627 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005628 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005629 u64 count = 0;
5630
5631 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005632 list_for_each_entry_safe(stp, st_next,
5633 &oop->oo_owner.so_stateids, st_perstateowner) {
5634 list_for_each_entry_safe(lst, lst_next,
5635 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05005636 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005637 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005638 if (++count == max)
5639 return count;
5640 }
5641 }
5642 }
5643
5644 return count;
5645}
5646
5647u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5648{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005649 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005650}
5651
Bryan Schumaker184c1842012-11-29 11:40:44 -05005652u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5653{
5654 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5655 nfsd_print_count(clp, count, "locked files");
5656 return count;
5657}
5658
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005659static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5660{
5661 struct nfs4_openowner *oop, *next;
5662 u64 count = 0;
5663
5664 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5665 if (func)
5666 func(oop);
5667 if (++count == max)
5668 break;
5669 }
5670
5671 return count;
5672}
5673
5674u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5675{
5676 return nfsd_foreach_client_open(clp, max, release_openowner);
5677}
5678
Bryan Schumaker184c1842012-11-29 11:40:44 -05005679u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5680{
5681 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5682 nfsd_print_count(clp, count, "open files");
5683 return count;
5684}
5685
Bryan Schumaker269de302012-11-29 11:40:42 -05005686static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5687 struct list_head *victims)
5688{
5689 struct nfs4_delegation *dp, *next;
5690 u64 count = 0;
5691
Benny Halevycdc97502014-05-30 09:09:30 -04005692 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005693 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04005694 if (victims) {
5695 /*
5696 * It's not safe to mess with delegations that have a
5697 * non-zero dl_time. They might have already been broken
5698 * and could be processed by the laundromat outside of
5699 * the state_lock. Just leave them be.
5700 */
5701 if (dp->dl_time != 0)
5702 continue;
5703
Jeff Layton42690672014-07-25 07:34:20 -04005704 unhash_delegation_locked(dp);
5705 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005706 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005707 if (++count == max)
5708 break;
5709 }
5710 return count;
5711}
5712
5713u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5714{
5715 struct nfs4_delegation *dp, *next;
5716 LIST_HEAD(victims);
5717 u64 count;
5718
Benny Halevycdc97502014-05-30 09:09:30 -04005719 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005720 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005721 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005722
Jeff Layton2d4a5322014-07-25 07:34:21 -04005723 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5724 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005725 revoke_delegation(dp);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005726 }
Bryan Schumaker269de302012-11-29 11:40:42 -05005727
5728 return count;
5729}
5730
5731u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5732{
Jeff Laytondff13992014-07-08 14:02:49 -04005733 struct nfs4_delegation *dp;
Bryan Schumaker269de302012-11-29 11:40:42 -05005734 LIST_HEAD(victims);
5735 u64 count;
5736
Benny Halevycdc97502014-05-30 09:09:30 -04005737 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005738 count = nfsd_find_all_delegations(clp, max, &victims);
Jeff Laytondff13992014-07-08 14:02:49 -04005739 while (!list_empty(&victims)) {
5740 dp = list_first_entry(&victims, struct nfs4_delegation,
5741 dl_recall_lru);
5742 list_del_init(&dp->dl_recall_lru);
5743 dp->dl_time = 0;
Bryan Schumaker269de302012-11-29 11:40:42 -05005744 nfsd_break_one_deleg(dp);
Jeff Laytondff13992014-07-08 14:02:49 -04005745 }
Benny Halevycdc97502014-05-30 09:09:30 -04005746 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005747
5748 return count;
5749}
5750
Bryan Schumaker184c1842012-11-29 11:40:44 -05005751u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5752{
5753 u64 count = 0;
5754
Benny Halevycdc97502014-05-30 09:09:30 -04005755 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005756 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005757 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005758
5759 nfsd_print_count(clp, count, "delegations");
5760 return count;
5761}
5762
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005763u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005764{
5765 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005766 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005767 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005768
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005769 if (!nfsd_netns_ready(nn))
5770 return 0;
5771
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005772 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005773 count += func(clp, max - count);
5774 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005775 break;
5776 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005777
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005778 return count;
5779}
5780
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005781struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5782{
5783 struct nfs4_client *clp;
5784 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5785
5786 if (!nfsd_netns_ready(nn))
5787 return NULL;
5788
5789 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5790 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5791 return clp;
5792 }
5793 return NULL;
5794}
5795
Bryan Schumaker65178db2011-11-01 13:35:21 -04005796#endif /* CONFIG_NFSD_FAULT_INJECTION */
5797
Meelap Shahc2f1a552007-07-17 04:04:39 -07005798/*
5799 * Since the lifetime of a delegation isn't limited to that of an open, a
5800 * client may quite reasonably hang on to a delegation as long as it has
5801 * the inode cached. This becomes an obvious problem the first time a
5802 * client's inode cache approaches the size of the server's total memory.
5803 *
5804 * For now we avoid this problem by imposing a hard limit on the number
5805 * of delegations, which varies according to the server's memory size.
5806 */
5807static void
5808set_max_delegations(void)
5809{
5810 /*
5811 * Allow at most 4 delegations per megabyte of RAM. Quick
5812 * estimates suggest that in the worst case (where every delegation
5813 * is for a different inode), a delegation could take about 1.5K,
5814 * giving a worst case usage of about 6% of memory.
5815 */
5816 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5817}
5818
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005819static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005820{
5821 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5822 int i;
5823
5824 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5825 CLIENT_HASH_SIZE, GFP_KERNEL);
5826 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005827 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005828 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5829 CLIENT_HASH_SIZE, GFP_KERNEL);
5830 if (!nn->unconf_id_hashtbl)
5831 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005832 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5833 OWNER_HASH_SIZE, GFP_KERNEL);
5834 if (!nn->ownerstr_hashtbl)
5835 goto err_ownerstr;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005836 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5837 SESSION_HASH_SIZE, GFP_KERNEL);
5838 if (!nn->sessionid_hashtbl)
5839 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005840
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005841 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005842 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005843 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005844 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005845 for (i = 0; i < OWNER_HASH_SIZE; i++)
5846 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005847 for (i = 0; i < SESSION_HASH_SIZE; i++)
5848 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005849 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005850 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005851 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005852 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005853 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005854 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005855
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005856 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005857 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005858
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005859 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005860
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005861err_sessionid:
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03005862 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005863err_ownerstr:
5864 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005865err_unconf_id:
5866 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005867err:
5868 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005869}
5870
5871static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005872nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005873{
5874 int i;
5875 struct nfs4_client *clp = NULL;
5876 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5877
5878 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5879 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5880 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5881 destroy_client(clp);
5882 }
5883 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005884
Kinglong Mee2b905632014-03-26 22:09:30 +08005885 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5886 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5887 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5888 destroy_client(clp);
5889 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005890 }
5891
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005892 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005893 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005894 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005895 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005896 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005897}
5898
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005899int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005900nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005901{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005902 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005903 int ret;
5904
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005905 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005906 if (ret)
5907 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005908 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005909 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005910 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005911 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005912 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005913 nn->nfsd4_grace, net);
5914 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005915 return 0;
5916}
5917
5918/* initialization to perform when the nfsd service is started: */
5919
5920int
5921nfs4_state_start(void)
5922{
5923 int ret;
5924
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005925 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005926 if (ret)
5927 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005928 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005929 if (laundry_wq == NULL) {
5930 ret = -ENOMEM;
5931 goto out_recovery;
5932 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005933 ret = nfsd4_create_callback_queue();
5934 if (ret)
5935 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005936
Meelap Shahc2f1a552007-07-17 04:04:39 -07005937 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005938
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005939 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005940
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005941out_free_laundry:
5942 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005943out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005944 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945}
5946
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005947void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005948nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005952 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005954 cancel_delayed_work_sync(&nn->laundromat_work);
5955 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005956
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005957 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04005959 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005960 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005962 unhash_delegation_locked(dp);
5963 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 }
Benny Halevycdc97502014-05-30 09:09:30 -04005965 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 list_for_each_safe(pos, next, &reaplist) {
5967 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04005968 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04005969 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 }
5971
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005972 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005973 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005974 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975}
5976
5977void
5978nfs4_state_shutdown(void)
5979{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005980 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005981 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005983
5984static void
5985get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5986{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005987 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5988 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005989}
5990
5991static void
5992put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5993{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005994 if (cstate->minorversion) {
5995 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5996 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5997 }
5998}
5999
6000void
6001clear_current_stateid(struct nfsd4_compound_state *cstate)
6002{
6003 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006004}
6005
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006006/*
6007 * functions to set current state id
6008 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006009void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006010nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6011{
6012 put_stateid(cstate, &odp->od_stateid);
6013}
6014
6015void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006016nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6017{
6018 put_stateid(cstate, &open->op_stateid);
6019}
6020
6021void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006022nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6023{
6024 put_stateid(cstate, &close->cl_stateid);
6025}
6026
6027void
6028nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6029{
6030 put_stateid(cstate, &lock->lk_resp_stateid);
6031}
6032
6033/*
6034 * functions to consume current state id
6035 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006036
6037void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006038nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6039{
6040 get_stateid(cstate, &odp->od_stateid);
6041}
6042
6043void
6044nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6045{
6046 get_stateid(cstate, &drp->dr_stateid);
6047}
6048
6049void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006050nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6051{
6052 get_stateid(cstate, &fsp->fr_stateid);
6053}
6054
6055void
6056nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6057{
6058 get_stateid(cstate, &setattr->sa_stateid);
6059}
6060
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006061void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006062nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6063{
6064 get_stateid(cstate, &close->cl_stateid);
6065}
6066
6067void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006068nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006069{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006070 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006071}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006072
6073void
6074nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6075{
6076 get_stateid(cstate, &read->rd_stateid);
6077}
6078
6079void
6080nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6081{
6082 get_stateid(cstate, &write->wr_stateid);
6083}