blob: eefd29ec43f2dee0f435138c8c15fcc8e1bbf936 [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>
Daniel Borkmann87545892014-12-10 16:33:11 +010044#include <linux/jhash.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
J. Bruce Fields8b671b82009-02-22 14:51:34 -080078/*
79 * Currently used for the del_recall_lru and file hash table. In an
80 * effort to decrease the scope of the client_mutex, this spinlock may
81 * eventually cover more:
82 */
Benny Halevycdc97502014-05-30 09:09:30 -040083static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080084
Jeff Laytonb401be222014-07-29 21:34:33 -040085/*
86 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
87 * the refcount on the open stateid to drop.
88 */
89static DECLARE_WAIT_QUEUE_HEAD(close_wq);
90
Christoph Hellwigabf11352014-05-21 07:43:03 -070091static struct kmem_cache *openowner_slab;
92static struct kmem_cache *lockowner_slab;
93static struct kmem_cache *file_slab;
94static struct kmem_cache *stateid_slab;
95static struct kmem_cache *deleg_slab;
NeilBrowne60d4392005-06-23 22:03:01 -070096
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -040097static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -080098
Christoph Hellwig0162ac22014-09-24 12:19:19 +020099static struct nfsd4_callback_ops nfsd4_cb_recall_ops;
100
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400101static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800102{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400103 return ses->se_flags & NFS4_SESSION_DEAD;
104}
105
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400106static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
107{
108 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400109 return nfserr_jukebox;
110 ses->se_flags |= NFS4_SESSION_DEAD;
111 return nfs_ok;
112}
113
J. Bruce Fields221a6872013-04-01 22:23:49 -0400114static bool is_client_expired(struct nfs4_client *clp)
115{
116 return clp->cl_time == 0;
117}
118
J. Bruce Fields221a6872013-04-01 22:23:49 -0400119static __be32 get_client_locked(struct nfs4_client *clp)
120{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400121 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
122
123 lockdep_assert_held(&nn->client_lock);
124
J. Bruce Fields221a6872013-04-01 22:23:49 -0400125 if (is_client_expired(clp))
126 return nfserr_expired;
127 atomic_inc(&clp->cl_refcount);
128 return nfs_ok;
129}
130
131/* must be called under the client_lock */
132static inline void
133renew_client_locked(struct nfs4_client *clp)
134{
135 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
136
137 if (is_client_expired(clp)) {
138 WARN_ON(1);
139 printk("%s: client (clientid %08x/%08x) already expired\n",
140 __func__,
141 clp->cl_clientid.cl_boot,
142 clp->cl_clientid.cl_id);
143 return;
144 }
145
146 dprintk("renewing client (clientid %08x/%08x)\n",
147 clp->cl_clientid.cl_boot,
148 clp->cl_clientid.cl_id);
149 list_move_tail(&clp->cl_lru, &nn->client_lru);
150 clp->cl_time = get_seconds();
151}
152
Fengguang Wuba138432013-04-16 22:14:15 -0400153static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400154{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400155 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
156
157 lockdep_assert_held(&nn->client_lock);
158
J. Bruce Fields221a6872013-04-01 22:23:49 -0400159 if (!atomic_dec_and_test(&clp->cl_refcount))
160 return;
161 if (!is_client_expired(clp))
162 renew_client_locked(clp);
163}
164
Jeff Layton4b24ca72014-06-30 11:48:44 -0400165static void put_client_renew(struct nfs4_client *clp)
166{
167 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
168
Jeff Laytond6c249b2014-07-08 14:02:50 -0400169 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
170 return;
171 if (!is_client_expired(clp))
172 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400173 spin_unlock(&nn->client_lock);
174}
175
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400176static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
177{
178 __be32 status;
179
180 if (is_session_dead(ses))
181 return nfserr_badsession;
182 status = get_client_locked(ses->se_client);
183 if (status)
184 return status;
185 atomic_inc(&ses->se_ref);
186 return nfs_ok;
187}
188
189static void nfsd4_put_session_locked(struct nfsd4_session *ses)
190{
191 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400192 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
193
194 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400195
196 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
197 free_session(ses);
198 put_client_renew_locked(clp);
199}
200
201static void nfsd4_put_session(struct nfsd4_session *ses)
202{
203 struct nfs4_client *clp = ses->se_client;
204 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
205
206 spin_lock(&nn->client_lock);
207 nfsd4_put_session_locked(ses);
208 spin_unlock(&nn->client_lock);
209}
210
Kinglong Meeb5971af2014-08-22 10:18:43 -0400211static inline struct nfs4_stateowner *
212nfs4_get_stateowner(struct nfs4_stateowner *sop)
213{
214 atomic_inc(&sop->so_count);
215 return sop;
216}
217
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400218static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400219same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400220{
221 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400222 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400223}
224
225static struct nfs4_openowner *
226find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400227 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400228{
229 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400230
Trond Myklebustd4f04892014-07-29 21:34:36 -0400231 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400232
Trond Myklebustd4f04892014-07-29 21:34:36 -0400233 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
234 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400235 if (!so->so_is_open_owner)
236 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400237 if (same_owner_str(so, &open->op_owner))
238 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400239 }
240 return NULL;
241}
242
243static struct nfs4_openowner *
244find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400245 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400246{
247 struct nfs4_openowner *oo;
248
Trond Myklebustd4f04892014-07-29 21:34:36 -0400249 spin_lock(&clp->cl_lock);
250 oo = find_openstateowner_str_locked(hashval, open, clp);
251 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400252 return oo;
253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255static inline u32
256opaque_hashval(const void *ptr, int nbytes)
257{
258 unsigned char *cptr = (unsigned char *) ptr;
259
260 u32 x = 0;
261 while (nbytes--) {
262 x *= 37;
263 x += *cptr++;
264 }
265 return x;
266}
267
Jeff Layton5b095e92014-10-23 08:01:02 -0400268static void nfsd4_free_file_rcu(struct rcu_head *rcu)
J. Bruce Fields32513b42011-10-13 16:00:16 -0400269{
Jeff Layton5b095e92014-10-23 08:01:02 -0400270 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
271
272 kmem_cache_free(file_slab, fp);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400273}
274
Christoph Hellwige6ba76e2014-08-14 08:50:16 +0200275void
NeilBrown13cd2182005-06-23 22:03:10 -0700276put_nfs4_file(struct nfs4_file *fi)
277{
Jeff Layton02e12152014-07-16 10:31:57 -0400278 might_lock(&state_lock);
279
Benny Halevycdc97502014-05-30 09:09:30 -0400280 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton5b095e92014-10-23 08:01:02 -0400281 hlist_del_rcu(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400282 spin_unlock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -0400283 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
284 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800285 }
NeilBrown13cd2182005-06-23 22:03:10 -0700286}
287
Trond Myklebustde186432014-07-10 14:07:26 -0400288static struct file *
289__nfs4_get_fd(struct nfs4_file *f, int oflag)
290{
291 if (f->fi_fds[oflag])
292 return get_file(f->fi_fds[oflag]);
293 return NULL;
294}
295
296static struct file *
297find_writeable_file_locked(struct nfs4_file *f)
298{
299 struct file *ret;
300
301 lockdep_assert_held(&f->fi_lock);
302
303 ret = __nfs4_get_fd(f, O_WRONLY);
304 if (!ret)
305 ret = __nfs4_get_fd(f, O_RDWR);
306 return ret;
307}
308
309static struct file *
310find_writeable_file(struct nfs4_file *f)
311{
312 struct file *ret;
313
314 spin_lock(&f->fi_lock);
315 ret = find_writeable_file_locked(f);
316 spin_unlock(&f->fi_lock);
317
318 return ret;
319}
320
321static struct file *find_readable_file_locked(struct nfs4_file *f)
322{
323 struct file *ret;
324
325 lockdep_assert_held(&f->fi_lock);
326
327 ret = __nfs4_get_fd(f, O_RDONLY);
328 if (!ret)
329 ret = __nfs4_get_fd(f, O_RDWR);
330 return ret;
331}
332
333static struct file *
334find_readable_file(struct nfs4_file *f)
335{
336 struct file *ret;
337
338 spin_lock(&f->fi_lock);
339 ret = find_readable_file_locked(f);
340 spin_unlock(&f->fi_lock);
341
342 return ret;
343}
344
Christoph Hellwig4d227fc2014-08-17 07:40:00 -0500345struct file *
Trond Myklebustde186432014-07-10 14:07:26 -0400346find_any_file(struct nfs4_file *f)
347{
348 struct file *ret;
349
350 spin_lock(&f->fi_lock);
351 ret = __nfs4_get_fd(f, O_RDWR);
352 if (!ret) {
353 ret = __nfs4_get_fd(f, O_WRONLY);
354 if (!ret)
355 ret = __nfs4_get_fd(f, O_RDONLY);
356 }
357 spin_unlock(&f->fi_lock);
358 return ret;
359}
360
Trond Myklebust02a35082014-07-25 07:34:22 -0400361static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800362unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700363
364/*
365 * Open owner state (share locks)
366 */
367
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500368/* hash tables for lock and open owners */
369#define OWNER_HASH_BITS 8
370#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
371#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700372
Trond Myklebustd4f04892014-07-29 21:34:36 -0400373static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400374{
375 unsigned int ret;
376
377 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500378 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400379}
NeilBrownef0f3392006-04-10 22:55:41 -0700380
NeilBrownef0f3392006-04-10 22:55:41 -0700381/* hash table for nfs4_file */
382#define FILE_HASH_BITS 8
383#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800384
Trond Myklebustca943212014-07-23 16:17:39 -0400385static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400386{
Trond Myklebustca943212014-07-23 16:17:39 -0400387 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
388}
389
390static unsigned int file_hashval(struct knfsd_fh *fh)
391{
392 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
393}
394
Jeff Layton89876f82013-04-02 09:01:59 -0400395static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700396
Jeff Layton12659652014-07-10 14:07:28 -0400397static void
398__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400399{
Jeff Layton7214e862014-07-10 14:07:33 -0400400 lockdep_assert_held(&fp->fi_lock);
401
Jeff Layton12659652014-07-10 14:07:28 -0400402 if (access & NFS4_SHARE_ACCESS_WRITE)
403 atomic_inc(&fp->fi_access[O_WRONLY]);
404 if (access & NFS4_SHARE_ACCESS_READ)
405 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400406}
407
Jeff Layton12659652014-07-10 14:07:28 -0400408static __be32
409nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400410{
Jeff Layton7214e862014-07-10 14:07:33 -0400411 lockdep_assert_held(&fp->fi_lock);
412
Jeff Layton12659652014-07-10 14:07:28 -0400413 /* Does this access mode make sense? */
414 if (access & ~NFS4_SHARE_ACCESS_BOTH)
415 return nfserr_inval;
416
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400417 /* Does it conflict with a deny mode already set? */
418 if ((access & fp->fi_share_deny) != 0)
419 return nfserr_share_denied;
420
Jeff Layton12659652014-07-10 14:07:28 -0400421 __nfs4_file_get_access(fp, access);
422 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400423}
424
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400425static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
426{
427 /* Common case is that there is no deny mode. */
428 if (deny) {
429 /* Does this deny mode make sense? */
430 if (deny & ~NFS4_SHARE_DENY_BOTH)
431 return nfserr_inval;
432
433 if ((deny & NFS4_SHARE_DENY_READ) &&
434 atomic_read(&fp->fi_access[O_RDONLY]))
435 return nfserr_share_denied;
436
437 if ((deny & NFS4_SHARE_DENY_WRITE) &&
438 atomic_read(&fp->fi_access[O_WRONLY]))
439 return nfserr_share_denied;
440 }
441 return nfs_ok;
442}
443
J. Bruce Fields998db522010-08-07 09:21:41 -0400444static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400445{
Trond Myklebustde186432014-07-10 14:07:26 -0400446 might_lock(&fp->fi_lock);
447
448 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
449 struct file *f1 = NULL;
450 struct file *f2 = NULL;
451
Jeff Layton6d338b52014-07-10 14:07:29 -0400452 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400453 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400454 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400455 spin_unlock(&fp->fi_lock);
456 if (f1)
457 fput(f1);
458 if (f2)
459 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400460 }
461}
462
Jeff Layton12659652014-07-10 14:07:28 -0400463static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400464{
Jeff Layton12659652014-07-10 14:07:28 -0400465 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
466
467 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400468 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400469 if (access & NFS4_SHARE_ACCESS_READ)
470 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400471}
472
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200473struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
Trond Myklebust60116952014-07-29 21:34:06 -0400474 struct kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400475{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500476 struct nfs4_stid *stid;
477 int new_id;
478
Trond Myklebustf8338832014-07-25 07:34:19 -0400479 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500480 if (!stid)
481 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400482
Jeff Layton4770d722014-07-29 21:34:10 -0400483 idr_preload(GFP_KERNEL);
484 spin_lock(&cl->cl_lock);
485 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
486 spin_unlock(&cl->cl_lock);
487 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700488 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500489 goto out_free;
490 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500491 stid->sc_stateid.si_opaque.so_id = new_id;
492 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
493 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400494 atomic_set(&stid->sc_count, 1);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500495
J. Bruce Fields996e0932011-10-17 11:14:48 -0400496 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500497 * It shouldn't be a problem to reuse an opaque stateid value.
498 * I don't think it is for 4.1. But with 4.0 I worry that, for
499 * example, a stray write retransmission could be accepted by
500 * the server when it should have been rejected. Therefore,
501 * adopt a trick from the sctp code to attempt to maximize the
502 * amount of time until an id is reused, by ensuring they always
503 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400504 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500505 return stid;
506out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800507 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500508 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400509}
510
Jeff Laytonb49e0842014-07-29 21:34:11 -0400511static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400512{
Trond Myklebust60116952014-07-29 21:34:06 -0400513 struct nfs4_stid *stid;
514 struct nfs4_ol_stateid *stp;
515
516 stid = nfs4_alloc_stid(clp, stateid_slab);
517 if (!stid)
518 return NULL;
519
520 stp = openlockstateid(stid);
521 stp->st_stid.sc_free = nfs4_free_ol_stateid;
522 return stp;
523}
524
525static void nfs4_free_deleg(struct nfs4_stid *stid)
526{
Trond Myklebust60116952014-07-29 21:34:06 -0400527 kmem_cache_free(deleg_slab, stid);
528 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400529}
530
NeilBrown6282cd52014-06-04 17:39:26 +1000531/*
532 * When we recall a delegation, we should be careful not to hand it
533 * out again straight away.
534 * To ensure this we keep a pair of bloom filters ('new' and 'old')
535 * in which the filehandles of recalled delegations are "stored".
536 * If a filehandle appear in either filter, a delegation is blocked.
537 * When a delegation is recalled, the filehandle is stored in the "new"
538 * filter.
539 * Every 30 seconds we swap the filters and clear the "new" one,
540 * unless both are empty of course.
541 *
542 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
543 * low 3 bytes as hash-table indices.
544 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400545 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000546 * is used to manage concurrent access. Testing does not need the lock
547 * except when swapping the two filters.
548 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400549static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000550static struct bloom_pair {
551 int entries, old_entries;
552 time_t swap_time;
553 int new; /* index into 'set' */
554 DECLARE_BITMAP(set[2], 256);
555} blocked_delegations;
556
557static int delegation_blocked(struct knfsd_fh *fh)
558{
559 u32 hash;
560 struct bloom_pair *bd = &blocked_delegations;
561
562 if (bd->entries == 0)
563 return 0;
564 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400565 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000566 if (seconds_since_boot() - bd->swap_time > 30) {
567 bd->entries -= bd->old_entries;
568 bd->old_entries = bd->entries;
569 memset(bd->set[bd->new], 0,
570 sizeof(bd->set[0]));
571 bd->new = 1-bd->new;
572 bd->swap_time = seconds_since_boot();
573 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400574 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000575 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100576 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000577 if (test_bit(hash&255, bd->set[0]) &&
578 test_bit((hash>>8)&255, bd->set[0]) &&
579 test_bit((hash>>16)&255, bd->set[0]))
580 return 1;
581
582 if (test_bit(hash&255, bd->set[1]) &&
583 test_bit((hash>>8)&255, bd->set[1]) &&
584 test_bit((hash>>16)&255, bd->set[1]))
585 return 1;
586
587 return 0;
588}
589
590static void block_delegations(struct knfsd_fh *fh)
591{
592 u32 hash;
593 struct bloom_pair *bd = &blocked_delegations;
594
Daniel Borkmann87545892014-12-10 16:33:11 +0100595 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000596
Jeff Laytonf54fe962014-07-25 07:34:26 -0400597 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000598 __set_bit(hash&255, bd->set[bd->new]);
599 __set_bit((hash>>8)&255, bd->set[bd->new]);
600 __set_bit((hash>>16)&255, bd->set[bd->new]);
601 if (bd->entries == 0)
602 bd->swap_time = seconds_since_boot();
603 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400604 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000605}
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607static struct nfs4_delegation *
Jeff Laytonf9416e22014-07-25 07:34:23 -0400608alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400611 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400614 n = atomic_long_inc_return(&num_delegations);
615 if (n < 0 || n > max_delegations)
616 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000617 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400618 goto out_dec;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400619 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700620 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400621 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400622
623 dp->dl_stid.sc_free = nfs4_free_deleg;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400624 /*
625 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400626 * meaning of seqid 0 isn't meaningful, really, but let's avoid
627 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400628 */
629 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700630 INIT_LIST_HEAD(&dp->dl_perfile);
631 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400633 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200634 dp->dl_retries = 1;
635 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200636 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400638out_dec:
639 atomic_long_dec(&num_delegations);
640 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643void
Trond Myklebust60116952014-07-29 21:34:06 -0400644nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Trond Myklebust11b91642014-07-29 21:34:08 -0400646 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400647 struct nfs4_client *clp = s->sc_client;
648
Jeff Layton4770d722014-07-29 21:34:10 -0400649 might_lock(&clp->cl_lock);
650
Jeff Laytonb401be222014-07-29 21:34:33 -0400651 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
652 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400653 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400654 }
Trond Myklebust60116952014-07-29 21:34:06 -0400655 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400656 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400657 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400658 if (fp)
659 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500662static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Jeff Layton6bcc0342014-08-09 10:22:40 -0400664 struct file *filp = NULL;
Jeff Layton417c6622014-07-21 09:34:57 -0400665
Jeff Layton6bcc0342014-08-09 10:22:40 -0400666 spin_lock(&fp->fi_lock);
Jeff Layton67db1032014-12-13 09:11:40 -0500667 if (fp->fi_deleg_file && --fp->fi_delegees == 0)
Jeff Layton6bcc0342014-08-09 10:22:40 -0400668 swap(filp, fp->fi_deleg_file);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400669 spin_unlock(&fp->fi_lock);
670
671 if (filp) {
Christoph Hellwig2ab99ee2015-01-21 19:14:02 +0100672 vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400673 fput(filp);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200677void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400678{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500679 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400680}
681
Benny Halevy931ee562014-05-30 09:09:27 -0400682static void
683hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
684{
Benny Halevycdc97502014-05-30 09:09:30 -0400685 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400686 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400687
Trond Myklebust67cb1272014-07-29 21:34:17 -0400688 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400689 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400690 list_add(&dp->dl_perfile, &fp->fi_delegations);
691 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
692}
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694static void
Jeff Layton42690672014-07-25 07:34:20 -0400695unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Trond Myklebust11b91642014-07-29 21:34:08 -0400697 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400698
Jeff Layton42690672014-07-25 07:34:20 -0400699 lockdep_assert_held(&state_lock);
700
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400701 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400702 /* Ensure that deleg break won't try to requeue it */
703 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400704 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400705 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400707 list_del_init(&dp->dl_perfile);
708 spin_unlock(&fp->fi_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400709}
710
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400711static void destroy_delegation(struct nfs4_delegation *dp)
712{
Jeff Layton42690672014-07-25 07:34:20 -0400713 spin_lock(&state_lock);
714 unhash_delegation_locked(dp);
715 spin_unlock(&state_lock);
Jeff Laytonafbda402014-08-09 10:22:41 -0400716 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -0400717 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400718}
719
720static void revoke_delegation(struct nfs4_delegation *dp)
721{
722 struct nfs4_client *clp = dp->dl_stid.sc_client;
723
Jeff Layton2d4a5322014-07-25 07:34:21 -0400724 WARN_ON(!list_empty(&dp->dl_recall_lru));
725
Jeff Laytonafbda402014-08-09 10:22:41 -0400726 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
727
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400728 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400729 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400730 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400731 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400732 spin_lock(&clp->cl_lock);
733 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
734 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400735 }
736}
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738/*
739 * SETCLIENTID state
740 */
741
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400742static unsigned int clientid_hashval(u32 id)
743{
744 return id & CLIENT_HASH_MASK;
745}
746
747static unsigned int clientstr_hashval(const char *name)
748{
749 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
750}
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400753 * We store the NONE, READ, WRITE, and BOTH bits separately in the
754 * st_{access,deny}_bmap field of the stateid, in order to track not
755 * only what share bits are currently in force, but also what
756 * combinations of share bits previous opens have used. This allows us
757 * to enforce the recommendation of rfc 3530 14.2.19 that the server
758 * return an error if the client attempt to downgrade to a combination
759 * of share bits not explicable by closing some of its previous opens.
760 *
761 * XXX: This enforcement is actually incomplete, since we don't keep
762 * track of access/deny bit combinations; so, e.g., we allow:
763 *
764 * OPEN allow read, deny write
765 * OPEN allow both, deny none
766 * DOWNGRADE allow read, deny none
767 *
768 * which we should reject.
769 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400770static unsigned int
771bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400772 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400773 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400774
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400775 for (i = 1; i < 4; i++) {
776 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400777 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400778 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400779 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400780}
781
Jeff Layton82c5ff12012-05-11 09:45:13 -0400782/* set share access for a given stateid */
783static inline void
784set_access(u32 access, struct nfs4_ol_stateid *stp)
785{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400786 unsigned char mask = 1 << access;
787
788 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
789 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400790}
791
792/* clear share access for a given stateid */
793static inline void
794clear_access(u32 access, struct nfs4_ol_stateid *stp)
795{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400796 unsigned char mask = 1 << access;
797
798 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
799 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400800}
801
802/* test whether a given stateid has access */
803static inline bool
804test_access(u32 access, struct nfs4_ol_stateid *stp)
805{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400806 unsigned char mask = 1 << access;
807
808 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400809}
810
Jeff Laytonce0fc432012-05-11 09:45:14 -0400811/* set share deny for a given stateid */
812static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400813set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400814{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400815 unsigned char mask = 1 << deny;
816
817 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
818 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400819}
820
821/* clear share deny for a given stateid */
822static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400823clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400824{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400825 unsigned char mask = 1 << deny;
826
827 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
828 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400829}
830
831/* test whether a given stateid is denying specific access */
832static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400833test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400834{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400835 unsigned char mask = 1 << deny;
836
837 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400838}
839
840static int nfs4_access_to_omode(u32 access)
841{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400842 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400843 case NFS4_SHARE_ACCESS_READ:
844 return O_RDONLY;
845 case NFS4_SHARE_ACCESS_WRITE:
846 return O_WRONLY;
847 case NFS4_SHARE_ACCESS_BOTH:
848 return O_RDWR;
849 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500850 WARN_ON_ONCE(1);
851 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400852}
853
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400854/*
855 * A stateid that had a deny mode associated with it is being released
856 * or downgraded. Recalculate the deny mode on the file.
857 */
858static void
859recalculate_deny_mode(struct nfs4_file *fp)
860{
861 struct nfs4_ol_stateid *stp;
862
863 spin_lock(&fp->fi_lock);
864 fp->fi_share_deny = 0;
865 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
866 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
867 spin_unlock(&fp->fi_lock);
868}
869
870static void
871reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
872{
873 int i;
874 bool change = false;
875
876 for (i = 1; i < 4; i++) {
877 if ((i & deny) != i) {
878 change = true;
879 clear_deny(i, stp);
880 }
881 }
882
883 /* Recalculate per-file deny mode if there was a change */
884 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -0400885 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400886}
887
Jeff Layton82c5ff12012-05-11 09:45:13 -0400888/* release all access and file references for a given stateid */
889static void
890release_all_access(struct nfs4_ol_stateid *stp)
891{
892 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -0400893 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400894
895 if (fp && stp->st_deny_bmap != 0)
896 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400897
898 for (i = 1; i < 4; i++) {
899 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -0400900 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400901 clear_access(i, stp);
902 }
903}
904
Jeff Layton6b180f02014-07-29 21:34:26 -0400905static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
906{
Jeff Laytona819ecc2014-07-29 21:34:38 -0400907 struct nfs4_client *clp = sop->so_client;
908
909 might_lock(&clp->cl_lock);
910
911 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -0400912 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -0400913 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -0400914 spin_unlock(&clp->cl_lock);
Jeff Layton6b180f02014-07-29 21:34:26 -0400915 kfree(sop->so_owner.data);
916 sop->so_ops->so_free(sop);
917}
918
Jeff Layton4ae098d2014-07-29 21:34:43 -0400919static void unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500920{
Trond Myklebust11b91642014-07-29 21:34:08 -0400921 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -0400922
Jeff Layton1c755dc2014-07-29 21:34:12 -0400923 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
924
Trond Myklebust1d31a252014-07-10 14:07:25 -0400925 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500926 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400927 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500928 list_del(&stp->st_perstateowner);
929}
930
Trond Myklebust60116952014-07-29 21:34:06 -0400931static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500932{
Trond Myklebust60116952014-07-29 21:34:06 -0400933 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400934
Trond Myklebust60116952014-07-29 21:34:06 -0400935 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -0400936 if (stp->st_stateowner)
937 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -0400938 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500939}
940
Jeff Laytonb49e0842014-07-29 21:34:11 -0400941static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500942{
Jeff Laytonb49e0842014-07-29 21:34:11 -0400943 struct nfs4_ol_stateid *stp = openlockstateid(stid);
944 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500945 struct file *file;
946
Jeff Laytonb49e0842014-07-29 21:34:11 -0400947 file = find_any_file(stp->st_stid.sc_file);
948 if (file)
949 filp_close(file, (fl_owner_t)lo);
950 nfs4_free_ol_stateid(stid);
951}
952
Jeff Layton2c41beb2014-07-29 21:34:41 -0400953/*
954 * Put the persistent reference to an already unhashed generic stateid, while
955 * holding the cl_lock. If it's the last reference, then put it onto the
956 * reaplist for later destruction.
957 */
958static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
959 struct list_head *reaplist)
960{
961 struct nfs4_stid *s = &stp->st_stid;
962 struct nfs4_client *clp = s->sc_client;
963
964 lockdep_assert_held(&clp->cl_lock);
965
966 WARN_ON_ONCE(!list_empty(&stp->st_locks));
967
968 if (!atomic_dec_and_test(&s->sc_count)) {
969 wake_up_all(&close_wq);
970 return;
971 }
972
973 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
974 list_add(&stp->st_locks, reaplist);
975}
976
Jeff Layton3c1c9952014-07-29 21:34:39 -0400977static void unhash_lock_stateid(struct nfs4_ol_stateid *stp)
978{
979 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
980
981 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
982
983 list_del_init(&stp->st_locks);
Jeff Layton4ae098d2014-07-29 21:34:43 -0400984 unhash_ol_stateid(stp);
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200985 nfs4_unhash_stid(&stp->st_stid);
Jeff Layton3c1c9952014-07-29 21:34:39 -0400986}
987
Jeff Layton5adfd882014-07-29 21:34:31 -0400988static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -0400989{
Jeff Layton1c755dc2014-07-29 21:34:12 -0400990 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
991
992 spin_lock(&oo->oo_owner.so_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -0400993 unhash_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -0400994 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400995 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500996}
997
Trond Myklebustc58c6612014-07-29 21:34:35 -0400998static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500999{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001000 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001001
Trond Myklebustd4f04892014-07-29 21:34:36 -04001002 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001003
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001004 list_del_init(&lo->lo_owner.so_strhash);
1005}
1006
Jeff Layton2c41beb2014-07-29 21:34:41 -04001007/*
1008 * Free a list of generic stateids that were collected earlier after being
1009 * fully unhashed.
1010 */
1011static void
1012free_ol_stateid_reaplist(struct list_head *reaplist)
1013{
1014 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001015 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001016
1017 might_sleep();
1018
1019 while (!list_empty(reaplist)) {
1020 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1021 st_locks);
1022 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001023 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001024 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001025 if (fp)
1026 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001027 }
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 Myklebustd4f04892014-07-29 21:34:36 -04001032 struct nfs4_client *clp = lo->lo_owner.so_client;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001033 struct nfs4_ol_stateid *stp;
1034 struct list_head reaplist;
1035
1036 INIT_LIST_HEAD(&reaplist);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001037
Trond Myklebustd4f04892014-07-29 21:34:36 -04001038 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001039 unhash_lockowner_locked(lo);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001040 while (!list_empty(&lo->lo_owner.so_stateids)) {
1041 stp = list_first_entry(&lo->lo_owner.so_stateids,
1042 struct nfs4_ol_stateid, st_perstateowner);
1043 unhash_lock_stateid(stp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001044 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001045 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001046 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001047 free_ol_stateid_reaplist(&reaplist);
Jeff Layton6b180f02014-07-29 21:34:26 -04001048 nfs4_put_stateowner(&lo->lo_owner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001049}
1050
Jeff Laytond83017f2014-07-29 21:34:42 -04001051static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1052 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001053{
1054 struct nfs4_ol_stateid *stp;
1055
1056 while (!list_empty(&open_stp->st_locks)) {
1057 stp = list_entry(open_stp->st_locks.next,
1058 struct nfs4_ol_stateid, st_locks);
Jeff Laytond83017f2014-07-29 21:34:42 -04001059 unhash_lock_stateid(stp);
1060 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001061 }
1062}
1063
Jeff Laytond83017f2014-07-29 21:34:42 -04001064static void unhash_open_stateid(struct nfs4_ol_stateid *stp,
1065 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001066{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001067 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1068
Jeff Layton4ae098d2014-07-29 21:34:43 -04001069 unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001070 release_open_stateid_locks(stp, reaplist);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001071}
1072
1073static void release_open_stateid(struct nfs4_ol_stateid *stp)
1074{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001075 LIST_HEAD(reaplist);
1076
1077 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04001078 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001079 put_ol_stateid_locked(stp, &reaplist);
1080 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1081 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001082}
1083
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001084static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001085{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001086 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001087
Trond Myklebustd4f04892014-07-29 21:34:36 -04001088 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001089
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001090 list_del_init(&oo->oo_owner.so_strhash);
1091 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001092}
1093
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001094static void release_last_closed_stateid(struct nfs4_openowner *oo)
1095{
Jeff Layton217526e2014-07-30 08:27:11 -04001096 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1097 nfsd_net_id);
1098 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001099
Jeff Layton217526e2014-07-30 08:27:11 -04001100 spin_lock(&nn->client_lock);
1101 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001102 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001103 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001104 oo->oo_last_closed_stid = NULL;
1105 }
Jeff Layton217526e2014-07-30 08:27:11 -04001106 spin_unlock(&nn->client_lock);
1107 if (s)
1108 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001109}
1110
Jeff Layton2c41beb2014-07-29 21:34:41 -04001111static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001112{
1113 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001114 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001115 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001116
Jeff Layton2c41beb2014-07-29 21:34:41 -04001117 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001118
Trond Myklebustd4f04892014-07-29 21:34:36 -04001119 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001120 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001121 while (!list_empty(&oo->oo_owner.so_stateids)) {
1122 stp = list_first_entry(&oo->oo_owner.so_stateids,
1123 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytond83017f2014-07-29 21:34:42 -04001124 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001125 put_ol_stateid_locked(stp, &reaplist);
1126 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001127 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001128 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001129 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001130 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001131}
1132
Marc Eshel5282fd72009-04-03 08:27:52 +03001133static inline int
1134hash_sessionid(struct nfs4_sessionid *sessionid)
1135{
1136 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1137
1138 return sid->sequence % SESSION_HASH_SIZE;
1139}
1140
Trond Myklebust8f199b82012-03-20 15:11:17 -04001141#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001142static inline void
1143dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1144{
1145 u32 *ptr = (u32 *)(&sessionid->data[0]);
1146 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1147}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001148#else
1149static inline void
1150dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1151{
1152}
1153#endif
1154
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001155/*
1156 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1157 * won't be used for replay.
1158 */
1159void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1160{
1161 struct nfs4_stateowner *so = cstate->replay_owner;
1162
1163 if (nfserr == nfserr_replay_me)
1164 return;
1165
1166 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001167 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001168 return;
1169 }
1170 if (!so)
1171 return;
1172 if (so->so_is_open_owner)
1173 release_last_closed_stateid(openowner(so));
1174 so->so_seqid++;
1175 return;
1176}
Marc Eshel5282fd72009-04-03 08:27:52 +03001177
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001178static void
1179gen_sessionid(struct nfsd4_session *ses)
1180{
1181 struct nfs4_client *clp = ses->se_client;
1182 struct nfsd4_sessionid *sid;
1183
1184 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1185 sid->clientid = clp->cl_clientid;
1186 sid->sequence = current_sessionid++;
1187 sid->reserved = 0;
1188}
1189
1190/*
Andy Adamsona6496372009-08-28 08:45:01 -04001191 * The protocol defines ca_maxresponssize_cached to include the size of
1192 * the rpc header, but all we need to cache is the data starting after
1193 * the end of the initial SEQUENCE operation--the rest we regenerate
1194 * each time. Therefore we can advertise a ca_maxresponssize_cached
1195 * value that is the number of bytes in our cache plus a few additional
1196 * bytes. In order to stay on the safe side, and not promise more than
1197 * we can cache, those additional bytes must be the minimum possible: 24
1198 * bytes of rpc header (xid through accept state, with AUTH_NULL
1199 * verifier), 12 for the compound header (with zero-length tag), and 44
1200 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001201 */
Andy Adamsona6496372009-08-28 08:45:01 -04001202#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1203
Andy Adamson557ce262009-08-28 08:45:04 -04001204static void
1205free_session_slots(struct nfsd4_session *ses)
1206{
1207 int i;
1208
1209 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1210 kfree(ses->se_slots[i]);
1211}
1212
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001213/*
1214 * We don't actually need to cache the rpc and session headers, so we
1215 * can allocate a little less for each slot:
1216 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001217static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001218{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001219 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001220
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001221 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1222 size = 0;
1223 else
1224 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1225 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001226}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001227
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001228/*
1229 * XXX: If we run out of reserved DRC memory we could (up to a point)
1230 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001231 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001232 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001233static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001234{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001235 u32 slotsize = slot_bytes(ca);
1236 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001237 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001238
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001239 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001240 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1241 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001242 num = min_t(int, num, avail / slotsize);
1243 nfsd_drc_mem_used += num * slotsize;
1244 spin_unlock(&nfsd_drc_lock);
1245
1246 return num;
1247}
1248
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001249static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001250{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001251 int slotsize = slot_bytes(ca);
1252
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001253 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001254 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001255 spin_unlock(&nfsd_drc_lock);
1256}
1257
Kinglong Mee60810e52014-01-01 00:35:47 +08001258static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1259 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001260{
Kinglong Mee60810e52014-01-01 00:35:47 +08001261 int numslots = fattrs->maxreqs;
1262 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001263 struct nfsd4_session *new;
1264 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001265
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001266 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001267 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1268 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001269
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001270 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001271 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001272 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001273 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001274 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001275 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001276 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001277 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001278 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001279
1280 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1281 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1282
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001283 return new;
1284out_free:
1285 while (i--)
1286 kfree(new->se_slots[i]);
1287 kfree(new);
1288 return NULL;
1289}
1290
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001291static void free_conn(struct nfsd4_conn *c)
1292{
1293 svc_xprt_put(c->cn_xprt);
1294 kfree(c);
1295}
1296
1297static void nfsd4_conn_lost(struct svc_xpt_user *u)
1298{
1299 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1300 struct nfs4_client *clp = c->cn_session->se_client;
1301
1302 spin_lock(&clp->cl_lock);
1303 if (!list_empty(&c->cn_persession)) {
1304 list_del(&c->cn_persession);
1305 free_conn(c);
1306 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001307 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001308 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001309}
1310
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001311static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001312{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001313 struct nfsd4_conn *conn;
1314
1315 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1316 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001317 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001318 svc_xprt_get(rqstp->rq_xprt);
1319 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001320 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001321 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1322 return conn;
1323}
1324
J. Bruce Fields328ead22010-09-29 16:11:06 -04001325static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1326{
1327 conn->cn_session = ses;
1328 list_add(&conn->cn_persession, &ses->se_conns);
1329}
1330
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001331static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1332{
1333 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001334
1335 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001336 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001337 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001338}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001339
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001340static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001341{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001342 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001343 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001344}
1345
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001346static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001347{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001348 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001349
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001350 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001351 ret = nfsd4_register_conn(conn);
1352 if (ret)
1353 /* oops; xprt is already down: */
1354 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001355 /* We may have gained or lost a callback channel: */
1356 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001357}
1358
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001359static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001360{
1361 u32 dir = NFS4_CDFC4_FORE;
1362
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001363 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001364 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001365 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001366}
1367
1368/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001369static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001370{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001371 struct nfs4_client *clp = s->se_client;
1372 struct nfsd4_conn *c;
1373
1374 spin_lock(&clp->cl_lock);
1375 while (!list_empty(&s->se_conns)) {
1376 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1377 list_del_init(&c->cn_persession);
1378 spin_unlock(&clp->cl_lock);
1379
1380 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1381 free_conn(c);
1382
1383 spin_lock(&clp->cl_lock);
1384 }
1385 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001386}
1387
J. Bruce Fields1377b692012-09-11 21:42:40 -04001388static void __free_session(struct nfsd4_session *ses)
1389{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001390 free_session_slots(ses);
1391 kfree(ses);
1392}
1393
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001394static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001395{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001396 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001397 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001398 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001399}
Andy Adamson557ce262009-08-28 08:45:04 -04001400
Fengguang Wu135ae822012-11-10 07:20:25 -05001401static 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 -04001402{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001403 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001404 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001405
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001406 new->se_client = clp;
1407 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001408
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001409 INIT_LIST_HEAD(&new->se_conns);
1410
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001411 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001412 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001413 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001414 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001415 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001416 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001417 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001418 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001419 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001420 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001421
Chuck Leverb0d2e422014-08-22 15:10:59 -04001422 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001423 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001424 /*
1425 * This is a little silly; with sessions there's no real
1426 * use for the callback address. Use the peer address
1427 * as a reasonable default for now, but consider fixing
1428 * the rpc client not to require an address in the
1429 * future:
1430 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001431 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1432 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001433 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001434}
1435
Benny Halevy9089f1b2010-05-12 00:12:26 +03001436/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001437static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001438__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001439{
1440 struct nfsd4_session *elem;
1441 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001442 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001443
Trond Myklebust0a880a22014-07-30 08:27:10 -04001444 lockdep_assert_held(&nn->client_lock);
1445
Marc Eshel5282fd72009-04-03 08:27:52 +03001446 dump_sessionid(__func__, sessionid);
1447 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001448 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001449 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001450 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1451 NFS4_MAX_SESSIONID_LEN)) {
1452 return elem;
1453 }
1454 }
1455
1456 dprintk("%s: session not found\n", __func__);
1457 return NULL;
1458}
1459
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001460static struct nfsd4_session *
1461find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1462 __be32 *ret)
1463{
1464 struct nfsd4_session *session;
1465 __be32 status = nfserr_badsession;
1466
1467 session = __find_in_sessionid_hashtbl(sessionid, net);
1468 if (!session)
1469 goto out;
1470 status = nfsd4_get_session_locked(session);
1471 if (status)
1472 session = NULL;
1473out:
1474 *ret = status;
1475 return session;
1476}
1477
Benny Halevy9089f1b2010-05-12 00:12:26 +03001478/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001479static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001480unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001481{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001482 struct nfs4_client *clp = ses->se_client;
1483 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1484
1485 lockdep_assert_held(&nn->client_lock);
1486
Andy Adamson7116ed62009-04-03 08:27:43 +03001487 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001488 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001489 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001490 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001491}
1492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1494static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001495STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001497 /*
1498 * We're assuming the clid was not given out from a boot
1499 * precisely 2^32 (about 136 years) before this one. That seems
1500 * a safe assumption:
1501 */
1502 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001504 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001505 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return 1;
1507}
1508
1509/*
1510 * XXX Should we use a slab cache ?
1511 * This type of memory management is somewhat inefficient, but we use it
1512 * anyway since SETCLIENTID is not a common operation.
1513 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001514static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001517 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001519 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1520 if (clp == NULL)
1521 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001522 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001523 if (clp->cl_name.data == NULL)
1524 goto err_no_name;
1525 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1526 OWNER_HASH_SIZE, GFP_KERNEL);
1527 if (!clp->cl_ownerstr_hashtbl)
1528 goto err_no_hashtbl;
1529 for (i = 0; i < OWNER_HASH_SIZE; i++)
1530 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001531 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001532 INIT_LIST_HEAD(&clp->cl_sessions);
1533 idr_init(&clp->cl_stateids);
1534 atomic_set(&clp->cl_refcount, 0);
1535 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1536 INIT_LIST_HEAD(&clp->cl_idhash);
1537 INIT_LIST_HEAD(&clp->cl_openowners);
1538 INIT_LIST_HEAD(&clp->cl_delegations);
1539 INIT_LIST_HEAD(&clp->cl_lru);
1540 INIT_LIST_HEAD(&clp->cl_callbacks);
1541 INIT_LIST_HEAD(&clp->cl_revoked);
1542 spin_lock_init(&clp->cl_lock);
1543 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001545err_no_hashtbl:
1546 kfree(clp->cl_name.data);
1547err_no_name:
1548 kfree(clp);
1549 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
1551
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001552static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553free_client(struct nfs4_client *clp)
1554{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001555 while (!list_empty(&clp->cl_sessions)) {
1556 struct nfsd4_session *ses;
1557 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1558 se_perclnt);
1559 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001560 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1561 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001562 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001563 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001564 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001565 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001567 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 kfree(clp);
1569}
1570
Benny Halevy84d38ac2010-05-12 00:13:16 +03001571/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001572static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001573unhash_client_locked(struct nfs4_client *clp)
1574{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001575 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001576 struct nfsd4_session *ses;
1577
Trond Myklebust0a880a22014-07-30 08:27:10 -04001578 lockdep_assert_held(&nn->client_lock);
1579
Trond Myklebust4beb3452014-07-30 08:27:02 -04001580 /* Mark the client as expired! */
1581 clp->cl_time = 0;
1582 /* Make it invisible */
1583 if (!list_empty(&clp->cl_idhash)) {
1584 list_del_init(&clp->cl_idhash);
1585 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1586 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1587 else
1588 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1589 }
1590 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001591 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001592 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1593 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001594 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001595}
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001598unhash_client(struct nfs4_client *clp)
1599{
1600 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1601
1602 spin_lock(&nn->client_lock);
1603 unhash_client_locked(clp);
1604 spin_unlock(&nn->client_lock);
1605}
1606
Jeff Layton97403d92014-07-30 08:27:12 -04001607static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1608{
1609 if (atomic_read(&clp->cl_refcount))
1610 return nfserr_jukebox;
1611 unhash_client_locked(clp);
1612 return nfs_ok;
1613}
1614
Trond Myklebust4beb3452014-07-30 08:27:02 -04001615static void
1616__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001618 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 struct list_head reaplist;
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001623 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001624 while (!list_empty(&clp->cl_delegations)) {
1625 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001626 unhash_delegation_locked(dp);
1627 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
Benny Halevycdc97502014-05-30 09:09:30 -04001629 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 while (!list_empty(&reaplist)) {
1631 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001632 list_del_init(&dp->dl_recall_lru);
Jeff Laytonafbda402014-08-09 10:22:41 -04001633 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04001634 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001636 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001637 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001638 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001639 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001640 }
NeilBrownea1da632005-06-23 22:04:17 -07001641 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001642 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04001643 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001644 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001646 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001647 if (clp->cl_cb_conn.cb_xprt)
1648 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001649 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
1651
Trond Myklebust4beb3452014-07-30 08:27:02 -04001652static void
1653destroy_client(struct nfs4_client *clp)
1654{
1655 unhash_client(clp);
1656 __destroy_client(clp);
1657}
1658
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001659static void expire_client(struct nfs4_client *clp)
1660{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001661 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001662 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001663 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001664}
1665
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001666static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1667{
1668 memcpy(target->cl_verifier.data, source->data,
1669 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
1671
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001672static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1673{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1675 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1676}
1677
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001678static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001679{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001680 if (source->cr_principal) {
1681 target->cr_principal =
1682 kstrdup(source->cr_principal, GFP_KERNEL);
1683 if (target->cr_principal == NULL)
1684 return -ENOMEM;
1685 } else
1686 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001687 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 target->cr_uid = source->cr_uid;
1689 target->cr_gid = source->cr_gid;
1690 target->cr_group_info = source->cr_group_info;
1691 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001692 target->cr_gss_mech = source->cr_gss_mech;
1693 if (source->cr_gss_mech)
1694 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001695 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001698static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001699compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1700{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001701 if (o1->len < o2->len)
1702 return -1;
1703 if (o1->len > o2->len)
1704 return 1;
1705 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001706}
1707
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001708static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001709{
NeilBrowna55370a2005-06-23 22:03:52 -07001710 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711}
1712
1713static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001714same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1715{
1716 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
1718
1719static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001720same_clid(clientid_t *cl1, clientid_t *cl2)
1721{
1722 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723}
1724
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001725static bool groups_equal(struct group_info *g1, struct group_info *g2)
1726{
1727 int i;
1728
1729 if (g1->ngroups != g2->ngroups)
1730 return false;
1731 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001732 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001733 return false;
1734 return true;
1735}
1736
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001737/*
1738 * RFC 3530 language requires clid_inuse be returned when the
1739 * "principal" associated with a requests differs from that previously
1740 * used. We use uid, gid's, and gss principal string as our best
1741 * approximation. We also don't want to allow non-gss use of a client
1742 * established using gss: in theory cr_principal should catch that
1743 * change, but in practice cr_principal can be null even in the gss case
1744 * since gssd doesn't always pass down a principal string.
1745 */
1746static bool is_gss_cred(struct svc_cred *cr)
1747{
1748 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1749 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1750}
1751
1752
Vivek Trivedi5559b502012-07-24 21:18:20 +05301753static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001754same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1755{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001756 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001757 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1758 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001759 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1760 return false;
1761 if (cr1->cr_principal == cr2->cr_principal)
1762 return true;
1763 if (!cr1->cr_principal || !cr2->cr_principal)
1764 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301765 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766}
1767
J. Bruce Fields57266a62013-04-13 14:27:29 -04001768static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1769{
1770 struct svc_cred *cr = &rqstp->rq_cred;
1771 u32 service;
1772
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001773 if (!cr->cr_gss_mech)
1774 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001775 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1776 return service == RPC_GSS_SVC_INTEGRITY ||
1777 service == RPC_GSS_SVC_PRIVACY;
1778}
1779
1780static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1781{
1782 struct svc_cred *cr = &rqstp->rq_cred;
1783
1784 if (!cl->cl_mach_cred)
1785 return true;
1786 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1787 return false;
1788 if (!svc_rqst_integrity_protected(rqstp))
1789 return false;
1790 if (!cr->cr_principal)
1791 return false;
1792 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1793}
1794
Jeff Layton294ac322014-07-30 08:27:15 -04001795static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001796{
Chuck Leverab4684d2012-03-02 17:13:50 -05001797 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Jeff Laytonf4199922014-06-17 07:44:11 -04001799 /*
1800 * This is opaque to client, so no need to byte-swap. Use
1801 * __force to keep sparse happy
1802 */
1803 verf[0] = (__force __be32)get_seconds();
Jeff Layton294ac322014-07-30 08:27:15 -04001804 verf[1] = (__force __be32)nn->clientid_counter;
Chuck Leverab4684d2012-03-02 17:13:50 -05001805 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806}
1807
Jeff Layton294ac322014-07-30 08:27:15 -04001808static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1809{
1810 clp->cl_clientid.cl_boot = nn->boot_time;
1811 clp->cl_clientid.cl_id = nn->clientid_counter++;
1812 gen_confirm(clp, nn);
1813}
1814
Jeff Layton4770d722014-07-29 21:34:10 -04001815static struct nfs4_stid *
1816find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001817{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001818 struct nfs4_stid *ret;
1819
1820 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1821 if (!ret || !ret->sc_type)
1822 return NULL;
1823 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001824}
1825
Jeff Layton4770d722014-07-29 21:34:10 -04001826static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001827find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001828{
1829 struct nfs4_stid *s;
1830
Jeff Layton4770d722014-07-29 21:34:10 -04001831 spin_lock(&cl->cl_lock);
1832 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001833 if (s != NULL) {
1834 if (typemask & s->sc_type)
1835 atomic_inc(&s->sc_count);
1836 else
1837 s = NULL;
1838 }
Jeff Layton4770d722014-07-29 21:34:10 -04001839 spin_unlock(&cl->cl_lock);
1840 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001841}
1842
Jeff Layton2216d442012-11-12 15:00:57 -05001843static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001844 struct svc_rqst *rqstp, nfs4_verifier *verf)
1845{
1846 struct nfs4_client *clp;
1847 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001848 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001849 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001850
1851 clp = alloc_client(name);
1852 if (clp == NULL)
1853 return NULL;
1854
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001855 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1856 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001857 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001858 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001859 }
Christoph Hellwig0162ac22014-09-24 12:19:19 +02001860 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Benny Halevy07cd4902010-05-12 00:13:41 +03001861 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001862 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001863 copy_verf(clp, verf);
1864 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001865 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001866 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001867 return clp;
1868}
1869
NeilBrownfd39ca92005-06-23 22:04:03 -07001870static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001871add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1872{
1873 struct rb_node **new = &(root->rb_node), *parent = NULL;
1874 struct nfs4_client *clp;
1875
1876 while (*new) {
1877 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1878 parent = *new;
1879
1880 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1881 new = &((*new)->rb_left);
1882 else
1883 new = &((*new)->rb_right);
1884 }
1885
1886 rb_link_node(&new_clp->cl_namenode, parent, new);
1887 rb_insert_color(&new_clp->cl_namenode, root);
1888}
1889
1890static struct nfs4_client *
1891find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1892{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001893 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001894 struct rb_node *node = root->rb_node;
1895 struct nfs4_client *clp;
1896
1897 while (node) {
1898 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1899 cmp = compare_blob(&clp->cl_name, name);
1900 if (cmp > 0)
1901 node = node->rb_left;
1902 else if (cmp < 0)
1903 node = node->rb_right;
1904 else
1905 return clp;
1906 }
1907 return NULL;
1908}
1909
1910static void
1911add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
1913 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001914 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Trond Myklebust0a880a22014-07-30 08:27:10 -04001916 lockdep_assert_held(&nn->client_lock);
1917
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001918 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001919 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001921 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001922 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
1924
NeilBrownfd39ca92005-06-23 22:04:03 -07001925static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926move_to_confirmed(struct nfs4_client *clp)
1927{
1928 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001929 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Trond Myklebust0a880a22014-07-30 08:27:10 -04001931 lockdep_assert_held(&nn->client_lock);
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001934 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001935 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001936 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001937 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001938 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939}
1940
1941static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001942find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
1944 struct nfs4_client *clp;
1945 unsigned int idhashval = clientid_hashval(clid->cl_id);
1946
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001947 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001948 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001949 if ((bool)clp->cl_minorversion != sessions)
1950 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001951 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
1955 return NULL;
1956}
1957
1958static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001959find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1960{
1961 struct list_head *tbl = nn->conf_id_hashtbl;
1962
Trond Myklebust0a880a22014-07-30 08:27:10 -04001963 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001964 return find_client_in_id_table(tbl, clid, sessions);
1965}
1966
1967static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001968find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001970 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Trond Myklebust0a880a22014-07-30 08:27:10 -04001972 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001973 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974}
1975
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001976static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001977{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001978 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001979}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001980
NeilBrown28ce6052005-06-23 22:03:56 -07001981static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001982find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001983{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001984 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001985 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001986}
1987
1988static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001989find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001990{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001991 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001992 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001993}
1994
NeilBrownfd39ca92005-06-23 22:04:03 -07001995static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001996gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001998 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001999 struct sockaddr *sa = svc_addr(rqstp);
2000 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002001 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Jeff Layton7077ecb2009-08-14 12:57:58 -04002003 /* Currently, we only support tcp and tcp6 for the callback channel */
2004 if (se->se_callback_netid_len == 3 &&
2005 !memcmp(se->se_callback_netid_val, "tcp", 3))
2006 expected_family = AF_INET;
2007 else if (se->se_callback_netid_len == 4 &&
2008 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2009 expected_family = AF_INET6;
2010 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 goto out_err;
2012
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002013 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002014 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002015 (struct sockaddr *)&conn->cb_addr,
2016 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002017
J. Bruce Fields07263f12010-05-31 19:09:40 -04002018 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002020
J. Bruce Fields07263f12010-05-31 19:09:40 -04002021 if (conn->cb_addr.ss_family == AF_INET6)
2022 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002023
J. Bruce Fields07263f12010-05-31 19:09:40 -04002024 conn->cb_prog = se->se_callback_prog;
2025 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002026 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 return;
2028out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002029 conn->cb_addr.ss_family = AF_UNSPEC;
2030 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002031 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 "will not receive delegations\n",
2033 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 return;
2036}
2037
Andy Adamson074fe892009-04-03 08:28:15 +03002038/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002039 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002040 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002041static void
Andy Adamson074fe892009-04-03 08:28:15 +03002042nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2043{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002044 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002045 struct nfsd4_slot *slot = resp->cstate.slot;
2046 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002047
Andy Adamson557ce262009-08-28 08:45:04 -04002048 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002049
Andy Adamson557ce262009-08-28 08:45:04 -04002050 slot->sl_opcnt = resp->opcnt;
2051 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002052
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002053 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002054 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002055 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002056 return;
2057 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002058 base = resp->cstate.data_offset;
2059 slot->sl_datalen = buf->len - base;
2060 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04002061 WARN("%s: sessions DRC could not cache compound\n", __func__);
2062 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002063}
2064
2065/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002066 * Encode the replay sequence operation from the slot values.
2067 * If cachethis is FALSE encode the uncached rep error on the next
2068 * operation which sets resp->p and increments resp->opcnt for
2069 * nfs4svc_encode_compoundres.
2070 *
Andy Adamson074fe892009-04-03 08:28:15 +03002071 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002072static __be32
2073nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2074 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002075{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002076 struct nfsd4_op *op;
2077 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002078
Andy Adamsonabfabf82009-07-23 19:02:18 -04002079 /* Encode the replayed sequence operation */
2080 op = &args->ops[resp->opcnt - 1];
2081 nfsd4_encode_operation(resp, op);
2082
2083 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002084 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002085 op = &args->ops[resp->opcnt++];
2086 op->status = nfserr_retry_uncached_rep;
2087 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002088 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002089 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002090}
2091
2092/*
Andy Adamson557ce262009-08-28 08:45:04 -04002093 * The sequence operation is not cached because we can use the slot and
2094 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002095 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002096static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002097nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2098 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002099{
Andy Adamson557ce262009-08-28 08:45:04 -04002100 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002101 struct xdr_stream *xdr = &resp->xdr;
2102 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002103 __be32 status;
2104
Andy Adamson557ce262009-08-28 08:45:04 -04002105 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002106
Andy Adamsonabfabf82009-07-23 19:02:18 -04002107 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002108 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002109 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002110
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002111 p = xdr_reserve_space(xdr, slot->sl_datalen);
2112 if (!p) {
2113 WARN_ON_ONCE(1);
2114 return nfserr_serverfault;
2115 }
2116 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2117 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002118
Andy Adamson557ce262009-08-28 08:45:04 -04002119 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002120 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002121}
2122
Andy Adamson0733d212009-04-03 08:28:01 +03002123/*
2124 * Set the exchange_id flags returned by the server.
2125 */
2126static void
2127nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2128{
2129 /* pNFS is not supported */
2130 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2131
2132 /* Referrals are supported, Migration is not. */
2133 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2134
2135 /* set the wire flags to return to client. */
2136 clid->flags = new->cl_exchange_flags;
2137}
2138
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002139static bool client_has_state(struct nfs4_client *clp)
2140{
2141 /*
2142 * Note clp->cl_openowners check isn't quite right: there's no
2143 * need to count owners without stateid's.
2144 *
2145 * Also note we should probably be using this in 4.0 case too.
2146 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002147 return !list_empty(&clp->cl_openowners)
2148 || !list_empty(&clp->cl_delegations)
2149 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002150}
2151
Al Virob37ad282006-10-19 23:28:59 -07002152__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002153nfsd4_exchange_id(struct svc_rqst *rqstp,
2154 struct nfsd4_compound_state *cstate,
2155 struct nfsd4_exchange_id *exid)
2156{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002157 struct nfs4_client *conf, *new;
2158 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002159 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002160 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002161 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002162 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002163 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002164 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002165
Jeff Layton363168b2009-08-14 12:57:56 -04002166 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002167 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002168 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002169 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002170 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002171
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002172 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002173 return nfserr_inval;
2174
Andy Adamson0733d212009-04-03 08:28:01 +03002175 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002176 case SP4_MACH_CRED:
2177 if (!svc_rqst_integrity_protected(rqstp))
2178 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002179 case SP4_NONE:
2180 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002181 default: /* checked by xdr code */
2182 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002183 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002184 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002185 }
2186
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002187 new = create_client(exid->clname, rqstp, &verf);
2188 if (new == NULL)
2189 return nfserr_jukebox;
2190
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002191 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002192 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002193 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002194 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002195 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2196 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2197
J. Bruce Fields136e6582012-05-12 20:37:23 -04002198 if (update) {
2199 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002200 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002201 goto out;
2202 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002203 if (!mach_creds_match(conf, rqstp)) {
2204 status = nfserr_wrong_cred;
2205 goto out;
2206 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002207 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002208 status = nfserr_perm;
2209 goto out;
2210 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002211 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002212 status = nfserr_not_same;
2213 goto out;
2214 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002215 /* case 6 */
2216 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002217 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002218 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002219 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002220 if (client_has_state(conf)) {
2221 status = nfserr_clid_inuse;
2222 goto out;
2223 }
Andy Adamson0733d212009-04-03 08:28:01 +03002224 goto out_new;
2225 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002226 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002227 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002228 goto out_copy;
2229 }
2230 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002231 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002232 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002233 }
2234
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002235 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002236 status = nfserr_noent;
2237 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002238 }
2239
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002240 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002241 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002242 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002243
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002244 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002245out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002246 if (conf) {
2247 status = mark_client_expired_locked(conf);
2248 if (status)
2249 goto out;
2250 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002251 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002252 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002253
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002254 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002255 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002256 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002257out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002258 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2259 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002260
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002261 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2262 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002263
2264 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002265 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002266 status = nfs_ok;
2267
2268out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002269 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002270 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002271 expire_client(new);
2272 if (unconf)
2273 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002274 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002275}
2276
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002277static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002278check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002279{
Andy Adamson88e588d2009-07-23 19:02:15 -04002280 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2281 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002282
2283 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002284 if (slot_inuse) {
2285 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002286 return nfserr_jukebox;
2287 else
2288 return nfserr_seq_misordered;
2289 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002290 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002291 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002292 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002293 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002294 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002295 return nfserr_seq_misordered;
2296}
2297
Andy Adamson49557cc2009-07-23 19:02:16 -04002298/*
2299 * Cache the create session result into the create session single DRC
2300 * slot cache by saving the xdr structure. sl_seqid has been set.
2301 * Do this for solo or embedded create session operations.
2302 */
2303static void
2304nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002305 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002306{
2307 slot->sl_status = nfserr;
2308 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2309}
2310
2311static __be32
2312nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2313 struct nfsd4_clid_slot *slot)
2314{
2315 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2316 return slot->sl_status;
2317}
2318
Mi Jinlong1b74c252011-07-14 14:50:17 +08002319#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2320 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2321 1 + /* MIN tag is length with zero, only length */ \
2322 3 + /* version, opcount, opcode */ \
2323 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2324 /* seqid, slotID, slotID, cache */ \
2325 4 ) * sizeof(__be32))
2326
2327#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2328 2 + /* verifier: AUTH_NULL, length 0 */\
2329 1 + /* status */ \
2330 1 + /* MIN tag is length with zero, only length */ \
2331 3 + /* opcount, opcode, opstatus*/ \
2332 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2333 /* seqid, slotID, slotID, slotID, status */ \
2334 5 ) * sizeof(__be32))
2335
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002336static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002337{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002338 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2339
J. Bruce Fields373cd402013-04-08 15:42:12 -04002340 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2341 return nfserr_toosmall;
2342 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2343 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002344 ca->headerpadsz = 0;
2345 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2346 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2347 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2348 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2349 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2350 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2351 /*
2352 * Note decreasing slot size below client's request may make it
2353 * difficult for client to function correctly, whereas
2354 * decreasing the number of slots will (just?) affect
2355 * performance. When short on memory we therefore prefer to
2356 * decrease number of slots instead of their size. Clients that
2357 * request larger slots than they need will get poor results:
2358 */
2359 ca->maxreqs = nfsd4_get_drc_mem(ca);
2360 if (!ca->maxreqs)
2361 return nfserr_jukebox;
2362
J. Bruce Fields373cd402013-04-08 15:42:12 -04002363 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002364}
2365
Kinglong Mee8a891632013-12-23 18:11:02 +08002366#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2367 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2368#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2369 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2370
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002371static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2372{
2373 ca->headerpadsz = 0;
2374
2375 /*
2376 * These RPC_MAX_HEADER macros are overkill, especially since we
2377 * don't even do gss on the backchannel yet. But this is still
2378 * less than 1k. Tighten up this estimate in the unlikely event
2379 * it turns out to be a problem for some client:
2380 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002381 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002382 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002383 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002384 return nfserr_toosmall;
2385 ca->maxresp_cached = 0;
2386 if (ca->maxops < 2)
2387 return nfserr_toosmall;
2388
2389 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002390}
2391
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002392static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2393{
2394 switch (cbs->flavor) {
2395 case RPC_AUTH_NULL:
2396 case RPC_AUTH_UNIX:
2397 return nfs_ok;
2398 default:
2399 /*
2400 * GSS case: the spec doesn't allow us to return this
2401 * error. But it also doesn't allow us not to support
2402 * GSS.
2403 * I'd rather this fail hard than return some error the
2404 * client might think it can already handle:
2405 */
2406 return nfserr_encr_alg_unsupp;
2407 }
2408}
2409
Andy Adamson069b6ad2009-04-03 08:27:58 +03002410__be32
2411nfsd4_create_session(struct svc_rqst *rqstp,
2412 struct nfsd4_compound_state *cstate,
2413 struct nfsd4_create_session *cr_ses)
2414{
Jeff Layton363168b2009-08-14 12:57:56 -04002415 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002416 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002417 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002418 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002419 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002420 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002421 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002422 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002423
Mi Jinlonga62573d2011-03-23 17:57:07 +08002424 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2425 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002426 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2427 if (status)
2428 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002429 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002430 if (status)
2431 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002432 status = check_backchannel_attrs(&cr_ses->back_channel);
2433 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002434 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002435 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002436 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002437 if (!new)
2438 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002439 conn = alloc_conn_from_crses(rqstp, cr_ses);
2440 if (!conn)
2441 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002442
Jeff Laytond20c11d2014-07-30 08:27:07 -04002443 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002444 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002445 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002446 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002447
2448 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002449 status = nfserr_wrong_cred;
2450 if (!mach_creds_match(conf, rqstp))
2451 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002452 cs_slot = &conf->cl_cs_slot;
2453 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002454 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002455 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002456 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002457 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002458 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002459 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002460 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002461 } else if (unconf) {
2462 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002463 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002464 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002465 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002466 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002467 status = nfserr_wrong_cred;
2468 if (!mach_creds_match(unconf, rqstp))
2469 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002470 cs_slot = &unconf->cl_cs_slot;
2471 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002472 if (status) {
2473 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002474 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002475 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002476 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002477 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002478 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002479 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002480 if (status) {
2481 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002482 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002483 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002484 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002485 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002486 conf = unconf;
2487 } else {
2488 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002489 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002490 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002491 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002492 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002493 * We do not support RDMA or persistent sessions
2494 */
2495 cr_ses->flags &= ~SESSION4_PERSIST;
2496 cr_ses->flags &= ~SESSION4_RDMA;
2497
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002498 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002499 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002500
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002501 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002502 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002503 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002504 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002505
Jeff Laytond20c11d2014-07-30 08:27:07 -04002506 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002507 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002508 spin_unlock(&nn->client_lock);
2509 /* init connection and backchannel */
2510 nfsd4_init_conn(rqstp, conn, new);
2511 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002512 if (old)
2513 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002514 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002515out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002516 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002517 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002518 if (old)
2519 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002520out_free_session:
2521 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002522out_release_drc_mem:
2523 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002524 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002525}
2526
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002527static __be32 nfsd4_map_bcts_dir(u32 *dir)
2528{
2529 switch (*dir) {
2530 case NFS4_CDFC4_FORE:
2531 case NFS4_CDFC4_BACK:
2532 return nfs_ok;
2533 case NFS4_CDFC4_FORE_OR_BOTH:
2534 case NFS4_CDFC4_BACK_OR_BOTH:
2535 *dir = NFS4_CDFC4_BOTH;
2536 return nfs_ok;
2537 };
2538 return nfserr_inval;
2539}
2540
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002541__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2542{
2543 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002544 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002545 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002546
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002547 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2548 if (status)
2549 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002550 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002551 session->se_cb_prog = bc->bc_cb_program;
2552 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002553 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002554
2555 nfsd4_probe_callback(session->se_client);
2556
2557 return nfs_ok;
2558}
2559
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002560__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2561 struct nfsd4_compound_state *cstate,
2562 struct nfsd4_bind_conn_to_session *bcts)
2563{
2564 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002565 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002566 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002567 struct net *net = SVC_NET(rqstp);
2568 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002569
2570 if (!nfsd4_last_compound_op(rqstp))
2571 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002572 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002573 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002574 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002575 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002576 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002577 status = nfserr_wrong_cred;
2578 if (!mach_creds_match(session->se_client, rqstp))
2579 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002580 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002581 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002582 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002583 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002584 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002585 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002586 goto out;
2587 nfsd4_init_conn(rqstp, conn, session);
2588 status = nfs_ok;
2589out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002590 nfsd4_put_session(session);
2591out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002592 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002593}
2594
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002595static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2596{
2597 if (!session)
2598 return 0;
2599 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2600}
2601
Andy Adamson069b6ad2009-04-03 08:27:58 +03002602__be32
2603nfsd4_destroy_session(struct svc_rqst *r,
2604 struct nfsd4_compound_state *cstate,
2605 struct nfsd4_destroy_session *sessionid)
2606{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002607 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002608 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002609 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002610 struct net *net = SVC_NET(r);
2611 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002612
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002613 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002614 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002615 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002616 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002617 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002618 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002619 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002620 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002621 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002622 if (!ses)
2623 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002624 status = nfserr_wrong_cred;
2625 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002626 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002627 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002628 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002629 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002630 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002631 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002632
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002633 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002634
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002635 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002636 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002637out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002638 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff092013-03-14 19:55:33 -04002639out_client_lock:
2640 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002641out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03002642 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002643}
2644
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002645static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002646{
2647 struct nfsd4_conn *c;
2648
2649 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002650 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002651 return c;
2652 }
2653 }
2654 return NULL;
2655}
2656
J. Bruce Fields57266a62013-04-13 14:27:29 -04002657static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002658{
2659 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002660 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002661 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002662 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002663
2664 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002665 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002666 if (c)
2667 goto out_free;
2668 status = nfserr_conn_not_bound_to_session;
2669 if (clp->cl_mach_cred)
2670 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002671 __nfsd4_hash_conn(new, ses);
2672 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002673 ret = nfsd4_register_conn(new);
2674 if (ret)
2675 /* oops; xprt is already down: */
2676 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002677 return nfs_ok;
2678out_free:
2679 spin_unlock(&clp->cl_lock);
2680 free_conn(new);
2681 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002682}
2683
Mi Jinlong868b89c2011-04-27 09:09:58 +08002684static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2685{
2686 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2687
2688 return args->opcnt > session->se_fchannel.maxops;
2689}
2690
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002691static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2692 struct nfsd4_session *session)
2693{
2694 struct xdr_buf *xb = &rqstp->rq_arg;
2695
2696 return xb->len > session->se_fchannel.maxreq_sz;
2697}
2698
Andy Adamson069b6ad2009-04-03 08:27:58 +03002699__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002700nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002701 struct nfsd4_compound_state *cstate,
2702 struct nfsd4_sequence *seq)
2703{
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002704 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002705 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002706 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002707 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002708 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002709 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002710 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002711 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002712 struct net *net = SVC_NET(rqstp);
2713 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002714
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002715 if (resp->opcnt != 1)
2716 return nfserr_sequence_pos;
2717
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002718 /*
2719 * Will be either used or freed by nfsd4_sequence_check_conn
2720 * below.
2721 */
2722 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2723 if (!conn)
2724 return nfserr_jukebox;
2725
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002726 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002727 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002728 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002729 goto out_no_session;
2730 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002731
Mi Jinlong868b89c2011-04-27 09:09:58 +08002732 status = nfserr_too_many_ops;
2733 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002734 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002735
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002736 status = nfserr_req_too_big;
2737 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002738 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002739
Benny Halevyb85d4c02009-04-03 08:28:08 +03002740 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002741 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002742 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002743
Andy Adamson557ce262009-08-28 08:45:04 -04002744 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002745 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2746
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002747 /* We do not negotiate the number of slots yet, so set the
2748 * maxslots to the session maxreqs which is used to encode
2749 * sr_highest_slotid and the sr_target_slot id to maxslots */
2750 seq->maxslots = session->se_fchannel.maxreqs;
2751
J. Bruce Fields73e79482012-02-13 16:39:00 -05002752 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2753 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002754 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002755 status = nfserr_seq_misordered;
2756 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002757 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002758 cstate->slot = slot;
2759 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002760 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002761 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002762 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002763 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002764 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002765 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002766 }
2767 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002768 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002769
J. Bruce Fields57266a62013-04-13 14:27:29 -04002770 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002771 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002772 if (status)
2773 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002774
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002775 buflen = (seq->cachethis) ?
2776 session->se_fchannel.maxresp_cached :
2777 session->se_fchannel.maxresp_sz;
2778 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2779 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002780 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002781 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002782 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002783
2784 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002785 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002786 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002787 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002788 if (seq->cachethis)
2789 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002790 else
2791 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002792
2793 cstate->slot = slot;
2794 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002795 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002796
Benny Halevyb85d4c02009-04-03 08:28:08 +03002797out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002798 switch (clp->cl_cb_state) {
2799 case NFSD4_CB_DOWN:
2800 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2801 break;
2802 case NFSD4_CB_FAULT:
2803 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2804 break;
2805 default:
2806 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002807 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002808 if (!list_empty(&clp->cl_revoked))
2809 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002810out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002811 if (conn)
2812 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002813 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002814 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002815out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002816 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002817 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002818}
2819
Trond Myklebustb6076642014-06-30 11:48:35 -04002820void
2821nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2822{
2823 struct nfsd4_compound_state *cs = &resp->cstate;
2824
2825 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002826 if (cs->status != nfserr_replay_cache) {
2827 nfsd4_store_cache_entry(resp);
2828 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2829 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002830 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002831 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002832 } else if (cs->clp)
2833 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002834}
2835
Mi Jinlong345c2842011-10-20 17:51:39 +08002836__be32
2837nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2838{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002839 struct nfs4_client *conf, *unconf;
2840 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002841 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002842 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002843
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002844 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002845 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002846 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002847 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002848
2849 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002850 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002851 status = nfserr_clientid_busy;
2852 goto out;
2853 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04002854 status = mark_client_expired_locked(conf);
2855 if (status)
2856 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002857 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08002858 } else if (unconf)
2859 clp = unconf;
2860 else {
2861 status = nfserr_stale_clientid;
2862 goto out;
2863 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002864 if (!mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002865 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002866 status = nfserr_wrong_cred;
2867 goto out;
2868 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002869 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002870out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002871 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002872 if (clp)
2873 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002874 return status;
2875}
2876
Andy Adamson069b6ad2009-04-03 08:27:58 +03002877__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002878nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2879{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002880 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002881
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002882 if (rc->rca_one_fs) {
2883 if (!cstate->current_fh.fh_dentry)
2884 return nfserr_nofilehandle;
2885 /*
2886 * We don't take advantage of the rca_one_fs case.
2887 * That's OK, it's optional, we can safely ignore it.
2888 */
2889 return nfs_ok;
2890 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002891
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002892 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002893 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2894 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002895 goto out;
2896
2897 status = nfserr_stale_clientid;
2898 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002899 /*
2900 * The following error isn't really legal.
2901 * But we only get here if the client just explicitly
2902 * destroyed the client. Surely it no longer cares what
2903 * error it gets back on an operation for the dead
2904 * client.
2905 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002906 goto out;
2907
2908 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002909 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002910out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002911 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002912}
2913
2914__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002915nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2916 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002918 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002920 struct nfs4_client *conf, *new;
2921 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002922 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002923 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2924
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002925 new = create_client(clname, rqstp, &clverifier);
2926 if (new == NULL)
2927 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002928 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002929 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002930 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002931 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002932 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002934 if (clp_used_exchangeid(conf))
2935 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002936 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002937 char addr_str[INET6_ADDRSTRLEN];
2938 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2939 sizeof(addr_str));
2940 dprintk("NFSD: setclientid: string in use by client "
2941 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 goto out;
2943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002945 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002946 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002947 unhash_client_locked(unconf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002948 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002949 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002951 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002952 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002953 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002954 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002955 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2957 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2958 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002959 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 status = nfs_ok;
2961out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002962 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002963 if (new)
2964 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002965 if (unconf)
2966 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 return status;
2968}
2969
2970
Al Virob37ad282006-10-19 23:28:59 -07002971__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002972nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2973 struct nfsd4_compound_state *cstate,
2974 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975{
NeilBrown21ab45a2005-06-23 22:04:14 -07002976 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002977 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2979 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002980 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002981 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002983 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07002985
Jeff Laytond20c11d2014-07-30 08:27:07 -04002986 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002987 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002988 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002989 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002990 * We try hard to give out unique clientid's, so if we get an
2991 * attempt to confirm the same clientid with a different cred,
2992 * there's a bug somewhere. Let's charitably assume it's our
2993 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002994 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002995 status = nfserr_serverfault;
2996 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2997 goto out;
2998 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2999 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003000 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003001 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3002 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003004 else /* case 4: client hasn't noticed we rebooted yet? */
3005 status = nfserr_stale_clientid;
3006 goto out;
3007 }
3008 status = nfs_ok;
3009 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003010 old = unconf;
3011 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003012 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003013 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003014 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3015 if (old) {
3016 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003017 if (status) {
3018 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003019 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003020 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003021 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003022 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003023 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003024 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003025 get_client_locked(conf);
3026 spin_unlock(&nn->client_lock);
3027 nfsd4_probe_callback(conf);
3028 spin_lock(&nn->client_lock);
3029 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003031 spin_unlock(&nn->client_lock);
3032 if (old)
3033 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 return status;
3035}
3036
J. Bruce Fields32513b42011-10-13 16:00:16 -04003037static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003039 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3040}
3041
3042/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003043static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3044 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003045{
Trond Myklebust950e0112014-06-30 11:48:31 -04003046 lockdep_assert_held(&state_lock);
3047
J. Bruce Fields32513b42011-10-13 16:00:16 -04003048 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003049 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003050 INIT_LIST_HEAD(&fp->fi_stateids);
3051 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003052 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003053 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003054 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003055 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003056 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3057 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton5b095e92014-10-23 08:01:02 -04003058 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059}
3060
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003061void
NeilBrowne60d4392005-06-23 22:03:01 -07003062nfsd4_free_slabs(void)
3063{
Christoph Hellwigabf11352014-05-21 07:43:03 -07003064 kmem_cache_destroy(openowner_slab);
3065 kmem_cache_destroy(lockowner_slab);
3066 kmem_cache_destroy(file_slab);
3067 kmem_cache_destroy(stateid_slab);
3068 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003069}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
Bryan Schumaker72083392011-11-01 15:24:59 -04003071int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072nfsd4_init_slabs(void)
3073{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003074 openowner_slab = kmem_cache_create("nfsd4_openowners",
3075 sizeof(struct nfs4_openowner), 0, 0, NULL);
3076 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003077 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003078 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003079 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003080 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003081 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003082 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003083 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003084 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003085 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003086 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003087 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003088 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003089 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003090 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003091 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003092 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003093 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003095
3096out_free_stateid_slab:
3097 kmem_cache_destroy(stateid_slab);
3098out_free_file_slab:
3099 kmem_cache_destroy(file_slab);
3100out_free_lockowner_slab:
3101 kmem_cache_destroy(lockowner_slab);
3102out_free_openowner_slab:
3103 kmem_cache_destroy(openowner_slab);
3104out:
NeilBrowne60d4392005-06-23 22:03:01 -07003105 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3106 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107}
3108
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003109static void init_nfs4_replay(struct nfs4_replay *rp)
3110{
3111 rp->rp_status = nfserr_serverfault;
3112 rp->rp_buflen = 0;
3113 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003114 mutex_init(&rp->rp_mutex);
3115}
3116
3117static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3118 struct nfs4_stateowner *so)
3119{
3120 if (!nfsd4_has_session(cstate)) {
3121 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003122 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04003123 }
3124}
3125
3126void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3127{
3128 struct nfs4_stateowner *so = cstate->replay_owner;
3129
3130 if (so != NULL) {
3131 cstate->replay_owner = NULL;
3132 mutex_unlock(&so->so_replay.rp_mutex);
3133 nfs4_put_stateowner(so);
3134 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003135}
3136
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003137static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138{
3139 struct nfs4_stateowner *sop;
3140
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003141 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003142 if (!sop)
3143 return NULL;
3144
3145 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3146 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003147 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003148 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003150 sop->so_owner.len = owner->len;
3151
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003152 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003153 sop->so_client = clp;
3154 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003155 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003156 return sop;
3157}
3158
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003159static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003160{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003161 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003162
Trond Myklebustd4f04892014-07-29 21:34:36 -04003163 list_add(&oo->oo_owner.so_strhash,
3164 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003165 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166}
3167
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003168static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3169{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003170 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003171}
3172
Jeff Layton6b180f02014-07-29 21:34:26 -04003173static void nfs4_free_openowner(struct nfs4_stateowner *so)
3174{
3175 struct nfs4_openowner *oo = openowner(so);
3176
3177 kmem_cache_free(openowner_slab, oo);
3178}
3179
3180static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003181 .so_unhash = nfs4_unhash_openowner,
3182 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003183};
3184
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003185static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003186alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003187 struct nfsd4_compound_state *cstate)
3188{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003189 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003190 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003192 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3193 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003195 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003196 oo->oo_owner.so_is_open_owner = 1;
3197 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003198 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003199 if (nfsd4_has_session(cstate))
3200 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003201 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003202 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003203 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003204 spin_lock(&clp->cl_lock);
3205 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003206 if (ret == NULL) {
3207 hash_openowner(oo, clp, strhashval);
3208 ret = oo;
3209 } else
3210 nfs4_free_openowner(&oo->oo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003211 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003212 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213}
3214
J. Bruce Fields996e0932011-10-17 11:14:48 -04003215static 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 -04003216 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003218 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003219 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003220 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003221 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07003222 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003223 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 stp->st_access_bmap = 0;
3225 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003226 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003227 spin_lock(&oo->oo_owner.so_client->cl_lock);
3228 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003229 spin_lock(&fp->fi_lock);
3230 list_add(&stp->st_perfile, &fp->fi_stateids);
3231 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003232 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233}
3234
Jeff Laytond3134b12014-07-29 21:34:32 -04003235/*
3236 * In the 4.0 case we need to keep the owners around a little while to handle
3237 * CLOSE replay. We still do need to release any file access that is held by
3238 * them before returning however.
3239 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003241move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242{
Jeff Layton217526e2014-07-30 08:27:11 -04003243 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003244 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3245 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3246 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003247
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003248 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
Jeff Laytonb401be222014-07-29 21:34:33 -04003250 /*
3251 * We know that we hold one reference via nfsd4_close, and another
3252 * "persistent" reference for the client. If the refcount is higher
3253 * than 2, then there are still calls in progress that are using this
3254 * stateid. We can't put the sc_file reference until they are finished.
3255 * Wait for the refcount to drop to 2. Since it has been unhashed,
3256 * there should be no danger of the refcount going back up again at
3257 * this point.
3258 */
3259 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3260
Jeff Laytond3134b12014-07-29 21:34:32 -04003261 release_all_access(s);
3262 if (s->st_stid.sc_file) {
3263 put_nfs4_file(s->st_stid.sc_file);
3264 s->st_stid.sc_file = NULL;
3265 }
Jeff Layton217526e2014-07-30 08:27:11 -04003266
3267 spin_lock(&nn->client_lock);
3268 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003269 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003270 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003271 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003272 spin_unlock(&nn->client_lock);
3273 if (last)
3274 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275}
3276
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277/* search file_hashtbl[] for file */
3278static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04003279find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 struct nfs4_file *fp;
3282
Jeff Layton5b095e92014-10-23 08:01:02 -04003283 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02003284 if (fh_match(&fp->fi_fhandle, fh)) {
Jeff Layton5b095e92014-10-23 08:01:02 -04003285 if (atomic_inc_not_zero(&fp->fi_ref))
3286 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 }
3289 return NULL;
3290}
3291
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02003292struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003293find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003294{
3295 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003296 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003297
Jeff Layton5b095e92014-10-23 08:01:02 -04003298 rcu_read_lock();
3299 fp = find_file_locked(fh, hashval);
3300 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04003301 return fp;
3302}
3303
3304static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003305find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003306{
3307 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003308 unsigned int hashval = file_hashval(fh);
3309
3310 rcu_read_lock();
3311 fp = find_file_locked(fh, hashval);
3312 rcu_read_unlock();
3313 if (fp)
3314 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04003315
3316 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04003317 fp = find_file_locked(fh, hashval);
3318 if (likely(fp == NULL)) {
3319 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04003320 fp = new;
3321 }
3322 spin_unlock(&state_lock);
3323
3324 return fp;
3325}
3326
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003327/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 * Called to check deny when READ with all zero stateid or
3329 * WRITE with all zero or all one stateid
3330 */
Al Virob37ad282006-10-19 23:28:59 -07003331static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3333{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003335 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
Trond Myklebustca943212014-07-23 16:17:39 -04003337 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003338 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003339 return ret;
3340 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003341 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003342 if (fp->fi_share_deny & deny_type)
3343 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003344 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003345 put_nfs4_file(fp);
3346 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347}
3348
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003349static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003351 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04003352 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3353 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003354
Trond Myklebust11b91642014-07-29 21:34:08 -04003355 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003356
Jeff Layton02e12152014-07-16 10:31:57 -04003357 /*
3358 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003359 * already holding inode->i_lock.
3360 *
Jeff Laytondff13992014-07-08 14:02:49 -04003361 * If the dl_time != 0, then we know that it has already been
3362 * queued for a lease break. Don't queue it again.
3363 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003364 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003365 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003366 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003367 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003368 }
Jeff Layton02e12152014-07-16 10:31:57 -04003369 spin_unlock(&state_lock);
3370}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003372static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3373 struct rpc_task *task)
3374{
3375 struct nfs4_delegation *dp = cb_to_delegation(cb);
3376
3377 switch (task->tk_status) {
3378 case 0:
3379 return 1;
3380 case -EBADHANDLE:
3381 case -NFS4ERR_BAD_STATEID:
3382 /*
3383 * Race: client probably got cb_recall before open reply
3384 * granting delegation.
3385 */
3386 if (dp->dl_retries--) {
3387 rpc_delay(task, 2 * HZ);
3388 return 0;
3389 }
3390 /*FALLTHRU*/
3391 default:
3392 return -1;
3393 }
3394}
3395
3396static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3397{
3398 struct nfs4_delegation *dp = cb_to_delegation(cb);
3399
3400 nfs4_put_stid(&dp->dl_stid);
3401}
3402
3403static struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3404 .prepare = nfsd4_cb_recall_prepare,
3405 .done = nfsd4_cb_recall_done,
3406 .release = nfsd4_cb_recall_release,
3407};
3408
Jeff Layton02e12152014-07-16 10:31:57 -04003409static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3410{
3411 /*
3412 * We're assuming the state code never drops its reference
3413 * without first removing the lease. Since we're in this lease
3414 * callback (and since the lease code is serialized by the kernel
3415 * lock) we know the server hasn't removed the lease yet, we know
3416 * it's safe to take a reference.
3417 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003418 atomic_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02003419 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003420}
3421
Jeff Layton1c8c6012013-06-21 08:58:15 -04003422/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003423static bool
3424nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003425{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003426 bool ret = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003427 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3428 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003429
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003430 if (!fp) {
3431 WARN(1, "(%p)->fl_owner NULL\n", fl);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003432 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003433 }
3434 if (fp->fi_had_conflict) {
3435 WARN(1, "duplicate break on %p\n", fp);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003436 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003437 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003438 /*
3439 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003440 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003441 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003442 */
3443 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Jeff Layton02e12152014-07-16 10:31:57 -04003445 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003446 fp->fi_had_conflict = true;
3447 /*
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003448 * If there are no delegations on the list, then return true
3449 * so that the lease code will go ahead and delete it.
Jeff Layton417c6622014-07-21 09:34:57 -04003450 */
3451 if (list_empty(&fp->fi_delegations))
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003452 ret = true;
Jeff Layton417c6622014-07-21 09:34:57 -04003453 else
3454 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3455 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003456 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003457 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458}
3459
Jeff Laytonc45198e2014-09-01 07:12:07 -04003460static int
Jeff Layton7448cc32015-01-16 15:05:57 -05003461nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3462 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463{
3464 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04003465 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 else
3467 return -EAGAIN;
3468}
3469
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003470static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003471 .lm_break = nfsd_break_deleg_cb,
3472 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473};
3474
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003475static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3476{
3477 if (nfsd4_has_session(cstate))
3478 return nfs_ok;
3479 if (seqid == so->so_seqid - 1)
3480 return nfserr_replay_me;
3481 if (seqid == so->so_seqid)
3482 return nfs_ok;
3483 return nfserr_bad_seqid;
3484}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
Jeff Layton4b24ca72014-06-30 11:48:44 -04003486static __be32 lookup_clientid(clientid_t *clid,
3487 struct nfsd4_compound_state *cstate,
3488 struct nfsd_net *nn)
3489{
3490 struct nfs4_client *found;
3491
3492 if (cstate->clp) {
3493 found = cstate->clp;
3494 if (!same_clid(&found->cl_clientid, clid))
3495 return nfserr_stale_clientid;
3496 return nfs_ok;
3497 }
3498
3499 if (STALE_CLIENTID(clid, nn))
3500 return nfserr_stale_clientid;
3501
3502 /*
3503 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3504 * cached already then we know this is for is for v4.0 and "sessions"
3505 * will be false.
3506 */
3507 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003508 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003509 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003510 if (!found) {
3511 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003512 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003513 }
3514 atomic_inc(&found->cl_refcount);
3515 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003516
3517 /* Cache the nfs4_client in cstate! */
3518 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003519 return nfs_ok;
3520}
3521
Al Virob37ad282006-10-19 23:28:59 -07003522__be32
Andy Adamson66689582009-04-03 08:28:45 +03003523nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003524 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 clientid_t *clientid = &open->op_clientid;
3527 struct nfs4_client *clp = NULL;
3528 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003529 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003530 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003532 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003534 /*
3535 * In case we need it later, after we've already created the
3536 * file and don't want to risk a further failure:
3537 */
3538 open->op_file = nfsd4_alloc_file();
3539 if (open->op_file == NULL)
3540 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
Trond Myklebust2d91e892014-06-30 11:48:46 -04003542 status = lookup_clientid(clientid, cstate, nn);
3543 if (status)
3544 return status;
3545 clp = cstate->clp;
3546
Trond Myklebustd4f04892014-07-29 21:34:36 -04003547 strhashval = ownerstr_hashval(&open->op_owner);
3548 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003549 open->op_openowner = oo;
3550 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003551 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003553 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003554 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003555 release_openowner(oo);
3556 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003557 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003558 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003559 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3560 if (status)
3561 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003562 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003563new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003564 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003565 if (oo == NULL)
3566 return nfserr_jukebox;
3567 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003568alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003569 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003570 if (!open->op_stp)
3571 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003572 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573}
3574
Al Virob37ad282006-10-19 23:28:59 -07003575static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003576nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3577{
3578 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3579 return nfserr_openmode;
3580 else
3581 return nfs_ok;
3582}
3583
Daniel Mackc47d8322011-05-16 16:38:14 +02003584static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003585{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003586 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3587}
3588
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003589static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003590{
3591 struct nfs4_stid *ret;
3592
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003593 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003594 if (!ret)
3595 return NULL;
3596 return delegstateid(ret);
3597}
3598
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003599static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3600{
3601 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3602 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3603}
3604
Al Virob37ad282006-10-19 23:28:59 -07003605static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003606nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003607 struct nfs4_delegation **dp)
3608{
3609 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003610 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003611 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003612
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003613 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3614 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003615 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003616 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003617 status = nfs4_check_delegmode(deleg, flags);
3618 if (status) {
3619 nfs4_put_stid(&deleg->dl_stid);
3620 goto out;
3621 }
3622 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003623out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003624 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003625 return nfs_ok;
3626 if (status)
3627 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003628 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003629 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003630}
3631
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003632static struct nfs4_ol_stateid *
3633nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003635 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003636 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
Trond Myklebust1d31a252014-07-10 14:07:25 -04003638 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003639 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 /* ignore lock owners */
3641 if (local->st_stateowner->so_is_open_owner == 0)
3642 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003643 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003644 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003645 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003646 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003649 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003650 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651}
3652
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003653static inline int nfs4_access_to_access(u32 nfs4_access)
3654{
3655 int flags = 0;
3656
3657 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3658 flags |= NFSD_MAY_READ;
3659 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3660 flags |= NFSD_MAY_WRITE;
3661 return flags;
3662}
3663
Al Virob37ad282006-10-19 23:28:59 -07003664static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3666 struct nfsd4_open *open)
3667{
3668 struct iattr iattr = {
3669 .ia_valid = ATTR_SIZE,
3670 .ia_size = 0,
3671 };
3672 if (!open->op_truncate)
3673 return 0;
3674 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003675 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3677}
3678
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003679static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003680 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3681 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003682{
Trond Myklebustde186432014-07-10 14:07:26 -04003683 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003684 __be32 status;
3685 int oflag = nfs4_access_to_omode(open->op_share_access);
3686 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003687 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003688
Trond Myklebustde186432014-07-10 14:07:26 -04003689 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003690
3691 /*
3692 * Are we trying to set a deny mode that would conflict with
3693 * current access?
3694 */
3695 status = nfs4_file_check_deny(fp, open->op_share_deny);
3696 if (status != nfs_ok) {
3697 spin_unlock(&fp->fi_lock);
3698 goto out;
3699 }
3700
3701 /* set access to the file */
3702 status = nfs4_file_get_access(fp, open->op_share_access);
3703 if (status != nfs_ok) {
3704 spin_unlock(&fp->fi_lock);
3705 goto out;
3706 }
3707
3708 /* Set access bits in stateid */
3709 old_access_bmap = stp->st_access_bmap;
3710 set_access(open->op_share_access, stp);
3711
3712 /* Set new deny mask */
3713 old_deny_bmap = stp->st_deny_bmap;
3714 set_deny(open->op_share_deny, stp);
3715 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3716
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003717 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003718 spin_unlock(&fp->fi_lock);
3719 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003720 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003721 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003722 spin_lock(&fp->fi_lock);
3723 if (!fp->fi_fds[oflag]) {
3724 fp->fi_fds[oflag] = filp;
3725 filp = NULL;
3726 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003727 }
Trond Myklebustde186432014-07-10 14:07:26 -04003728 spin_unlock(&fp->fi_lock);
3729 if (filp)
3730 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003731
3732 status = nfsd4_truncate(rqstp, cur_fh, open);
3733 if (status)
3734 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003735out:
3736 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003737out_put_access:
3738 stp->st_access_bmap = old_access_bmap;
3739 nfs4_file_put_access(fp, open->op_share_access);
3740 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3741 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003742}
3743
Al Virob37ad282006-10-19 23:28:59 -07003744static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003745nfs4_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 -07003746{
Al Virob37ad282006-10-19 23:28:59 -07003747 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003748 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003750 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003751 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003752
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003753 /* test and set deny mode */
3754 spin_lock(&fp->fi_lock);
3755 status = nfs4_file_check_deny(fp, open->op_share_deny);
3756 if (status == nfs_ok) {
3757 old_deny_bmap = stp->st_deny_bmap;
3758 set_deny(open->op_share_deny, stp);
3759 fp->fi_share_deny |=
3760 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3761 }
3762 spin_unlock(&fp->fi_lock);
3763
3764 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003767 status = nfsd4_truncate(rqstp, cur_fh, open);
3768 if (status != nfs_ok)
3769 reset_union_bmap_deny(old_deny_bmap, stp);
3770 return status;
3771}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003774nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003776 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777}
3778
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003779/* Should we give out recallable state?: */
3780static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3781{
3782 if (clp->cl_cb_state == NFSD4_CB_UP)
3783 return true;
3784 /*
3785 * In the sessions case, since we don't have to establish a
3786 * separate connection for callbacks, we assume it's OK
3787 * until we hear otherwise:
3788 */
3789 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3790}
3791
Jeff Laytond564fbe2014-07-16 10:31:58 -04003792static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003793{
3794 struct file_lock *fl;
3795
3796 fl = locks_alloc_lock();
3797 if (!fl)
3798 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003799 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003800 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003801 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3802 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003803 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003804 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003805 return fl;
3806}
3807
J. Bruce Fields99c41512013-05-21 16:21:25 -04003808static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003809{
Trond Myklebust11b91642014-07-29 21:34:08 -04003810 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton415b96c2014-08-22 12:26:36 -04003811 struct file_lock *fl, *ret;
Jeff Layton417c6622014-07-21 09:34:57 -04003812 struct file *filp;
3813 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003814
Jeff Laytond564fbe2014-07-16 10:31:58 -04003815 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003816 if (!fl)
3817 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003818 filp = find_readable_file(fp);
3819 if (!filp) {
3820 /* We should always have a readable file here */
3821 WARN_ON_ONCE(1);
3822 return -EBADF;
3823 }
3824 fl->fl_file = filp;
Jeff Layton415b96c2014-08-22 12:26:36 -04003825 ret = fl;
Jeff Laytone6f5c782014-08-22 10:40:25 -04003826 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003827 if (fl)
Jeff Layton417c6622014-07-21 09:34:57 -04003828 locks_free_lock(fl);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003829 if (status)
Jeff Layton417c6622014-07-21 09:34:57 -04003830 goto out_fput;
Benny Halevycdc97502014-05-30 09:09:30 -04003831 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003832 spin_lock(&fp->fi_lock);
3833 /* Did the lease get broken before we took the lock? */
3834 status = -EAGAIN;
3835 if (fp->fi_had_conflict)
3836 goto out_unlock;
3837 /* Race breaker */
Jeff Layton0c637be2014-08-22 12:05:43 -04003838 if (fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04003839 status = 0;
Jeff Layton67db1032014-12-13 09:11:40 -05003840 ++fp->fi_delegees;
Jeff Layton417c6622014-07-21 09:34:57 -04003841 hash_delegation_locked(dp, fp);
3842 goto out_unlock;
3843 }
Jeff Layton417c6622014-07-21 09:34:57 -04003844 fp->fi_deleg_file = filp;
Jeff Layton67db1032014-12-13 09:11:40 -05003845 fp->fi_delegees = 1;
Benny Halevy931ee562014-05-30 09:09:27 -04003846 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003847 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003848 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003849 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003850out_unlock:
3851 spin_unlock(&fp->fi_lock);
3852 spin_unlock(&state_lock);
3853out_fput:
3854 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003855 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003856}
3857
Jeff Layton0b266932014-07-25 07:34:25 -04003858static struct nfs4_delegation *
3859nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3860 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003861{
Jeff Layton0b266932014-07-25 07:34:25 -04003862 int status;
3863 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003864
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003865 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003866 return ERR_PTR(-EAGAIN);
3867
3868 dp = alloc_init_deleg(clp, fh);
3869 if (!dp)
3870 return ERR_PTR(-ENOMEM);
3871
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003872 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003873 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003874 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003875 dp->dl_stid.sc_file = fp;
Jeff Layton0c637be2014-08-22 12:05:43 -04003876 if (!fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04003877 spin_unlock(&fp->fi_lock);
3878 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003879 status = nfs4_setlease(dp);
3880 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003881 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003882 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003883 status = -EAGAIN;
3884 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003885 }
Jeff Layton67db1032014-12-13 09:11:40 -05003886 ++fp->fi_delegees;
Benny Halevy931ee562014-05-30 09:09:27 -04003887 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003888 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003889out_unlock:
3890 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003891 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003892out:
3893 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003894 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003895 return ERR_PTR(status);
3896 }
3897 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003898}
3899
Benny Halevy4aa89132012-02-21 14:16:44 -08003900static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3901{
3902 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3903 if (status == -EAGAIN)
3904 open->op_why_no_deleg = WND4_CONTENTION;
3905 else {
3906 open->op_why_no_deleg = WND4_RESOURCE;
3907 switch (open->op_deleg_want) {
3908 case NFS4_SHARE_WANT_READ_DELEG:
3909 case NFS4_SHARE_WANT_WRITE_DELEG:
3910 case NFS4_SHARE_WANT_ANY_DELEG:
3911 break;
3912 case NFS4_SHARE_WANT_CANCEL:
3913 open->op_why_no_deleg = WND4_CANCELLED;
3914 break;
3915 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003916 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003917 }
3918 }
3919}
3920
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921/*
3922 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003923 *
3924 * Note we don't support write delegations, and won't until the vfs has
3925 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 */
3927static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003928nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3929 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930{
3931 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003932 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3933 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003934 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003935 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003937 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003938 open->op_recall = 0;
3939 switch (open->op_claim_type) {
3940 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003941 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003942 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003943 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3944 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003945 break;
3946 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003947 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003948 /*
3949 * Let's not give out any delegations till everyone's
3950 * had the chance to reclaim theirs....
3951 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003952 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003953 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003954 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003955 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003956 /*
3957 * Also, if the file was opened for write or
3958 * create, there's a good chance the client's
3959 * about to write to it, resulting in an
3960 * immediate recall (since we don't support
3961 * write delegations):
3962 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003963 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003964 goto out_no_deleg;
3965 if (open->op_create == NFS4_OPEN_CREATE)
3966 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003967 break;
3968 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003969 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003970 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003971 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003972 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003973 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003975 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003977 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003978 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003979 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003980 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003981 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003982out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003983 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3984 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003985 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003986 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003987 open->op_recall = 1;
3988 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003989
3990 /* 4.1 client asking for a delegation? */
3991 if (open->op_deleg_want)
3992 nfsd4_open_deleg_none_ext(open, status);
3993 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994}
3995
Benny Halevye27f49c2012-02-21 14:16:54 -08003996static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3997 struct nfs4_delegation *dp)
3998{
3999 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4000 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4001 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4002 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4003 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4004 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4005 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4006 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4007 }
4008 /* Otherwise the client must be confused wanting a delegation
4009 * it already has, therefore we don't return
4010 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4011 */
4012}
4013
Al Virob37ad282006-10-19 23:28:59 -07004014__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4016{
Andy Adamson66689582009-04-03 08:28:45 +03004017 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004018 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004020 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004021 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004022 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 /*
4025 * Lookup file; if found, lookup stateid and check open request,
4026 * and check for delegations in the process of being recalled.
4027 * If not found, create the nfs4_file struct
4028 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004029 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004030 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004031 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004032 if (status)
4033 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04004034 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004036 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004037 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004038 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004039 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004040 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 }
4042
4043 /*
4044 * OPEN the file, or upgrade an existing OPEN.
4045 * If truncate fails, the OPEN fails.
4046 */
4047 if (stp) {
4048 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004049 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 if (status)
4051 goto out;
4052 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004053 stp = open->op_stp;
4054 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004055 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004056 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4057 if (status) {
4058 release_open_stateid(stp);
4059 goto out;
4060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004062 update_stateid(&stp->st_stid.sc_stateid);
4063 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064
Benny Halevyd24433c2012-02-16 20:57:17 +02004065 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004066 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4067 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4068 open->op_why_no_deleg = WND4_NOT_WANTED;
4069 goto nodeleg;
4070 }
4071 }
4072
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 /*
4074 * Attempt to hand out a delegation. No error return, because the
4075 * OPEN succeeds even if we fail.
4076 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004077 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004078nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 status = nfs_ok;
4080
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004081 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004082 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004084 /* 4.1 client trying to upgrade/downgrade delegation? */
4085 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004086 open->op_deleg_want)
4087 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004088
NeilBrown13cd2182005-06-23 22:03:10 -07004089 if (fp)
4090 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004091 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05004092 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 /*
4094 * To finish the open response, we just need to set the rflags.
4095 */
4096 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004097 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004098 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004100 if (dp)
4101 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004102 if (stp)
4103 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104
4105 return status;
4106}
4107
Jeff Layton58fb12e2014-07-29 21:34:27 -04004108void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4109 struct nfsd4_open *open, __be32 status)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004110{
4111 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004112 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004113
Jeff Laytond3134b12014-07-29 21:34:32 -04004114 nfsd4_cstate_assign_replay(cstate, so);
4115 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004116 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004117 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04004118 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004119 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004120 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004121}
4122
Al Virob37ad282006-10-19 23:28:59 -07004123__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004124nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4125 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126{
4127 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004128 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004129 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 dprintk("process_renew(%08x/%08x): starting\n",
4132 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004133 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004134 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004136 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004138 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004139 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 goto out;
4141 status = nfs_ok;
4142out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 return status;
4144}
4145
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004146void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004147nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004148{
Jeff Layton33dcc482012-04-10 11:08:48 -04004149 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004150 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004151 return;
4152
NeilBrowna76b4312005-06-23 22:04:01 -07004153 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004154 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004155 /*
4156 * If the server goes down again right now, an NFSv4
4157 * client will still be allowed to reclaim after it comes back up,
4158 * even if it hasn't yet had a chance to reclaim state this time.
4159 *
4160 */
Jeff Layton919b8042014-09-12 16:40:20 -04004161 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004162 /*
4163 * At this point, NFSv4 clients can still reclaim. But if the
4164 * server crashes, any that have not yet reclaimed will be out
4165 * of luck on the next boot.
4166 *
4167 * (NFSv4.1+ clients are considered to have reclaimed once they
4168 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4169 * have reclaimed after their first OPEN.)
4170 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004171 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004172 /*
4173 * At this point, and once lockd and/or any other containers
4174 * exit their grace period, further reclaims will fail and
4175 * regular locking can resume.
4176 */
NeilBrowna76b4312005-06-23 22:04:01 -07004177}
4178
NeilBrownfd39ca92005-06-23 22:04:03 -07004179static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004180nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181{
4182 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004183 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004185 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004187 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004188 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004191 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004192 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004193 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004194 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 clp = list_entry(pos, struct nfs4_client, cl_lru);
4196 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4197 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004198 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 break;
4200 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004201 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004202 dprintk("NFSD: client in use (clientid %08x)\n",
4203 clp->cl_clientid.cl_id);
4204 continue;
4205 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004206 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004207 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004208 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004209 list_for_each_safe(pos, next, &reaplist) {
4210 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 dprintk("NFSD: purging unused client (clientid %08x)\n",
4212 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004213 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 expire_client(clp);
4215 }
Benny Halevycdc97502014-05-30 09:09:30 -04004216 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004217 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004219 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4220 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004222 t = dp->dl_time - cutoff;
4223 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 break;
4225 }
Jeff Layton42690672014-07-25 07:34:20 -04004226 unhash_delegation_locked(dp);
4227 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 }
Benny Halevycdc97502014-05-30 09:09:30 -04004229 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004230 while (!list_empty(&reaplist)) {
4231 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4232 dl_recall_lru);
4233 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004234 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 }
Jeff Layton217526e2014-07-30 08:27:11 -04004236
4237 spin_lock(&nn->client_lock);
4238 while (!list_empty(&nn->close_lru)) {
4239 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4240 oo_close_lru);
4241 if (time_after((unsigned long)oo->oo_time,
4242 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004243 t = oo->oo_time - cutoff;
4244 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 break;
4246 }
Jeff Layton217526e2014-07-30 08:27:11 -04004247 list_del_init(&oo->oo_close_lru);
4248 stp = oo->oo_last_closed_stid;
4249 oo->oo_last_closed_stid = NULL;
4250 spin_unlock(&nn->client_lock);
4251 nfs4_put_stid(&stp->st_stid);
4252 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 }
Jeff Layton217526e2014-07-30 08:27:11 -04004254 spin_unlock(&nn->client_lock);
4255
Jeff Laytona832e7a2014-05-30 09:09:26 -04004256 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004257 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258}
4259
Harvey Harrisona254b242008-02-20 12:49:00 -08004260static struct workqueue_struct *laundry_wq;
4261static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004262
4263static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004264laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265{
4266 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004267 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4268 work);
4269 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4270 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004272 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004274 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275}
4276
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004277static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004278{
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02004279 if (!fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004280 return nfserr_bad_stateid;
4281 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282}
4283
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004285access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004287 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4288 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4289 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290}
4291
4292static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004293access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004295 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4296 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297}
4298
4299static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004300__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301{
Al Virob37ad282006-10-19 23:28:59 -07004302 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303
J. Bruce Fields02921912010-07-29 15:16:59 -04004304 /* For lock stateid's, we test the parent open, not the lock: */
4305 if (stp->st_openstp)
4306 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004307 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004309 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 goto out;
4311 status = nfs_ok;
4312out:
4313 return status;
4314}
4315
Al Virob37ad282006-10-19 23:28:59 -07004316static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004317check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004319 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004321 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004322 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 * conflicting state; so we must wait out the grace period. */
4324 return nfserr_grace;
4325 } else if (flags & WR_STATE)
4326 return nfs4_share_conflict(current_fh,
4327 NFS4_SHARE_DENY_WRITE);
4328 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4329 return nfs4_share_conflict(current_fh,
4330 NFS4_SHARE_DENY_READ);
4331}
4332
4333/*
4334 * Allow READ/WRITE during grace period on recovered state only for files
4335 * that are not able to provide mandatory locking.
4336 */
4337static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004338grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004340 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341}
4342
J. Bruce Fields81b82962011-08-23 11:03:29 -04004343/* Returns true iff a is later than b: */
4344static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4345{
Jim Rees1a9357f2013-05-17 17:33:00 -04004346 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004347}
4348
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004349static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004350{
Andy Adamson66689582009-04-03 08:28:45 +03004351 /*
4352 * When sessions are used the stateid generation number is ignored
4353 * when it is zero.
4354 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004355 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004356 return nfs_ok;
4357
4358 if (in->si_generation == ref->si_generation)
4359 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004360
J. Bruce Fields0836f582008-01-26 19:08:12 -05004361 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004362 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004363 return nfserr_bad_stateid;
4364 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004365 * However, we could see a stateid from the past, even from a
4366 * non-buggy client. For example, if the client sends a lock
4367 * while some IO is outstanding, the lock may bump si_generation
4368 * while the IO is still in flight. The client could avoid that
4369 * situation by waiting for responses on all the IO requests,
4370 * but better performance may result in retrying IO that
4371 * receives an old_stateid error if requests are rarely
4372 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004373 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004374 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004375}
4376
Chuck Lever7df302f2012-05-29 13:56:37 -04004377static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004378{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004379 struct nfs4_stid *s;
4380 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004381 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004382
Chuck Lever7df302f2012-05-29 13:56:37 -04004383 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004384 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004385 /* Client debugging aid. */
4386 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4387 char addr_str[INET6_ADDRSTRLEN];
4388 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4389 sizeof(addr_str));
4390 pr_warn_ratelimited("NFSD: client %s testing state ID "
4391 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004392 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004393 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004394 spin_lock(&cl->cl_lock);
4395 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004396 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004397 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004398 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004399 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004400 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004401 switch (s->sc_type) {
4402 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004403 status = nfs_ok;
4404 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004405 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004406 status = nfserr_deleg_revoked;
4407 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004408 case NFS4_OPEN_STID:
4409 case NFS4_LOCK_STID:
4410 ols = openlockstateid(s);
4411 if (ols->st_stateowner->so_is_open_owner
4412 && !(openowner(ols->st_stateowner)->oo_flags
4413 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004414 status = nfserr_bad_stateid;
4415 else
4416 status = nfs_ok;
4417 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004418 default:
4419 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004420 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004421 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004422 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004423 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004424 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004425out_unlock:
4426 spin_unlock(&cl->cl_lock);
4427 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004428}
4429
Christoph Hellwigcd61c522014-08-14 08:44:57 +02004430__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004431nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4432 stateid_t *stateid, unsigned char typemask,
4433 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004434{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004435 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004436
4437 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4438 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004439 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004440 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004441 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004442 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004443 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004444 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004445 if (status)
4446 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004447 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004448 if (!*s)
4449 return nfserr_bad_stateid;
4450 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004451}
4452
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453/*
4454* Checks for stateid operations
4455*/
Al Virob37ad282006-10-19 23:28:59 -07004456__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004457nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004458 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004460 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004461 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004463 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004465 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004466 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004467 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 if (filpp)
4470 *filpp = NULL;
4471
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004472 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 return nfserr_grace;
4474
4475 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004476 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004478 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004479 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004480 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004481 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04004482 return status;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004483 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4484 if (status)
4485 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004486 switch (s->sc_type) {
4487 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004488 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004489 status = nfs4_check_delegmode(dp, flags);
4490 if (status)
4491 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004492 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004493 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004494 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004495 WARN_ON_ONCE(1);
4496 status = nfserr_serverfault;
4497 goto out;
4498 }
Trond Myklebustde186432014-07-10 14:07:26 -04004499 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004500 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004501 break;
4502 case NFS4_OPEN_STID:
4503 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004504 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004505 status = nfs4_check_fh(current_fh, stp);
4506 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004508 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004509 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004511 status = nfs4_check_openmode(stp, flags);
4512 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004514 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004515 struct nfs4_file *fp = stp->st_stid.sc_file;
4516
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004517 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004518 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004519 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004520 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004521 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004522 break;
4523 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004524 status = nfserr_bad_stateid;
4525 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 }
4527 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004528 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004529 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004531 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 return status;
4533}
4534
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004535/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004536 * Test if the stateid is valid
4537 */
4538__be32
4539nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4540 struct nfsd4_test_stateid *test_stateid)
4541{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004542 struct nfsd4_test_stateid_id *stateid;
4543 struct nfs4_client *cl = cstate->session->se_client;
4544
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004545 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004546 stateid->ts_id_status =
4547 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004548
Bryan Schumaker17456802011-07-13 10:50:48 -04004549 return nfs_ok;
4550}
4551
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004552__be32
4553nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4554 struct nfsd4_free_stateid *free_stateid)
4555{
4556 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004557 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004558 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004559 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004560 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004561 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004562
Jeff Layton1af71cc2014-07-29 21:34:14 -04004563 spin_lock(&cl->cl_lock);
4564 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004565 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004566 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004567 switch (s->sc_type) {
4568 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004569 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004570 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004571 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004572 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4573 if (ret)
4574 break;
4575 ret = nfserr_locks_held;
4576 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004577 case NFS4_LOCK_STID:
4578 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4579 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004580 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004581 stp = openlockstateid(s);
4582 ret = nfserr_locks_held;
4583 if (check_for_locks(stp->st_stid.sc_file,
4584 lockowner(stp->st_stateowner)))
4585 break;
4586 unhash_lock_stateid(stp);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004587 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004588 nfs4_put_stid(s);
4589 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004590 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004591 case NFS4_REVOKED_DELEG_STID:
4592 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004593 list_del_init(&dp->dl_recall_lru);
4594 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004595 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004596 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004597 goto out;
4598 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004599 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004600out_unlock:
4601 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004602out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004603 return ret;
4604}
4605
NeilBrown4c4cd222005-07-07 17:59:27 -07004606static inline int
4607setlkflg (int type)
4608{
4609 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4610 RD_STATE : WR_STATE;
4611}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004613static __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 -04004614{
4615 struct svc_fh *current_fh = &cstate->current_fh;
4616 struct nfs4_stateowner *sop = stp->st_stateowner;
4617 __be32 status;
4618
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004619 status = nfsd4_check_seqid(cstate, sop, seqid);
4620 if (status)
4621 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004622 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4623 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004624 /*
4625 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004626 * nfserr_replay_me from the previous step, and
4627 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004628 */
4629 return nfserr_bad_stateid;
4630 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4631 if (status)
4632 return status;
4633 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004634}
4635
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636/*
4637 * Checks for sequence id mutating operations.
4638 */
Al Virob37ad282006-10-19 23:28:59 -07004639static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004640nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004641 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004642 struct nfs4_ol_stateid **stpp,
4643 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004645 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004646 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004647 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004649 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4650 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004651
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004653 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004654 if (status)
4655 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004656 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004657 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004659 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004660 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004661 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004662 else
4663 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004664 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004665}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004666
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004667static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4668 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004669{
4670 __be32 status;
4671 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004672 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004674 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004675 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004676 if (status)
4677 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004678 oo = openowner(stp->st_stateowner);
4679 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4680 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004681 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004682 }
4683 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004684 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685}
4686
Al Virob37ad282006-10-19 23:28:59 -07004687__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004688nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004689 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690{
Al Virob37ad282006-10-19 23:28:59 -07004691 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004692 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004693 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004694 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695
Al Viroa6a9f182013-09-16 10:57:01 -04004696 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4697 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004699 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004700 if (status)
4701 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004703 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004704 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004705 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004706 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004707 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004708 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004709 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004710 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004711 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004712 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004713 update_stateid(&stp->st_stid.sc_stateid);
4714 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004715 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004716 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004717
Jeff Layton2a4317c2012-03-21 16:42:43 -04004718 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004719 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004720put_stateid:
4721 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004723 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 return status;
4725}
4726
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004727static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004729 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004730 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004731 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004732 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004733}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004734
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004735static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4736{
4737 switch (to_access) {
4738 case NFS4_SHARE_ACCESS_READ:
4739 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4740 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4741 break;
4742 case NFS4_SHARE_ACCESS_WRITE:
4743 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4744 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4745 break;
4746 case NFS4_SHARE_ACCESS_BOTH:
4747 break;
4748 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004749 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 }
4751}
4752
Al Virob37ad282006-10-19 23:28:59 -07004753__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004754nfsd4_open_downgrade(struct svc_rqst *rqstp,
4755 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004756 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757{
Al Virob37ad282006-10-19 23:28:59 -07004758 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004759 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004760 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761
Al Viroa6a9f182013-09-16 10:57:01 -04004762 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4763 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004765 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004766 if (od->od_deleg_want)
4767 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4768 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004770 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004771 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004772 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004775 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004776 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004778 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004780 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004781 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004783 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004785 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786
Jeff Laytonce0fc432012-05-11 09:45:14 -04004787 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004789 update_stateid(&stp->st_stid.sc_stateid);
4790 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004792put_stateid:
4793 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004795 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 return status;
4797}
4798
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004799static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4800{
Trond Myklebustacf92952014-06-30 11:48:37 -04004801 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytond83017f2014-07-29 21:34:42 -04004802 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004803
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004804 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04004805 spin_lock(&clp->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04004806 unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004807
Jeff Laytond83017f2014-07-29 21:34:42 -04004808 if (clp->cl_minorversion) {
4809 put_ol_stateid_locked(s, &reaplist);
4810 spin_unlock(&clp->cl_lock);
4811 free_ol_stateid_reaplist(&reaplist);
4812 } else {
4813 spin_unlock(&clp->cl_lock);
4814 free_ol_stateid_reaplist(&reaplist);
Jeff Laytond3134b12014-07-29 21:34:32 -04004815 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04004816 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004817}
4818
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819/*
4820 * nfs4_unlock_state() called after encode
4821 */
Al Virob37ad282006-10-19 23:28:59 -07004822__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004823nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004824 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825{
Al Virob37ad282006-10-19 23:28:59 -07004826 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004827 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004828 struct net *net = SVC_NET(rqstp);
4829 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830
Al Viroa6a9f182013-09-16 10:57:01 -04004831 dprintk("NFSD: nfsd4_close on file %pd\n",
4832 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004834 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4835 &close->cl_stateid,
4836 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004837 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004838 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004839 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004841 update_stateid(&stp->st_stid.sc_stateid);
4842 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004844 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004845
4846 /* put reference from nfs4_preprocess_seqid_op */
4847 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 return status;
4850}
4851
Al Virob37ad282006-10-19 23:28:59 -07004852__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004853nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4854 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004856 struct nfs4_delegation *dp;
4857 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004858 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004859 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004860 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004862 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004863 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004865 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004866 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004867 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004868 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004869 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004870 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004871 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004872
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004873 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004874put_stateid:
4875 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876out:
4877 return status;
4878}
4879
4880
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882
Benny Halevy87df4de2008-12-15 19:42:03 +02004883static inline u64
4884end_offset(u64 start, u64 len)
4885{
4886 u64 end;
4887
4888 end = start + len;
4889 return end >= start ? end: NFS4_MAX_UINT64;
4890}
4891
4892/* last octet in a range */
4893static inline u64
4894last_byte_offset(u64 start, u64 len)
4895{
4896 u64 end;
4897
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004898 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004899 end = start + len;
4900 return end > start ? end - 1: NFS4_MAX_UINT64;
4901}
4902
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903/*
4904 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4905 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4906 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4907 * locking, this prevents us from being completely protocol-compliant. The
4908 * real solution to this problem is to start using unsigned file offsets in
4909 * the VFS, but this is a very deep change!
4910 */
4911static inline void
4912nfs4_transform_lock_offset(struct file_lock *lock)
4913{
4914 if (lock->fl_start < 0)
4915 lock->fl_start = OFFSET_MAX;
4916 if (lock->fl_end < 0)
4917 lock->fl_end = OFFSET_MAX;
4918}
4919
Kinglong Meeaef95832014-08-22 10:18:44 -04004920static void nfsd4_fl_get_owner(struct file_lock *dst, struct file_lock *src)
4921{
4922 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)src->fl_owner;
4923 dst->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lo->lo_owner));
4924}
4925
4926static void nfsd4_fl_put_owner(struct file_lock *fl)
4927{
4928 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
4929
4930 if (lo) {
4931 nfs4_put_stateowner(&lo->lo_owner);
4932 fl->fl_owner = NULL;
4933 }
4934}
4935
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004936static const struct lock_manager_operations nfsd_posix_mng_ops = {
Kinglong Meeaef95832014-08-22 10:18:44 -04004937 .lm_get_owner = nfsd4_fl_get_owner,
4938 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07004939};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940
4941static inline void
4942nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4943{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004944 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945
NeilBrownd5b90262006-04-10 22:55:22 -07004946 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004947 lo = (struct nfs4_lockowner *) fl->fl_owner;
4948 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4949 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004950 if (!deny->ld_owner.data)
4951 /* We just don't care that much */
4952 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004953 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4954 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004955 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004956nevermind:
4957 deny->ld_owner.len = 0;
4958 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004959 deny->ld_clientid.cl_boot = 0;
4960 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 }
4962 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004963 deny->ld_length = NFS4_MAX_UINT64;
4964 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4966 deny->ld_type = NFS4_READ_LT;
4967 if (fl->fl_type != F_RDLCK)
4968 deny->ld_type = NFS4_WRITE_LT;
4969}
4970
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004971static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004972find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004973 struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004975 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004976 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
Trond Myklebust0a880a22014-07-30 08:27:10 -04004978 lockdep_assert_held(&clp->cl_lock);
4979
Trond Myklebustd4f04892014-07-29 21:34:36 -04004980 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4981 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004982 if (so->so_is_open_owner)
4983 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04004984 if (same_owner_str(so, owner))
4985 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 }
4987 return NULL;
4988}
4989
Trond Myklebustc58c6612014-07-29 21:34:35 -04004990static struct nfs4_lockowner *
4991find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004992 struct nfs4_client *clp)
Trond Myklebustc58c6612014-07-29 21:34:35 -04004993{
4994 struct nfs4_lockowner *lo;
4995
Trond Myklebustd4f04892014-07-29 21:34:36 -04004996 spin_lock(&clp->cl_lock);
4997 lo = find_lockowner_str_locked(clid, owner, clp);
4998 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004999 return lo;
5000}
5001
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005002static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5003{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005004 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005005}
5006
Jeff Layton6b180f02014-07-29 21:34:26 -04005007static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5008{
5009 struct nfs4_lockowner *lo = lockowner(sop);
5010
5011 kmem_cache_free(lockowner_slab, lo);
5012}
5013
5014static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005015 .so_unhash = nfs4_unhash_lockowner,
5016 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005017};
5018
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019/*
5020 * Alloc a lock owner structure.
5021 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005022 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005024 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005026static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005027alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5028 struct nfs4_ol_stateid *open_stp,
5029 struct nfsd4_lock *lock)
5030{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005031 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005033 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5034 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005036 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5037 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005038 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005039 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005040 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005041 ret = find_lockowner_str_locked(&clp->cl_clientid,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005042 &lock->lk_new_owner, clp);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005043 if (ret == NULL) {
5044 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005045 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005046 ret = lo;
5047 } else
5048 nfs4_free_lockowner(&lo->lo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005049 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005050 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051}
5052
Jeff Layton356a95e2014-07-29 21:34:13 -04005053static void
5054init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5055 struct nfs4_file *fp, struct inode *inode,
5056 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005058 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059
Jeff Layton356a95e2014-07-29 21:34:13 -04005060 lockdep_assert_held(&clp->cl_lock);
5061
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005062 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005063 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005064 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07005065 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005066 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005067 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005068 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005070 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005071 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005072 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005073 spin_lock(&fp->fi_lock);
5074 list_add(&stp->st_perfile, &fp->fi_stateids);
5075 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076}
5077
Jeff Laytonc53530d2014-06-30 11:48:39 -04005078static struct nfs4_ol_stateid *
5079find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5080{
5081 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005082 struct nfs4_client *clp = lo->lo_owner.so_client;
5083
5084 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005085
5086 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005087 if (lst->st_stid.sc_file == fp) {
5088 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005089 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005090 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005091 }
5092 return NULL;
5093}
5094
Jeff Layton356a95e2014-07-29 21:34:13 -04005095static struct nfs4_ol_stateid *
5096find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5097 struct inode *inode, struct nfs4_ol_stateid *ost,
5098 bool *new)
5099{
5100 struct nfs4_stid *ns = NULL;
5101 struct nfs4_ol_stateid *lst;
5102 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5103 struct nfs4_client *clp = oo->oo_owner.so_client;
5104
5105 spin_lock(&clp->cl_lock);
5106 lst = find_lock_stateid(lo, fi);
5107 if (lst == NULL) {
5108 spin_unlock(&clp->cl_lock);
5109 ns = nfs4_alloc_stid(clp, stateid_slab);
5110 if (ns == NULL)
5111 return NULL;
5112
5113 spin_lock(&clp->cl_lock);
5114 lst = find_lock_stateid(lo, fi);
5115 if (likely(!lst)) {
5116 lst = openlockstateid(ns);
5117 init_lock_stateid(lst, lo, fi, inode, ost);
5118 ns = NULL;
5119 *new = true;
5120 }
5121 }
5122 spin_unlock(&clp->cl_lock);
5123 if (ns)
5124 nfs4_put_stid(ns);
5125 return lst;
5126}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005127
NeilBrownfd39ca92005-06-23 22:04:03 -07005128static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129check_lock_length(u64 offset, u64 length)
5130{
Benny Halevy87df4de2008-12-15 19:42:03 +02005131 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 LOFF_OVERFLOW(offset, length)));
5133}
5134
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005135static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005136{
Trond Myklebust11b91642014-07-29 21:34:08 -04005137 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005138
Jeff Layton7214e862014-07-10 14:07:33 -04005139 lockdep_assert_held(&fp->fi_lock);
5140
Jeff Layton82c5ff12012-05-11 09:45:13 -04005141 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005142 return;
Jeff Layton12659652014-07-10 14:07:28 -04005143 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005144 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005145}
5146
Jeff Layton356a95e2014-07-29 21:34:13 -04005147static __be32
5148lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5149 struct nfs4_ol_stateid *ost,
5150 struct nfsd4_lock *lock,
5151 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005152{
Jeff Layton5db1c032014-07-29 21:34:28 -04005153 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005154 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005155 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5156 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005157 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005158 struct nfs4_lockowner *lo;
5159 unsigned int strhashval;
5160
Trond Myklebustd4f04892014-07-29 21:34:36 -04005161 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005162 if (!lo) {
Trond Myklebustd4f04892014-07-29 21:34:36 -04005163 strhashval = ownerstr_hashval(&lock->v.new.owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005164 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5165 if (lo == NULL)
5166 return nfserr_jukebox;
5167 } else {
5168 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005169 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005170 if (!cstate->minorversion &&
5171 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005172 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005173 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005174
Jeff Layton356a95e2014-07-29 21:34:13 -04005175 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005176 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005177 status = nfserr_jukebox;
5178 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005179 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005180 status = nfs_ok;
5181out:
5182 nfs4_put_stateowner(&lo->lo_owner);
5183 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005184}
5185
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186/*
5187 * LOCK operation
5188 */
Al Virob37ad282006-10-19 23:28:59 -07005189__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005190nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005191 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005193 struct nfs4_openowner *open_sop = NULL;
5194 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005195 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005196 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005197 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005198 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005199 struct file_lock *file_lock = NULL;
5200 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005201 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005202 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005203 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005204 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005205 struct net *net = SVC_NET(rqstp);
5206 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207
5208 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5209 (long long) lock->lk_offset,
5210 (long long) lock->lk_length);
5211
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 if (check_lock_length(lock->lk_offset, lock->lk_length))
5213 return nfserr_inval;
5214
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005215 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005216 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005217 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5218 return status;
5219 }
5220
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005222 if (nfsd4_has_session(cstate))
5223 /* See rfc 5661 18.10.3: given clientid is ignored: */
5224 memcpy(&lock->v.new.clientid,
5225 &cstate->session->se_client->cl_clientid,
5226 sizeof(clientid_t));
5227
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005229 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005233 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 lock->lk_new_open_seqid,
5235 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005236 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005237 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005239 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005240 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005241 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005242 &lock->v.new.clientid))
5243 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005244 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005245 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005246 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005247 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005248 lock->lk_old_lock_seqid,
5249 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005250 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005251 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005252 if (status)
5253 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005254 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005256 lkflg = setlkflg(lock->lk_type);
5257 status = nfs4_check_openmode(lock_stp, lkflg);
5258 if (status)
5259 goto out;
5260
NeilBrown0dd395d2005-07-07 17:59:15 -07005261 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005262 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005263 goto out;
5264 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005265 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005266 goto out;
5267
Jeff Layton21179d82012-08-21 08:03:32 -04005268 file_lock = locks_alloc_lock();
5269 if (!file_lock) {
5270 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5271 status = nfserr_jukebox;
5272 goto out;
5273 }
5274
Trond Myklebust11b91642014-07-29 21:34:08 -04005275 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 switch (lock->lk_type) {
5277 case NFS4_READ_LT:
5278 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005279 spin_lock(&fp->fi_lock);
5280 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005281 if (filp)
5282 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005283 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005284 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005285 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 case NFS4_WRITE_LT:
5287 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005288 spin_lock(&fp->fi_lock);
5289 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005290 if (filp)
5291 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005292 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005293 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005294 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 default:
5296 status = nfserr_inval;
5297 goto out;
5298 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005299 if (!filp) {
5300 status = nfserr_openmode;
5301 goto out;
5302 }
Kinglong Meeaef95832014-08-22 10:18:44 -04005303
5304 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04005305 file_lock->fl_pid = current->tgid;
5306 file_lock->fl_file = filp;
5307 file_lock->fl_flags = FL_POSIX;
5308 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5309 file_lock->fl_start = lock->lk_offset;
5310 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5311 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312
Jeff Layton21179d82012-08-21 08:03:32 -04005313 conflock = locks_alloc_lock();
5314 if (!conflock) {
5315 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5316 status = nfserr_jukebox;
5317 goto out;
5318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319
Jeff Layton21179d82012-08-21 08:03:32 -04005320 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005321 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005323 update_stateid(&lock_stp->st_stid.sc_stateid);
5324 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005326 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005327 break;
5328 case (EAGAIN): /* conflock holds conflicting lock */
5329 status = nfserr_denied;
5330 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005331 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005332 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 case (EDEADLK):
5334 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005335 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005336 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005337 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005338 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005339 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341out:
Trond Myklebustde186432014-07-10 14:07:26 -04005342 if (filp)
5343 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005344 if (lock_stp) {
5345 /* Bump seqid manually if the 4.0 replay owner is openowner */
5346 if (cstate->replay_owner &&
5347 cstate->replay_owner != &lock_sop->lo_owner &&
5348 seqid_mutating_err(ntohl(status)))
5349 lock_sop->lo_owner.so_seqid++;
5350
5351 /*
5352 * If this is a new, never-before-used stateid, and we are
5353 * returning an error, then just go ahead and release it.
5354 */
5355 if (status && new)
5356 release_lock_stateid(lock_stp);
5357
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005358 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005359 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005360 if (open_stp)
5361 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005362 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005363 if (file_lock)
5364 locks_free_lock(file_lock);
5365 if (conflock)
5366 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 return status;
5368}
5369
5370/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005371 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5372 * so we do a temporary open here just to get an open file to pass to
5373 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5374 * inode operation.)
5375 */
Al Viro04da6e92012-04-13 00:00:04 -04005376static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005377{
5378 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005379 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5380 if (!err) {
5381 err = nfserrno(vfs_test_lock(file, lock));
5382 nfsd_close(file);
5383 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005384 return err;
5385}
5386
5387/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 * LOCKT operation
5389 */
Al Virob37ad282006-10-19 23:28:59 -07005390__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005391nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5392 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393{
Jeff Layton21179d82012-08-21 08:03:32 -04005394 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005395 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005396 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005397 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005399 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 return nfserr_grace;
5401
5402 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5403 return nfserr_inval;
5404
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005405 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005406 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005407 if (status)
5408 goto out;
5409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005411 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413
Jeff Layton21179d82012-08-21 08:03:32 -04005414 file_lock = locks_alloc_lock();
5415 if (!file_lock) {
5416 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5417 status = nfserr_jukebox;
5418 goto out;
5419 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005420
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 switch (lockt->lt_type) {
5422 case NFS4_READ_LT:
5423 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005424 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 break;
5426 case NFS4_WRITE_LT:
5427 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005428 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 break;
5430 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005431 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 status = nfserr_inval;
5433 goto out;
5434 }
5435
Trond Myklebustd4f04892014-07-29 21:34:36 -04005436 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5437 cstate->clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005438 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005439 file_lock->fl_owner = (fl_owner_t)lo;
5440 file_lock->fl_pid = current->tgid;
5441 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442
Jeff Layton21179d82012-08-21 08:03:32 -04005443 file_lock->fl_start = lockt->lt_offset;
5444 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445
Jeff Layton21179d82012-08-21 08:03:32 -04005446 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447
Jeff Layton21179d82012-08-21 08:03:32 -04005448 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005449 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005450 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005451
Jeff Layton21179d82012-08-21 08:03:32 -04005452 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005454 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 }
5456out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005457 if (lo)
5458 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04005459 if (file_lock)
5460 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 return status;
5462}
5463
Al Virob37ad282006-10-19 23:28:59 -07005464__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005465nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005466 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005468 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005470 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005471 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005472 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005473 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5474
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5476 (long long) locku->lu_offset,
5477 (long long) locku->lu_length);
5478
5479 if (check_lock_length(locku->lu_offset, locku->lu_length))
5480 return nfserr_inval;
5481
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005482 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005483 &locku->lu_stateid, NFS4_LOCK_STID,
5484 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005485 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005487 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005488 if (!filp) {
5489 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005490 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005491 }
Jeff Layton21179d82012-08-21 08:03:32 -04005492 file_lock = locks_alloc_lock();
5493 if (!file_lock) {
5494 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5495 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005496 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005497 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005498
Jeff Layton21179d82012-08-21 08:03:32 -04005499 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04005500 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04005501 file_lock->fl_pid = current->tgid;
5502 file_lock->fl_file = filp;
5503 file_lock->fl_flags = FL_POSIX;
5504 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5505 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506
Jeff Layton21179d82012-08-21 08:03:32 -04005507 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5508 locku->lu_length);
5509 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510
Jeff Layton21179d82012-08-21 08:03:32 -04005511 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005512 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005513 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 goto out_nfserr;
5515 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005516 update_stateid(&stp->st_stid.sc_stateid);
5517 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005518fput:
5519 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005520put_stateid:
5521 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005523 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005524 if (file_lock)
5525 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 return status;
5527
5528out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005529 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005530 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531}
5532
5533/*
5534 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005535 * true: locks held by lockowner
5536 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005538static bool
5539check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005541 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005542 int status = false;
5543 struct file *filp = find_any_file(fp);
5544 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005545 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005546
5547 if (!filp) {
5548 /* Any valid lock stateid should have some sort of access */
5549 WARN_ON_ONCE(1);
5550 return status;
5551 }
5552
5553 inode = file_inode(filp);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005554 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005556 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05005557 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005558 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
5559 if (fl->fl_owner == (fl_owner_t)lowner) {
5560 status = true;
5561 break;
5562 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005563 }
Jeff Layton6109c852015-01-16 15:05:57 -05005564 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 }
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005566 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 return status;
5568}
5569
Al Virob37ad282006-10-19 23:28:59 -07005570__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005571nfsd4_release_lockowner(struct svc_rqst *rqstp,
5572 struct nfsd4_compound_state *cstate,
5573 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574{
5575 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005576 struct nfs4_stateowner *sop;
5577 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005578 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005580 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005581 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005582 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005583 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584
5585 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5586 clid->cl_boot, clid->cl_id);
5587
Jeff Layton4b24ca72014-06-30 11:48:44 -04005588 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005589 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04005590 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005591
Trond Myklebustd4f04892014-07-29 21:34:36 -04005592 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005593 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005594 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005595 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005596 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005597
5598 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005599 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005600
Jeff Layton882e9d22014-07-29 21:34:37 -04005601 /* see if there are still any locks associated with it */
5602 lo = lockowner(sop);
5603 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5604 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5605 status = nfserr_locks_held;
5606 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04005607 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04005608 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005609 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005610
Kinglong Meeb5971af2014-08-22 10:18:43 -04005611 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04005612 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005613 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005614 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005615 if (lo)
5616 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 return status;
5618}
5619
5620static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005621alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622{
NeilBrowna55370a2005-06-23 22:03:52 -07005623 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624}
5625
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005626bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005627nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005628{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005629 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005630
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005631 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005632 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005633}
5634
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635/*
5636 * failure => all reset bets are off, nfserr_no_grace...
5637 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005638struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005639nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640{
5641 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005642 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643
NeilBrowna55370a2005-06-23 22:03:52 -07005644 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5645 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005646 if (crp) {
5647 strhashval = clientstr_hashval(name);
5648 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005649 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005650 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005651 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005652 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005653 }
5654 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655}
5656
Jeff Layton2a4317c2012-03-21 16:42:43 -04005657void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005658nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005659{
5660 list_del(&crp->cr_strhash);
5661 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005662 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005663}
5664
5665void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005666nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667{
5668 struct nfs4_client_reclaim *crp = NULL;
5669 int i;
5670
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005672 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5673 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005675 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 }
5677 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005678 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679}
5680
5681/*
5682 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005683struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005684nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685{
5686 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 struct nfs4_client_reclaim *crp = NULL;
5688
Jeff Layton278c9312012-11-12 15:00:52 -05005689 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690
Jeff Layton278c9312012-11-12 15:00:52 -05005691 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005692 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005693 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 return crp;
5695 }
5696 }
5697 return NULL;
5698}
5699
5700/*
5701* Called from OPEN. Look for clientid in reclaim list.
5702*/
Al Virob37ad282006-10-19 23:28:59 -07005703__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005704nfs4_check_open_reclaim(clientid_t *clid,
5705 struct nfsd4_compound_state *cstate,
5706 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005708 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005709
5710 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005711 status = lookup_clientid(clid, cstate, nn);
5712 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005713 return nfserr_reclaim_bad;
5714
Jeff Layton3b3e7b72014-09-12 16:40:22 -04005715 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
5716 return nfserr_no_grace;
5717
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005718 if (nfsd4_client_record_check(cstate->clp))
5719 return nfserr_reclaim_bad;
5720
5721 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722}
5723
Bryan Schumaker65178db2011-11-01 13:35:21 -04005724#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04005725static inline void
5726put_client(struct nfs4_client *clp)
5727{
5728 atomic_dec(&clp->cl_refcount);
5729}
5730
Jeff Layton285abde2014-07-30 08:27:24 -04005731static struct nfs4_client *
5732nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5733{
5734 struct nfs4_client *clp;
5735 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5736 nfsd_net_id);
5737
5738 if (!nfsd_netns_ready(nn))
5739 return NULL;
5740
5741 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5742 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5743 return clp;
5744 }
5745 return NULL;
5746}
5747
Jeff Layton7ec0e362014-07-30 08:27:17 -04005748u64
Jeff Layton285abde2014-07-30 08:27:24 -04005749nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04005750{
5751 struct nfs4_client *clp;
5752 u64 count = 0;
5753 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5754 nfsd_net_id);
5755 char buf[INET6_ADDRSTRLEN];
5756
5757 if (!nfsd_netns_ready(nn))
5758 return 0;
5759
5760 spin_lock(&nn->client_lock);
5761 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5762 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5763 pr_info("NFS Client: %s\n", buf);
5764 ++count;
5765 }
5766 spin_unlock(&nn->client_lock);
5767
5768 return count;
5769}
Bryan Schumaker65178db2011-11-01 13:35:21 -04005770
Jeff Laytona0926d12014-07-30 08:27:18 -04005771u64
Jeff Layton285abde2014-07-30 08:27:24 -04005772nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04005773{
5774 u64 count = 0;
5775 struct nfs4_client *clp;
5776 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5777 nfsd_net_id);
5778
5779 if (!nfsd_netns_ready(nn))
5780 return count;
5781
5782 spin_lock(&nn->client_lock);
5783 clp = nfsd_find_client(addr, addr_size);
5784 if (clp) {
5785 if (mark_client_expired_locked(clp) == nfs_ok)
5786 ++count;
5787 else
5788 clp = NULL;
5789 }
5790 spin_unlock(&nn->client_lock);
5791
5792 if (clp)
5793 expire_client(clp);
5794
5795 return count;
5796}
5797
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005798u64
Jeff Layton285abde2014-07-30 08:27:24 -04005799nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005800{
5801 u64 count = 0;
5802 struct nfs4_client *clp, *next;
5803 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5804 nfsd_net_id);
5805 LIST_HEAD(reaplist);
5806
5807 if (!nfsd_netns_ready(nn))
5808 return count;
5809
5810 spin_lock(&nn->client_lock);
5811 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5812 if (mark_client_expired_locked(clp) == nfs_ok) {
5813 list_add(&clp->cl_lru, &reaplist);
5814 if (max != 0 && ++count >= max)
5815 break;
5816 }
5817 }
5818 spin_unlock(&nn->client_lock);
5819
5820 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
5821 expire_client(clp);
5822
5823 return count;
5824}
5825
Bryan Schumaker184c1842012-11-29 11:40:44 -05005826static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5827 const char *type)
5828{
5829 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005830 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005831 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5832}
5833
Jeff Layton016200c2014-07-30 08:27:21 -04005834static void
5835nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
5836 struct list_head *collect)
5837{
5838 struct nfs4_client *clp = lst->st_stid.sc_client;
5839 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5840 nfsd_net_id);
5841
5842 if (!collect)
5843 return;
5844
5845 lockdep_assert_held(&nn->client_lock);
5846 atomic_inc(&clp->cl_refcount);
5847 list_add(&lst->st_locks, collect);
5848}
5849
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005850static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04005851 struct list_head *collect,
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005852 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005853{
5854 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005855 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005856 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005857 u64 count = 0;
5858
Jeff Layton016200c2014-07-30 08:27:21 -04005859 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005860 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005861 list_for_each_entry_safe(stp, st_next,
5862 &oop->oo_owner.so_stateids, st_perstateowner) {
5863 list_for_each_entry_safe(lst, lst_next,
5864 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04005865 if (func) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005866 func(lst);
Jeff Layton016200c2014-07-30 08:27:21 -04005867 nfsd_inject_add_lock_to_list(lst,
5868 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04005869 }
Jeff Layton016200c2014-07-30 08:27:21 -04005870 ++count;
5871 /*
5872 * Despite the fact that these functions deal
5873 * with 64-bit integers for "count", we must
5874 * ensure that it doesn't blow up the
5875 * clp->cl_refcount. Throw a warning if we
5876 * start to approach INT_MAX here.
5877 */
5878 WARN_ON_ONCE(count == (INT_MAX / 2));
5879 if (count == max)
5880 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005881 }
5882 }
5883 }
Jeff Layton016200c2014-07-30 08:27:21 -04005884out:
5885 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005886
5887 return count;
5888}
5889
Jeff Layton016200c2014-07-30 08:27:21 -04005890static u64
5891nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
5892 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05005893{
Jeff Layton016200c2014-07-30 08:27:21 -04005894 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005895}
5896
Jeff Layton016200c2014-07-30 08:27:21 -04005897static u64
5898nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05005899{
Jeff Layton016200c2014-07-30 08:27:21 -04005900 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005901 nfsd_print_count(clp, count, "locked files");
5902 return count;
5903}
5904
Jeff Layton016200c2014-07-30 08:27:21 -04005905u64
Jeff Layton285abde2014-07-30 08:27:24 -04005906nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04005907{
5908 struct nfs4_client *clp;
5909 u64 count = 0;
5910 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5911 nfsd_net_id);
5912
5913 if (!nfsd_netns_ready(nn))
5914 return 0;
5915
5916 spin_lock(&nn->client_lock);
5917 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5918 count += nfsd_print_client_locks(clp);
5919 spin_unlock(&nn->client_lock);
5920
5921 return count;
5922}
5923
5924static void
5925nfsd_reap_locks(struct list_head *reaplist)
5926{
5927 struct nfs4_client *clp;
5928 struct nfs4_ol_stateid *stp, *next;
5929
5930 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
5931 list_del_init(&stp->st_locks);
5932 clp = stp->st_stid.sc_client;
5933 nfs4_put_stid(&stp->st_stid);
5934 put_client(clp);
5935 }
5936}
5937
5938u64
Jeff Layton285abde2014-07-30 08:27:24 -04005939nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04005940{
5941 unsigned int count = 0;
5942 struct nfs4_client *clp;
5943 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5944 nfsd_net_id);
5945 LIST_HEAD(reaplist);
5946
5947 if (!nfsd_netns_ready(nn))
5948 return count;
5949
5950 spin_lock(&nn->client_lock);
5951 clp = nfsd_find_client(addr, addr_size);
5952 if (clp)
5953 count = nfsd_collect_client_locks(clp, &reaplist, 0);
5954 spin_unlock(&nn->client_lock);
5955 nfsd_reap_locks(&reaplist);
5956 return count;
5957}
5958
5959u64
Jeff Layton285abde2014-07-30 08:27:24 -04005960nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04005961{
5962 u64 count = 0;
5963 struct nfs4_client *clp;
5964 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5965 nfsd_net_id);
5966 LIST_HEAD(reaplist);
5967
5968 if (!nfsd_netns_ready(nn))
5969 return count;
5970
5971 spin_lock(&nn->client_lock);
5972 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5973 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
5974 if (max != 0 && count >= max)
5975 break;
5976 }
5977 spin_unlock(&nn->client_lock);
5978 nfsd_reap_locks(&reaplist);
5979 return count;
5980}
5981
Jeff Layton82e05ef2014-07-30 08:27:22 -04005982static u64
5983nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
5984 struct list_head *collect,
5985 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005986{
5987 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04005988 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5989 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005990 u64 count = 0;
5991
Jeff Layton82e05ef2014-07-30 08:27:22 -04005992 lockdep_assert_held(&nn->client_lock);
5993
5994 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005995 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04005996 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005997 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04005998 if (collect) {
5999 atomic_inc(&clp->cl_refcount);
6000 list_add(&oop->oo_perclient, collect);
6001 }
6002 }
6003 ++count;
6004 /*
6005 * Despite the fact that these functions deal with
6006 * 64-bit integers for "count", we must ensure that
6007 * it doesn't blow up the clp->cl_refcount. Throw a
6008 * warning if we start to approach INT_MAX here.
6009 */
6010 WARN_ON_ONCE(count == (INT_MAX / 2));
6011 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006012 break;
6013 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04006014 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006015
6016 return count;
6017}
6018
Jeff Layton82e05ef2014-07-30 08:27:22 -04006019static u64
6020nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006021{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006022 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6023
6024 nfsd_print_count(clp, count, "openowners");
6025 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006026}
6027
Jeff Layton82e05ef2014-07-30 08:27:22 -04006028static u64
6029nfsd_collect_client_openowners(struct nfs4_client *clp,
6030 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006031{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006032 return nfsd_foreach_client_openowner(clp, max, collect,
6033 unhash_openowner_locked);
6034}
6035
6036u64
Jeff Layton285abde2014-07-30 08:27:24 -04006037nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006038{
6039 struct nfs4_client *clp;
6040 u64 count = 0;
6041 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6042 nfsd_net_id);
6043
6044 if (!nfsd_netns_ready(nn))
6045 return 0;
6046
6047 spin_lock(&nn->client_lock);
6048 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6049 count += nfsd_print_client_openowners(clp);
6050 spin_unlock(&nn->client_lock);
6051
6052 return count;
6053}
6054
6055static void
6056nfsd_reap_openowners(struct list_head *reaplist)
6057{
6058 struct nfs4_client *clp;
6059 struct nfs4_openowner *oop, *next;
6060
6061 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6062 list_del_init(&oop->oo_perclient);
6063 clp = oop->oo_owner.so_client;
6064 release_openowner(oop);
6065 put_client(clp);
6066 }
6067}
6068
6069u64
Jeff Layton285abde2014-07-30 08:27:24 -04006070nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6071 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006072{
6073 unsigned int count = 0;
6074 struct nfs4_client *clp;
6075 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6076 nfsd_net_id);
6077 LIST_HEAD(reaplist);
6078
6079 if (!nfsd_netns_ready(nn))
6080 return count;
6081
6082 spin_lock(&nn->client_lock);
6083 clp = nfsd_find_client(addr, addr_size);
6084 if (clp)
6085 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6086 spin_unlock(&nn->client_lock);
6087 nfsd_reap_openowners(&reaplist);
6088 return count;
6089}
6090
6091u64
Jeff Layton285abde2014-07-30 08:27:24 -04006092nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006093{
6094 u64 count = 0;
6095 struct nfs4_client *clp;
6096 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6097 nfsd_net_id);
6098 LIST_HEAD(reaplist);
6099
6100 if (!nfsd_netns_ready(nn))
6101 return count;
6102
6103 spin_lock(&nn->client_lock);
6104 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6105 count += nfsd_collect_client_openowners(clp, &reaplist,
6106 max - count);
6107 if (max != 0 && count >= max)
6108 break;
6109 }
6110 spin_unlock(&nn->client_lock);
6111 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006112 return count;
6113}
6114
Bryan Schumaker269de302012-11-29 11:40:42 -05006115static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6116 struct list_head *victims)
6117{
6118 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006119 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6120 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006121 u64 count = 0;
6122
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006123 lockdep_assert_held(&nn->client_lock);
6124
6125 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006126 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006127 if (victims) {
6128 /*
6129 * It's not safe to mess with delegations that have a
6130 * non-zero dl_time. They might have already been broken
6131 * and could be processed by the laundromat outside of
6132 * the state_lock. Just leave them be.
6133 */
6134 if (dp->dl_time != 0)
6135 continue;
6136
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006137 atomic_inc(&clp->cl_refcount);
Jeff Layton42690672014-07-25 07:34:20 -04006138 unhash_delegation_locked(dp);
6139 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006140 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006141 ++count;
6142 /*
6143 * Despite the fact that these functions deal with
6144 * 64-bit integers for "count", we must ensure that
6145 * it doesn't blow up the clp->cl_refcount. Throw a
6146 * warning if we start to approach INT_MAX here.
6147 */
6148 WARN_ON_ONCE(count == (INT_MAX / 2));
6149 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006150 break;
6151 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006152 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006153 return count;
6154}
6155
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006156static u64
6157nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006158{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006159 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006160
6161 nfsd_print_count(clp, count, "delegations");
6162 return count;
6163}
6164
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006165u64
Jeff Layton285abde2014-07-30 08:27:24 -04006166nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006167{
6168 struct nfs4_client *clp;
6169 u64 count = 0;
6170 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6171 nfsd_net_id);
6172
6173 if (!nfsd_netns_ready(nn))
6174 return 0;
6175
6176 spin_lock(&nn->client_lock);
6177 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6178 count += nfsd_print_client_delegations(clp);
6179 spin_unlock(&nn->client_lock);
6180
6181 return count;
6182}
6183
6184static void
6185nfsd_forget_delegations(struct list_head *reaplist)
6186{
6187 struct nfs4_client *clp;
6188 struct nfs4_delegation *dp, *next;
6189
6190 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6191 list_del_init(&dp->dl_recall_lru);
6192 clp = dp->dl_stid.sc_client;
6193 revoke_delegation(dp);
6194 put_client(clp);
6195 }
6196}
6197
6198u64
Jeff Layton285abde2014-07-30 08:27:24 -04006199nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6200 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006201{
6202 u64 count = 0;
6203 struct nfs4_client *clp;
6204 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6205 nfsd_net_id);
6206 LIST_HEAD(reaplist);
6207
6208 if (!nfsd_netns_ready(nn))
6209 return count;
6210
6211 spin_lock(&nn->client_lock);
6212 clp = nfsd_find_client(addr, addr_size);
6213 if (clp)
6214 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6215 spin_unlock(&nn->client_lock);
6216
6217 nfsd_forget_delegations(&reaplist);
6218 return count;
6219}
6220
6221u64
Jeff Layton285abde2014-07-30 08:27:24 -04006222nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006223{
6224 u64 count = 0;
6225 struct nfs4_client *clp;
6226 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6227 nfsd_net_id);
6228 LIST_HEAD(reaplist);
6229
6230 if (!nfsd_netns_ready(nn))
6231 return count;
6232
6233 spin_lock(&nn->client_lock);
6234 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6235 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6236 if (max != 0 && count >= max)
6237 break;
6238 }
6239 spin_unlock(&nn->client_lock);
6240 nfsd_forget_delegations(&reaplist);
6241 return count;
6242}
6243
6244static void
6245nfsd_recall_delegations(struct list_head *reaplist)
6246{
6247 struct nfs4_client *clp;
6248 struct nfs4_delegation *dp, *next;
6249
6250 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6251 list_del_init(&dp->dl_recall_lru);
6252 clp = dp->dl_stid.sc_client;
6253 /*
6254 * We skipped all entries that had a zero dl_time before,
6255 * so we can now reset the dl_time back to 0. If a delegation
6256 * break comes in now, then it won't make any difference since
6257 * we're recalling it either way.
6258 */
6259 spin_lock(&state_lock);
6260 dp->dl_time = 0;
6261 spin_unlock(&state_lock);
6262 nfsd_break_one_deleg(dp);
6263 put_client(clp);
6264 }
6265}
6266
6267u64
Jeff Layton285abde2014-07-30 08:27:24 -04006268nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006269 size_t addr_size)
6270{
6271 u64 count = 0;
6272 struct nfs4_client *clp;
6273 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6274 nfsd_net_id);
6275 LIST_HEAD(reaplist);
6276
6277 if (!nfsd_netns_ready(nn))
6278 return count;
6279
6280 spin_lock(&nn->client_lock);
6281 clp = nfsd_find_client(addr, addr_size);
6282 if (clp)
6283 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6284 spin_unlock(&nn->client_lock);
6285
6286 nfsd_recall_delegations(&reaplist);
6287 return count;
6288}
6289
6290u64
Jeff Layton285abde2014-07-30 08:27:24 -04006291nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006292{
6293 u64 count = 0;
6294 struct nfs4_client *clp, *next;
6295 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6296 nfsd_net_id);
6297 LIST_HEAD(reaplist);
6298
6299 if (!nfsd_netns_ready(nn))
6300 return count;
6301
6302 spin_lock(&nn->client_lock);
6303 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6304 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6305 if (max != 0 && ++count >= max)
6306 break;
6307 }
6308 spin_unlock(&nn->client_lock);
6309 nfsd_recall_delegations(&reaplist);
6310 return count;
6311}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006312#endif /* CONFIG_NFSD_FAULT_INJECTION */
6313
Meelap Shahc2f1a552007-07-17 04:04:39 -07006314/*
6315 * Since the lifetime of a delegation isn't limited to that of an open, a
6316 * client may quite reasonably hang on to a delegation as long as it has
6317 * the inode cached. This becomes an obvious problem the first time a
6318 * client's inode cache approaches the size of the server's total memory.
6319 *
6320 * For now we avoid this problem by imposing a hard limit on the number
6321 * of delegations, which varies according to the server's memory size.
6322 */
6323static void
6324set_max_delegations(void)
6325{
6326 /*
6327 * Allow at most 4 delegations per megabyte of RAM. Quick
6328 * estimates suggest that in the worst case (where every delegation
6329 * is for a different inode), a delegation could take about 1.5K,
6330 * giving a worst case usage of about 6% of memory.
6331 */
6332 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6333}
6334
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006335static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006336{
6337 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6338 int i;
6339
6340 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6341 CLIENT_HASH_SIZE, GFP_KERNEL);
6342 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006343 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006344 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6345 CLIENT_HASH_SIZE, GFP_KERNEL);
6346 if (!nn->unconf_id_hashtbl)
6347 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006348 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6349 SESSION_HASH_SIZE, GFP_KERNEL);
6350 if (!nn->sessionid_hashtbl)
6351 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006352
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006353 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006354 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006355 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006356 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006357 for (i = 0; i < SESSION_HASH_SIZE; i++)
6358 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006359 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006360 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03006361 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03006362 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006363 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03006364 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006365
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006366 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006367 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006368
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006369 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006370
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006371err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03006372 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006373err_unconf_id:
6374 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006375err:
6376 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006377}
6378
6379static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006380nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006381{
6382 int i;
6383 struct nfs4_client *clp = NULL;
6384 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6385
6386 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6387 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6388 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6389 destroy_client(clp);
6390 }
6391 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006392
Kinglong Mee2b905632014-03-26 22:09:30 +08006393 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6394 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6395 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6396 destroy_client(clp);
6397 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006398 }
6399
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006400 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006401 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006402 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006403 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006404}
6405
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006406int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006407nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006408{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006409 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006410 int ret;
6411
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006412 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006413 if (ret)
6414 return ret;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006415 nn->boot_time = get_seconds();
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006416 nn->grace_ended = false;
Jeff Laytond4318ac2014-09-12 16:40:21 -04006417 locks_start_grace(net, &nn->nfsd4_manager);
6418 nfsd4_client_tracking_init(net);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006419 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006420 nn->nfsd4_grace, net);
6421 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006422 return 0;
6423}
6424
6425/* initialization to perform when the nfsd service is started: */
6426
6427int
6428nfs4_state_start(void)
6429{
6430 int ret;
6431
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006432 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006433 if (ret)
6434 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07006435 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05006436 if (laundry_wq == NULL) {
6437 ret = -ENOMEM;
6438 goto out_recovery;
6439 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006440 ret = nfsd4_create_callback_queue();
6441 if (ret)
6442 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006443
Meelap Shahc2f1a552007-07-17 04:04:39 -07006444 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006445
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006446 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006447
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006448out_free_laundry:
6449 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006450out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006451 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452}
6453
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006454void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006455nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006459 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006461 cancel_delayed_work_sync(&nn->laundromat_work);
6462 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006463
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04006465 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006466 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006468 unhash_delegation_locked(dp);
6469 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470 }
Benny Halevycdc97502014-05-30 09:09:30 -04006471 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472 list_for_each_safe(pos, next, &reaplist) {
6473 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006474 list_del_init(&dp->dl_recall_lru);
Jeff Laytonafbda402014-08-09 10:22:41 -04006475 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04006476 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477 }
6478
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006479 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006480 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481}
6482
6483void
6484nfs4_state_shutdown(void)
6485{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006486 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006487 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006489
6490static void
6491get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6492{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006493 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6494 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006495}
6496
6497static void
6498put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6499{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006500 if (cstate->minorversion) {
6501 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6502 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6503 }
6504}
6505
6506void
6507clear_current_stateid(struct nfsd4_compound_state *cstate)
6508{
6509 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006510}
6511
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006512/*
6513 * functions to set current state id
6514 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006515void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006516nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6517{
6518 put_stateid(cstate, &odp->od_stateid);
6519}
6520
6521void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006522nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6523{
6524 put_stateid(cstate, &open->op_stateid);
6525}
6526
6527void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006528nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6529{
6530 put_stateid(cstate, &close->cl_stateid);
6531}
6532
6533void
6534nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6535{
6536 put_stateid(cstate, &lock->lk_resp_stateid);
6537}
6538
6539/*
6540 * functions to consume current state id
6541 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006542
6543void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006544nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6545{
6546 get_stateid(cstate, &odp->od_stateid);
6547}
6548
6549void
6550nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6551{
6552 get_stateid(cstate, &drp->dr_stateid);
6553}
6554
6555void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006556nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6557{
6558 get_stateid(cstate, &fsp->fr_stateid);
6559}
6560
6561void
6562nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6563{
6564 get_stateid(cstate, &setattr->sa_stateid);
6565}
6566
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006567void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006568nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6569{
6570 get_stateid(cstate, &close->cl_stateid);
6571}
6572
6573void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006574nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006575{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006576 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006577}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006578
6579void
6580nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6581{
6582 get_stateid(cstate, &read->rd_stateid);
6583}
6584
6585void
6586nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6587{
6588 get_stateid(cstate, &write->wr_stateid);
6589}