blob: 1f4b85b15125a10af047fad1ff41f3718855f5ef [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Chuck Lever7df302f2012-05-29 13:56:37 -040041#include <linux/ratelimit.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050042#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton59766872013-02-04 12:50:00 -050043#include <linux/sunrpc/addr.h>
Daniel Borkmann87545892014-12-10 16:33:11 +010044#include <linux/jhash.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020045#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040046#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050047#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040048#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040050#include "netns.h"
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define NFSDDBG_FACILITY NFSDDBG_PROC
53
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050054#define all_ones {{~0,~0},~0}
55static const stateid_t one_stateid = {
56 .si_generation = ~0,
57 .si_opaque = all_ones,
58};
59static const stateid_t zero_stateid = {
60 /* all fields zero */
61};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010062static const stateid_t currentstateid = {
63 .si_generation = 1,
64};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050065
Andy Adamsonec6b5d72009-04-03 08:28:28 +030066static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070067
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050068#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
69#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010070#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040073static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040074static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
J. Bruce Fields8b671b82009-02-22 14:51:34 -080076/* Locking: */
77
J. Bruce Fields8b671b82009-02-22 14:51:34 -080078/*
79 * Currently used for the del_recall_lru and file hash table. In an
80 * effort to decrease the scope of the client_mutex, this spinlock may
81 * eventually cover more:
82 */
Benny Halevycdc97502014-05-30 09:09:30 -040083static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080084
Jeff Laytonb401be222014-07-29 21:34:33 -040085/*
86 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
87 * the refcount on the open stateid to drop.
88 */
89static DECLARE_WAIT_QUEUE_HEAD(close_wq);
90
Christoph Hellwigabf11352014-05-21 07:43:03 -070091static struct kmem_cache *openowner_slab;
92static struct kmem_cache *lockowner_slab;
93static struct kmem_cache *file_slab;
94static struct kmem_cache *stateid_slab;
95static struct kmem_cache *deleg_slab;
NeilBrowne60d4392005-06-23 22:03:01 -070096
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -040097static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -080098
Christoph Hellwig0162ac22014-09-24 12:19:19 +020099static struct nfsd4_callback_ops nfsd4_cb_recall_ops;
100
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400101static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800102{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400103 return ses->se_flags & NFS4_SESSION_DEAD;
104}
105
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400106static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
107{
108 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400109 return nfserr_jukebox;
110 ses->se_flags |= NFS4_SESSION_DEAD;
111 return nfs_ok;
112}
113
J. Bruce Fields221a6872013-04-01 22:23:49 -0400114static bool is_client_expired(struct nfs4_client *clp)
115{
116 return clp->cl_time == 0;
117}
118
J. Bruce Fields221a6872013-04-01 22:23:49 -0400119static __be32 get_client_locked(struct nfs4_client *clp)
120{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400121 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
122
123 lockdep_assert_held(&nn->client_lock);
124
J. Bruce Fields221a6872013-04-01 22:23:49 -0400125 if (is_client_expired(clp))
126 return nfserr_expired;
127 atomic_inc(&clp->cl_refcount);
128 return nfs_ok;
129}
130
131/* must be called under the client_lock */
132static inline void
133renew_client_locked(struct nfs4_client *clp)
134{
135 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
136
137 if (is_client_expired(clp)) {
138 WARN_ON(1);
139 printk("%s: client (clientid %08x/%08x) already expired\n",
140 __func__,
141 clp->cl_clientid.cl_boot,
142 clp->cl_clientid.cl_id);
143 return;
144 }
145
146 dprintk("renewing client (clientid %08x/%08x)\n",
147 clp->cl_clientid.cl_boot,
148 clp->cl_clientid.cl_id);
149 list_move_tail(&clp->cl_lru, &nn->client_lru);
150 clp->cl_time = get_seconds();
151}
152
Fengguang Wuba138432013-04-16 22:14:15 -0400153static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400154{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400155 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
156
157 lockdep_assert_held(&nn->client_lock);
158
J. Bruce Fields221a6872013-04-01 22:23:49 -0400159 if (!atomic_dec_and_test(&clp->cl_refcount))
160 return;
161 if (!is_client_expired(clp))
162 renew_client_locked(clp);
163}
164
Jeff Layton4b24ca72014-06-30 11:48:44 -0400165static void put_client_renew(struct nfs4_client *clp)
166{
167 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
168
Jeff Laytond6c249b2014-07-08 14:02:50 -0400169 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
170 return;
171 if (!is_client_expired(clp))
172 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400173 spin_unlock(&nn->client_lock);
174}
175
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400176static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
177{
178 __be32 status;
179
180 if (is_session_dead(ses))
181 return nfserr_badsession;
182 status = get_client_locked(ses->se_client);
183 if (status)
184 return status;
185 atomic_inc(&ses->se_ref);
186 return nfs_ok;
187}
188
189static void nfsd4_put_session_locked(struct nfsd4_session *ses)
190{
191 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400192 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
193
194 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400195
196 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
197 free_session(ses);
198 put_client_renew_locked(clp);
199}
200
201static void nfsd4_put_session(struct nfsd4_session *ses)
202{
203 struct nfs4_client *clp = ses->se_client;
204 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
205
206 spin_lock(&nn->client_lock);
207 nfsd4_put_session_locked(ses);
208 spin_unlock(&nn->client_lock);
209}
210
Kinglong Meeb5971af2014-08-22 10:18:43 -0400211static inline struct nfs4_stateowner *
212nfs4_get_stateowner(struct nfs4_stateowner *sop)
213{
214 atomic_inc(&sop->so_count);
215 return sop;
216}
217
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400218static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400219same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400220{
221 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400222 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400223}
224
225static struct nfs4_openowner *
226find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400227 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400228{
229 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400230
Trond Myklebustd4f04892014-07-29 21:34:36 -0400231 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400232
Trond Myklebustd4f04892014-07-29 21:34:36 -0400233 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
234 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400235 if (!so->so_is_open_owner)
236 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400237 if (same_owner_str(so, &open->op_owner))
238 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400239 }
240 return NULL;
241}
242
243static struct nfs4_openowner *
244find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400245 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400246{
247 struct nfs4_openowner *oo;
248
Trond Myklebustd4f04892014-07-29 21:34:36 -0400249 spin_lock(&clp->cl_lock);
250 oo = find_openstateowner_str_locked(hashval, open, clp);
251 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400252 return oo;
253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255static inline u32
256opaque_hashval(const void *ptr, int nbytes)
257{
258 unsigned char *cptr = (unsigned char *) ptr;
259
260 u32 x = 0;
261 while (nbytes--) {
262 x *= 37;
263 x += *cptr++;
264 }
265 return x;
266}
267
Jeff Layton5b095e92014-10-23 08:01:02 -0400268static void nfsd4_free_file_rcu(struct rcu_head *rcu)
J. Bruce Fields32513b42011-10-13 16:00:16 -0400269{
Jeff Layton5b095e92014-10-23 08:01:02 -0400270 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
271
272 kmem_cache_free(file_slab, fp);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400273}
274
NeilBrown13cd2182005-06-23 22:03:10 -0700275static inline void
276put_nfs4_file(struct nfs4_file *fi)
277{
Jeff Layton02e12152014-07-16 10:31:57 -0400278 might_lock(&state_lock);
279
Benny Halevycdc97502014-05-30 09:09:30 -0400280 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton5b095e92014-10-23 08:01:02 -0400281 hlist_del_rcu(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400282 spin_unlock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -0400283 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
284 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800285 }
NeilBrown13cd2182005-06-23 22:03:10 -0700286}
287
288static inline void
289get_nfs4_file(struct nfs4_file *fi)
290{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800291 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700292}
293
Trond Myklebustde186432014-07-10 14:07:26 -0400294static struct file *
295__nfs4_get_fd(struct nfs4_file *f, int oflag)
296{
297 if (f->fi_fds[oflag])
298 return get_file(f->fi_fds[oflag]);
299 return NULL;
300}
301
302static struct file *
303find_writeable_file_locked(struct nfs4_file *f)
304{
305 struct file *ret;
306
307 lockdep_assert_held(&f->fi_lock);
308
309 ret = __nfs4_get_fd(f, O_WRONLY);
310 if (!ret)
311 ret = __nfs4_get_fd(f, O_RDWR);
312 return ret;
313}
314
315static struct file *
316find_writeable_file(struct nfs4_file *f)
317{
318 struct file *ret;
319
320 spin_lock(&f->fi_lock);
321 ret = find_writeable_file_locked(f);
322 spin_unlock(&f->fi_lock);
323
324 return ret;
325}
326
327static struct file *find_readable_file_locked(struct nfs4_file *f)
328{
329 struct file *ret;
330
331 lockdep_assert_held(&f->fi_lock);
332
333 ret = __nfs4_get_fd(f, O_RDONLY);
334 if (!ret)
335 ret = __nfs4_get_fd(f, O_RDWR);
336 return ret;
337}
338
339static struct file *
340find_readable_file(struct nfs4_file *f)
341{
342 struct file *ret;
343
344 spin_lock(&f->fi_lock);
345 ret = find_readable_file_locked(f);
346 spin_unlock(&f->fi_lock);
347
348 return ret;
349}
350
351static struct file *
352find_any_file(struct nfs4_file *f)
353{
354 struct file *ret;
355
356 spin_lock(&f->fi_lock);
357 ret = __nfs4_get_fd(f, O_RDWR);
358 if (!ret) {
359 ret = __nfs4_get_fd(f, O_WRONLY);
360 if (!ret)
361 ret = __nfs4_get_fd(f, O_RDONLY);
362 }
363 spin_unlock(&f->fi_lock);
364 return ret;
365}
366
Trond Myklebust02a35082014-07-25 07:34:22 -0400367static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800368unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700369
370/*
371 * Open owner state (share locks)
372 */
373
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500374/* hash tables for lock and open owners */
375#define OWNER_HASH_BITS 8
376#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
377#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700378
Trond Myklebustd4f04892014-07-29 21:34:36 -0400379static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400380{
381 unsigned int ret;
382
383 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500384 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400385}
NeilBrownef0f3392006-04-10 22:55:41 -0700386
NeilBrownef0f3392006-04-10 22:55:41 -0700387/* hash table for nfs4_file */
388#define FILE_HASH_BITS 8
389#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800390
Trond Myklebustca943212014-07-23 16:17:39 -0400391static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400392{
Trond Myklebustca943212014-07-23 16:17:39 -0400393 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
394}
395
396static unsigned int file_hashval(struct knfsd_fh *fh)
397{
398 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
399}
400
401static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
402{
403 return fh1->fh_size == fh2->fh_size &&
404 !memcmp(fh1->fh_base.fh_pad,
405 fh2->fh_base.fh_pad,
406 fh1->fh_size);
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400407}
408
Jeff Layton89876f82013-04-02 09:01:59 -0400409static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700410
Jeff Layton12659652014-07-10 14:07:28 -0400411static void
412__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400413{
Jeff Layton7214e862014-07-10 14:07:33 -0400414 lockdep_assert_held(&fp->fi_lock);
415
Jeff Layton12659652014-07-10 14:07:28 -0400416 if (access & NFS4_SHARE_ACCESS_WRITE)
417 atomic_inc(&fp->fi_access[O_WRONLY]);
418 if (access & NFS4_SHARE_ACCESS_READ)
419 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400420}
421
Jeff Layton12659652014-07-10 14:07:28 -0400422static __be32
423nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400424{
Jeff Layton7214e862014-07-10 14:07:33 -0400425 lockdep_assert_held(&fp->fi_lock);
426
Jeff Layton12659652014-07-10 14:07:28 -0400427 /* Does this access mode make sense? */
428 if (access & ~NFS4_SHARE_ACCESS_BOTH)
429 return nfserr_inval;
430
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400431 /* Does it conflict with a deny mode already set? */
432 if ((access & fp->fi_share_deny) != 0)
433 return nfserr_share_denied;
434
Jeff Layton12659652014-07-10 14:07:28 -0400435 __nfs4_file_get_access(fp, access);
436 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400437}
438
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400439static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
440{
441 /* Common case is that there is no deny mode. */
442 if (deny) {
443 /* Does this deny mode make sense? */
444 if (deny & ~NFS4_SHARE_DENY_BOTH)
445 return nfserr_inval;
446
447 if ((deny & NFS4_SHARE_DENY_READ) &&
448 atomic_read(&fp->fi_access[O_RDONLY]))
449 return nfserr_share_denied;
450
451 if ((deny & NFS4_SHARE_DENY_WRITE) &&
452 atomic_read(&fp->fi_access[O_WRONLY]))
453 return nfserr_share_denied;
454 }
455 return nfs_ok;
456}
457
J. Bruce Fields998db522010-08-07 09:21:41 -0400458static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400459{
Trond Myklebustde186432014-07-10 14:07:26 -0400460 might_lock(&fp->fi_lock);
461
462 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
463 struct file *f1 = NULL;
464 struct file *f2 = NULL;
465
Jeff Layton6d338b52014-07-10 14:07:29 -0400466 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400467 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400468 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400469 spin_unlock(&fp->fi_lock);
470 if (f1)
471 fput(f1);
472 if (f2)
473 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400474 }
475}
476
Jeff Layton12659652014-07-10 14:07:28 -0400477static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400478{
Jeff Layton12659652014-07-10 14:07:28 -0400479 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
480
481 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400482 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400483 if (access & NFS4_SHARE_ACCESS_READ)
484 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400485}
486
Trond Myklebust60116952014-07-29 21:34:06 -0400487static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
488 struct kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400489{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500490 struct nfs4_stid *stid;
491 int new_id;
492
Trond Myklebustf8338832014-07-25 07:34:19 -0400493 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500494 if (!stid)
495 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400496
Jeff Layton4770d722014-07-29 21:34:10 -0400497 idr_preload(GFP_KERNEL);
498 spin_lock(&cl->cl_lock);
499 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
500 spin_unlock(&cl->cl_lock);
501 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700502 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500503 goto out_free;
504 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500505 stid->sc_stateid.si_opaque.so_id = new_id;
506 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
507 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400508 atomic_set(&stid->sc_count, 1);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500509
J. Bruce Fields996e0932011-10-17 11:14:48 -0400510 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500511 * It shouldn't be a problem to reuse an opaque stateid value.
512 * I don't think it is for 4.1. But with 4.0 I worry that, for
513 * example, a stray write retransmission could be accepted by
514 * the server when it should have been rejected. Therefore,
515 * adopt a trick from the sctp code to attempt to maximize the
516 * amount of time until an id is reused, by ensuring they always
517 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400518 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500519 return stid;
520out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800521 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500522 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400523}
524
Jeff Laytonb49e0842014-07-29 21:34:11 -0400525static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400526{
Trond Myklebust60116952014-07-29 21:34:06 -0400527 struct nfs4_stid *stid;
528 struct nfs4_ol_stateid *stp;
529
530 stid = nfs4_alloc_stid(clp, stateid_slab);
531 if (!stid)
532 return NULL;
533
534 stp = openlockstateid(stid);
535 stp->st_stid.sc_free = nfs4_free_ol_stateid;
536 return stp;
537}
538
539static void nfs4_free_deleg(struct nfs4_stid *stid)
540{
Trond Myklebust60116952014-07-29 21:34:06 -0400541 kmem_cache_free(deleg_slab, stid);
542 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400543}
544
NeilBrown6282cd52014-06-04 17:39:26 +1000545/*
546 * When we recall a delegation, we should be careful not to hand it
547 * out again straight away.
548 * To ensure this we keep a pair of bloom filters ('new' and 'old')
549 * in which the filehandles of recalled delegations are "stored".
550 * If a filehandle appear in either filter, a delegation is blocked.
551 * When a delegation is recalled, the filehandle is stored in the "new"
552 * filter.
553 * Every 30 seconds we swap the filters and clear the "new" one,
554 * unless both are empty of course.
555 *
556 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
557 * low 3 bytes as hash-table indices.
558 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400559 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000560 * is used to manage concurrent access. Testing does not need the lock
561 * except when swapping the two filters.
562 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400563static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000564static struct bloom_pair {
565 int entries, old_entries;
566 time_t swap_time;
567 int new; /* index into 'set' */
568 DECLARE_BITMAP(set[2], 256);
569} blocked_delegations;
570
571static int delegation_blocked(struct knfsd_fh *fh)
572{
573 u32 hash;
574 struct bloom_pair *bd = &blocked_delegations;
575
576 if (bd->entries == 0)
577 return 0;
578 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400579 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000580 if (seconds_since_boot() - bd->swap_time > 30) {
581 bd->entries -= bd->old_entries;
582 bd->old_entries = bd->entries;
583 memset(bd->set[bd->new], 0,
584 sizeof(bd->set[0]));
585 bd->new = 1-bd->new;
586 bd->swap_time = seconds_since_boot();
587 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400588 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000589 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100590 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000591 if (test_bit(hash&255, bd->set[0]) &&
592 test_bit((hash>>8)&255, bd->set[0]) &&
593 test_bit((hash>>16)&255, bd->set[0]))
594 return 1;
595
596 if (test_bit(hash&255, bd->set[1]) &&
597 test_bit((hash>>8)&255, bd->set[1]) &&
598 test_bit((hash>>16)&255, bd->set[1]))
599 return 1;
600
601 return 0;
602}
603
604static void block_delegations(struct knfsd_fh *fh)
605{
606 u32 hash;
607 struct bloom_pair *bd = &blocked_delegations;
608
Daniel Borkmann87545892014-12-10 16:33:11 +0100609 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000610
Jeff Laytonf54fe962014-07-25 07:34:26 -0400611 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000612 __set_bit(hash&255, bd->set[bd->new]);
613 __set_bit((hash>>8)&255, bd->set[bd->new]);
614 __set_bit((hash>>16)&255, bd->set[bd->new]);
615 if (bd->entries == 0)
616 bd->swap_time = seconds_since_boot();
617 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400618 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000619}
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621static struct nfs4_delegation *
Jeff Laytonf9416e22014-07-25 07:34:23 -0400622alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400625 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400628 n = atomic_long_inc_return(&num_delegations);
629 if (n < 0 || n > max_delegations)
630 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000631 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400632 goto out_dec;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400633 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700634 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400635 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400636
637 dp->dl_stid.sc_free = nfs4_free_deleg;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400638 /*
639 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400640 * meaning of seqid 0 isn't meaningful, really, but let's avoid
641 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400642 */
643 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700644 INIT_LIST_HEAD(&dp->dl_perfile);
645 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400647 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200648 dp->dl_retries = 1;
649 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200650 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400652out_dec:
653 atomic_long_dec(&num_delegations);
654 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
657void
Trond Myklebust60116952014-07-29 21:34:06 -0400658nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Trond Myklebust11b91642014-07-29 21:34:08 -0400660 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400661 struct nfs4_client *clp = s->sc_client;
662
Jeff Layton4770d722014-07-29 21:34:10 -0400663 might_lock(&clp->cl_lock);
664
Jeff Laytonb401be222014-07-29 21:34:33 -0400665 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
666 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400667 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400668 }
Trond Myklebust60116952014-07-29 21:34:06 -0400669 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400670 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400671 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400672 if (fp)
673 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500676static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Jeff Layton6bcc0342014-08-09 10:22:40 -0400678 struct file *filp = NULL;
Jeff Layton417c6622014-07-21 09:34:57 -0400679
Jeff Layton6bcc0342014-08-09 10:22:40 -0400680 spin_lock(&fp->fi_lock);
Jeff Layton67db1032014-12-13 09:11:40 -0500681 if (fp->fi_deleg_file && --fp->fi_delegees == 0)
Jeff Layton6bcc0342014-08-09 10:22:40 -0400682 swap(filp, fp->fi_deleg_file);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400683 spin_unlock(&fp->fi_lock);
684
685 if (filp) {
Jeff Laytone6f5c782014-08-22 10:40:25 -0400686 vfs_setlease(filp, F_UNLCK, NULL, NULL);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400687 fput(filp);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400691static void unhash_stid(struct nfs4_stid *s)
692{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500693 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400694}
695
Benny Halevy931ee562014-05-30 09:09:27 -0400696static void
697hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
698{
Benny Halevycdc97502014-05-30 09:09:30 -0400699 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400700 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400701
Trond Myklebust67cb1272014-07-29 21:34:17 -0400702 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400703 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400704 list_add(&dp->dl_perfile, &fp->fi_delegations);
705 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
706}
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708static void
Jeff Layton42690672014-07-25 07:34:20 -0400709unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Trond Myklebust11b91642014-07-29 21:34:08 -0400711 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400712
Jeff Layton42690672014-07-25 07:34:20 -0400713 lockdep_assert_held(&state_lock);
714
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400715 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400716 /* Ensure that deleg break won't try to requeue it */
717 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400718 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400719 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400721 list_del_init(&dp->dl_perfile);
722 spin_unlock(&fp->fi_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400723}
724
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400725static void destroy_delegation(struct nfs4_delegation *dp)
726{
Jeff Layton42690672014-07-25 07:34:20 -0400727 spin_lock(&state_lock);
728 unhash_delegation_locked(dp);
729 spin_unlock(&state_lock);
Jeff Laytonafbda402014-08-09 10:22:41 -0400730 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -0400731 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400732}
733
734static void revoke_delegation(struct nfs4_delegation *dp)
735{
736 struct nfs4_client *clp = dp->dl_stid.sc_client;
737
Jeff Layton2d4a5322014-07-25 07:34:21 -0400738 WARN_ON(!list_empty(&dp->dl_recall_lru));
739
Jeff Laytonafbda402014-08-09 10:22:41 -0400740 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
741
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400742 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400743 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400744 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400745 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400746 spin_lock(&clp->cl_lock);
747 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
748 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400749 }
750}
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752/*
753 * SETCLIENTID state
754 */
755
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400756static unsigned int clientid_hashval(u32 id)
757{
758 return id & CLIENT_HASH_MASK;
759}
760
761static unsigned int clientstr_hashval(const char *name)
762{
763 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
764}
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400767 * We store the NONE, READ, WRITE, and BOTH bits separately in the
768 * st_{access,deny}_bmap field of the stateid, in order to track not
769 * only what share bits are currently in force, but also what
770 * combinations of share bits previous opens have used. This allows us
771 * to enforce the recommendation of rfc 3530 14.2.19 that the server
772 * return an error if the client attempt to downgrade to a combination
773 * of share bits not explicable by closing some of its previous opens.
774 *
775 * XXX: This enforcement is actually incomplete, since we don't keep
776 * track of access/deny bit combinations; so, e.g., we allow:
777 *
778 * OPEN allow read, deny write
779 * OPEN allow both, deny none
780 * DOWNGRADE allow read, deny none
781 *
782 * which we should reject.
783 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400784static unsigned int
785bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400786 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400787 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400788
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400789 for (i = 1; i < 4; i++) {
790 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400791 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400792 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400793 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400794}
795
Jeff Layton82c5ff12012-05-11 09:45:13 -0400796/* set share access for a given stateid */
797static inline void
798set_access(u32 access, struct nfs4_ol_stateid *stp)
799{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400800 unsigned char mask = 1 << access;
801
802 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
803 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400804}
805
806/* clear share access for a given stateid */
807static inline void
808clear_access(u32 access, struct nfs4_ol_stateid *stp)
809{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400810 unsigned char mask = 1 << access;
811
812 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
813 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400814}
815
816/* test whether a given stateid has access */
817static inline bool
818test_access(u32 access, struct nfs4_ol_stateid *stp)
819{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400820 unsigned char mask = 1 << access;
821
822 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400823}
824
Jeff Laytonce0fc432012-05-11 09:45:14 -0400825/* set share deny for a given stateid */
826static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400827set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400828{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400829 unsigned char mask = 1 << deny;
830
831 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
832 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400833}
834
835/* clear share deny for a given stateid */
836static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400837clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400838{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400839 unsigned char mask = 1 << deny;
840
841 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
842 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400843}
844
845/* test whether a given stateid is denying specific access */
846static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400847test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400848{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400849 unsigned char mask = 1 << deny;
850
851 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400852}
853
854static int nfs4_access_to_omode(u32 access)
855{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400856 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400857 case NFS4_SHARE_ACCESS_READ:
858 return O_RDONLY;
859 case NFS4_SHARE_ACCESS_WRITE:
860 return O_WRONLY;
861 case NFS4_SHARE_ACCESS_BOTH:
862 return O_RDWR;
863 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500864 WARN_ON_ONCE(1);
865 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400866}
867
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400868/*
869 * A stateid that had a deny mode associated with it is being released
870 * or downgraded. Recalculate the deny mode on the file.
871 */
872static void
873recalculate_deny_mode(struct nfs4_file *fp)
874{
875 struct nfs4_ol_stateid *stp;
876
877 spin_lock(&fp->fi_lock);
878 fp->fi_share_deny = 0;
879 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
880 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
881 spin_unlock(&fp->fi_lock);
882}
883
884static void
885reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
886{
887 int i;
888 bool change = false;
889
890 for (i = 1; i < 4; i++) {
891 if ((i & deny) != i) {
892 change = true;
893 clear_deny(i, stp);
894 }
895 }
896
897 /* Recalculate per-file deny mode if there was a change */
898 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -0400899 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400900}
901
Jeff Layton82c5ff12012-05-11 09:45:13 -0400902/* release all access and file references for a given stateid */
903static void
904release_all_access(struct nfs4_ol_stateid *stp)
905{
906 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -0400907 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400908
909 if (fp && stp->st_deny_bmap != 0)
910 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400911
912 for (i = 1; i < 4; i++) {
913 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -0400914 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400915 clear_access(i, stp);
916 }
917}
918
Jeff Layton6b180f02014-07-29 21:34:26 -0400919static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
920{
Jeff Laytona819ecc2014-07-29 21:34:38 -0400921 struct nfs4_client *clp = sop->so_client;
922
923 might_lock(&clp->cl_lock);
924
925 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -0400926 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -0400927 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -0400928 spin_unlock(&clp->cl_lock);
Jeff Layton6b180f02014-07-29 21:34:26 -0400929 kfree(sop->so_owner.data);
930 sop->so_ops->so_free(sop);
931}
932
Jeff Layton4ae098d2014-07-29 21:34:43 -0400933static void unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500934{
Trond Myklebust11b91642014-07-29 21:34:08 -0400935 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -0400936
Jeff Layton1c755dc2014-07-29 21:34:12 -0400937 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
938
Trond Myklebust1d31a252014-07-10 14:07:25 -0400939 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500940 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400941 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500942 list_del(&stp->st_perstateowner);
943}
944
Trond Myklebust60116952014-07-29 21:34:06 -0400945static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500946{
Trond Myklebust60116952014-07-29 21:34:06 -0400947 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400948
Trond Myklebust60116952014-07-29 21:34:06 -0400949 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -0400950 if (stp->st_stateowner)
951 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -0400952 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500953}
954
Jeff Laytonb49e0842014-07-29 21:34:11 -0400955static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500956{
Jeff Laytonb49e0842014-07-29 21:34:11 -0400957 struct nfs4_ol_stateid *stp = openlockstateid(stid);
958 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500959 struct file *file;
960
Jeff Laytonb49e0842014-07-29 21:34:11 -0400961 file = find_any_file(stp->st_stid.sc_file);
962 if (file)
963 filp_close(file, (fl_owner_t)lo);
964 nfs4_free_ol_stateid(stid);
965}
966
Jeff Layton2c41beb2014-07-29 21:34:41 -0400967/*
968 * Put the persistent reference to an already unhashed generic stateid, while
969 * holding the cl_lock. If it's the last reference, then put it onto the
970 * reaplist for later destruction.
971 */
972static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
973 struct list_head *reaplist)
974{
975 struct nfs4_stid *s = &stp->st_stid;
976 struct nfs4_client *clp = s->sc_client;
977
978 lockdep_assert_held(&clp->cl_lock);
979
980 WARN_ON_ONCE(!list_empty(&stp->st_locks));
981
982 if (!atomic_dec_and_test(&s->sc_count)) {
983 wake_up_all(&close_wq);
984 return;
985 }
986
987 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
988 list_add(&stp->st_locks, reaplist);
989}
990
Jeff Layton3c1c9952014-07-29 21:34:39 -0400991static void unhash_lock_stateid(struct nfs4_ol_stateid *stp)
992{
993 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
994
995 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
996
997 list_del_init(&stp->st_locks);
Jeff Layton4ae098d2014-07-29 21:34:43 -0400998 unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -0400999 unhash_stid(&stp->st_stid);
1000}
1001
Jeff Layton5adfd882014-07-29 21:34:31 -04001002static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001003{
Jeff Layton1c755dc2014-07-29 21:34:12 -04001004 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1005
1006 spin_lock(&oo->oo_owner.so_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001007 unhash_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001008 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04001009 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001010}
1011
Trond Myklebustc58c6612014-07-29 21:34:35 -04001012static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001013{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001014 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001015
Trond Myklebustd4f04892014-07-29 21:34:36 -04001016 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001017
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001018 list_del_init(&lo->lo_owner.so_strhash);
1019}
1020
Jeff Layton2c41beb2014-07-29 21:34:41 -04001021/*
1022 * Free a list of generic stateids that were collected earlier after being
1023 * fully unhashed.
1024 */
1025static void
1026free_ol_stateid_reaplist(struct list_head *reaplist)
1027{
1028 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001029 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001030
1031 might_sleep();
1032
1033 while (!list_empty(reaplist)) {
1034 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1035 st_locks);
1036 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001037 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001038 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001039 if (fp)
1040 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001041 }
1042}
1043
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001044static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001045{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001046 struct nfs4_client *clp = lo->lo_owner.so_client;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001047 struct nfs4_ol_stateid *stp;
1048 struct list_head reaplist;
1049
1050 INIT_LIST_HEAD(&reaplist);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001051
Trond Myklebustd4f04892014-07-29 21:34:36 -04001052 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001053 unhash_lockowner_locked(lo);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001054 while (!list_empty(&lo->lo_owner.so_stateids)) {
1055 stp = list_first_entry(&lo->lo_owner.so_stateids,
1056 struct nfs4_ol_stateid, st_perstateowner);
1057 unhash_lock_stateid(stp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001058 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001059 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001060 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001061 free_ol_stateid_reaplist(&reaplist);
Jeff Layton6b180f02014-07-29 21:34:26 -04001062 nfs4_put_stateowner(&lo->lo_owner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001063}
1064
Jeff Laytond83017f2014-07-29 21:34:42 -04001065static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1066 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001067{
1068 struct nfs4_ol_stateid *stp;
1069
1070 while (!list_empty(&open_stp->st_locks)) {
1071 stp = list_entry(open_stp->st_locks.next,
1072 struct nfs4_ol_stateid, st_locks);
Jeff Laytond83017f2014-07-29 21:34:42 -04001073 unhash_lock_stateid(stp);
1074 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001075 }
1076}
1077
Jeff Laytond83017f2014-07-29 21:34:42 -04001078static void unhash_open_stateid(struct nfs4_ol_stateid *stp,
1079 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001080{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001081 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1082
Jeff Layton4ae098d2014-07-29 21:34:43 -04001083 unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001084 release_open_stateid_locks(stp, reaplist);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001085}
1086
1087static void release_open_stateid(struct nfs4_ol_stateid *stp)
1088{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001089 LIST_HEAD(reaplist);
1090
1091 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04001092 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001093 put_ol_stateid_locked(stp, &reaplist);
1094 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1095 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001096}
1097
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001098static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001099{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001100 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001101
Trond Myklebustd4f04892014-07-29 21:34:36 -04001102 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001103
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001104 list_del_init(&oo->oo_owner.so_strhash);
1105 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001106}
1107
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001108static void release_last_closed_stateid(struct nfs4_openowner *oo)
1109{
Jeff Layton217526e2014-07-30 08:27:11 -04001110 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1111 nfsd_net_id);
1112 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001113
Jeff Layton217526e2014-07-30 08:27:11 -04001114 spin_lock(&nn->client_lock);
1115 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001116 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001117 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001118 oo->oo_last_closed_stid = NULL;
1119 }
Jeff Layton217526e2014-07-30 08:27:11 -04001120 spin_unlock(&nn->client_lock);
1121 if (s)
1122 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001123}
1124
Jeff Layton2c41beb2014-07-29 21:34:41 -04001125static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001126{
1127 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001128 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001129 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001130
Jeff Layton2c41beb2014-07-29 21:34:41 -04001131 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001132
Trond Myklebustd4f04892014-07-29 21:34:36 -04001133 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001134 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001135 while (!list_empty(&oo->oo_owner.so_stateids)) {
1136 stp = list_first_entry(&oo->oo_owner.so_stateids,
1137 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytond83017f2014-07-29 21:34:42 -04001138 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001139 put_ol_stateid_locked(stp, &reaplist);
1140 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001141 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001142 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001143 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001144 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001145}
1146
Marc Eshel5282fd72009-04-03 08:27:52 +03001147static inline int
1148hash_sessionid(struct nfs4_sessionid *sessionid)
1149{
1150 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1151
1152 return sid->sequence % SESSION_HASH_SIZE;
1153}
1154
Trond Myklebust8f199b82012-03-20 15:11:17 -04001155#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001156static inline void
1157dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1158{
1159 u32 *ptr = (u32 *)(&sessionid->data[0]);
1160 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1161}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001162#else
1163static inline void
1164dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1165{
1166}
1167#endif
1168
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001169/*
1170 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1171 * won't be used for replay.
1172 */
1173void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1174{
1175 struct nfs4_stateowner *so = cstate->replay_owner;
1176
1177 if (nfserr == nfserr_replay_me)
1178 return;
1179
1180 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001181 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001182 return;
1183 }
1184 if (!so)
1185 return;
1186 if (so->so_is_open_owner)
1187 release_last_closed_stateid(openowner(so));
1188 so->so_seqid++;
1189 return;
1190}
Marc Eshel5282fd72009-04-03 08:27:52 +03001191
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001192static void
1193gen_sessionid(struct nfsd4_session *ses)
1194{
1195 struct nfs4_client *clp = ses->se_client;
1196 struct nfsd4_sessionid *sid;
1197
1198 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1199 sid->clientid = clp->cl_clientid;
1200 sid->sequence = current_sessionid++;
1201 sid->reserved = 0;
1202}
1203
1204/*
Andy Adamsona6496372009-08-28 08:45:01 -04001205 * The protocol defines ca_maxresponssize_cached to include the size of
1206 * the rpc header, but all we need to cache is the data starting after
1207 * the end of the initial SEQUENCE operation--the rest we regenerate
1208 * each time. Therefore we can advertise a ca_maxresponssize_cached
1209 * value that is the number of bytes in our cache plus a few additional
1210 * bytes. In order to stay on the safe side, and not promise more than
1211 * we can cache, those additional bytes must be the minimum possible: 24
1212 * bytes of rpc header (xid through accept state, with AUTH_NULL
1213 * verifier), 12 for the compound header (with zero-length tag), and 44
1214 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001215 */
Andy Adamsona6496372009-08-28 08:45:01 -04001216#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1217
Andy Adamson557ce262009-08-28 08:45:04 -04001218static void
1219free_session_slots(struct nfsd4_session *ses)
1220{
1221 int i;
1222
1223 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1224 kfree(ses->se_slots[i]);
1225}
1226
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001227/*
1228 * We don't actually need to cache the rpc and session headers, so we
1229 * can allocate a little less for each slot:
1230 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001231static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001232{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001233 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001234
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001235 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1236 size = 0;
1237 else
1238 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1239 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001240}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001241
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001242/*
1243 * XXX: If we run out of reserved DRC memory we could (up to a point)
1244 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001245 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001246 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001247static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001248{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001249 u32 slotsize = slot_bytes(ca);
1250 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001251 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001252
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001253 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001254 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1255 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001256 num = min_t(int, num, avail / slotsize);
1257 nfsd_drc_mem_used += num * slotsize;
1258 spin_unlock(&nfsd_drc_lock);
1259
1260 return num;
1261}
1262
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001263static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001264{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001265 int slotsize = slot_bytes(ca);
1266
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001267 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001268 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001269 spin_unlock(&nfsd_drc_lock);
1270}
1271
Kinglong Mee60810e52014-01-01 00:35:47 +08001272static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1273 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001274{
Kinglong Mee60810e52014-01-01 00:35:47 +08001275 int numslots = fattrs->maxreqs;
1276 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001277 struct nfsd4_session *new;
1278 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001279
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001280 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001281 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1282 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001283
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001284 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001285 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001286 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001287 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001288 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001289 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001290 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001291 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001292 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001293
1294 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1295 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1296
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001297 return new;
1298out_free:
1299 while (i--)
1300 kfree(new->se_slots[i]);
1301 kfree(new);
1302 return NULL;
1303}
1304
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001305static void free_conn(struct nfsd4_conn *c)
1306{
1307 svc_xprt_put(c->cn_xprt);
1308 kfree(c);
1309}
1310
1311static void nfsd4_conn_lost(struct svc_xpt_user *u)
1312{
1313 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1314 struct nfs4_client *clp = c->cn_session->se_client;
1315
1316 spin_lock(&clp->cl_lock);
1317 if (!list_empty(&c->cn_persession)) {
1318 list_del(&c->cn_persession);
1319 free_conn(c);
1320 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001321 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001322 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001323}
1324
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001325static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001326{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001327 struct nfsd4_conn *conn;
1328
1329 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1330 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001331 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001332 svc_xprt_get(rqstp->rq_xprt);
1333 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001334 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001335 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1336 return conn;
1337}
1338
J. Bruce Fields328ead22010-09-29 16:11:06 -04001339static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1340{
1341 conn->cn_session = ses;
1342 list_add(&conn->cn_persession, &ses->se_conns);
1343}
1344
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001345static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1346{
1347 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001348
1349 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001350 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001351 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001352}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001353
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001354static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001355{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001356 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001357 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001358}
1359
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001360static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001361{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001362 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001363
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001364 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001365 ret = nfsd4_register_conn(conn);
1366 if (ret)
1367 /* oops; xprt is already down: */
1368 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001369 /* We may have gained or lost a callback channel: */
1370 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001371}
1372
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001373static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001374{
1375 u32 dir = NFS4_CDFC4_FORE;
1376
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001377 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001378 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001379 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001380}
1381
1382/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001383static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001384{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001385 struct nfs4_client *clp = s->se_client;
1386 struct nfsd4_conn *c;
1387
1388 spin_lock(&clp->cl_lock);
1389 while (!list_empty(&s->se_conns)) {
1390 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1391 list_del_init(&c->cn_persession);
1392 spin_unlock(&clp->cl_lock);
1393
1394 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1395 free_conn(c);
1396
1397 spin_lock(&clp->cl_lock);
1398 }
1399 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001400}
1401
J. Bruce Fields1377b692012-09-11 21:42:40 -04001402static void __free_session(struct nfsd4_session *ses)
1403{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001404 free_session_slots(ses);
1405 kfree(ses);
1406}
1407
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001408static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001409{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001410 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001411 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001412 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001413}
Andy Adamson557ce262009-08-28 08:45:04 -04001414
Fengguang Wu135ae822012-11-10 07:20:25 -05001415static 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 -04001416{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001417 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001418 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001419
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001420 new->se_client = clp;
1421 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001422
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001423 INIT_LIST_HEAD(&new->se_conns);
1424
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001425 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001426 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001427 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001428 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001429 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001430 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001431 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001432 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001433 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001434 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001435
Chuck Leverb0d2e422014-08-22 15:10:59 -04001436 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001437 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001438 /*
1439 * This is a little silly; with sessions there's no real
1440 * use for the callback address. Use the peer address
1441 * as a reasonable default for now, but consider fixing
1442 * the rpc client not to require an address in the
1443 * future:
1444 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001445 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1446 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001447 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001448}
1449
Benny Halevy9089f1b2010-05-12 00:12:26 +03001450/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001451static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001452__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001453{
1454 struct nfsd4_session *elem;
1455 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001456 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001457
Trond Myklebust0a880a22014-07-30 08:27:10 -04001458 lockdep_assert_held(&nn->client_lock);
1459
Marc Eshel5282fd72009-04-03 08:27:52 +03001460 dump_sessionid(__func__, sessionid);
1461 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001462 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001463 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001464 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1465 NFS4_MAX_SESSIONID_LEN)) {
1466 return elem;
1467 }
1468 }
1469
1470 dprintk("%s: session not found\n", __func__);
1471 return NULL;
1472}
1473
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001474static struct nfsd4_session *
1475find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1476 __be32 *ret)
1477{
1478 struct nfsd4_session *session;
1479 __be32 status = nfserr_badsession;
1480
1481 session = __find_in_sessionid_hashtbl(sessionid, net);
1482 if (!session)
1483 goto out;
1484 status = nfsd4_get_session_locked(session);
1485 if (status)
1486 session = NULL;
1487out:
1488 *ret = status;
1489 return session;
1490}
1491
Benny Halevy9089f1b2010-05-12 00:12:26 +03001492/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001493static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001494unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001495{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001496 struct nfs4_client *clp = ses->se_client;
1497 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1498
1499 lockdep_assert_held(&nn->client_lock);
1500
Andy Adamson7116ed62009-04-03 08:27:43 +03001501 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001502 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001503 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001504 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001505}
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1508static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001509STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001511 /*
1512 * We're assuming the clid was not given out from a boot
1513 * precisely 2^32 (about 136 years) before this one. That seems
1514 * a safe assumption:
1515 */
1516 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001518 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001519 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return 1;
1521}
1522
1523/*
1524 * XXX Should we use a slab cache ?
1525 * This type of memory management is somewhat inefficient, but we use it
1526 * anyway since SETCLIENTID is not a common operation.
1527 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001528static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
1530 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001531 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001533 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1534 if (clp == NULL)
1535 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001536 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001537 if (clp->cl_name.data == NULL)
1538 goto err_no_name;
1539 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1540 OWNER_HASH_SIZE, GFP_KERNEL);
1541 if (!clp->cl_ownerstr_hashtbl)
1542 goto err_no_hashtbl;
1543 for (i = 0; i < OWNER_HASH_SIZE; i++)
1544 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001545 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001546 INIT_LIST_HEAD(&clp->cl_sessions);
1547 idr_init(&clp->cl_stateids);
1548 atomic_set(&clp->cl_refcount, 0);
1549 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1550 INIT_LIST_HEAD(&clp->cl_idhash);
1551 INIT_LIST_HEAD(&clp->cl_openowners);
1552 INIT_LIST_HEAD(&clp->cl_delegations);
1553 INIT_LIST_HEAD(&clp->cl_lru);
1554 INIT_LIST_HEAD(&clp->cl_callbacks);
1555 INIT_LIST_HEAD(&clp->cl_revoked);
1556 spin_lock_init(&clp->cl_lock);
1557 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001559err_no_hashtbl:
1560 kfree(clp->cl_name.data);
1561err_no_name:
1562 kfree(clp);
1563 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001566static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567free_client(struct nfs4_client *clp)
1568{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001569 while (!list_empty(&clp->cl_sessions)) {
1570 struct nfsd4_session *ses;
1571 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1572 se_perclnt);
1573 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001574 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1575 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001576 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001577 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001578 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001579 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001581 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 kfree(clp);
1583}
1584
Benny Halevy84d38ac2010-05-12 00:13:16 +03001585/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001586static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001587unhash_client_locked(struct nfs4_client *clp)
1588{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001589 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001590 struct nfsd4_session *ses;
1591
Trond Myklebust0a880a22014-07-30 08:27:10 -04001592 lockdep_assert_held(&nn->client_lock);
1593
Trond Myklebust4beb3452014-07-30 08:27:02 -04001594 /* Mark the client as expired! */
1595 clp->cl_time = 0;
1596 /* Make it invisible */
1597 if (!list_empty(&clp->cl_idhash)) {
1598 list_del_init(&clp->cl_idhash);
1599 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1600 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1601 else
1602 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1603 }
1604 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001605 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001606 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1607 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001608 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001609}
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001612unhash_client(struct nfs4_client *clp)
1613{
1614 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1615
1616 spin_lock(&nn->client_lock);
1617 unhash_client_locked(clp);
1618 spin_unlock(&nn->client_lock);
1619}
1620
Jeff Layton97403d92014-07-30 08:27:12 -04001621static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1622{
1623 if (atomic_read(&clp->cl_refcount))
1624 return nfserr_jukebox;
1625 unhash_client_locked(clp);
1626 return nfs_ok;
1627}
1628
Trond Myklebust4beb3452014-07-30 08:27:02 -04001629static void
1630__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001632 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 struct list_head reaplist;
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001637 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001638 while (!list_empty(&clp->cl_delegations)) {
1639 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001640 unhash_delegation_locked(dp);
1641 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
Benny Halevycdc97502014-05-30 09:09:30 -04001643 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 while (!list_empty(&reaplist)) {
1645 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001646 list_del_init(&dp->dl_recall_lru);
Jeff Laytonafbda402014-08-09 10:22:41 -04001647 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04001648 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001650 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001651 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001652 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001653 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001654 }
NeilBrownea1da632005-06-23 22:04:17 -07001655 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001656 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04001657 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001658 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001660 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001661 if (clp->cl_cb_conn.cb_xprt)
1662 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001663 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665
Trond Myklebust4beb3452014-07-30 08:27:02 -04001666static void
1667destroy_client(struct nfs4_client *clp)
1668{
1669 unhash_client(clp);
1670 __destroy_client(clp);
1671}
1672
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001673static void expire_client(struct nfs4_client *clp)
1674{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001675 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001676 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001677 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001678}
1679
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001680static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1681{
1682 memcpy(target->cl_verifier.data, source->data,
1683 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
1685
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001686static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1687{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1689 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1690}
1691
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001692static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001693{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001694 if (source->cr_principal) {
1695 target->cr_principal =
1696 kstrdup(source->cr_principal, GFP_KERNEL);
1697 if (target->cr_principal == NULL)
1698 return -ENOMEM;
1699 } else
1700 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001701 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 target->cr_uid = source->cr_uid;
1703 target->cr_gid = source->cr_gid;
1704 target->cr_group_info = source->cr_group_info;
1705 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001706 target->cr_gss_mech = source->cr_gss_mech;
1707 if (source->cr_gss_mech)
1708 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001709 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710}
1711
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001712static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001713compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1714{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001715 if (o1->len < o2->len)
1716 return -1;
1717 if (o1->len > o2->len)
1718 return 1;
1719 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001720}
1721
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001722static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001723{
NeilBrowna55370a2005-06-23 22:03:52 -07001724 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
1727static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001728same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1729{
1730 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
1733static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001734same_clid(clientid_t *cl1, clientid_t *cl2)
1735{
1736 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737}
1738
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001739static bool groups_equal(struct group_info *g1, struct group_info *g2)
1740{
1741 int i;
1742
1743 if (g1->ngroups != g2->ngroups)
1744 return false;
1745 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001746 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001747 return false;
1748 return true;
1749}
1750
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001751/*
1752 * RFC 3530 language requires clid_inuse be returned when the
1753 * "principal" associated with a requests differs from that previously
1754 * used. We use uid, gid's, and gss principal string as our best
1755 * approximation. We also don't want to allow non-gss use of a client
1756 * established using gss: in theory cr_principal should catch that
1757 * change, but in practice cr_principal can be null even in the gss case
1758 * since gssd doesn't always pass down a principal string.
1759 */
1760static bool is_gss_cred(struct svc_cred *cr)
1761{
1762 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1763 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1764}
1765
1766
Vivek Trivedi5559b502012-07-24 21:18:20 +05301767static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001768same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1769{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001770 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001771 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1772 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001773 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1774 return false;
1775 if (cr1->cr_principal == cr2->cr_principal)
1776 return true;
1777 if (!cr1->cr_principal || !cr2->cr_principal)
1778 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301779 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
J. Bruce Fields57266a62013-04-13 14:27:29 -04001782static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1783{
1784 struct svc_cred *cr = &rqstp->rq_cred;
1785 u32 service;
1786
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001787 if (!cr->cr_gss_mech)
1788 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001789 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1790 return service == RPC_GSS_SVC_INTEGRITY ||
1791 service == RPC_GSS_SVC_PRIVACY;
1792}
1793
1794static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1795{
1796 struct svc_cred *cr = &rqstp->rq_cred;
1797
1798 if (!cl->cl_mach_cred)
1799 return true;
1800 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1801 return false;
1802 if (!svc_rqst_integrity_protected(rqstp))
1803 return false;
1804 if (!cr->cr_principal)
1805 return false;
1806 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1807}
1808
Jeff Layton294ac322014-07-30 08:27:15 -04001809static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001810{
Chuck Leverab4684d2012-03-02 17:13:50 -05001811 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Jeff Laytonf4199922014-06-17 07:44:11 -04001813 /*
1814 * This is opaque to client, so no need to byte-swap. Use
1815 * __force to keep sparse happy
1816 */
1817 verf[0] = (__force __be32)get_seconds();
Jeff Layton294ac322014-07-30 08:27:15 -04001818 verf[1] = (__force __be32)nn->clientid_counter;
Chuck Leverab4684d2012-03-02 17:13:50 -05001819 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
1821
Jeff Layton294ac322014-07-30 08:27:15 -04001822static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1823{
1824 clp->cl_clientid.cl_boot = nn->boot_time;
1825 clp->cl_clientid.cl_id = nn->clientid_counter++;
1826 gen_confirm(clp, nn);
1827}
1828
Jeff Layton4770d722014-07-29 21:34:10 -04001829static struct nfs4_stid *
1830find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001831{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001832 struct nfs4_stid *ret;
1833
1834 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1835 if (!ret || !ret->sc_type)
1836 return NULL;
1837 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001838}
1839
Jeff Layton4770d722014-07-29 21:34:10 -04001840static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001841find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001842{
1843 struct nfs4_stid *s;
1844
Jeff Layton4770d722014-07-29 21:34:10 -04001845 spin_lock(&cl->cl_lock);
1846 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001847 if (s != NULL) {
1848 if (typemask & s->sc_type)
1849 atomic_inc(&s->sc_count);
1850 else
1851 s = NULL;
1852 }
Jeff Layton4770d722014-07-29 21:34:10 -04001853 spin_unlock(&cl->cl_lock);
1854 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001855}
1856
Jeff Layton2216d442012-11-12 15:00:57 -05001857static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001858 struct svc_rqst *rqstp, nfs4_verifier *verf)
1859{
1860 struct nfs4_client *clp;
1861 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001862 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001863 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001864
1865 clp = alloc_client(name);
1866 if (clp == NULL)
1867 return NULL;
1868
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001869 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1870 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001871 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001872 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001873 }
Christoph Hellwig0162ac22014-09-24 12:19:19 +02001874 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Benny Halevy07cd4902010-05-12 00:13:41 +03001875 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001876 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001877 copy_verf(clp, verf);
1878 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001879 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001880 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001881 return clp;
1882}
1883
NeilBrownfd39ca92005-06-23 22:04:03 -07001884static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001885add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1886{
1887 struct rb_node **new = &(root->rb_node), *parent = NULL;
1888 struct nfs4_client *clp;
1889
1890 while (*new) {
1891 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1892 parent = *new;
1893
1894 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1895 new = &((*new)->rb_left);
1896 else
1897 new = &((*new)->rb_right);
1898 }
1899
1900 rb_link_node(&new_clp->cl_namenode, parent, new);
1901 rb_insert_color(&new_clp->cl_namenode, root);
1902}
1903
1904static struct nfs4_client *
1905find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1906{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001907 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001908 struct rb_node *node = root->rb_node;
1909 struct nfs4_client *clp;
1910
1911 while (node) {
1912 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1913 cmp = compare_blob(&clp->cl_name, name);
1914 if (cmp > 0)
1915 node = node->rb_left;
1916 else if (cmp < 0)
1917 node = node->rb_right;
1918 else
1919 return clp;
1920 }
1921 return NULL;
1922}
1923
1924static void
1925add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
1927 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001928 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Trond Myklebust0a880a22014-07-30 08:27:10 -04001930 lockdep_assert_held(&nn->client_lock);
1931
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001932 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001933 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001935 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001936 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937}
1938
NeilBrownfd39ca92005-06-23 22:04:03 -07001939static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940move_to_confirmed(struct nfs4_client *clp)
1941{
1942 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001943 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Trond Myklebust0a880a22014-07-30 08:27:10 -04001945 lockdep_assert_held(&nn->client_lock);
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001948 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001949 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001950 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001951 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001952 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953}
1954
1955static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001956find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
1958 struct nfs4_client *clp;
1959 unsigned int idhashval = clientid_hashval(clid->cl_id);
1960
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001961 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001962 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001963 if ((bool)clp->cl_minorversion != sessions)
1964 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001965 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
1969 return NULL;
1970}
1971
1972static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001973find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1974{
1975 struct list_head *tbl = nn->conf_id_hashtbl;
1976
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);
1979}
1980
1981static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001982find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001984 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Trond Myklebust0a880a22014-07-30 08:27:10 -04001986 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001987 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988}
1989
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001990static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001991{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001992 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001993}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001994
NeilBrown28ce6052005-06-23 22:03:56 -07001995static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001996find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001997{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001998 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001999 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002000}
2001
2002static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002003find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002004{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002005 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002006 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002007}
2008
NeilBrownfd39ca92005-06-23 22:04:03 -07002009static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002010gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002012 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002013 struct sockaddr *sa = svc_addr(rqstp);
2014 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002015 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Jeff Layton7077ecb2009-08-14 12:57:58 -04002017 /* Currently, we only support tcp and tcp6 for the callback channel */
2018 if (se->se_callback_netid_len == 3 &&
2019 !memcmp(se->se_callback_netid_val, "tcp", 3))
2020 expected_family = AF_INET;
2021 else if (se->se_callback_netid_len == 4 &&
2022 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2023 expected_family = AF_INET6;
2024 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 goto out_err;
2026
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002027 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002028 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002029 (struct sockaddr *)&conn->cb_addr,
2030 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002031
J. Bruce Fields07263f12010-05-31 19:09:40 -04002032 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002034
J. Bruce Fields07263f12010-05-31 19:09:40 -04002035 if (conn->cb_addr.ss_family == AF_INET6)
2036 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002037
J. Bruce Fields07263f12010-05-31 19:09:40 -04002038 conn->cb_prog = se->se_callback_prog;
2039 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002040 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 return;
2042out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002043 conn->cb_addr.ss_family = AF_UNSPEC;
2044 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002045 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 "will not receive delegations\n",
2047 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 return;
2050}
2051
Andy Adamson074fe892009-04-03 08:28:15 +03002052/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002053 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002054 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002055static void
Andy Adamson074fe892009-04-03 08:28:15 +03002056nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2057{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002058 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002059 struct nfsd4_slot *slot = resp->cstate.slot;
2060 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002061
Andy Adamson557ce262009-08-28 08:45:04 -04002062 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002063
Andy Adamson557ce262009-08-28 08:45:04 -04002064 slot->sl_opcnt = resp->opcnt;
2065 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002066
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002067 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002068 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002069 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002070 return;
2071 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002072 base = resp->cstate.data_offset;
2073 slot->sl_datalen = buf->len - base;
2074 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04002075 WARN("%s: sessions DRC could not cache compound\n", __func__);
2076 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002077}
2078
2079/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002080 * Encode the replay sequence operation from the slot values.
2081 * If cachethis is FALSE encode the uncached rep error on the next
2082 * operation which sets resp->p and increments resp->opcnt for
2083 * nfs4svc_encode_compoundres.
2084 *
Andy Adamson074fe892009-04-03 08:28:15 +03002085 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002086static __be32
2087nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2088 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002089{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002090 struct nfsd4_op *op;
2091 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002092
Andy Adamsonabfabf82009-07-23 19:02:18 -04002093 /* Encode the replayed sequence operation */
2094 op = &args->ops[resp->opcnt - 1];
2095 nfsd4_encode_operation(resp, op);
2096
2097 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002098 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002099 op = &args->ops[resp->opcnt++];
2100 op->status = nfserr_retry_uncached_rep;
2101 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002102 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002103 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002104}
2105
2106/*
Andy Adamson557ce262009-08-28 08:45:04 -04002107 * The sequence operation is not cached because we can use the slot and
2108 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002109 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002110static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002111nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2112 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002113{
Andy Adamson557ce262009-08-28 08:45:04 -04002114 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002115 struct xdr_stream *xdr = &resp->xdr;
2116 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002117 __be32 status;
2118
Andy Adamson557ce262009-08-28 08:45:04 -04002119 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002120
Andy Adamsonabfabf82009-07-23 19:02:18 -04002121 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002122 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002123 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002124
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002125 p = xdr_reserve_space(xdr, slot->sl_datalen);
2126 if (!p) {
2127 WARN_ON_ONCE(1);
2128 return nfserr_serverfault;
2129 }
2130 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2131 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002132
Andy Adamson557ce262009-08-28 08:45:04 -04002133 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002134 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002135}
2136
Andy Adamson0733d212009-04-03 08:28:01 +03002137/*
2138 * Set the exchange_id flags returned by the server.
2139 */
2140static void
2141nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2142{
2143 /* pNFS is not supported */
2144 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2145
2146 /* Referrals are supported, Migration is not. */
2147 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2148
2149 /* set the wire flags to return to client. */
2150 clid->flags = new->cl_exchange_flags;
2151}
2152
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002153static bool client_has_state(struct nfs4_client *clp)
2154{
2155 /*
2156 * Note clp->cl_openowners check isn't quite right: there's no
2157 * need to count owners without stateid's.
2158 *
2159 * Also note we should probably be using this in 4.0 case too.
2160 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002161 return !list_empty(&clp->cl_openowners)
2162 || !list_empty(&clp->cl_delegations)
2163 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002164}
2165
Al Virob37ad282006-10-19 23:28:59 -07002166__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002167nfsd4_exchange_id(struct svc_rqst *rqstp,
2168 struct nfsd4_compound_state *cstate,
2169 struct nfsd4_exchange_id *exid)
2170{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002171 struct nfs4_client *conf, *new;
2172 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002173 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002174 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002175 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002176 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002177 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002178 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002179
Jeff Layton363168b2009-08-14 12:57:56 -04002180 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002181 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002182 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002183 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002184 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002185
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002186 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002187 return nfserr_inval;
2188
Andy Adamson0733d212009-04-03 08:28:01 +03002189 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002190 case SP4_MACH_CRED:
2191 if (!svc_rqst_integrity_protected(rqstp))
2192 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002193 case SP4_NONE:
2194 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002195 default: /* checked by xdr code */
2196 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002197 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002198 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002199 }
2200
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002201 new = create_client(exid->clname, rqstp, &verf);
2202 if (new == NULL)
2203 return nfserr_jukebox;
2204
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002205 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002206 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002207 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002208 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002209 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2210 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2211
J. Bruce Fields136e6582012-05-12 20:37:23 -04002212 if (update) {
2213 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002214 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002215 goto out;
2216 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002217 if (!mach_creds_match(conf, rqstp)) {
2218 status = nfserr_wrong_cred;
2219 goto out;
2220 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002221 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002222 status = nfserr_perm;
2223 goto out;
2224 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002225 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002226 status = nfserr_not_same;
2227 goto out;
2228 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002229 /* case 6 */
2230 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002231 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002232 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002233 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002234 if (client_has_state(conf)) {
2235 status = nfserr_clid_inuse;
2236 goto out;
2237 }
Andy Adamson0733d212009-04-03 08:28:01 +03002238 goto out_new;
2239 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002240 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002241 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002242 goto out_copy;
2243 }
2244 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002245 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002246 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002247 }
2248
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002249 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002250 status = nfserr_noent;
2251 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002252 }
2253
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002254 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002255 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002256 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002257
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002258 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002259out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002260 if (conf) {
2261 status = mark_client_expired_locked(conf);
2262 if (status)
2263 goto out;
2264 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002265 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002266 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002267
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002268 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002269 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002270 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002271out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002272 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2273 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002274
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002275 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2276 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002277
2278 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002279 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002280 status = nfs_ok;
2281
2282out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002283 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002284 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002285 expire_client(new);
2286 if (unconf)
2287 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002288 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002289}
2290
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002291static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002292check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002293{
Andy Adamson88e588d2009-07-23 19:02:15 -04002294 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2295 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002296
2297 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002298 if (slot_inuse) {
2299 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002300 return nfserr_jukebox;
2301 else
2302 return nfserr_seq_misordered;
2303 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002304 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002305 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002306 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002307 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002308 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002309 return nfserr_seq_misordered;
2310}
2311
Andy Adamson49557cc2009-07-23 19:02:16 -04002312/*
2313 * Cache the create session result into the create session single DRC
2314 * slot cache by saving the xdr structure. sl_seqid has been set.
2315 * Do this for solo or embedded create session operations.
2316 */
2317static void
2318nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002319 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002320{
2321 slot->sl_status = nfserr;
2322 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2323}
2324
2325static __be32
2326nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2327 struct nfsd4_clid_slot *slot)
2328{
2329 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2330 return slot->sl_status;
2331}
2332
Mi Jinlong1b74c252011-07-14 14:50:17 +08002333#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2334 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2335 1 + /* MIN tag is length with zero, only length */ \
2336 3 + /* version, opcount, opcode */ \
2337 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2338 /* seqid, slotID, slotID, cache */ \
2339 4 ) * sizeof(__be32))
2340
2341#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2342 2 + /* verifier: AUTH_NULL, length 0 */\
2343 1 + /* status */ \
2344 1 + /* MIN tag is length with zero, only length */ \
2345 3 + /* opcount, opcode, opstatus*/ \
2346 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2347 /* seqid, slotID, slotID, slotID, status */ \
2348 5 ) * sizeof(__be32))
2349
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002350static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002351{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002352 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2353
J. Bruce Fields373cd402013-04-08 15:42:12 -04002354 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2355 return nfserr_toosmall;
2356 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2357 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002358 ca->headerpadsz = 0;
2359 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2360 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2361 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2362 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2363 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2364 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2365 /*
2366 * Note decreasing slot size below client's request may make it
2367 * difficult for client to function correctly, whereas
2368 * decreasing the number of slots will (just?) affect
2369 * performance. When short on memory we therefore prefer to
2370 * decrease number of slots instead of their size. Clients that
2371 * request larger slots than they need will get poor results:
2372 */
2373 ca->maxreqs = nfsd4_get_drc_mem(ca);
2374 if (!ca->maxreqs)
2375 return nfserr_jukebox;
2376
J. Bruce Fields373cd402013-04-08 15:42:12 -04002377 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002378}
2379
Kinglong Mee8a891632013-12-23 18:11:02 +08002380#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2381 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2382#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2383 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2384
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002385static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2386{
2387 ca->headerpadsz = 0;
2388
2389 /*
2390 * These RPC_MAX_HEADER macros are overkill, especially since we
2391 * don't even do gss on the backchannel yet. But this is still
2392 * less than 1k. Tighten up this estimate in the unlikely event
2393 * it turns out to be a problem for some client:
2394 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002395 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002396 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002397 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002398 return nfserr_toosmall;
2399 ca->maxresp_cached = 0;
2400 if (ca->maxops < 2)
2401 return nfserr_toosmall;
2402
2403 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002404}
2405
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002406static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2407{
2408 switch (cbs->flavor) {
2409 case RPC_AUTH_NULL:
2410 case RPC_AUTH_UNIX:
2411 return nfs_ok;
2412 default:
2413 /*
2414 * GSS case: the spec doesn't allow us to return this
2415 * error. But it also doesn't allow us not to support
2416 * GSS.
2417 * I'd rather this fail hard than return some error the
2418 * client might think it can already handle:
2419 */
2420 return nfserr_encr_alg_unsupp;
2421 }
2422}
2423
Andy Adamson069b6ad2009-04-03 08:27:58 +03002424__be32
2425nfsd4_create_session(struct svc_rqst *rqstp,
2426 struct nfsd4_compound_state *cstate,
2427 struct nfsd4_create_session *cr_ses)
2428{
Jeff Layton363168b2009-08-14 12:57:56 -04002429 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002430 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002431 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002432 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002433 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002434 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002435 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002436 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002437
Mi Jinlonga62573d2011-03-23 17:57:07 +08002438 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2439 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002440 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2441 if (status)
2442 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002443 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002444 if (status)
2445 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002446 status = check_backchannel_attrs(&cr_ses->back_channel);
2447 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002448 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002449 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002450 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002451 if (!new)
2452 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002453 conn = alloc_conn_from_crses(rqstp, cr_ses);
2454 if (!conn)
2455 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002456
Jeff Laytond20c11d2014-07-30 08:27:07 -04002457 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002458 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002459 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002460 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002461
2462 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002463 status = nfserr_wrong_cred;
2464 if (!mach_creds_match(conf, rqstp))
2465 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002466 cs_slot = &conf->cl_cs_slot;
2467 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002468 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002469 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002470 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002471 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002472 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002473 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002474 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002475 } else if (unconf) {
2476 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002477 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002478 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002479 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002480 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002481 status = nfserr_wrong_cred;
2482 if (!mach_creds_match(unconf, rqstp))
2483 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002484 cs_slot = &unconf->cl_cs_slot;
2485 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002486 if (status) {
2487 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002488 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002489 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002490 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002491 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002492 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002493 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002494 if (status) {
2495 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002496 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002497 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002498 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002499 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002500 conf = unconf;
2501 } else {
2502 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002503 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002504 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002505 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002506 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002507 * We do not support RDMA or persistent sessions
2508 */
2509 cr_ses->flags &= ~SESSION4_PERSIST;
2510 cr_ses->flags &= ~SESSION4_RDMA;
2511
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002512 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002513 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002514
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002515 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002516 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002517 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002518 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002519
Jeff Laytond20c11d2014-07-30 08:27:07 -04002520 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002521 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002522 spin_unlock(&nn->client_lock);
2523 /* init connection and backchannel */
2524 nfsd4_init_conn(rqstp, conn, new);
2525 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002526 if (old)
2527 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002528 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002529out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002530 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002531 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002532 if (old)
2533 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002534out_free_session:
2535 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002536out_release_drc_mem:
2537 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002538 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002539}
2540
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002541static __be32 nfsd4_map_bcts_dir(u32 *dir)
2542{
2543 switch (*dir) {
2544 case NFS4_CDFC4_FORE:
2545 case NFS4_CDFC4_BACK:
2546 return nfs_ok;
2547 case NFS4_CDFC4_FORE_OR_BOTH:
2548 case NFS4_CDFC4_BACK_OR_BOTH:
2549 *dir = NFS4_CDFC4_BOTH;
2550 return nfs_ok;
2551 };
2552 return nfserr_inval;
2553}
2554
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002555__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2556{
2557 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002558 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002559 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002560
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002561 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2562 if (status)
2563 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002564 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002565 session->se_cb_prog = bc->bc_cb_program;
2566 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002567 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002568
2569 nfsd4_probe_callback(session->se_client);
2570
2571 return nfs_ok;
2572}
2573
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002574__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2575 struct nfsd4_compound_state *cstate,
2576 struct nfsd4_bind_conn_to_session *bcts)
2577{
2578 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002579 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002580 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002581 struct net *net = SVC_NET(rqstp);
2582 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002583
2584 if (!nfsd4_last_compound_op(rqstp))
2585 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002586 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002587 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002588 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002589 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002590 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002591 status = nfserr_wrong_cred;
2592 if (!mach_creds_match(session->se_client, rqstp))
2593 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002594 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002595 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002596 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002597 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002598 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002599 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002600 goto out;
2601 nfsd4_init_conn(rqstp, conn, session);
2602 status = nfs_ok;
2603out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002604 nfsd4_put_session(session);
2605out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002606 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002607}
2608
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002609static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2610{
2611 if (!session)
2612 return 0;
2613 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2614}
2615
Andy Adamson069b6ad2009-04-03 08:27:58 +03002616__be32
2617nfsd4_destroy_session(struct svc_rqst *r,
2618 struct nfsd4_compound_state *cstate,
2619 struct nfsd4_destroy_session *sessionid)
2620{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002621 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002622 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002623 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002624 struct net *net = SVC_NET(r);
2625 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002626
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002627 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002628 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002629 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002630 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002631 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002632 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002633 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002634 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002635 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002636 if (!ses)
2637 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002638 status = nfserr_wrong_cred;
2639 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002640 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002641 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002642 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002643 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002644 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002645 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002646
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002647 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002648
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002649 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002650 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002651out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002652 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002653out_client_lock:
2654 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002655out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03002656 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002657}
2658
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002659static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002660{
2661 struct nfsd4_conn *c;
2662
2663 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002664 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002665 return c;
2666 }
2667 }
2668 return NULL;
2669}
2670
J. Bruce Fields57266a62013-04-13 14:27:29 -04002671static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002672{
2673 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002674 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002675 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002676 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002677
2678 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002679 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002680 if (c)
2681 goto out_free;
2682 status = nfserr_conn_not_bound_to_session;
2683 if (clp->cl_mach_cred)
2684 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002685 __nfsd4_hash_conn(new, ses);
2686 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002687 ret = nfsd4_register_conn(new);
2688 if (ret)
2689 /* oops; xprt is already down: */
2690 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002691 return nfs_ok;
2692out_free:
2693 spin_unlock(&clp->cl_lock);
2694 free_conn(new);
2695 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002696}
2697
Mi Jinlong868b89c2011-04-27 09:09:58 +08002698static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2699{
2700 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2701
2702 return args->opcnt > session->se_fchannel.maxops;
2703}
2704
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002705static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2706 struct nfsd4_session *session)
2707{
2708 struct xdr_buf *xb = &rqstp->rq_arg;
2709
2710 return xb->len > session->se_fchannel.maxreq_sz;
2711}
2712
Andy Adamson069b6ad2009-04-03 08:27:58 +03002713__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002714nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002715 struct nfsd4_compound_state *cstate,
2716 struct nfsd4_sequence *seq)
2717{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002718 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002719 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002720 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002721 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002722 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002723 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002724 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002725 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002726 struct net *net = SVC_NET(rqstp);
2727 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002728
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002729 if (resp->opcnt != 1)
2730 return nfserr_sequence_pos;
2731
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002732 /*
2733 * Will be either used or freed by nfsd4_sequence_check_conn
2734 * below.
2735 */
2736 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2737 if (!conn)
2738 return nfserr_jukebox;
2739
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002740 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002741 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002742 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002743 goto out_no_session;
2744 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002745
Mi Jinlong868b89c2011-04-27 09:09:58 +08002746 status = nfserr_too_many_ops;
2747 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002748 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002749
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002750 status = nfserr_req_too_big;
2751 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002752 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002753
Benny Halevyb85d4c02009-04-03 08:28:08 +03002754 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002755 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002756 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002757
Andy Adamson557ce262009-08-28 08:45:04 -04002758 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002759 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2760
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002761 /* We do not negotiate the number of slots yet, so set the
2762 * maxslots to the session maxreqs which is used to encode
2763 * sr_highest_slotid and the sr_target_slot id to maxslots */
2764 seq->maxslots = session->se_fchannel.maxreqs;
2765
J. Bruce Fields73e79482012-02-13 16:39:00 -05002766 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2767 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002768 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002769 status = nfserr_seq_misordered;
2770 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002771 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002772 cstate->slot = slot;
2773 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002774 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002775 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002776 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002777 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002778 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002779 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002780 }
2781 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002782 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002783
J. Bruce Fields57266a62013-04-13 14:27:29 -04002784 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002785 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002786 if (status)
2787 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002788
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002789 buflen = (seq->cachethis) ?
2790 session->se_fchannel.maxresp_cached :
2791 session->se_fchannel.maxresp_sz;
2792 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2793 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002794 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002795 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002796 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002797
2798 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002799 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002800 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002801 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002802 if (seq->cachethis)
2803 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002804 else
2805 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002806
2807 cstate->slot = slot;
2808 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002809 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002810
Benny Halevyb85d4c02009-04-03 08:28:08 +03002811out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002812 switch (clp->cl_cb_state) {
2813 case NFSD4_CB_DOWN:
2814 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2815 break;
2816 case NFSD4_CB_FAULT:
2817 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2818 break;
2819 default:
2820 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002821 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002822 if (!list_empty(&clp->cl_revoked))
2823 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002824out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002825 if (conn)
2826 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002827 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002828 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002829out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002830 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002831 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002832}
2833
Trond Myklebustb6076642014-06-30 11:48:35 -04002834void
2835nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2836{
2837 struct nfsd4_compound_state *cs = &resp->cstate;
2838
2839 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002840 if (cs->status != nfserr_replay_cache) {
2841 nfsd4_store_cache_entry(resp);
2842 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2843 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002844 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002845 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002846 } else if (cs->clp)
2847 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002848}
2849
Mi Jinlong345c2842011-10-20 17:51:39 +08002850__be32
2851nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2852{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002853 struct nfs4_client *conf, *unconf;
2854 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002855 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002856 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002857
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002858 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002859 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002860 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002861 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002862
2863 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002864 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002865 status = nfserr_clientid_busy;
2866 goto out;
2867 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04002868 status = mark_client_expired_locked(conf);
2869 if (status)
2870 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002871 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08002872 } else if (unconf)
2873 clp = unconf;
2874 else {
2875 status = nfserr_stale_clientid;
2876 goto out;
2877 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002878 if (!mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002879 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002880 status = nfserr_wrong_cred;
2881 goto out;
2882 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002883 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002884out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002885 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002886 if (clp)
2887 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002888 return status;
2889}
2890
Andy Adamson069b6ad2009-04-03 08:27:58 +03002891__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002892nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2893{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002894 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002895
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002896 if (rc->rca_one_fs) {
2897 if (!cstate->current_fh.fh_dentry)
2898 return nfserr_nofilehandle;
2899 /*
2900 * We don't take advantage of the rca_one_fs case.
2901 * That's OK, it's optional, we can safely ignore it.
2902 */
2903 return nfs_ok;
2904 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002905
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002906 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002907 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2908 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002909 goto out;
2910
2911 status = nfserr_stale_clientid;
2912 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002913 /*
2914 * The following error isn't really legal.
2915 * But we only get here if the client just explicitly
2916 * destroyed the client. Surely it no longer cares what
2917 * error it gets back on an operation for the dead
2918 * client.
2919 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002920 goto out;
2921
2922 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002923 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002924out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002925 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002926}
2927
2928__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002929nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2930 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002932 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002934 struct nfs4_client *conf, *new;
2935 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002936 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002937 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2938
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002939 new = create_client(clname, rqstp, &clverifier);
2940 if (new == NULL)
2941 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002942 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002943 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002944 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002945 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002946 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002948 if (clp_used_exchangeid(conf))
2949 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002950 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002951 char addr_str[INET6_ADDRSTRLEN];
2952 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2953 sizeof(addr_str));
2954 dprintk("NFSD: setclientid: string in use by client "
2955 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 goto out;
2957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002959 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002960 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002961 unhash_client_locked(unconf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002962 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002963 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002965 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002966 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002967 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002968 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002969 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2971 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2972 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002973 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 status = nfs_ok;
2975out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002976 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002977 if (new)
2978 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002979 if (unconf)
2980 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 return status;
2982}
2983
2984
Al Virob37ad282006-10-19 23:28:59 -07002985__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002986nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2987 struct nfsd4_compound_state *cstate,
2988 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989{
NeilBrown21ab45a2005-06-23 22:04:14 -07002990 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002991 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2993 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002994 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002995 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002997 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07002999
Jeff Laytond20c11d2014-07-30 08:27:07 -04003000 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003001 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003002 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003003 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003004 * We try hard to give out unique clientid's, so if we get an
3005 * attempt to confirm the same clientid with a different cred,
3006 * there's a bug somewhere. Let's charitably assume it's our
3007 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003008 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04003009 status = nfserr_serverfault;
3010 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3011 goto out;
3012 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3013 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003014 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003015 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3016 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003018 else /* case 4: client hasn't noticed we rebooted yet? */
3019 status = nfserr_stale_clientid;
3020 goto out;
3021 }
3022 status = nfs_ok;
3023 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003024 old = unconf;
3025 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003026 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003027 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003028 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3029 if (old) {
3030 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003031 if (status) {
3032 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003033 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003034 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003035 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003036 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003037 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003038 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003039 get_client_locked(conf);
3040 spin_unlock(&nn->client_lock);
3041 nfsd4_probe_callback(conf);
3042 spin_lock(&nn->client_lock);
3043 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003045 spin_unlock(&nn->client_lock);
3046 if (old)
3047 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 return status;
3049}
3050
J. Bruce Fields32513b42011-10-13 16:00:16 -04003051static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003053 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3054}
3055
3056/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003057static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3058 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003059{
Trond Myklebust950e0112014-06-30 11:48:31 -04003060 lockdep_assert_held(&state_lock);
3061
J. Bruce Fields32513b42011-10-13 16:00:16 -04003062 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003063 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003064 INIT_LIST_HEAD(&fp->fi_stateids);
3065 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003066 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003067 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003068 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003069 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003070 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3071 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton5b095e92014-10-23 08:01:02 -04003072 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073}
3074
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003075void
NeilBrowne60d4392005-06-23 22:03:01 -07003076nfsd4_free_slabs(void)
3077{
Christoph Hellwigabf11352014-05-21 07:43:03 -07003078 kmem_cache_destroy(openowner_slab);
3079 kmem_cache_destroy(lockowner_slab);
3080 kmem_cache_destroy(file_slab);
3081 kmem_cache_destroy(stateid_slab);
3082 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003083}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Bryan Schumaker72083392011-11-01 15:24:59 -04003085int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086nfsd4_init_slabs(void)
3087{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003088 openowner_slab = kmem_cache_create("nfsd4_openowners",
3089 sizeof(struct nfs4_openowner), 0, 0, NULL);
3090 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003091 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003092 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003093 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003094 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003095 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003096 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003097 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003098 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003099 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003100 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003101 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003102 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003103 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003104 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003105 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003106 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003107 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003109
3110out_free_stateid_slab:
3111 kmem_cache_destroy(stateid_slab);
3112out_free_file_slab:
3113 kmem_cache_destroy(file_slab);
3114out_free_lockowner_slab:
3115 kmem_cache_destroy(lockowner_slab);
3116out_free_openowner_slab:
3117 kmem_cache_destroy(openowner_slab);
3118out:
NeilBrowne60d4392005-06-23 22:03:01 -07003119 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3120 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121}
3122
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003123static void init_nfs4_replay(struct nfs4_replay *rp)
3124{
3125 rp->rp_status = nfserr_serverfault;
3126 rp->rp_buflen = 0;
3127 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003128 mutex_init(&rp->rp_mutex);
3129}
3130
3131static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3132 struct nfs4_stateowner *so)
3133{
3134 if (!nfsd4_has_session(cstate)) {
3135 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003136 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04003137 }
3138}
3139
3140void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3141{
3142 struct nfs4_stateowner *so = cstate->replay_owner;
3143
3144 if (so != NULL) {
3145 cstate->replay_owner = NULL;
3146 mutex_unlock(&so->so_replay.rp_mutex);
3147 nfs4_put_stateowner(so);
3148 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003149}
3150
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003151static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
3153 struct nfs4_stateowner *sop;
3154
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003155 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003156 if (!sop)
3157 return NULL;
3158
3159 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3160 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003161 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003162 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003164 sop->so_owner.len = owner->len;
3165
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003166 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003167 sop->so_client = clp;
3168 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003169 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003170 return sop;
3171}
3172
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003173static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003174{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003175 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003176
Trond Myklebustd4f04892014-07-29 21:34:36 -04003177 list_add(&oo->oo_owner.so_strhash,
3178 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003179 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180}
3181
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003182static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3183{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003184 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003185}
3186
Jeff Layton6b180f02014-07-29 21:34:26 -04003187static void nfs4_free_openowner(struct nfs4_stateowner *so)
3188{
3189 struct nfs4_openowner *oo = openowner(so);
3190
3191 kmem_cache_free(openowner_slab, oo);
3192}
3193
3194static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003195 .so_unhash = nfs4_unhash_openowner,
3196 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003197};
3198
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003199static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003200alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003201 struct nfsd4_compound_state *cstate)
3202{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003203 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003204 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003206 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3207 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003209 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003210 oo->oo_owner.so_is_open_owner = 1;
3211 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003212 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003213 if (nfsd4_has_session(cstate))
3214 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003215 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003216 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003217 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003218 spin_lock(&clp->cl_lock);
3219 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003220 if (ret == NULL) {
3221 hash_openowner(oo, clp, strhashval);
3222 ret = oo;
3223 } else
3224 nfs4_free_openowner(&oo->oo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003225 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003226 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227}
3228
J. Bruce Fields996e0932011-10-17 11:14:48 -04003229static 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 -04003230 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003232 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003233 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003234 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003235 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07003236 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003237 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 stp->st_access_bmap = 0;
3239 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003240 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003241 spin_lock(&oo->oo_owner.so_client->cl_lock);
3242 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003243 spin_lock(&fp->fi_lock);
3244 list_add(&stp->st_perfile, &fp->fi_stateids);
3245 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003246 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247}
3248
Jeff Laytond3134b12014-07-29 21:34:32 -04003249/*
3250 * In the 4.0 case we need to keep the owners around a little while to handle
3251 * CLOSE replay. We still do need to release any file access that is held by
3252 * them before returning however.
3253 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003255move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256{
Jeff Layton217526e2014-07-30 08:27:11 -04003257 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003258 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3259 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3260 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003261
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003262 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
Jeff Laytonb401be222014-07-29 21:34:33 -04003264 /*
3265 * We know that we hold one reference via nfsd4_close, and another
3266 * "persistent" reference for the client. If the refcount is higher
3267 * than 2, then there are still calls in progress that are using this
3268 * stateid. We can't put the sc_file reference until they are finished.
3269 * Wait for the refcount to drop to 2. Since it has been unhashed,
3270 * there should be no danger of the refcount going back up again at
3271 * this point.
3272 */
3273 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3274
Jeff Laytond3134b12014-07-29 21:34:32 -04003275 release_all_access(s);
3276 if (s->st_stid.sc_file) {
3277 put_nfs4_file(s->st_stid.sc_file);
3278 s->st_stid.sc_file = NULL;
3279 }
Jeff Layton217526e2014-07-30 08:27:11 -04003280
3281 spin_lock(&nn->client_lock);
3282 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003283 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003284 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003285 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003286 spin_unlock(&nn->client_lock);
3287 if (last)
3288 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289}
3290
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291/* search file_hashtbl[] for file */
3292static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04003293find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 struct nfs4_file *fp;
3296
Jeff Layton5b095e92014-10-23 08:01:02 -04003297 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003298 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
Jeff Layton5b095e92014-10-23 08:01:02 -04003299 if (atomic_inc_not_zero(&fp->fi_ref))
3300 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 }
3303 return NULL;
3304}
3305
Trond Myklebust950e0112014-06-30 11:48:31 -04003306static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003307find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003308{
3309 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003310 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003311
Jeff Layton5b095e92014-10-23 08:01:02 -04003312 rcu_read_lock();
3313 fp = find_file_locked(fh, hashval);
3314 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04003315 return fp;
3316}
3317
3318static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003319find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003320{
3321 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003322 unsigned int hashval = file_hashval(fh);
3323
3324 rcu_read_lock();
3325 fp = find_file_locked(fh, hashval);
3326 rcu_read_unlock();
3327 if (fp)
3328 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04003329
3330 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04003331 fp = find_file_locked(fh, hashval);
3332 if (likely(fp == NULL)) {
3333 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04003334 fp = new;
3335 }
3336 spin_unlock(&state_lock);
3337
3338 return fp;
3339}
3340
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003341/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 * Called to check deny when READ with all zero stateid or
3343 * WRITE with all zero or all one stateid
3344 */
Al Virob37ad282006-10-19 23:28:59 -07003345static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3347{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003349 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350
Trond Myklebustca943212014-07-23 16:17:39 -04003351 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003352 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003353 return ret;
3354 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003355 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003356 if (fp->fi_share_deny & deny_type)
3357 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003358 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003359 put_nfs4_file(fp);
3360 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361}
3362
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003363static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003365 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04003366 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3367 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003368
Trond Myklebust11b91642014-07-29 21:34:08 -04003369 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003370
Jeff Layton02e12152014-07-16 10:31:57 -04003371 /*
3372 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003373 * already holding inode->i_lock.
3374 *
Jeff Laytondff13992014-07-08 14:02:49 -04003375 * If the dl_time != 0, then we know that it has already been
3376 * queued for a lease break. Don't queue it again.
3377 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003378 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003379 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003380 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003381 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003382 }
Jeff Layton02e12152014-07-16 10:31:57 -04003383 spin_unlock(&state_lock);
3384}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003386static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3387 struct rpc_task *task)
3388{
3389 struct nfs4_delegation *dp = cb_to_delegation(cb);
3390
3391 switch (task->tk_status) {
3392 case 0:
3393 return 1;
3394 case -EBADHANDLE:
3395 case -NFS4ERR_BAD_STATEID:
3396 /*
3397 * Race: client probably got cb_recall before open reply
3398 * granting delegation.
3399 */
3400 if (dp->dl_retries--) {
3401 rpc_delay(task, 2 * HZ);
3402 return 0;
3403 }
3404 /*FALLTHRU*/
3405 default:
3406 return -1;
3407 }
3408}
3409
3410static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3411{
3412 struct nfs4_delegation *dp = cb_to_delegation(cb);
3413
3414 nfs4_put_stid(&dp->dl_stid);
3415}
3416
3417static struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3418 .prepare = nfsd4_cb_recall_prepare,
3419 .done = nfsd4_cb_recall_done,
3420 .release = nfsd4_cb_recall_release,
3421};
3422
Jeff Layton02e12152014-07-16 10:31:57 -04003423static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3424{
3425 /*
3426 * We're assuming the state code never drops its reference
3427 * without first removing the lease. Since we're in this lease
3428 * callback (and since the lease code is serialized by the kernel
3429 * lock) we know the server hasn't removed the lease yet, we know
3430 * it's safe to take a reference.
3431 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003432 atomic_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02003433 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003434}
3435
Jeff Layton1c8c6012013-06-21 08:58:15 -04003436/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003437static bool
3438nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003439{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003440 bool ret = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003441 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3442 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003443
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003444 if (!fp) {
3445 WARN(1, "(%p)->fl_owner NULL\n", fl);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003446 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003447 }
3448 if (fp->fi_had_conflict) {
3449 WARN(1, "duplicate break on %p\n", fp);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003450 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003451 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003452 /*
3453 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003454 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003455 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003456 */
3457 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
Jeff Layton02e12152014-07-16 10:31:57 -04003459 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003460 fp->fi_had_conflict = true;
3461 /*
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003462 * If there are no delegations on the list, then return true
3463 * so that the lease code will go ahead and delete it.
Jeff Layton417c6622014-07-21 09:34:57 -04003464 */
3465 if (list_empty(&fp->fi_delegations))
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003466 ret = true;
Jeff Layton417c6622014-07-21 09:34:57 -04003467 else
3468 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3469 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003470 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003471 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472}
3473
Jeff Laytonc45198e2014-09-01 07:12:07 -04003474static int
3475nfsd_change_deleg_cb(struct file_lock **onlist, int arg, struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476{
3477 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04003478 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 else
3480 return -EAGAIN;
3481}
3482
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003483static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003484 .lm_break = nfsd_break_deleg_cb,
3485 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486};
3487
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003488static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3489{
3490 if (nfsd4_has_session(cstate))
3491 return nfs_ok;
3492 if (seqid == so->so_seqid - 1)
3493 return nfserr_replay_me;
3494 if (seqid == so->so_seqid)
3495 return nfs_ok;
3496 return nfserr_bad_seqid;
3497}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498
Jeff Layton4b24ca72014-06-30 11:48:44 -04003499static __be32 lookup_clientid(clientid_t *clid,
3500 struct nfsd4_compound_state *cstate,
3501 struct nfsd_net *nn)
3502{
3503 struct nfs4_client *found;
3504
3505 if (cstate->clp) {
3506 found = cstate->clp;
3507 if (!same_clid(&found->cl_clientid, clid))
3508 return nfserr_stale_clientid;
3509 return nfs_ok;
3510 }
3511
3512 if (STALE_CLIENTID(clid, nn))
3513 return nfserr_stale_clientid;
3514
3515 /*
3516 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3517 * cached already then we know this is for is for v4.0 and "sessions"
3518 * will be false.
3519 */
3520 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003521 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003522 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003523 if (!found) {
3524 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003525 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003526 }
3527 atomic_inc(&found->cl_refcount);
3528 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003529
3530 /* Cache the nfs4_client in cstate! */
3531 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003532 return nfs_ok;
3533}
3534
Al Virob37ad282006-10-19 23:28:59 -07003535__be32
Andy Adamson66689582009-04-03 08:28:45 +03003536nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003537 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 clientid_t *clientid = &open->op_clientid;
3540 struct nfs4_client *clp = NULL;
3541 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003542 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003543 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003545 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003547 /*
3548 * In case we need it later, after we've already created the
3549 * file and don't want to risk a further failure:
3550 */
3551 open->op_file = nfsd4_alloc_file();
3552 if (open->op_file == NULL)
3553 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
Trond Myklebust2d91e892014-06-30 11:48:46 -04003555 status = lookup_clientid(clientid, cstate, nn);
3556 if (status)
3557 return status;
3558 clp = cstate->clp;
3559
Trond Myklebustd4f04892014-07-29 21:34:36 -04003560 strhashval = ownerstr_hashval(&open->op_owner);
3561 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003562 open->op_openowner = oo;
3563 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003564 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003566 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003567 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003568 release_openowner(oo);
3569 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003570 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003571 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003572 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3573 if (status)
3574 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003575 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003576new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003577 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003578 if (oo == NULL)
3579 return nfserr_jukebox;
3580 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003581alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003582 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003583 if (!open->op_stp)
3584 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003585 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586}
3587
Al Virob37ad282006-10-19 23:28:59 -07003588static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003589nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3590{
3591 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3592 return nfserr_openmode;
3593 else
3594 return nfs_ok;
3595}
3596
Daniel Mackc47d8322011-05-16 16:38:14 +02003597static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003598{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003599 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3600}
3601
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003602static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003603{
3604 struct nfs4_stid *ret;
3605
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003606 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003607 if (!ret)
3608 return NULL;
3609 return delegstateid(ret);
3610}
3611
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003612static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3613{
3614 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3615 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3616}
3617
Al Virob37ad282006-10-19 23:28:59 -07003618static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003619nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003620 struct nfs4_delegation **dp)
3621{
3622 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003623 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003624 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003625
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003626 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3627 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003628 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003629 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003630 status = nfs4_check_delegmode(deleg, flags);
3631 if (status) {
3632 nfs4_put_stid(&deleg->dl_stid);
3633 goto out;
3634 }
3635 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003636out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003637 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003638 return nfs_ok;
3639 if (status)
3640 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003641 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003642 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003643}
3644
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003645static struct nfs4_ol_stateid *
3646nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003648 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003649 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650
Trond Myklebust1d31a252014-07-10 14:07:25 -04003651 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003652 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 /* ignore lock owners */
3654 if (local->st_stateowner->so_is_open_owner == 0)
3655 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003656 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003657 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003658 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003659 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003662 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003663 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664}
3665
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003666static inline int nfs4_access_to_access(u32 nfs4_access)
3667{
3668 int flags = 0;
3669
3670 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3671 flags |= NFSD_MAY_READ;
3672 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3673 flags |= NFSD_MAY_WRITE;
3674 return flags;
3675}
3676
Al Virob37ad282006-10-19 23:28:59 -07003677static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3679 struct nfsd4_open *open)
3680{
3681 struct iattr iattr = {
3682 .ia_valid = ATTR_SIZE,
3683 .ia_size = 0,
3684 };
3685 if (!open->op_truncate)
3686 return 0;
3687 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003688 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3690}
3691
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003692static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003693 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3694 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003695{
Trond Myklebustde186432014-07-10 14:07:26 -04003696 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003697 __be32 status;
3698 int oflag = nfs4_access_to_omode(open->op_share_access);
3699 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003700 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003701
Trond Myklebustde186432014-07-10 14:07:26 -04003702 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003703
3704 /*
3705 * Are we trying to set a deny mode that would conflict with
3706 * current access?
3707 */
3708 status = nfs4_file_check_deny(fp, open->op_share_deny);
3709 if (status != nfs_ok) {
3710 spin_unlock(&fp->fi_lock);
3711 goto out;
3712 }
3713
3714 /* set access to the file */
3715 status = nfs4_file_get_access(fp, open->op_share_access);
3716 if (status != nfs_ok) {
3717 spin_unlock(&fp->fi_lock);
3718 goto out;
3719 }
3720
3721 /* Set access bits in stateid */
3722 old_access_bmap = stp->st_access_bmap;
3723 set_access(open->op_share_access, stp);
3724
3725 /* Set new deny mask */
3726 old_deny_bmap = stp->st_deny_bmap;
3727 set_deny(open->op_share_deny, stp);
3728 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3729
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003730 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003731 spin_unlock(&fp->fi_lock);
3732 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003733 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003734 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003735 spin_lock(&fp->fi_lock);
3736 if (!fp->fi_fds[oflag]) {
3737 fp->fi_fds[oflag] = filp;
3738 filp = NULL;
3739 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003740 }
Trond Myklebustde186432014-07-10 14:07:26 -04003741 spin_unlock(&fp->fi_lock);
3742 if (filp)
3743 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003744
3745 status = nfsd4_truncate(rqstp, cur_fh, open);
3746 if (status)
3747 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003748out:
3749 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003750out_put_access:
3751 stp->st_access_bmap = old_access_bmap;
3752 nfs4_file_put_access(fp, open->op_share_access);
3753 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3754 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003755}
3756
Al Virob37ad282006-10-19 23:28:59 -07003757static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003758nfs4_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 -07003759{
Al Virob37ad282006-10-19 23:28:59 -07003760 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003761 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003763 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003764 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003765
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003766 /* test and set deny mode */
3767 spin_lock(&fp->fi_lock);
3768 status = nfs4_file_check_deny(fp, open->op_share_deny);
3769 if (status == nfs_ok) {
3770 old_deny_bmap = stp->st_deny_bmap;
3771 set_deny(open->op_share_deny, stp);
3772 fp->fi_share_deny |=
3773 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3774 }
3775 spin_unlock(&fp->fi_lock);
3776
3777 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003780 status = nfsd4_truncate(rqstp, cur_fh, open);
3781 if (status != nfs_ok)
3782 reset_union_bmap_deny(old_deny_bmap, stp);
3783 return status;
3784}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003787nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003789 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790}
3791
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003792/* Should we give out recallable state?: */
3793static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3794{
3795 if (clp->cl_cb_state == NFSD4_CB_UP)
3796 return true;
3797 /*
3798 * In the sessions case, since we don't have to establish a
3799 * separate connection for callbacks, we assume it's OK
3800 * until we hear otherwise:
3801 */
3802 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3803}
3804
Jeff Laytond564fbe2014-07-16 10:31:58 -04003805static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003806{
3807 struct file_lock *fl;
3808
3809 fl = locks_alloc_lock();
3810 if (!fl)
3811 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003812 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003813 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003814 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3815 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003816 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003817 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003818 return fl;
3819}
3820
J. Bruce Fields99c41512013-05-21 16:21:25 -04003821static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003822{
Trond Myklebust11b91642014-07-29 21:34:08 -04003823 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton415b96c2014-08-22 12:26:36 -04003824 struct file_lock *fl, *ret;
Jeff Layton417c6622014-07-21 09:34:57 -04003825 struct file *filp;
3826 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003827
Jeff Laytond564fbe2014-07-16 10:31:58 -04003828 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003829 if (!fl)
3830 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003831 filp = find_readable_file(fp);
3832 if (!filp) {
3833 /* We should always have a readable file here */
3834 WARN_ON_ONCE(1);
3835 return -EBADF;
3836 }
3837 fl->fl_file = filp;
Jeff Layton415b96c2014-08-22 12:26:36 -04003838 ret = fl;
Jeff Laytone6f5c782014-08-22 10:40:25 -04003839 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003840 if (fl)
Jeff Layton417c6622014-07-21 09:34:57 -04003841 locks_free_lock(fl);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003842 if (status)
Jeff Layton417c6622014-07-21 09:34:57 -04003843 goto out_fput;
Benny Halevycdc97502014-05-30 09:09:30 -04003844 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003845 spin_lock(&fp->fi_lock);
3846 /* Did the lease get broken before we took the lock? */
3847 status = -EAGAIN;
3848 if (fp->fi_had_conflict)
3849 goto out_unlock;
3850 /* Race breaker */
Jeff Layton0c637be2014-08-22 12:05:43 -04003851 if (fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04003852 status = 0;
Jeff Layton67db1032014-12-13 09:11:40 -05003853 ++fp->fi_delegees;
Jeff Layton417c6622014-07-21 09:34:57 -04003854 hash_delegation_locked(dp, fp);
3855 goto out_unlock;
3856 }
Jeff Layton417c6622014-07-21 09:34:57 -04003857 fp->fi_deleg_file = filp;
Jeff Layton67db1032014-12-13 09:11:40 -05003858 fp->fi_delegees = 1;
Benny Halevy931ee562014-05-30 09:09:27 -04003859 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003860 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003861 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003862 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003863out_unlock:
3864 spin_unlock(&fp->fi_lock);
3865 spin_unlock(&state_lock);
3866out_fput:
3867 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003868 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003869}
3870
Jeff Layton0b266932014-07-25 07:34:25 -04003871static struct nfs4_delegation *
3872nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3873 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003874{
Jeff Layton0b266932014-07-25 07:34:25 -04003875 int status;
3876 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003877
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003878 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003879 return ERR_PTR(-EAGAIN);
3880
3881 dp = alloc_init_deleg(clp, fh);
3882 if (!dp)
3883 return ERR_PTR(-ENOMEM);
3884
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003885 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003886 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003887 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003888 dp->dl_stid.sc_file = fp;
Jeff Layton0c637be2014-08-22 12:05:43 -04003889 if (!fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04003890 spin_unlock(&fp->fi_lock);
3891 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003892 status = nfs4_setlease(dp);
3893 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003894 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003895 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003896 status = -EAGAIN;
3897 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003898 }
Jeff Layton67db1032014-12-13 09:11:40 -05003899 ++fp->fi_delegees;
Benny Halevy931ee562014-05-30 09:09:27 -04003900 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003901 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003902out_unlock:
3903 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003904 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003905out:
3906 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003907 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003908 return ERR_PTR(status);
3909 }
3910 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003911}
3912
Benny Halevy4aa89132012-02-21 14:16:44 -08003913static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3914{
3915 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3916 if (status == -EAGAIN)
3917 open->op_why_no_deleg = WND4_CONTENTION;
3918 else {
3919 open->op_why_no_deleg = WND4_RESOURCE;
3920 switch (open->op_deleg_want) {
3921 case NFS4_SHARE_WANT_READ_DELEG:
3922 case NFS4_SHARE_WANT_WRITE_DELEG:
3923 case NFS4_SHARE_WANT_ANY_DELEG:
3924 break;
3925 case NFS4_SHARE_WANT_CANCEL:
3926 open->op_why_no_deleg = WND4_CANCELLED;
3927 break;
3928 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003929 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003930 }
3931 }
3932}
3933
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934/*
3935 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003936 *
3937 * Note we don't support write delegations, and won't until the vfs has
3938 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 */
3940static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003941nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3942 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943{
3944 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003945 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3946 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003947 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003948 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003950 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003951 open->op_recall = 0;
3952 switch (open->op_claim_type) {
3953 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003954 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003955 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003956 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3957 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003958 break;
3959 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003960 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003961 /*
3962 * Let's not give out any delegations till everyone's
3963 * had the chance to reclaim theirs....
3964 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003965 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003966 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003967 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003968 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003969 /*
3970 * Also, if the file was opened for write or
3971 * create, there's a good chance the client's
3972 * about to write to it, resulting in an
3973 * immediate recall (since we don't support
3974 * write delegations):
3975 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003976 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003977 goto out_no_deleg;
3978 if (open->op_create == NFS4_OPEN_CREATE)
3979 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003980 break;
3981 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003982 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003983 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003984 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003985 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003986 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003988 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003990 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003991 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003992 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003993 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003994 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003995out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003996 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3997 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003998 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003999 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004000 open->op_recall = 1;
4001 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004002
4003 /* 4.1 client asking for a delegation? */
4004 if (open->op_deleg_want)
4005 nfsd4_open_deleg_none_ext(open, status);
4006 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007}
4008
Benny Halevye27f49c2012-02-21 14:16:54 -08004009static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4010 struct nfs4_delegation *dp)
4011{
4012 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4013 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4014 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4015 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4016 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4017 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4018 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4019 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4020 }
4021 /* Otherwise the client must be confused wanting a delegation
4022 * it already has, therefore we don't return
4023 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4024 */
4025}
4026
Al Virob37ad282006-10-19 23:28:59 -07004027__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4029{
Andy Adamson66689582009-04-03 08:28:45 +03004030 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004031 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004033 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004034 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004035 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 /*
4038 * Lookup file; if found, lookup stateid and check open request,
4039 * and check for delegations in the process of being recalled.
4040 * If not found, create the nfs4_file struct
4041 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004042 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004043 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004044 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004045 if (status)
4046 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04004047 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004049 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004050 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004051 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004052 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004053 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 }
4055
4056 /*
4057 * OPEN the file, or upgrade an existing OPEN.
4058 * If truncate fails, the OPEN fails.
4059 */
4060 if (stp) {
4061 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004062 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 if (status)
4064 goto out;
4065 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004066 stp = open->op_stp;
4067 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004068 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004069 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4070 if (status) {
4071 release_open_stateid(stp);
4072 goto out;
4073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004075 update_stateid(&stp->st_stid.sc_stateid);
4076 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077
Benny Halevyd24433c2012-02-16 20:57:17 +02004078 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004079 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4080 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4081 open->op_why_no_deleg = WND4_NOT_WANTED;
4082 goto nodeleg;
4083 }
4084 }
4085
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 /*
4087 * Attempt to hand out a delegation. No error return, because the
4088 * OPEN succeeds even if we fail.
4089 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004090 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004091nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 status = nfs_ok;
4093
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004094 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004095 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004097 /* 4.1 client trying to upgrade/downgrade delegation? */
4098 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004099 open->op_deleg_want)
4100 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004101
NeilBrown13cd2182005-06-23 22:03:10 -07004102 if (fp)
4103 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004104 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05004105 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 /*
4107 * To finish the open response, we just need to set the rflags.
4108 */
4109 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004110 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004111 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004113 if (dp)
4114 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004115 if (stp)
4116 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
4118 return status;
4119}
4120
Jeff Layton58fb12e2014-07-29 21:34:27 -04004121void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4122 struct nfsd4_open *open, __be32 status)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004123{
4124 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004125 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004126
Jeff Laytond3134b12014-07-29 21:34:32 -04004127 nfsd4_cstate_assign_replay(cstate, so);
4128 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004129 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004130 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04004131 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004132 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004133 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004134}
4135
Al Virob37ad282006-10-19 23:28:59 -07004136__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004137nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4138 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139{
4140 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004141 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004142 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 dprintk("process_renew(%08x/%08x): starting\n",
4145 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004146 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004147 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004149 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004151 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004152 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 goto out;
4154 status = nfs_ok;
4155out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 return status;
4157}
4158
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004159void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004160nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004161{
Jeff Layton33dcc482012-04-10 11:08:48 -04004162 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004163 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004164 return;
4165
NeilBrowna76b4312005-06-23 22:04:01 -07004166 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004167 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004168 /*
4169 * If the server goes down again right now, an NFSv4
4170 * client will still be allowed to reclaim after it comes back up,
4171 * even if it hasn't yet had a chance to reclaim state this time.
4172 *
4173 */
Jeff Layton919b8042014-09-12 16:40:20 -04004174 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004175 /*
4176 * At this point, NFSv4 clients can still reclaim. But if the
4177 * server crashes, any that have not yet reclaimed will be out
4178 * of luck on the next boot.
4179 *
4180 * (NFSv4.1+ clients are considered to have reclaimed once they
4181 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4182 * have reclaimed after their first OPEN.)
4183 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004184 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004185 /*
4186 * At this point, and once lockd and/or any other containers
4187 * exit their grace period, further reclaims will fail and
4188 * regular locking can resume.
4189 */
NeilBrowna76b4312005-06-23 22:04:01 -07004190}
4191
NeilBrownfd39ca92005-06-23 22:04:03 -07004192static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004193nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194{
4195 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004196 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004198 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004200 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004201 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004204 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004205 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004206 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004207 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 clp = list_entry(pos, struct nfs4_client, cl_lru);
4209 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4210 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004211 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 break;
4213 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004214 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004215 dprintk("NFSD: client in use (clientid %08x)\n",
4216 clp->cl_clientid.cl_id);
4217 continue;
4218 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004219 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004220 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004221 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004222 list_for_each_safe(pos, next, &reaplist) {
4223 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 dprintk("NFSD: purging unused client (clientid %08x)\n",
4225 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004226 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 expire_client(clp);
4228 }
Benny Halevycdc97502014-05-30 09:09:30 -04004229 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004230 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004232 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4233 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004235 t = dp->dl_time - cutoff;
4236 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 break;
4238 }
Jeff Layton42690672014-07-25 07:34:20 -04004239 unhash_delegation_locked(dp);
4240 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 }
Benny Halevycdc97502014-05-30 09:09:30 -04004242 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004243 while (!list_empty(&reaplist)) {
4244 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4245 dl_recall_lru);
4246 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004247 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 }
Jeff Layton217526e2014-07-30 08:27:11 -04004249
4250 spin_lock(&nn->client_lock);
4251 while (!list_empty(&nn->close_lru)) {
4252 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4253 oo_close_lru);
4254 if (time_after((unsigned long)oo->oo_time,
4255 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004256 t = oo->oo_time - cutoff;
4257 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 break;
4259 }
Jeff Layton217526e2014-07-30 08:27:11 -04004260 list_del_init(&oo->oo_close_lru);
4261 stp = oo->oo_last_closed_stid;
4262 oo->oo_last_closed_stid = NULL;
4263 spin_unlock(&nn->client_lock);
4264 nfs4_put_stid(&stp->st_stid);
4265 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 }
Jeff Layton217526e2014-07-30 08:27:11 -04004267 spin_unlock(&nn->client_lock);
4268
Jeff Laytona832e7a2014-05-30 09:09:26 -04004269 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004270 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271}
4272
Harvey Harrisona254b242008-02-20 12:49:00 -08004273static struct workqueue_struct *laundry_wq;
4274static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004275
4276static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004277laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278{
4279 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004280 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4281 work);
4282 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4283 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004285 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004287 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288}
4289
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004290static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004291{
Trond Myklebust11b91642014-07-29 21:34:08 -04004292 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004293 return nfserr_bad_stateid;
4294 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295}
4296
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004298access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004300 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4301 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4302 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303}
4304
4305static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004306access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004308 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4309 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310}
4311
4312static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004313__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314{
Al Virob37ad282006-10-19 23:28:59 -07004315 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316
J. Bruce Fields02921912010-07-29 15:16:59 -04004317 /* For lock stateid's, we test the parent open, not the lock: */
4318 if (stp->st_openstp)
4319 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004320 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004322 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 goto out;
4324 status = nfs_ok;
4325out:
4326 return status;
4327}
4328
Al Virob37ad282006-10-19 23:28:59 -07004329static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004330check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004332 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004334 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004335 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 * conflicting state; so we must wait out the grace period. */
4337 return nfserr_grace;
4338 } else if (flags & WR_STATE)
4339 return nfs4_share_conflict(current_fh,
4340 NFS4_SHARE_DENY_WRITE);
4341 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4342 return nfs4_share_conflict(current_fh,
4343 NFS4_SHARE_DENY_READ);
4344}
4345
4346/*
4347 * Allow READ/WRITE during grace period on recovered state only for files
4348 * that are not able to provide mandatory locking.
4349 */
4350static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004351grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004353 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354}
4355
J. Bruce Fields81b82962011-08-23 11:03:29 -04004356/* Returns true iff a is later than b: */
4357static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4358{
Jim Rees1a9357f2013-05-17 17:33:00 -04004359 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004360}
4361
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004362static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004363{
Andy Adamson66689582009-04-03 08:28:45 +03004364 /*
4365 * When sessions are used the stateid generation number is ignored
4366 * when it is zero.
4367 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004368 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004369 return nfs_ok;
4370
4371 if (in->si_generation == ref->si_generation)
4372 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004373
J. Bruce Fields0836f582008-01-26 19:08:12 -05004374 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004375 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004376 return nfserr_bad_stateid;
4377 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004378 * However, we could see a stateid from the past, even from a
4379 * non-buggy client. For example, if the client sends a lock
4380 * while some IO is outstanding, the lock may bump si_generation
4381 * while the IO is still in flight. The client could avoid that
4382 * situation by waiting for responses on all the IO requests,
4383 * but better performance may result in retrying IO that
4384 * receives an old_stateid error if requests are rarely
4385 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004386 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004387 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004388}
4389
Chuck Lever7df302f2012-05-29 13:56:37 -04004390static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004391{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004392 struct nfs4_stid *s;
4393 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004394 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004395
Chuck Lever7df302f2012-05-29 13:56:37 -04004396 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004397 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004398 /* Client debugging aid. */
4399 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4400 char addr_str[INET6_ADDRSTRLEN];
4401 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4402 sizeof(addr_str));
4403 pr_warn_ratelimited("NFSD: client %s testing state ID "
4404 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004405 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004406 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004407 spin_lock(&cl->cl_lock);
4408 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004409 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004410 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004411 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004412 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004413 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004414 switch (s->sc_type) {
4415 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004416 status = nfs_ok;
4417 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004418 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004419 status = nfserr_deleg_revoked;
4420 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004421 case NFS4_OPEN_STID:
4422 case NFS4_LOCK_STID:
4423 ols = openlockstateid(s);
4424 if (ols->st_stateowner->so_is_open_owner
4425 && !(openowner(ols->st_stateowner)->oo_flags
4426 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004427 status = nfserr_bad_stateid;
4428 else
4429 status = nfs_ok;
4430 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004431 default:
4432 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004433 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004434 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004435 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004436 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004437 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004438out_unlock:
4439 spin_unlock(&cl->cl_lock);
4440 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004441}
4442
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004443static __be32
4444nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4445 stateid_t *stateid, unsigned char typemask,
4446 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004447{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004448 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004449
4450 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4451 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004452 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004453 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004454 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004455 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004456 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004457 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004458 if (status)
4459 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004460 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004461 if (!*s)
4462 return nfserr_bad_stateid;
4463 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004464}
4465
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466/*
4467* Checks for stateid operations
4468*/
Al Virob37ad282006-10-19 23:28:59 -07004469__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004470nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004471 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004473 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004474 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004476 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004478 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004479 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004480 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 if (filpp)
4483 *filpp = NULL;
4484
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004485 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 return nfserr_grace;
4487
4488 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004489 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004491 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004492 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004493 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004494 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04004495 return status;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004496 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4497 if (status)
4498 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004499 switch (s->sc_type) {
4500 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004501 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004502 status = nfs4_check_delegmode(dp, flags);
4503 if (status)
4504 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004505 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004506 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004507 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004508 WARN_ON_ONCE(1);
4509 status = nfserr_serverfault;
4510 goto out;
4511 }
Trond Myklebustde186432014-07-10 14:07:26 -04004512 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004513 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004514 break;
4515 case NFS4_OPEN_STID:
4516 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004517 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004518 status = nfs4_check_fh(current_fh, stp);
4519 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004521 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004522 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004524 status = nfs4_check_openmode(stp, flags);
4525 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004527 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004528 struct nfs4_file *fp = stp->st_stid.sc_file;
4529
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004530 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004531 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004532 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004533 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004534 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004535 break;
4536 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004537 status = nfserr_bad_stateid;
4538 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 }
4540 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004541 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004542 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004544 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 return status;
4546}
4547
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004548/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004549 * Test if the stateid is valid
4550 */
4551__be32
4552nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4553 struct nfsd4_test_stateid *test_stateid)
4554{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004555 struct nfsd4_test_stateid_id *stateid;
4556 struct nfs4_client *cl = cstate->session->se_client;
4557
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004558 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004559 stateid->ts_id_status =
4560 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004561
Bryan Schumaker17456802011-07-13 10:50:48 -04004562 return nfs_ok;
4563}
4564
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004565__be32
4566nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4567 struct nfsd4_free_stateid *free_stateid)
4568{
4569 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004570 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004571 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004572 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004573 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004574 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004575
Jeff Layton1af71cc2014-07-29 21:34:14 -04004576 spin_lock(&cl->cl_lock);
4577 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004578 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004579 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004580 switch (s->sc_type) {
4581 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004582 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004583 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004584 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004585 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4586 if (ret)
4587 break;
4588 ret = nfserr_locks_held;
4589 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004590 case NFS4_LOCK_STID:
4591 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4592 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004593 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004594 stp = openlockstateid(s);
4595 ret = nfserr_locks_held;
4596 if (check_for_locks(stp->st_stid.sc_file,
4597 lockowner(stp->st_stateowner)))
4598 break;
4599 unhash_lock_stateid(stp);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004600 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004601 nfs4_put_stid(s);
4602 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004603 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004604 case NFS4_REVOKED_DELEG_STID:
4605 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004606 list_del_init(&dp->dl_recall_lru);
4607 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004608 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004609 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004610 goto out;
4611 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004612 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004613out_unlock:
4614 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004615out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004616 return ret;
4617}
4618
NeilBrown4c4cd222005-07-07 17:59:27 -07004619static inline int
4620setlkflg (int type)
4621{
4622 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4623 RD_STATE : WR_STATE;
4624}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004626static __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 -04004627{
4628 struct svc_fh *current_fh = &cstate->current_fh;
4629 struct nfs4_stateowner *sop = stp->st_stateowner;
4630 __be32 status;
4631
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004632 status = nfsd4_check_seqid(cstate, sop, seqid);
4633 if (status)
4634 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004635 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4636 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004637 /*
4638 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004639 * nfserr_replay_me from the previous step, and
4640 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004641 */
4642 return nfserr_bad_stateid;
4643 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4644 if (status)
4645 return status;
4646 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004647}
4648
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649/*
4650 * Checks for sequence id mutating operations.
4651 */
Al Virob37ad282006-10-19 23:28:59 -07004652static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004653nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004654 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004655 struct nfs4_ol_stateid **stpp,
4656 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004658 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004659 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004660 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004662 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4663 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004664
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004666 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004667 if (status)
4668 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004669 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004670 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004672 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004673 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004674 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004675 else
4676 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004677 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004678}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004679
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004680static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4681 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004682{
4683 __be32 status;
4684 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004685 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004687 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004688 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004689 if (status)
4690 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004691 oo = openowner(stp->st_stateowner);
4692 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4693 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004694 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004695 }
4696 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004697 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698}
4699
Al Virob37ad282006-10-19 23:28:59 -07004700__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004701nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004702 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703{
Al Virob37ad282006-10-19 23:28:59 -07004704 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004705 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004706 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004707 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
Al Viroa6a9f182013-09-16 10:57:01 -04004709 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4710 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004712 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004713 if (status)
4714 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004716 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004717 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004718 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004719 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004720 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004721 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004722 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004723 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004724 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004725 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004726 update_stateid(&stp->st_stid.sc_stateid);
4727 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004728 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004729 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004730
Jeff Layton2a4317c2012-03-21 16:42:43 -04004731 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004732 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004733put_stateid:
4734 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004736 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 return status;
4738}
4739
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004740static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004742 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004743 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004744 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004745 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004746}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004747
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004748static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4749{
4750 switch (to_access) {
4751 case NFS4_SHARE_ACCESS_READ:
4752 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4753 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4754 break;
4755 case NFS4_SHARE_ACCESS_WRITE:
4756 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4757 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4758 break;
4759 case NFS4_SHARE_ACCESS_BOTH:
4760 break;
4761 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004762 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 }
4764}
4765
Al Virob37ad282006-10-19 23:28:59 -07004766__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004767nfsd4_open_downgrade(struct svc_rqst *rqstp,
4768 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004769 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770{
Al Virob37ad282006-10-19 23:28:59 -07004771 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004772 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004773 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
Al Viroa6a9f182013-09-16 10:57:01 -04004775 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4776 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004778 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004779 if (od->od_deleg_want)
4780 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4781 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004783 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004784 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004785 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004788 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004789 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004791 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004793 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004794 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004796 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004798 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799
Jeff Laytonce0fc432012-05-11 09:45:14 -04004800 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004802 update_stateid(&stp->st_stid.sc_stateid);
4803 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004805put_stateid:
4806 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004808 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 return status;
4810}
4811
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004812static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4813{
Trond Myklebustacf92952014-06-30 11:48:37 -04004814 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytond83017f2014-07-29 21:34:42 -04004815 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004816
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004817 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04004818 spin_lock(&clp->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04004819 unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004820
Jeff Laytond83017f2014-07-29 21:34:42 -04004821 if (clp->cl_minorversion) {
4822 put_ol_stateid_locked(s, &reaplist);
4823 spin_unlock(&clp->cl_lock);
4824 free_ol_stateid_reaplist(&reaplist);
4825 } else {
4826 spin_unlock(&clp->cl_lock);
4827 free_ol_stateid_reaplist(&reaplist);
Jeff Laytond3134b12014-07-29 21:34:32 -04004828 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04004829 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004830}
4831
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832/*
4833 * nfs4_unlock_state() called after encode
4834 */
Al Virob37ad282006-10-19 23:28:59 -07004835__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004836nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004837 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838{
Al Virob37ad282006-10-19 23:28:59 -07004839 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004840 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004841 struct net *net = SVC_NET(rqstp);
4842 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843
Al Viroa6a9f182013-09-16 10:57:01 -04004844 dprintk("NFSD: nfsd4_close on file %pd\n",
4845 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004847 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4848 &close->cl_stateid,
4849 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004850 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004851 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004852 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004854 update_stateid(&stp->st_stid.sc_stateid);
4855 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004857 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004858
4859 /* put reference from nfs4_preprocess_seqid_op */
4860 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 return status;
4863}
4864
Al Virob37ad282006-10-19 23:28:59 -07004865__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004866nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4867 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004869 struct nfs4_delegation *dp;
4870 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004871 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004872 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004873 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004875 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004876 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004878 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004879 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004880 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004881 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004882 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004883 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004884 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004885
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004886 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004887put_stateid:
4888 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889out:
4890 return status;
4891}
4892
4893
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895
Benny Halevy87df4de2008-12-15 19:42:03 +02004896static inline u64
4897end_offset(u64 start, u64 len)
4898{
4899 u64 end;
4900
4901 end = start + len;
4902 return end >= start ? end: NFS4_MAX_UINT64;
4903}
4904
4905/* last octet in a range */
4906static inline u64
4907last_byte_offset(u64 start, u64 len)
4908{
4909 u64 end;
4910
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004911 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004912 end = start + len;
4913 return end > start ? end - 1: NFS4_MAX_UINT64;
4914}
4915
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916/*
4917 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4918 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4919 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4920 * locking, this prevents us from being completely protocol-compliant. The
4921 * real solution to this problem is to start using unsigned file offsets in
4922 * the VFS, but this is a very deep change!
4923 */
4924static inline void
4925nfs4_transform_lock_offset(struct file_lock *lock)
4926{
4927 if (lock->fl_start < 0)
4928 lock->fl_start = OFFSET_MAX;
4929 if (lock->fl_end < 0)
4930 lock->fl_end = OFFSET_MAX;
4931}
4932
Kinglong Meeaef95832014-08-22 10:18:44 -04004933static void nfsd4_fl_get_owner(struct file_lock *dst, struct file_lock *src)
4934{
4935 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)src->fl_owner;
4936 dst->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lo->lo_owner));
4937}
4938
4939static void nfsd4_fl_put_owner(struct file_lock *fl)
4940{
4941 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
4942
4943 if (lo) {
4944 nfs4_put_stateowner(&lo->lo_owner);
4945 fl->fl_owner = NULL;
4946 }
4947}
4948
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004949static const struct lock_manager_operations nfsd_posix_mng_ops = {
Kinglong Meeaef95832014-08-22 10:18:44 -04004950 .lm_get_owner = nfsd4_fl_get_owner,
4951 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07004952};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953
4954static inline void
4955nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4956{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004957 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958
NeilBrownd5b90262006-04-10 22:55:22 -07004959 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004960 lo = (struct nfs4_lockowner *) fl->fl_owner;
4961 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4962 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004963 if (!deny->ld_owner.data)
4964 /* We just don't care that much */
4965 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004966 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4967 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004968 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004969nevermind:
4970 deny->ld_owner.len = 0;
4971 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004972 deny->ld_clientid.cl_boot = 0;
4973 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 }
4975 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004976 deny->ld_length = NFS4_MAX_UINT64;
4977 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4979 deny->ld_type = NFS4_READ_LT;
4980 if (fl->fl_type != F_RDLCK)
4981 deny->ld_type = NFS4_WRITE_LT;
4982}
4983
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004984static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004985find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004986 struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004988 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004989 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990
Trond Myklebust0a880a22014-07-30 08:27:10 -04004991 lockdep_assert_held(&clp->cl_lock);
4992
Trond Myklebustd4f04892014-07-29 21:34:36 -04004993 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4994 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004995 if (so->so_is_open_owner)
4996 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04004997 if (same_owner_str(so, owner))
4998 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 }
5000 return NULL;
5001}
5002
Trond Myklebustc58c6612014-07-29 21:34:35 -04005003static struct nfs4_lockowner *
5004find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005005 struct nfs4_client *clp)
Trond Myklebustc58c6612014-07-29 21:34:35 -04005006{
5007 struct nfs4_lockowner *lo;
5008
Trond Myklebustd4f04892014-07-29 21:34:36 -04005009 spin_lock(&clp->cl_lock);
5010 lo = find_lockowner_str_locked(clid, owner, clp);
5011 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005012 return lo;
5013}
5014
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005015static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5016{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005017 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005018}
5019
Jeff Layton6b180f02014-07-29 21:34:26 -04005020static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5021{
5022 struct nfs4_lockowner *lo = lockowner(sop);
5023
5024 kmem_cache_free(lockowner_slab, lo);
5025}
5026
5027static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005028 .so_unhash = nfs4_unhash_lockowner,
5029 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005030};
5031
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032/*
5033 * Alloc a lock owner structure.
5034 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005035 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005037 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005039static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005040alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5041 struct nfs4_ol_stateid *open_stp,
5042 struct nfsd4_lock *lock)
5043{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005044 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005046 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5047 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005049 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5050 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005051 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005052 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005053 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005054 ret = find_lockowner_str_locked(&clp->cl_clientid,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005055 &lock->lk_new_owner, clp);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005056 if (ret == NULL) {
5057 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005058 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005059 ret = lo;
5060 } else
5061 nfs4_free_lockowner(&lo->lo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005062 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005063 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064}
5065
Jeff Layton356a95e2014-07-29 21:34:13 -04005066static void
5067init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5068 struct nfs4_file *fp, struct inode *inode,
5069 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005071 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072
Jeff Layton356a95e2014-07-29 21:34:13 -04005073 lockdep_assert_held(&clp->cl_lock);
5074
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005075 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005076 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005077 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07005078 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005079 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005080 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005081 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005083 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005084 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005085 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005086 spin_lock(&fp->fi_lock);
5087 list_add(&stp->st_perfile, &fp->fi_stateids);
5088 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089}
5090
Jeff Laytonc53530d2014-06-30 11:48:39 -04005091static struct nfs4_ol_stateid *
5092find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5093{
5094 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005095 struct nfs4_client *clp = lo->lo_owner.so_client;
5096
5097 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005098
5099 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005100 if (lst->st_stid.sc_file == fp) {
5101 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005102 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005103 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005104 }
5105 return NULL;
5106}
5107
Jeff Layton356a95e2014-07-29 21:34:13 -04005108static struct nfs4_ol_stateid *
5109find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5110 struct inode *inode, struct nfs4_ol_stateid *ost,
5111 bool *new)
5112{
5113 struct nfs4_stid *ns = NULL;
5114 struct nfs4_ol_stateid *lst;
5115 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5116 struct nfs4_client *clp = oo->oo_owner.so_client;
5117
5118 spin_lock(&clp->cl_lock);
5119 lst = find_lock_stateid(lo, fi);
5120 if (lst == NULL) {
5121 spin_unlock(&clp->cl_lock);
5122 ns = nfs4_alloc_stid(clp, stateid_slab);
5123 if (ns == NULL)
5124 return NULL;
5125
5126 spin_lock(&clp->cl_lock);
5127 lst = find_lock_stateid(lo, fi);
5128 if (likely(!lst)) {
5129 lst = openlockstateid(ns);
5130 init_lock_stateid(lst, lo, fi, inode, ost);
5131 ns = NULL;
5132 *new = true;
5133 }
5134 }
5135 spin_unlock(&clp->cl_lock);
5136 if (ns)
5137 nfs4_put_stid(ns);
5138 return lst;
5139}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005140
NeilBrownfd39ca92005-06-23 22:04:03 -07005141static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142check_lock_length(u64 offset, u64 length)
5143{
Benny Halevy87df4de2008-12-15 19:42:03 +02005144 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 LOFF_OVERFLOW(offset, length)));
5146}
5147
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005148static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005149{
Trond Myklebust11b91642014-07-29 21:34:08 -04005150 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005151
Jeff Layton7214e862014-07-10 14:07:33 -04005152 lockdep_assert_held(&fp->fi_lock);
5153
Jeff Layton82c5ff12012-05-11 09:45:13 -04005154 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005155 return;
Jeff Layton12659652014-07-10 14:07:28 -04005156 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005157 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005158}
5159
Jeff Layton356a95e2014-07-29 21:34:13 -04005160static __be32
5161lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5162 struct nfs4_ol_stateid *ost,
5163 struct nfsd4_lock *lock,
5164 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005165{
Jeff Layton5db1c032014-07-29 21:34:28 -04005166 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005167 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005168 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5169 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005170 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005171 struct nfs4_lockowner *lo;
5172 unsigned int strhashval;
5173
Trond Myklebustd4f04892014-07-29 21:34:36 -04005174 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005175 if (!lo) {
Trond Myklebustd4f04892014-07-29 21:34:36 -04005176 strhashval = ownerstr_hashval(&lock->v.new.owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005177 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5178 if (lo == NULL)
5179 return nfserr_jukebox;
5180 } else {
5181 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005182 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005183 if (!cstate->minorversion &&
5184 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005185 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005186 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005187
Jeff Layton356a95e2014-07-29 21:34:13 -04005188 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005189 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005190 status = nfserr_jukebox;
5191 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005192 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005193 status = nfs_ok;
5194out:
5195 nfs4_put_stateowner(&lo->lo_owner);
5196 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005197}
5198
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199/*
5200 * LOCK operation
5201 */
Al Virob37ad282006-10-19 23:28:59 -07005202__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005203nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005204 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005206 struct nfs4_openowner *open_sop = NULL;
5207 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005208 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005209 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005210 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005211 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005212 struct file_lock *file_lock = NULL;
5213 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005214 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005215 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005216 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005217 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005218 struct net *net = SVC_NET(rqstp);
5219 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220
5221 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5222 (long long) lock->lk_offset,
5223 (long long) lock->lk_length);
5224
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 if (check_lock_length(lock->lk_offset, lock->lk_length))
5226 return nfserr_inval;
5227
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005228 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005229 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005230 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5231 return status;
5232 }
5233
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005235 if (nfsd4_has_session(cstate))
5236 /* See rfc 5661 18.10.3: given clientid is ignored: */
5237 memcpy(&lock->v.new.clientid,
5238 &cstate->session->se_client->cl_clientid,
5239 sizeof(clientid_t));
5240
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005242 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005246 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 lock->lk_new_open_seqid,
5248 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005249 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005250 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005252 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005253 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005254 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005255 &lock->v.new.clientid))
5256 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005257 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005258 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005259 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005260 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005261 lock->lk_old_lock_seqid,
5262 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005263 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005264 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005265 if (status)
5266 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005267 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005269 lkflg = setlkflg(lock->lk_type);
5270 status = nfs4_check_openmode(lock_stp, lkflg);
5271 if (status)
5272 goto out;
5273
NeilBrown0dd395d2005-07-07 17:59:15 -07005274 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005275 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005276 goto out;
5277 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005278 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005279 goto out;
5280
Jeff Layton21179d82012-08-21 08:03:32 -04005281 file_lock = locks_alloc_lock();
5282 if (!file_lock) {
5283 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5284 status = nfserr_jukebox;
5285 goto out;
5286 }
5287
Trond Myklebust11b91642014-07-29 21:34:08 -04005288 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 switch (lock->lk_type) {
5290 case NFS4_READ_LT:
5291 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005292 spin_lock(&fp->fi_lock);
5293 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005294 if (filp)
5295 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005296 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005297 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005298 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 case NFS4_WRITE_LT:
5300 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005301 spin_lock(&fp->fi_lock);
5302 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005303 if (filp)
5304 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005305 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005306 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005307 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 default:
5309 status = nfserr_inval;
5310 goto out;
5311 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005312 if (!filp) {
5313 status = nfserr_openmode;
5314 goto out;
5315 }
Kinglong Meeaef95832014-08-22 10:18:44 -04005316
5317 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04005318 file_lock->fl_pid = current->tgid;
5319 file_lock->fl_file = filp;
5320 file_lock->fl_flags = FL_POSIX;
5321 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5322 file_lock->fl_start = lock->lk_offset;
5323 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5324 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325
Jeff Layton21179d82012-08-21 08:03:32 -04005326 conflock = locks_alloc_lock();
5327 if (!conflock) {
5328 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5329 status = nfserr_jukebox;
5330 goto out;
5331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332
Jeff Layton21179d82012-08-21 08:03:32 -04005333 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005334 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005336 update_stateid(&lock_stp->st_stid.sc_stateid);
5337 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005339 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005340 break;
5341 case (EAGAIN): /* conflock holds conflicting lock */
5342 status = nfserr_denied;
5343 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005344 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005345 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 case (EDEADLK):
5347 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005348 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005349 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005350 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005351 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005352 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354out:
Trond Myklebustde186432014-07-10 14:07:26 -04005355 if (filp)
5356 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005357 if (lock_stp) {
5358 /* Bump seqid manually if the 4.0 replay owner is openowner */
5359 if (cstate->replay_owner &&
5360 cstate->replay_owner != &lock_sop->lo_owner &&
5361 seqid_mutating_err(ntohl(status)))
5362 lock_sop->lo_owner.so_seqid++;
5363
5364 /*
5365 * If this is a new, never-before-used stateid, and we are
5366 * returning an error, then just go ahead and release it.
5367 */
5368 if (status && new)
5369 release_lock_stateid(lock_stp);
5370
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005371 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005372 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005373 if (open_stp)
5374 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005375 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005376 if (file_lock)
5377 locks_free_lock(file_lock);
5378 if (conflock)
5379 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 return status;
5381}
5382
5383/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005384 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5385 * so we do a temporary open here just to get an open file to pass to
5386 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5387 * inode operation.)
5388 */
Al Viro04da6e92012-04-13 00:00:04 -04005389static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005390{
5391 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005392 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5393 if (!err) {
5394 err = nfserrno(vfs_test_lock(file, lock));
5395 nfsd_close(file);
5396 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005397 return err;
5398}
5399
5400/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 * LOCKT operation
5402 */
Al Virob37ad282006-10-19 23:28:59 -07005403__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005404nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5405 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406{
Jeff Layton21179d82012-08-21 08:03:32 -04005407 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005408 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005409 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005410 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005412 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 return nfserr_grace;
5414
5415 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5416 return nfserr_inval;
5417
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005418 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005419 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005420 if (status)
5421 goto out;
5422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005424 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426
Jeff Layton21179d82012-08-21 08:03:32 -04005427 file_lock = locks_alloc_lock();
5428 if (!file_lock) {
5429 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5430 status = nfserr_jukebox;
5431 goto out;
5432 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005433
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 switch (lockt->lt_type) {
5435 case NFS4_READ_LT:
5436 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005437 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 break;
5439 case NFS4_WRITE_LT:
5440 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005441 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 break;
5443 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005444 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 status = nfserr_inval;
5446 goto out;
5447 }
5448
Trond Myklebustd4f04892014-07-29 21:34:36 -04005449 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5450 cstate->clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005451 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005452 file_lock->fl_owner = (fl_owner_t)lo;
5453 file_lock->fl_pid = current->tgid;
5454 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455
Jeff Layton21179d82012-08-21 08:03:32 -04005456 file_lock->fl_start = lockt->lt_offset;
5457 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458
Jeff Layton21179d82012-08-21 08:03:32 -04005459 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460
Jeff Layton21179d82012-08-21 08:03:32 -04005461 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005462 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005463 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005464
Jeff Layton21179d82012-08-21 08:03:32 -04005465 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005467 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 }
5469out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005470 if (lo)
5471 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04005472 if (file_lock)
5473 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 return status;
5475}
5476
Al Virob37ad282006-10-19 23:28:59 -07005477__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005478nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005479 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005481 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005483 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005484 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005485 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005486 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5487
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5489 (long long) locku->lu_offset,
5490 (long long) locku->lu_length);
5491
5492 if (check_lock_length(locku->lu_offset, locku->lu_length))
5493 return nfserr_inval;
5494
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005495 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005496 &locku->lu_stateid, NFS4_LOCK_STID,
5497 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005498 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005500 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005501 if (!filp) {
5502 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005503 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005504 }
Jeff Layton21179d82012-08-21 08:03:32 -04005505 file_lock = locks_alloc_lock();
5506 if (!file_lock) {
5507 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5508 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005509 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005510 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005511
Jeff Layton21179d82012-08-21 08:03:32 -04005512 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04005513 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04005514 file_lock->fl_pid = current->tgid;
5515 file_lock->fl_file = filp;
5516 file_lock->fl_flags = FL_POSIX;
5517 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5518 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519
Jeff Layton21179d82012-08-21 08:03:32 -04005520 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5521 locku->lu_length);
5522 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523
Jeff Layton21179d82012-08-21 08:03:32 -04005524 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005525 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005526 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 goto out_nfserr;
5528 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005529 update_stateid(&stp->st_stid.sc_stateid);
5530 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005531fput:
5532 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005533put_stateid:
5534 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005536 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005537 if (file_lock)
5538 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 return status;
5540
5541out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005542 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005543 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544}
5545
5546/*
5547 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005548 * true: locks held by lockowner
5549 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005551static bool
5552check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553{
5554 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005555 int status = false;
5556 struct file *filp = find_any_file(fp);
5557 struct inode *inode;
5558
5559 if (!filp) {
5560 /* Any valid lock stateid should have some sort of access */
5561 WARN_ON_ONCE(1);
5562 return status;
5563 }
5564
5565 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
Jeff Layton1c8c6012013-06-21 08:58:15 -04005567 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005569 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005570 status = true;
5571 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005574 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005575 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 return status;
5577}
5578
Al Virob37ad282006-10-19 23:28:59 -07005579__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005580nfsd4_release_lockowner(struct svc_rqst *rqstp,
5581 struct nfsd4_compound_state *cstate,
5582 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583{
5584 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005585 struct nfs4_stateowner *sop;
5586 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005587 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005589 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005590 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005591 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005592 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593
5594 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5595 clid->cl_boot, clid->cl_id);
5596
Jeff Layton4b24ca72014-06-30 11:48:44 -04005597 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005598 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04005599 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005600
Trond Myklebustd4f04892014-07-29 21:34:36 -04005601 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005602 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005603 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005604 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005605 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005606
5607 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005608 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005609
Jeff Layton882e9d22014-07-29 21:34:37 -04005610 /* see if there are still any locks associated with it */
5611 lo = lockowner(sop);
5612 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5613 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5614 status = nfserr_locks_held;
5615 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04005616 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04005617 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005618 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005619
Kinglong Meeb5971af2014-08-22 10:18:43 -04005620 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04005621 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005622 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005623 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005624 if (lo)
5625 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 return status;
5627}
5628
5629static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005630alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631{
NeilBrowna55370a2005-06-23 22:03:52 -07005632 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633}
5634
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005635bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005636nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005637{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005638 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005639
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005640 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005641 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005642}
5643
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644/*
5645 * failure => all reset bets are off, nfserr_no_grace...
5646 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005647struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005648nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649{
5650 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005651 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652
NeilBrowna55370a2005-06-23 22:03:52 -07005653 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5654 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005655 if (crp) {
5656 strhashval = clientstr_hashval(name);
5657 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005658 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005659 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005660 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005661 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005662 }
5663 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664}
5665
Jeff Layton2a4317c2012-03-21 16:42:43 -04005666void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005667nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005668{
5669 list_del(&crp->cr_strhash);
5670 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005671 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005672}
5673
5674void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005675nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676{
5677 struct nfs4_client_reclaim *crp = NULL;
5678 int i;
5679
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005681 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5682 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005684 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 }
5686 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005687 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688}
5689
5690/*
5691 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005692struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005693nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694{
5695 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 struct nfs4_client_reclaim *crp = NULL;
5697
Jeff Layton278c9312012-11-12 15:00:52 -05005698 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699
Jeff Layton278c9312012-11-12 15:00:52 -05005700 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005701 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005702 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 return crp;
5704 }
5705 }
5706 return NULL;
5707}
5708
5709/*
5710* Called from OPEN. Look for clientid in reclaim list.
5711*/
Al Virob37ad282006-10-19 23:28:59 -07005712__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005713nfs4_check_open_reclaim(clientid_t *clid,
5714 struct nfsd4_compound_state *cstate,
5715 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005717 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005718
5719 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005720 status = lookup_clientid(clid, cstate, nn);
5721 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005722 return nfserr_reclaim_bad;
5723
Jeff Layton3b3e7b72014-09-12 16:40:22 -04005724 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
5725 return nfserr_no_grace;
5726
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005727 if (nfsd4_client_record_check(cstate->clp))
5728 return nfserr_reclaim_bad;
5729
5730 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731}
5732
Bryan Schumaker65178db2011-11-01 13:35:21 -04005733#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04005734static inline void
5735put_client(struct nfs4_client *clp)
5736{
5737 atomic_dec(&clp->cl_refcount);
5738}
5739
Jeff Layton285abde2014-07-30 08:27:24 -04005740static struct nfs4_client *
5741nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5742{
5743 struct nfs4_client *clp;
5744 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5745 nfsd_net_id);
5746
5747 if (!nfsd_netns_ready(nn))
5748 return NULL;
5749
5750 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5751 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5752 return clp;
5753 }
5754 return NULL;
5755}
5756
Jeff Layton7ec0e362014-07-30 08:27:17 -04005757u64
Jeff Layton285abde2014-07-30 08:27:24 -04005758nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04005759{
5760 struct nfs4_client *clp;
5761 u64 count = 0;
5762 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5763 nfsd_net_id);
5764 char buf[INET6_ADDRSTRLEN];
5765
5766 if (!nfsd_netns_ready(nn))
5767 return 0;
5768
5769 spin_lock(&nn->client_lock);
5770 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5771 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5772 pr_info("NFS Client: %s\n", buf);
5773 ++count;
5774 }
5775 spin_unlock(&nn->client_lock);
5776
5777 return count;
5778}
Bryan Schumaker65178db2011-11-01 13:35:21 -04005779
Jeff Laytona0926d12014-07-30 08:27:18 -04005780u64
Jeff Layton285abde2014-07-30 08:27:24 -04005781nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04005782{
5783 u64 count = 0;
5784 struct nfs4_client *clp;
5785 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5786 nfsd_net_id);
5787
5788 if (!nfsd_netns_ready(nn))
5789 return count;
5790
5791 spin_lock(&nn->client_lock);
5792 clp = nfsd_find_client(addr, addr_size);
5793 if (clp) {
5794 if (mark_client_expired_locked(clp) == nfs_ok)
5795 ++count;
5796 else
5797 clp = NULL;
5798 }
5799 spin_unlock(&nn->client_lock);
5800
5801 if (clp)
5802 expire_client(clp);
5803
5804 return count;
5805}
5806
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005807u64
Jeff Layton285abde2014-07-30 08:27:24 -04005808nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005809{
5810 u64 count = 0;
5811 struct nfs4_client *clp, *next;
5812 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5813 nfsd_net_id);
5814 LIST_HEAD(reaplist);
5815
5816 if (!nfsd_netns_ready(nn))
5817 return count;
5818
5819 spin_lock(&nn->client_lock);
5820 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5821 if (mark_client_expired_locked(clp) == nfs_ok) {
5822 list_add(&clp->cl_lru, &reaplist);
5823 if (max != 0 && ++count >= max)
5824 break;
5825 }
5826 }
5827 spin_unlock(&nn->client_lock);
5828
5829 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
5830 expire_client(clp);
5831
5832 return count;
5833}
5834
Bryan Schumaker184c1842012-11-29 11:40:44 -05005835static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5836 const char *type)
5837{
5838 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005839 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005840 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5841}
5842
Jeff Layton016200c2014-07-30 08:27:21 -04005843static void
5844nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
5845 struct list_head *collect)
5846{
5847 struct nfs4_client *clp = lst->st_stid.sc_client;
5848 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5849 nfsd_net_id);
5850
5851 if (!collect)
5852 return;
5853
5854 lockdep_assert_held(&nn->client_lock);
5855 atomic_inc(&clp->cl_refcount);
5856 list_add(&lst->st_locks, collect);
5857}
5858
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005859static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04005860 struct list_head *collect,
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005861 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005862{
5863 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005864 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005865 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005866 u64 count = 0;
5867
Jeff Layton016200c2014-07-30 08:27:21 -04005868 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005869 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005870 list_for_each_entry_safe(stp, st_next,
5871 &oop->oo_owner.so_stateids, st_perstateowner) {
5872 list_for_each_entry_safe(lst, lst_next,
5873 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04005874 if (func) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005875 func(lst);
Jeff Layton016200c2014-07-30 08:27:21 -04005876 nfsd_inject_add_lock_to_list(lst,
5877 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04005878 }
Jeff Layton016200c2014-07-30 08:27:21 -04005879 ++count;
5880 /*
5881 * Despite the fact that these functions deal
5882 * with 64-bit integers for "count", we must
5883 * ensure that it doesn't blow up the
5884 * clp->cl_refcount. Throw a warning if we
5885 * start to approach INT_MAX here.
5886 */
5887 WARN_ON_ONCE(count == (INT_MAX / 2));
5888 if (count == max)
5889 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005890 }
5891 }
5892 }
Jeff Layton016200c2014-07-30 08:27:21 -04005893out:
5894 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005895
5896 return count;
5897}
5898
Jeff Layton016200c2014-07-30 08:27:21 -04005899static u64
5900nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
5901 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05005902{
Jeff Layton016200c2014-07-30 08:27:21 -04005903 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005904}
5905
Jeff Layton016200c2014-07-30 08:27:21 -04005906static u64
5907nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05005908{
Jeff Layton016200c2014-07-30 08:27:21 -04005909 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005910 nfsd_print_count(clp, count, "locked files");
5911 return count;
5912}
5913
Jeff Layton016200c2014-07-30 08:27:21 -04005914u64
Jeff Layton285abde2014-07-30 08:27:24 -04005915nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04005916{
5917 struct nfs4_client *clp;
5918 u64 count = 0;
5919 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5920 nfsd_net_id);
5921
5922 if (!nfsd_netns_ready(nn))
5923 return 0;
5924
5925 spin_lock(&nn->client_lock);
5926 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5927 count += nfsd_print_client_locks(clp);
5928 spin_unlock(&nn->client_lock);
5929
5930 return count;
5931}
5932
5933static void
5934nfsd_reap_locks(struct list_head *reaplist)
5935{
5936 struct nfs4_client *clp;
5937 struct nfs4_ol_stateid *stp, *next;
5938
5939 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
5940 list_del_init(&stp->st_locks);
5941 clp = stp->st_stid.sc_client;
5942 nfs4_put_stid(&stp->st_stid);
5943 put_client(clp);
5944 }
5945}
5946
5947u64
Jeff Layton285abde2014-07-30 08:27:24 -04005948nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04005949{
5950 unsigned int count = 0;
5951 struct nfs4_client *clp;
5952 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5953 nfsd_net_id);
5954 LIST_HEAD(reaplist);
5955
5956 if (!nfsd_netns_ready(nn))
5957 return count;
5958
5959 spin_lock(&nn->client_lock);
5960 clp = nfsd_find_client(addr, addr_size);
5961 if (clp)
5962 count = nfsd_collect_client_locks(clp, &reaplist, 0);
5963 spin_unlock(&nn->client_lock);
5964 nfsd_reap_locks(&reaplist);
5965 return count;
5966}
5967
5968u64
Jeff Layton285abde2014-07-30 08:27:24 -04005969nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04005970{
5971 u64 count = 0;
5972 struct nfs4_client *clp;
5973 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5974 nfsd_net_id);
5975 LIST_HEAD(reaplist);
5976
5977 if (!nfsd_netns_ready(nn))
5978 return count;
5979
5980 spin_lock(&nn->client_lock);
5981 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5982 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
5983 if (max != 0 && count >= max)
5984 break;
5985 }
5986 spin_unlock(&nn->client_lock);
5987 nfsd_reap_locks(&reaplist);
5988 return count;
5989}
5990
Jeff Layton82e05ef2014-07-30 08:27:22 -04005991static u64
5992nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
5993 struct list_head *collect,
5994 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005995{
5996 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04005997 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5998 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005999 u64 count = 0;
6000
Jeff Layton82e05ef2014-07-30 08:27:22 -04006001 lockdep_assert_held(&nn->client_lock);
6002
6003 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006004 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04006005 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006006 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04006007 if (collect) {
6008 atomic_inc(&clp->cl_refcount);
6009 list_add(&oop->oo_perclient, collect);
6010 }
6011 }
6012 ++count;
6013 /*
6014 * Despite the fact that these functions deal with
6015 * 64-bit integers for "count", we must ensure that
6016 * it doesn't blow up the clp->cl_refcount. Throw a
6017 * warning if we start to approach INT_MAX here.
6018 */
6019 WARN_ON_ONCE(count == (INT_MAX / 2));
6020 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006021 break;
6022 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04006023 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006024
6025 return count;
6026}
6027
Jeff Layton82e05ef2014-07-30 08:27:22 -04006028static u64
6029nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006030{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006031 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6032
6033 nfsd_print_count(clp, count, "openowners");
6034 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006035}
6036
Jeff Layton82e05ef2014-07-30 08:27:22 -04006037static u64
6038nfsd_collect_client_openowners(struct nfs4_client *clp,
6039 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006040{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006041 return nfsd_foreach_client_openowner(clp, max, collect,
6042 unhash_openowner_locked);
6043}
6044
6045u64
Jeff Layton285abde2014-07-30 08:27:24 -04006046nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006047{
6048 struct nfs4_client *clp;
6049 u64 count = 0;
6050 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6051 nfsd_net_id);
6052
6053 if (!nfsd_netns_ready(nn))
6054 return 0;
6055
6056 spin_lock(&nn->client_lock);
6057 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6058 count += nfsd_print_client_openowners(clp);
6059 spin_unlock(&nn->client_lock);
6060
6061 return count;
6062}
6063
6064static void
6065nfsd_reap_openowners(struct list_head *reaplist)
6066{
6067 struct nfs4_client *clp;
6068 struct nfs4_openowner *oop, *next;
6069
6070 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6071 list_del_init(&oop->oo_perclient);
6072 clp = oop->oo_owner.so_client;
6073 release_openowner(oop);
6074 put_client(clp);
6075 }
6076}
6077
6078u64
Jeff Layton285abde2014-07-30 08:27:24 -04006079nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6080 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006081{
6082 unsigned int count = 0;
6083 struct nfs4_client *clp;
6084 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6085 nfsd_net_id);
6086 LIST_HEAD(reaplist);
6087
6088 if (!nfsd_netns_ready(nn))
6089 return count;
6090
6091 spin_lock(&nn->client_lock);
6092 clp = nfsd_find_client(addr, addr_size);
6093 if (clp)
6094 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6095 spin_unlock(&nn->client_lock);
6096 nfsd_reap_openowners(&reaplist);
6097 return count;
6098}
6099
6100u64
Jeff Layton285abde2014-07-30 08:27:24 -04006101nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006102{
6103 u64 count = 0;
6104 struct nfs4_client *clp;
6105 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6106 nfsd_net_id);
6107 LIST_HEAD(reaplist);
6108
6109 if (!nfsd_netns_ready(nn))
6110 return count;
6111
6112 spin_lock(&nn->client_lock);
6113 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6114 count += nfsd_collect_client_openowners(clp, &reaplist,
6115 max - count);
6116 if (max != 0 && count >= max)
6117 break;
6118 }
6119 spin_unlock(&nn->client_lock);
6120 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006121 return count;
6122}
6123
Bryan Schumaker269de302012-11-29 11:40:42 -05006124static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6125 struct list_head *victims)
6126{
6127 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006128 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6129 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006130 u64 count = 0;
6131
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006132 lockdep_assert_held(&nn->client_lock);
6133
6134 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006135 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006136 if (victims) {
6137 /*
6138 * It's not safe to mess with delegations that have a
6139 * non-zero dl_time. They might have already been broken
6140 * and could be processed by the laundromat outside of
6141 * the state_lock. Just leave them be.
6142 */
6143 if (dp->dl_time != 0)
6144 continue;
6145
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006146 atomic_inc(&clp->cl_refcount);
Jeff Layton42690672014-07-25 07:34:20 -04006147 unhash_delegation_locked(dp);
6148 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006149 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006150 ++count;
6151 /*
6152 * Despite the fact that these functions deal with
6153 * 64-bit integers for "count", we must ensure that
6154 * it doesn't blow up the clp->cl_refcount. Throw a
6155 * warning if we start to approach INT_MAX here.
6156 */
6157 WARN_ON_ONCE(count == (INT_MAX / 2));
6158 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006159 break;
6160 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006161 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006162 return count;
6163}
6164
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006165static u64
6166nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006167{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006168 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006169
6170 nfsd_print_count(clp, count, "delegations");
6171 return count;
6172}
6173
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006174u64
Jeff Layton285abde2014-07-30 08:27:24 -04006175nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006176{
6177 struct nfs4_client *clp;
6178 u64 count = 0;
6179 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6180 nfsd_net_id);
6181
6182 if (!nfsd_netns_ready(nn))
6183 return 0;
6184
6185 spin_lock(&nn->client_lock);
6186 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6187 count += nfsd_print_client_delegations(clp);
6188 spin_unlock(&nn->client_lock);
6189
6190 return count;
6191}
6192
6193static void
6194nfsd_forget_delegations(struct list_head *reaplist)
6195{
6196 struct nfs4_client *clp;
6197 struct nfs4_delegation *dp, *next;
6198
6199 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6200 list_del_init(&dp->dl_recall_lru);
6201 clp = dp->dl_stid.sc_client;
6202 revoke_delegation(dp);
6203 put_client(clp);
6204 }
6205}
6206
6207u64
Jeff Layton285abde2014-07-30 08:27:24 -04006208nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6209 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006210{
6211 u64 count = 0;
6212 struct nfs4_client *clp;
6213 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6214 nfsd_net_id);
6215 LIST_HEAD(reaplist);
6216
6217 if (!nfsd_netns_ready(nn))
6218 return count;
6219
6220 spin_lock(&nn->client_lock);
6221 clp = nfsd_find_client(addr, addr_size);
6222 if (clp)
6223 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6224 spin_unlock(&nn->client_lock);
6225
6226 nfsd_forget_delegations(&reaplist);
6227 return count;
6228}
6229
6230u64
Jeff Layton285abde2014-07-30 08:27:24 -04006231nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006232{
6233 u64 count = 0;
6234 struct nfs4_client *clp;
6235 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6236 nfsd_net_id);
6237 LIST_HEAD(reaplist);
6238
6239 if (!nfsd_netns_ready(nn))
6240 return count;
6241
6242 spin_lock(&nn->client_lock);
6243 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6244 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6245 if (max != 0 && count >= max)
6246 break;
6247 }
6248 spin_unlock(&nn->client_lock);
6249 nfsd_forget_delegations(&reaplist);
6250 return count;
6251}
6252
6253static void
6254nfsd_recall_delegations(struct list_head *reaplist)
6255{
6256 struct nfs4_client *clp;
6257 struct nfs4_delegation *dp, *next;
6258
6259 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6260 list_del_init(&dp->dl_recall_lru);
6261 clp = dp->dl_stid.sc_client;
6262 /*
6263 * We skipped all entries that had a zero dl_time before,
6264 * so we can now reset the dl_time back to 0. If a delegation
6265 * break comes in now, then it won't make any difference since
6266 * we're recalling it either way.
6267 */
6268 spin_lock(&state_lock);
6269 dp->dl_time = 0;
6270 spin_unlock(&state_lock);
6271 nfsd_break_one_deleg(dp);
6272 put_client(clp);
6273 }
6274}
6275
6276u64
Jeff Layton285abde2014-07-30 08:27:24 -04006277nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006278 size_t addr_size)
6279{
6280 u64 count = 0;
6281 struct nfs4_client *clp;
6282 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6283 nfsd_net_id);
6284 LIST_HEAD(reaplist);
6285
6286 if (!nfsd_netns_ready(nn))
6287 return count;
6288
6289 spin_lock(&nn->client_lock);
6290 clp = nfsd_find_client(addr, addr_size);
6291 if (clp)
6292 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6293 spin_unlock(&nn->client_lock);
6294
6295 nfsd_recall_delegations(&reaplist);
6296 return count;
6297}
6298
6299u64
Jeff Layton285abde2014-07-30 08:27:24 -04006300nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006301{
6302 u64 count = 0;
6303 struct nfs4_client *clp, *next;
6304 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6305 nfsd_net_id);
6306 LIST_HEAD(reaplist);
6307
6308 if (!nfsd_netns_ready(nn))
6309 return count;
6310
6311 spin_lock(&nn->client_lock);
6312 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6313 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6314 if (max != 0 && ++count >= max)
6315 break;
6316 }
6317 spin_unlock(&nn->client_lock);
6318 nfsd_recall_delegations(&reaplist);
6319 return count;
6320}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006321#endif /* CONFIG_NFSD_FAULT_INJECTION */
6322
Meelap Shahc2f1a552007-07-17 04:04:39 -07006323/*
6324 * Since the lifetime of a delegation isn't limited to that of an open, a
6325 * client may quite reasonably hang on to a delegation as long as it has
6326 * the inode cached. This becomes an obvious problem the first time a
6327 * client's inode cache approaches the size of the server's total memory.
6328 *
6329 * For now we avoid this problem by imposing a hard limit on the number
6330 * of delegations, which varies according to the server's memory size.
6331 */
6332static void
6333set_max_delegations(void)
6334{
6335 /*
6336 * Allow at most 4 delegations per megabyte of RAM. Quick
6337 * estimates suggest that in the worst case (where every delegation
6338 * is for a different inode), a delegation could take about 1.5K,
6339 * giving a worst case usage of about 6% of memory.
6340 */
6341 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6342}
6343
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006344static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006345{
6346 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6347 int i;
6348
6349 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6350 CLIENT_HASH_SIZE, GFP_KERNEL);
6351 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006352 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006353 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6354 CLIENT_HASH_SIZE, GFP_KERNEL);
6355 if (!nn->unconf_id_hashtbl)
6356 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006357 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6358 SESSION_HASH_SIZE, GFP_KERNEL);
6359 if (!nn->sessionid_hashtbl)
6360 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006361
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006362 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006363 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006364 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006365 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006366 for (i = 0; i < SESSION_HASH_SIZE; i++)
6367 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006368 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006369 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03006370 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03006371 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006372 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03006373 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006374
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006375 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006376 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006377
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006378 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006379
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006380err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03006381 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006382err_unconf_id:
6383 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006384err:
6385 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006386}
6387
6388static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006389nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006390{
6391 int i;
6392 struct nfs4_client *clp = NULL;
6393 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6394
6395 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6396 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6397 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6398 destroy_client(clp);
6399 }
6400 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006401
Kinglong Mee2b905632014-03-26 22:09:30 +08006402 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6403 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6404 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6405 destroy_client(clp);
6406 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006407 }
6408
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006409 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006410 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006411 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006412 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006413}
6414
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006415int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006416nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006417{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006418 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006419 int ret;
6420
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006421 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006422 if (ret)
6423 return ret;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006424 nn->boot_time = get_seconds();
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006425 nn->grace_ended = false;
Jeff Laytond4318ac2014-09-12 16:40:21 -04006426 locks_start_grace(net, &nn->nfsd4_manager);
6427 nfsd4_client_tracking_init(net);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006428 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006429 nn->nfsd4_grace, net);
6430 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006431 return 0;
6432}
6433
6434/* initialization to perform when the nfsd service is started: */
6435
6436int
6437nfs4_state_start(void)
6438{
6439 int ret;
6440
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006441 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006442 if (ret)
6443 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07006444 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05006445 if (laundry_wq == NULL) {
6446 ret = -ENOMEM;
6447 goto out_recovery;
6448 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006449 ret = nfsd4_create_callback_queue();
6450 if (ret)
6451 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006452
Meelap Shahc2f1a552007-07-17 04:04:39 -07006453 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006454
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006455 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006456
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006457out_free_laundry:
6458 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006459out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006460 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461}
6462
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006463void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006464nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006468 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006470 cancel_delayed_work_sync(&nn->laundromat_work);
6471 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006472
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04006474 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006475 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006477 unhash_delegation_locked(dp);
6478 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479 }
Benny Halevycdc97502014-05-30 09:09:30 -04006480 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481 list_for_each_safe(pos, next, &reaplist) {
6482 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006483 list_del_init(&dp->dl_recall_lru);
Jeff Laytonafbda402014-08-09 10:22:41 -04006484 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04006485 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486 }
6487
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006488 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006489 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490}
6491
6492void
6493nfs4_state_shutdown(void)
6494{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006495 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006496 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006498
6499static void
6500get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6501{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006502 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6503 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006504}
6505
6506static void
6507put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6508{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006509 if (cstate->minorversion) {
6510 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6511 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6512 }
6513}
6514
6515void
6516clear_current_stateid(struct nfsd4_compound_state *cstate)
6517{
6518 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006519}
6520
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006521/*
6522 * functions to set current state id
6523 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006524void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006525nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6526{
6527 put_stateid(cstate, &odp->od_stateid);
6528}
6529
6530void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006531nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6532{
6533 put_stateid(cstate, &open->op_stateid);
6534}
6535
6536void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006537nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6538{
6539 put_stateid(cstate, &close->cl_stateid);
6540}
6541
6542void
6543nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6544{
6545 put_stateid(cstate, &lock->lk_resp_stateid);
6546}
6547
6548/*
6549 * functions to consume current state id
6550 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006551
6552void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006553nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6554{
6555 get_stateid(cstate, &odp->od_stateid);
6556}
6557
6558void
6559nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6560{
6561 get_stateid(cstate, &drp->dr_stateid);
6562}
6563
6564void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006565nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6566{
6567 get_stateid(cstate, &fsp->fr_stateid);
6568}
6569
6570void
6571nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6572{
6573 get_stateid(cstate, &setattr->sa_stateid);
6574}
6575
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006576void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006577nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6578{
6579 get_stateid(cstate, &close->cl_stateid);
6580}
6581
6582void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006583nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006584{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006585 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006586}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006587
6588void
6589nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6590{
6591 get_stateid(cstate, &read->rd_stateid);
6592}
6593
6594void
6595nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6596{
6597 get_stateid(cstate, &write->wr_stateid);
6598}