blob: 75faacb03e8e8770228c826715d18ea6be41c366 [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
Jeff Layton89876f82013-04-02 09:01:59 -0400401static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700402
Jeff Layton12659652014-07-10 14:07:28 -0400403static void
404__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400405{
Jeff Layton7214e862014-07-10 14:07:33 -0400406 lockdep_assert_held(&fp->fi_lock);
407
Jeff Layton12659652014-07-10 14:07:28 -0400408 if (access & NFS4_SHARE_ACCESS_WRITE)
409 atomic_inc(&fp->fi_access[O_WRONLY]);
410 if (access & NFS4_SHARE_ACCESS_READ)
411 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400412}
413
Jeff Layton12659652014-07-10 14:07:28 -0400414static __be32
415nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400416{
Jeff Layton7214e862014-07-10 14:07:33 -0400417 lockdep_assert_held(&fp->fi_lock);
418
Jeff Layton12659652014-07-10 14:07:28 -0400419 /* Does this access mode make sense? */
420 if (access & ~NFS4_SHARE_ACCESS_BOTH)
421 return nfserr_inval;
422
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400423 /* Does it conflict with a deny mode already set? */
424 if ((access & fp->fi_share_deny) != 0)
425 return nfserr_share_denied;
426
Jeff Layton12659652014-07-10 14:07:28 -0400427 __nfs4_file_get_access(fp, access);
428 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400429}
430
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400431static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
432{
433 /* Common case is that there is no deny mode. */
434 if (deny) {
435 /* Does this deny mode make sense? */
436 if (deny & ~NFS4_SHARE_DENY_BOTH)
437 return nfserr_inval;
438
439 if ((deny & NFS4_SHARE_DENY_READ) &&
440 atomic_read(&fp->fi_access[O_RDONLY]))
441 return nfserr_share_denied;
442
443 if ((deny & NFS4_SHARE_DENY_WRITE) &&
444 atomic_read(&fp->fi_access[O_WRONLY]))
445 return nfserr_share_denied;
446 }
447 return nfs_ok;
448}
449
J. Bruce Fields998db522010-08-07 09:21:41 -0400450static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400451{
Trond Myklebustde186432014-07-10 14:07:26 -0400452 might_lock(&fp->fi_lock);
453
454 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
455 struct file *f1 = NULL;
456 struct file *f2 = NULL;
457
Jeff Layton6d338b52014-07-10 14:07:29 -0400458 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400459 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400460 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400461 spin_unlock(&fp->fi_lock);
462 if (f1)
463 fput(f1);
464 if (f2)
465 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400466 }
467}
468
Jeff Layton12659652014-07-10 14:07:28 -0400469static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400470{
Jeff Layton12659652014-07-10 14:07:28 -0400471 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
472
473 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400474 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400475 if (access & NFS4_SHARE_ACCESS_READ)
476 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400477}
478
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200479struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
Trond Myklebust60116952014-07-29 21:34:06 -0400480 struct kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400481{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500482 struct nfs4_stid *stid;
483 int new_id;
484
Trond Myklebustf8338832014-07-25 07:34:19 -0400485 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500486 if (!stid)
487 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400488
Jeff Layton4770d722014-07-29 21:34:10 -0400489 idr_preload(GFP_KERNEL);
490 spin_lock(&cl->cl_lock);
491 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
492 spin_unlock(&cl->cl_lock);
493 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700494 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500495 goto out_free;
496 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500497 stid->sc_stateid.si_opaque.so_id = new_id;
498 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
499 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400500 atomic_set(&stid->sc_count, 1);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500501
J. Bruce Fields996e0932011-10-17 11:14:48 -0400502 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500503 * It shouldn't be a problem to reuse an opaque stateid value.
504 * I don't think it is for 4.1. But with 4.0 I worry that, for
505 * example, a stray write retransmission could be accepted by
506 * the server when it should have been rejected. Therefore,
507 * adopt a trick from the sctp code to attempt to maximize the
508 * amount of time until an id is reused, by ensuring they always
509 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400510 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500511 return stid;
512out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800513 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500514 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400515}
516
Jeff Laytonb49e0842014-07-29 21:34:11 -0400517static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400518{
Trond Myklebust60116952014-07-29 21:34:06 -0400519 struct nfs4_stid *stid;
520 struct nfs4_ol_stateid *stp;
521
522 stid = nfs4_alloc_stid(clp, stateid_slab);
523 if (!stid)
524 return NULL;
525
526 stp = openlockstateid(stid);
527 stp->st_stid.sc_free = nfs4_free_ol_stateid;
528 return stp;
529}
530
531static void nfs4_free_deleg(struct nfs4_stid *stid)
532{
Trond Myklebust60116952014-07-29 21:34:06 -0400533 kmem_cache_free(deleg_slab, stid);
534 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400535}
536
NeilBrown6282cd52014-06-04 17:39:26 +1000537/*
538 * When we recall a delegation, we should be careful not to hand it
539 * out again straight away.
540 * To ensure this we keep a pair of bloom filters ('new' and 'old')
541 * in which the filehandles of recalled delegations are "stored".
542 * If a filehandle appear in either filter, a delegation is blocked.
543 * When a delegation is recalled, the filehandle is stored in the "new"
544 * filter.
545 * Every 30 seconds we swap the filters and clear the "new" one,
546 * unless both are empty of course.
547 *
548 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
549 * low 3 bytes as hash-table indices.
550 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400551 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000552 * is used to manage concurrent access. Testing does not need the lock
553 * except when swapping the two filters.
554 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400555static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000556static struct bloom_pair {
557 int entries, old_entries;
558 time_t swap_time;
559 int new; /* index into 'set' */
560 DECLARE_BITMAP(set[2], 256);
561} blocked_delegations;
562
563static int delegation_blocked(struct knfsd_fh *fh)
564{
565 u32 hash;
566 struct bloom_pair *bd = &blocked_delegations;
567
568 if (bd->entries == 0)
569 return 0;
570 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400571 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000572 if (seconds_since_boot() - bd->swap_time > 30) {
573 bd->entries -= bd->old_entries;
574 bd->old_entries = bd->entries;
575 memset(bd->set[bd->new], 0,
576 sizeof(bd->set[0]));
577 bd->new = 1-bd->new;
578 bd->swap_time = seconds_since_boot();
579 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400580 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000581 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100582 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000583 if (test_bit(hash&255, bd->set[0]) &&
584 test_bit((hash>>8)&255, bd->set[0]) &&
585 test_bit((hash>>16)&255, bd->set[0]))
586 return 1;
587
588 if (test_bit(hash&255, bd->set[1]) &&
589 test_bit((hash>>8)&255, bd->set[1]) &&
590 test_bit((hash>>16)&255, bd->set[1]))
591 return 1;
592
593 return 0;
594}
595
596static void block_delegations(struct knfsd_fh *fh)
597{
598 u32 hash;
599 struct bloom_pair *bd = &blocked_delegations;
600
Daniel Borkmann87545892014-12-10 16:33:11 +0100601 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000602
Jeff Laytonf54fe962014-07-25 07:34:26 -0400603 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000604 __set_bit(hash&255, bd->set[bd->new]);
605 __set_bit((hash>>8)&255, bd->set[bd->new]);
606 __set_bit((hash>>16)&255, bd->set[bd->new]);
607 if (bd->entries == 0)
608 bd->swap_time = seconds_since_boot();
609 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400610 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000611}
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613static struct nfs4_delegation *
Jeff Laytonf9416e22014-07-25 07:34:23 -0400614alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400617 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400620 n = atomic_long_inc_return(&num_delegations);
621 if (n < 0 || n > max_delegations)
622 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000623 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400624 goto out_dec;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400625 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700626 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400627 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400628
629 dp->dl_stid.sc_free = nfs4_free_deleg;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400630 /*
631 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400632 * meaning of seqid 0 isn't meaningful, really, but let's avoid
633 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400634 */
635 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700636 INIT_LIST_HEAD(&dp->dl_perfile);
637 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400639 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200640 dp->dl_retries = 1;
641 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200642 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400644out_dec:
645 atomic_long_dec(&num_delegations);
646 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
649void
Trond Myklebust60116952014-07-29 21:34:06 -0400650nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Trond Myklebust11b91642014-07-29 21:34:08 -0400652 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400653 struct nfs4_client *clp = s->sc_client;
654
Jeff Layton4770d722014-07-29 21:34:10 -0400655 might_lock(&clp->cl_lock);
656
Jeff Laytonb401be222014-07-29 21:34:33 -0400657 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
658 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400659 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400660 }
Trond Myklebust60116952014-07-29 21:34:06 -0400661 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400662 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400663 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400664 if (fp)
665 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500668static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Jeff Layton6bcc0342014-08-09 10:22:40 -0400670 struct file *filp = NULL;
Jeff Layton417c6622014-07-21 09:34:57 -0400671
Jeff Layton6bcc0342014-08-09 10:22:40 -0400672 spin_lock(&fp->fi_lock);
Jeff Layton67db1032014-12-13 09:11:40 -0500673 if (fp->fi_deleg_file && --fp->fi_delegees == 0)
Jeff Layton6bcc0342014-08-09 10:22:40 -0400674 swap(filp, fp->fi_deleg_file);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400675 spin_unlock(&fp->fi_lock);
676
677 if (filp) {
Christoph Hellwig2ab99ee2015-01-21 19:14:02 +0100678 vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400679 fput(filp);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200683void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400684{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500685 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400686}
687
Benny Halevy931ee562014-05-30 09:09:27 -0400688static void
689hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
690{
Benny Halevycdc97502014-05-30 09:09:30 -0400691 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400692 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400693
Trond Myklebust67cb1272014-07-29 21:34:17 -0400694 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400695 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400696 list_add(&dp->dl_perfile, &fp->fi_delegations);
697 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
698}
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700static void
Jeff Layton42690672014-07-25 07:34:20 -0400701unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Trond Myklebust11b91642014-07-29 21:34:08 -0400703 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400704
Jeff Layton42690672014-07-25 07:34:20 -0400705 lockdep_assert_held(&state_lock);
706
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400707 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400708 /* Ensure that deleg break won't try to requeue it */
709 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400710 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400711 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400713 list_del_init(&dp->dl_perfile);
714 spin_unlock(&fp->fi_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400715}
716
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400717static void destroy_delegation(struct nfs4_delegation *dp)
718{
Jeff Layton42690672014-07-25 07:34:20 -0400719 spin_lock(&state_lock);
720 unhash_delegation_locked(dp);
721 spin_unlock(&state_lock);
Jeff Laytonafbda402014-08-09 10:22:41 -0400722 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -0400723 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400724}
725
726static void revoke_delegation(struct nfs4_delegation *dp)
727{
728 struct nfs4_client *clp = dp->dl_stid.sc_client;
729
Jeff Layton2d4a5322014-07-25 07:34:21 -0400730 WARN_ON(!list_empty(&dp->dl_recall_lru));
731
Jeff Laytonafbda402014-08-09 10:22:41 -0400732 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
733
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400734 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400735 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400736 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400737 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400738 spin_lock(&clp->cl_lock);
739 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
740 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400741 }
742}
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744/*
745 * SETCLIENTID state
746 */
747
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400748static unsigned int clientid_hashval(u32 id)
749{
750 return id & CLIENT_HASH_MASK;
751}
752
753static unsigned int clientstr_hashval(const char *name)
754{
755 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
756}
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400759 * We store the NONE, READ, WRITE, and BOTH bits separately in the
760 * st_{access,deny}_bmap field of the stateid, in order to track not
761 * only what share bits are currently in force, but also what
762 * combinations of share bits previous opens have used. This allows us
763 * to enforce the recommendation of rfc 3530 14.2.19 that the server
764 * return an error if the client attempt to downgrade to a combination
765 * of share bits not explicable by closing some of its previous opens.
766 *
767 * XXX: This enforcement is actually incomplete, since we don't keep
768 * track of access/deny bit combinations; so, e.g., we allow:
769 *
770 * OPEN allow read, deny write
771 * OPEN allow both, deny none
772 * DOWNGRADE allow read, deny none
773 *
774 * which we should reject.
775 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400776static unsigned int
777bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400778 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400779 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400780
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400781 for (i = 1; i < 4; i++) {
782 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400783 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400784 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400785 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400786}
787
Jeff Layton82c5ff12012-05-11 09:45:13 -0400788/* set share access for a given stateid */
789static inline void
790set_access(u32 access, struct nfs4_ol_stateid *stp)
791{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400792 unsigned char mask = 1 << access;
793
794 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
795 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400796}
797
798/* clear share access for a given stateid */
799static inline void
800clear_access(u32 access, struct nfs4_ol_stateid *stp)
801{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400802 unsigned char mask = 1 << access;
803
804 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
805 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400806}
807
808/* test whether a given stateid has access */
809static inline bool
810test_access(u32 access, struct nfs4_ol_stateid *stp)
811{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400812 unsigned char mask = 1 << access;
813
814 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400815}
816
Jeff Laytonce0fc432012-05-11 09:45:14 -0400817/* set share deny for a given stateid */
818static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400819set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400820{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400821 unsigned char mask = 1 << deny;
822
823 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
824 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400825}
826
827/* clear share deny for a given stateid */
828static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400829clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400830{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400831 unsigned char mask = 1 << deny;
832
833 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
834 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400835}
836
837/* test whether a given stateid is denying specific access */
838static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400839test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400840{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400841 unsigned char mask = 1 << deny;
842
843 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400844}
845
846static int nfs4_access_to_omode(u32 access)
847{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400848 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400849 case NFS4_SHARE_ACCESS_READ:
850 return O_RDONLY;
851 case NFS4_SHARE_ACCESS_WRITE:
852 return O_WRONLY;
853 case NFS4_SHARE_ACCESS_BOTH:
854 return O_RDWR;
855 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500856 WARN_ON_ONCE(1);
857 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400858}
859
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400860/*
861 * A stateid that had a deny mode associated with it is being released
862 * or downgraded. Recalculate the deny mode on the file.
863 */
864static void
865recalculate_deny_mode(struct nfs4_file *fp)
866{
867 struct nfs4_ol_stateid *stp;
868
869 spin_lock(&fp->fi_lock);
870 fp->fi_share_deny = 0;
871 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
872 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
873 spin_unlock(&fp->fi_lock);
874}
875
876static void
877reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
878{
879 int i;
880 bool change = false;
881
882 for (i = 1; i < 4; i++) {
883 if ((i & deny) != i) {
884 change = true;
885 clear_deny(i, stp);
886 }
887 }
888
889 /* Recalculate per-file deny mode if there was a change */
890 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -0400891 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400892}
893
Jeff Layton82c5ff12012-05-11 09:45:13 -0400894/* release all access and file references for a given stateid */
895static void
896release_all_access(struct nfs4_ol_stateid *stp)
897{
898 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -0400899 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400900
901 if (fp && stp->st_deny_bmap != 0)
902 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400903
904 for (i = 1; i < 4; i++) {
905 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -0400906 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400907 clear_access(i, stp);
908 }
909}
910
Jeff Layton6b180f02014-07-29 21:34:26 -0400911static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
912{
Jeff Laytona819ecc2014-07-29 21:34:38 -0400913 struct nfs4_client *clp = sop->so_client;
914
915 might_lock(&clp->cl_lock);
916
917 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -0400918 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -0400919 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -0400920 spin_unlock(&clp->cl_lock);
Jeff Layton6b180f02014-07-29 21:34:26 -0400921 kfree(sop->so_owner.data);
922 sop->so_ops->so_free(sop);
923}
924
Jeff Layton4ae098d2014-07-29 21:34:43 -0400925static void unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500926{
Trond Myklebust11b91642014-07-29 21:34:08 -0400927 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -0400928
Jeff Layton1c755dc2014-07-29 21:34:12 -0400929 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
930
Trond Myklebust1d31a252014-07-10 14:07:25 -0400931 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500932 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400933 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500934 list_del(&stp->st_perstateowner);
935}
936
Trond Myklebust60116952014-07-29 21:34:06 -0400937static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500938{
Trond Myklebust60116952014-07-29 21:34:06 -0400939 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400940
Trond Myklebust60116952014-07-29 21:34:06 -0400941 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -0400942 if (stp->st_stateowner)
943 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -0400944 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500945}
946
Jeff Laytonb49e0842014-07-29 21:34:11 -0400947static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500948{
Jeff Laytonb49e0842014-07-29 21:34:11 -0400949 struct nfs4_ol_stateid *stp = openlockstateid(stid);
950 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500951 struct file *file;
952
Jeff Laytonb49e0842014-07-29 21:34:11 -0400953 file = find_any_file(stp->st_stid.sc_file);
954 if (file)
955 filp_close(file, (fl_owner_t)lo);
956 nfs4_free_ol_stateid(stid);
957}
958
Jeff Layton2c41beb2014-07-29 21:34:41 -0400959/*
960 * Put the persistent reference to an already unhashed generic stateid, while
961 * holding the cl_lock. If it's the last reference, then put it onto the
962 * reaplist for later destruction.
963 */
964static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
965 struct list_head *reaplist)
966{
967 struct nfs4_stid *s = &stp->st_stid;
968 struct nfs4_client *clp = s->sc_client;
969
970 lockdep_assert_held(&clp->cl_lock);
971
972 WARN_ON_ONCE(!list_empty(&stp->st_locks));
973
974 if (!atomic_dec_and_test(&s->sc_count)) {
975 wake_up_all(&close_wq);
976 return;
977 }
978
979 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
980 list_add(&stp->st_locks, reaplist);
981}
982
Jeff Layton3c1c9952014-07-29 21:34:39 -0400983static void unhash_lock_stateid(struct nfs4_ol_stateid *stp)
984{
985 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
986
987 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
988
989 list_del_init(&stp->st_locks);
Jeff Layton4ae098d2014-07-29 21:34:43 -0400990 unhash_ol_stateid(stp);
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200991 nfs4_unhash_stid(&stp->st_stid);
Jeff Layton3c1c9952014-07-29 21:34:39 -0400992}
993
Jeff Layton5adfd882014-07-29 21:34:31 -0400994static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -0400995{
Jeff Layton1c755dc2014-07-29 21:34:12 -0400996 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
997
998 spin_lock(&oo->oo_owner.so_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -0400999 unhash_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001000 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04001001 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001002}
1003
Trond Myklebustc58c6612014-07-29 21:34:35 -04001004static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001005{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001006 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001007
Trond Myklebustd4f04892014-07-29 21:34:36 -04001008 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001009
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001010 list_del_init(&lo->lo_owner.so_strhash);
1011}
1012
Jeff Layton2c41beb2014-07-29 21:34:41 -04001013/*
1014 * Free a list of generic stateids that were collected earlier after being
1015 * fully unhashed.
1016 */
1017static void
1018free_ol_stateid_reaplist(struct list_head *reaplist)
1019{
1020 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001021 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001022
1023 might_sleep();
1024
1025 while (!list_empty(reaplist)) {
1026 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1027 st_locks);
1028 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001029 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001030 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001031 if (fp)
1032 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001033 }
1034}
1035
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001036static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001037{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001038 struct nfs4_client *clp = lo->lo_owner.so_client;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001039 struct nfs4_ol_stateid *stp;
1040 struct list_head reaplist;
1041
1042 INIT_LIST_HEAD(&reaplist);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001043
Trond Myklebustd4f04892014-07-29 21:34:36 -04001044 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001045 unhash_lockowner_locked(lo);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001046 while (!list_empty(&lo->lo_owner.so_stateids)) {
1047 stp = list_first_entry(&lo->lo_owner.so_stateids,
1048 struct nfs4_ol_stateid, st_perstateowner);
1049 unhash_lock_stateid(stp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001050 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001051 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001052 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001053 free_ol_stateid_reaplist(&reaplist);
Jeff Layton6b180f02014-07-29 21:34:26 -04001054 nfs4_put_stateowner(&lo->lo_owner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001055}
1056
Jeff Laytond83017f2014-07-29 21:34:42 -04001057static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1058 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001059{
1060 struct nfs4_ol_stateid *stp;
1061
1062 while (!list_empty(&open_stp->st_locks)) {
1063 stp = list_entry(open_stp->st_locks.next,
1064 struct nfs4_ol_stateid, st_locks);
Jeff Laytond83017f2014-07-29 21:34:42 -04001065 unhash_lock_stateid(stp);
1066 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001067 }
1068}
1069
Jeff Laytond83017f2014-07-29 21:34:42 -04001070static void unhash_open_stateid(struct nfs4_ol_stateid *stp,
1071 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001072{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001073 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1074
Jeff Layton4ae098d2014-07-29 21:34:43 -04001075 unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001076 release_open_stateid_locks(stp, reaplist);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001077}
1078
1079static void release_open_stateid(struct nfs4_ol_stateid *stp)
1080{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001081 LIST_HEAD(reaplist);
1082
1083 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04001084 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001085 put_ol_stateid_locked(stp, &reaplist);
1086 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1087 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001088}
1089
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001090static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001091{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001092 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001093
Trond Myklebustd4f04892014-07-29 21:34:36 -04001094 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001095
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001096 list_del_init(&oo->oo_owner.so_strhash);
1097 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001098}
1099
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001100static void release_last_closed_stateid(struct nfs4_openowner *oo)
1101{
Jeff Layton217526e2014-07-30 08:27:11 -04001102 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1103 nfsd_net_id);
1104 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001105
Jeff Layton217526e2014-07-30 08:27:11 -04001106 spin_lock(&nn->client_lock);
1107 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001108 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001109 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001110 oo->oo_last_closed_stid = NULL;
1111 }
Jeff Layton217526e2014-07-30 08:27:11 -04001112 spin_unlock(&nn->client_lock);
1113 if (s)
1114 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001115}
1116
Jeff Layton2c41beb2014-07-29 21:34:41 -04001117static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001118{
1119 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001120 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001121 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001122
Jeff Layton2c41beb2014-07-29 21:34:41 -04001123 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001124
Trond Myklebustd4f04892014-07-29 21:34:36 -04001125 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001126 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001127 while (!list_empty(&oo->oo_owner.so_stateids)) {
1128 stp = list_first_entry(&oo->oo_owner.so_stateids,
1129 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytond83017f2014-07-29 21:34:42 -04001130 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001131 put_ol_stateid_locked(stp, &reaplist);
1132 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001133 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001134 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001135 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001136 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001137}
1138
Marc Eshel5282fd72009-04-03 08:27:52 +03001139static inline int
1140hash_sessionid(struct nfs4_sessionid *sessionid)
1141{
1142 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1143
1144 return sid->sequence % SESSION_HASH_SIZE;
1145}
1146
Trond Myklebust8f199b82012-03-20 15:11:17 -04001147#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001148static inline void
1149dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1150{
1151 u32 *ptr = (u32 *)(&sessionid->data[0]);
1152 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1153}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001154#else
1155static inline void
1156dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1157{
1158}
1159#endif
1160
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001161/*
1162 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1163 * won't be used for replay.
1164 */
1165void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1166{
1167 struct nfs4_stateowner *so = cstate->replay_owner;
1168
1169 if (nfserr == nfserr_replay_me)
1170 return;
1171
1172 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001173 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001174 return;
1175 }
1176 if (!so)
1177 return;
1178 if (so->so_is_open_owner)
1179 release_last_closed_stateid(openowner(so));
1180 so->so_seqid++;
1181 return;
1182}
Marc Eshel5282fd72009-04-03 08:27:52 +03001183
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001184static void
1185gen_sessionid(struct nfsd4_session *ses)
1186{
1187 struct nfs4_client *clp = ses->se_client;
1188 struct nfsd4_sessionid *sid;
1189
1190 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1191 sid->clientid = clp->cl_clientid;
1192 sid->sequence = current_sessionid++;
1193 sid->reserved = 0;
1194}
1195
1196/*
Andy Adamsona6496372009-08-28 08:45:01 -04001197 * The protocol defines ca_maxresponssize_cached to include the size of
1198 * the rpc header, but all we need to cache is the data starting after
1199 * the end of the initial SEQUENCE operation--the rest we regenerate
1200 * each time. Therefore we can advertise a ca_maxresponssize_cached
1201 * value that is the number of bytes in our cache plus a few additional
1202 * bytes. In order to stay on the safe side, and not promise more than
1203 * we can cache, those additional bytes must be the minimum possible: 24
1204 * bytes of rpc header (xid through accept state, with AUTH_NULL
1205 * verifier), 12 for the compound header (with zero-length tag), and 44
1206 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001207 */
Andy Adamsona6496372009-08-28 08:45:01 -04001208#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1209
Andy Adamson557ce262009-08-28 08:45:04 -04001210static void
1211free_session_slots(struct nfsd4_session *ses)
1212{
1213 int i;
1214
1215 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1216 kfree(ses->se_slots[i]);
1217}
1218
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001219/*
1220 * We don't actually need to cache the rpc and session headers, so we
1221 * can allocate a little less for each slot:
1222 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001223static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001224{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001225 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001226
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001227 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1228 size = 0;
1229 else
1230 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1231 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001232}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001233
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001234/*
1235 * XXX: If we run out of reserved DRC memory we could (up to a point)
1236 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001237 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001238 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001239static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001240{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001241 u32 slotsize = slot_bytes(ca);
1242 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001243 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001244
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001245 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001246 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1247 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001248 num = min_t(int, num, avail / slotsize);
1249 nfsd_drc_mem_used += num * slotsize;
1250 spin_unlock(&nfsd_drc_lock);
1251
1252 return num;
1253}
1254
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001255static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001256{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001257 int slotsize = slot_bytes(ca);
1258
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001259 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001260 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001261 spin_unlock(&nfsd_drc_lock);
1262}
1263
Kinglong Mee60810e52014-01-01 00:35:47 +08001264static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1265 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001266{
Kinglong Mee60810e52014-01-01 00:35:47 +08001267 int numslots = fattrs->maxreqs;
1268 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001269 struct nfsd4_session *new;
1270 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001271
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001272 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001273 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1274 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001275
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001276 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001277 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001278 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001279 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001280 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001281 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001282 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001283 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001284 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001285
1286 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1287 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1288
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001289 return new;
1290out_free:
1291 while (i--)
1292 kfree(new->se_slots[i]);
1293 kfree(new);
1294 return NULL;
1295}
1296
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001297static void free_conn(struct nfsd4_conn *c)
1298{
1299 svc_xprt_put(c->cn_xprt);
1300 kfree(c);
1301}
1302
1303static void nfsd4_conn_lost(struct svc_xpt_user *u)
1304{
1305 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1306 struct nfs4_client *clp = c->cn_session->se_client;
1307
1308 spin_lock(&clp->cl_lock);
1309 if (!list_empty(&c->cn_persession)) {
1310 list_del(&c->cn_persession);
1311 free_conn(c);
1312 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001313 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001314 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001315}
1316
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001317static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001318{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001319 struct nfsd4_conn *conn;
1320
1321 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1322 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001323 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001324 svc_xprt_get(rqstp->rq_xprt);
1325 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001326 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001327 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1328 return conn;
1329}
1330
J. Bruce Fields328ead22010-09-29 16:11:06 -04001331static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1332{
1333 conn->cn_session = ses;
1334 list_add(&conn->cn_persession, &ses->se_conns);
1335}
1336
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001337static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1338{
1339 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001340
1341 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001342 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001343 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001344}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001345
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001346static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001347{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001348 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001349 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001350}
1351
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001352static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001353{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001354 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001355
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001356 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001357 ret = nfsd4_register_conn(conn);
1358 if (ret)
1359 /* oops; xprt is already down: */
1360 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001361 /* We may have gained or lost a callback channel: */
1362 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001363}
1364
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001365static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001366{
1367 u32 dir = NFS4_CDFC4_FORE;
1368
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001369 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001370 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001371 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001372}
1373
1374/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001375static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001376{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001377 struct nfs4_client *clp = s->se_client;
1378 struct nfsd4_conn *c;
1379
1380 spin_lock(&clp->cl_lock);
1381 while (!list_empty(&s->se_conns)) {
1382 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1383 list_del_init(&c->cn_persession);
1384 spin_unlock(&clp->cl_lock);
1385
1386 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1387 free_conn(c);
1388
1389 spin_lock(&clp->cl_lock);
1390 }
1391 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001392}
1393
J. Bruce Fields1377b692012-09-11 21:42:40 -04001394static void __free_session(struct nfsd4_session *ses)
1395{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001396 free_session_slots(ses);
1397 kfree(ses);
1398}
1399
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001400static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001401{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001402 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001403 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001404 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001405}
Andy Adamson557ce262009-08-28 08:45:04 -04001406
Fengguang Wu135ae822012-11-10 07:20:25 -05001407static 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 -04001408{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001409 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001410 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001411
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001412 new->se_client = clp;
1413 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001414
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001415 INIT_LIST_HEAD(&new->se_conns);
1416
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001417 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001418 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001419 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001420 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001421 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001422 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001423 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001424 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001425 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001426 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001427
Chuck Leverb0d2e422014-08-22 15:10:59 -04001428 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001429 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001430 /*
1431 * This is a little silly; with sessions there's no real
1432 * use for the callback address. Use the peer address
1433 * as a reasonable default for now, but consider fixing
1434 * the rpc client not to require an address in the
1435 * future:
1436 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001437 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1438 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001439 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001440}
1441
Benny Halevy9089f1b2010-05-12 00:12:26 +03001442/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001443static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001444__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001445{
1446 struct nfsd4_session *elem;
1447 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001448 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001449
Trond Myklebust0a880a22014-07-30 08:27:10 -04001450 lockdep_assert_held(&nn->client_lock);
1451
Marc Eshel5282fd72009-04-03 08:27:52 +03001452 dump_sessionid(__func__, sessionid);
1453 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001454 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001455 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001456 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1457 NFS4_MAX_SESSIONID_LEN)) {
1458 return elem;
1459 }
1460 }
1461
1462 dprintk("%s: session not found\n", __func__);
1463 return NULL;
1464}
1465
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001466static struct nfsd4_session *
1467find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1468 __be32 *ret)
1469{
1470 struct nfsd4_session *session;
1471 __be32 status = nfserr_badsession;
1472
1473 session = __find_in_sessionid_hashtbl(sessionid, net);
1474 if (!session)
1475 goto out;
1476 status = nfsd4_get_session_locked(session);
1477 if (status)
1478 session = NULL;
1479out:
1480 *ret = status;
1481 return session;
1482}
1483
Benny Halevy9089f1b2010-05-12 00:12:26 +03001484/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001485static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001486unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001487{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001488 struct nfs4_client *clp = ses->se_client;
1489 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1490
1491 lockdep_assert_held(&nn->client_lock);
1492
Andy Adamson7116ed62009-04-03 08:27:43 +03001493 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001494 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001495 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001496 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001497}
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1500static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001501STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001503 /*
1504 * We're assuming the clid was not given out from a boot
1505 * precisely 2^32 (about 136 years) before this one. That seems
1506 * a safe assumption:
1507 */
1508 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001510 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001511 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return 1;
1513}
1514
1515/*
1516 * XXX Should we use a slab cache ?
1517 * This type of memory management is somewhat inefficient, but we use it
1518 * anyway since SETCLIENTID is not a common operation.
1519 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001520static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001523 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001525 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1526 if (clp == NULL)
1527 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001528 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001529 if (clp->cl_name.data == NULL)
1530 goto err_no_name;
1531 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1532 OWNER_HASH_SIZE, GFP_KERNEL);
1533 if (!clp->cl_ownerstr_hashtbl)
1534 goto err_no_hashtbl;
1535 for (i = 0; i < OWNER_HASH_SIZE; i++)
1536 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001537 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001538 INIT_LIST_HEAD(&clp->cl_sessions);
1539 idr_init(&clp->cl_stateids);
1540 atomic_set(&clp->cl_refcount, 0);
1541 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1542 INIT_LIST_HEAD(&clp->cl_idhash);
1543 INIT_LIST_HEAD(&clp->cl_openowners);
1544 INIT_LIST_HEAD(&clp->cl_delegations);
1545 INIT_LIST_HEAD(&clp->cl_lru);
1546 INIT_LIST_HEAD(&clp->cl_callbacks);
1547 INIT_LIST_HEAD(&clp->cl_revoked);
1548 spin_lock_init(&clp->cl_lock);
1549 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001551err_no_hashtbl:
1552 kfree(clp->cl_name.data);
1553err_no_name:
1554 kfree(clp);
1555 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001558static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559free_client(struct nfs4_client *clp)
1560{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001561 while (!list_empty(&clp->cl_sessions)) {
1562 struct nfsd4_session *ses;
1563 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1564 se_perclnt);
1565 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001566 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1567 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001568 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001569 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001570 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001571 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001573 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 kfree(clp);
1575}
1576
Benny Halevy84d38ac2010-05-12 00:13:16 +03001577/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001578static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001579unhash_client_locked(struct nfs4_client *clp)
1580{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001581 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001582 struct nfsd4_session *ses;
1583
Trond Myklebust0a880a22014-07-30 08:27:10 -04001584 lockdep_assert_held(&nn->client_lock);
1585
Trond Myklebust4beb3452014-07-30 08:27:02 -04001586 /* Mark the client as expired! */
1587 clp->cl_time = 0;
1588 /* Make it invisible */
1589 if (!list_empty(&clp->cl_idhash)) {
1590 list_del_init(&clp->cl_idhash);
1591 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1592 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1593 else
1594 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1595 }
1596 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001597 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001598 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1599 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001600 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001601}
1602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001604unhash_client(struct nfs4_client *clp)
1605{
1606 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1607
1608 spin_lock(&nn->client_lock);
1609 unhash_client_locked(clp);
1610 spin_unlock(&nn->client_lock);
1611}
1612
Jeff Layton97403d92014-07-30 08:27:12 -04001613static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1614{
1615 if (atomic_read(&clp->cl_refcount))
1616 return nfserr_jukebox;
1617 unhash_client_locked(clp);
1618 return nfs_ok;
1619}
1620
Trond Myklebust4beb3452014-07-30 08:27:02 -04001621static void
1622__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001624 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 struct list_head reaplist;
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001629 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001630 while (!list_empty(&clp->cl_delegations)) {
1631 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001632 unhash_delegation_locked(dp);
1633 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
Benny Halevycdc97502014-05-30 09:09:30 -04001635 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 while (!list_empty(&reaplist)) {
1637 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001638 list_del_init(&dp->dl_recall_lru);
Jeff Laytonafbda402014-08-09 10:22:41 -04001639 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04001640 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001642 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001643 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001644 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001645 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001646 }
NeilBrownea1da632005-06-23 22:04:17 -07001647 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001648 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04001649 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001650 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001652 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001653 if (clp->cl_cb_conn.cb_xprt)
1654 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001655 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
Trond Myklebust4beb3452014-07-30 08:27:02 -04001658static void
1659destroy_client(struct nfs4_client *clp)
1660{
1661 unhash_client(clp);
1662 __destroy_client(clp);
1663}
1664
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001665static void expire_client(struct nfs4_client *clp)
1666{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001667 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001668 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001669 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001670}
1671
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001672static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1673{
1674 memcpy(target->cl_verifier.data, source->data,
1675 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676}
1677
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001678static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1679{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1681 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1682}
1683
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001684static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001685{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001686 if (source->cr_principal) {
1687 target->cr_principal =
1688 kstrdup(source->cr_principal, GFP_KERNEL);
1689 if (target->cr_principal == NULL)
1690 return -ENOMEM;
1691 } else
1692 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001693 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 target->cr_uid = source->cr_uid;
1695 target->cr_gid = source->cr_gid;
1696 target->cr_group_info = source->cr_group_info;
1697 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001698 target->cr_gss_mech = source->cr_gss_mech;
1699 if (source->cr_gss_mech)
1700 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001701 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702}
1703
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001704static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001705compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1706{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001707 if (o1->len < o2->len)
1708 return -1;
1709 if (o1->len > o2->len)
1710 return 1;
1711 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001712}
1713
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001714static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001715{
NeilBrowna55370a2005-06-23 22:03:52 -07001716 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
1718
1719static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001720same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1721{
1722 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723}
1724
1725static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001726same_clid(clientid_t *cl1, clientid_t *cl2)
1727{
1728 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729}
1730
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001731static bool groups_equal(struct group_info *g1, struct group_info *g2)
1732{
1733 int i;
1734
1735 if (g1->ngroups != g2->ngroups)
1736 return false;
1737 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001738 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001739 return false;
1740 return true;
1741}
1742
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001743/*
1744 * RFC 3530 language requires clid_inuse be returned when the
1745 * "principal" associated with a requests differs from that previously
1746 * used. We use uid, gid's, and gss principal string as our best
1747 * approximation. We also don't want to allow non-gss use of a client
1748 * established using gss: in theory cr_principal should catch that
1749 * change, but in practice cr_principal can be null even in the gss case
1750 * since gssd doesn't always pass down a principal string.
1751 */
1752static bool is_gss_cred(struct svc_cred *cr)
1753{
1754 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1755 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1756}
1757
1758
Vivek Trivedi5559b502012-07-24 21:18:20 +05301759static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001760same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1761{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001762 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001763 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1764 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001765 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1766 return false;
1767 if (cr1->cr_principal == cr2->cr_principal)
1768 return true;
1769 if (!cr1->cr_principal || !cr2->cr_principal)
1770 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301771 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772}
1773
J. Bruce Fields57266a62013-04-13 14:27:29 -04001774static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1775{
1776 struct svc_cred *cr = &rqstp->rq_cred;
1777 u32 service;
1778
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001779 if (!cr->cr_gss_mech)
1780 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001781 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1782 return service == RPC_GSS_SVC_INTEGRITY ||
1783 service == RPC_GSS_SVC_PRIVACY;
1784}
1785
1786static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1787{
1788 struct svc_cred *cr = &rqstp->rq_cred;
1789
1790 if (!cl->cl_mach_cred)
1791 return true;
1792 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1793 return false;
1794 if (!svc_rqst_integrity_protected(rqstp))
1795 return false;
1796 if (!cr->cr_principal)
1797 return false;
1798 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1799}
1800
Jeff Layton294ac322014-07-30 08:27:15 -04001801static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001802{
Chuck Leverab4684d2012-03-02 17:13:50 -05001803 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Jeff Laytonf4199922014-06-17 07:44:11 -04001805 /*
1806 * This is opaque to client, so no need to byte-swap. Use
1807 * __force to keep sparse happy
1808 */
1809 verf[0] = (__force __be32)get_seconds();
Jeff Layton294ac322014-07-30 08:27:15 -04001810 verf[1] = (__force __be32)nn->clientid_counter;
Chuck Leverab4684d2012-03-02 17:13:50 -05001811 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812}
1813
Jeff Layton294ac322014-07-30 08:27:15 -04001814static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1815{
1816 clp->cl_clientid.cl_boot = nn->boot_time;
1817 clp->cl_clientid.cl_id = nn->clientid_counter++;
1818 gen_confirm(clp, nn);
1819}
1820
Jeff Layton4770d722014-07-29 21:34:10 -04001821static struct nfs4_stid *
1822find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001823{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001824 struct nfs4_stid *ret;
1825
1826 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1827 if (!ret || !ret->sc_type)
1828 return NULL;
1829 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001830}
1831
Jeff Layton4770d722014-07-29 21:34:10 -04001832static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001833find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001834{
1835 struct nfs4_stid *s;
1836
Jeff Layton4770d722014-07-29 21:34:10 -04001837 spin_lock(&cl->cl_lock);
1838 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001839 if (s != NULL) {
1840 if (typemask & s->sc_type)
1841 atomic_inc(&s->sc_count);
1842 else
1843 s = NULL;
1844 }
Jeff Layton4770d722014-07-29 21:34:10 -04001845 spin_unlock(&cl->cl_lock);
1846 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001847}
1848
Jeff Layton2216d442012-11-12 15:00:57 -05001849static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001850 struct svc_rqst *rqstp, nfs4_verifier *verf)
1851{
1852 struct nfs4_client *clp;
1853 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001854 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001855 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001856
1857 clp = alloc_client(name);
1858 if (clp == NULL)
1859 return NULL;
1860
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001861 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1862 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001863 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001864 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001865 }
Christoph Hellwig0162ac22014-09-24 12:19:19 +02001866 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Benny Halevy07cd4902010-05-12 00:13:41 +03001867 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001868 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001869 copy_verf(clp, verf);
1870 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001871 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001872 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001873 return clp;
1874}
1875
NeilBrownfd39ca92005-06-23 22:04:03 -07001876static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001877add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1878{
1879 struct rb_node **new = &(root->rb_node), *parent = NULL;
1880 struct nfs4_client *clp;
1881
1882 while (*new) {
1883 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1884 parent = *new;
1885
1886 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1887 new = &((*new)->rb_left);
1888 else
1889 new = &((*new)->rb_right);
1890 }
1891
1892 rb_link_node(&new_clp->cl_namenode, parent, new);
1893 rb_insert_color(&new_clp->cl_namenode, root);
1894}
1895
1896static struct nfs4_client *
1897find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1898{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001899 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001900 struct rb_node *node = root->rb_node;
1901 struct nfs4_client *clp;
1902
1903 while (node) {
1904 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1905 cmp = compare_blob(&clp->cl_name, name);
1906 if (cmp > 0)
1907 node = node->rb_left;
1908 else if (cmp < 0)
1909 node = node->rb_right;
1910 else
1911 return clp;
1912 }
1913 return NULL;
1914}
1915
1916static void
1917add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
1919 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001920 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Trond Myklebust0a880a22014-07-30 08:27:10 -04001922 lockdep_assert_held(&nn->client_lock);
1923
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001924 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001925 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001927 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001928 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
NeilBrownfd39ca92005-06-23 22:04:03 -07001931static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932move_to_confirmed(struct nfs4_client *clp)
1933{
1934 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001935 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Trond Myklebust0a880a22014-07-30 08:27:10 -04001937 lockdep_assert_held(&nn->client_lock);
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001940 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001941 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001942 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001943 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001944 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945}
1946
1947static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001948find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
1950 struct nfs4_client *clp;
1951 unsigned int idhashval = clientid_hashval(clid->cl_id);
1952
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001953 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001954 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001955 if ((bool)clp->cl_minorversion != sessions)
1956 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001957 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
1961 return NULL;
1962}
1963
1964static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001965find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1966{
1967 struct list_head *tbl = nn->conf_id_hashtbl;
1968
Trond Myklebust0a880a22014-07-30 08:27:10 -04001969 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001970 return find_client_in_id_table(tbl, clid, sessions);
1971}
1972
1973static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001974find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001976 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Trond Myklebust0a880a22014-07-30 08:27:10 -04001978 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001979 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980}
1981
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001982static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001983{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001984 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001985}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001986
NeilBrown28ce6052005-06-23 22:03:56 -07001987static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001988find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001989{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001990 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001991 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001992}
1993
1994static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001995find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001996{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001997 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001998 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001999}
2000
NeilBrownfd39ca92005-06-23 22:04:03 -07002001static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002002gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002004 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002005 struct sockaddr *sa = svc_addr(rqstp);
2006 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002007 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Jeff Layton7077ecb2009-08-14 12:57:58 -04002009 /* Currently, we only support tcp and tcp6 for the callback channel */
2010 if (se->se_callback_netid_len == 3 &&
2011 !memcmp(se->se_callback_netid_val, "tcp", 3))
2012 expected_family = AF_INET;
2013 else if (se->se_callback_netid_len == 4 &&
2014 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2015 expected_family = AF_INET6;
2016 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 goto out_err;
2018
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002019 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002020 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002021 (struct sockaddr *)&conn->cb_addr,
2022 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002023
J. Bruce Fields07263f12010-05-31 19:09:40 -04002024 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002026
J. Bruce Fields07263f12010-05-31 19:09:40 -04002027 if (conn->cb_addr.ss_family == AF_INET6)
2028 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002029
J. Bruce Fields07263f12010-05-31 19:09:40 -04002030 conn->cb_prog = se->se_callback_prog;
2031 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002032 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 return;
2034out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002035 conn->cb_addr.ss_family = AF_UNSPEC;
2036 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002037 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 "will not receive delegations\n",
2039 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 return;
2042}
2043
Andy Adamson074fe892009-04-03 08:28:15 +03002044/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002045 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002046 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002047static void
Andy Adamson074fe892009-04-03 08:28:15 +03002048nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2049{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002050 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002051 struct nfsd4_slot *slot = resp->cstate.slot;
2052 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002053
Andy Adamson557ce262009-08-28 08:45:04 -04002054 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002055
Andy Adamson557ce262009-08-28 08:45:04 -04002056 slot->sl_opcnt = resp->opcnt;
2057 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002058
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002059 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002060 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002061 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002062 return;
2063 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002064 base = resp->cstate.data_offset;
2065 slot->sl_datalen = buf->len - base;
2066 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04002067 WARN("%s: sessions DRC could not cache compound\n", __func__);
2068 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002069}
2070
2071/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002072 * Encode the replay sequence operation from the slot values.
2073 * If cachethis is FALSE encode the uncached rep error on the next
2074 * operation which sets resp->p and increments resp->opcnt for
2075 * nfs4svc_encode_compoundres.
2076 *
Andy Adamson074fe892009-04-03 08:28:15 +03002077 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002078static __be32
2079nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2080 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002081{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002082 struct nfsd4_op *op;
2083 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002084
Andy Adamsonabfabf82009-07-23 19:02:18 -04002085 /* Encode the replayed sequence operation */
2086 op = &args->ops[resp->opcnt - 1];
2087 nfsd4_encode_operation(resp, op);
2088
2089 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002090 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002091 op = &args->ops[resp->opcnt++];
2092 op->status = nfserr_retry_uncached_rep;
2093 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002094 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002095 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002096}
2097
2098/*
Andy Adamson557ce262009-08-28 08:45:04 -04002099 * The sequence operation is not cached because we can use the slot and
2100 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002101 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002102static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002103nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2104 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002105{
Andy Adamson557ce262009-08-28 08:45:04 -04002106 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002107 struct xdr_stream *xdr = &resp->xdr;
2108 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002109 __be32 status;
2110
Andy Adamson557ce262009-08-28 08:45:04 -04002111 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002112
Andy Adamsonabfabf82009-07-23 19:02:18 -04002113 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002114 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002115 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002116
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002117 p = xdr_reserve_space(xdr, slot->sl_datalen);
2118 if (!p) {
2119 WARN_ON_ONCE(1);
2120 return nfserr_serverfault;
2121 }
2122 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2123 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002124
Andy Adamson557ce262009-08-28 08:45:04 -04002125 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002126 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002127}
2128
Andy Adamson0733d212009-04-03 08:28:01 +03002129/*
2130 * Set the exchange_id flags returned by the server.
2131 */
2132static void
2133nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2134{
2135 /* pNFS is not supported */
2136 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2137
2138 /* Referrals are supported, Migration is not. */
2139 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2140
2141 /* set the wire flags to return to client. */
2142 clid->flags = new->cl_exchange_flags;
2143}
2144
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002145static bool client_has_state(struct nfs4_client *clp)
2146{
2147 /*
2148 * Note clp->cl_openowners check isn't quite right: there's no
2149 * need to count owners without stateid's.
2150 *
2151 * Also note we should probably be using this in 4.0 case too.
2152 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002153 return !list_empty(&clp->cl_openowners)
2154 || !list_empty(&clp->cl_delegations)
2155 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002156}
2157
Al Virob37ad282006-10-19 23:28:59 -07002158__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002159nfsd4_exchange_id(struct svc_rqst *rqstp,
2160 struct nfsd4_compound_state *cstate,
2161 struct nfsd4_exchange_id *exid)
2162{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002163 struct nfs4_client *conf, *new;
2164 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002165 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002166 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002167 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002168 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002169 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002170 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002171
Jeff Layton363168b2009-08-14 12:57:56 -04002172 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002173 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002174 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002175 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002176 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002177
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002178 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002179 return nfserr_inval;
2180
Andy Adamson0733d212009-04-03 08:28:01 +03002181 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002182 case SP4_MACH_CRED:
2183 if (!svc_rqst_integrity_protected(rqstp))
2184 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002185 case SP4_NONE:
2186 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002187 default: /* checked by xdr code */
2188 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002189 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002190 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002191 }
2192
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002193 new = create_client(exid->clname, rqstp, &verf);
2194 if (new == NULL)
2195 return nfserr_jukebox;
2196
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002197 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002198 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002199 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002200 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002201 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2202 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2203
J. Bruce Fields136e6582012-05-12 20:37:23 -04002204 if (update) {
2205 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002206 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002207 goto out;
2208 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002209 if (!mach_creds_match(conf, rqstp)) {
2210 status = nfserr_wrong_cred;
2211 goto out;
2212 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002213 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002214 status = nfserr_perm;
2215 goto out;
2216 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002217 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002218 status = nfserr_not_same;
2219 goto out;
2220 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002221 /* case 6 */
2222 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002223 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002224 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002225 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002226 if (client_has_state(conf)) {
2227 status = nfserr_clid_inuse;
2228 goto out;
2229 }
Andy Adamson0733d212009-04-03 08:28:01 +03002230 goto out_new;
2231 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002232 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002233 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002234 goto out_copy;
2235 }
2236 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002237 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002238 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002239 }
2240
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002241 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002242 status = nfserr_noent;
2243 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002244 }
2245
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002246 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002247 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002248 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002249
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002250 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002251out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002252 if (conf) {
2253 status = mark_client_expired_locked(conf);
2254 if (status)
2255 goto out;
2256 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002257 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002258 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002259
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002260 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002261 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002262 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002263out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002264 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2265 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002266
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002267 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2268 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002269
2270 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002271 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002272 status = nfs_ok;
2273
2274out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002275 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002276 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002277 expire_client(new);
2278 if (unconf)
2279 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002280 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002281}
2282
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002283static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002284check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002285{
Andy Adamson88e588d2009-07-23 19:02:15 -04002286 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2287 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002288
2289 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002290 if (slot_inuse) {
2291 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002292 return nfserr_jukebox;
2293 else
2294 return nfserr_seq_misordered;
2295 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002296 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002297 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002298 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002299 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002300 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002301 return nfserr_seq_misordered;
2302}
2303
Andy Adamson49557cc2009-07-23 19:02:16 -04002304/*
2305 * Cache the create session result into the create session single DRC
2306 * slot cache by saving the xdr structure. sl_seqid has been set.
2307 * Do this for solo or embedded create session operations.
2308 */
2309static void
2310nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002311 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002312{
2313 slot->sl_status = nfserr;
2314 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2315}
2316
2317static __be32
2318nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2319 struct nfsd4_clid_slot *slot)
2320{
2321 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2322 return slot->sl_status;
2323}
2324
Mi Jinlong1b74c252011-07-14 14:50:17 +08002325#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2326 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2327 1 + /* MIN tag is length with zero, only length */ \
2328 3 + /* version, opcount, opcode */ \
2329 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2330 /* seqid, slotID, slotID, cache */ \
2331 4 ) * sizeof(__be32))
2332
2333#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2334 2 + /* verifier: AUTH_NULL, length 0 */\
2335 1 + /* status */ \
2336 1 + /* MIN tag is length with zero, only length */ \
2337 3 + /* opcount, opcode, opstatus*/ \
2338 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2339 /* seqid, slotID, slotID, slotID, status */ \
2340 5 ) * sizeof(__be32))
2341
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002342static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002343{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002344 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2345
J. Bruce Fields373cd402013-04-08 15:42:12 -04002346 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2347 return nfserr_toosmall;
2348 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2349 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002350 ca->headerpadsz = 0;
2351 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2352 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2353 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2354 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2355 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2356 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2357 /*
2358 * Note decreasing slot size below client's request may make it
2359 * difficult for client to function correctly, whereas
2360 * decreasing the number of slots will (just?) affect
2361 * performance. When short on memory we therefore prefer to
2362 * decrease number of slots instead of their size. Clients that
2363 * request larger slots than they need will get poor results:
2364 */
2365 ca->maxreqs = nfsd4_get_drc_mem(ca);
2366 if (!ca->maxreqs)
2367 return nfserr_jukebox;
2368
J. Bruce Fields373cd402013-04-08 15:42:12 -04002369 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002370}
2371
Kinglong Mee8a891632013-12-23 18:11:02 +08002372#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2373 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2374#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2375 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2376
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002377static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2378{
2379 ca->headerpadsz = 0;
2380
2381 /*
2382 * These RPC_MAX_HEADER macros are overkill, especially since we
2383 * don't even do gss on the backchannel yet. But this is still
2384 * less than 1k. Tighten up this estimate in the unlikely event
2385 * it turns out to be a problem for some client:
2386 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002387 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002388 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002389 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002390 return nfserr_toosmall;
2391 ca->maxresp_cached = 0;
2392 if (ca->maxops < 2)
2393 return nfserr_toosmall;
2394
2395 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002396}
2397
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002398static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2399{
2400 switch (cbs->flavor) {
2401 case RPC_AUTH_NULL:
2402 case RPC_AUTH_UNIX:
2403 return nfs_ok;
2404 default:
2405 /*
2406 * GSS case: the spec doesn't allow us to return this
2407 * error. But it also doesn't allow us not to support
2408 * GSS.
2409 * I'd rather this fail hard than return some error the
2410 * client might think it can already handle:
2411 */
2412 return nfserr_encr_alg_unsupp;
2413 }
2414}
2415
Andy Adamson069b6ad2009-04-03 08:27:58 +03002416__be32
2417nfsd4_create_session(struct svc_rqst *rqstp,
2418 struct nfsd4_compound_state *cstate,
2419 struct nfsd4_create_session *cr_ses)
2420{
Jeff Layton363168b2009-08-14 12:57:56 -04002421 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002422 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002423 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002424 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002425 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002426 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002427 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002428 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002429
Mi Jinlonga62573d2011-03-23 17:57:07 +08002430 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2431 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002432 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2433 if (status)
2434 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002435 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002436 if (status)
2437 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002438 status = check_backchannel_attrs(&cr_ses->back_channel);
2439 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002440 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002441 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002442 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002443 if (!new)
2444 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002445 conn = alloc_conn_from_crses(rqstp, cr_ses);
2446 if (!conn)
2447 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002448
Jeff Laytond20c11d2014-07-30 08:27:07 -04002449 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002450 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002451 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002452 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002453
2454 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002455 status = nfserr_wrong_cred;
2456 if (!mach_creds_match(conf, rqstp))
2457 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002458 cs_slot = &conf->cl_cs_slot;
2459 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002460 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002461 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002462 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002463 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002464 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002465 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002466 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002467 } else if (unconf) {
2468 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002469 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002470 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002471 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002472 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002473 status = nfserr_wrong_cred;
2474 if (!mach_creds_match(unconf, rqstp))
2475 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002476 cs_slot = &unconf->cl_cs_slot;
2477 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002478 if (status) {
2479 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002480 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002481 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002482 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002483 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002484 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002485 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002486 if (status) {
2487 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002488 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002489 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002490 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002491 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002492 conf = unconf;
2493 } else {
2494 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002495 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002496 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002497 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002498 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002499 * We do not support RDMA or persistent sessions
2500 */
2501 cr_ses->flags &= ~SESSION4_PERSIST;
2502 cr_ses->flags &= ~SESSION4_RDMA;
2503
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002504 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002505 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002506
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002507 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002508 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002509 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002510 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002511
Jeff Laytond20c11d2014-07-30 08:27:07 -04002512 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002513 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002514 spin_unlock(&nn->client_lock);
2515 /* init connection and backchannel */
2516 nfsd4_init_conn(rqstp, conn, new);
2517 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002518 if (old)
2519 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002520 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002521out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002522 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002523 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002524 if (old)
2525 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002526out_free_session:
2527 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002528out_release_drc_mem:
2529 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002530 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002531}
2532
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002533static __be32 nfsd4_map_bcts_dir(u32 *dir)
2534{
2535 switch (*dir) {
2536 case NFS4_CDFC4_FORE:
2537 case NFS4_CDFC4_BACK:
2538 return nfs_ok;
2539 case NFS4_CDFC4_FORE_OR_BOTH:
2540 case NFS4_CDFC4_BACK_OR_BOTH:
2541 *dir = NFS4_CDFC4_BOTH;
2542 return nfs_ok;
2543 };
2544 return nfserr_inval;
2545}
2546
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002547__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2548{
2549 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002550 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002551 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002552
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002553 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2554 if (status)
2555 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002556 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002557 session->se_cb_prog = bc->bc_cb_program;
2558 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002559 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002560
2561 nfsd4_probe_callback(session->se_client);
2562
2563 return nfs_ok;
2564}
2565
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002566__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2567 struct nfsd4_compound_state *cstate,
2568 struct nfsd4_bind_conn_to_session *bcts)
2569{
2570 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002571 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002572 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002573 struct net *net = SVC_NET(rqstp);
2574 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002575
2576 if (!nfsd4_last_compound_op(rqstp))
2577 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002578 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002579 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002580 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002581 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002582 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002583 status = nfserr_wrong_cred;
2584 if (!mach_creds_match(session->se_client, rqstp))
2585 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002586 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002587 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002588 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002589 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002590 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002591 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002592 goto out;
2593 nfsd4_init_conn(rqstp, conn, session);
2594 status = nfs_ok;
2595out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002596 nfsd4_put_session(session);
2597out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002598 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002599}
2600
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002601static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2602{
2603 if (!session)
2604 return 0;
2605 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2606}
2607
Andy Adamson069b6ad2009-04-03 08:27:58 +03002608__be32
2609nfsd4_destroy_session(struct svc_rqst *r,
2610 struct nfsd4_compound_state *cstate,
2611 struct nfsd4_destroy_session *sessionid)
2612{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002613 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002614 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002615 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002616 struct net *net = SVC_NET(r);
2617 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002618
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002619 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002620 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002621 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002622 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002623 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002624 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002625 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002626 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002627 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002628 if (!ses)
2629 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002630 status = nfserr_wrong_cred;
2631 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002632 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002633 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002634 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002635 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002636 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002637 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002638
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002639 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002640
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002641 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002642 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002643out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002644 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002645out_client_lock:
2646 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002647out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03002648 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002649}
2650
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002651static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002652{
2653 struct nfsd4_conn *c;
2654
2655 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002656 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002657 return c;
2658 }
2659 }
2660 return NULL;
2661}
2662
J. Bruce Fields57266a62013-04-13 14:27:29 -04002663static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002664{
2665 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002666 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002667 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002668 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002669
2670 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002671 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002672 if (c)
2673 goto out_free;
2674 status = nfserr_conn_not_bound_to_session;
2675 if (clp->cl_mach_cred)
2676 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002677 __nfsd4_hash_conn(new, ses);
2678 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002679 ret = nfsd4_register_conn(new);
2680 if (ret)
2681 /* oops; xprt is already down: */
2682 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002683 return nfs_ok;
2684out_free:
2685 spin_unlock(&clp->cl_lock);
2686 free_conn(new);
2687 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002688}
2689
Mi Jinlong868b89c2011-04-27 09:09:58 +08002690static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2691{
2692 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2693
2694 return args->opcnt > session->se_fchannel.maxops;
2695}
2696
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002697static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2698 struct nfsd4_session *session)
2699{
2700 struct xdr_buf *xb = &rqstp->rq_arg;
2701
2702 return xb->len > session->se_fchannel.maxreq_sz;
2703}
2704
Andy Adamson069b6ad2009-04-03 08:27:58 +03002705__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002706nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002707 struct nfsd4_compound_state *cstate,
2708 struct nfsd4_sequence *seq)
2709{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002710 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002711 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002712 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002713 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002714 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002715 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002716 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002717 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002718 struct net *net = SVC_NET(rqstp);
2719 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002720
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002721 if (resp->opcnt != 1)
2722 return nfserr_sequence_pos;
2723
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002724 /*
2725 * Will be either used or freed by nfsd4_sequence_check_conn
2726 * below.
2727 */
2728 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2729 if (!conn)
2730 return nfserr_jukebox;
2731
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002732 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002733 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002734 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002735 goto out_no_session;
2736 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002737
Mi Jinlong868b89c2011-04-27 09:09:58 +08002738 status = nfserr_too_many_ops;
2739 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002740 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002741
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002742 status = nfserr_req_too_big;
2743 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002744 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002745
Benny Halevyb85d4c02009-04-03 08:28:08 +03002746 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002747 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002748 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002749
Andy Adamson557ce262009-08-28 08:45:04 -04002750 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002751 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2752
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002753 /* We do not negotiate the number of slots yet, so set the
2754 * maxslots to the session maxreqs which is used to encode
2755 * sr_highest_slotid and the sr_target_slot id to maxslots */
2756 seq->maxslots = session->se_fchannel.maxreqs;
2757
J. Bruce Fields73e79482012-02-13 16:39:00 -05002758 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2759 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002760 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002761 status = nfserr_seq_misordered;
2762 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002763 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002764 cstate->slot = slot;
2765 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002766 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002767 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002768 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002769 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002770 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002771 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002772 }
2773 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002774 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002775
J. Bruce Fields57266a62013-04-13 14:27:29 -04002776 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002777 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002778 if (status)
2779 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002780
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002781 buflen = (seq->cachethis) ?
2782 session->se_fchannel.maxresp_cached :
2783 session->se_fchannel.maxresp_sz;
2784 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2785 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002786 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002787 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002788 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002789
2790 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002791 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002792 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002793 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002794 if (seq->cachethis)
2795 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002796 else
2797 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002798
2799 cstate->slot = slot;
2800 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002801 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002802
Benny Halevyb85d4c02009-04-03 08:28:08 +03002803out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002804 switch (clp->cl_cb_state) {
2805 case NFSD4_CB_DOWN:
2806 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2807 break;
2808 case NFSD4_CB_FAULT:
2809 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2810 break;
2811 default:
2812 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002813 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002814 if (!list_empty(&clp->cl_revoked))
2815 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002816out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002817 if (conn)
2818 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002819 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002820 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002821out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002822 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002823 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002824}
2825
Trond Myklebustb6076642014-06-30 11:48:35 -04002826void
2827nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2828{
2829 struct nfsd4_compound_state *cs = &resp->cstate;
2830
2831 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002832 if (cs->status != nfserr_replay_cache) {
2833 nfsd4_store_cache_entry(resp);
2834 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2835 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002836 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002837 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002838 } else if (cs->clp)
2839 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002840}
2841
Mi Jinlong345c2842011-10-20 17:51:39 +08002842__be32
2843nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2844{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002845 struct nfs4_client *conf, *unconf;
2846 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002847 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002848 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002849
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002850 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002851 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002852 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002853 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002854
2855 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002856 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002857 status = nfserr_clientid_busy;
2858 goto out;
2859 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04002860 status = mark_client_expired_locked(conf);
2861 if (status)
2862 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002863 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08002864 } else if (unconf)
2865 clp = unconf;
2866 else {
2867 status = nfserr_stale_clientid;
2868 goto out;
2869 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002870 if (!mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002871 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002872 status = nfserr_wrong_cred;
2873 goto out;
2874 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002875 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002876out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002877 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002878 if (clp)
2879 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002880 return status;
2881}
2882
Andy Adamson069b6ad2009-04-03 08:27:58 +03002883__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002884nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2885{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002886 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002887
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002888 if (rc->rca_one_fs) {
2889 if (!cstate->current_fh.fh_dentry)
2890 return nfserr_nofilehandle;
2891 /*
2892 * We don't take advantage of the rca_one_fs case.
2893 * That's OK, it's optional, we can safely ignore it.
2894 */
2895 return nfs_ok;
2896 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002897
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002898 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002899 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2900 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002901 goto out;
2902
2903 status = nfserr_stale_clientid;
2904 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002905 /*
2906 * The following error isn't really legal.
2907 * But we only get here if the client just explicitly
2908 * destroyed the client. Surely it no longer cares what
2909 * error it gets back on an operation for the dead
2910 * client.
2911 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002912 goto out;
2913
2914 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002915 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002916out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002917 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002918}
2919
2920__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002921nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2922 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002924 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002926 struct nfs4_client *conf, *new;
2927 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002928 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002929 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2930
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002931 new = create_client(clname, rqstp, &clverifier);
2932 if (new == NULL)
2933 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002934 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002935 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002936 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002937 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002938 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002940 if (clp_used_exchangeid(conf))
2941 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002942 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002943 char addr_str[INET6_ADDRSTRLEN];
2944 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2945 sizeof(addr_str));
2946 dprintk("NFSD: setclientid: string in use by client "
2947 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 goto out;
2949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002951 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002952 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002953 unhash_client_locked(unconf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002954 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002955 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002957 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002958 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002959 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002960 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002961 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2963 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2964 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002965 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 status = nfs_ok;
2967out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002968 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002969 if (new)
2970 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002971 if (unconf)
2972 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 return status;
2974}
2975
2976
Al Virob37ad282006-10-19 23:28:59 -07002977__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002978nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2979 struct nfsd4_compound_state *cstate,
2980 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981{
NeilBrown21ab45a2005-06-23 22:04:14 -07002982 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002983 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2985 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002986 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002987 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002989 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07002991
Jeff Laytond20c11d2014-07-30 08:27:07 -04002992 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002993 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002994 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002995 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002996 * We try hard to give out unique clientid's, so if we get an
2997 * attempt to confirm the same clientid with a different cred,
2998 * there's a bug somewhere. Let's charitably assume it's our
2999 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003000 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04003001 status = nfserr_serverfault;
3002 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3003 goto out;
3004 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3005 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003006 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003007 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3008 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003010 else /* case 4: client hasn't noticed we rebooted yet? */
3011 status = nfserr_stale_clientid;
3012 goto out;
3013 }
3014 status = nfs_ok;
3015 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003016 old = unconf;
3017 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003018 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003019 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003020 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3021 if (old) {
3022 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003023 if (status) {
3024 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003025 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003026 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003027 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003028 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003029 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003030 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003031 get_client_locked(conf);
3032 spin_unlock(&nn->client_lock);
3033 nfsd4_probe_callback(conf);
3034 spin_lock(&nn->client_lock);
3035 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003037 spin_unlock(&nn->client_lock);
3038 if (old)
3039 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 return status;
3041}
3042
J. Bruce Fields32513b42011-10-13 16:00:16 -04003043static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003045 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3046}
3047
3048/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003049static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3050 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003051{
Trond Myklebust950e0112014-06-30 11:48:31 -04003052 lockdep_assert_held(&state_lock);
3053
J. Bruce Fields32513b42011-10-13 16:00:16 -04003054 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003055 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003056 INIT_LIST_HEAD(&fp->fi_stateids);
3057 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003058 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003059 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003060 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003061 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003062 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3063 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton5b095e92014-10-23 08:01:02 -04003064 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065}
3066
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003067void
NeilBrowne60d4392005-06-23 22:03:01 -07003068nfsd4_free_slabs(void)
3069{
Christoph Hellwigabf11352014-05-21 07:43:03 -07003070 kmem_cache_destroy(openowner_slab);
3071 kmem_cache_destroy(lockowner_slab);
3072 kmem_cache_destroy(file_slab);
3073 kmem_cache_destroy(stateid_slab);
3074 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003075}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Bryan Schumaker72083392011-11-01 15:24:59 -04003077int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078nfsd4_init_slabs(void)
3079{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003080 openowner_slab = kmem_cache_create("nfsd4_openowners",
3081 sizeof(struct nfs4_openowner), 0, 0, NULL);
3082 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003083 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003084 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003085 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003086 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003087 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003088 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003089 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003090 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003091 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003092 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003093 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003094 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003095 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003096 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003097 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003098 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003099 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003101
3102out_free_stateid_slab:
3103 kmem_cache_destroy(stateid_slab);
3104out_free_file_slab:
3105 kmem_cache_destroy(file_slab);
3106out_free_lockowner_slab:
3107 kmem_cache_destroy(lockowner_slab);
3108out_free_openowner_slab:
3109 kmem_cache_destroy(openowner_slab);
3110out:
NeilBrowne60d4392005-06-23 22:03:01 -07003111 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3112 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113}
3114
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003115static void init_nfs4_replay(struct nfs4_replay *rp)
3116{
3117 rp->rp_status = nfserr_serverfault;
3118 rp->rp_buflen = 0;
3119 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003120 mutex_init(&rp->rp_mutex);
3121}
3122
3123static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3124 struct nfs4_stateowner *so)
3125{
3126 if (!nfsd4_has_session(cstate)) {
3127 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003128 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04003129 }
3130}
3131
3132void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3133{
3134 struct nfs4_stateowner *so = cstate->replay_owner;
3135
3136 if (so != NULL) {
3137 cstate->replay_owner = NULL;
3138 mutex_unlock(&so->so_replay.rp_mutex);
3139 nfs4_put_stateowner(so);
3140 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003141}
3142
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003143static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144{
3145 struct nfs4_stateowner *sop;
3146
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003147 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003148 if (!sop)
3149 return NULL;
3150
3151 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3152 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003153 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003154 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003156 sop->so_owner.len = owner->len;
3157
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003158 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003159 sop->so_client = clp;
3160 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003161 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003162 return sop;
3163}
3164
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003165static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003166{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003167 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003168
Trond Myklebustd4f04892014-07-29 21:34:36 -04003169 list_add(&oo->oo_owner.so_strhash,
3170 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003171 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172}
3173
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003174static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3175{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003176 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003177}
3178
Jeff Layton6b180f02014-07-29 21:34:26 -04003179static void nfs4_free_openowner(struct nfs4_stateowner *so)
3180{
3181 struct nfs4_openowner *oo = openowner(so);
3182
3183 kmem_cache_free(openowner_slab, oo);
3184}
3185
3186static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003187 .so_unhash = nfs4_unhash_openowner,
3188 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003189};
3190
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003191static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003192alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003193 struct nfsd4_compound_state *cstate)
3194{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003195 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003196 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003198 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3199 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003201 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003202 oo->oo_owner.so_is_open_owner = 1;
3203 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003204 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003205 if (nfsd4_has_session(cstate))
3206 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003207 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003208 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003209 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003210 spin_lock(&clp->cl_lock);
3211 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003212 if (ret == NULL) {
3213 hash_openowner(oo, clp, strhashval);
3214 ret = oo;
3215 } else
3216 nfs4_free_openowner(&oo->oo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003217 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003218 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219}
3220
J. Bruce Fields996e0932011-10-17 11:14:48 -04003221static 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 -04003222 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003224 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003225 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003226 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003227 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07003228 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003229 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 stp->st_access_bmap = 0;
3231 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003232 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003233 spin_lock(&oo->oo_owner.so_client->cl_lock);
3234 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003235 spin_lock(&fp->fi_lock);
3236 list_add(&stp->st_perfile, &fp->fi_stateids);
3237 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003238 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239}
3240
Jeff Laytond3134b12014-07-29 21:34:32 -04003241/*
3242 * In the 4.0 case we need to keep the owners around a little while to handle
3243 * CLOSE replay. We still do need to release any file access that is held by
3244 * them before returning however.
3245 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003247move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248{
Jeff Layton217526e2014-07-30 08:27:11 -04003249 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003250 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3251 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3252 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003253
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003254 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255
Jeff Laytonb401be222014-07-29 21:34:33 -04003256 /*
3257 * We know that we hold one reference via nfsd4_close, and another
3258 * "persistent" reference for the client. If the refcount is higher
3259 * than 2, then there are still calls in progress that are using this
3260 * stateid. We can't put the sc_file reference until they are finished.
3261 * Wait for the refcount to drop to 2. Since it has been unhashed,
3262 * there should be no danger of the refcount going back up again at
3263 * this point.
3264 */
3265 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3266
Jeff Laytond3134b12014-07-29 21:34:32 -04003267 release_all_access(s);
3268 if (s->st_stid.sc_file) {
3269 put_nfs4_file(s->st_stid.sc_file);
3270 s->st_stid.sc_file = NULL;
3271 }
Jeff Layton217526e2014-07-30 08:27:11 -04003272
3273 spin_lock(&nn->client_lock);
3274 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003275 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003276 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003277 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003278 spin_unlock(&nn->client_lock);
3279 if (last)
3280 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281}
3282
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283/* search file_hashtbl[] for file */
3284static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04003285find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 struct nfs4_file *fp;
3288
Jeff Layton5b095e92014-10-23 08:01:02 -04003289 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02003290 if (fh_match(&fp->fi_fhandle, fh)) {
Jeff Layton5b095e92014-10-23 08:01:02 -04003291 if (atomic_inc_not_zero(&fp->fi_ref))
3292 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 }
3295 return NULL;
3296}
3297
Trond Myklebust950e0112014-06-30 11:48:31 -04003298static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003299find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003300{
3301 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003302 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003303
Jeff Layton5b095e92014-10-23 08:01:02 -04003304 rcu_read_lock();
3305 fp = find_file_locked(fh, hashval);
3306 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04003307 return fp;
3308}
3309
3310static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003311find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003312{
3313 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003314 unsigned int hashval = file_hashval(fh);
3315
3316 rcu_read_lock();
3317 fp = find_file_locked(fh, hashval);
3318 rcu_read_unlock();
3319 if (fp)
3320 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04003321
3322 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04003323 fp = find_file_locked(fh, hashval);
3324 if (likely(fp == NULL)) {
3325 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04003326 fp = new;
3327 }
3328 spin_unlock(&state_lock);
3329
3330 return fp;
3331}
3332
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003333/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 * Called to check deny when READ with all zero stateid or
3335 * WRITE with all zero or all one stateid
3336 */
Al Virob37ad282006-10-19 23:28:59 -07003337static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3339{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003341 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342
Trond Myklebustca943212014-07-23 16:17:39 -04003343 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003344 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003345 return ret;
3346 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003347 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003348 if (fp->fi_share_deny & deny_type)
3349 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003350 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003351 put_nfs4_file(fp);
3352 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353}
3354
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003355static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003357 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04003358 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3359 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003360
Trond Myklebust11b91642014-07-29 21:34:08 -04003361 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003362
Jeff Layton02e12152014-07-16 10:31:57 -04003363 /*
3364 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003365 * already holding inode->i_lock.
3366 *
Jeff Laytondff13992014-07-08 14:02:49 -04003367 * If the dl_time != 0, then we know that it has already been
3368 * queued for a lease break. Don't queue it again.
3369 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003370 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003371 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003372 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003373 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003374 }
Jeff Layton02e12152014-07-16 10:31:57 -04003375 spin_unlock(&state_lock);
3376}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003378static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3379 struct rpc_task *task)
3380{
3381 struct nfs4_delegation *dp = cb_to_delegation(cb);
3382
3383 switch (task->tk_status) {
3384 case 0:
3385 return 1;
3386 case -EBADHANDLE:
3387 case -NFS4ERR_BAD_STATEID:
3388 /*
3389 * Race: client probably got cb_recall before open reply
3390 * granting delegation.
3391 */
3392 if (dp->dl_retries--) {
3393 rpc_delay(task, 2 * HZ);
3394 return 0;
3395 }
3396 /*FALLTHRU*/
3397 default:
3398 return -1;
3399 }
3400}
3401
3402static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3403{
3404 struct nfs4_delegation *dp = cb_to_delegation(cb);
3405
3406 nfs4_put_stid(&dp->dl_stid);
3407}
3408
3409static struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3410 .prepare = nfsd4_cb_recall_prepare,
3411 .done = nfsd4_cb_recall_done,
3412 .release = nfsd4_cb_recall_release,
3413};
3414
Jeff Layton02e12152014-07-16 10:31:57 -04003415static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3416{
3417 /*
3418 * We're assuming the state code never drops its reference
3419 * without first removing the lease. Since we're in this lease
3420 * callback (and since the lease code is serialized by the kernel
3421 * lock) we know the server hasn't removed the lease yet, we know
3422 * it's safe to take a reference.
3423 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003424 atomic_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02003425 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003426}
3427
Jeff Layton1c8c6012013-06-21 08:58:15 -04003428/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003429static bool
3430nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003431{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003432 bool ret = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003433 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3434 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003435
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003436 if (!fp) {
3437 WARN(1, "(%p)->fl_owner NULL\n", fl);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003438 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003439 }
3440 if (fp->fi_had_conflict) {
3441 WARN(1, "duplicate break on %p\n", fp);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003442 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003443 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003444 /*
3445 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003446 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003447 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003448 */
3449 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
Jeff Layton02e12152014-07-16 10:31:57 -04003451 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003452 fp->fi_had_conflict = true;
3453 /*
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003454 * If there are no delegations on the list, then return true
3455 * so that the lease code will go ahead and delete it.
Jeff Layton417c6622014-07-21 09:34:57 -04003456 */
3457 if (list_empty(&fp->fi_delegations))
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003458 ret = true;
Jeff Layton417c6622014-07-21 09:34:57 -04003459 else
3460 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3461 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003462 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003463 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464}
3465
Jeff Laytonc45198e2014-09-01 07:12:07 -04003466static int
Jeff Layton7448cc32015-01-16 15:05:57 -05003467nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3468 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469{
3470 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04003471 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 else
3473 return -EAGAIN;
3474}
3475
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003476static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003477 .lm_break = nfsd_break_deleg_cb,
3478 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479};
3480
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003481static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3482{
3483 if (nfsd4_has_session(cstate))
3484 return nfs_ok;
3485 if (seqid == so->so_seqid - 1)
3486 return nfserr_replay_me;
3487 if (seqid == so->so_seqid)
3488 return nfs_ok;
3489 return nfserr_bad_seqid;
3490}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
Jeff Layton4b24ca72014-06-30 11:48:44 -04003492static __be32 lookup_clientid(clientid_t *clid,
3493 struct nfsd4_compound_state *cstate,
3494 struct nfsd_net *nn)
3495{
3496 struct nfs4_client *found;
3497
3498 if (cstate->clp) {
3499 found = cstate->clp;
3500 if (!same_clid(&found->cl_clientid, clid))
3501 return nfserr_stale_clientid;
3502 return nfs_ok;
3503 }
3504
3505 if (STALE_CLIENTID(clid, nn))
3506 return nfserr_stale_clientid;
3507
3508 /*
3509 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3510 * cached already then we know this is for is for v4.0 and "sessions"
3511 * will be false.
3512 */
3513 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003514 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003515 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003516 if (!found) {
3517 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003518 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003519 }
3520 atomic_inc(&found->cl_refcount);
3521 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003522
3523 /* Cache the nfs4_client in cstate! */
3524 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003525 return nfs_ok;
3526}
3527
Al Virob37ad282006-10-19 23:28:59 -07003528__be32
Andy Adamson66689582009-04-03 08:28:45 +03003529nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003530 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 clientid_t *clientid = &open->op_clientid;
3533 struct nfs4_client *clp = NULL;
3534 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003535 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003536 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003538 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003540 /*
3541 * In case we need it later, after we've already created the
3542 * file and don't want to risk a further failure:
3543 */
3544 open->op_file = nfsd4_alloc_file();
3545 if (open->op_file == NULL)
3546 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547
Trond Myklebust2d91e892014-06-30 11:48:46 -04003548 status = lookup_clientid(clientid, cstate, nn);
3549 if (status)
3550 return status;
3551 clp = cstate->clp;
3552
Trond Myklebustd4f04892014-07-29 21:34:36 -04003553 strhashval = ownerstr_hashval(&open->op_owner);
3554 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003555 open->op_openowner = oo;
3556 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003557 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003559 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003560 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003561 release_openowner(oo);
3562 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003563 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003564 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003565 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3566 if (status)
3567 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003568 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003569new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003570 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003571 if (oo == NULL)
3572 return nfserr_jukebox;
3573 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003574alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003575 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003576 if (!open->op_stp)
3577 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003578 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579}
3580
Al Virob37ad282006-10-19 23:28:59 -07003581static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003582nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3583{
3584 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3585 return nfserr_openmode;
3586 else
3587 return nfs_ok;
3588}
3589
Daniel Mackc47d8322011-05-16 16:38:14 +02003590static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003591{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003592 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3593}
3594
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003595static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003596{
3597 struct nfs4_stid *ret;
3598
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003599 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003600 if (!ret)
3601 return NULL;
3602 return delegstateid(ret);
3603}
3604
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003605static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3606{
3607 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3608 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3609}
3610
Al Virob37ad282006-10-19 23:28:59 -07003611static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003612nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003613 struct nfs4_delegation **dp)
3614{
3615 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003616 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003617 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003618
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003619 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3620 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003621 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003622 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003623 status = nfs4_check_delegmode(deleg, flags);
3624 if (status) {
3625 nfs4_put_stid(&deleg->dl_stid);
3626 goto out;
3627 }
3628 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003629out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003630 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003631 return nfs_ok;
3632 if (status)
3633 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003634 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003635 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003636}
3637
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003638static struct nfs4_ol_stateid *
3639nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003641 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003642 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643
Trond Myklebust1d31a252014-07-10 14:07:25 -04003644 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003645 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 /* ignore lock owners */
3647 if (local->st_stateowner->so_is_open_owner == 0)
3648 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003649 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003650 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003651 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003652 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003655 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003656 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657}
3658
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003659static inline int nfs4_access_to_access(u32 nfs4_access)
3660{
3661 int flags = 0;
3662
3663 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3664 flags |= NFSD_MAY_READ;
3665 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3666 flags |= NFSD_MAY_WRITE;
3667 return flags;
3668}
3669
Al Virob37ad282006-10-19 23:28:59 -07003670static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3672 struct nfsd4_open *open)
3673{
3674 struct iattr iattr = {
3675 .ia_valid = ATTR_SIZE,
3676 .ia_size = 0,
3677 };
3678 if (!open->op_truncate)
3679 return 0;
3680 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003681 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3683}
3684
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003685static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003686 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3687 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003688{
Trond Myklebustde186432014-07-10 14:07:26 -04003689 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003690 __be32 status;
3691 int oflag = nfs4_access_to_omode(open->op_share_access);
3692 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003693 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003694
Trond Myklebustde186432014-07-10 14:07:26 -04003695 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003696
3697 /*
3698 * Are we trying to set a deny mode that would conflict with
3699 * current access?
3700 */
3701 status = nfs4_file_check_deny(fp, open->op_share_deny);
3702 if (status != nfs_ok) {
3703 spin_unlock(&fp->fi_lock);
3704 goto out;
3705 }
3706
3707 /* set access to the file */
3708 status = nfs4_file_get_access(fp, open->op_share_access);
3709 if (status != nfs_ok) {
3710 spin_unlock(&fp->fi_lock);
3711 goto out;
3712 }
3713
3714 /* Set access bits in stateid */
3715 old_access_bmap = stp->st_access_bmap;
3716 set_access(open->op_share_access, stp);
3717
3718 /* Set new deny mask */
3719 old_deny_bmap = stp->st_deny_bmap;
3720 set_deny(open->op_share_deny, stp);
3721 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3722
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003723 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003724 spin_unlock(&fp->fi_lock);
3725 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003726 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003727 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003728 spin_lock(&fp->fi_lock);
3729 if (!fp->fi_fds[oflag]) {
3730 fp->fi_fds[oflag] = filp;
3731 filp = NULL;
3732 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003733 }
Trond Myklebustde186432014-07-10 14:07:26 -04003734 spin_unlock(&fp->fi_lock);
3735 if (filp)
3736 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003737
3738 status = nfsd4_truncate(rqstp, cur_fh, open);
3739 if (status)
3740 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003741out:
3742 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003743out_put_access:
3744 stp->st_access_bmap = old_access_bmap;
3745 nfs4_file_put_access(fp, open->op_share_access);
3746 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3747 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003748}
3749
Al Virob37ad282006-10-19 23:28:59 -07003750static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003751nfs4_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 -07003752{
Al Virob37ad282006-10-19 23:28:59 -07003753 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003754 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003756 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003757 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003758
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003759 /* test and set deny mode */
3760 spin_lock(&fp->fi_lock);
3761 status = nfs4_file_check_deny(fp, open->op_share_deny);
3762 if (status == nfs_ok) {
3763 old_deny_bmap = stp->st_deny_bmap;
3764 set_deny(open->op_share_deny, stp);
3765 fp->fi_share_deny |=
3766 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3767 }
3768 spin_unlock(&fp->fi_lock);
3769
3770 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003773 status = nfsd4_truncate(rqstp, cur_fh, open);
3774 if (status != nfs_ok)
3775 reset_union_bmap_deny(old_deny_bmap, stp);
3776 return status;
3777}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003780nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003782 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783}
3784
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003785/* Should we give out recallable state?: */
3786static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3787{
3788 if (clp->cl_cb_state == NFSD4_CB_UP)
3789 return true;
3790 /*
3791 * In the sessions case, since we don't have to establish a
3792 * separate connection for callbacks, we assume it's OK
3793 * until we hear otherwise:
3794 */
3795 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3796}
3797
Jeff Laytond564fbe2014-07-16 10:31:58 -04003798static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003799{
3800 struct file_lock *fl;
3801
3802 fl = locks_alloc_lock();
3803 if (!fl)
3804 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003805 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003806 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003807 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3808 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003809 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003810 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003811 return fl;
3812}
3813
J. Bruce Fields99c41512013-05-21 16:21:25 -04003814static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003815{
Trond Myklebust11b91642014-07-29 21:34:08 -04003816 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton415b96c2014-08-22 12:26:36 -04003817 struct file_lock *fl, *ret;
Jeff Layton417c6622014-07-21 09:34:57 -04003818 struct file *filp;
3819 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003820
Jeff Laytond564fbe2014-07-16 10:31:58 -04003821 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003822 if (!fl)
3823 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003824 filp = find_readable_file(fp);
3825 if (!filp) {
3826 /* We should always have a readable file here */
3827 WARN_ON_ONCE(1);
3828 return -EBADF;
3829 }
3830 fl->fl_file = filp;
Jeff Layton415b96c2014-08-22 12:26:36 -04003831 ret = fl;
Jeff Laytone6f5c782014-08-22 10:40:25 -04003832 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003833 if (fl)
Jeff Layton417c6622014-07-21 09:34:57 -04003834 locks_free_lock(fl);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003835 if (status)
Jeff Layton417c6622014-07-21 09:34:57 -04003836 goto out_fput;
Benny Halevycdc97502014-05-30 09:09:30 -04003837 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003838 spin_lock(&fp->fi_lock);
3839 /* Did the lease get broken before we took the lock? */
3840 status = -EAGAIN;
3841 if (fp->fi_had_conflict)
3842 goto out_unlock;
3843 /* Race breaker */
Jeff Layton0c637be2014-08-22 12:05:43 -04003844 if (fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04003845 status = 0;
Jeff Layton67db1032014-12-13 09:11:40 -05003846 ++fp->fi_delegees;
Jeff Layton417c6622014-07-21 09:34:57 -04003847 hash_delegation_locked(dp, fp);
3848 goto out_unlock;
3849 }
Jeff Layton417c6622014-07-21 09:34:57 -04003850 fp->fi_deleg_file = filp;
Jeff Layton67db1032014-12-13 09:11:40 -05003851 fp->fi_delegees = 1;
Benny Halevy931ee562014-05-30 09:09:27 -04003852 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003853 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003854 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003855 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003856out_unlock:
3857 spin_unlock(&fp->fi_lock);
3858 spin_unlock(&state_lock);
3859out_fput:
3860 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003861 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003862}
3863
Jeff Layton0b266932014-07-25 07:34:25 -04003864static struct nfs4_delegation *
3865nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3866 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003867{
Jeff Layton0b266932014-07-25 07:34:25 -04003868 int status;
3869 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003870
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003871 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003872 return ERR_PTR(-EAGAIN);
3873
3874 dp = alloc_init_deleg(clp, fh);
3875 if (!dp)
3876 return ERR_PTR(-ENOMEM);
3877
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003878 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003879 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003880 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003881 dp->dl_stid.sc_file = fp;
Jeff Layton0c637be2014-08-22 12:05:43 -04003882 if (!fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04003883 spin_unlock(&fp->fi_lock);
3884 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003885 status = nfs4_setlease(dp);
3886 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003887 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003888 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003889 status = -EAGAIN;
3890 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003891 }
Jeff Layton67db1032014-12-13 09:11:40 -05003892 ++fp->fi_delegees;
Benny Halevy931ee562014-05-30 09:09:27 -04003893 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003894 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003895out_unlock:
3896 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003897 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003898out:
3899 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003900 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003901 return ERR_PTR(status);
3902 }
3903 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003904}
3905
Benny Halevy4aa89132012-02-21 14:16:44 -08003906static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3907{
3908 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3909 if (status == -EAGAIN)
3910 open->op_why_no_deleg = WND4_CONTENTION;
3911 else {
3912 open->op_why_no_deleg = WND4_RESOURCE;
3913 switch (open->op_deleg_want) {
3914 case NFS4_SHARE_WANT_READ_DELEG:
3915 case NFS4_SHARE_WANT_WRITE_DELEG:
3916 case NFS4_SHARE_WANT_ANY_DELEG:
3917 break;
3918 case NFS4_SHARE_WANT_CANCEL:
3919 open->op_why_no_deleg = WND4_CANCELLED;
3920 break;
3921 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003922 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003923 }
3924 }
3925}
3926
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927/*
3928 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003929 *
3930 * Note we don't support write delegations, and won't until the vfs has
3931 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 */
3933static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003934nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3935 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936{
3937 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003938 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3939 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003940 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003941 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003943 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003944 open->op_recall = 0;
3945 switch (open->op_claim_type) {
3946 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003947 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003948 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003949 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3950 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003951 break;
3952 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003953 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003954 /*
3955 * Let's not give out any delegations till everyone's
3956 * had the chance to reclaim theirs....
3957 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003958 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003959 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003960 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003961 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003962 /*
3963 * Also, if the file was opened for write or
3964 * create, there's a good chance the client's
3965 * about to write to it, resulting in an
3966 * immediate recall (since we don't support
3967 * write delegations):
3968 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003969 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003970 goto out_no_deleg;
3971 if (open->op_create == NFS4_OPEN_CREATE)
3972 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003973 break;
3974 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003975 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003976 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003977 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003978 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003979 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003981 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003983 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003984 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003985 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003986 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003987 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003988out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003989 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3990 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003991 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003992 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003993 open->op_recall = 1;
3994 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003995
3996 /* 4.1 client asking for a delegation? */
3997 if (open->op_deleg_want)
3998 nfsd4_open_deleg_none_ext(open, status);
3999 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000}
4001
Benny Halevye27f49c2012-02-21 14:16:54 -08004002static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4003 struct nfs4_delegation *dp)
4004{
4005 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4006 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4007 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4008 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4009 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4010 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4011 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4012 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4013 }
4014 /* Otherwise the client must be confused wanting a delegation
4015 * it already has, therefore we don't return
4016 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4017 */
4018}
4019
Al Virob37ad282006-10-19 23:28:59 -07004020__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4022{
Andy Adamson66689582009-04-03 08:28:45 +03004023 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004024 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004026 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004027 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004028 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 /*
4031 * Lookup file; if found, lookup stateid and check open request,
4032 * and check for delegations in the process of being recalled.
4033 * If not found, create the nfs4_file struct
4034 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004035 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004036 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004037 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004038 if (status)
4039 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04004040 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004042 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004043 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004044 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004045 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004046 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 }
4048
4049 /*
4050 * OPEN the file, or upgrade an existing OPEN.
4051 * If truncate fails, the OPEN fails.
4052 */
4053 if (stp) {
4054 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004055 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 if (status)
4057 goto out;
4058 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004059 stp = open->op_stp;
4060 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004061 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004062 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4063 if (status) {
4064 release_open_stateid(stp);
4065 goto out;
4066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004068 update_stateid(&stp->st_stid.sc_stateid);
4069 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070
Benny Halevyd24433c2012-02-16 20:57:17 +02004071 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004072 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4073 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4074 open->op_why_no_deleg = WND4_NOT_WANTED;
4075 goto nodeleg;
4076 }
4077 }
4078
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 /*
4080 * Attempt to hand out a delegation. No error return, because the
4081 * OPEN succeeds even if we fail.
4082 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004083 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004084nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 status = nfs_ok;
4086
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004087 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004088 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004090 /* 4.1 client trying to upgrade/downgrade delegation? */
4091 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004092 open->op_deleg_want)
4093 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004094
NeilBrown13cd2182005-06-23 22:03:10 -07004095 if (fp)
4096 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004097 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05004098 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 /*
4100 * To finish the open response, we just need to set the rflags.
4101 */
4102 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004103 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004104 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004106 if (dp)
4107 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004108 if (stp)
4109 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110
4111 return status;
4112}
4113
Jeff Layton58fb12e2014-07-29 21:34:27 -04004114void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4115 struct nfsd4_open *open, __be32 status)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004116{
4117 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004118 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004119
Jeff Laytond3134b12014-07-29 21:34:32 -04004120 nfsd4_cstate_assign_replay(cstate, so);
4121 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004122 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004123 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04004124 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004125 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004126 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004127}
4128
Al Virob37ad282006-10-19 23:28:59 -07004129__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004130nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4131 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132{
4133 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004134 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004135 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 dprintk("process_renew(%08x/%08x): starting\n",
4138 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004139 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004140 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004142 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004144 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004145 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 goto out;
4147 status = nfs_ok;
4148out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 return status;
4150}
4151
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004152void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004153nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004154{
Jeff Layton33dcc482012-04-10 11:08:48 -04004155 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004156 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004157 return;
4158
NeilBrowna76b4312005-06-23 22:04:01 -07004159 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004160 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004161 /*
4162 * If the server goes down again right now, an NFSv4
4163 * client will still be allowed to reclaim after it comes back up,
4164 * even if it hasn't yet had a chance to reclaim state this time.
4165 *
4166 */
Jeff Layton919b8042014-09-12 16:40:20 -04004167 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004168 /*
4169 * At this point, NFSv4 clients can still reclaim. But if the
4170 * server crashes, any that have not yet reclaimed will be out
4171 * of luck on the next boot.
4172 *
4173 * (NFSv4.1+ clients are considered to have reclaimed once they
4174 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4175 * have reclaimed after their first OPEN.)
4176 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004177 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004178 /*
4179 * At this point, and once lockd and/or any other containers
4180 * exit their grace period, further reclaims will fail and
4181 * regular locking can resume.
4182 */
NeilBrowna76b4312005-06-23 22:04:01 -07004183}
4184
NeilBrownfd39ca92005-06-23 22:04:03 -07004185static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004186nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187{
4188 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004189 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004191 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004193 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004194 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004197 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004198 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004199 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004200 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 clp = list_entry(pos, struct nfs4_client, cl_lru);
4202 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4203 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004204 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 break;
4206 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004207 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004208 dprintk("NFSD: client in use (clientid %08x)\n",
4209 clp->cl_clientid.cl_id);
4210 continue;
4211 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004212 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004213 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004214 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004215 list_for_each_safe(pos, next, &reaplist) {
4216 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 dprintk("NFSD: purging unused client (clientid %08x)\n",
4218 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004219 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 expire_client(clp);
4221 }
Benny Halevycdc97502014-05-30 09:09:30 -04004222 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004223 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004225 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4226 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004228 t = dp->dl_time - cutoff;
4229 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 break;
4231 }
Jeff Layton42690672014-07-25 07:34:20 -04004232 unhash_delegation_locked(dp);
4233 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 }
Benny Halevycdc97502014-05-30 09:09:30 -04004235 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004236 while (!list_empty(&reaplist)) {
4237 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4238 dl_recall_lru);
4239 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004240 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 }
Jeff Layton217526e2014-07-30 08:27:11 -04004242
4243 spin_lock(&nn->client_lock);
4244 while (!list_empty(&nn->close_lru)) {
4245 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4246 oo_close_lru);
4247 if (time_after((unsigned long)oo->oo_time,
4248 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004249 t = oo->oo_time - cutoff;
4250 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 break;
4252 }
Jeff Layton217526e2014-07-30 08:27:11 -04004253 list_del_init(&oo->oo_close_lru);
4254 stp = oo->oo_last_closed_stid;
4255 oo->oo_last_closed_stid = NULL;
4256 spin_unlock(&nn->client_lock);
4257 nfs4_put_stid(&stp->st_stid);
4258 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 }
Jeff Layton217526e2014-07-30 08:27:11 -04004260 spin_unlock(&nn->client_lock);
4261
Jeff Laytona832e7a2014-05-30 09:09:26 -04004262 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004263 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264}
4265
Harvey Harrisona254b242008-02-20 12:49:00 -08004266static struct workqueue_struct *laundry_wq;
4267static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004268
4269static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004270laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271{
4272 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004273 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4274 work);
4275 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4276 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004278 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004280 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281}
4282
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004283static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004284{
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02004285 if (!fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004286 return nfserr_bad_stateid;
4287 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288}
4289
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004291access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004293 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4294 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4295 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296}
4297
4298static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004299access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004301 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4302 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303}
4304
4305static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004306__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307{
Al Virob37ad282006-10-19 23:28:59 -07004308 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309
J. Bruce Fields02921912010-07-29 15:16:59 -04004310 /* For lock stateid's, we test the parent open, not the lock: */
4311 if (stp->st_openstp)
4312 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004313 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004315 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 goto out;
4317 status = nfs_ok;
4318out:
4319 return status;
4320}
4321
Al Virob37ad282006-10-19 23:28:59 -07004322static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004323check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004325 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004327 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004328 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 * conflicting state; so we must wait out the grace period. */
4330 return nfserr_grace;
4331 } else if (flags & WR_STATE)
4332 return nfs4_share_conflict(current_fh,
4333 NFS4_SHARE_DENY_WRITE);
4334 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4335 return nfs4_share_conflict(current_fh,
4336 NFS4_SHARE_DENY_READ);
4337}
4338
4339/*
4340 * Allow READ/WRITE during grace period on recovered state only for files
4341 * that are not able to provide mandatory locking.
4342 */
4343static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004344grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004346 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347}
4348
J. Bruce Fields81b82962011-08-23 11:03:29 -04004349/* Returns true iff a is later than b: */
4350static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4351{
Jim Rees1a9357f2013-05-17 17:33:00 -04004352 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004353}
4354
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004355static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004356{
Andy Adamson66689582009-04-03 08:28:45 +03004357 /*
4358 * When sessions are used the stateid generation number is ignored
4359 * when it is zero.
4360 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004361 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004362 return nfs_ok;
4363
4364 if (in->si_generation == ref->si_generation)
4365 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004366
J. Bruce Fields0836f582008-01-26 19:08:12 -05004367 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004368 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004369 return nfserr_bad_stateid;
4370 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004371 * However, we could see a stateid from the past, even from a
4372 * non-buggy client. For example, if the client sends a lock
4373 * while some IO is outstanding, the lock may bump si_generation
4374 * while the IO is still in flight. The client could avoid that
4375 * situation by waiting for responses on all the IO requests,
4376 * but better performance may result in retrying IO that
4377 * receives an old_stateid error if requests are rarely
4378 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004379 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004380 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004381}
4382
Chuck Lever7df302f2012-05-29 13:56:37 -04004383static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004384{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004385 struct nfs4_stid *s;
4386 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004387 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004388
Chuck Lever7df302f2012-05-29 13:56:37 -04004389 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004390 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004391 /* Client debugging aid. */
4392 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4393 char addr_str[INET6_ADDRSTRLEN];
4394 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4395 sizeof(addr_str));
4396 pr_warn_ratelimited("NFSD: client %s testing state ID "
4397 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004398 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004399 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004400 spin_lock(&cl->cl_lock);
4401 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004402 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004403 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004404 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004405 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004406 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004407 switch (s->sc_type) {
4408 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004409 status = nfs_ok;
4410 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004411 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004412 status = nfserr_deleg_revoked;
4413 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004414 case NFS4_OPEN_STID:
4415 case NFS4_LOCK_STID:
4416 ols = openlockstateid(s);
4417 if (ols->st_stateowner->so_is_open_owner
4418 && !(openowner(ols->st_stateowner)->oo_flags
4419 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004420 status = nfserr_bad_stateid;
4421 else
4422 status = nfs_ok;
4423 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004424 default:
4425 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004426 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004427 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004428 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004429 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004430 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004431out_unlock:
4432 spin_unlock(&cl->cl_lock);
4433 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004434}
4435
Christoph Hellwigcd61c522014-08-14 08:44:57 +02004436__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004437nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4438 stateid_t *stateid, unsigned char typemask,
4439 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004440{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004441 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004442
4443 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4444 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004445 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004446 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004447 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004448 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004449 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004450 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004451 if (status)
4452 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004453 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004454 if (!*s)
4455 return nfserr_bad_stateid;
4456 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004457}
4458
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459/*
4460* Checks for stateid operations
4461*/
Al Virob37ad282006-10-19 23:28:59 -07004462__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004463nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004464 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004466 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004467 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004469 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004471 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004472 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004473 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 if (filpp)
4476 *filpp = NULL;
4477
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004478 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 return nfserr_grace;
4480
4481 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004482 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004484 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004485 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004486 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004487 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04004488 return status;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004489 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4490 if (status)
4491 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004492 switch (s->sc_type) {
4493 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004494 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004495 status = nfs4_check_delegmode(dp, flags);
4496 if (status)
4497 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004498 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004499 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004500 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004501 WARN_ON_ONCE(1);
4502 status = nfserr_serverfault;
4503 goto out;
4504 }
Trond Myklebustde186432014-07-10 14:07:26 -04004505 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004506 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004507 break;
4508 case NFS4_OPEN_STID:
4509 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004510 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004511 status = nfs4_check_fh(current_fh, stp);
4512 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004514 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004515 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004517 status = nfs4_check_openmode(stp, flags);
4518 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004520 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004521 struct nfs4_file *fp = stp->st_stid.sc_file;
4522
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004523 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004524 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004525 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004526 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004527 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004528 break;
4529 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004530 status = nfserr_bad_stateid;
4531 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 }
4533 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004534 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004535 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004537 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 return status;
4539}
4540
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004541/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004542 * Test if the stateid is valid
4543 */
4544__be32
4545nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4546 struct nfsd4_test_stateid *test_stateid)
4547{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004548 struct nfsd4_test_stateid_id *stateid;
4549 struct nfs4_client *cl = cstate->session->se_client;
4550
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004551 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004552 stateid->ts_id_status =
4553 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004554
Bryan Schumaker17456802011-07-13 10:50:48 -04004555 return nfs_ok;
4556}
4557
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004558__be32
4559nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4560 struct nfsd4_free_stateid *free_stateid)
4561{
4562 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004563 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004564 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004565 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004566 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004567 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004568
Jeff Layton1af71cc2014-07-29 21:34:14 -04004569 spin_lock(&cl->cl_lock);
4570 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004571 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004572 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004573 switch (s->sc_type) {
4574 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004575 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004576 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004577 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004578 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4579 if (ret)
4580 break;
4581 ret = nfserr_locks_held;
4582 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004583 case NFS4_LOCK_STID:
4584 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4585 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004586 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004587 stp = openlockstateid(s);
4588 ret = nfserr_locks_held;
4589 if (check_for_locks(stp->st_stid.sc_file,
4590 lockowner(stp->st_stateowner)))
4591 break;
4592 unhash_lock_stateid(stp);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004593 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004594 nfs4_put_stid(s);
4595 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004596 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004597 case NFS4_REVOKED_DELEG_STID:
4598 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004599 list_del_init(&dp->dl_recall_lru);
4600 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004601 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004602 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004603 goto out;
4604 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004605 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004606out_unlock:
4607 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004608out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004609 return ret;
4610}
4611
NeilBrown4c4cd222005-07-07 17:59:27 -07004612static inline int
4613setlkflg (int type)
4614{
4615 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4616 RD_STATE : WR_STATE;
4617}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004619static __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 -04004620{
4621 struct svc_fh *current_fh = &cstate->current_fh;
4622 struct nfs4_stateowner *sop = stp->st_stateowner;
4623 __be32 status;
4624
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004625 status = nfsd4_check_seqid(cstate, sop, seqid);
4626 if (status)
4627 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004628 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4629 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004630 /*
4631 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004632 * nfserr_replay_me from the previous step, and
4633 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004634 */
4635 return nfserr_bad_stateid;
4636 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4637 if (status)
4638 return status;
4639 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004640}
4641
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642/*
4643 * Checks for sequence id mutating operations.
4644 */
Al Virob37ad282006-10-19 23:28:59 -07004645static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004646nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004647 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004648 struct nfs4_ol_stateid **stpp,
4649 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004651 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004652 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004653 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004655 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4656 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004657
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004659 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004660 if (status)
4661 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004662 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004663 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004665 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004666 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004667 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004668 else
4669 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004670 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004671}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004672
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004673static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4674 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004675{
4676 __be32 status;
4677 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004678 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004680 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004681 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004682 if (status)
4683 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004684 oo = openowner(stp->st_stateowner);
4685 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4686 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004687 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004688 }
4689 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004690 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691}
4692
Al Virob37ad282006-10-19 23:28:59 -07004693__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004694nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004695 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696{
Al Virob37ad282006-10-19 23:28:59 -07004697 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004698 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004699 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004700 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701
Al Viroa6a9f182013-09-16 10:57:01 -04004702 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4703 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004705 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004706 if (status)
4707 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004709 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004710 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004711 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004712 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004713 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004714 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004715 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004716 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004717 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004718 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004719 update_stateid(&stp->st_stid.sc_stateid);
4720 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004721 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004722 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004723
Jeff Layton2a4317c2012-03-21 16:42:43 -04004724 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004725 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004726put_stateid:
4727 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004729 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 return status;
4731}
4732
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004733static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004735 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004736 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004737 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004738 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004739}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004740
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004741static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4742{
4743 switch (to_access) {
4744 case NFS4_SHARE_ACCESS_READ:
4745 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4746 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4747 break;
4748 case NFS4_SHARE_ACCESS_WRITE:
4749 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4750 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4751 break;
4752 case NFS4_SHARE_ACCESS_BOTH:
4753 break;
4754 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004755 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 }
4757}
4758
Al Virob37ad282006-10-19 23:28:59 -07004759__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004760nfsd4_open_downgrade(struct svc_rqst *rqstp,
4761 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004762 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763{
Al Virob37ad282006-10-19 23:28:59 -07004764 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004765 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004766 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767
Al Viroa6a9f182013-09-16 10:57:01 -04004768 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4769 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004771 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004772 if (od->od_deleg_want)
4773 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4774 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004776 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004777 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004778 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004781 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004782 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004784 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004786 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004787 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004789 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004791 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792
Jeff Laytonce0fc432012-05-11 09:45:14 -04004793 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004795 update_stateid(&stp->st_stid.sc_stateid);
4796 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004798put_stateid:
4799 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004801 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 return status;
4803}
4804
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004805static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4806{
Trond Myklebustacf92952014-06-30 11:48:37 -04004807 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytond83017f2014-07-29 21:34:42 -04004808 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004809
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004810 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04004811 spin_lock(&clp->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04004812 unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004813
Jeff Laytond83017f2014-07-29 21:34:42 -04004814 if (clp->cl_minorversion) {
4815 put_ol_stateid_locked(s, &reaplist);
4816 spin_unlock(&clp->cl_lock);
4817 free_ol_stateid_reaplist(&reaplist);
4818 } else {
4819 spin_unlock(&clp->cl_lock);
4820 free_ol_stateid_reaplist(&reaplist);
Jeff Laytond3134b12014-07-29 21:34:32 -04004821 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04004822 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004823}
4824
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825/*
4826 * nfs4_unlock_state() called after encode
4827 */
Al Virob37ad282006-10-19 23:28:59 -07004828__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004829nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004830 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831{
Al Virob37ad282006-10-19 23:28:59 -07004832 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004833 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004834 struct net *net = SVC_NET(rqstp);
4835 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
Al Viroa6a9f182013-09-16 10:57:01 -04004837 dprintk("NFSD: nfsd4_close on file %pd\n",
4838 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004840 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4841 &close->cl_stateid,
4842 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004843 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004844 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004845 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004847 update_stateid(&stp->st_stid.sc_stateid);
4848 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004850 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004851
4852 /* put reference from nfs4_preprocess_seqid_op */
4853 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 return status;
4856}
4857
Al Virob37ad282006-10-19 23:28:59 -07004858__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004859nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4860 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004862 struct nfs4_delegation *dp;
4863 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004864 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004865 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004866 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004868 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004869 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004871 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004872 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004873 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004874 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004875 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004876 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004877 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004878
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004879 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004880put_stateid:
4881 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882out:
4883 return status;
4884}
4885
4886
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888
Benny Halevy87df4de2008-12-15 19:42:03 +02004889static inline u64
4890end_offset(u64 start, u64 len)
4891{
4892 u64 end;
4893
4894 end = start + len;
4895 return end >= start ? end: NFS4_MAX_UINT64;
4896}
4897
4898/* last octet in a range */
4899static inline u64
4900last_byte_offset(u64 start, u64 len)
4901{
4902 u64 end;
4903
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004904 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004905 end = start + len;
4906 return end > start ? end - 1: NFS4_MAX_UINT64;
4907}
4908
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909/*
4910 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4911 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4912 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4913 * locking, this prevents us from being completely protocol-compliant. The
4914 * real solution to this problem is to start using unsigned file offsets in
4915 * the VFS, but this is a very deep change!
4916 */
4917static inline void
4918nfs4_transform_lock_offset(struct file_lock *lock)
4919{
4920 if (lock->fl_start < 0)
4921 lock->fl_start = OFFSET_MAX;
4922 if (lock->fl_end < 0)
4923 lock->fl_end = OFFSET_MAX;
4924}
4925
Kinglong Meeaef95832014-08-22 10:18:44 -04004926static void nfsd4_fl_get_owner(struct file_lock *dst, struct file_lock *src)
4927{
4928 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)src->fl_owner;
4929 dst->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lo->lo_owner));
4930}
4931
4932static void nfsd4_fl_put_owner(struct file_lock *fl)
4933{
4934 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
4935
4936 if (lo) {
4937 nfs4_put_stateowner(&lo->lo_owner);
4938 fl->fl_owner = NULL;
4939 }
4940}
4941
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004942static const struct lock_manager_operations nfsd_posix_mng_ops = {
Kinglong Meeaef95832014-08-22 10:18:44 -04004943 .lm_get_owner = nfsd4_fl_get_owner,
4944 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07004945};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946
4947static inline void
4948nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4949{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004950 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951
NeilBrownd5b90262006-04-10 22:55:22 -07004952 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004953 lo = (struct nfs4_lockowner *) fl->fl_owner;
4954 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4955 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004956 if (!deny->ld_owner.data)
4957 /* We just don't care that much */
4958 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004959 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4960 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004961 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004962nevermind:
4963 deny->ld_owner.len = 0;
4964 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004965 deny->ld_clientid.cl_boot = 0;
4966 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 }
4968 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004969 deny->ld_length = NFS4_MAX_UINT64;
4970 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4972 deny->ld_type = NFS4_READ_LT;
4973 if (fl->fl_type != F_RDLCK)
4974 deny->ld_type = NFS4_WRITE_LT;
4975}
4976
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004977static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004978find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004979 struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004981 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004982 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983
Trond Myklebust0a880a22014-07-30 08:27:10 -04004984 lockdep_assert_held(&clp->cl_lock);
4985
Trond Myklebustd4f04892014-07-29 21:34:36 -04004986 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4987 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004988 if (so->so_is_open_owner)
4989 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04004990 if (same_owner_str(so, owner))
4991 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 }
4993 return NULL;
4994}
4995
Trond Myklebustc58c6612014-07-29 21:34:35 -04004996static struct nfs4_lockowner *
4997find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004998 struct nfs4_client *clp)
Trond Myklebustc58c6612014-07-29 21:34:35 -04004999{
5000 struct nfs4_lockowner *lo;
5001
Trond Myklebustd4f04892014-07-29 21:34:36 -04005002 spin_lock(&clp->cl_lock);
5003 lo = find_lockowner_str_locked(clid, owner, clp);
5004 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005005 return lo;
5006}
5007
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005008static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5009{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005010 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005011}
5012
Jeff Layton6b180f02014-07-29 21:34:26 -04005013static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5014{
5015 struct nfs4_lockowner *lo = lockowner(sop);
5016
5017 kmem_cache_free(lockowner_slab, lo);
5018}
5019
5020static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005021 .so_unhash = nfs4_unhash_lockowner,
5022 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005023};
5024
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025/*
5026 * Alloc a lock owner structure.
5027 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005028 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005030 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005032static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005033alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5034 struct nfs4_ol_stateid *open_stp,
5035 struct nfsd4_lock *lock)
5036{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005037 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005039 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5040 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005042 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5043 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005044 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005045 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005046 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005047 ret = find_lockowner_str_locked(&clp->cl_clientid,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005048 &lock->lk_new_owner, clp);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005049 if (ret == NULL) {
5050 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005051 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005052 ret = lo;
5053 } else
5054 nfs4_free_lockowner(&lo->lo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005055 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005056 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057}
5058
Jeff Layton356a95e2014-07-29 21:34:13 -04005059static void
5060init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5061 struct nfs4_file *fp, struct inode *inode,
5062 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005064 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065
Jeff Layton356a95e2014-07-29 21:34:13 -04005066 lockdep_assert_held(&clp->cl_lock);
5067
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005068 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005069 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005070 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07005071 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005072 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005073 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005074 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005076 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005077 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005078 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005079 spin_lock(&fp->fi_lock);
5080 list_add(&stp->st_perfile, &fp->fi_stateids);
5081 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082}
5083
Jeff Laytonc53530d2014-06-30 11:48:39 -04005084static struct nfs4_ol_stateid *
5085find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5086{
5087 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005088 struct nfs4_client *clp = lo->lo_owner.so_client;
5089
5090 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005091
5092 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005093 if (lst->st_stid.sc_file == fp) {
5094 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005095 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005096 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005097 }
5098 return NULL;
5099}
5100
Jeff Layton356a95e2014-07-29 21:34:13 -04005101static struct nfs4_ol_stateid *
5102find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5103 struct inode *inode, struct nfs4_ol_stateid *ost,
5104 bool *new)
5105{
5106 struct nfs4_stid *ns = NULL;
5107 struct nfs4_ol_stateid *lst;
5108 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5109 struct nfs4_client *clp = oo->oo_owner.so_client;
5110
5111 spin_lock(&clp->cl_lock);
5112 lst = find_lock_stateid(lo, fi);
5113 if (lst == NULL) {
5114 spin_unlock(&clp->cl_lock);
5115 ns = nfs4_alloc_stid(clp, stateid_slab);
5116 if (ns == NULL)
5117 return NULL;
5118
5119 spin_lock(&clp->cl_lock);
5120 lst = find_lock_stateid(lo, fi);
5121 if (likely(!lst)) {
5122 lst = openlockstateid(ns);
5123 init_lock_stateid(lst, lo, fi, inode, ost);
5124 ns = NULL;
5125 *new = true;
5126 }
5127 }
5128 spin_unlock(&clp->cl_lock);
5129 if (ns)
5130 nfs4_put_stid(ns);
5131 return lst;
5132}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005133
NeilBrownfd39ca92005-06-23 22:04:03 -07005134static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135check_lock_length(u64 offset, u64 length)
5136{
Benny Halevy87df4de2008-12-15 19:42:03 +02005137 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 LOFF_OVERFLOW(offset, length)));
5139}
5140
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005141static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005142{
Trond Myklebust11b91642014-07-29 21:34:08 -04005143 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005144
Jeff Layton7214e862014-07-10 14:07:33 -04005145 lockdep_assert_held(&fp->fi_lock);
5146
Jeff Layton82c5ff12012-05-11 09:45:13 -04005147 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005148 return;
Jeff Layton12659652014-07-10 14:07:28 -04005149 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005150 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005151}
5152
Jeff Layton356a95e2014-07-29 21:34:13 -04005153static __be32
5154lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5155 struct nfs4_ol_stateid *ost,
5156 struct nfsd4_lock *lock,
5157 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005158{
Jeff Layton5db1c032014-07-29 21:34:28 -04005159 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005160 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005161 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5162 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005163 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005164 struct nfs4_lockowner *lo;
5165 unsigned int strhashval;
5166
Trond Myklebustd4f04892014-07-29 21:34:36 -04005167 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005168 if (!lo) {
Trond Myklebustd4f04892014-07-29 21:34:36 -04005169 strhashval = ownerstr_hashval(&lock->v.new.owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005170 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5171 if (lo == NULL)
5172 return nfserr_jukebox;
5173 } else {
5174 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005175 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005176 if (!cstate->minorversion &&
5177 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005178 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005179 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005180
Jeff Layton356a95e2014-07-29 21:34:13 -04005181 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005182 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005183 status = nfserr_jukebox;
5184 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005185 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005186 status = nfs_ok;
5187out:
5188 nfs4_put_stateowner(&lo->lo_owner);
5189 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005190}
5191
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192/*
5193 * LOCK operation
5194 */
Al Virob37ad282006-10-19 23:28:59 -07005195__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005196nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005197 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005199 struct nfs4_openowner *open_sop = NULL;
5200 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005201 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005202 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005203 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005204 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005205 struct file_lock *file_lock = NULL;
5206 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005207 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005208 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005209 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005210 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005211 struct net *net = SVC_NET(rqstp);
5212 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213
5214 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5215 (long long) lock->lk_offset,
5216 (long long) lock->lk_length);
5217
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 if (check_lock_length(lock->lk_offset, lock->lk_length))
5219 return nfserr_inval;
5220
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005221 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005222 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005223 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5224 return status;
5225 }
5226
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005228 if (nfsd4_has_session(cstate))
5229 /* See rfc 5661 18.10.3: given clientid is ignored: */
5230 memcpy(&lock->v.new.clientid,
5231 &cstate->session->se_client->cl_clientid,
5232 sizeof(clientid_t));
5233
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005235 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005239 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 lock->lk_new_open_seqid,
5241 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005242 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005243 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005245 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005246 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005247 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005248 &lock->v.new.clientid))
5249 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005250 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005251 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005252 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005253 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005254 lock->lk_old_lock_seqid,
5255 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005256 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005257 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005258 if (status)
5259 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005260 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005262 lkflg = setlkflg(lock->lk_type);
5263 status = nfs4_check_openmode(lock_stp, lkflg);
5264 if (status)
5265 goto out;
5266
NeilBrown0dd395d2005-07-07 17:59:15 -07005267 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005268 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005269 goto out;
5270 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005271 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005272 goto out;
5273
Jeff Layton21179d82012-08-21 08:03:32 -04005274 file_lock = locks_alloc_lock();
5275 if (!file_lock) {
5276 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5277 status = nfserr_jukebox;
5278 goto out;
5279 }
5280
Trond Myklebust11b91642014-07-29 21:34:08 -04005281 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 switch (lock->lk_type) {
5283 case NFS4_READ_LT:
5284 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005285 spin_lock(&fp->fi_lock);
5286 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005287 if (filp)
5288 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005289 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005290 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005291 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 case NFS4_WRITE_LT:
5293 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005294 spin_lock(&fp->fi_lock);
5295 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005296 if (filp)
5297 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005298 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005299 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005300 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 default:
5302 status = nfserr_inval;
5303 goto out;
5304 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005305 if (!filp) {
5306 status = nfserr_openmode;
5307 goto out;
5308 }
Kinglong Meeaef95832014-08-22 10:18:44 -04005309
5310 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04005311 file_lock->fl_pid = current->tgid;
5312 file_lock->fl_file = filp;
5313 file_lock->fl_flags = FL_POSIX;
5314 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5315 file_lock->fl_start = lock->lk_offset;
5316 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5317 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318
Jeff Layton21179d82012-08-21 08:03:32 -04005319 conflock = locks_alloc_lock();
5320 if (!conflock) {
5321 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5322 status = nfserr_jukebox;
5323 goto out;
5324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325
Jeff Layton21179d82012-08-21 08:03:32 -04005326 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005327 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005329 update_stateid(&lock_stp->st_stid.sc_stateid);
5330 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005332 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005333 break;
5334 case (EAGAIN): /* conflock holds conflicting lock */
5335 status = nfserr_denied;
5336 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005337 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005338 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 case (EDEADLK):
5340 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005341 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005342 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005343 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005344 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005345 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347out:
Trond Myklebustde186432014-07-10 14:07:26 -04005348 if (filp)
5349 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005350 if (lock_stp) {
5351 /* Bump seqid manually if the 4.0 replay owner is openowner */
5352 if (cstate->replay_owner &&
5353 cstate->replay_owner != &lock_sop->lo_owner &&
5354 seqid_mutating_err(ntohl(status)))
5355 lock_sop->lo_owner.so_seqid++;
5356
5357 /*
5358 * If this is a new, never-before-used stateid, and we are
5359 * returning an error, then just go ahead and release it.
5360 */
5361 if (status && new)
5362 release_lock_stateid(lock_stp);
5363
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005364 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005365 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005366 if (open_stp)
5367 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005368 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005369 if (file_lock)
5370 locks_free_lock(file_lock);
5371 if (conflock)
5372 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 return status;
5374}
5375
5376/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005377 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5378 * so we do a temporary open here just to get an open file to pass to
5379 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5380 * inode operation.)
5381 */
Al Viro04da6e92012-04-13 00:00:04 -04005382static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005383{
5384 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005385 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5386 if (!err) {
5387 err = nfserrno(vfs_test_lock(file, lock));
5388 nfsd_close(file);
5389 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005390 return err;
5391}
5392
5393/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 * LOCKT operation
5395 */
Al Virob37ad282006-10-19 23:28:59 -07005396__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005397nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5398 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399{
Jeff Layton21179d82012-08-21 08:03:32 -04005400 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005401 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005402 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005403 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005405 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 return nfserr_grace;
5407
5408 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5409 return nfserr_inval;
5410
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005411 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005412 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005413 if (status)
5414 goto out;
5415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005417 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419
Jeff Layton21179d82012-08-21 08:03:32 -04005420 file_lock = locks_alloc_lock();
5421 if (!file_lock) {
5422 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5423 status = nfserr_jukebox;
5424 goto out;
5425 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005426
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 switch (lockt->lt_type) {
5428 case NFS4_READ_LT:
5429 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005430 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 break;
5432 case NFS4_WRITE_LT:
5433 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005434 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 break;
5436 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005437 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 status = nfserr_inval;
5439 goto out;
5440 }
5441
Trond Myklebustd4f04892014-07-29 21:34:36 -04005442 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5443 cstate->clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005444 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005445 file_lock->fl_owner = (fl_owner_t)lo;
5446 file_lock->fl_pid = current->tgid;
5447 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448
Jeff Layton21179d82012-08-21 08:03:32 -04005449 file_lock->fl_start = lockt->lt_offset;
5450 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451
Jeff Layton21179d82012-08-21 08:03:32 -04005452 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453
Jeff Layton21179d82012-08-21 08:03:32 -04005454 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005455 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005456 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005457
Jeff Layton21179d82012-08-21 08:03:32 -04005458 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005460 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 }
5462out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005463 if (lo)
5464 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04005465 if (file_lock)
5466 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 return status;
5468}
5469
Al Virob37ad282006-10-19 23:28:59 -07005470__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005471nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005472 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005474 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005476 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005477 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005478 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005479 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5480
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5482 (long long) locku->lu_offset,
5483 (long long) locku->lu_length);
5484
5485 if (check_lock_length(locku->lu_offset, locku->lu_length))
5486 return nfserr_inval;
5487
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005488 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005489 &locku->lu_stateid, NFS4_LOCK_STID,
5490 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005491 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005493 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005494 if (!filp) {
5495 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005496 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005497 }
Jeff Layton21179d82012-08-21 08:03:32 -04005498 file_lock = locks_alloc_lock();
5499 if (!file_lock) {
5500 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5501 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005502 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005503 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005504
Jeff Layton21179d82012-08-21 08:03:32 -04005505 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04005506 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04005507 file_lock->fl_pid = current->tgid;
5508 file_lock->fl_file = filp;
5509 file_lock->fl_flags = FL_POSIX;
5510 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5511 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512
Jeff Layton21179d82012-08-21 08:03:32 -04005513 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5514 locku->lu_length);
5515 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516
Jeff Layton21179d82012-08-21 08:03:32 -04005517 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005518 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005519 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 goto out_nfserr;
5521 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005522 update_stateid(&stp->st_stid.sc_stateid);
5523 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005524fput:
5525 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005526put_stateid:
5527 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005529 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005530 if (file_lock)
5531 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 return status;
5533
5534out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005535 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005536 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537}
5538
5539/*
5540 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005541 * true: locks held by lockowner
5542 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005544static bool
5545check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005547 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005548 int status = false;
5549 struct file *filp = find_any_file(fp);
5550 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005551 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005552
5553 if (!filp) {
5554 /* Any valid lock stateid should have some sort of access */
5555 WARN_ON_ONCE(1);
5556 return status;
5557 }
5558
5559 inode = file_inode(filp);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005560 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005562 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05005563 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005564 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
5565 if (fl->fl_owner == (fl_owner_t)lowner) {
5566 status = true;
5567 break;
5568 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005569 }
Jeff Layton6109c852015-01-16 15:05:57 -05005570 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 }
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005572 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 return status;
5574}
5575
Al Virob37ad282006-10-19 23:28:59 -07005576__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005577nfsd4_release_lockowner(struct svc_rqst *rqstp,
5578 struct nfsd4_compound_state *cstate,
5579 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580{
5581 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005582 struct nfs4_stateowner *sop;
5583 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005584 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005586 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005587 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005588 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005589 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590
5591 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5592 clid->cl_boot, clid->cl_id);
5593
Jeff Layton4b24ca72014-06-30 11:48:44 -04005594 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005595 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04005596 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005597
Trond Myklebustd4f04892014-07-29 21:34:36 -04005598 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005599 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005600 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005601 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005602 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005603
5604 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005605 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005606
Jeff Layton882e9d22014-07-29 21:34:37 -04005607 /* see if there are still any locks associated with it */
5608 lo = lockowner(sop);
5609 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5610 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5611 status = nfserr_locks_held;
5612 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04005613 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04005614 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005615 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005616
Kinglong Meeb5971af2014-08-22 10:18:43 -04005617 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04005618 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005619 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005620 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005621 if (lo)
5622 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 return status;
5624}
5625
5626static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005627alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628{
NeilBrowna55370a2005-06-23 22:03:52 -07005629 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630}
5631
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005632bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005633nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005634{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005635 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005636
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005637 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005638 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005639}
5640
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641/*
5642 * failure => all reset bets are off, nfserr_no_grace...
5643 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005644struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005645nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646{
5647 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005648 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649
NeilBrowna55370a2005-06-23 22:03:52 -07005650 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5651 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005652 if (crp) {
5653 strhashval = clientstr_hashval(name);
5654 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005655 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005656 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005657 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005658 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005659 }
5660 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661}
5662
Jeff Layton2a4317c2012-03-21 16:42:43 -04005663void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005664nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005665{
5666 list_del(&crp->cr_strhash);
5667 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005668 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005669}
5670
5671void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005672nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673{
5674 struct nfs4_client_reclaim *crp = NULL;
5675 int i;
5676
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005678 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5679 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005681 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 }
5683 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005684 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685}
5686
5687/*
5688 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005689struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005690nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691{
5692 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 struct nfs4_client_reclaim *crp = NULL;
5694
Jeff Layton278c9312012-11-12 15:00:52 -05005695 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696
Jeff Layton278c9312012-11-12 15:00:52 -05005697 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005698 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005699 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 return crp;
5701 }
5702 }
5703 return NULL;
5704}
5705
5706/*
5707* Called from OPEN. Look for clientid in reclaim list.
5708*/
Al Virob37ad282006-10-19 23:28:59 -07005709__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005710nfs4_check_open_reclaim(clientid_t *clid,
5711 struct nfsd4_compound_state *cstate,
5712 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005714 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005715
5716 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005717 status = lookup_clientid(clid, cstate, nn);
5718 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005719 return nfserr_reclaim_bad;
5720
Jeff Layton3b3e7b72014-09-12 16:40:22 -04005721 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
5722 return nfserr_no_grace;
5723
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005724 if (nfsd4_client_record_check(cstate->clp))
5725 return nfserr_reclaim_bad;
5726
5727 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728}
5729
Bryan Schumaker65178db2011-11-01 13:35:21 -04005730#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04005731static inline void
5732put_client(struct nfs4_client *clp)
5733{
5734 atomic_dec(&clp->cl_refcount);
5735}
5736
Jeff Layton285abde2014-07-30 08:27:24 -04005737static struct nfs4_client *
5738nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5739{
5740 struct nfs4_client *clp;
5741 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5742 nfsd_net_id);
5743
5744 if (!nfsd_netns_ready(nn))
5745 return NULL;
5746
5747 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5748 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5749 return clp;
5750 }
5751 return NULL;
5752}
5753
Jeff Layton7ec0e362014-07-30 08:27:17 -04005754u64
Jeff Layton285abde2014-07-30 08:27:24 -04005755nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04005756{
5757 struct nfs4_client *clp;
5758 u64 count = 0;
5759 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5760 nfsd_net_id);
5761 char buf[INET6_ADDRSTRLEN];
5762
5763 if (!nfsd_netns_ready(nn))
5764 return 0;
5765
5766 spin_lock(&nn->client_lock);
5767 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5768 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5769 pr_info("NFS Client: %s\n", buf);
5770 ++count;
5771 }
5772 spin_unlock(&nn->client_lock);
5773
5774 return count;
5775}
Bryan Schumaker65178db2011-11-01 13:35:21 -04005776
Jeff Laytona0926d12014-07-30 08:27:18 -04005777u64
Jeff Layton285abde2014-07-30 08:27:24 -04005778nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04005779{
5780 u64 count = 0;
5781 struct nfs4_client *clp;
5782 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5783 nfsd_net_id);
5784
5785 if (!nfsd_netns_ready(nn))
5786 return count;
5787
5788 spin_lock(&nn->client_lock);
5789 clp = nfsd_find_client(addr, addr_size);
5790 if (clp) {
5791 if (mark_client_expired_locked(clp) == nfs_ok)
5792 ++count;
5793 else
5794 clp = NULL;
5795 }
5796 spin_unlock(&nn->client_lock);
5797
5798 if (clp)
5799 expire_client(clp);
5800
5801 return count;
5802}
5803
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005804u64
Jeff Layton285abde2014-07-30 08:27:24 -04005805nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005806{
5807 u64 count = 0;
5808 struct nfs4_client *clp, *next;
5809 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5810 nfsd_net_id);
5811 LIST_HEAD(reaplist);
5812
5813 if (!nfsd_netns_ready(nn))
5814 return count;
5815
5816 spin_lock(&nn->client_lock);
5817 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5818 if (mark_client_expired_locked(clp) == nfs_ok) {
5819 list_add(&clp->cl_lru, &reaplist);
5820 if (max != 0 && ++count >= max)
5821 break;
5822 }
5823 }
5824 spin_unlock(&nn->client_lock);
5825
5826 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
5827 expire_client(clp);
5828
5829 return count;
5830}
5831
Bryan Schumaker184c1842012-11-29 11:40:44 -05005832static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5833 const char *type)
5834{
5835 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005836 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005837 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5838}
5839
Jeff Layton016200c2014-07-30 08:27:21 -04005840static void
5841nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
5842 struct list_head *collect)
5843{
5844 struct nfs4_client *clp = lst->st_stid.sc_client;
5845 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5846 nfsd_net_id);
5847
5848 if (!collect)
5849 return;
5850
5851 lockdep_assert_held(&nn->client_lock);
5852 atomic_inc(&clp->cl_refcount);
5853 list_add(&lst->st_locks, collect);
5854}
5855
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005856static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04005857 struct list_head *collect,
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005858 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005859{
5860 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005861 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005862 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005863 u64 count = 0;
5864
Jeff Layton016200c2014-07-30 08:27:21 -04005865 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005866 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005867 list_for_each_entry_safe(stp, st_next,
5868 &oop->oo_owner.so_stateids, st_perstateowner) {
5869 list_for_each_entry_safe(lst, lst_next,
5870 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04005871 if (func) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005872 func(lst);
Jeff Layton016200c2014-07-30 08:27:21 -04005873 nfsd_inject_add_lock_to_list(lst,
5874 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04005875 }
Jeff Layton016200c2014-07-30 08:27:21 -04005876 ++count;
5877 /*
5878 * Despite the fact that these functions deal
5879 * with 64-bit integers for "count", we must
5880 * ensure that it doesn't blow up the
5881 * clp->cl_refcount. Throw a warning if we
5882 * start to approach INT_MAX here.
5883 */
5884 WARN_ON_ONCE(count == (INT_MAX / 2));
5885 if (count == max)
5886 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005887 }
5888 }
5889 }
Jeff Layton016200c2014-07-30 08:27:21 -04005890out:
5891 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005892
5893 return count;
5894}
5895
Jeff Layton016200c2014-07-30 08:27:21 -04005896static u64
5897nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
5898 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05005899{
Jeff Layton016200c2014-07-30 08:27:21 -04005900 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005901}
5902
Jeff Layton016200c2014-07-30 08:27:21 -04005903static u64
5904nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05005905{
Jeff Layton016200c2014-07-30 08:27:21 -04005906 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005907 nfsd_print_count(clp, count, "locked files");
5908 return count;
5909}
5910
Jeff Layton016200c2014-07-30 08:27:21 -04005911u64
Jeff Layton285abde2014-07-30 08:27:24 -04005912nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04005913{
5914 struct nfs4_client *clp;
5915 u64 count = 0;
5916 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5917 nfsd_net_id);
5918
5919 if (!nfsd_netns_ready(nn))
5920 return 0;
5921
5922 spin_lock(&nn->client_lock);
5923 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5924 count += nfsd_print_client_locks(clp);
5925 spin_unlock(&nn->client_lock);
5926
5927 return count;
5928}
5929
5930static void
5931nfsd_reap_locks(struct list_head *reaplist)
5932{
5933 struct nfs4_client *clp;
5934 struct nfs4_ol_stateid *stp, *next;
5935
5936 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
5937 list_del_init(&stp->st_locks);
5938 clp = stp->st_stid.sc_client;
5939 nfs4_put_stid(&stp->st_stid);
5940 put_client(clp);
5941 }
5942}
5943
5944u64
Jeff Layton285abde2014-07-30 08:27:24 -04005945nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04005946{
5947 unsigned int count = 0;
5948 struct nfs4_client *clp;
5949 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5950 nfsd_net_id);
5951 LIST_HEAD(reaplist);
5952
5953 if (!nfsd_netns_ready(nn))
5954 return count;
5955
5956 spin_lock(&nn->client_lock);
5957 clp = nfsd_find_client(addr, addr_size);
5958 if (clp)
5959 count = nfsd_collect_client_locks(clp, &reaplist, 0);
5960 spin_unlock(&nn->client_lock);
5961 nfsd_reap_locks(&reaplist);
5962 return count;
5963}
5964
5965u64
Jeff Layton285abde2014-07-30 08:27:24 -04005966nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04005967{
5968 u64 count = 0;
5969 struct nfs4_client *clp;
5970 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5971 nfsd_net_id);
5972 LIST_HEAD(reaplist);
5973
5974 if (!nfsd_netns_ready(nn))
5975 return count;
5976
5977 spin_lock(&nn->client_lock);
5978 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5979 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
5980 if (max != 0 && count >= max)
5981 break;
5982 }
5983 spin_unlock(&nn->client_lock);
5984 nfsd_reap_locks(&reaplist);
5985 return count;
5986}
5987
Jeff Layton82e05ef2014-07-30 08:27:22 -04005988static u64
5989nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
5990 struct list_head *collect,
5991 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005992{
5993 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04005994 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5995 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005996 u64 count = 0;
5997
Jeff Layton82e05ef2014-07-30 08:27:22 -04005998 lockdep_assert_held(&nn->client_lock);
5999
6000 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006001 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04006002 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006003 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04006004 if (collect) {
6005 atomic_inc(&clp->cl_refcount);
6006 list_add(&oop->oo_perclient, collect);
6007 }
6008 }
6009 ++count;
6010 /*
6011 * Despite the fact that these functions deal with
6012 * 64-bit integers for "count", we must ensure that
6013 * it doesn't blow up the clp->cl_refcount. Throw a
6014 * warning if we start to approach INT_MAX here.
6015 */
6016 WARN_ON_ONCE(count == (INT_MAX / 2));
6017 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006018 break;
6019 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04006020 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006021
6022 return count;
6023}
6024
Jeff Layton82e05ef2014-07-30 08:27:22 -04006025static u64
6026nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006027{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006028 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6029
6030 nfsd_print_count(clp, count, "openowners");
6031 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006032}
6033
Jeff Layton82e05ef2014-07-30 08:27:22 -04006034static u64
6035nfsd_collect_client_openowners(struct nfs4_client *clp,
6036 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006037{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006038 return nfsd_foreach_client_openowner(clp, max, collect,
6039 unhash_openowner_locked);
6040}
6041
6042u64
Jeff Layton285abde2014-07-30 08:27:24 -04006043nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006044{
6045 struct nfs4_client *clp;
6046 u64 count = 0;
6047 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6048 nfsd_net_id);
6049
6050 if (!nfsd_netns_ready(nn))
6051 return 0;
6052
6053 spin_lock(&nn->client_lock);
6054 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6055 count += nfsd_print_client_openowners(clp);
6056 spin_unlock(&nn->client_lock);
6057
6058 return count;
6059}
6060
6061static void
6062nfsd_reap_openowners(struct list_head *reaplist)
6063{
6064 struct nfs4_client *clp;
6065 struct nfs4_openowner *oop, *next;
6066
6067 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6068 list_del_init(&oop->oo_perclient);
6069 clp = oop->oo_owner.so_client;
6070 release_openowner(oop);
6071 put_client(clp);
6072 }
6073}
6074
6075u64
Jeff Layton285abde2014-07-30 08:27:24 -04006076nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6077 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006078{
6079 unsigned int count = 0;
6080 struct nfs4_client *clp;
6081 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6082 nfsd_net_id);
6083 LIST_HEAD(reaplist);
6084
6085 if (!nfsd_netns_ready(nn))
6086 return count;
6087
6088 spin_lock(&nn->client_lock);
6089 clp = nfsd_find_client(addr, addr_size);
6090 if (clp)
6091 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6092 spin_unlock(&nn->client_lock);
6093 nfsd_reap_openowners(&reaplist);
6094 return count;
6095}
6096
6097u64
Jeff Layton285abde2014-07-30 08:27:24 -04006098nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006099{
6100 u64 count = 0;
6101 struct nfs4_client *clp;
6102 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6103 nfsd_net_id);
6104 LIST_HEAD(reaplist);
6105
6106 if (!nfsd_netns_ready(nn))
6107 return count;
6108
6109 spin_lock(&nn->client_lock);
6110 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6111 count += nfsd_collect_client_openowners(clp, &reaplist,
6112 max - count);
6113 if (max != 0 && count >= max)
6114 break;
6115 }
6116 spin_unlock(&nn->client_lock);
6117 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006118 return count;
6119}
6120
Bryan Schumaker269de302012-11-29 11:40:42 -05006121static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6122 struct list_head *victims)
6123{
6124 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006125 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6126 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006127 u64 count = 0;
6128
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006129 lockdep_assert_held(&nn->client_lock);
6130
6131 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006132 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006133 if (victims) {
6134 /*
6135 * It's not safe to mess with delegations that have a
6136 * non-zero dl_time. They might have already been broken
6137 * and could be processed by the laundromat outside of
6138 * the state_lock. Just leave them be.
6139 */
6140 if (dp->dl_time != 0)
6141 continue;
6142
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006143 atomic_inc(&clp->cl_refcount);
Jeff Layton42690672014-07-25 07:34:20 -04006144 unhash_delegation_locked(dp);
6145 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006146 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006147 ++count;
6148 /*
6149 * Despite the fact that these functions deal with
6150 * 64-bit integers for "count", we must ensure that
6151 * it doesn't blow up the clp->cl_refcount. Throw a
6152 * warning if we start to approach INT_MAX here.
6153 */
6154 WARN_ON_ONCE(count == (INT_MAX / 2));
6155 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006156 break;
6157 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006158 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006159 return count;
6160}
6161
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006162static u64
6163nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006164{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006165 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006166
6167 nfsd_print_count(clp, count, "delegations");
6168 return count;
6169}
6170
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006171u64
Jeff Layton285abde2014-07-30 08:27:24 -04006172nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006173{
6174 struct nfs4_client *clp;
6175 u64 count = 0;
6176 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6177 nfsd_net_id);
6178
6179 if (!nfsd_netns_ready(nn))
6180 return 0;
6181
6182 spin_lock(&nn->client_lock);
6183 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6184 count += nfsd_print_client_delegations(clp);
6185 spin_unlock(&nn->client_lock);
6186
6187 return count;
6188}
6189
6190static void
6191nfsd_forget_delegations(struct list_head *reaplist)
6192{
6193 struct nfs4_client *clp;
6194 struct nfs4_delegation *dp, *next;
6195
6196 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6197 list_del_init(&dp->dl_recall_lru);
6198 clp = dp->dl_stid.sc_client;
6199 revoke_delegation(dp);
6200 put_client(clp);
6201 }
6202}
6203
6204u64
Jeff Layton285abde2014-07-30 08:27:24 -04006205nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6206 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006207{
6208 u64 count = 0;
6209 struct nfs4_client *clp;
6210 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6211 nfsd_net_id);
6212 LIST_HEAD(reaplist);
6213
6214 if (!nfsd_netns_ready(nn))
6215 return count;
6216
6217 spin_lock(&nn->client_lock);
6218 clp = nfsd_find_client(addr, addr_size);
6219 if (clp)
6220 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6221 spin_unlock(&nn->client_lock);
6222
6223 nfsd_forget_delegations(&reaplist);
6224 return count;
6225}
6226
6227u64
Jeff Layton285abde2014-07-30 08:27:24 -04006228nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006229{
6230 u64 count = 0;
6231 struct nfs4_client *clp;
6232 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6233 nfsd_net_id);
6234 LIST_HEAD(reaplist);
6235
6236 if (!nfsd_netns_ready(nn))
6237 return count;
6238
6239 spin_lock(&nn->client_lock);
6240 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6241 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6242 if (max != 0 && count >= max)
6243 break;
6244 }
6245 spin_unlock(&nn->client_lock);
6246 nfsd_forget_delegations(&reaplist);
6247 return count;
6248}
6249
6250static void
6251nfsd_recall_delegations(struct list_head *reaplist)
6252{
6253 struct nfs4_client *clp;
6254 struct nfs4_delegation *dp, *next;
6255
6256 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6257 list_del_init(&dp->dl_recall_lru);
6258 clp = dp->dl_stid.sc_client;
6259 /*
6260 * We skipped all entries that had a zero dl_time before,
6261 * so we can now reset the dl_time back to 0. If a delegation
6262 * break comes in now, then it won't make any difference since
6263 * we're recalling it either way.
6264 */
6265 spin_lock(&state_lock);
6266 dp->dl_time = 0;
6267 spin_unlock(&state_lock);
6268 nfsd_break_one_deleg(dp);
6269 put_client(clp);
6270 }
6271}
6272
6273u64
Jeff Layton285abde2014-07-30 08:27:24 -04006274nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006275 size_t addr_size)
6276{
6277 u64 count = 0;
6278 struct nfs4_client *clp;
6279 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6280 nfsd_net_id);
6281 LIST_HEAD(reaplist);
6282
6283 if (!nfsd_netns_ready(nn))
6284 return count;
6285
6286 spin_lock(&nn->client_lock);
6287 clp = nfsd_find_client(addr, addr_size);
6288 if (clp)
6289 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6290 spin_unlock(&nn->client_lock);
6291
6292 nfsd_recall_delegations(&reaplist);
6293 return count;
6294}
6295
6296u64
Jeff Layton285abde2014-07-30 08:27:24 -04006297nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006298{
6299 u64 count = 0;
6300 struct nfs4_client *clp, *next;
6301 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6302 nfsd_net_id);
6303 LIST_HEAD(reaplist);
6304
6305 if (!nfsd_netns_ready(nn))
6306 return count;
6307
6308 spin_lock(&nn->client_lock);
6309 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6310 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6311 if (max != 0 && ++count >= max)
6312 break;
6313 }
6314 spin_unlock(&nn->client_lock);
6315 nfsd_recall_delegations(&reaplist);
6316 return count;
6317}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006318#endif /* CONFIG_NFSD_FAULT_INJECTION */
6319
Meelap Shahc2f1a552007-07-17 04:04:39 -07006320/*
6321 * Since the lifetime of a delegation isn't limited to that of an open, a
6322 * client may quite reasonably hang on to a delegation as long as it has
6323 * the inode cached. This becomes an obvious problem the first time a
6324 * client's inode cache approaches the size of the server's total memory.
6325 *
6326 * For now we avoid this problem by imposing a hard limit on the number
6327 * of delegations, which varies according to the server's memory size.
6328 */
6329static void
6330set_max_delegations(void)
6331{
6332 /*
6333 * Allow at most 4 delegations per megabyte of RAM. Quick
6334 * estimates suggest that in the worst case (where every delegation
6335 * is for a different inode), a delegation could take about 1.5K,
6336 * giving a worst case usage of about 6% of memory.
6337 */
6338 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6339}
6340
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006341static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006342{
6343 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6344 int i;
6345
6346 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6347 CLIENT_HASH_SIZE, GFP_KERNEL);
6348 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006349 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006350 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6351 CLIENT_HASH_SIZE, GFP_KERNEL);
6352 if (!nn->unconf_id_hashtbl)
6353 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006354 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6355 SESSION_HASH_SIZE, GFP_KERNEL);
6356 if (!nn->sessionid_hashtbl)
6357 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006358
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006359 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006360 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006361 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006362 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006363 for (i = 0; i < SESSION_HASH_SIZE; i++)
6364 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006365 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006366 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03006367 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03006368 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006369 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03006370 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006371
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006372 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006373 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006374
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006375 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006376
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006377err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03006378 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006379err_unconf_id:
6380 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006381err:
6382 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006383}
6384
6385static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006386nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006387{
6388 int i;
6389 struct nfs4_client *clp = NULL;
6390 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6391
6392 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6393 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6394 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6395 destroy_client(clp);
6396 }
6397 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006398
Kinglong Mee2b905632014-03-26 22:09:30 +08006399 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6400 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6401 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6402 destroy_client(clp);
6403 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006404 }
6405
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006406 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006407 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006408 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006409 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006410}
6411
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006412int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006413nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006414{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006415 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006416 int ret;
6417
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006418 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006419 if (ret)
6420 return ret;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006421 nn->boot_time = get_seconds();
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006422 nn->grace_ended = false;
Jeff Laytond4318ac2014-09-12 16:40:21 -04006423 locks_start_grace(net, &nn->nfsd4_manager);
6424 nfsd4_client_tracking_init(net);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006425 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006426 nn->nfsd4_grace, net);
6427 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006428 return 0;
6429}
6430
6431/* initialization to perform when the nfsd service is started: */
6432
6433int
6434nfs4_state_start(void)
6435{
6436 int ret;
6437
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006438 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006439 if (ret)
6440 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07006441 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05006442 if (laundry_wq == NULL) {
6443 ret = -ENOMEM;
6444 goto out_recovery;
6445 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006446 ret = nfsd4_create_callback_queue();
6447 if (ret)
6448 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006449
Meelap Shahc2f1a552007-07-17 04:04:39 -07006450 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006451
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006452 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006453
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006454out_free_laundry:
6455 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006456out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006457 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458}
6459
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006460void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006461nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006465 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006467 cancel_delayed_work_sync(&nn->laundromat_work);
6468 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006469
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04006471 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006472 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006474 unhash_delegation_locked(dp);
6475 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476 }
Benny Halevycdc97502014-05-30 09:09:30 -04006477 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478 list_for_each_safe(pos, next, &reaplist) {
6479 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006480 list_del_init(&dp->dl_recall_lru);
Jeff Laytonafbda402014-08-09 10:22:41 -04006481 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04006482 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483 }
6484
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006485 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006486 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487}
6488
6489void
6490nfs4_state_shutdown(void)
6491{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006492 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006493 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006495
6496static void
6497get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6498{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006499 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6500 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006501}
6502
6503static void
6504put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6505{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006506 if (cstate->minorversion) {
6507 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6508 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6509 }
6510}
6511
6512void
6513clear_current_stateid(struct nfsd4_compound_state *cstate)
6514{
6515 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006516}
6517
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006518/*
6519 * functions to set current state id
6520 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006521void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006522nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6523{
6524 put_stateid(cstate, &odp->od_stateid);
6525}
6526
6527void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006528nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6529{
6530 put_stateid(cstate, &open->op_stateid);
6531}
6532
6533void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006534nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6535{
6536 put_stateid(cstate, &close->cl_stateid);
6537}
6538
6539void
6540nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6541{
6542 put_stateid(cstate, &lock->lk_resp_stateid);
6543}
6544
6545/*
6546 * functions to consume current state id
6547 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006548
6549void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006550nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6551{
6552 get_stateid(cstate, &odp->od_stateid);
6553}
6554
6555void
6556nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6557{
6558 get_stateid(cstate, &drp->dr_stateid);
6559}
6560
6561void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006562nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6563{
6564 get_stateid(cstate, &fsp->fr_stateid);
6565}
6566
6567void
6568nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6569{
6570 get_stateid(cstate, &setattr->sa_stateid);
6571}
6572
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006573void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006574nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6575{
6576 get_stateid(cstate, &close->cl_stateid);
6577}
6578
6579void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006580nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006581{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006582 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006583}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006584
6585void
6586nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6587{
6588 get_stateid(cstate, &read->rd_stateid);
6589}
6590
6591void
6592nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6593{
6594 get_stateid(cstate, &write->wr_stateid);
6595}