blob: fb8c667250352d7f4cb493e0f02b94324f24daec [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"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020051#include "pnfs.h"
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define NFSDDBG_FACILITY NFSDDBG_PROC
54
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050055#define all_ones {{~0,~0},~0}
56static const stateid_t one_stateid = {
57 .si_generation = ~0,
58 .si_opaque = all_ones,
59};
60static const stateid_t zero_stateid = {
61 /* all fields zero */
62};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010063static const stateid_t currentstateid = {
64 .si_generation = 1,
65};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050066
Andy Adamsonec6b5d72009-04-03 08:28:28 +030067static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070068
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050069#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
70#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010071#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040074static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040075static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
J. Bruce Fields8b671b82009-02-22 14:51:34 -080077/* Locking: */
78
J. Bruce Fields8b671b82009-02-22 14:51:34 -080079/*
80 * Currently used for the del_recall_lru and file hash table. In an
81 * effort to decrease the scope of the client_mutex, this spinlock may
82 * eventually cover more:
83 */
Benny Halevycdc97502014-05-30 09:09:30 -040084static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080085
Jeff Laytonb401be222014-07-29 21:34:33 -040086/*
87 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
88 * the refcount on the open stateid to drop.
89 */
90static DECLARE_WAIT_QUEUE_HEAD(close_wq);
91
Christoph Hellwigabf11352014-05-21 07:43:03 -070092static struct kmem_cache *openowner_slab;
93static struct kmem_cache *lockowner_slab;
94static struct kmem_cache *file_slab;
95static struct kmem_cache *stateid_slab;
96static struct kmem_cache *deleg_slab;
NeilBrowne60d4392005-06-23 22:03:01 -070097
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -040098static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -080099
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200100static struct nfsd4_callback_ops nfsd4_cb_recall_ops;
101
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400102static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800103{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400104 return ses->se_flags & NFS4_SESSION_DEAD;
105}
106
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400107static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
108{
109 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400110 return nfserr_jukebox;
111 ses->se_flags |= NFS4_SESSION_DEAD;
112 return nfs_ok;
113}
114
J. Bruce Fields221a6872013-04-01 22:23:49 -0400115static bool is_client_expired(struct nfs4_client *clp)
116{
117 return clp->cl_time == 0;
118}
119
J. Bruce Fields221a6872013-04-01 22:23:49 -0400120static __be32 get_client_locked(struct nfs4_client *clp)
121{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400122 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
123
124 lockdep_assert_held(&nn->client_lock);
125
J. Bruce Fields221a6872013-04-01 22:23:49 -0400126 if (is_client_expired(clp))
127 return nfserr_expired;
128 atomic_inc(&clp->cl_refcount);
129 return nfs_ok;
130}
131
132/* must be called under the client_lock */
133static inline void
134renew_client_locked(struct nfs4_client *clp)
135{
136 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
137
138 if (is_client_expired(clp)) {
139 WARN_ON(1);
140 printk("%s: client (clientid %08x/%08x) already expired\n",
141 __func__,
142 clp->cl_clientid.cl_boot,
143 clp->cl_clientid.cl_id);
144 return;
145 }
146
147 dprintk("renewing client (clientid %08x/%08x)\n",
148 clp->cl_clientid.cl_boot,
149 clp->cl_clientid.cl_id);
150 list_move_tail(&clp->cl_lru, &nn->client_lru);
151 clp->cl_time = get_seconds();
152}
153
Fengguang Wuba138432013-04-16 22:14:15 -0400154static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400155{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400156 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
157
158 lockdep_assert_held(&nn->client_lock);
159
J. Bruce Fields221a6872013-04-01 22:23:49 -0400160 if (!atomic_dec_and_test(&clp->cl_refcount))
161 return;
162 if (!is_client_expired(clp))
163 renew_client_locked(clp);
164}
165
Jeff Layton4b24ca72014-06-30 11:48:44 -0400166static void put_client_renew(struct nfs4_client *clp)
167{
168 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
169
Jeff Laytond6c249b2014-07-08 14:02:50 -0400170 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
171 return;
172 if (!is_client_expired(clp))
173 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400174 spin_unlock(&nn->client_lock);
175}
176
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400177static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
178{
179 __be32 status;
180
181 if (is_session_dead(ses))
182 return nfserr_badsession;
183 status = get_client_locked(ses->se_client);
184 if (status)
185 return status;
186 atomic_inc(&ses->se_ref);
187 return nfs_ok;
188}
189
190static void nfsd4_put_session_locked(struct nfsd4_session *ses)
191{
192 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400193 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
194
195 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400196
197 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
198 free_session(ses);
199 put_client_renew_locked(clp);
200}
201
202static void nfsd4_put_session(struct nfsd4_session *ses)
203{
204 struct nfs4_client *clp = ses->se_client;
205 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
206
207 spin_lock(&nn->client_lock);
208 nfsd4_put_session_locked(ses);
209 spin_unlock(&nn->client_lock);
210}
211
Kinglong Meeb5971af2014-08-22 10:18:43 -0400212static inline struct nfs4_stateowner *
213nfs4_get_stateowner(struct nfs4_stateowner *sop)
214{
215 atomic_inc(&sop->so_count);
216 return sop;
217}
218
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400219static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400220same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400221{
222 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400223 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400224}
225
226static struct nfs4_openowner *
227find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400228 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400229{
230 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400231
Trond Myklebustd4f04892014-07-29 21:34:36 -0400232 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400233
Trond Myklebustd4f04892014-07-29 21:34:36 -0400234 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
235 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400236 if (!so->so_is_open_owner)
237 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400238 if (same_owner_str(so, &open->op_owner))
239 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400240 }
241 return NULL;
242}
243
244static struct nfs4_openowner *
245find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400246 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400247{
248 struct nfs4_openowner *oo;
249
Trond Myklebustd4f04892014-07-29 21:34:36 -0400250 spin_lock(&clp->cl_lock);
251 oo = find_openstateowner_str_locked(hashval, open, clp);
252 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400253 return oo;
254}
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256static inline u32
257opaque_hashval(const void *ptr, int nbytes)
258{
259 unsigned char *cptr = (unsigned char *) ptr;
260
261 u32 x = 0;
262 while (nbytes--) {
263 x *= 37;
264 x += *cptr++;
265 }
266 return x;
267}
268
Jeff Layton5b095e92014-10-23 08:01:02 -0400269static void nfsd4_free_file_rcu(struct rcu_head *rcu)
J. Bruce Fields32513b42011-10-13 16:00:16 -0400270{
Jeff Layton5b095e92014-10-23 08:01:02 -0400271 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
272
273 kmem_cache_free(file_slab, fp);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400274}
275
Christoph Hellwige6ba76e2014-08-14 08:50:16 +0200276void
NeilBrown13cd2182005-06-23 22:03:10 -0700277put_nfs4_file(struct nfs4_file *fi)
278{
Jeff Layton02e12152014-07-16 10:31:57 -0400279 might_lock(&state_lock);
280
Benny Halevycdc97502014-05-30 09:09:30 -0400281 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton5b095e92014-10-23 08:01:02 -0400282 hlist_del_rcu(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400283 spin_unlock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -0400284 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
285 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800286 }
NeilBrown13cd2182005-06-23 22:03:10 -0700287}
288
Trond Myklebustde186432014-07-10 14:07:26 -0400289static struct file *
290__nfs4_get_fd(struct nfs4_file *f, int oflag)
291{
292 if (f->fi_fds[oflag])
293 return get_file(f->fi_fds[oflag]);
294 return NULL;
295}
296
297static struct file *
298find_writeable_file_locked(struct nfs4_file *f)
299{
300 struct file *ret;
301
302 lockdep_assert_held(&f->fi_lock);
303
304 ret = __nfs4_get_fd(f, O_WRONLY);
305 if (!ret)
306 ret = __nfs4_get_fd(f, O_RDWR);
307 return ret;
308}
309
310static struct file *
311find_writeable_file(struct nfs4_file *f)
312{
313 struct file *ret;
314
315 spin_lock(&f->fi_lock);
316 ret = find_writeable_file_locked(f);
317 spin_unlock(&f->fi_lock);
318
319 return ret;
320}
321
322static struct file *find_readable_file_locked(struct nfs4_file *f)
323{
324 struct file *ret;
325
326 lockdep_assert_held(&f->fi_lock);
327
328 ret = __nfs4_get_fd(f, O_RDONLY);
329 if (!ret)
330 ret = __nfs4_get_fd(f, O_RDWR);
331 return ret;
332}
333
334static struct file *
335find_readable_file(struct nfs4_file *f)
336{
337 struct file *ret;
338
339 spin_lock(&f->fi_lock);
340 ret = find_readable_file_locked(f);
341 spin_unlock(&f->fi_lock);
342
343 return ret;
344}
345
Christoph Hellwig4d227fc2014-08-17 07:40:00 -0500346struct file *
Trond Myklebustde186432014-07-10 14:07:26 -0400347find_any_file(struct nfs4_file *f)
348{
349 struct file *ret;
350
351 spin_lock(&f->fi_lock);
352 ret = __nfs4_get_fd(f, O_RDWR);
353 if (!ret) {
354 ret = __nfs4_get_fd(f, O_WRONLY);
355 if (!ret)
356 ret = __nfs4_get_fd(f, O_RDONLY);
357 }
358 spin_unlock(&f->fi_lock);
359 return ret;
360}
361
Trond Myklebust02a35082014-07-25 07:34:22 -0400362static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800363unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700364
365/*
366 * Open owner state (share locks)
367 */
368
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500369/* hash tables for lock and open owners */
370#define OWNER_HASH_BITS 8
371#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
372#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700373
Trond Myklebustd4f04892014-07-29 21:34:36 -0400374static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400375{
376 unsigned int ret;
377
378 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500379 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400380}
NeilBrownef0f3392006-04-10 22:55:41 -0700381
NeilBrownef0f3392006-04-10 22:55:41 -0700382/* hash table for nfs4_file */
383#define FILE_HASH_BITS 8
384#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800385
Trond Myklebustca943212014-07-23 16:17:39 -0400386static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400387{
Trond Myklebustca943212014-07-23 16:17:39 -0400388 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
389}
390
391static unsigned int file_hashval(struct knfsd_fh *fh)
392{
393 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
394}
395
Jeff Layton89876f82013-04-02 09:01:59 -0400396static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700397
Jeff Layton12659652014-07-10 14:07:28 -0400398static void
399__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400400{
Jeff Layton7214e862014-07-10 14:07:33 -0400401 lockdep_assert_held(&fp->fi_lock);
402
Jeff Layton12659652014-07-10 14:07:28 -0400403 if (access & NFS4_SHARE_ACCESS_WRITE)
404 atomic_inc(&fp->fi_access[O_WRONLY]);
405 if (access & NFS4_SHARE_ACCESS_READ)
406 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400407}
408
Jeff Layton12659652014-07-10 14:07:28 -0400409static __be32
410nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400411{
Jeff Layton7214e862014-07-10 14:07:33 -0400412 lockdep_assert_held(&fp->fi_lock);
413
Jeff Layton12659652014-07-10 14:07:28 -0400414 /* Does this access mode make sense? */
415 if (access & ~NFS4_SHARE_ACCESS_BOTH)
416 return nfserr_inval;
417
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400418 /* Does it conflict with a deny mode already set? */
419 if ((access & fp->fi_share_deny) != 0)
420 return nfserr_share_denied;
421
Jeff Layton12659652014-07-10 14:07:28 -0400422 __nfs4_file_get_access(fp, access);
423 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400424}
425
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400426static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
427{
428 /* Common case is that there is no deny mode. */
429 if (deny) {
430 /* Does this deny mode make sense? */
431 if (deny & ~NFS4_SHARE_DENY_BOTH)
432 return nfserr_inval;
433
434 if ((deny & NFS4_SHARE_DENY_READ) &&
435 atomic_read(&fp->fi_access[O_RDONLY]))
436 return nfserr_share_denied;
437
438 if ((deny & NFS4_SHARE_DENY_WRITE) &&
439 atomic_read(&fp->fi_access[O_WRONLY]))
440 return nfserr_share_denied;
441 }
442 return nfs_ok;
443}
444
J. Bruce Fields998db522010-08-07 09:21:41 -0400445static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400446{
Trond Myklebustde186432014-07-10 14:07:26 -0400447 might_lock(&fp->fi_lock);
448
449 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
450 struct file *f1 = NULL;
451 struct file *f2 = NULL;
452
Jeff Layton6d338b52014-07-10 14:07:29 -0400453 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400454 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400455 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400456 spin_unlock(&fp->fi_lock);
457 if (f1)
458 fput(f1);
459 if (f2)
460 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400461 }
462}
463
Jeff Layton12659652014-07-10 14:07:28 -0400464static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400465{
Jeff Layton12659652014-07-10 14:07:28 -0400466 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
467
468 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400469 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400470 if (access & NFS4_SHARE_ACCESS_READ)
471 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400472}
473
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200474struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
Trond Myklebust60116952014-07-29 21:34:06 -0400475 struct kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400476{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500477 struct nfs4_stid *stid;
478 int new_id;
479
Trond Myklebustf8338832014-07-25 07:34:19 -0400480 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500481 if (!stid)
482 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400483
Jeff Layton4770d722014-07-29 21:34:10 -0400484 idr_preload(GFP_KERNEL);
485 spin_lock(&cl->cl_lock);
486 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
487 spin_unlock(&cl->cl_lock);
488 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700489 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500490 goto out_free;
491 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500492 stid->sc_stateid.si_opaque.so_id = new_id;
493 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
494 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400495 atomic_set(&stid->sc_count, 1);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500496
J. Bruce Fields996e0932011-10-17 11:14:48 -0400497 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500498 * It shouldn't be a problem to reuse an opaque stateid value.
499 * I don't think it is for 4.1. But with 4.0 I worry that, for
500 * example, a stray write retransmission could be accepted by
501 * the server when it should have been rejected. Therefore,
502 * adopt a trick from the sctp code to attempt to maximize the
503 * amount of time until an id is reused, by ensuring they always
504 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400505 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500506 return stid;
507out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800508 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500509 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400510}
511
Jeff Laytonb49e0842014-07-29 21:34:11 -0400512static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400513{
Trond Myklebust60116952014-07-29 21:34:06 -0400514 struct nfs4_stid *stid;
515 struct nfs4_ol_stateid *stp;
516
517 stid = nfs4_alloc_stid(clp, stateid_slab);
518 if (!stid)
519 return NULL;
520
521 stp = openlockstateid(stid);
522 stp->st_stid.sc_free = nfs4_free_ol_stateid;
523 return stp;
524}
525
526static void nfs4_free_deleg(struct nfs4_stid *stid)
527{
Trond Myklebust60116952014-07-29 21:34:06 -0400528 kmem_cache_free(deleg_slab, stid);
529 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400530}
531
NeilBrown6282cd52014-06-04 17:39:26 +1000532/*
533 * When we recall a delegation, we should be careful not to hand it
534 * out again straight away.
535 * To ensure this we keep a pair of bloom filters ('new' and 'old')
536 * in which the filehandles of recalled delegations are "stored".
537 * If a filehandle appear in either filter, a delegation is blocked.
538 * When a delegation is recalled, the filehandle is stored in the "new"
539 * filter.
540 * Every 30 seconds we swap the filters and clear the "new" one,
541 * unless both are empty of course.
542 *
543 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
544 * low 3 bytes as hash-table indices.
545 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400546 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000547 * is used to manage concurrent access. Testing does not need the lock
548 * except when swapping the two filters.
549 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400550static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000551static struct bloom_pair {
552 int entries, old_entries;
553 time_t swap_time;
554 int new; /* index into 'set' */
555 DECLARE_BITMAP(set[2], 256);
556} blocked_delegations;
557
558static int delegation_blocked(struct knfsd_fh *fh)
559{
560 u32 hash;
561 struct bloom_pair *bd = &blocked_delegations;
562
563 if (bd->entries == 0)
564 return 0;
565 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400566 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000567 if (seconds_since_boot() - bd->swap_time > 30) {
568 bd->entries -= bd->old_entries;
569 bd->old_entries = bd->entries;
570 memset(bd->set[bd->new], 0,
571 sizeof(bd->set[0]));
572 bd->new = 1-bd->new;
573 bd->swap_time = seconds_since_boot();
574 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400575 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000576 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100577 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000578 if (test_bit(hash&255, bd->set[0]) &&
579 test_bit((hash>>8)&255, bd->set[0]) &&
580 test_bit((hash>>16)&255, bd->set[0]))
581 return 1;
582
583 if (test_bit(hash&255, bd->set[1]) &&
584 test_bit((hash>>8)&255, bd->set[1]) &&
585 test_bit((hash>>16)&255, bd->set[1]))
586 return 1;
587
588 return 0;
589}
590
591static void block_delegations(struct knfsd_fh *fh)
592{
593 u32 hash;
594 struct bloom_pair *bd = &blocked_delegations;
595
Daniel Borkmann87545892014-12-10 16:33:11 +0100596 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000597
Jeff Laytonf54fe962014-07-25 07:34:26 -0400598 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000599 __set_bit(hash&255, bd->set[bd->new]);
600 __set_bit((hash>>8)&255, bd->set[bd->new]);
601 __set_bit((hash>>16)&255, bd->set[bd->new]);
602 if (bd->entries == 0)
603 bd->swap_time = seconds_since_boot();
604 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400605 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000606}
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608static struct nfs4_delegation *
Jeff Laytonf9416e22014-07-25 07:34:23 -0400609alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400612 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400615 n = atomic_long_inc_return(&num_delegations);
616 if (n < 0 || n > max_delegations)
617 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000618 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400619 goto out_dec;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400620 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700621 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400622 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400623
624 dp->dl_stid.sc_free = nfs4_free_deleg;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400625 /*
626 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400627 * meaning of seqid 0 isn't meaningful, really, but let's avoid
628 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400629 */
630 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700631 INIT_LIST_HEAD(&dp->dl_perfile);
632 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400634 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200635 dp->dl_retries = 1;
636 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200637 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400639out_dec:
640 atomic_long_dec(&num_delegations);
641 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
644void
Trond Myklebust60116952014-07-29 21:34:06 -0400645nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Trond Myklebust11b91642014-07-29 21:34:08 -0400647 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400648 struct nfs4_client *clp = s->sc_client;
649
Jeff Layton4770d722014-07-29 21:34:10 -0400650 might_lock(&clp->cl_lock);
651
Jeff Laytonb401be222014-07-29 21:34:33 -0400652 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
653 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400654 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400655 }
Trond Myklebust60116952014-07-29 21:34:06 -0400656 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400657 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400658 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400659 if (fp)
660 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500663static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Jeff Layton6bcc0342014-08-09 10:22:40 -0400665 struct file *filp = NULL;
Jeff Layton417c6622014-07-21 09:34:57 -0400666
Jeff Layton6bcc0342014-08-09 10:22:40 -0400667 spin_lock(&fp->fi_lock);
Jeff Layton67db1032014-12-13 09:11:40 -0500668 if (fp->fi_deleg_file && --fp->fi_delegees == 0)
Jeff Layton6bcc0342014-08-09 10:22:40 -0400669 swap(filp, fp->fi_deleg_file);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400670 spin_unlock(&fp->fi_lock);
671
672 if (filp) {
Christoph Hellwig2ab99ee2015-01-21 19:14:02 +0100673 vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400674 fput(filp);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200678void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400679{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500680 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400681}
682
Benny Halevy931ee562014-05-30 09:09:27 -0400683static void
684hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
685{
Benny Halevycdc97502014-05-30 09:09:30 -0400686 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400687 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400688
Trond Myklebust67cb1272014-07-29 21:34:17 -0400689 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400690 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400691 list_add(&dp->dl_perfile, &fp->fi_delegations);
692 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
693}
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695static void
Jeff Layton42690672014-07-25 07:34:20 -0400696unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Trond Myklebust11b91642014-07-29 21:34:08 -0400698 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400699
Jeff Layton42690672014-07-25 07:34:20 -0400700 lockdep_assert_held(&state_lock);
701
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400702 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400703 /* Ensure that deleg break won't try to requeue it */
704 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400705 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400706 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400708 list_del_init(&dp->dl_perfile);
709 spin_unlock(&fp->fi_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400710}
711
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400712static void destroy_delegation(struct nfs4_delegation *dp)
713{
Jeff Layton42690672014-07-25 07:34:20 -0400714 spin_lock(&state_lock);
715 unhash_delegation_locked(dp);
716 spin_unlock(&state_lock);
Jeff Laytonafbda402014-08-09 10:22:41 -0400717 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -0400718 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400719}
720
721static void revoke_delegation(struct nfs4_delegation *dp)
722{
723 struct nfs4_client *clp = dp->dl_stid.sc_client;
724
Jeff Layton2d4a5322014-07-25 07:34:21 -0400725 WARN_ON(!list_empty(&dp->dl_recall_lru));
726
Jeff Laytonafbda402014-08-09 10:22:41 -0400727 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
728
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400729 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400730 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400731 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400732 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400733 spin_lock(&clp->cl_lock);
734 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
735 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400736 }
737}
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739/*
740 * SETCLIENTID state
741 */
742
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400743static unsigned int clientid_hashval(u32 id)
744{
745 return id & CLIENT_HASH_MASK;
746}
747
748static unsigned int clientstr_hashval(const char *name)
749{
750 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
751}
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400754 * We store the NONE, READ, WRITE, and BOTH bits separately in the
755 * st_{access,deny}_bmap field of the stateid, in order to track not
756 * only what share bits are currently in force, but also what
757 * combinations of share bits previous opens have used. This allows us
758 * to enforce the recommendation of rfc 3530 14.2.19 that the server
759 * return an error if the client attempt to downgrade to a combination
760 * of share bits not explicable by closing some of its previous opens.
761 *
762 * XXX: This enforcement is actually incomplete, since we don't keep
763 * track of access/deny bit combinations; so, e.g., we allow:
764 *
765 * OPEN allow read, deny write
766 * OPEN allow both, deny none
767 * DOWNGRADE allow read, deny none
768 *
769 * which we should reject.
770 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400771static unsigned int
772bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400773 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400774 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400775
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400776 for (i = 1; i < 4; i++) {
777 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400778 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400779 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400780 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400781}
782
Jeff Layton82c5ff12012-05-11 09:45:13 -0400783/* set share access for a given stateid */
784static inline void
785set_access(u32 access, struct nfs4_ol_stateid *stp)
786{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400787 unsigned char mask = 1 << access;
788
789 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
790 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400791}
792
793/* clear share access for a given stateid */
794static inline void
795clear_access(u32 access, struct nfs4_ol_stateid *stp)
796{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400797 unsigned char mask = 1 << access;
798
799 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
800 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400801}
802
803/* test whether a given stateid has access */
804static inline bool
805test_access(u32 access, struct nfs4_ol_stateid *stp)
806{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400807 unsigned char mask = 1 << access;
808
809 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400810}
811
Jeff Laytonce0fc432012-05-11 09:45:14 -0400812/* set share deny for a given stateid */
813static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400814set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400815{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400816 unsigned char mask = 1 << deny;
817
818 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
819 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400820}
821
822/* clear share deny for a given stateid */
823static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400824clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400825{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400826 unsigned char mask = 1 << deny;
827
828 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
829 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400830}
831
832/* test whether a given stateid is denying specific access */
833static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400834test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400835{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400836 unsigned char mask = 1 << deny;
837
838 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400839}
840
841static int nfs4_access_to_omode(u32 access)
842{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400843 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400844 case NFS4_SHARE_ACCESS_READ:
845 return O_RDONLY;
846 case NFS4_SHARE_ACCESS_WRITE:
847 return O_WRONLY;
848 case NFS4_SHARE_ACCESS_BOTH:
849 return O_RDWR;
850 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500851 WARN_ON_ONCE(1);
852 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400853}
854
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400855/*
856 * A stateid that had a deny mode associated with it is being released
857 * or downgraded. Recalculate the deny mode on the file.
858 */
859static void
860recalculate_deny_mode(struct nfs4_file *fp)
861{
862 struct nfs4_ol_stateid *stp;
863
864 spin_lock(&fp->fi_lock);
865 fp->fi_share_deny = 0;
866 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
867 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
868 spin_unlock(&fp->fi_lock);
869}
870
871static void
872reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
873{
874 int i;
875 bool change = false;
876
877 for (i = 1; i < 4; i++) {
878 if ((i & deny) != i) {
879 change = true;
880 clear_deny(i, stp);
881 }
882 }
883
884 /* Recalculate per-file deny mode if there was a change */
885 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -0400886 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400887}
888
Jeff Layton82c5ff12012-05-11 09:45:13 -0400889/* release all access and file references for a given stateid */
890static void
891release_all_access(struct nfs4_ol_stateid *stp)
892{
893 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -0400894 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400895
896 if (fp && stp->st_deny_bmap != 0)
897 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400898
899 for (i = 1; i < 4; i++) {
900 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -0400901 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400902 clear_access(i, stp);
903 }
904}
905
Jeff Layton6b180f02014-07-29 21:34:26 -0400906static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
907{
Jeff Laytona819ecc2014-07-29 21:34:38 -0400908 struct nfs4_client *clp = sop->so_client;
909
910 might_lock(&clp->cl_lock);
911
912 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -0400913 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -0400914 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -0400915 spin_unlock(&clp->cl_lock);
Jeff Layton6b180f02014-07-29 21:34:26 -0400916 kfree(sop->so_owner.data);
917 sop->so_ops->so_free(sop);
918}
919
Jeff Layton4ae098d2014-07-29 21:34:43 -0400920static void unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500921{
Trond Myklebust11b91642014-07-29 21:34:08 -0400922 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -0400923
Jeff Layton1c755dc2014-07-29 21:34:12 -0400924 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
925
Trond Myklebust1d31a252014-07-10 14:07:25 -0400926 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500927 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400928 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500929 list_del(&stp->st_perstateowner);
930}
931
Trond Myklebust60116952014-07-29 21:34:06 -0400932static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500933{
Trond Myklebust60116952014-07-29 21:34:06 -0400934 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400935
Trond Myklebust60116952014-07-29 21:34:06 -0400936 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -0400937 if (stp->st_stateowner)
938 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -0400939 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500940}
941
Jeff Laytonb49e0842014-07-29 21:34:11 -0400942static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500943{
Jeff Laytonb49e0842014-07-29 21:34:11 -0400944 struct nfs4_ol_stateid *stp = openlockstateid(stid);
945 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500946 struct file *file;
947
Jeff Laytonb49e0842014-07-29 21:34:11 -0400948 file = find_any_file(stp->st_stid.sc_file);
949 if (file)
950 filp_close(file, (fl_owner_t)lo);
951 nfs4_free_ol_stateid(stid);
952}
953
Jeff Layton2c41beb2014-07-29 21:34:41 -0400954/*
955 * Put the persistent reference to an already unhashed generic stateid, while
956 * holding the cl_lock. If it's the last reference, then put it onto the
957 * reaplist for later destruction.
958 */
959static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
960 struct list_head *reaplist)
961{
962 struct nfs4_stid *s = &stp->st_stid;
963 struct nfs4_client *clp = s->sc_client;
964
965 lockdep_assert_held(&clp->cl_lock);
966
967 WARN_ON_ONCE(!list_empty(&stp->st_locks));
968
969 if (!atomic_dec_and_test(&s->sc_count)) {
970 wake_up_all(&close_wq);
971 return;
972 }
973
974 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
975 list_add(&stp->st_locks, reaplist);
976}
977
Jeff Layton3c1c9952014-07-29 21:34:39 -0400978static void unhash_lock_stateid(struct nfs4_ol_stateid *stp)
979{
980 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
981
982 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
983
984 list_del_init(&stp->st_locks);
Jeff Layton4ae098d2014-07-29 21:34:43 -0400985 unhash_ol_stateid(stp);
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200986 nfs4_unhash_stid(&stp->st_stid);
Jeff Layton3c1c9952014-07-29 21:34:39 -0400987}
988
Jeff Layton5adfd882014-07-29 21:34:31 -0400989static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -0400990{
Jeff Layton1c755dc2014-07-29 21:34:12 -0400991 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
992
993 spin_lock(&oo->oo_owner.so_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -0400994 unhash_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -0400995 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400996 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500997}
998
Trond Myklebustc58c6612014-07-29 21:34:35 -0400999static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001000{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001001 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001002
Trond Myklebustd4f04892014-07-29 21:34:36 -04001003 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001004
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001005 list_del_init(&lo->lo_owner.so_strhash);
1006}
1007
Jeff Layton2c41beb2014-07-29 21:34:41 -04001008/*
1009 * Free a list of generic stateids that were collected earlier after being
1010 * fully unhashed.
1011 */
1012static void
1013free_ol_stateid_reaplist(struct list_head *reaplist)
1014{
1015 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001016 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001017
1018 might_sleep();
1019
1020 while (!list_empty(reaplist)) {
1021 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1022 st_locks);
1023 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001024 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001025 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001026 if (fp)
1027 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001028 }
1029}
1030
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001031static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001032{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001033 struct nfs4_client *clp = lo->lo_owner.so_client;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001034 struct nfs4_ol_stateid *stp;
1035 struct list_head reaplist;
1036
1037 INIT_LIST_HEAD(&reaplist);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001038
Trond Myklebustd4f04892014-07-29 21:34:36 -04001039 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001040 unhash_lockowner_locked(lo);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001041 while (!list_empty(&lo->lo_owner.so_stateids)) {
1042 stp = list_first_entry(&lo->lo_owner.so_stateids,
1043 struct nfs4_ol_stateid, st_perstateowner);
1044 unhash_lock_stateid(stp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001045 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001046 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001047 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001048 free_ol_stateid_reaplist(&reaplist);
Jeff Layton6b180f02014-07-29 21:34:26 -04001049 nfs4_put_stateowner(&lo->lo_owner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001050}
1051
Jeff Laytond83017f2014-07-29 21:34:42 -04001052static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1053 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001054{
1055 struct nfs4_ol_stateid *stp;
1056
1057 while (!list_empty(&open_stp->st_locks)) {
1058 stp = list_entry(open_stp->st_locks.next,
1059 struct nfs4_ol_stateid, st_locks);
Jeff Laytond83017f2014-07-29 21:34:42 -04001060 unhash_lock_stateid(stp);
1061 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001062 }
1063}
1064
Jeff Laytond83017f2014-07-29 21:34:42 -04001065static void unhash_open_stateid(struct nfs4_ol_stateid *stp,
1066 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001067{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001068 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1069
Jeff Layton4ae098d2014-07-29 21:34:43 -04001070 unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001071 release_open_stateid_locks(stp, reaplist);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001072}
1073
1074static void release_open_stateid(struct nfs4_ol_stateid *stp)
1075{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001076 LIST_HEAD(reaplist);
1077
1078 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04001079 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001080 put_ol_stateid_locked(stp, &reaplist);
1081 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1082 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001083}
1084
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001085static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001086{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001087 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001088
Trond Myklebustd4f04892014-07-29 21:34:36 -04001089 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001090
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001091 list_del_init(&oo->oo_owner.so_strhash);
1092 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001093}
1094
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001095static void release_last_closed_stateid(struct nfs4_openowner *oo)
1096{
Jeff Layton217526e2014-07-30 08:27:11 -04001097 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1098 nfsd_net_id);
1099 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001100
Jeff Layton217526e2014-07-30 08:27:11 -04001101 spin_lock(&nn->client_lock);
1102 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001103 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001104 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001105 oo->oo_last_closed_stid = NULL;
1106 }
Jeff Layton217526e2014-07-30 08:27:11 -04001107 spin_unlock(&nn->client_lock);
1108 if (s)
1109 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001110}
1111
Jeff Layton2c41beb2014-07-29 21:34:41 -04001112static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001113{
1114 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001115 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001116 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001117
Jeff Layton2c41beb2014-07-29 21:34:41 -04001118 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001119
Trond Myklebustd4f04892014-07-29 21:34:36 -04001120 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001121 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001122 while (!list_empty(&oo->oo_owner.so_stateids)) {
1123 stp = list_first_entry(&oo->oo_owner.so_stateids,
1124 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytond83017f2014-07-29 21:34:42 -04001125 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001126 put_ol_stateid_locked(stp, &reaplist);
1127 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001128 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001129 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001130 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001131 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001132}
1133
Marc Eshel5282fd72009-04-03 08:27:52 +03001134static inline int
1135hash_sessionid(struct nfs4_sessionid *sessionid)
1136{
1137 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1138
1139 return sid->sequence % SESSION_HASH_SIZE;
1140}
1141
Mark Salter135dd002015-04-06 09:46:00 -04001142#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001143static inline void
1144dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1145{
1146 u32 *ptr = (u32 *)(&sessionid->data[0]);
1147 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1148}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001149#else
1150static inline void
1151dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1152{
1153}
1154#endif
1155
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001156/*
1157 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1158 * won't be used for replay.
1159 */
1160void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1161{
1162 struct nfs4_stateowner *so = cstate->replay_owner;
1163
1164 if (nfserr == nfserr_replay_me)
1165 return;
1166
1167 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001168 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001169 return;
1170 }
1171 if (!so)
1172 return;
1173 if (so->so_is_open_owner)
1174 release_last_closed_stateid(openowner(so));
1175 so->so_seqid++;
1176 return;
1177}
Marc Eshel5282fd72009-04-03 08:27:52 +03001178
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001179static void
1180gen_sessionid(struct nfsd4_session *ses)
1181{
1182 struct nfs4_client *clp = ses->se_client;
1183 struct nfsd4_sessionid *sid;
1184
1185 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1186 sid->clientid = clp->cl_clientid;
1187 sid->sequence = current_sessionid++;
1188 sid->reserved = 0;
1189}
1190
1191/*
Andy Adamsona6496372009-08-28 08:45:01 -04001192 * The protocol defines ca_maxresponssize_cached to include the size of
1193 * the rpc header, but all we need to cache is the data starting after
1194 * the end of the initial SEQUENCE operation--the rest we regenerate
1195 * each time. Therefore we can advertise a ca_maxresponssize_cached
1196 * value that is the number of bytes in our cache plus a few additional
1197 * bytes. In order to stay on the safe side, and not promise more than
1198 * we can cache, those additional bytes must be the minimum possible: 24
1199 * bytes of rpc header (xid through accept state, with AUTH_NULL
1200 * verifier), 12 for the compound header (with zero-length tag), and 44
1201 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001202 */
Andy Adamsona6496372009-08-28 08:45:01 -04001203#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1204
Andy Adamson557ce262009-08-28 08:45:04 -04001205static void
1206free_session_slots(struct nfsd4_session *ses)
1207{
1208 int i;
1209
1210 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1211 kfree(ses->se_slots[i]);
1212}
1213
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001214/*
1215 * We don't actually need to cache the rpc and session headers, so we
1216 * can allocate a little less for each slot:
1217 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001218static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001219{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001220 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001221
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001222 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1223 size = 0;
1224 else
1225 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1226 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001227}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001228
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001229/*
1230 * XXX: If we run out of reserved DRC memory we could (up to a point)
1231 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001232 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001233 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001234static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001235{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001236 u32 slotsize = slot_bytes(ca);
1237 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001238 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001239
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001240 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001241 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1242 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001243 num = min_t(int, num, avail / slotsize);
1244 nfsd_drc_mem_used += num * slotsize;
1245 spin_unlock(&nfsd_drc_lock);
1246
1247 return num;
1248}
1249
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001250static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001251{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001252 int slotsize = slot_bytes(ca);
1253
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001254 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001255 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001256 spin_unlock(&nfsd_drc_lock);
1257}
1258
Kinglong Mee60810e52014-01-01 00:35:47 +08001259static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1260 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001261{
Kinglong Mee60810e52014-01-01 00:35:47 +08001262 int numslots = fattrs->maxreqs;
1263 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001264 struct nfsd4_session *new;
1265 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001266
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001267 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001268 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1269 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001270
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001271 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001272 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001273 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001274 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001275 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001276 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001277 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001278 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001279 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001280
1281 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1282 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1283
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001284 return new;
1285out_free:
1286 while (i--)
1287 kfree(new->se_slots[i]);
1288 kfree(new);
1289 return NULL;
1290}
1291
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001292static void free_conn(struct nfsd4_conn *c)
1293{
1294 svc_xprt_put(c->cn_xprt);
1295 kfree(c);
1296}
1297
1298static void nfsd4_conn_lost(struct svc_xpt_user *u)
1299{
1300 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1301 struct nfs4_client *clp = c->cn_session->se_client;
1302
1303 spin_lock(&clp->cl_lock);
1304 if (!list_empty(&c->cn_persession)) {
1305 list_del(&c->cn_persession);
1306 free_conn(c);
1307 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001308 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001309 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001310}
1311
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001312static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001313{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001314 struct nfsd4_conn *conn;
1315
1316 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1317 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001318 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001319 svc_xprt_get(rqstp->rq_xprt);
1320 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001321 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001322 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1323 return conn;
1324}
1325
J. Bruce Fields328ead22010-09-29 16:11:06 -04001326static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1327{
1328 conn->cn_session = ses;
1329 list_add(&conn->cn_persession, &ses->se_conns);
1330}
1331
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001332static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1333{
1334 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001335
1336 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001337 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001338 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001339}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001340
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001341static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001342{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001343 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001344 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001345}
1346
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001347static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001348{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001349 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001350
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001351 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001352 ret = nfsd4_register_conn(conn);
1353 if (ret)
1354 /* oops; xprt is already down: */
1355 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001356 /* We may have gained or lost a callback channel: */
1357 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001358}
1359
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001360static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001361{
1362 u32 dir = NFS4_CDFC4_FORE;
1363
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001364 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001365 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001366 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001367}
1368
1369/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001370static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001371{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001372 struct nfs4_client *clp = s->se_client;
1373 struct nfsd4_conn *c;
1374
1375 spin_lock(&clp->cl_lock);
1376 while (!list_empty(&s->se_conns)) {
1377 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1378 list_del_init(&c->cn_persession);
1379 spin_unlock(&clp->cl_lock);
1380
1381 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1382 free_conn(c);
1383
1384 spin_lock(&clp->cl_lock);
1385 }
1386 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001387}
1388
J. Bruce Fields1377b692012-09-11 21:42:40 -04001389static void __free_session(struct nfsd4_session *ses)
1390{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001391 free_session_slots(ses);
1392 kfree(ses);
1393}
1394
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001395static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001396{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001397 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001398 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001399 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001400}
Andy Adamson557ce262009-08-28 08:45:04 -04001401
Fengguang Wu135ae822012-11-10 07:20:25 -05001402static 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 -04001403{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001404 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001405 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001406
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001407 new->se_client = clp;
1408 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001409
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001410 INIT_LIST_HEAD(&new->se_conns);
1411
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001412 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001413 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001414 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001415 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001416 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001417 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001418 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001419 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001420 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001421 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001422
Chuck Leverb0d2e422014-08-22 15:10:59 -04001423 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001424 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001425 /*
1426 * This is a little silly; with sessions there's no real
1427 * use for the callback address. Use the peer address
1428 * as a reasonable default for now, but consider fixing
1429 * the rpc client not to require an address in the
1430 * future:
1431 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001432 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1433 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001434 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001435}
1436
Benny Halevy9089f1b2010-05-12 00:12:26 +03001437/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001438static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001439__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001440{
1441 struct nfsd4_session *elem;
1442 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001443 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001444
Trond Myklebust0a880a22014-07-30 08:27:10 -04001445 lockdep_assert_held(&nn->client_lock);
1446
Marc Eshel5282fd72009-04-03 08:27:52 +03001447 dump_sessionid(__func__, sessionid);
1448 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001449 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001450 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001451 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1452 NFS4_MAX_SESSIONID_LEN)) {
1453 return elem;
1454 }
1455 }
1456
1457 dprintk("%s: session not found\n", __func__);
1458 return NULL;
1459}
1460
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001461static struct nfsd4_session *
1462find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1463 __be32 *ret)
1464{
1465 struct nfsd4_session *session;
1466 __be32 status = nfserr_badsession;
1467
1468 session = __find_in_sessionid_hashtbl(sessionid, net);
1469 if (!session)
1470 goto out;
1471 status = nfsd4_get_session_locked(session);
1472 if (status)
1473 session = NULL;
1474out:
1475 *ret = status;
1476 return session;
1477}
1478
Benny Halevy9089f1b2010-05-12 00:12:26 +03001479/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001480static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001481unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001482{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001483 struct nfs4_client *clp = ses->se_client;
1484 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1485
1486 lockdep_assert_held(&nn->client_lock);
1487
Andy Adamson7116ed62009-04-03 08:27:43 +03001488 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001489 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001490 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001491 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001492}
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1495static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001496STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001498 /*
1499 * We're assuming the clid was not given out from a boot
1500 * precisely 2^32 (about 136 years) before this one. That seems
1501 * a safe assumption:
1502 */
1503 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001505 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001506 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return 1;
1508}
1509
1510/*
1511 * XXX Should we use a slab cache ?
1512 * This type of memory management is somewhat inefficient, but we use it
1513 * anyway since SETCLIENTID is not a common operation.
1514 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001515static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
1517 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001518 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001520 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1521 if (clp == NULL)
1522 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001523 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001524 if (clp->cl_name.data == NULL)
1525 goto err_no_name;
1526 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1527 OWNER_HASH_SIZE, GFP_KERNEL);
1528 if (!clp->cl_ownerstr_hashtbl)
1529 goto err_no_hashtbl;
1530 for (i = 0; i < OWNER_HASH_SIZE; i++)
1531 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001532 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001533 INIT_LIST_HEAD(&clp->cl_sessions);
1534 idr_init(&clp->cl_stateids);
1535 atomic_set(&clp->cl_refcount, 0);
1536 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1537 INIT_LIST_HEAD(&clp->cl_idhash);
1538 INIT_LIST_HEAD(&clp->cl_openowners);
1539 INIT_LIST_HEAD(&clp->cl_delegations);
1540 INIT_LIST_HEAD(&clp->cl_lru);
1541 INIT_LIST_HEAD(&clp->cl_callbacks);
1542 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001543#ifdef CONFIG_NFSD_PNFS
1544 INIT_LIST_HEAD(&clp->cl_lo_states);
1545#endif
Trond Myklebust5694c932014-04-18 14:43:56 -04001546 spin_lock_init(&clp->cl_lock);
1547 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001549err_no_hashtbl:
1550 kfree(clp->cl_name.data);
1551err_no_name:
1552 kfree(clp);
1553 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
1555
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001556static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557free_client(struct nfs4_client *clp)
1558{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001559 while (!list_empty(&clp->cl_sessions)) {
1560 struct nfsd4_session *ses;
1561 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1562 se_perclnt);
1563 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001564 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1565 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001566 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001567 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001568 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001569 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001571 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 kfree(clp);
1573}
1574
Benny Halevy84d38ac2010-05-12 00:13:16 +03001575/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001576static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001577unhash_client_locked(struct nfs4_client *clp)
1578{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001579 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001580 struct nfsd4_session *ses;
1581
Trond Myklebust0a880a22014-07-30 08:27:10 -04001582 lockdep_assert_held(&nn->client_lock);
1583
Trond Myklebust4beb3452014-07-30 08:27:02 -04001584 /* Mark the client as expired! */
1585 clp->cl_time = 0;
1586 /* Make it invisible */
1587 if (!list_empty(&clp->cl_idhash)) {
1588 list_del_init(&clp->cl_idhash);
1589 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1590 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1591 else
1592 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1593 }
1594 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001595 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001596 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1597 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001598 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001599}
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001602unhash_client(struct nfs4_client *clp)
1603{
1604 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1605
1606 spin_lock(&nn->client_lock);
1607 unhash_client_locked(clp);
1608 spin_unlock(&nn->client_lock);
1609}
1610
Jeff Layton97403d92014-07-30 08:27:12 -04001611static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1612{
1613 if (atomic_read(&clp->cl_refcount))
1614 return nfserr_jukebox;
1615 unhash_client_locked(clp);
1616 return nfs_ok;
1617}
1618
Trond Myklebust4beb3452014-07-30 08:27:02 -04001619static void
1620__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001622 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 struct list_head reaplist;
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001627 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001628 while (!list_empty(&clp->cl_delegations)) {
1629 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001630 unhash_delegation_locked(dp);
1631 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
Benny Halevycdc97502014-05-30 09:09:30 -04001633 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 while (!list_empty(&reaplist)) {
1635 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001636 list_del_init(&dp->dl_recall_lru);
Jeff Laytonafbda402014-08-09 10:22:41 -04001637 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04001638 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001640 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05001641 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001642 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001643 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001644 }
NeilBrownea1da632005-06-23 22:04:17 -07001645 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001646 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04001647 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001648 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001650 nfsd4_return_all_client_layouts(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001651 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001652 if (clp->cl_cb_conn.cb_xprt)
1653 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001654 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
1656
Trond Myklebust4beb3452014-07-30 08:27:02 -04001657static void
1658destroy_client(struct nfs4_client *clp)
1659{
1660 unhash_client(clp);
1661 __destroy_client(clp);
1662}
1663
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001664static void expire_client(struct nfs4_client *clp)
1665{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001666 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001667 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001668 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001669}
1670
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001671static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1672{
1673 memcpy(target->cl_verifier.data, source->data,
1674 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675}
1676
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001677static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1678{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1680 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1681}
1682
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001683static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001684{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001685 if (source->cr_principal) {
1686 target->cr_principal =
1687 kstrdup(source->cr_principal, GFP_KERNEL);
1688 if (target->cr_principal == NULL)
1689 return -ENOMEM;
1690 } else
1691 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001692 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 target->cr_uid = source->cr_uid;
1694 target->cr_gid = source->cr_gid;
1695 target->cr_group_info = source->cr_group_info;
1696 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001697 target->cr_gss_mech = source->cr_gss_mech;
1698 if (source->cr_gss_mech)
1699 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001700 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
1702
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001703static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001704compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1705{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001706 if (o1->len < o2->len)
1707 return -1;
1708 if (o1->len > o2->len)
1709 return 1;
1710 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001711}
1712
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001713static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001714{
NeilBrowna55370a2005-06-23 22:03:52 -07001715 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}
1717
1718static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001719same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1720{
1721 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
1724static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001725same_clid(clientid_t *cl1, clientid_t *cl2)
1726{
1727 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001730static bool groups_equal(struct group_info *g1, struct group_info *g2)
1731{
1732 int i;
1733
1734 if (g1->ngroups != g2->ngroups)
1735 return false;
1736 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001737 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001738 return false;
1739 return true;
1740}
1741
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001742/*
1743 * RFC 3530 language requires clid_inuse be returned when the
1744 * "principal" associated with a requests differs from that previously
1745 * used. We use uid, gid's, and gss principal string as our best
1746 * approximation. We also don't want to allow non-gss use of a client
1747 * established using gss: in theory cr_principal should catch that
1748 * change, but in practice cr_principal can be null even in the gss case
1749 * since gssd doesn't always pass down a principal string.
1750 */
1751static bool is_gss_cred(struct svc_cred *cr)
1752{
1753 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1754 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1755}
1756
1757
Vivek Trivedi5559b502012-07-24 21:18:20 +05301758static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001759same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1760{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001761 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001762 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1763 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001764 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1765 return false;
1766 if (cr1->cr_principal == cr2->cr_principal)
1767 return true;
1768 if (!cr1->cr_principal || !cr2->cr_principal)
1769 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301770 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772
J. Bruce Fields57266a62013-04-13 14:27:29 -04001773static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1774{
1775 struct svc_cred *cr = &rqstp->rq_cred;
1776 u32 service;
1777
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001778 if (!cr->cr_gss_mech)
1779 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001780 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1781 return service == RPC_GSS_SVC_INTEGRITY ||
1782 service == RPC_GSS_SVC_PRIVACY;
1783}
1784
1785static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1786{
1787 struct svc_cred *cr = &rqstp->rq_cred;
1788
1789 if (!cl->cl_mach_cred)
1790 return true;
1791 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1792 return false;
1793 if (!svc_rqst_integrity_protected(rqstp))
1794 return false;
1795 if (!cr->cr_principal)
1796 return false;
1797 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1798}
1799
Jeff Layton294ac322014-07-30 08:27:15 -04001800static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001801{
Chuck Leverab4684d2012-03-02 17:13:50 -05001802 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Jeff Laytonf4199922014-06-17 07:44:11 -04001804 /*
1805 * This is opaque to client, so no need to byte-swap. Use
1806 * __force to keep sparse happy
1807 */
1808 verf[0] = (__force __be32)get_seconds();
Jeff Layton294ac322014-07-30 08:27:15 -04001809 verf[1] = (__force __be32)nn->clientid_counter;
Chuck Leverab4684d2012-03-02 17:13:50 -05001810 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
Jeff Layton294ac322014-07-30 08:27:15 -04001813static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1814{
1815 clp->cl_clientid.cl_boot = nn->boot_time;
1816 clp->cl_clientid.cl_id = nn->clientid_counter++;
1817 gen_confirm(clp, nn);
1818}
1819
Jeff Layton4770d722014-07-29 21:34:10 -04001820static struct nfs4_stid *
1821find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001822{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001823 struct nfs4_stid *ret;
1824
1825 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1826 if (!ret || !ret->sc_type)
1827 return NULL;
1828 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001829}
1830
Jeff Layton4770d722014-07-29 21:34:10 -04001831static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001832find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001833{
1834 struct nfs4_stid *s;
1835
Jeff Layton4770d722014-07-29 21:34:10 -04001836 spin_lock(&cl->cl_lock);
1837 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001838 if (s != NULL) {
1839 if (typemask & s->sc_type)
1840 atomic_inc(&s->sc_count);
1841 else
1842 s = NULL;
1843 }
Jeff Layton4770d722014-07-29 21:34:10 -04001844 spin_unlock(&cl->cl_lock);
1845 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001846}
1847
Jeff Layton2216d442012-11-12 15:00:57 -05001848static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001849 struct svc_rqst *rqstp, nfs4_verifier *verf)
1850{
1851 struct nfs4_client *clp;
1852 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001853 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001854 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001855
1856 clp = alloc_client(name);
1857 if (clp == NULL)
1858 return NULL;
1859
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001860 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1861 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001862 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001863 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001864 }
Christoph Hellwig0162ac22014-09-24 12:19:19 +02001865 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Benny Halevy07cd4902010-05-12 00:13:41 +03001866 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001867 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001868 copy_verf(clp, verf);
1869 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001870 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001871 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001872 return clp;
1873}
1874
NeilBrownfd39ca92005-06-23 22:04:03 -07001875static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001876add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1877{
1878 struct rb_node **new = &(root->rb_node), *parent = NULL;
1879 struct nfs4_client *clp;
1880
1881 while (*new) {
1882 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1883 parent = *new;
1884
1885 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1886 new = &((*new)->rb_left);
1887 else
1888 new = &((*new)->rb_right);
1889 }
1890
1891 rb_link_node(&new_clp->cl_namenode, parent, new);
1892 rb_insert_color(&new_clp->cl_namenode, root);
1893}
1894
1895static struct nfs4_client *
1896find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1897{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001898 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001899 struct rb_node *node = root->rb_node;
1900 struct nfs4_client *clp;
1901
1902 while (node) {
1903 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1904 cmp = compare_blob(&clp->cl_name, name);
1905 if (cmp > 0)
1906 node = node->rb_left;
1907 else if (cmp < 0)
1908 node = node->rb_right;
1909 else
1910 return clp;
1911 }
1912 return NULL;
1913}
1914
1915static void
1916add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
1918 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001919 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Trond Myklebust0a880a22014-07-30 08:27:10 -04001921 lockdep_assert_held(&nn->client_lock);
1922
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001923 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001924 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001926 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001927 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928}
1929
NeilBrownfd39ca92005-06-23 22:04:03 -07001930static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931move_to_confirmed(struct nfs4_client *clp)
1932{
1933 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001934 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Trond Myklebust0a880a22014-07-30 08:27:10 -04001936 lockdep_assert_held(&nn->client_lock);
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001939 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001940 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001941 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001942 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001943 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944}
1945
1946static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001947find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
1949 struct nfs4_client *clp;
1950 unsigned int idhashval = clientid_hashval(clid->cl_id);
1951
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001952 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001953 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001954 if ((bool)clp->cl_minorversion != sessions)
1955 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001956 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 }
1960 return NULL;
1961}
1962
1963static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001964find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1965{
1966 struct list_head *tbl = nn->conf_id_hashtbl;
1967
Trond Myklebust0a880a22014-07-30 08:27:10 -04001968 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001969 return find_client_in_id_table(tbl, clid, sessions);
1970}
1971
1972static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001973find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001975 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Trond Myklebust0a880a22014-07-30 08:27:10 -04001977 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001978 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
1980
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001981static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001982{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001983 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001984}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001985
NeilBrown28ce6052005-06-23 22:03:56 -07001986static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001987find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001988{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001989 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001990 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001991}
1992
1993static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001994find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001995{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001996 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001997 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001998}
1999
NeilBrownfd39ca92005-06-23 22:04:03 -07002000static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002001gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002003 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002004 struct sockaddr *sa = svc_addr(rqstp);
2005 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002006 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Jeff Layton7077ecb2009-08-14 12:57:58 -04002008 /* Currently, we only support tcp and tcp6 for the callback channel */
2009 if (se->se_callback_netid_len == 3 &&
2010 !memcmp(se->se_callback_netid_val, "tcp", 3))
2011 expected_family = AF_INET;
2012 else if (se->se_callback_netid_len == 4 &&
2013 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2014 expected_family = AF_INET6;
2015 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 goto out_err;
2017
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002018 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002019 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002020 (struct sockaddr *)&conn->cb_addr,
2021 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002022
J. Bruce Fields07263f12010-05-31 19:09:40 -04002023 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002025
J. Bruce Fields07263f12010-05-31 19:09:40 -04002026 if (conn->cb_addr.ss_family == AF_INET6)
2027 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002028
J. Bruce Fields07263f12010-05-31 19:09:40 -04002029 conn->cb_prog = se->se_callback_prog;
2030 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002031 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 return;
2033out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002034 conn->cb_addr.ss_family = AF_UNSPEC;
2035 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002036 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 "will not receive delegations\n",
2038 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 return;
2041}
2042
Andy Adamson074fe892009-04-03 08:28:15 +03002043/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002044 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002045 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002046static void
Andy Adamson074fe892009-04-03 08:28:15 +03002047nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2048{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002049 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002050 struct nfsd4_slot *slot = resp->cstate.slot;
2051 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002052
Andy Adamson557ce262009-08-28 08:45:04 -04002053 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002054
Andy Adamson557ce262009-08-28 08:45:04 -04002055 slot->sl_opcnt = resp->opcnt;
2056 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002057
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002058 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002059 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002060 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002061 return;
2062 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002063 base = resp->cstate.data_offset;
2064 slot->sl_datalen = buf->len - base;
2065 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04002066 WARN("%s: sessions DRC could not cache compound\n", __func__);
2067 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002068}
2069
2070/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002071 * Encode the replay sequence operation from the slot values.
2072 * If cachethis is FALSE encode the uncached rep error on the next
2073 * operation which sets resp->p and increments resp->opcnt for
2074 * nfs4svc_encode_compoundres.
2075 *
Andy Adamson074fe892009-04-03 08:28:15 +03002076 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002077static __be32
2078nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2079 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002080{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002081 struct nfsd4_op *op;
2082 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002083
Andy Adamsonabfabf82009-07-23 19:02:18 -04002084 /* Encode the replayed sequence operation */
2085 op = &args->ops[resp->opcnt - 1];
2086 nfsd4_encode_operation(resp, op);
2087
2088 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002089 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002090 op = &args->ops[resp->opcnt++];
2091 op->status = nfserr_retry_uncached_rep;
2092 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002093 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002094 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002095}
2096
2097/*
Andy Adamson557ce262009-08-28 08:45:04 -04002098 * The sequence operation is not cached because we can use the slot and
2099 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002100 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002101static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002102nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2103 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002104{
Andy Adamson557ce262009-08-28 08:45:04 -04002105 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002106 struct xdr_stream *xdr = &resp->xdr;
2107 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002108 __be32 status;
2109
Andy Adamson557ce262009-08-28 08:45:04 -04002110 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002111
Andy Adamsonabfabf82009-07-23 19:02:18 -04002112 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002113 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002114 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002115
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002116 p = xdr_reserve_space(xdr, slot->sl_datalen);
2117 if (!p) {
2118 WARN_ON_ONCE(1);
2119 return nfserr_serverfault;
2120 }
2121 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2122 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002123
Andy Adamson557ce262009-08-28 08:45:04 -04002124 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002125 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002126}
2127
Andy Adamson0733d212009-04-03 08:28:01 +03002128/*
2129 * Set the exchange_id flags returned by the server.
2130 */
2131static void
2132nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2133{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002134#ifdef CONFIG_NFSD_PNFS
2135 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2136#else
Andy Adamson0733d212009-04-03 08:28:01 +03002137 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002138#endif
Andy Adamson0733d212009-04-03 08:28:01 +03002139
2140 /* Referrals are supported, Migration is not. */
2141 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2142
2143 /* set the wire flags to return to client. */
2144 clid->flags = new->cl_exchange_flags;
2145}
2146
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002147static bool client_has_state(struct nfs4_client *clp)
2148{
2149 /*
2150 * Note clp->cl_openowners check isn't quite right: there's no
2151 * need to count owners without stateid's.
2152 *
2153 * Also note we should probably be using this in 4.0 case too.
2154 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002155 return !list_empty(&clp->cl_openowners)
2156 || !list_empty(&clp->cl_delegations)
2157 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002158}
2159
Al Virob37ad282006-10-19 23:28:59 -07002160__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002161nfsd4_exchange_id(struct svc_rqst *rqstp,
2162 struct nfsd4_compound_state *cstate,
2163 struct nfsd4_exchange_id *exid)
2164{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002165 struct nfs4_client *conf, *new;
2166 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002167 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002168 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002169 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002170 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002171 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002172 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002173
Jeff Layton363168b2009-08-14 12:57:56 -04002174 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002175 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002176 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002177 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002178 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002179
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002180 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002181 return nfserr_inval;
2182
Andy Adamson0733d212009-04-03 08:28:01 +03002183 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002184 case SP4_MACH_CRED:
2185 if (!svc_rqst_integrity_protected(rqstp))
2186 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002187 case SP4_NONE:
2188 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002189 default: /* checked by xdr code */
2190 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002191 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002192 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002193 }
2194
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002195 new = create_client(exid->clname, rqstp, &verf);
2196 if (new == NULL)
2197 return nfserr_jukebox;
2198
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002199 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002200 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002201 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002202 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002203 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2204 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2205
J. Bruce Fields136e6582012-05-12 20:37:23 -04002206 if (update) {
2207 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002208 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002209 goto out;
2210 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002211 if (!mach_creds_match(conf, rqstp)) {
2212 status = nfserr_wrong_cred;
2213 goto out;
2214 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002215 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002216 status = nfserr_perm;
2217 goto out;
2218 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002219 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002220 status = nfserr_not_same;
2221 goto out;
2222 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002223 /* case 6 */
2224 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002225 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002226 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002227 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002228 if (client_has_state(conf)) {
2229 status = nfserr_clid_inuse;
2230 goto out;
2231 }
Andy Adamson0733d212009-04-03 08:28:01 +03002232 goto out_new;
2233 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002234 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002235 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002236 goto out_copy;
2237 }
2238 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002239 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002240 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002241 }
2242
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002243 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002244 status = nfserr_noent;
2245 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002246 }
2247
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002248 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002249 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002250 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002251
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002252 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002253out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002254 if (conf) {
2255 status = mark_client_expired_locked(conf);
2256 if (status)
2257 goto out;
2258 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002259 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002260 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002261
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002262 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002263 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002264 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002265out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002266 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2267 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002268
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002269 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2270 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002271
2272 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002273 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002274 status = nfs_ok;
2275
2276out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002277 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002278 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002279 expire_client(new);
2280 if (unconf)
2281 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002282 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002283}
2284
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002285static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002286check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002287{
Andy Adamson88e588d2009-07-23 19:02:15 -04002288 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2289 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002290
2291 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002292 if (slot_inuse) {
2293 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002294 return nfserr_jukebox;
2295 else
2296 return nfserr_seq_misordered;
2297 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002298 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002299 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002300 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002301 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002302 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002303 return nfserr_seq_misordered;
2304}
2305
Andy Adamson49557cc2009-07-23 19:02:16 -04002306/*
2307 * Cache the create session result into the create session single DRC
2308 * slot cache by saving the xdr structure. sl_seqid has been set.
2309 * Do this for solo or embedded create session operations.
2310 */
2311static void
2312nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002313 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002314{
2315 slot->sl_status = nfserr;
2316 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2317}
2318
2319static __be32
2320nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2321 struct nfsd4_clid_slot *slot)
2322{
2323 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2324 return slot->sl_status;
2325}
2326
Mi Jinlong1b74c252011-07-14 14:50:17 +08002327#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2328 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2329 1 + /* MIN tag is length with zero, only length */ \
2330 3 + /* version, opcount, opcode */ \
2331 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2332 /* seqid, slotID, slotID, cache */ \
2333 4 ) * sizeof(__be32))
2334
2335#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2336 2 + /* verifier: AUTH_NULL, length 0 */\
2337 1 + /* status */ \
2338 1 + /* MIN tag is length with zero, only length */ \
2339 3 + /* opcount, opcode, opstatus*/ \
2340 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2341 /* seqid, slotID, slotID, slotID, status */ \
2342 5 ) * sizeof(__be32))
2343
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002344static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002345{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002346 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2347
J. Bruce Fields373cd402013-04-08 15:42:12 -04002348 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2349 return nfserr_toosmall;
2350 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2351 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002352 ca->headerpadsz = 0;
2353 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2354 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2355 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2356 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2357 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2358 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2359 /*
2360 * Note decreasing slot size below client's request may make it
2361 * difficult for client to function correctly, whereas
2362 * decreasing the number of slots will (just?) affect
2363 * performance. When short on memory we therefore prefer to
2364 * decrease number of slots instead of their size. Clients that
2365 * request larger slots than they need will get poor results:
2366 */
2367 ca->maxreqs = nfsd4_get_drc_mem(ca);
2368 if (!ca->maxreqs)
2369 return nfserr_jukebox;
2370
J. Bruce Fields373cd402013-04-08 15:42:12 -04002371 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002372}
2373
Kinglong Mee8a891632013-12-23 18:11:02 +08002374#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2375 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2376#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2377 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2378
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002379static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2380{
2381 ca->headerpadsz = 0;
2382
2383 /*
2384 * These RPC_MAX_HEADER macros are overkill, especially since we
2385 * don't even do gss on the backchannel yet. But this is still
2386 * less than 1k. Tighten up this estimate in the unlikely event
2387 * it turns out to be a problem for some client:
2388 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002389 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002390 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002391 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002392 return nfserr_toosmall;
2393 ca->maxresp_cached = 0;
2394 if (ca->maxops < 2)
2395 return nfserr_toosmall;
2396
2397 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002398}
2399
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002400static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2401{
2402 switch (cbs->flavor) {
2403 case RPC_AUTH_NULL:
2404 case RPC_AUTH_UNIX:
2405 return nfs_ok;
2406 default:
2407 /*
2408 * GSS case: the spec doesn't allow us to return this
2409 * error. But it also doesn't allow us not to support
2410 * GSS.
2411 * I'd rather this fail hard than return some error the
2412 * client might think it can already handle:
2413 */
2414 return nfserr_encr_alg_unsupp;
2415 }
2416}
2417
Andy Adamson069b6ad2009-04-03 08:27:58 +03002418__be32
2419nfsd4_create_session(struct svc_rqst *rqstp,
2420 struct nfsd4_compound_state *cstate,
2421 struct nfsd4_create_session *cr_ses)
2422{
Jeff Layton363168b2009-08-14 12:57:56 -04002423 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002424 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002425 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002426 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002427 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002428 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002429 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002430 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002431
Mi Jinlonga62573d2011-03-23 17:57:07 +08002432 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2433 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002434 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2435 if (status)
2436 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002437 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002438 if (status)
2439 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002440 status = check_backchannel_attrs(&cr_ses->back_channel);
2441 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002442 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002443 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002444 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002445 if (!new)
2446 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002447 conn = alloc_conn_from_crses(rqstp, cr_ses);
2448 if (!conn)
2449 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002450
Jeff Laytond20c11d2014-07-30 08:27:07 -04002451 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002452 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002453 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002454 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002455
2456 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002457 status = nfserr_wrong_cred;
2458 if (!mach_creds_match(conf, rqstp))
2459 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002460 cs_slot = &conf->cl_cs_slot;
2461 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002462 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002463 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002464 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002465 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002466 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002467 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002468 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002469 } else if (unconf) {
2470 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002471 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002472 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002473 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002474 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002475 status = nfserr_wrong_cred;
2476 if (!mach_creds_match(unconf, rqstp))
2477 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002478 cs_slot = &unconf->cl_cs_slot;
2479 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002480 if (status) {
2481 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002482 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002483 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002484 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002485 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002486 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002487 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002488 if (status) {
2489 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002490 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002491 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002492 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002493 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002494 conf = unconf;
2495 } else {
2496 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002497 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002498 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002499 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002500 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002501 * We do not support RDMA or persistent sessions
2502 */
2503 cr_ses->flags &= ~SESSION4_PERSIST;
2504 cr_ses->flags &= ~SESSION4_RDMA;
2505
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002506 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002507 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002508
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002509 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002510 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002511 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002512 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002513
Jeff Laytond20c11d2014-07-30 08:27:07 -04002514 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002515 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002516 spin_unlock(&nn->client_lock);
2517 /* init connection and backchannel */
2518 nfsd4_init_conn(rqstp, conn, new);
2519 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002520 if (old)
2521 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002522 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002523out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002524 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002525 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002526 if (old)
2527 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002528out_free_session:
2529 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002530out_release_drc_mem:
2531 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002532 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002533}
2534
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002535static __be32 nfsd4_map_bcts_dir(u32 *dir)
2536{
2537 switch (*dir) {
2538 case NFS4_CDFC4_FORE:
2539 case NFS4_CDFC4_BACK:
2540 return nfs_ok;
2541 case NFS4_CDFC4_FORE_OR_BOTH:
2542 case NFS4_CDFC4_BACK_OR_BOTH:
2543 *dir = NFS4_CDFC4_BOTH;
2544 return nfs_ok;
2545 };
2546 return nfserr_inval;
2547}
2548
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002549__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2550{
2551 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002552 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002553 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002554
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002555 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2556 if (status)
2557 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002558 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002559 session->se_cb_prog = bc->bc_cb_program;
2560 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002561 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002562
2563 nfsd4_probe_callback(session->se_client);
2564
2565 return nfs_ok;
2566}
2567
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002568__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2569 struct nfsd4_compound_state *cstate,
2570 struct nfsd4_bind_conn_to_session *bcts)
2571{
2572 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002573 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002574 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002575 struct net *net = SVC_NET(rqstp);
2576 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002577
2578 if (!nfsd4_last_compound_op(rqstp))
2579 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002580 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002581 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002582 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002583 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002584 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002585 status = nfserr_wrong_cred;
2586 if (!mach_creds_match(session->se_client, rqstp))
2587 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002588 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002589 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002590 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002591 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002592 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002593 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002594 goto out;
2595 nfsd4_init_conn(rqstp, conn, session);
2596 status = nfs_ok;
2597out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002598 nfsd4_put_session(session);
2599out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002600 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002601}
2602
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002603static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2604{
2605 if (!session)
2606 return 0;
2607 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2608}
2609
Andy Adamson069b6ad2009-04-03 08:27:58 +03002610__be32
2611nfsd4_destroy_session(struct svc_rqst *r,
2612 struct nfsd4_compound_state *cstate,
2613 struct nfsd4_destroy_session *sessionid)
2614{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002615 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002616 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002617 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002618 struct net *net = SVC_NET(r);
2619 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002620
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002621 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002622 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002623 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002624 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002625 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002626 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002627 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002628 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002629 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002630 if (!ses)
2631 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002632 status = nfserr_wrong_cred;
2633 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002634 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002635 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002636 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002637 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002638 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002639 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002640
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002641 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002642
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002643 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002644 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002645out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002646 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002647out_client_lock:
2648 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002649out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03002650 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002651}
2652
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002653static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002654{
2655 struct nfsd4_conn *c;
2656
2657 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002658 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002659 return c;
2660 }
2661 }
2662 return NULL;
2663}
2664
J. Bruce Fields57266a62013-04-13 14:27:29 -04002665static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002666{
2667 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002668 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002669 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002670 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002671
2672 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002673 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002674 if (c)
2675 goto out_free;
2676 status = nfserr_conn_not_bound_to_session;
2677 if (clp->cl_mach_cred)
2678 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002679 __nfsd4_hash_conn(new, ses);
2680 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002681 ret = nfsd4_register_conn(new);
2682 if (ret)
2683 /* oops; xprt is already down: */
2684 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002685 return nfs_ok;
2686out_free:
2687 spin_unlock(&clp->cl_lock);
2688 free_conn(new);
2689 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002690}
2691
Mi Jinlong868b89c2011-04-27 09:09:58 +08002692static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2693{
2694 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2695
2696 return args->opcnt > session->se_fchannel.maxops;
2697}
2698
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002699static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2700 struct nfsd4_session *session)
2701{
2702 struct xdr_buf *xb = &rqstp->rq_arg;
2703
2704 return xb->len > session->se_fchannel.maxreq_sz;
2705}
2706
Andy Adamson069b6ad2009-04-03 08:27:58 +03002707__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002708nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002709 struct nfsd4_compound_state *cstate,
2710 struct nfsd4_sequence *seq)
2711{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002712 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002713 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002714 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002715 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002716 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002717 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002718 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002719 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002720 struct net *net = SVC_NET(rqstp);
2721 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002722
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002723 if (resp->opcnt != 1)
2724 return nfserr_sequence_pos;
2725
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002726 /*
2727 * Will be either used or freed by nfsd4_sequence_check_conn
2728 * below.
2729 */
2730 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2731 if (!conn)
2732 return nfserr_jukebox;
2733
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002734 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002735 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002736 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002737 goto out_no_session;
2738 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002739
Mi Jinlong868b89c2011-04-27 09:09:58 +08002740 status = nfserr_too_many_ops;
2741 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002742 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002743
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002744 status = nfserr_req_too_big;
2745 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002746 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002747
Benny Halevyb85d4c02009-04-03 08:28:08 +03002748 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002749 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002750 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002751
Andy Adamson557ce262009-08-28 08:45:04 -04002752 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002753 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2754
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002755 /* We do not negotiate the number of slots yet, so set the
2756 * maxslots to the session maxreqs which is used to encode
2757 * sr_highest_slotid and the sr_target_slot id to maxslots */
2758 seq->maxslots = session->se_fchannel.maxreqs;
2759
J. Bruce Fields73e79482012-02-13 16:39:00 -05002760 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2761 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002762 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002763 status = nfserr_seq_misordered;
2764 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002765 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002766 cstate->slot = slot;
2767 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002768 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002769 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002770 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002771 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002772 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002773 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002774 }
2775 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002776 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002777
J. Bruce Fields57266a62013-04-13 14:27:29 -04002778 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002779 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002780 if (status)
2781 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002782
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002783 buflen = (seq->cachethis) ?
2784 session->se_fchannel.maxresp_cached :
2785 session->se_fchannel.maxresp_sz;
2786 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2787 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002788 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002789 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002790 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002791
2792 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002793 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002794 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002795 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002796 if (seq->cachethis)
2797 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002798 else
2799 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002800
2801 cstate->slot = slot;
2802 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002803 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002804
Benny Halevyb85d4c02009-04-03 08:28:08 +03002805out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002806 switch (clp->cl_cb_state) {
2807 case NFSD4_CB_DOWN:
2808 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2809 break;
2810 case NFSD4_CB_FAULT:
2811 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2812 break;
2813 default:
2814 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002815 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002816 if (!list_empty(&clp->cl_revoked))
2817 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002818out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002819 if (conn)
2820 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002821 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002822 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002823out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002824 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002825 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002826}
2827
Trond Myklebustb6076642014-06-30 11:48:35 -04002828void
2829nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2830{
2831 struct nfsd4_compound_state *cs = &resp->cstate;
2832
2833 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002834 if (cs->status != nfserr_replay_cache) {
2835 nfsd4_store_cache_entry(resp);
2836 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2837 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002838 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002839 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002840 } else if (cs->clp)
2841 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002842}
2843
Mi Jinlong345c2842011-10-20 17:51:39 +08002844__be32
2845nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2846{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002847 struct nfs4_client *conf, *unconf;
2848 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002849 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002850 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002851
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002852 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002853 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002854 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002855 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002856
2857 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002858 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002859 status = nfserr_clientid_busy;
2860 goto out;
2861 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04002862 status = mark_client_expired_locked(conf);
2863 if (status)
2864 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002865 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08002866 } else if (unconf)
2867 clp = unconf;
2868 else {
2869 status = nfserr_stale_clientid;
2870 goto out;
2871 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002872 if (!mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002873 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002874 status = nfserr_wrong_cred;
2875 goto out;
2876 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002877 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002878out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002879 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002880 if (clp)
2881 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002882 return status;
2883}
2884
Andy Adamson069b6ad2009-04-03 08:27:58 +03002885__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002886nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2887{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002888 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002889
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002890 if (rc->rca_one_fs) {
2891 if (!cstate->current_fh.fh_dentry)
2892 return nfserr_nofilehandle;
2893 /*
2894 * We don't take advantage of the rca_one_fs case.
2895 * That's OK, it's optional, we can safely ignore it.
2896 */
2897 return nfs_ok;
2898 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002899
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002900 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002901 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2902 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002903 goto out;
2904
2905 status = nfserr_stale_clientid;
2906 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002907 /*
2908 * The following error isn't really legal.
2909 * But we only get here if the client just explicitly
2910 * destroyed the client. Surely it no longer cares what
2911 * error it gets back on an operation for the dead
2912 * client.
2913 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002914 goto out;
2915
2916 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002917 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002918out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002919 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002920}
2921
2922__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002923nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2924 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002926 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002928 struct nfs4_client *conf, *new;
2929 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002930 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002931 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2932
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002933 new = create_client(clname, rqstp, &clverifier);
2934 if (new == NULL)
2935 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002936 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002937 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002938 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002939 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002940 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002942 if (clp_used_exchangeid(conf))
2943 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002944 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002945 char addr_str[INET6_ADDRSTRLEN];
2946 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2947 sizeof(addr_str));
2948 dprintk("NFSD: setclientid: string in use by client "
2949 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 goto out;
2951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002953 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002954 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002955 unhash_client_locked(unconf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002956 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002957 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002959 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002960 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002961 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002962 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002963 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2965 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2966 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002967 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 status = nfs_ok;
2969out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002970 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002971 if (new)
2972 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002973 if (unconf)
2974 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 return status;
2976}
2977
2978
Al Virob37ad282006-10-19 23:28:59 -07002979__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002980nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2981 struct nfsd4_compound_state *cstate,
2982 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983{
NeilBrown21ab45a2005-06-23 22:04:14 -07002984 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002985 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2987 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002988 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002989 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002991 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07002993
Jeff Laytond20c11d2014-07-30 08:27:07 -04002994 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002995 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002996 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002997 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002998 * We try hard to give out unique clientid's, so if we get an
2999 * attempt to confirm the same clientid with a different cred,
3000 * there's a bug somewhere. Let's charitably assume it's our
3001 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003002 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04003003 status = nfserr_serverfault;
3004 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3005 goto out;
3006 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3007 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003008 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003009 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3010 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003012 else /* case 4: client hasn't noticed we rebooted yet? */
3013 status = nfserr_stale_clientid;
3014 goto out;
3015 }
3016 status = nfs_ok;
3017 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003018 old = unconf;
3019 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003020 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003021 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003022 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3023 if (old) {
3024 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003025 if (status) {
3026 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003027 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003028 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003029 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003030 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003031 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003032 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003033 get_client_locked(conf);
3034 spin_unlock(&nn->client_lock);
3035 nfsd4_probe_callback(conf);
3036 spin_lock(&nn->client_lock);
3037 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003039 spin_unlock(&nn->client_lock);
3040 if (old)
3041 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 return status;
3043}
3044
J. Bruce Fields32513b42011-10-13 16:00:16 -04003045static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003047 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3048}
3049
3050/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003051static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3052 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003053{
Trond Myklebust950e0112014-06-30 11:48:31 -04003054 lockdep_assert_held(&state_lock);
3055
J. Bruce Fields32513b42011-10-13 16:00:16 -04003056 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003057 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003058 INIT_LIST_HEAD(&fp->fi_stateids);
3059 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003060 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003061 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003062 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003063 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003064 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3065 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003066#ifdef CONFIG_NFSD_PNFS
3067 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02003068 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003069#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04003070 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071}
3072
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003073void
NeilBrowne60d4392005-06-23 22:03:01 -07003074nfsd4_free_slabs(void)
3075{
Christoph Hellwigabf11352014-05-21 07:43:03 -07003076 kmem_cache_destroy(openowner_slab);
3077 kmem_cache_destroy(lockowner_slab);
3078 kmem_cache_destroy(file_slab);
3079 kmem_cache_destroy(stateid_slab);
3080 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003081}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
Bryan Schumaker72083392011-11-01 15:24:59 -04003083int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084nfsd4_init_slabs(void)
3085{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003086 openowner_slab = kmem_cache_create("nfsd4_openowners",
3087 sizeof(struct nfs4_openowner), 0, 0, NULL);
3088 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003089 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003090 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003091 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003092 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003093 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003094 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003095 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003096 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003097 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003098 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003099 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003100 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003101 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003102 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003103 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003104 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003105 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003107
3108out_free_stateid_slab:
3109 kmem_cache_destroy(stateid_slab);
3110out_free_file_slab:
3111 kmem_cache_destroy(file_slab);
3112out_free_lockowner_slab:
3113 kmem_cache_destroy(lockowner_slab);
3114out_free_openowner_slab:
3115 kmem_cache_destroy(openowner_slab);
3116out:
NeilBrowne60d4392005-06-23 22:03:01 -07003117 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3118 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119}
3120
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003121static void init_nfs4_replay(struct nfs4_replay *rp)
3122{
3123 rp->rp_status = nfserr_serverfault;
3124 rp->rp_buflen = 0;
3125 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003126 mutex_init(&rp->rp_mutex);
3127}
3128
3129static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3130 struct nfs4_stateowner *so)
3131{
3132 if (!nfsd4_has_session(cstate)) {
3133 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003134 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04003135 }
3136}
3137
3138void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3139{
3140 struct nfs4_stateowner *so = cstate->replay_owner;
3141
3142 if (so != NULL) {
3143 cstate->replay_owner = NULL;
3144 mutex_unlock(&so->so_replay.rp_mutex);
3145 nfs4_put_stateowner(so);
3146 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003147}
3148
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003149static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150{
3151 struct nfs4_stateowner *sop;
3152
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003153 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003154 if (!sop)
3155 return NULL;
3156
3157 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3158 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003159 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003160 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003162 sop->so_owner.len = owner->len;
3163
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003164 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003165 sop->so_client = clp;
3166 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003167 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003168 return sop;
3169}
3170
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003171static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003172{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003173 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003174
Trond Myklebustd4f04892014-07-29 21:34:36 -04003175 list_add(&oo->oo_owner.so_strhash,
3176 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003177 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178}
3179
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003180static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3181{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003182 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003183}
3184
Jeff Layton6b180f02014-07-29 21:34:26 -04003185static void nfs4_free_openowner(struct nfs4_stateowner *so)
3186{
3187 struct nfs4_openowner *oo = openowner(so);
3188
3189 kmem_cache_free(openowner_slab, oo);
3190}
3191
3192static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003193 .so_unhash = nfs4_unhash_openowner,
3194 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003195};
3196
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003197static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003198alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003199 struct nfsd4_compound_state *cstate)
3200{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003201 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003202 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003204 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3205 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003207 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003208 oo->oo_owner.so_is_open_owner = 1;
3209 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003210 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003211 if (nfsd4_has_session(cstate))
3212 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003213 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003214 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003215 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003216 spin_lock(&clp->cl_lock);
3217 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003218 if (ret == NULL) {
3219 hash_openowner(oo, clp, strhashval);
3220 ret = oo;
3221 } else
3222 nfs4_free_openowner(&oo->oo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003223 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04003224 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225}
3226
J. Bruce Fields996e0932011-10-17 11:14:48 -04003227static 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 -04003228 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003230 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003231 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003232 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003233 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07003234 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003235 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 stp->st_access_bmap = 0;
3237 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003238 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003239 spin_lock(&oo->oo_owner.so_client->cl_lock);
3240 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003241 spin_lock(&fp->fi_lock);
3242 list_add(&stp->st_perfile, &fp->fi_stateids);
3243 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003244 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245}
3246
Jeff Laytond3134b12014-07-29 21:34:32 -04003247/*
3248 * In the 4.0 case we need to keep the owners around a little while to handle
3249 * CLOSE replay. We still do need to release any file access that is held by
3250 * them before returning however.
3251 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003253move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254{
Jeff Layton217526e2014-07-30 08:27:11 -04003255 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003256 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3257 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3258 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003259
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003260 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
Jeff Laytonb401be222014-07-29 21:34:33 -04003262 /*
3263 * We know that we hold one reference via nfsd4_close, and another
3264 * "persistent" reference for the client. If the refcount is higher
3265 * than 2, then there are still calls in progress that are using this
3266 * stateid. We can't put the sc_file reference until they are finished.
3267 * Wait for the refcount to drop to 2. Since it has been unhashed,
3268 * there should be no danger of the refcount going back up again at
3269 * this point.
3270 */
3271 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3272
Jeff Laytond3134b12014-07-29 21:34:32 -04003273 release_all_access(s);
3274 if (s->st_stid.sc_file) {
3275 put_nfs4_file(s->st_stid.sc_file);
3276 s->st_stid.sc_file = NULL;
3277 }
Jeff Layton217526e2014-07-30 08:27:11 -04003278
3279 spin_lock(&nn->client_lock);
3280 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003281 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003282 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003283 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003284 spin_unlock(&nn->client_lock);
3285 if (last)
3286 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287}
3288
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289/* search file_hashtbl[] for file */
3290static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04003291find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 struct nfs4_file *fp;
3294
Jeff Layton5b095e92014-10-23 08:01:02 -04003295 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02003296 if (fh_match(&fp->fi_fhandle, fh)) {
Jeff Layton5b095e92014-10-23 08:01:02 -04003297 if (atomic_inc_not_zero(&fp->fi_ref))
3298 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 }
3301 return NULL;
3302}
3303
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02003304struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003305find_file(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);
Trond Myklebust950e0112014-06-30 11:48:31 -04003309
Jeff Layton5b095e92014-10-23 08:01:02 -04003310 rcu_read_lock();
3311 fp = find_file_locked(fh, hashval);
3312 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04003313 return fp;
3314}
3315
3316static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003317find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003318{
3319 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003320 unsigned int hashval = file_hashval(fh);
3321
3322 rcu_read_lock();
3323 fp = find_file_locked(fh, hashval);
3324 rcu_read_unlock();
3325 if (fp)
3326 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04003327
3328 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04003329 fp = find_file_locked(fh, hashval);
3330 if (likely(fp == NULL)) {
3331 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04003332 fp = new;
3333 }
3334 spin_unlock(&state_lock);
3335
3336 return fp;
3337}
3338
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003339/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 * Called to check deny when READ with all zero stateid or
3341 * WRITE with all zero or all one stateid
3342 */
Al Virob37ad282006-10-19 23:28:59 -07003343static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3345{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003347 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
Trond Myklebustca943212014-07-23 16:17:39 -04003349 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003350 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003351 return ret;
3352 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003353 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003354 if (fp->fi_share_deny & deny_type)
3355 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003356 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003357 put_nfs4_file(fp);
3358 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359}
3360
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003361static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003363 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04003364 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3365 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003366
Trond Myklebust11b91642014-07-29 21:34:08 -04003367 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003368
Jeff Layton02e12152014-07-16 10:31:57 -04003369 /*
3370 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003371 * already holding inode->i_lock.
3372 *
Jeff Laytondff13992014-07-08 14:02:49 -04003373 * If the dl_time != 0, then we know that it has already been
3374 * queued for a lease break. Don't queue it again.
3375 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003376 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003377 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003378 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003379 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003380 }
Jeff Layton02e12152014-07-16 10:31:57 -04003381 spin_unlock(&state_lock);
3382}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003384static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3385 struct rpc_task *task)
3386{
3387 struct nfs4_delegation *dp = cb_to_delegation(cb);
3388
3389 switch (task->tk_status) {
3390 case 0:
3391 return 1;
3392 case -EBADHANDLE:
3393 case -NFS4ERR_BAD_STATEID:
3394 /*
3395 * Race: client probably got cb_recall before open reply
3396 * granting delegation.
3397 */
3398 if (dp->dl_retries--) {
3399 rpc_delay(task, 2 * HZ);
3400 return 0;
3401 }
3402 /*FALLTHRU*/
3403 default:
3404 return -1;
3405 }
3406}
3407
3408static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3409{
3410 struct nfs4_delegation *dp = cb_to_delegation(cb);
3411
3412 nfs4_put_stid(&dp->dl_stid);
3413}
3414
3415static struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3416 .prepare = nfsd4_cb_recall_prepare,
3417 .done = nfsd4_cb_recall_done,
3418 .release = nfsd4_cb_recall_release,
3419};
3420
Jeff Layton02e12152014-07-16 10:31:57 -04003421static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3422{
3423 /*
3424 * We're assuming the state code never drops its reference
3425 * without first removing the lease. Since we're in this lease
3426 * callback (and since the lease code is serialized by the kernel
3427 * lock) we know the server hasn't removed the lease yet, we know
3428 * it's safe to take a reference.
3429 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003430 atomic_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02003431 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003432}
3433
Jeff Layton1c8c6012013-06-21 08:58:15 -04003434/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003435static bool
3436nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003437{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003438 bool ret = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003439 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3440 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003441
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003442 if (!fp) {
3443 WARN(1, "(%p)->fl_owner NULL\n", fl);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003444 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003445 }
3446 if (fp->fi_had_conflict) {
3447 WARN(1, "duplicate break on %p\n", fp);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003448 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003449 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003450 /*
3451 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003452 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003453 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003454 */
3455 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
Jeff Layton02e12152014-07-16 10:31:57 -04003457 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003458 fp->fi_had_conflict = true;
3459 /*
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003460 * If there are no delegations on the list, then return true
3461 * so that the lease code will go ahead and delete it.
Jeff Layton417c6622014-07-21 09:34:57 -04003462 */
3463 if (list_empty(&fp->fi_delegations))
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003464 ret = true;
Jeff Layton417c6622014-07-21 09:34:57 -04003465 else
3466 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3467 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003468 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003469 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470}
3471
Jeff Laytonc45198e2014-09-01 07:12:07 -04003472static int
Jeff Layton7448cc32015-01-16 15:05:57 -05003473nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3474 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475{
3476 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04003477 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 else
3479 return -EAGAIN;
3480}
3481
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003482static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003483 .lm_break = nfsd_break_deleg_cb,
3484 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485};
3486
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003487static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3488{
3489 if (nfsd4_has_session(cstate))
3490 return nfs_ok;
3491 if (seqid == so->so_seqid - 1)
3492 return nfserr_replay_me;
3493 if (seqid == so->so_seqid)
3494 return nfs_ok;
3495 return nfserr_bad_seqid;
3496}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
Jeff Layton4b24ca72014-06-30 11:48:44 -04003498static __be32 lookup_clientid(clientid_t *clid,
3499 struct nfsd4_compound_state *cstate,
3500 struct nfsd_net *nn)
3501{
3502 struct nfs4_client *found;
3503
3504 if (cstate->clp) {
3505 found = cstate->clp;
3506 if (!same_clid(&found->cl_clientid, clid))
3507 return nfserr_stale_clientid;
3508 return nfs_ok;
3509 }
3510
3511 if (STALE_CLIENTID(clid, nn))
3512 return nfserr_stale_clientid;
3513
3514 /*
3515 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3516 * cached already then we know this is for is for v4.0 and "sessions"
3517 * will be false.
3518 */
3519 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003520 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003521 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003522 if (!found) {
3523 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003524 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003525 }
3526 atomic_inc(&found->cl_refcount);
3527 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003528
3529 /* Cache the nfs4_client in cstate! */
3530 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003531 return nfs_ok;
3532}
3533
Al Virob37ad282006-10-19 23:28:59 -07003534__be32
Andy Adamson66689582009-04-03 08:28:45 +03003535nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003536 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 clientid_t *clientid = &open->op_clientid;
3539 struct nfs4_client *clp = NULL;
3540 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003541 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003542 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003544 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003546 /*
3547 * In case we need it later, after we've already created the
3548 * file and don't want to risk a further failure:
3549 */
3550 open->op_file = nfsd4_alloc_file();
3551 if (open->op_file == NULL)
3552 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
Trond Myklebust2d91e892014-06-30 11:48:46 -04003554 status = lookup_clientid(clientid, cstate, nn);
3555 if (status)
3556 return status;
3557 clp = cstate->clp;
3558
Trond Myklebustd4f04892014-07-29 21:34:36 -04003559 strhashval = ownerstr_hashval(&open->op_owner);
3560 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003561 open->op_openowner = oo;
3562 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003563 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003565 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003566 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003567 release_openowner(oo);
3568 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003569 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003570 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003571 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3572 if (status)
3573 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003574 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003575new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003576 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003577 if (oo == NULL)
3578 return nfserr_jukebox;
3579 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003580alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003581 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003582 if (!open->op_stp)
3583 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003584 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585}
3586
Al Virob37ad282006-10-19 23:28:59 -07003587static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003588nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3589{
3590 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3591 return nfserr_openmode;
3592 else
3593 return nfs_ok;
3594}
3595
Daniel Mackc47d8322011-05-16 16:38:14 +02003596static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003597{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003598 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3599}
3600
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003601static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003602{
3603 struct nfs4_stid *ret;
3604
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003605 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003606 if (!ret)
3607 return NULL;
3608 return delegstateid(ret);
3609}
3610
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003611static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3612{
3613 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3614 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3615}
3616
Al Virob37ad282006-10-19 23:28:59 -07003617static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003618nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003619 struct nfs4_delegation **dp)
3620{
3621 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003622 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003623 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003624
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003625 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3626 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003627 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003628 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003629 status = nfs4_check_delegmode(deleg, flags);
3630 if (status) {
3631 nfs4_put_stid(&deleg->dl_stid);
3632 goto out;
3633 }
3634 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003635out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003636 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003637 return nfs_ok;
3638 if (status)
3639 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003640 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003641 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003642}
3643
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003644static struct nfs4_ol_stateid *
3645nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003647 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003648 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
Trond Myklebust1d31a252014-07-10 14:07:25 -04003650 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003651 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 /* ignore lock owners */
3653 if (local->st_stateowner->so_is_open_owner == 0)
3654 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003655 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003656 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003657 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003658 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003661 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003662 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663}
3664
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003665static inline int nfs4_access_to_access(u32 nfs4_access)
3666{
3667 int flags = 0;
3668
3669 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3670 flags |= NFSD_MAY_READ;
3671 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3672 flags |= NFSD_MAY_WRITE;
3673 return flags;
3674}
3675
Al Virob37ad282006-10-19 23:28:59 -07003676static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3678 struct nfsd4_open *open)
3679{
3680 struct iattr iattr = {
3681 .ia_valid = ATTR_SIZE,
3682 .ia_size = 0,
3683 };
3684 if (!open->op_truncate)
3685 return 0;
3686 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003687 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3689}
3690
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003691static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003692 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3693 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003694{
Trond Myklebustde186432014-07-10 14:07:26 -04003695 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003696 __be32 status;
3697 int oflag = nfs4_access_to_omode(open->op_share_access);
3698 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003699 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003700
Trond Myklebustde186432014-07-10 14:07:26 -04003701 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003702
3703 /*
3704 * Are we trying to set a deny mode that would conflict with
3705 * current access?
3706 */
3707 status = nfs4_file_check_deny(fp, open->op_share_deny);
3708 if (status != nfs_ok) {
3709 spin_unlock(&fp->fi_lock);
3710 goto out;
3711 }
3712
3713 /* set access to the file */
3714 status = nfs4_file_get_access(fp, open->op_share_access);
3715 if (status != nfs_ok) {
3716 spin_unlock(&fp->fi_lock);
3717 goto out;
3718 }
3719
3720 /* Set access bits in stateid */
3721 old_access_bmap = stp->st_access_bmap;
3722 set_access(open->op_share_access, stp);
3723
3724 /* Set new deny mask */
3725 old_deny_bmap = stp->st_deny_bmap;
3726 set_deny(open->op_share_deny, stp);
3727 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3728
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003729 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003730 spin_unlock(&fp->fi_lock);
3731 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003732 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003733 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003734 spin_lock(&fp->fi_lock);
3735 if (!fp->fi_fds[oflag]) {
3736 fp->fi_fds[oflag] = filp;
3737 filp = NULL;
3738 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003739 }
Trond Myklebustde186432014-07-10 14:07:26 -04003740 spin_unlock(&fp->fi_lock);
3741 if (filp)
3742 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003743
3744 status = nfsd4_truncate(rqstp, cur_fh, open);
3745 if (status)
3746 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003747out:
3748 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003749out_put_access:
3750 stp->st_access_bmap = old_access_bmap;
3751 nfs4_file_put_access(fp, open->op_share_access);
3752 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3753 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003754}
3755
Al Virob37ad282006-10-19 23:28:59 -07003756static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003757nfs4_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 -07003758{
Al Virob37ad282006-10-19 23:28:59 -07003759 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003760 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003762 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003763 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003764
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003765 /* test and set deny mode */
3766 spin_lock(&fp->fi_lock);
3767 status = nfs4_file_check_deny(fp, open->op_share_deny);
3768 if (status == nfs_ok) {
3769 old_deny_bmap = stp->st_deny_bmap;
3770 set_deny(open->op_share_deny, stp);
3771 fp->fi_share_deny |=
3772 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3773 }
3774 spin_unlock(&fp->fi_lock);
3775
3776 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003779 status = nfsd4_truncate(rqstp, cur_fh, open);
3780 if (status != nfs_ok)
3781 reset_union_bmap_deny(old_deny_bmap, stp);
3782 return status;
3783}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003786nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003788 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789}
3790
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003791/* Should we give out recallable state?: */
3792static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3793{
3794 if (clp->cl_cb_state == NFSD4_CB_UP)
3795 return true;
3796 /*
3797 * In the sessions case, since we don't have to establish a
3798 * separate connection for callbacks, we assume it's OK
3799 * until we hear otherwise:
3800 */
3801 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3802}
3803
Jeff Laytond564fbe2014-07-16 10:31:58 -04003804static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003805{
3806 struct file_lock *fl;
3807
3808 fl = locks_alloc_lock();
3809 if (!fl)
3810 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003811 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003812 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003813 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3814 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003815 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003816 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003817 return fl;
3818}
3819
J. Bruce Fields99c41512013-05-21 16:21:25 -04003820static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003821{
Trond Myklebust11b91642014-07-29 21:34:08 -04003822 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton415b96c2014-08-22 12:26:36 -04003823 struct file_lock *fl, *ret;
Jeff Layton417c6622014-07-21 09:34:57 -04003824 struct file *filp;
3825 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003826
Jeff Laytond564fbe2014-07-16 10:31:58 -04003827 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003828 if (!fl)
3829 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003830 filp = find_readable_file(fp);
3831 if (!filp) {
3832 /* We should always have a readable file here */
3833 WARN_ON_ONCE(1);
3834 return -EBADF;
3835 }
3836 fl->fl_file = filp;
Jeff Layton415b96c2014-08-22 12:26:36 -04003837 ret = fl;
Jeff Laytone6f5c782014-08-22 10:40:25 -04003838 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003839 if (fl)
Jeff Layton417c6622014-07-21 09:34:57 -04003840 locks_free_lock(fl);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003841 if (status)
Jeff Layton417c6622014-07-21 09:34:57 -04003842 goto out_fput;
Benny Halevycdc97502014-05-30 09:09:30 -04003843 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003844 spin_lock(&fp->fi_lock);
3845 /* Did the lease get broken before we took the lock? */
3846 status = -EAGAIN;
3847 if (fp->fi_had_conflict)
3848 goto out_unlock;
3849 /* Race breaker */
Jeff Layton0c637be2014-08-22 12:05:43 -04003850 if (fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04003851 status = 0;
Jeff Layton67db1032014-12-13 09:11:40 -05003852 ++fp->fi_delegees;
Jeff Layton417c6622014-07-21 09:34:57 -04003853 hash_delegation_locked(dp, fp);
3854 goto out_unlock;
3855 }
Jeff Layton417c6622014-07-21 09:34:57 -04003856 fp->fi_deleg_file = filp;
Jeff Layton67db1032014-12-13 09:11:40 -05003857 fp->fi_delegees = 1;
Benny Halevy931ee562014-05-30 09:09:27 -04003858 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003859 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003860 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003861 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003862out_unlock:
3863 spin_unlock(&fp->fi_lock);
3864 spin_unlock(&state_lock);
3865out_fput:
3866 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003867 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003868}
3869
Jeff Layton0b266932014-07-25 07:34:25 -04003870static struct nfs4_delegation *
3871nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3872 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003873{
Jeff Layton0b266932014-07-25 07:34:25 -04003874 int status;
3875 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003876
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003877 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003878 return ERR_PTR(-EAGAIN);
3879
3880 dp = alloc_init_deleg(clp, fh);
3881 if (!dp)
3882 return ERR_PTR(-ENOMEM);
3883
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003884 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003885 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003886 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003887 dp->dl_stid.sc_file = fp;
Jeff Layton0c637be2014-08-22 12:05:43 -04003888 if (!fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04003889 spin_unlock(&fp->fi_lock);
3890 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003891 status = nfs4_setlease(dp);
3892 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003893 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003894 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003895 status = -EAGAIN;
3896 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003897 }
Jeff Layton67db1032014-12-13 09:11:40 -05003898 ++fp->fi_delegees;
Benny Halevy931ee562014-05-30 09:09:27 -04003899 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003900 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003901out_unlock:
3902 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003903 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003904out:
3905 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003906 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003907 return ERR_PTR(status);
3908 }
3909 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003910}
3911
Benny Halevy4aa89132012-02-21 14:16:44 -08003912static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3913{
3914 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3915 if (status == -EAGAIN)
3916 open->op_why_no_deleg = WND4_CONTENTION;
3917 else {
3918 open->op_why_no_deleg = WND4_RESOURCE;
3919 switch (open->op_deleg_want) {
3920 case NFS4_SHARE_WANT_READ_DELEG:
3921 case NFS4_SHARE_WANT_WRITE_DELEG:
3922 case NFS4_SHARE_WANT_ANY_DELEG:
3923 break;
3924 case NFS4_SHARE_WANT_CANCEL:
3925 open->op_why_no_deleg = WND4_CANCELLED;
3926 break;
3927 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003928 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003929 }
3930 }
3931}
3932
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933/*
3934 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003935 *
3936 * Note we don't support write delegations, and won't until the vfs has
3937 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 */
3939static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003940nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3941 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942{
3943 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003944 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3945 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003946 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003947 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003949 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003950 open->op_recall = 0;
3951 switch (open->op_claim_type) {
3952 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003953 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003954 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003955 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3956 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003957 break;
3958 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003959 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003960 /*
3961 * Let's not give out any delegations till everyone's
3962 * had the chance to reclaim theirs....
3963 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003964 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003965 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003966 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003967 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003968 /*
3969 * Also, if the file was opened for write or
3970 * create, there's a good chance the client's
3971 * about to write to it, resulting in an
3972 * immediate recall (since we don't support
3973 * write delegations):
3974 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003975 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003976 goto out_no_deleg;
3977 if (open->op_create == NFS4_OPEN_CREATE)
3978 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003979 break;
3980 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003981 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003982 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003983 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003984 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003985 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003987 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003989 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003990 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003991 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003992 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003993 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003994out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003995 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3996 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003997 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003998 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003999 open->op_recall = 1;
4000 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004001
4002 /* 4.1 client asking for a delegation? */
4003 if (open->op_deleg_want)
4004 nfsd4_open_deleg_none_ext(open, status);
4005 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006}
4007
Benny Halevye27f49c2012-02-21 14:16:54 -08004008static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4009 struct nfs4_delegation *dp)
4010{
4011 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4012 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4013 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4014 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4015 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4016 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4017 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4018 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4019 }
4020 /* Otherwise the client must be confused wanting a delegation
4021 * it already has, therefore we don't return
4022 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4023 */
4024}
4025
Al Virob37ad282006-10-19 23:28:59 -07004026__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4028{
Andy Adamson66689582009-04-03 08:28:45 +03004029 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004030 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004032 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004033 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004034 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 /*
4037 * Lookup file; if found, lookup stateid and check open request,
4038 * and check for delegations in the process of being recalled.
4039 * If not found, create the nfs4_file struct
4040 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004041 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004042 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004043 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004044 if (status)
4045 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04004046 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004048 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004049 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004050 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004051 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 }
4053
4054 /*
4055 * OPEN the file, or upgrade an existing OPEN.
4056 * If truncate fails, the OPEN fails.
4057 */
4058 if (stp) {
4059 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004060 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 if (status)
4062 goto out;
4063 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004064 stp = open->op_stp;
4065 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004066 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004067 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4068 if (status) {
4069 release_open_stateid(stp);
4070 goto out;
4071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004073 update_stateid(&stp->st_stid.sc_stateid);
4074 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075
Benny Halevyd24433c2012-02-16 20:57:17 +02004076 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004077 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4078 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4079 open->op_why_no_deleg = WND4_NOT_WANTED;
4080 goto nodeleg;
4081 }
4082 }
4083
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 /*
4085 * Attempt to hand out a delegation. No error return, because the
4086 * OPEN succeeds even if we fail.
4087 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004088 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004089nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 status = nfs_ok;
4091
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004092 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004093 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004095 /* 4.1 client trying to upgrade/downgrade delegation? */
4096 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004097 open->op_deleg_want)
4098 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004099
NeilBrown13cd2182005-06-23 22:03:10 -07004100 if (fp)
4101 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004102 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05004103 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 /*
4105 * To finish the open response, we just need to set the rflags.
4106 */
4107 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004108 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004109 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004111 if (dp)
4112 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004113 if (stp)
4114 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115
4116 return status;
4117}
4118
Jeff Layton58fb12e2014-07-29 21:34:27 -04004119void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04004120 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004121{
4122 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004123 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004124
Jeff Laytond3134b12014-07-29 21:34:32 -04004125 nfsd4_cstate_assign_replay(cstate, so);
4126 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004127 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004128 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04004129 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004130 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004131 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004132}
4133
Al Virob37ad282006-10-19 23:28:59 -07004134__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004135nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4136 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137{
4138 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004139 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004140 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 dprintk("process_renew(%08x/%08x): starting\n",
4143 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004144 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004145 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004147 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004149 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004150 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 goto out;
4152 status = nfs_ok;
4153out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 return status;
4155}
4156
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004157void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004158nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004159{
Jeff Layton33dcc482012-04-10 11:08:48 -04004160 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004161 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004162 return;
4163
NeilBrowna76b4312005-06-23 22:04:01 -07004164 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004165 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004166 /*
4167 * If the server goes down again right now, an NFSv4
4168 * client will still be allowed to reclaim after it comes back up,
4169 * even if it hasn't yet had a chance to reclaim state this time.
4170 *
4171 */
Jeff Layton919b8042014-09-12 16:40:20 -04004172 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004173 /*
4174 * At this point, NFSv4 clients can still reclaim. But if the
4175 * server crashes, any that have not yet reclaimed will be out
4176 * of luck on the next boot.
4177 *
4178 * (NFSv4.1+ clients are considered to have reclaimed once they
4179 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4180 * have reclaimed after their first OPEN.)
4181 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004182 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004183 /*
4184 * At this point, and once lockd and/or any other containers
4185 * exit their grace period, further reclaims will fail and
4186 * regular locking can resume.
4187 */
NeilBrowna76b4312005-06-23 22:04:01 -07004188}
4189
NeilBrownfd39ca92005-06-23 22:04:03 -07004190static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004191nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192{
4193 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004194 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004196 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004198 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004199 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004202 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004203 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004204 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004205 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 clp = list_entry(pos, struct nfs4_client, cl_lru);
4207 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4208 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004209 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 break;
4211 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004212 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004213 dprintk("NFSD: client in use (clientid %08x)\n",
4214 clp->cl_clientid.cl_id);
4215 continue;
4216 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004217 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004218 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004219 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004220 list_for_each_safe(pos, next, &reaplist) {
4221 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 dprintk("NFSD: purging unused client (clientid %08x)\n",
4223 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004224 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 expire_client(clp);
4226 }
Benny Halevycdc97502014-05-30 09:09:30 -04004227 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004228 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004230 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4231 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004233 t = dp->dl_time - cutoff;
4234 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 break;
4236 }
Jeff Layton42690672014-07-25 07:34:20 -04004237 unhash_delegation_locked(dp);
4238 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 }
Benny Halevycdc97502014-05-30 09:09:30 -04004240 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004241 while (!list_empty(&reaplist)) {
4242 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4243 dl_recall_lru);
4244 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004245 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 }
Jeff Layton217526e2014-07-30 08:27:11 -04004247
4248 spin_lock(&nn->client_lock);
4249 while (!list_empty(&nn->close_lru)) {
4250 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4251 oo_close_lru);
4252 if (time_after((unsigned long)oo->oo_time,
4253 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004254 t = oo->oo_time - cutoff;
4255 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 break;
4257 }
Jeff Layton217526e2014-07-30 08:27:11 -04004258 list_del_init(&oo->oo_close_lru);
4259 stp = oo->oo_last_closed_stid;
4260 oo->oo_last_closed_stid = NULL;
4261 spin_unlock(&nn->client_lock);
4262 nfs4_put_stid(&stp->st_stid);
4263 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 }
Jeff Layton217526e2014-07-30 08:27:11 -04004265 spin_unlock(&nn->client_lock);
4266
Jeff Laytona832e7a2014-05-30 09:09:26 -04004267 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004268 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269}
4270
Harvey Harrisona254b242008-02-20 12:49:00 -08004271static struct workqueue_struct *laundry_wq;
4272static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004273
4274static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004275laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276{
4277 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004278 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4279 work);
4280 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4281 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004283 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004285 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286}
4287
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004288static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004289{
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02004290 if (!fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004291 return nfserr_bad_stateid;
4292 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293}
4294
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004296access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004298 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4299 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4300 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301}
4302
4303static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004304access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004306 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4307 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308}
4309
4310static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004311__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312{
Al Virob37ad282006-10-19 23:28:59 -07004313 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314
J. Bruce Fields02921912010-07-29 15:16:59 -04004315 /* For lock stateid's, we test the parent open, not the lock: */
4316 if (stp->st_openstp)
4317 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004318 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004320 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 goto out;
4322 status = nfs_ok;
4323out:
4324 return status;
4325}
4326
Al Virob37ad282006-10-19 23:28:59 -07004327static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004328check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004330 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004332 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004333 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 * conflicting state; so we must wait out the grace period. */
4335 return nfserr_grace;
4336 } else if (flags & WR_STATE)
4337 return nfs4_share_conflict(current_fh,
4338 NFS4_SHARE_DENY_WRITE);
4339 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4340 return nfs4_share_conflict(current_fh,
4341 NFS4_SHARE_DENY_READ);
4342}
4343
4344/*
4345 * Allow READ/WRITE during grace period on recovered state only for files
4346 * that are not able to provide mandatory locking.
4347 */
4348static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004349grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004351 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352}
4353
J. Bruce Fields81b82962011-08-23 11:03:29 -04004354/* Returns true iff a is later than b: */
4355static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4356{
Jim Rees1a9357f2013-05-17 17:33:00 -04004357 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004358}
4359
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004360static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004361{
Andy Adamson66689582009-04-03 08:28:45 +03004362 /*
4363 * When sessions are used the stateid generation number is ignored
4364 * when it is zero.
4365 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004366 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004367 return nfs_ok;
4368
4369 if (in->si_generation == ref->si_generation)
4370 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004371
J. Bruce Fields0836f582008-01-26 19:08:12 -05004372 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004373 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004374 return nfserr_bad_stateid;
4375 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004376 * However, we could see a stateid from the past, even from a
4377 * non-buggy client. For example, if the client sends a lock
4378 * while some IO is outstanding, the lock may bump si_generation
4379 * while the IO is still in flight. The client could avoid that
4380 * situation by waiting for responses on all the IO requests,
4381 * but better performance may result in retrying IO that
4382 * receives an old_stateid error if requests are rarely
4383 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004384 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004385 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004386}
4387
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004388static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4389{
4390 if (ols->st_stateowner->so_is_open_owner &&
4391 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4392 return nfserr_bad_stateid;
4393 return nfs_ok;
4394}
4395
Chuck Lever7df302f2012-05-29 13:56:37 -04004396static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004397{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004398 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004399 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004400
Chuck Lever7df302f2012-05-29 13:56:37 -04004401 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004402 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004403 /* Client debugging aid. */
4404 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4405 char addr_str[INET6_ADDRSTRLEN];
4406 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4407 sizeof(addr_str));
4408 pr_warn_ratelimited("NFSD: client %s testing state ID "
4409 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004410 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004411 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004412 spin_lock(&cl->cl_lock);
4413 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004414 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004415 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004416 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004417 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004418 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004419 switch (s->sc_type) {
4420 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004421 status = nfs_ok;
4422 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004423 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004424 status = nfserr_deleg_revoked;
4425 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004426 case NFS4_OPEN_STID:
4427 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004428 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04004429 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004430 default:
4431 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004432 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004433 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004434 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004435 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004436 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004437out_unlock:
4438 spin_unlock(&cl->cl_lock);
4439 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004440}
4441
Christoph Hellwigcd61c522014-08-14 08:44:57 +02004442__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004443nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4444 stateid_t *stateid, unsigned char typemask,
4445 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004446{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004447 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004448
4449 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4450 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004451 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004452 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004453 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004454 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004455 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004456 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004457 if (status)
4458 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004459 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004460 if (!*s)
4461 return nfserr_bad_stateid;
4462 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004463}
4464
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465/*
4466* Checks for stateid operations
4467*/
Al Virob37ad282006-10-19 23:28:59 -07004468__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004469nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004470 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004472 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004473 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004475 struct svc_fh *current_fh = &cstate->current_fh;
David Howells2b0143b2015-03-17 22:25:59 +00004476 struct inode *ino = d_inode(current_fh->fh_dentry);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004477 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004478 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004479 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 if (filpp)
4482 *filpp = NULL;
4483
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004484 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 return nfserr_grace;
4486
4487 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004488 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004490 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004491 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004492 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004493 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04004494 return status;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004495 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4496 if (status)
4497 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004498 switch (s->sc_type) {
4499 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004500 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004501 status = nfs4_check_delegmode(dp, flags);
4502 if (status)
4503 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004504 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004505 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004506 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004507 WARN_ON_ONCE(1);
4508 status = nfserr_serverfault;
4509 goto out;
4510 }
Trond Myklebustde186432014-07-10 14:07:26 -04004511 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004512 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004513 break;
4514 case NFS4_OPEN_STID:
4515 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004516 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004517 status = nfs4_check_fh(current_fh, stp);
4518 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 goto out;
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004520 status = nfsd4_check_openowner_confirmed(stp);
4521 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004523 status = nfs4_check_openmode(stp, flags);
4524 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004526 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004527 struct nfs4_file *fp = stp->st_stid.sc_file;
4528
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004529 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004530 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004531 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004532 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004533 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004534 break;
4535 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004536 status = nfserr_bad_stateid;
4537 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 }
4539 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004540 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004541 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004543 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 return status;
4545}
4546
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004547/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004548 * Test if the stateid is valid
4549 */
4550__be32
4551nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4552 struct nfsd4_test_stateid *test_stateid)
4553{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004554 struct nfsd4_test_stateid_id *stateid;
4555 struct nfs4_client *cl = cstate->session->se_client;
4556
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004557 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004558 stateid->ts_id_status =
4559 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004560
Bryan Schumaker17456802011-07-13 10:50:48 -04004561 return nfs_ok;
4562}
4563
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004564__be32
4565nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4566 struct nfsd4_free_stateid *free_stateid)
4567{
4568 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004569 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004570 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004571 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004572 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004573 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004574
Jeff Layton1af71cc2014-07-29 21:34:14 -04004575 spin_lock(&cl->cl_lock);
4576 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004577 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004578 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004579 switch (s->sc_type) {
4580 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004581 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004582 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004583 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004584 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4585 if (ret)
4586 break;
4587 ret = nfserr_locks_held;
4588 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004589 case NFS4_LOCK_STID:
4590 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4591 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004592 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004593 stp = openlockstateid(s);
4594 ret = nfserr_locks_held;
4595 if (check_for_locks(stp->st_stid.sc_file,
4596 lockowner(stp->st_stateowner)))
4597 break;
4598 unhash_lock_stateid(stp);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004599 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004600 nfs4_put_stid(s);
4601 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004602 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004603 case NFS4_REVOKED_DELEG_STID:
4604 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004605 list_del_init(&dp->dl_recall_lru);
4606 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004607 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004608 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004609 goto out;
4610 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004611 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004612out_unlock:
4613 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004614out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004615 return ret;
4616}
4617
NeilBrown4c4cd222005-07-07 17:59:27 -07004618static inline int
4619setlkflg (int type)
4620{
4621 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4622 RD_STATE : WR_STATE;
4623}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004625static __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 -04004626{
4627 struct svc_fh *current_fh = &cstate->current_fh;
4628 struct nfs4_stateowner *sop = stp->st_stateowner;
4629 __be32 status;
4630
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004631 status = nfsd4_check_seqid(cstate, sop, seqid);
4632 if (status)
4633 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004634 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4635 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004636 /*
4637 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004638 * nfserr_replay_me from the previous step, and
4639 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004640 */
4641 return nfserr_bad_stateid;
4642 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4643 if (status)
4644 return status;
4645 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004646}
4647
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648/*
4649 * Checks for sequence id mutating operations.
4650 */
Al Virob37ad282006-10-19 23:28:59 -07004651static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004652nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004653 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004654 struct nfs4_ol_stateid **stpp,
4655 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004657 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004658 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004659 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004661 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4662 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004663
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004665 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004666 if (status)
4667 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004668 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004669 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004671 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004672 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004673 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004674 else
4675 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004676 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004677}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004678
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004679static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4680 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004681{
4682 __be32 status;
4683 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004684 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004686 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004687 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004688 if (status)
4689 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004690 oo = openowner(stp->st_stateowner);
4691 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4692 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004693 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004694 }
4695 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004696 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697}
4698
Al Virob37ad282006-10-19 23:28:59 -07004699__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004700nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004701 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702{
Al Virob37ad282006-10-19 23:28:59 -07004703 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004704 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004705 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004706 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707
Al Viroa6a9f182013-09-16 10:57:01 -04004708 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4709 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004711 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004712 if (status)
4713 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004715 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004716 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004717 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004718 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004719 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004720 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004721 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004722 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004723 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004724 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004725 update_stateid(&stp->st_stid.sc_stateid);
4726 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004727 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004728 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004729
Jeff Layton2a4317c2012-03-21 16:42:43 -04004730 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004731 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004732put_stateid:
4733 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004735 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 return status;
4737}
4738
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004739static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004741 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004742 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004743 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004744 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004745}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004746
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004747static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4748{
4749 switch (to_access) {
4750 case NFS4_SHARE_ACCESS_READ:
4751 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4752 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4753 break;
4754 case NFS4_SHARE_ACCESS_WRITE:
4755 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4756 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4757 break;
4758 case NFS4_SHARE_ACCESS_BOTH:
4759 break;
4760 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004761 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 }
4763}
4764
Al Virob37ad282006-10-19 23:28:59 -07004765__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004766nfsd4_open_downgrade(struct svc_rqst *rqstp,
4767 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004768 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769{
Al Virob37ad282006-10-19 23:28:59 -07004770 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004771 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004772 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773
Al Viroa6a9f182013-09-16 10:57:01 -04004774 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4775 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004777 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004778 if (od->od_deleg_want)
4779 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4780 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004782 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004783 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004784 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004787 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004788 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004790 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004792 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004793 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004795 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004797 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
Jeff Laytonce0fc432012-05-11 09:45:14 -04004799 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004801 update_stateid(&stp->st_stid.sc_stateid);
4802 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004804put_stateid:
4805 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004807 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 return status;
4809}
4810
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004811static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4812{
Trond Myklebustacf92952014-06-30 11:48:37 -04004813 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytond83017f2014-07-29 21:34:42 -04004814 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004815
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004816 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04004817 spin_lock(&clp->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04004818 unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004819
Jeff Laytond83017f2014-07-29 21:34:42 -04004820 if (clp->cl_minorversion) {
4821 put_ol_stateid_locked(s, &reaplist);
4822 spin_unlock(&clp->cl_lock);
4823 free_ol_stateid_reaplist(&reaplist);
4824 } else {
4825 spin_unlock(&clp->cl_lock);
4826 free_ol_stateid_reaplist(&reaplist);
Jeff Laytond3134b12014-07-29 21:34:32 -04004827 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04004828 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004829}
4830
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831/*
4832 * nfs4_unlock_state() called after encode
4833 */
Al Virob37ad282006-10-19 23:28:59 -07004834__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004835nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004836 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837{
Al Virob37ad282006-10-19 23:28:59 -07004838 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004839 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004840 struct net *net = SVC_NET(rqstp);
4841 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842
Al Viroa6a9f182013-09-16 10:57:01 -04004843 dprintk("NFSD: nfsd4_close on file %pd\n",
4844 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004846 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4847 &close->cl_stateid,
4848 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004849 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004850 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004851 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004853 update_stateid(&stp->st_stid.sc_stateid);
4854 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004856 nfsd4_return_all_file_layouts(stp->st_stateowner->so_client,
4857 stp->st_stid.sc_file);
4858
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004859 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004860
4861 /* put reference from nfs4_preprocess_seqid_op */
4862 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 return status;
4865}
4866
Al Virob37ad282006-10-19 23:28:59 -07004867__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004868nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4869 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004871 struct nfs4_delegation *dp;
4872 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004873 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004874 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004875 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004877 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004878 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004880 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004881 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004882 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004883 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004884 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004885 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004886 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004887
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004888 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004889put_stateid:
4890 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891out:
4892 return status;
4893}
4894
4895
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
Benny Halevy87df4de2008-12-15 19:42:03 +02004898static inline u64
4899end_offset(u64 start, u64 len)
4900{
4901 u64 end;
4902
4903 end = start + len;
4904 return end >= start ? end: NFS4_MAX_UINT64;
4905}
4906
4907/* last octet in a range */
4908static inline u64
4909last_byte_offset(u64 start, u64 len)
4910{
4911 u64 end;
4912
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004913 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004914 end = start + len;
4915 return end > start ? end - 1: NFS4_MAX_UINT64;
4916}
4917
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918/*
4919 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4920 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4921 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4922 * locking, this prevents us from being completely protocol-compliant. The
4923 * real solution to this problem is to start using unsigned file offsets in
4924 * the VFS, but this is a very deep change!
4925 */
4926static inline void
4927nfs4_transform_lock_offset(struct file_lock *lock)
4928{
4929 if (lock->fl_start < 0)
4930 lock->fl_start = OFFSET_MAX;
4931 if (lock->fl_end < 0)
4932 lock->fl_end = OFFSET_MAX;
4933}
4934
Jeff Laytoncae80b32015-04-03 09:04:04 -04004935static fl_owner_t
4936nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04004937{
Jeff Laytoncae80b32015-04-03 09:04:04 -04004938 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
4939
4940 nfs4_get_stateowner(&lo->lo_owner);
4941 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04004942}
4943
Jeff Laytoncae80b32015-04-03 09:04:04 -04004944static void
4945nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04004946{
Jeff Laytoncae80b32015-04-03 09:04:04 -04004947 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04004948
Jeff Laytoncae80b32015-04-03 09:04:04 -04004949 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04004950 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04004951}
4952
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004953static const struct lock_manager_operations nfsd_posix_mng_ops = {
Kinglong Meeaef95832014-08-22 10:18:44 -04004954 .lm_get_owner = nfsd4_fl_get_owner,
4955 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07004956};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
4958static inline void
4959nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4960{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004961 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962
NeilBrownd5b90262006-04-10 22:55:22 -07004963 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004964 lo = (struct nfs4_lockowner *) fl->fl_owner;
4965 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4966 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004967 if (!deny->ld_owner.data)
4968 /* We just don't care that much */
4969 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004970 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4971 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004972 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004973nevermind:
4974 deny->ld_owner.len = 0;
4975 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004976 deny->ld_clientid.cl_boot = 0;
4977 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 }
4979 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004980 deny->ld_length = NFS4_MAX_UINT64;
4981 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4983 deny->ld_type = NFS4_READ_LT;
4984 if (fl->fl_type != F_RDLCK)
4985 deny->ld_type = NFS4_WRITE_LT;
4986}
4987
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004988static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004989find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004990 struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004992 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004993 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994
Trond Myklebust0a880a22014-07-30 08:27:10 -04004995 lockdep_assert_held(&clp->cl_lock);
4996
Trond Myklebustd4f04892014-07-29 21:34:36 -04004997 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4998 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004999 if (so->so_is_open_owner)
5000 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005001 if (same_owner_str(so, owner))
5002 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 }
5004 return NULL;
5005}
5006
Trond Myklebustc58c6612014-07-29 21:34:35 -04005007static struct nfs4_lockowner *
5008find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005009 struct nfs4_client *clp)
Trond Myklebustc58c6612014-07-29 21:34:35 -04005010{
5011 struct nfs4_lockowner *lo;
5012
Trond Myklebustd4f04892014-07-29 21:34:36 -04005013 spin_lock(&clp->cl_lock);
5014 lo = find_lockowner_str_locked(clid, owner, clp);
5015 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005016 return lo;
5017}
5018
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005019static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5020{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005021 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005022}
5023
Jeff Layton6b180f02014-07-29 21:34:26 -04005024static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5025{
5026 struct nfs4_lockowner *lo = lockowner(sop);
5027
5028 kmem_cache_free(lockowner_slab, lo);
5029}
5030
5031static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005032 .so_unhash = nfs4_unhash_lockowner,
5033 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005034};
5035
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036/*
5037 * Alloc a lock owner structure.
5038 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005039 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005041 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005043static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005044alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5045 struct nfs4_ol_stateid *open_stp,
5046 struct nfsd4_lock *lock)
5047{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005048 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005050 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5051 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005053 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5054 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005055 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005056 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005057 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005058 ret = find_lockowner_str_locked(&clp->cl_clientid,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005059 &lock->lk_new_owner, clp);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005060 if (ret == NULL) {
5061 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005062 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005063 ret = lo;
5064 } else
5065 nfs4_free_lockowner(&lo->lo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005066 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04005067 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068}
5069
Jeff Layton356a95e2014-07-29 21:34:13 -04005070static void
5071init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5072 struct nfs4_file *fp, struct inode *inode,
5073 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005075 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076
Jeff Layton356a95e2014-07-29 21:34:13 -04005077 lockdep_assert_held(&clp->cl_lock);
5078
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005079 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005080 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005081 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07005082 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005083 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005084 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005085 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005087 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005088 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005089 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005090 spin_lock(&fp->fi_lock);
5091 list_add(&stp->st_perfile, &fp->fi_stateids);
5092 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093}
5094
Jeff Laytonc53530d2014-06-30 11:48:39 -04005095static struct nfs4_ol_stateid *
5096find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5097{
5098 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005099 struct nfs4_client *clp = lo->lo_owner.so_client;
5100
5101 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005102
5103 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005104 if (lst->st_stid.sc_file == fp) {
5105 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005106 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005107 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005108 }
5109 return NULL;
5110}
5111
Jeff Layton356a95e2014-07-29 21:34:13 -04005112static struct nfs4_ol_stateid *
5113find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5114 struct inode *inode, struct nfs4_ol_stateid *ost,
5115 bool *new)
5116{
5117 struct nfs4_stid *ns = NULL;
5118 struct nfs4_ol_stateid *lst;
5119 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5120 struct nfs4_client *clp = oo->oo_owner.so_client;
5121
5122 spin_lock(&clp->cl_lock);
5123 lst = find_lock_stateid(lo, fi);
5124 if (lst == NULL) {
5125 spin_unlock(&clp->cl_lock);
5126 ns = nfs4_alloc_stid(clp, stateid_slab);
5127 if (ns == NULL)
5128 return NULL;
5129
5130 spin_lock(&clp->cl_lock);
5131 lst = find_lock_stateid(lo, fi);
5132 if (likely(!lst)) {
5133 lst = openlockstateid(ns);
5134 init_lock_stateid(lst, lo, fi, inode, ost);
5135 ns = NULL;
5136 *new = true;
5137 }
5138 }
5139 spin_unlock(&clp->cl_lock);
5140 if (ns)
5141 nfs4_put_stid(ns);
5142 return lst;
5143}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005144
NeilBrownfd39ca92005-06-23 22:04:03 -07005145static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146check_lock_length(u64 offset, u64 length)
5147{
Benny Halevy87df4de2008-12-15 19:42:03 +02005148 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 LOFF_OVERFLOW(offset, length)));
5150}
5151
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005152static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005153{
Trond Myklebust11b91642014-07-29 21:34:08 -04005154 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005155
Jeff Layton7214e862014-07-10 14:07:33 -04005156 lockdep_assert_held(&fp->fi_lock);
5157
Jeff Layton82c5ff12012-05-11 09:45:13 -04005158 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005159 return;
Jeff Layton12659652014-07-10 14:07:28 -04005160 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005161 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005162}
5163
Jeff Layton356a95e2014-07-29 21:34:13 -04005164static __be32
5165lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5166 struct nfs4_ol_stateid *ost,
5167 struct nfsd4_lock *lock,
5168 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005169{
Jeff Layton5db1c032014-07-29 21:34:28 -04005170 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005171 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005172 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5173 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00005174 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005175 struct nfs4_lockowner *lo;
5176 unsigned int strhashval;
5177
Trond Myklebustd4f04892014-07-29 21:34:36 -04005178 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005179 if (!lo) {
Trond Myklebustd4f04892014-07-29 21:34:36 -04005180 strhashval = ownerstr_hashval(&lock->v.new.owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005181 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5182 if (lo == NULL)
5183 return nfserr_jukebox;
5184 } else {
5185 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005186 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005187 if (!cstate->minorversion &&
5188 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005189 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005190 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005191
Jeff Layton356a95e2014-07-29 21:34:13 -04005192 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005193 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005194 status = nfserr_jukebox;
5195 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005196 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005197 status = nfs_ok;
5198out:
5199 nfs4_put_stateowner(&lo->lo_owner);
5200 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005201}
5202
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203/*
5204 * LOCK operation
5205 */
Al Virob37ad282006-10-19 23:28:59 -07005206__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005207nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005208 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005210 struct nfs4_openowner *open_sop = NULL;
5211 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005212 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005213 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005214 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005215 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005216 struct file_lock *file_lock = NULL;
5217 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005218 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005219 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005220 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005221 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005222 struct net *net = SVC_NET(rqstp);
5223 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224
5225 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5226 (long long) lock->lk_offset,
5227 (long long) lock->lk_length);
5228
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 if (check_lock_length(lock->lk_offset, lock->lk_length))
5230 return nfserr_inval;
5231
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005232 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005233 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005234 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5235 return status;
5236 }
5237
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005239 if (nfsd4_has_session(cstate))
5240 /* See rfc 5661 18.10.3: given clientid is ignored: */
5241 memcpy(&lock->v.new.clientid,
5242 &cstate->session->se_client->cl_clientid,
5243 sizeof(clientid_t));
5244
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005246 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005250 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 lock->lk_new_open_seqid,
5252 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005253 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005254 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005256 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005257 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005258 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005259 &lock->v.new.clientid))
5260 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005261 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005262 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005263 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005264 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005265 lock->lk_old_lock_seqid,
5266 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005267 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005268 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005269 if (status)
5270 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005271 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005273 lkflg = setlkflg(lock->lk_type);
5274 status = nfs4_check_openmode(lock_stp, lkflg);
5275 if (status)
5276 goto out;
5277
NeilBrown0dd395d2005-07-07 17:59:15 -07005278 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005279 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005280 goto out;
5281 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005282 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005283 goto out;
5284
Jeff Layton21179d82012-08-21 08:03:32 -04005285 file_lock = locks_alloc_lock();
5286 if (!file_lock) {
5287 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5288 status = nfserr_jukebox;
5289 goto out;
5290 }
5291
Trond Myklebust11b91642014-07-29 21:34:08 -04005292 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 switch (lock->lk_type) {
5294 case NFS4_READ_LT:
5295 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005296 spin_lock(&fp->fi_lock);
5297 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005298 if (filp)
5299 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005300 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005301 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005302 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 case NFS4_WRITE_LT:
5304 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005305 spin_lock(&fp->fi_lock);
5306 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005307 if (filp)
5308 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005309 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005310 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005311 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 default:
5313 status = nfserr_inval;
5314 goto out;
5315 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005316 if (!filp) {
5317 status = nfserr_openmode;
5318 goto out;
5319 }
Kinglong Meeaef95832014-08-22 10:18:44 -04005320
5321 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04005322 file_lock->fl_pid = current->tgid;
5323 file_lock->fl_file = filp;
5324 file_lock->fl_flags = FL_POSIX;
5325 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5326 file_lock->fl_start = lock->lk_offset;
5327 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5328 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329
Jeff Layton21179d82012-08-21 08:03:32 -04005330 conflock = locks_alloc_lock();
5331 if (!conflock) {
5332 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5333 status = nfserr_jukebox;
5334 goto out;
5335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336
Jeff Layton21179d82012-08-21 08:03:32 -04005337 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005338 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005340 update_stateid(&lock_stp->st_stid.sc_stateid);
5341 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005343 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005344 break;
5345 case (EAGAIN): /* conflock holds conflicting lock */
5346 status = nfserr_denied;
5347 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005348 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005349 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 case (EDEADLK):
5351 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005352 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005353 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005354 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005355 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005356 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358out:
Trond Myklebustde186432014-07-10 14:07:26 -04005359 if (filp)
5360 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005361 if (lock_stp) {
5362 /* Bump seqid manually if the 4.0 replay owner is openowner */
5363 if (cstate->replay_owner &&
5364 cstate->replay_owner != &lock_sop->lo_owner &&
5365 seqid_mutating_err(ntohl(status)))
5366 lock_sop->lo_owner.so_seqid++;
5367
5368 /*
5369 * If this is a new, never-before-used stateid, and we are
5370 * returning an error, then just go ahead and release it.
5371 */
5372 if (status && new)
5373 release_lock_stateid(lock_stp);
5374
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005375 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005376 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005377 if (open_stp)
5378 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005379 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005380 if (file_lock)
5381 locks_free_lock(file_lock);
5382 if (conflock)
5383 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 return status;
5385}
5386
5387/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005388 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5389 * so we do a temporary open here just to get an open file to pass to
5390 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5391 * inode operation.)
5392 */
Al Viro04da6e92012-04-13 00:00:04 -04005393static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005394{
5395 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005396 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5397 if (!err) {
5398 err = nfserrno(vfs_test_lock(file, lock));
5399 nfsd_close(file);
5400 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005401 return err;
5402}
5403
5404/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 * LOCKT operation
5406 */
Al Virob37ad282006-10-19 23:28:59 -07005407__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005408nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5409 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410{
Jeff Layton21179d82012-08-21 08:03:32 -04005411 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005412 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005413 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005414 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005416 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 return nfserr_grace;
5418
5419 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5420 return nfserr_inval;
5421
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005422 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005423 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005424 if (status)
5425 goto out;
5426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005428 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430
Jeff Layton21179d82012-08-21 08:03:32 -04005431 file_lock = locks_alloc_lock();
5432 if (!file_lock) {
5433 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5434 status = nfserr_jukebox;
5435 goto out;
5436 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005437
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 switch (lockt->lt_type) {
5439 case NFS4_READ_LT:
5440 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005441 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 break;
5443 case NFS4_WRITE_LT:
5444 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005445 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 break;
5447 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005448 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 status = nfserr_inval;
5450 goto out;
5451 }
5452
Trond Myklebustd4f04892014-07-29 21:34:36 -04005453 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5454 cstate->clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005455 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005456 file_lock->fl_owner = (fl_owner_t)lo;
5457 file_lock->fl_pid = current->tgid;
5458 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459
Jeff Layton21179d82012-08-21 08:03:32 -04005460 file_lock->fl_start = lockt->lt_offset;
5461 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462
Jeff Layton21179d82012-08-21 08:03:32 -04005463 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464
Jeff Layton21179d82012-08-21 08:03:32 -04005465 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005466 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005467 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005468
Jeff Layton21179d82012-08-21 08:03:32 -04005469 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005471 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 }
5473out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005474 if (lo)
5475 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04005476 if (file_lock)
5477 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 return status;
5479}
5480
Al Virob37ad282006-10-19 23:28:59 -07005481__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005482nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005483 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005485 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005487 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005488 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005489 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005490 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5491
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5493 (long long) locku->lu_offset,
5494 (long long) locku->lu_length);
5495
5496 if (check_lock_length(locku->lu_offset, locku->lu_length))
5497 return nfserr_inval;
5498
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005499 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005500 &locku->lu_stateid, NFS4_LOCK_STID,
5501 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005502 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005504 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005505 if (!filp) {
5506 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005507 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005508 }
Jeff Layton21179d82012-08-21 08:03:32 -04005509 file_lock = locks_alloc_lock();
5510 if (!file_lock) {
5511 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5512 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005513 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005514 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005515
Jeff Layton21179d82012-08-21 08:03:32 -04005516 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04005517 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04005518 file_lock->fl_pid = current->tgid;
5519 file_lock->fl_file = filp;
5520 file_lock->fl_flags = FL_POSIX;
5521 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5522 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523
Jeff Layton21179d82012-08-21 08:03:32 -04005524 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5525 locku->lu_length);
5526 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527
Jeff Layton21179d82012-08-21 08:03:32 -04005528 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005529 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005530 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 goto out_nfserr;
5532 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005533 update_stateid(&stp->st_stid.sc_stateid);
5534 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005535fput:
5536 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005537put_stateid:
5538 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005540 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005541 if (file_lock)
5542 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 return status;
5544
5545out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005546 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005547 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548}
5549
5550/*
5551 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005552 * true: locks held by lockowner
5553 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005555static bool
5556check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005558 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005559 int status = false;
5560 struct file *filp = find_any_file(fp);
5561 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005562 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005563
5564 if (!filp) {
5565 /* Any valid lock stateid should have some sort of access */
5566 WARN_ON_ONCE(1);
5567 return status;
5568 }
5569
5570 inode = file_inode(filp);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005571 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005573 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05005574 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005575 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
5576 if (fl->fl_owner == (fl_owner_t)lowner) {
5577 status = true;
5578 break;
5579 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005580 }
Jeff Layton6109c852015-01-16 15:05:57 -05005581 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 }
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005583 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 return status;
5585}
5586
Al Virob37ad282006-10-19 23:28:59 -07005587__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005588nfsd4_release_lockowner(struct svc_rqst *rqstp,
5589 struct nfsd4_compound_state *cstate,
5590 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591{
5592 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005593 struct nfs4_stateowner *sop;
5594 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005595 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005597 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005598 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005599 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005600 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601
5602 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5603 clid->cl_boot, clid->cl_id);
5604
Jeff Layton4b24ca72014-06-30 11:48:44 -04005605 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005606 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04005607 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005608
Trond Myklebustd4f04892014-07-29 21:34:36 -04005609 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005610 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005611 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005612 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005613 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005614
5615 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005616 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005617
Jeff Layton882e9d22014-07-29 21:34:37 -04005618 /* see if there are still any locks associated with it */
5619 lo = lockowner(sop);
5620 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5621 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5622 status = nfserr_locks_held;
5623 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04005624 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04005625 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005626 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005627
Kinglong Meeb5971af2014-08-22 10:18:43 -04005628 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04005629 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005630 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005631 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005632 if (lo)
5633 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 return status;
5635}
5636
5637static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005638alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639{
NeilBrowna55370a2005-06-23 22:03:52 -07005640 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641}
5642
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005643bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005644nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005645{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005646 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005647
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005648 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005649 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005650}
5651
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652/*
5653 * failure => all reset bets are off, nfserr_no_grace...
5654 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005655struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005656nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657{
5658 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005659 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660
NeilBrowna55370a2005-06-23 22:03:52 -07005661 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5662 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005663 if (crp) {
5664 strhashval = clientstr_hashval(name);
5665 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005666 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005667 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005668 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005669 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005670 }
5671 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672}
5673
Jeff Layton2a4317c2012-03-21 16:42:43 -04005674void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005675nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005676{
5677 list_del(&crp->cr_strhash);
5678 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005679 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005680}
5681
5682void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005683nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684{
5685 struct nfs4_client_reclaim *crp = NULL;
5686 int i;
5687
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005689 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5690 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005692 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 }
5694 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005695 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696}
5697
5698/*
5699 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005700struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005701nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702{
5703 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 struct nfs4_client_reclaim *crp = NULL;
5705
Jeff Layton278c9312012-11-12 15:00:52 -05005706 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707
Jeff Layton278c9312012-11-12 15:00:52 -05005708 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005709 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005710 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 return crp;
5712 }
5713 }
5714 return NULL;
5715}
5716
5717/*
5718* Called from OPEN. Look for clientid in reclaim list.
5719*/
Al Virob37ad282006-10-19 23:28:59 -07005720__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005721nfs4_check_open_reclaim(clientid_t *clid,
5722 struct nfsd4_compound_state *cstate,
5723 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005725 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005726
5727 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005728 status = lookup_clientid(clid, cstate, nn);
5729 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005730 return nfserr_reclaim_bad;
5731
Jeff Layton3b3e7b72014-09-12 16:40:22 -04005732 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
5733 return nfserr_no_grace;
5734
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005735 if (nfsd4_client_record_check(cstate->clp))
5736 return nfserr_reclaim_bad;
5737
5738 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739}
5740
Bryan Schumaker65178db2011-11-01 13:35:21 -04005741#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04005742static inline void
5743put_client(struct nfs4_client *clp)
5744{
5745 atomic_dec(&clp->cl_refcount);
5746}
5747
Jeff Layton285abde2014-07-30 08:27:24 -04005748static struct nfs4_client *
5749nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5750{
5751 struct nfs4_client *clp;
5752 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5753 nfsd_net_id);
5754
5755 if (!nfsd_netns_ready(nn))
5756 return NULL;
5757
5758 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5759 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5760 return clp;
5761 }
5762 return NULL;
5763}
5764
Jeff Layton7ec0e362014-07-30 08:27:17 -04005765u64
Jeff Layton285abde2014-07-30 08:27:24 -04005766nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04005767{
5768 struct nfs4_client *clp;
5769 u64 count = 0;
5770 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5771 nfsd_net_id);
5772 char buf[INET6_ADDRSTRLEN];
5773
5774 if (!nfsd_netns_ready(nn))
5775 return 0;
5776
5777 spin_lock(&nn->client_lock);
5778 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5779 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5780 pr_info("NFS Client: %s\n", buf);
5781 ++count;
5782 }
5783 spin_unlock(&nn->client_lock);
5784
5785 return count;
5786}
Bryan Schumaker65178db2011-11-01 13:35:21 -04005787
Jeff Laytona0926d12014-07-30 08:27:18 -04005788u64
Jeff Layton285abde2014-07-30 08:27:24 -04005789nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04005790{
5791 u64 count = 0;
5792 struct nfs4_client *clp;
5793 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5794 nfsd_net_id);
5795
5796 if (!nfsd_netns_ready(nn))
5797 return count;
5798
5799 spin_lock(&nn->client_lock);
5800 clp = nfsd_find_client(addr, addr_size);
5801 if (clp) {
5802 if (mark_client_expired_locked(clp) == nfs_ok)
5803 ++count;
5804 else
5805 clp = NULL;
5806 }
5807 spin_unlock(&nn->client_lock);
5808
5809 if (clp)
5810 expire_client(clp);
5811
5812 return count;
5813}
5814
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005815u64
Jeff Layton285abde2014-07-30 08:27:24 -04005816nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005817{
5818 u64 count = 0;
5819 struct nfs4_client *clp, *next;
5820 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5821 nfsd_net_id);
5822 LIST_HEAD(reaplist);
5823
5824 if (!nfsd_netns_ready(nn))
5825 return count;
5826
5827 spin_lock(&nn->client_lock);
5828 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5829 if (mark_client_expired_locked(clp) == nfs_ok) {
5830 list_add(&clp->cl_lru, &reaplist);
5831 if (max != 0 && ++count >= max)
5832 break;
5833 }
5834 }
5835 spin_unlock(&nn->client_lock);
5836
5837 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
5838 expire_client(clp);
5839
5840 return count;
5841}
5842
Bryan Schumaker184c1842012-11-29 11:40:44 -05005843static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5844 const char *type)
5845{
5846 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005847 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005848 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5849}
5850
Jeff Layton016200c2014-07-30 08:27:21 -04005851static void
5852nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
5853 struct list_head *collect)
5854{
5855 struct nfs4_client *clp = lst->st_stid.sc_client;
5856 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5857 nfsd_net_id);
5858
5859 if (!collect)
5860 return;
5861
5862 lockdep_assert_held(&nn->client_lock);
5863 atomic_inc(&clp->cl_refcount);
5864 list_add(&lst->st_locks, collect);
5865}
5866
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005867static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04005868 struct list_head *collect,
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005869 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005870{
5871 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005872 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005873 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005874 u64 count = 0;
5875
Jeff Layton016200c2014-07-30 08:27:21 -04005876 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005877 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005878 list_for_each_entry_safe(stp, st_next,
5879 &oop->oo_owner.so_stateids, st_perstateowner) {
5880 list_for_each_entry_safe(lst, lst_next,
5881 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04005882 if (func) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005883 func(lst);
Jeff Layton016200c2014-07-30 08:27:21 -04005884 nfsd_inject_add_lock_to_list(lst,
5885 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04005886 }
Jeff Layton016200c2014-07-30 08:27:21 -04005887 ++count;
5888 /*
5889 * Despite the fact that these functions deal
5890 * with 64-bit integers for "count", we must
5891 * ensure that it doesn't blow up the
5892 * clp->cl_refcount. Throw a warning if we
5893 * start to approach INT_MAX here.
5894 */
5895 WARN_ON_ONCE(count == (INT_MAX / 2));
5896 if (count == max)
5897 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005898 }
5899 }
5900 }
Jeff Layton016200c2014-07-30 08:27:21 -04005901out:
5902 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005903
5904 return count;
5905}
5906
Jeff Layton016200c2014-07-30 08:27:21 -04005907static u64
5908nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
5909 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05005910{
Jeff Layton016200c2014-07-30 08:27:21 -04005911 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005912}
5913
Jeff Layton016200c2014-07-30 08:27:21 -04005914static u64
5915nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05005916{
Jeff Layton016200c2014-07-30 08:27:21 -04005917 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005918 nfsd_print_count(clp, count, "locked files");
5919 return count;
5920}
5921
Jeff Layton016200c2014-07-30 08:27:21 -04005922u64
Jeff Layton285abde2014-07-30 08:27:24 -04005923nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04005924{
5925 struct nfs4_client *clp;
5926 u64 count = 0;
5927 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5928 nfsd_net_id);
5929
5930 if (!nfsd_netns_ready(nn))
5931 return 0;
5932
5933 spin_lock(&nn->client_lock);
5934 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5935 count += nfsd_print_client_locks(clp);
5936 spin_unlock(&nn->client_lock);
5937
5938 return count;
5939}
5940
5941static void
5942nfsd_reap_locks(struct list_head *reaplist)
5943{
5944 struct nfs4_client *clp;
5945 struct nfs4_ol_stateid *stp, *next;
5946
5947 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
5948 list_del_init(&stp->st_locks);
5949 clp = stp->st_stid.sc_client;
5950 nfs4_put_stid(&stp->st_stid);
5951 put_client(clp);
5952 }
5953}
5954
5955u64
Jeff Layton285abde2014-07-30 08:27:24 -04005956nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04005957{
5958 unsigned int count = 0;
5959 struct nfs4_client *clp;
5960 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5961 nfsd_net_id);
5962 LIST_HEAD(reaplist);
5963
5964 if (!nfsd_netns_ready(nn))
5965 return count;
5966
5967 spin_lock(&nn->client_lock);
5968 clp = nfsd_find_client(addr, addr_size);
5969 if (clp)
5970 count = nfsd_collect_client_locks(clp, &reaplist, 0);
5971 spin_unlock(&nn->client_lock);
5972 nfsd_reap_locks(&reaplist);
5973 return count;
5974}
5975
5976u64
Jeff Layton285abde2014-07-30 08:27:24 -04005977nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04005978{
5979 u64 count = 0;
5980 struct nfs4_client *clp;
5981 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5982 nfsd_net_id);
5983 LIST_HEAD(reaplist);
5984
5985 if (!nfsd_netns_ready(nn))
5986 return count;
5987
5988 spin_lock(&nn->client_lock);
5989 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5990 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
5991 if (max != 0 && count >= max)
5992 break;
5993 }
5994 spin_unlock(&nn->client_lock);
5995 nfsd_reap_locks(&reaplist);
5996 return count;
5997}
5998
Jeff Layton82e05ef2014-07-30 08:27:22 -04005999static u64
6000nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6001 struct list_head *collect,
6002 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006003{
6004 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04006005 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6006 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006007 u64 count = 0;
6008
Jeff Layton82e05ef2014-07-30 08:27:22 -04006009 lockdep_assert_held(&nn->client_lock);
6010
6011 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006012 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04006013 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006014 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04006015 if (collect) {
6016 atomic_inc(&clp->cl_refcount);
6017 list_add(&oop->oo_perclient, collect);
6018 }
6019 }
6020 ++count;
6021 /*
6022 * Despite the fact that these functions deal with
6023 * 64-bit integers for "count", we must ensure that
6024 * it doesn't blow up the clp->cl_refcount. Throw a
6025 * warning if we start to approach INT_MAX here.
6026 */
6027 WARN_ON_ONCE(count == (INT_MAX / 2));
6028 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006029 break;
6030 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04006031 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006032
6033 return count;
6034}
6035
Jeff Layton82e05ef2014-07-30 08:27:22 -04006036static u64
6037nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006038{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006039 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6040
6041 nfsd_print_count(clp, count, "openowners");
6042 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006043}
6044
Jeff Layton82e05ef2014-07-30 08:27:22 -04006045static u64
6046nfsd_collect_client_openowners(struct nfs4_client *clp,
6047 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006048{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006049 return nfsd_foreach_client_openowner(clp, max, collect,
6050 unhash_openowner_locked);
6051}
6052
6053u64
Jeff Layton285abde2014-07-30 08:27:24 -04006054nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006055{
6056 struct nfs4_client *clp;
6057 u64 count = 0;
6058 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6059 nfsd_net_id);
6060
6061 if (!nfsd_netns_ready(nn))
6062 return 0;
6063
6064 spin_lock(&nn->client_lock);
6065 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6066 count += nfsd_print_client_openowners(clp);
6067 spin_unlock(&nn->client_lock);
6068
6069 return count;
6070}
6071
6072static void
6073nfsd_reap_openowners(struct list_head *reaplist)
6074{
6075 struct nfs4_client *clp;
6076 struct nfs4_openowner *oop, *next;
6077
6078 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6079 list_del_init(&oop->oo_perclient);
6080 clp = oop->oo_owner.so_client;
6081 release_openowner(oop);
6082 put_client(clp);
6083 }
6084}
6085
6086u64
Jeff Layton285abde2014-07-30 08:27:24 -04006087nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6088 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006089{
6090 unsigned int count = 0;
6091 struct nfs4_client *clp;
6092 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6093 nfsd_net_id);
6094 LIST_HEAD(reaplist);
6095
6096 if (!nfsd_netns_ready(nn))
6097 return count;
6098
6099 spin_lock(&nn->client_lock);
6100 clp = nfsd_find_client(addr, addr_size);
6101 if (clp)
6102 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6103 spin_unlock(&nn->client_lock);
6104 nfsd_reap_openowners(&reaplist);
6105 return count;
6106}
6107
6108u64
Jeff Layton285abde2014-07-30 08:27:24 -04006109nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006110{
6111 u64 count = 0;
6112 struct nfs4_client *clp;
6113 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6114 nfsd_net_id);
6115 LIST_HEAD(reaplist);
6116
6117 if (!nfsd_netns_ready(nn))
6118 return count;
6119
6120 spin_lock(&nn->client_lock);
6121 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6122 count += nfsd_collect_client_openowners(clp, &reaplist,
6123 max - count);
6124 if (max != 0 && count >= max)
6125 break;
6126 }
6127 spin_unlock(&nn->client_lock);
6128 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006129 return count;
6130}
6131
Bryan Schumaker269de302012-11-29 11:40:42 -05006132static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6133 struct list_head *victims)
6134{
6135 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006136 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6137 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006138 u64 count = 0;
6139
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006140 lockdep_assert_held(&nn->client_lock);
6141
6142 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006143 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006144 if (victims) {
6145 /*
6146 * It's not safe to mess with delegations that have a
6147 * non-zero dl_time. They might have already been broken
6148 * and could be processed by the laundromat outside of
6149 * the state_lock. Just leave them be.
6150 */
6151 if (dp->dl_time != 0)
6152 continue;
6153
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006154 atomic_inc(&clp->cl_refcount);
Jeff Layton42690672014-07-25 07:34:20 -04006155 unhash_delegation_locked(dp);
6156 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006157 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006158 ++count;
6159 /*
6160 * Despite the fact that these functions deal with
6161 * 64-bit integers for "count", we must ensure that
6162 * it doesn't blow up the clp->cl_refcount. Throw a
6163 * warning if we start to approach INT_MAX here.
6164 */
6165 WARN_ON_ONCE(count == (INT_MAX / 2));
6166 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006167 break;
6168 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006169 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006170 return count;
6171}
6172
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006173static u64
6174nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006175{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006176 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006177
6178 nfsd_print_count(clp, count, "delegations");
6179 return count;
6180}
6181
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006182u64
Jeff Layton285abde2014-07-30 08:27:24 -04006183nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006184{
6185 struct nfs4_client *clp;
6186 u64 count = 0;
6187 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6188 nfsd_net_id);
6189
6190 if (!nfsd_netns_ready(nn))
6191 return 0;
6192
6193 spin_lock(&nn->client_lock);
6194 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6195 count += nfsd_print_client_delegations(clp);
6196 spin_unlock(&nn->client_lock);
6197
6198 return count;
6199}
6200
6201static void
6202nfsd_forget_delegations(struct list_head *reaplist)
6203{
6204 struct nfs4_client *clp;
6205 struct nfs4_delegation *dp, *next;
6206
6207 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6208 list_del_init(&dp->dl_recall_lru);
6209 clp = dp->dl_stid.sc_client;
6210 revoke_delegation(dp);
6211 put_client(clp);
6212 }
6213}
6214
6215u64
Jeff Layton285abde2014-07-30 08:27:24 -04006216nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6217 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006218{
6219 u64 count = 0;
6220 struct nfs4_client *clp;
6221 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6222 nfsd_net_id);
6223 LIST_HEAD(reaplist);
6224
6225 if (!nfsd_netns_ready(nn))
6226 return count;
6227
6228 spin_lock(&nn->client_lock);
6229 clp = nfsd_find_client(addr, addr_size);
6230 if (clp)
6231 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6232 spin_unlock(&nn->client_lock);
6233
6234 nfsd_forget_delegations(&reaplist);
6235 return count;
6236}
6237
6238u64
Jeff Layton285abde2014-07-30 08:27:24 -04006239nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006240{
6241 u64 count = 0;
6242 struct nfs4_client *clp;
6243 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6244 nfsd_net_id);
6245 LIST_HEAD(reaplist);
6246
6247 if (!nfsd_netns_ready(nn))
6248 return count;
6249
6250 spin_lock(&nn->client_lock);
6251 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6252 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6253 if (max != 0 && count >= max)
6254 break;
6255 }
6256 spin_unlock(&nn->client_lock);
6257 nfsd_forget_delegations(&reaplist);
6258 return count;
6259}
6260
6261static void
6262nfsd_recall_delegations(struct list_head *reaplist)
6263{
6264 struct nfs4_client *clp;
6265 struct nfs4_delegation *dp, *next;
6266
6267 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6268 list_del_init(&dp->dl_recall_lru);
6269 clp = dp->dl_stid.sc_client;
6270 /*
6271 * We skipped all entries that had a zero dl_time before,
6272 * so we can now reset the dl_time back to 0. If a delegation
6273 * break comes in now, then it won't make any difference since
6274 * we're recalling it either way.
6275 */
6276 spin_lock(&state_lock);
6277 dp->dl_time = 0;
6278 spin_unlock(&state_lock);
6279 nfsd_break_one_deleg(dp);
6280 put_client(clp);
6281 }
6282}
6283
6284u64
Jeff Layton285abde2014-07-30 08:27:24 -04006285nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006286 size_t addr_size)
6287{
6288 u64 count = 0;
6289 struct nfs4_client *clp;
6290 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6291 nfsd_net_id);
6292 LIST_HEAD(reaplist);
6293
6294 if (!nfsd_netns_ready(nn))
6295 return count;
6296
6297 spin_lock(&nn->client_lock);
6298 clp = nfsd_find_client(addr, addr_size);
6299 if (clp)
6300 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6301 spin_unlock(&nn->client_lock);
6302
6303 nfsd_recall_delegations(&reaplist);
6304 return count;
6305}
6306
6307u64
Jeff Layton285abde2014-07-30 08:27:24 -04006308nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006309{
6310 u64 count = 0;
6311 struct nfs4_client *clp, *next;
6312 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6313 nfsd_net_id);
6314 LIST_HEAD(reaplist);
6315
6316 if (!nfsd_netns_ready(nn))
6317 return count;
6318
6319 spin_lock(&nn->client_lock);
6320 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6321 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6322 if (max != 0 && ++count >= max)
6323 break;
6324 }
6325 spin_unlock(&nn->client_lock);
6326 nfsd_recall_delegations(&reaplist);
6327 return count;
6328}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006329#endif /* CONFIG_NFSD_FAULT_INJECTION */
6330
Meelap Shahc2f1a552007-07-17 04:04:39 -07006331/*
6332 * Since the lifetime of a delegation isn't limited to that of an open, a
6333 * client may quite reasonably hang on to a delegation as long as it has
6334 * the inode cached. This becomes an obvious problem the first time a
6335 * client's inode cache approaches the size of the server's total memory.
6336 *
6337 * For now we avoid this problem by imposing a hard limit on the number
6338 * of delegations, which varies according to the server's memory size.
6339 */
6340static void
6341set_max_delegations(void)
6342{
6343 /*
6344 * Allow at most 4 delegations per megabyte of RAM. Quick
6345 * estimates suggest that in the worst case (where every delegation
6346 * is for a different inode), a delegation could take about 1.5K,
6347 * giving a worst case usage of about 6% of memory.
6348 */
6349 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6350}
6351
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006352static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006353{
6354 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6355 int i;
6356
6357 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6358 CLIENT_HASH_SIZE, GFP_KERNEL);
6359 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006360 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006361 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6362 CLIENT_HASH_SIZE, GFP_KERNEL);
6363 if (!nn->unconf_id_hashtbl)
6364 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006365 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6366 SESSION_HASH_SIZE, GFP_KERNEL);
6367 if (!nn->sessionid_hashtbl)
6368 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006369
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006370 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006371 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006372 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006373 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006374 for (i = 0; i < SESSION_HASH_SIZE; i++)
6375 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006376 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006377 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03006378 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03006379 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006380 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03006381 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006382
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006383 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006384 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006385
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006386 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006387
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006388err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03006389 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006390err_unconf_id:
6391 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006392err:
6393 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006394}
6395
6396static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006397nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006398{
6399 int i;
6400 struct nfs4_client *clp = NULL;
6401 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6402
6403 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6404 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6405 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6406 destroy_client(clp);
6407 }
6408 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006409
Kinglong Mee2b905632014-03-26 22:09:30 +08006410 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6411 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6412 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6413 destroy_client(clp);
6414 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006415 }
6416
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006417 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006418 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006419 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006420 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006421}
6422
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006423int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006424nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006425{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006426 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006427 int ret;
6428
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006429 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006430 if (ret)
6431 return ret;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006432 nn->boot_time = get_seconds();
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006433 nn->grace_ended = false;
Jeff Laytond4318ac2014-09-12 16:40:21 -04006434 locks_start_grace(net, &nn->nfsd4_manager);
6435 nfsd4_client_tracking_init(net);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006436 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006437 nn->nfsd4_grace, net);
6438 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006439 return 0;
6440}
6441
6442/* initialization to perform when the nfsd service is started: */
6443
6444int
6445nfs4_state_start(void)
6446{
6447 int ret;
6448
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006449 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006450 if (ret)
6451 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07006452 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05006453 if (laundry_wq == NULL) {
6454 ret = -ENOMEM;
6455 goto out_recovery;
6456 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006457 ret = nfsd4_create_callback_queue();
6458 if (ret)
6459 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006460
Meelap Shahc2f1a552007-07-17 04:04:39 -07006461 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006462
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006463 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006464
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006465out_free_laundry:
6466 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006467out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006468 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469}
6470
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006471void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006472nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006476 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006478 cancel_delayed_work_sync(&nn->laundromat_work);
6479 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006480
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04006482 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006483 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006485 unhash_delegation_locked(dp);
6486 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487 }
Benny Halevycdc97502014-05-30 09:09:30 -04006488 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489 list_for_each_safe(pos, next, &reaplist) {
6490 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006491 list_del_init(&dp->dl_recall_lru);
Jeff Laytonafbda402014-08-09 10:22:41 -04006492 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04006493 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494 }
6495
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006496 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006497 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498}
6499
6500void
6501nfs4_state_shutdown(void)
6502{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006503 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006504 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006506
6507static void
6508get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6509{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006510 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6511 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006512}
6513
6514static void
6515put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6516{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006517 if (cstate->minorversion) {
6518 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6519 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6520 }
6521}
6522
6523void
6524clear_current_stateid(struct nfsd4_compound_state *cstate)
6525{
6526 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006527}
6528
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006529/*
6530 * functions to set current state id
6531 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006532void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006533nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6534{
6535 put_stateid(cstate, &odp->od_stateid);
6536}
6537
6538void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006539nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6540{
6541 put_stateid(cstate, &open->op_stateid);
6542}
6543
6544void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006545nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6546{
6547 put_stateid(cstate, &close->cl_stateid);
6548}
6549
6550void
6551nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6552{
6553 put_stateid(cstate, &lock->lk_resp_stateid);
6554}
6555
6556/*
6557 * functions to consume current state id
6558 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006559
6560void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006561nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6562{
6563 get_stateid(cstate, &odp->od_stateid);
6564}
6565
6566void
6567nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6568{
6569 get_stateid(cstate, &drp->dr_stateid);
6570}
6571
6572void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006573nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6574{
6575 get_stateid(cstate, &fsp->fr_stateid);
6576}
6577
6578void
6579nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6580{
6581 get_stateid(cstate, &setattr->sa_stateid);
6582}
6583
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006584void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006585nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6586{
6587 get_stateid(cstate, &close->cl_stateid);
6588}
6589
6590void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006591nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006592{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006593 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006594}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006595
6596void
6597nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6598{
6599 get_stateid(cstate, &read->rd_stateid);
6600}
6601
6602void
6603nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6604{
6605 get_stateid(cstate, &write->wr_stateid);
6606}