blob: 08746ec1d44a58e43b25eebec73293aa7799d06a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Chuck Lever7df302f2012-05-29 13:56:37 -040041#include <linux/ratelimit.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050042#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton59766872013-02-04 12:50:00 -050043#include <linux/sunrpc/addr.h>
Daniel Borkmann87545892014-12-10 16:33:11 +010044#include <linux/jhash.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020045#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040046#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050047#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040048#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040050#include "netns.h"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020051#include "pnfs.h"
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define NFSDDBG_FACILITY NFSDDBG_PROC
54
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050055#define all_ones {{~0,~0},~0}
56static const stateid_t one_stateid = {
57 .si_generation = ~0,
58 .si_opaque = all_ones,
59};
60static const stateid_t zero_stateid = {
61 /* all fields zero */
62};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010063static const stateid_t currentstateid = {
64 .si_generation = 1,
65};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050066
Andy Adamsonec6b5d72009-04-03 08:28:28 +030067static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070068
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050069#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
70#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010071#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040074static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040075static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
J. Bruce Fields8b671b82009-02-22 14:51:34 -080077/* Locking: */
78
J. Bruce Fields8b671b82009-02-22 14:51:34 -080079/*
80 * Currently used for the del_recall_lru and file hash table. In an
81 * effort to decrease the scope of the client_mutex, this spinlock may
82 * eventually cover more:
83 */
Benny Halevycdc97502014-05-30 09:09:30 -040084static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080085
Jeff Laytonb401be222014-07-29 21:34:33 -040086/*
87 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
88 * the refcount on the open stateid to drop.
89 */
90static DECLARE_WAIT_QUEUE_HEAD(close_wq);
91
Christoph Hellwigabf11352014-05-21 07:43:03 -070092static struct kmem_cache *openowner_slab;
93static struct kmem_cache *lockowner_slab;
94static struct kmem_cache *file_slab;
95static struct kmem_cache *stateid_slab;
96static struct kmem_cache *deleg_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +020097static struct kmem_cache *odstate_slab;
NeilBrowne60d4392005-06-23 22:03:01 -070098
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -040099static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800100
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200101static struct nfsd4_callback_ops nfsd4_cb_recall_ops;
102
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400103static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800104{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400105 return ses->se_flags & NFS4_SESSION_DEAD;
106}
107
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400108static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
109{
110 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400111 return nfserr_jukebox;
112 ses->se_flags |= NFS4_SESSION_DEAD;
113 return nfs_ok;
114}
115
J. Bruce Fields221a6872013-04-01 22:23:49 -0400116static bool is_client_expired(struct nfs4_client *clp)
117{
118 return clp->cl_time == 0;
119}
120
J. Bruce Fields221a6872013-04-01 22:23:49 -0400121static __be32 get_client_locked(struct nfs4_client *clp)
122{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400123 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
124
125 lockdep_assert_held(&nn->client_lock);
126
J. Bruce Fields221a6872013-04-01 22:23:49 -0400127 if (is_client_expired(clp))
128 return nfserr_expired;
129 atomic_inc(&clp->cl_refcount);
130 return nfs_ok;
131}
132
133/* must be called under the client_lock */
134static inline void
135renew_client_locked(struct nfs4_client *clp)
136{
137 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
138
139 if (is_client_expired(clp)) {
140 WARN_ON(1);
141 printk("%s: client (clientid %08x/%08x) already expired\n",
142 __func__,
143 clp->cl_clientid.cl_boot,
144 clp->cl_clientid.cl_id);
145 return;
146 }
147
148 dprintk("renewing client (clientid %08x/%08x)\n",
149 clp->cl_clientid.cl_boot,
150 clp->cl_clientid.cl_id);
151 list_move_tail(&clp->cl_lru, &nn->client_lru);
152 clp->cl_time = get_seconds();
153}
154
Fengguang Wuba138432013-04-16 22:14:15 -0400155static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400156{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400157 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
158
159 lockdep_assert_held(&nn->client_lock);
160
J. Bruce Fields221a6872013-04-01 22:23:49 -0400161 if (!atomic_dec_and_test(&clp->cl_refcount))
162 return;
163 if (!is_client_expired(clp))
164 renew_client_locked(clp);
165}
166
Jeff Layton4b24ca72014-06-30 11:48:44 -0400167static void put_client_renew(struct nfs4_client *clp)
168{
169 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
170
Jeff Laytond6c249b2014-07-08 14:02:50 -0400171 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
172 return;
173 if (!is_client_expired(clp))
174 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400175 spin_unlock(&nn->client_lock);
176}
177
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400178static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
179{
180 __be32 status;
181
182 if (is_session_dead(ses))
183 return nfserr_badsession;
184 status = get_client_locked(ses->se_client);
185 if (status)
186 return status;
187 atomic_inc(&ses->se_ref);
188 return nfs_ok;
189}
190
191static void nfsd4_put_session_locked(struct nfsd4_session *ses)
192{
193 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400194 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
195
196 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400197
198 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
199 free_session(ses);
200 put_client_renew_locked(clp);
201}
202
203static void nfsd4_put_session(struct nfsd4_session *ses)
204{
205 struct nfs4_client *clp = ses->se_client;
206 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
207
208 spin_lock(&nn->client_lock);
209 nfsd4_put_session_locked(ses);
210 spin_unlock(&nn->client_lock);
211}
212
Kinglong Meeb5971af2014-08-22 10:18:43 -0400213static inline struct nfs4_stateowner *
214nfs4_get_stateowner(struct nfs4_stateowner *sop)
215{
216 atomic_inc(&sop->so_count);
217 return sop;
218}
219
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400220static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400221same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400222{
223 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400224 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400225}
226
227static struct nfs4_openowner *
228find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400229 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400230{
231 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400232
Trond Myklebustd4f04892014-07-29 21:34:36 -0400233 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400234
Trond Myklebustd4f04892014-07-29 21:34:36 -0400235 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
236 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400237 if (!so->so_is_open_owner)
238 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400239 if (same_owner_str(so, &open->op_owner))
240 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400241 }
242 return NULL;
243}
244
245static struct nfs4_openowner *
246find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400247 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400248{
249 struct nfs4_openowner *oo;
250
Trond Myklebustd4f04892014-07-29 21:34:36 -0400251 spin_lock(&clp->cl_lock);
252 oo = find_openstateowner_str_locked(hashval, open, clp);
253 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400254 return oo;
255}
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257static inline u32
258opaque_hashval(const void *ptr, int nbytes)
259{
260 unsigned char *cptr = (unsigned char *) ptr;
261
262 u32 x = 0;
263 while (nbytes--) {
264 x *= 37;
265 x += *cptr++;
266 }
267 return x;
268}
269
Jeff Layton5b095e92014-10-23 08:01:02 -0400270static void nfsd4_free_file_rcu(struct rcu_head *rcu)
J. Bruce Fields32513b42011-10-13 16:00:16 -0400271{
Jeff Layton5b095e92014-10-23 08:01:02 -0400272 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
273
274 kmem_cache_free(file_slab, fp);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400275}
276
Christoph Hellwige6ba76e2014-08-14 08:50:16 +0200277void
NeilBrown13cd2182005-06-23 22:03:10 -0700278put_nfs4_file(struct nfs4_file *fi)
279{
Jeff Layton02e12152014-07-16 10:31:57 -0400280 might_lock(&state_lock);
281
Benny Halevycdc97502014-05-30 09:09:30 -0400282 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton5b095e92014-10-23 08:01:02 -0400283 hlist_del_rcu(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400284 spin_unlock(&state_lock);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200285 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
Jeff Layton5b095e92014-10-23 08:01:02 -0400286 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
287 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800288 }
NeilBrown13cd2182005-06-23 22:03:10 -0700289}
290
Trond Myklebustde186432014-07-10 14:07:26 -0400291static struct file *
292__nfs4_get_fd(struct nfs4_file *f, int oflag)
293{
294 if (f->fi_fds[oflag])
295 return get_file(f->fi_fds[oflag]);
296 return NULL;
297}
298
299static struct file *
300find_writeable_file_locked(struct nfs4_file *f)
301{
302 struct file *ret;
303
304 lockdep_assert_held(&f->fi_lock);
305
306 ret = __nfs4_get_fd(f, O_WRONLY);
307 if (!ret)
308 ret = __nfs4_get_fd(f, O_RDWR);
309 return ret;
310}
311
312static struct file *
313find_writeable_file(struct nfs4_file *f)
314{
315 struct file *ret;
316
317 spin_lock(&f->fi_lock);
318 ret = find_writeable_file_locked(f);
319 spin_unlock(&f->fi_lock);
320
321 return ret;
322}
323
324static struct file *find_readable_file_locked(struct nfs4_file *f)
325{
326 struct file *ret;
327
328 lockdep_assert_held(&f->fi_lock);
329
330 ret = __nfs4_get_fd(f, O_RDONLY);
331 if (!ret)
332 ret = __nfs4_get_fd(f, O_RDWR);
333 return ret;
334}
335
336static struct file *
337find_readable_file(struct nfs4_file *f)
338{
339 struct file *ret;
340
341 spin_lock(&f->fi_lock);
342 ret = find_readable_file_locked(f);
343 spin_unlock(&f->fi_lock);
344
345 return ret;
346}
347
Christoph Hellwig4d227fc2014-08-17 07:40:00 -0500348struct file *
Trond Myklebustde186432014-07-10 14:07:26 -0400349find_any_file(struct nfs4_file *f)
350{
351 struct file *ret;
352
353 spin_lock(&f->fi_lock);
354 ret = __nfs4_get_fd(f, O_RDWR);
355 if (!ret) {
356 ret = __nfs4_get_fd(f, O_WRONLY);
357 if (!ret)
358 ret = __nfs4_get_fd(f, O_RDONLY);
359 }
360 spin_unlock(&f->fi_lock);
361 return ret;
362}
363
Trond Myklebust02a35082014-07-25 07:34:22 -0400364static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800365unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700366
367/*
368 * Open owner state (share locks)
369 */
370
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500371/* hash tables for lock and open owners */
372#define OWNER_HASH_BITS 8
373#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
374#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700375
Trond Myklebustd4f04892014-07-29 21:34:36 -0400376static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400377{
378 unsigned int ret;
379
380 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500381 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400382}
NeilBrownef0f3392006-04-10 22:55:41 -0700383
NeilBrownef0f3392006-04-10 22:55:41 -0700384/* hash table for nfs4_file */
385#define FILE_HASH_BITS 8
386#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800387
Trond Myklebustca943212014-07-23 16:17:39 -0400388static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400389{
Trond Myklebustca943212014-07-23 16:17:39 -0400390 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
391}
392
393static unsigned int file_hashval(struct knfsd_fh *fh)
394{
395 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
396}
397
Jeff Layton89876f82013-04-02 09:01:59 -0400398static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700399
Jeff Layton12659652014-07-10 14:07:28 -0400400static void
401__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400402{
Jeff Layton7214e862014-07-10 14:07:33 -0400403 lockdep_assert_held(&fp->fi_lock);
404
Jeff Layton12659652014-07-10 14:07:28 -0400405 if (access & NFS4_SHARE_ACCESS_WRITE)
406 atomic_inc(&fp->fi_access[O_WRONLY]);
407 if (access & NFS4_SHARE_ACCESS_READ)
408 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400409}
410
Jeff Layton12659652014-07-10 14:07:28 -0400411static __be32
412nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400413{
Jeff Layton7214e862014-07-10 14:07:33 -0400414 lockdep_assert_held(&fp->fi_lock);
415
Jeff Layton12659652014-07-10 14:07:28 -0400416 /* Does this access mode make sense? */
417 if (access & ~NFS4_SHARE_ACCESS_BOTH)
418 return nfserr_inval;
419
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400420 /* Does it conflict with a deny mode already set? */
421 if ((access & fp->fi_share_deny) != 0)
422 return nfserr_share_denied;
423
Jeff Layton12659652014-07-10 14:07:28 -0400424 __nfs4_file_get_access(fp, access);
425 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400426}
427
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400428static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
429{
430 /* Common case is that there is no deny mode. */
431 if (deny) {
432 /* Does this deny mode make sense? */
433 if (deny & ~NFS4_SHARE_DENY_BOTH)
434 return nfserr_inval;
435
436 if ((deny & NFS4_SHARE_DENY_READ) &&
437 atomic_read(&fp->fi_access[O_RDONLY]))
438 return nfserr_share_denied;
439
440 if ((deny & NFS4_SHARE_DENY_WRITE) &&
441 atomic_read(&fp->fi_access[O_WRONLY]))
442 return nfserr_share_denied;
443 }
444 return nfs_ok;
445}
446
J. Bruce Fields998db522010-08-07 09:21:41 -0400447static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400448{
Trond Myklebustde186432014-07-10 14:07:26 -0400449 might_lock(&fp->fi_lock);
450
451 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
452 struct file *f1 = NULL;
453 struct file *f2 = NULL;
454
Jeff Layton6d338b52014-07-10 14:07:29 -0400455 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400456 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400457 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400458 spin_unlock(&fp->fi_lock);
459 if (f1)
460 fput(f1);
461 if (f2)
462 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400463 }
464}
465
Jeff Layton12659652014-07-10 14:07:28 -0400466static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400467{
Jeff Layton12659652014-07-10 14:07:28 -0400468 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
469
470 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400471 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400472 if (access & NFS4_SHARE_ACCESS_READ)
473 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400474}
475
Sachin Bhamare8287f002015-04-27 14:50:14 +0200476/*
477 * Allocate a new open/delegation state counter. This is needed for
478 * pNFS for proper return on close semantics.
479 *
480 * Note that we only allocate it for pNFS-enabled exports, otherwise
481 * all pointers to struct nfs4_clnt_odstate are always NULL.
482 */
483static struct nfs4_clnt_odstate *
484alloc_clnt_odstate(struct nfs4_client *clp)
485{
486 struct nfs4_clnt_odstate *co;
487
488 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
489 if (co) {
490 co->co_client = clp;
491 atomic_set(&co->co_odcount, 1);
492 }
493 return co;
494}
495
496static void
497hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
498{
499 struct nfs4_file *fp = co->co_file;
500
501 lockdep_assert_held(&fp->fi_lock);
502 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
503}
504
505static inline void
506get_clnt_odstate(struct nfs4_clnt_odstate *co)
507{
508 if (co)
509 atomic_inc(&co->co_odcount);
510}
511
512static void
513put_clnt_odstate(struct nfs4_clnt_odstate *co)
514{
515 struct nfs4_file *fp;
516
517 if (!co)
518 return;
519
520 fp = co->co_file;
521 if (atomic_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
522 list_del(&co->co_perfile);
523 spin_unlock(&fp->fi_lock);
524
525 nfsd4_return_all_file_layouts(co->co_client, fp);
526 kmem_cache_free(odstate_slab, co);
527 }
528}
529
530static struct nfs4_clnt_odstate *
531find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
532{
533 struct nfs4_clnt_odstate *co;
534 struct nfs4_client *cl;
535
536 if (!new)
537 return NULL;
538
539 cl = new->co_client;
540
541 spin_lock(&fp->fi_lock);
542 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
543 if (co->co_client == cl) {
544 get_clnt_odstate(co);
545 goto out;
546 }
547 }
548 co = new;
549 co->co_file = fp;
550 hash_clnt_odstate_locked(new);
551out:
552 spin_unlock(&fp->fi_lock);
553 return co;
554}
555
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200556struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
Trond Myklebust60116952014-07-29 21:34:06 -0400557 struct kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400558{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500559 struct nfs4_stid *stid;
560 int new_id;
561
Trond Myklebustf8338832014-07-25 07:34:19 -0400562 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500563 if (!stid)
564 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400565
Jeff Layton4770d722014-07-29 21:34:10 -0400566 idr_preload(GFP_KERNEL);
567 spin_lock(&cl->cl_lock);
568 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
569 spin_unlock(&cl->cl_lock);
570 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700571 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500572 goto out_free;
573 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500574 stid->sc_stateid.si_opaque.so_id = new_id;
575 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
576 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400577 atomic_set(&stid->sc_count, 1);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500578
J. Bruce Fields996e0932011-10-17 11:14:48 -0400579 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500580 * It shouldn't be a problem to reuse an opaque stateid value.
581 * I don't think it is for 4.1. But with 4.0 I worry that, for
582 * example, a stray write retransmission could be accepted by
583 * the server when it should have been rejected. Therefore,
584 * adopt a trick from the sctp code to attempt to maximize the
585 * amount of time until an id is reused, by ensuring they always
586 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400587 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500588 return stid;
589out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800590 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500591 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400592}
593
Jeff Laytonb49e0842014-07-29 21:34:11 -0400594static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400595{
Trond Myklebust60116952014-07-29 21:34:06 -0400596 struct nfs4_stid *stid;
597 struct nfs4_ol_stateid *stp;
598
599 stid = nfs4_alloc_stid(clp, stateid_slab);
600 if (!stid)
601 return NULL;
602
603 stp = openlockstateid(stid);
604 stp->st_stid.sc_free = nfs4_free_ol_stateid;
605 return stp;
606}
607
608static void nfs4_free_deleg(struct nfs4_stid *stid)
609{
Trond Myklebust60116952014-07-29 21:34:06 -0400610 kmem_cache_free(deleg_slab, stid);
611 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400612}
613
NeilBrown6282cd52014-06-04 17:39:26 +1000614/*
615 * When we recall a delegation, we should be careful not to hand it
616 * out again straight away.
617 * To ensure this we keep a pair of bloom filters ('new' and 'old')
618 * in which the filehandles of recalled delegations are "stored".
619 * If a filehandle appear in either filter, a delegation is blocked.
620 * When a delegation is recalled, the filehandle is stored in the "new"
621 * filter.
622 * Every 30 seconds we swap the filters and clear the "new" one,
623 * unless both are empty of course.
624 *
625 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
626 * low 3 bytes as hash-table indices.
627 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400628 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000629 * is used to manage concurrent access. Testing does not need the lock
630 * except when swapping the two filters.
631 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400632static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000633static struct bloom_pair {
634 int entries, old_entries;
635 time_t swap_time;
636 int new; /* index into 'set' */
637 DECLARE_BITMAP(set[2], 256);
638} blocked_delegations;
639
640static int delegation_blocked(struct knfsd_fh *fh)
641{
642 u32 hash;
643 struct bloom_pair *bd = &blocked_delegations;
644
645 if (bd->entries == 0)
646 return 0;
647 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400648 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000649 if (seconds_since_boot() - bd->swap_time > 30) {
650 bd->entries -= bd->old_entries;
651 bd->old_entries = bd->entries;
652 memset(bd->set[bd->new], 0,
653 sizeof(bd->set[0]));
654 bd->new = 1-bd->new;
655 bd->swap_time = seconds_since_boot();
656 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400657 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000658 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100659 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000660 if (test_bit(hash&255, bd->set[0]) &&
661 test_bit((hash>>8)&255, bd->set[0]) &&
662 test_bit((hash>>16)&255, bd->set[0]))
663 return 1;
664
665 if (test_bit(hash&255, bd->set[1]) &&
666 test_bit((hash>>8)&255, bd->set[1]) &&
667 test_bit((hash>>16)&255, bd->set[1]))
668 return 1;
669
670 return 0;
671}
672
673static void block_delegations(struct knfsd_fh *fh)
674{
675 u32 hash;
676 struct bloom_pair *bd = &blocked_delegations;
677
Daniel Borkmann87545892014-12-10 16:33:11 +0100678 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000679
Jeff Laytonf54fe962014-07-25 07:34:26 -0400680 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000681 __set_bit(hash&255, bd->set[bd->new]);
682 __set_bit((hash>>8)&255, bd->set[bd->new]);
683 __set_bit((hash>>16)&255, bd->set[bd->new]);
684 if (bd->entries == 0)
685 bd->swap_time = seconds_since_boot();
686 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400687 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000688}
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690static struct nfs4_delegation *
Sachin Bhamare8287f002015-04-27 14:50:14 +0200691alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh,
692 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400695 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400698 n = atomic_long_inc_return(&num_delegations);
699 if (n < 0 || n > max_delegations)
700 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000701 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400702 goto out_dec;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400703 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700704 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400705 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400706
707 dp->dl_stid.sc_free = nfs4_free_deleg;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400708 /*
709 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400710 * meaning of seqid 0 isn't meaningful, really, but let's avoid
711 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400712 */
713 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700714 INIT_LIST_HEAD(&dp->dl_perfile);
715 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200717 dp->dl_clnt_odstate = odstate;
718 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400719 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200720 dp->dl_retries = 1;
721 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200722 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400724out_dec:
725 atomic_long_dec(&num_delegations);
726 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
729void
Trond Myklebust60116952014-07-29 21:34:06 -0400730nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Trond Myklebust11b91642014-07-29 21:34:08 -0400732 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400733 struct nfs4_client *clp = s->sc_client;
734
Jeff Layton4770d722014-07-29 21:34:10 -0400735 might_lock(&clp->cl_lock);
736
Jeff Laytonb401be222014-07-29 21:34:33 -0400737 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
738 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400739 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400740 }
Trond Myklebust60116952014-07-29 21:34:06 -0400741 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400742 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400743 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400744 if (fp)
745 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500748static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Jeff Layton6bcc0342014-08-09 10:22:40 -0400750 struct file *filp = NULL;
Jeff Layton417c6622014-07-21 09:34:57 -0400751
Jeff Layton6bcc0342014-08-09 10:22:40 -0400752 spin_lock(&fp->fi_lock);
Jeff Layton67db1032014-12-13 09:11:40 -0500753 if (fp->fi_deleg_file && --fp->fi_delegees == 0)
Jeff Layton6bcc0342014-08-09 10:22:40 -0400754 swap(filp, fp->fi_deleg_file);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400755 spin_unlock(&fp->fi_lock);
756
757 if (filp) {
Christoph Hellwig2ab99ee2015-01-21 19:14:02 +0100758 vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400759 fput(filp);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200763void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400764{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500765 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400766}
767
Benny Halevy931ee562014-05-30 09:09:27 -0400768static void
769hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
770{
Benny Halevycdc97502014-05-30 09:09:30 -0400771 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400772 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400773
Trond Myklebust67cb1272014-07-29 21:34:17 -0400774 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400775 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400776 list_add(&dp->dl_perfile, &fp->fi_delegations);
777 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
778}
779
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400780static bool
Jeff Layton42690672014-07-25 07:34:20 -0400781unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Trond Myklebust11b91642014-07-29 21:34:08 -0400783 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400784
Jeff Layton42690672014-07-25 07:34:20 -0400785 lockdep_assert_held(&state_lock);
786
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400787 if (list_empty(&dp->dl_perfile))
788 return false;
789
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400790 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400791 /* Ensure that deleg break won't try to requeue it */
792 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400793 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400794 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400796 list_del_init(&dp->dl_perfile);
797 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400798 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400799}
800
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400801static void destroy_delegation(struct nfs4_delegation *dp)
802{
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400803 bool unhashed;
804
Jeff Layton42690672014-07-25 07:34:20 -0400805 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400806 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -0400807 spin_unlock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400808 if (unhashed) {
809 put_clnt_odstate(dp->dl_clnt_odstate);
810 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
811 nfs4_put_stid(&dp->dl_stid);
812 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400813}
814
815static void revoke_delegation(struct nfs4_delegation *dp)
816{
817 struct nfs4_client *clp = dp->dl_stid.sc_client;
818
Jeff Layton2d4a5322014-07-25 07:34:21 -0400819 WARN_ON(!list_empty(&dp->dl_recall_lru));
820
Sachin Bhamare8287f002015-04-27 14:50:14 +0200821 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -0400822 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
823
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400824 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400825 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400826 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400827 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400828 spin_lock(&clp->cl_lock);
829 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
830 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400831 }
832}
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834/*
835 * SETCLIENTID state
836 */
837
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400838static unsigned int clientid_hashval(u32 id)
839{
840 return id & CLIENT_HASH_MASK;
841}
842
843static unsigned int clientstr_hashval(const char *name)
844{
845 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
846}
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400849 * We store the NONE, READ, WRITE, and BOTH bits separately in the
850 * st_{access,deny}_bmap field of the stateid, in order to track not
851 * only what share bits are currently in force, but also what
852 * combinations of share bits previous opens have used. This allows us
853 * to enforce the recommendation of rfc 3530 14.2.19 that the server
854 * return an error if the client attempt to downgrade to a combination
855 * of share bits not explicable by closing some of its previous opens.
856 *
857 * XXX: This enforcement is actually incomplete, since we don't keep
858 * track of access/deny bit combinations; so, e.g., we allow:
859 *
860 * OPEN allow read, deny write
861 * OPEN allow both, deny none
862 * DOWNGRADE allow read, deny none
863 *
864 * which we should reject.
865 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400866static unsigned int
867bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400868 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400869 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400870
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400871 for (i = 1; i < 4; i++) {
872 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400873 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400874 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400875 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400876}
877
Jeff Layton82c5ff12012-05-11 09:45:13 -0400878/* set share access for a given stateid */
879static inline void
880set_access(u32 access, struct nfs4_ol_stateid *stp)
881{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400882 unsigned char mask = 1 << access;
883
884 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
885 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400886}
887
888/* clear share access for a given stateid */
889static inline void
890clear_access(u32 access, struct nfs4_ol_stateid *stp)
891{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400892 unsigned char mask = 1 << access;
893
894 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
895 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400896}
897
898/* test whether a given stateid has access */
899static inline bool
900test_access(u32 access, struct nfs4_ol_stateid *stp)
901{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400902 unsigned char mask = 1 << access;
903
904 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400905}
906
Jeff Laytonce0fc432012-05-11 09:45:14 -0400907/* set share deny for a given stateid */
908static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400909set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400910{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400911 unsigned char mask = 1 << deny;
912
913 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
914 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400915}
916
917/* clear share deny for a given stateid */
918static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400919clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400920{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400921 unsigned char mask = 1 << deny;
922
923 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
924 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400925}
926
927/* test whether a given stateid is denying specific access */
928static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400929test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400930{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400931 unsigned char mask = 1 << deny;
932
933 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400934}
935
936static int nfs4_access_to_omode(u32 access)
937{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400938 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400939 case NFS4_SHARE_ACCESS_READ:
940 return O_RDONLY;
941 case NFS4_SHARE_ACCESS_WRITE:
942 return O_WRONLY;
943 case NFS4_SHARE_ACCESS_BOTH:
944 return O_RDWR;
945 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500946 WARN_ON_ONCE(1);
947 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400948}
949
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400950/*
951 * A stateid that had a deny mode associated with it is being released
952 * or downgraded. Recalculate the deny mode on the file.
953 */
954static void
955recalculate_deny_mode(struct nfs4_file *fp)
956{
957 struct nfs4_ol_stateid *stp;
958
959 spin_lock(&fp->fi_lock);
960 fp->fi_share_deny = 0;
961 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
962 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
963 spin_unlock(&fp->fi_lock);
964}
965
966static void
967reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
968{
969 int i;
970 bool change = false;
971
972 for (i = 1; i < 4; i++) {
973 if ((i & deny) != i) {
974 change = true;
975 clear_deny(i, stp);
976 }
977 }
978
979 /* Recalculate per-file deny mode if there was a change */
980 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -0400981 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400982}
983
Jeff Layton82c5ff12012-05-11 09:45:13 -0400984/* release all access and file references for a given stateid */
985static void
986release_all_access(struct nfs4_ol_stateid *stp)
987{
988 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -0400989 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400990
991 if (fp && stp->st_deny_bmap != 0)
992 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400993
994 for (i = 1; i < 4; i++) {
995 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -0400996 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400997 clear_access(i, stp);
998 }
999}
1000
Kinglong Meed50ffde2015-07-16 12:05:07 +08001001static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1002{
1003 kfree(sop->so_owner.data);
1004 sop->so_ops->so_free(sop);
1005}
1006
Jeff Layton6b180f02014-07-29 21:34:26 -04001007static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1008{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001009 struct nfs4_client *clp = sop->so_client;
1010
1011 might_lock(&clp->cl_lock);
1012
1013 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001014 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001015 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001016 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001017 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001018}
1019
Jeff Laytone8568732015-08-24 12:41:47 -04001020static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001021{
Trond Myklebust11b91642014-07-29 21:34:08 -04001022 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001023
Jeff Layton1c755dc2014-07-29 21:34:12 -04001024 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1025
Jeff Laytone8568732015-08-24 12:41:47 -04001026 if (list_empty(&stp->st_perfile))
1027 return false;
1028
Trond Myklebust1d31a252014-07-10 14:07:25 -04001029 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001030 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001031 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001032 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001033 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001034}
1035
Trond Myklebust60116952014-07-29 21:34:06 -04001036static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001037{
Trond Myklebust60116952014-07-29 21:34:06 -04001038 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001039
Sachin Bhamare8287f002015-04-27 14:50:14 +02001040 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001041 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001042 if (stp->st_stateowner)
1043 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -04001044 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001045}
1046
Jeff Laytonb49e0842014-07-29 21:34:11 -04001047static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001048{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001049 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1050 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001051 struct file *file;
1052
Jeff Laytonb49e0842014-07-29 21:34:11 -04001053 file = find_any_file(stp->st_stid.sc_file);
1054 if (file)
1055 filp_close(file, (fl_owner_t)lo);
1056 nfs4_free_ol_stateid(stid);
1057}
1058
Jeff Layton2c41beb2014-07-29 21:34:41 -04001059/*
1060 * Put the persistent reference to an already unhashed generic stateid, while
1061 * holding the cl_lock. If it's the last reference, then put it onto the
1062 * reaplist for later destruction.
1063 */
1064static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1065 struct list_head *reaplist)
1066{
1067 struct nfs4_stid *s = &stp->st_stid;
1068 struct nfs4_client *clp = s->sc_client;
1069
1070 lockdep_assert_held(&clp->cl_lock);
1071
1072 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1073
1074 if (!atomic_dec_and_test(&s->sc_count)) {
1075 wake_up_all(&close_wq);
1076 return;
1077 }
1078
1079 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1080 list_add(&stp->st_locks, reaplist);
1081}
1082
Jeff Laytone8568732015-08-24 12:41:47 -04001083static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001084{
1085 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1086
1087 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
1088
1089 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001090 nfs4_unhash_stid(&stp->st_stid);
Jeff Laytone8568732015-08-24 12:41:47 -04001091 return unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001092}
1093
Jeff Layton5adfd882014-07-29 21:34:31 -04001094static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001095{
Jeff Layton1c755dc2014-07-29 21:34:12 -04001096 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001097 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001098
1099 spin_lock(&oo->oo_owner.so_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001100 unhashed = unhash_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001101 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001102 if (unhashed)
1103 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001104}
1105
Trond Myklebustc58c6612014-07-29 21:34:35 -04001106static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001107{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001108 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001109
Trond Myklebustd4f04892014-07-29 21:34:36 -04001110 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001111
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001112 list_del_init(&lo->lo_owner.so_strhash);
1113}
1114
Jeff Layton2c41beb2014-07-29 21:34:41 -04001115/*
1116 * Free a list of generic stateids that were collected earlier after being
1117 * fully unhashed.
1118 */
1119static void
1120free_ol_stateid_reaplist(struct list_head *reaplist)
1121{
1122 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001123 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001124
1125 might_sleep();
1126
1127 while (!list_empty(reaplist)) {
1128 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1129 st_locks);
1130 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001131 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001132 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001133 if (fp)
1134 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001135 }
1136}
1137
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001138static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001139{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001140 struct nfs4_client *clp = lo->lo_owner.so_client;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001141 struct nfs4_ol_stateid *stp;
1142 struct list_head reaplist;
1143
1144 INIT_LIST_HEAD(&reaplist);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001145
Trond Myklebustd4f04892014-07-29 21:34:36 -04001146 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001147 unhash_lockowner_locked(lo);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001148 while (!list_empty(&lo->lo_owner.so_stateids)) {
1149 stp = list_first_entry(&lo->lo_owner.so_stateids,
1150 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001151 WARN_ON(!unhash_lock_stateid(stp));
Jeff Layton2c41beb2014-07-29 21:34:41 -04001152 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001153 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001154 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001155 free_ol_stateid_reaplist(&reaplist);
Jeff Layton6b180f02014-07-29 21:34:26 -04001156 nfs4_put_stateowner(&lo->lo_owner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001157}
1158
Jeff Laytond83017f2014-07-29 21:34:42 -04001159static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1160 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001161{
1162 struct nfs4_ol_stateid *stp;
1163
Jeff Laytone8568732015-08-24 12:41:47 -04001164 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1165
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001166 while (!list_empty(&open_stp->st_locks)) {
1167 stp = list_entry(open_stp->st_locks.next,
1168 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001169 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001170 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001171 }
1172}
1173
Jeff Laytone8568732015-08-24 12:41:47 -04001174static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001175 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001176{
Jeff Laytone8568732015-08-24 12:41:47 -04001177 bool unhashed;
1178
Jeff Layton2c41beb2014-07-29 21:34:41 -04001179 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1180
Jeff Laytone8568732015-08-24 12:41:47 -04001181 unhashed = unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001182 release_open_stateid_locks(stp, reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04001183 return unhashed;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001184}
1185
1186static void release_open_stateid(struct nfs4_ol_stateid *stp)
1187{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001188 LIST_HEAD(reaplist);
1189
1190 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001191 if (unhash_open_stateid(stp, &reaplist))
1192 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001193 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1194 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001195}
1196
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001197static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001198{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001199 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001200
Trond Myklebustd4f04892014-07-29 21:34:36 -04001201 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001202
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001203 list_del_init(&oo->oo_owner.so_strhash);
1204 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001205}
1206
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001207static void release_last_closed_stateid(struct nfs4_openowner *oo)
1208{
Jeff Layton217526e2014-07-30 08:27:11 -04001209 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1210 nfsd_net_id);
1211 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001212
Jeff Layton217526e2014-07-30 08:27:11 -04001213 spin_lock(&nn->client_lock);
1214 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001215 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001216 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001217 oo->oo_last_closed_stid = NULL;
1218 }
Jeff Layton217526e2014-07-30 08:27:11 -04001219 spin_unlock(&nn->client_lock);
1220 if (s)
1221 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001222}
1223
Jeff Layton2c41beb2014-07-29 21:34:41 -04001224static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001225{
1226 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001227 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001228 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001229
Jeff Layton2c41beb2014-07-29 21:34:41 -04001230 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001231
Trond Myklebustd4f04892014-07-29 21:34:36 -04001232 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001233 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001234 while (!list_empty(&oo->oo_owner.so_stateids)) {
1235 stp = list_first_entry(&oo->oo_owner.so_stateids,
1236 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001237 if (unhash_open_stateid(stp, &reaplist))
1238 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001239 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001240 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001241 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001242 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001243 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001244}
1245
Marc Eshel5282fd72009-04-03 08:27:52 +03001246static inline int
1247hash_sessionid(struct nfs4_sessionid *sessionid)
1248{
1249 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1250
1251 return sid->sequence % SESSION_HASH_SIZE;
1252}
1253
Mark Salter135dd002015-04-06 09:46:00 -04001254#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001255static inline void
1256dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1257{
1258 u32 *ptr = (u32 *)(&sessionid->data[0]);
1259 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1260}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001261#else
1262static inline void
1263dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1264{
1265}
1266#endif
1267
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001268/*
1269 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1270 * won't be used for replay.
1271 */
1272void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1273{
1274 struct nfs4_stateowner *so = cstate->replay_owner;
1275
1276 if (nfserr == nfserr_replay_me)
1277 return;
1278
1279 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001280 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001281 return;
1282 }
1283 if (!so)
1284 return;
1285 if (so->so_is_open_owner)
1286 release_last_closed_stateid(openowner(so));
1287 so->so_seqid++;
1288 return;
1289}
Marc Eshel5282fd72009-04-03 08:27:52 +03001290
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001291static void
1292gen_sessionid(struct nfsd4_session *ses)
1293{
1294 struct nfs4_client *clp = ses->se_client;
1295 struct nfsd4_sessionid *sid;
1296
1297 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1298 sid->clientid = clp->cl_clientid;
1299 sid->sequence = current_sessionid++;
1300 sid->reserved = 0;
1301}
1302
1303/*
Andy Adamsona6496372009-08-28 08:45:01 -04001304 * The protocol defines ca_maxresponssize_cached to include the size of
1305 * the rpc header, but all we need to cache is the data starting after
1306 * the end of the initial SEQUENCE operation--the rest we regenerate
1307 * each time. Therefore we can advertise a ca_maxresponssize_cached
1308 * value that is the number of bytes in our cache plus a few additional
1309 * bytes. In order to stay on the safe side, and not promise more than
1310 * we can cache, those additional bytes must be the minimum possible: 24
1311 * bytes of rpc header (xid through accept state, with AUTH_NULL
1312 * verifier), 12 for the compound header (with zero-length tag), and 44
1313 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001314 */
Andy Adamsona6496372009-08-28 08:45:01 -04001315#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1316
Andy Adamson557ce262009-08-28 08:45:04 -04001317static void
1318free_session_slots(struct nfsd4_session *ses)
1319{
1320 int i;
1321
1322 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1323 kfree(ses->se_slots[i]);
1324}
1325
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001326/*
1327 * We don't actually need to cache the rpc and session headers, so we
1328 * can allocate a little less for each slot:
1329 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001330static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001331{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001332 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001333
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001334 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1335 size = 0;
1336 else
1337 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1338 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001339}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001340
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001341/*
1342 * XXX: If we run out of reserved DRC memory we could (up to a point)
1343 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001344 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001345 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001346static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001347{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001348 u32 slotsize = slot_bytes(ca);
1349 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001350 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001351
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001352 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001353 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1354 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001355 num = min_t(int, num, avail / slotsize);
1356 nfsd_drc_mem_used += num * slotsize;
1357 spin_unlock(&nfsd_drc_lock);
1358
1359 return num;
1360}
1361
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001362static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001363{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001364 int slotsize = slot_bytes(ca);
1365
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001366 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001367 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001368 spin_unlock(&nfsd_drc_lock);
1369}
1370
Kinglong Mee60810e52014-01-01 00:35:47 +08001371static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1372 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001373{
Kinglong Mee60810e52014-01-01 00:35:47 +08001374 int numslots = fattrs->maxreqs;
1375 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001376 struct nfsd4_session *new;
1377 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001378
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001379 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001380 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1381 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001382
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001383 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001384 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001385 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001386 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001387 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001388 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001389 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001390 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001391 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001392
1393 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1394 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1395
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001396 return new;
1397out_free:
1398 while (i--)
1399 kfree(new->se_slots[i]);
1400 kfree(new);
1401 return NULL;
1402}
1403
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001404static void free_conn(struct nfsd4_conn *c)
1405{
1406 svc_xprt_put(c->cn_xprt);
1407 kfree(c);
1408}
1409
1410static void nfsd4_conn_lost(struct svc_xpt_user *u)
1411{
1412 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1413 struct nfs4_client *clp = c->cn_session->se_client;
1414
1415 spin_lock(&clp->cl_lock);
1416 if (!list_empty(&c->cn_persession)) {
1417 list_del(&c->cn_persession);
1418 free_conn(c);
1419 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001420 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001421 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001422}
1423
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001424static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001425{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001426 struct nfsd4_conn *conn;
1427
1428 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1429 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001430 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001431 svc_xprt_get(rqstp->rq_xprt);
1432 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001433 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001434 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1435 return conn;
1436}
1437
J. Bruce Fields328ead22010-09-29 16:11:06 -04001438static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1439{
1440 conn->cn_session = ses;
1441 list_add(&conn->cn_persession, &ses->se_conns);
1442}
1443
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001444static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1445{
1446 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001447
1448 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001449 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001450 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001451}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001452
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001453static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001454{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001455 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001456 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001457}
1458
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001459static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001460{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001461 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001462
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001463 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001464 ret = nfsd4_register_conn(conn);
1465 if (ret)
1466 /* oops; xprt is already down: */
1467 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001468 /* We may have gained or lost a callback channel: */
1469 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001470}
1471
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001472static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001473{
1474 u32 dir = NFS4_CDFC4_FORE;
1475
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001476 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001477 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001478 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001479}
1480
1481/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001482static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001483{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001484 struct nfs4_client *clp = s->se_client;
1485 struct nfsd4_conn *c;
1486
1487 spin_lock(&clp->cl_lock);
1488 while (!list_empty(&s->se_conns)) {
1489 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1490 list_del_init(&c->cn_persession);
1491 spin_unlock(&clp->cl_lock);
1492
1493 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1494 free_conn(c);
1495
1496 spin_lock(&clp->cl_lock);
1497 }
1498 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001499}
1500
J. Bruce Fields1377b692012-09-11 21:42:40 -04001501static void __free_session(struct nfsd4_session *ses)
1502{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001503 free_session_slots(ses);
1504 kfree(ses);
1505}
1506
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001507static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001508{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001509 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001510 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001511 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001512}
Andy Adamson557ce262009-08-28 08:45:04 -04001513
Fengguang Wu135ae822012-11-10 07:20:25 -05001514static 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 -04001515{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001516 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001517 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001518
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001519 new->se_client = clp;
1520 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001521
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001522 INIT_LIST_HEAD(&new->se_conns);
1523
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001524 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001525 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001526 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001527 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001528 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001529 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001530 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001531 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001532 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001533 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001534
Chuck Leverb0d2e422014-08-22 15:10:59 -04001535 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001536 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001537 /*
1538 * This is a little silly; with sessions there's no real
1539 * use for the callback address. Use the peer address
1540 * as a reasonable default for now, but consider fixing
1541 * the rpc client not to require an address in the
1542 * future:
1543 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001544 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1545 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001546 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001547}
1548
Benny Halevy9089f1b2010-05-12 00:12:26 +03001549/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001550static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001551__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001552{
1553 struct nfsd4_session *elem;
1554 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001555 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001556
Trond Myklebust0a880a22014-07-30 08:27:10 -04001557 lockdep_assert_held(&nn->client_lock);
1558
Marc Eshel5282fd72009-04-03 08:27:52 +03001559 dump_sessionid(__func__, sessionid);
1560 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001561 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001562 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001563 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1564 NFS4_MAX_SESSIONID_LEN)) {
1565 return elem;
1566 }
1567 }
1568
1569 dprintk("%s: session not found\n", __func__);
1570 return NULL;
1571}
1572
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001573static struct nfsd4_session *
1574find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1575 __be32 *ret)
1576{
1577 struct nfsd4_session *session;
1578 __be32 status = nfserr_badsession;
1579
1580 session = __find_in_sessionid_hashtbl(sessionid, net);
1581 if (!session)
1582 goto out;
1583 status = nfsd4_get_session_locked(session);
1584 if (status)
1585 session = NULL;
1586out:
1587 *ret = status;
1588 return session;
1589}
1590
Benny Halevy9089f1b2010-05-12 00:12:26 +03001591/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001592static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001593unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001594{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001595 struct nfs4_client *clp = ses->se_client;
1596 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1597
1598 lockdep_assert_held(&nn->client_lock);
1599
Andy Adamson7116ed62009-04-03 08:27:43 +03001600 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001601 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001602 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001603 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001604}
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1607static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001608STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001610 /*
1611 * We're assuming the clid was not given out from a boot
1612 * precisely 2^32 (about 136 years) before this one. That seems
1613 * a safe assumption:
1614 */
1615 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001617 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001618 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 return 1;
1620}
1621
1622/*
1623 * XXX Should we use a slab cache ?
1624 * This type of memory management is somewhat inefficient, but we use it
1625 * anyway since SETCLIENTID is not a common operation.
1626 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001627static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
1629 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001630 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001632 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1633 if (clp == NULL)
1634 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001635 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001636 if (clp->cl_name.data == NULL)
1637 goto err_no_name;
1638 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1639 OWNER_HASH_SIZE, GFP_KERNEL);
1640 if (!clp->cl_ownerstr_hashtbl)
1641 goto err_no_hashtbl;
1642 for (i = 0; i < OWNER_HASH_SIZE; i++)
1643 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001644 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001645 INIT_LIST_HEAD(&clp->cl_sessions);
1646 idr_init(&clp->cl_stateids);
1647 atomic_set(&clp->cl_refcount, 0);
1648 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1649 INIT_LIST_HEAD(&clp->cl_idhash);
1650 INIT_LIST_HEAD(&clp->cl_openowners);
1651 INIT_LIST_HEAD(&clp->cl_delegations);
1652 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04001653 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001654#ifdef CONFIG_NFSD_PNFS
1655 INIT_LIST_HEAD(&clp->cl_lo_states);
1656#endif
Trond Myklebust5694c932014-04-18 14:43:56 -04001657 spin_lock_init(&clp->cl_lock);
1658 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001660err_no_hashtbl:
1661 kfree(clp->cl_name.data);
1662err_no_name:
1663 kfree(clp);
1664 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
1666
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001667static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668free_client(struct nfs4_client *clp)
1669{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001670 while (!list_empty(&clp->cl_sessions)) {
1671 struct nfsd4_session *ses;
1672 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1673 se_perclnt);
1674 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001675 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1676 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001677 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001678 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001679 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001680 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001682 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 kfree(clp);
1684}
1685
Benny Halevy84d38ac2010-05-12 00:13:16 +03001686/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001687static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001688unhash_client_locked(struct nfs4_client *clp)
1689{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001690 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001691 struct nfsd4_session *ses;
1692
Trond Myklebust0a880a22014-07-30 08:27:10 -04001693 lockdep_assert_held(&nn->client_lock);
1694
Trond Myklebust4beb3452014-07-30 08:27:02 -04001695 /* Mark the client as expired! */
1696 clp->cl_time = 0;
1697 /* Make it invisible */
1698 if (!list_empty(&clp->cl_idhash)) {
1699 list_del_init(&clp->cl_idhash);
1700 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1701 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1702 else
1703 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1704 }
1705 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001706 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001707 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1708 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001709 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001710}
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001713unhash_client(struct nfs4_client *clp)
1714{
1715 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1716
1717 spin_lock(&nn->client_lock);
1718 unhash_client_locked(clp);
1719 spin_unlock(&nn->client_lock);
1720}
1721
Jeff Layton97403d92014-07-30 08:27:12 -04001722static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1723{
1724 if (atomic_read(&clp->cl_refcount))
1725 return nfserr_jukebox;
1726 unhash_client_locked(clp);
1727 return nfs_ok;
1728}
1729
Trond Myklebust4beb3452014-07-30 08:27:02 -04001730static void
1731__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001733 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 struct list_head reaplist;
1736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001738 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001739 while (!list_empty(&clp->cl_delegations)) {
1740 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001741 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04001742 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
Benny Halevycdc97502014-05-30 09:09:30 -04001744 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 while (!list_empty(&reaplist)) {
1746 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001747 list_del_init(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +02001748 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -04001749 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04001750 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001752 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05001753 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001754 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001755 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001756 }
NeilBrownea1da632005-06-23 22:04:17 -07001757 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001758 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04001759 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001760 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001762 nfsd4_return_all_client_layouts(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001763 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001764 if (clp->cl_cb_conn.cb_xprt)
1765 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001766 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767}
1768
Trond Myklebust4beb3452014-07-30 08:27:02 -04001769static void
1770destroy_client(struct nfs4_client *clp)
1771{
1772 unhash_client(clp);
1773 __destroy_client(clp);
1774}
1775
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001776static void expire_client(struct nfs4_client *clp)
1777{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001778 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001779 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001780 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001781}
1782
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001783static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1784{
1785 memcpy(target->cl_verifier.data, source->data,
1786 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787}
1788
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001789static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1790{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1792 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1793}
1794
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001795static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001796{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001797 if (source->cr_principal) {
1798 target->cr_principal =
1799 kstrdup(source->cr_principal, GFP_KERNEL);
1800 if (target->cr_principal == NULL)
1801 return -ENOMEM;
1802 } else
1803 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001804 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 target->cr_uid = source->cr_uid;
1806 target->cr_gid = source->cr_gid;
1807 target->cr_group_info = source->cr_group_info;
1808 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001809 target->cr_gss_mech = source->cr_gss_mech;
1810 if (source->cr_gss_mech)
1811 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001815static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001816compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1817{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001818 if (o1->len < o2->len)
1819 return -1;
1820 if (o1->len > o2->len)
1821 return 1;
1822 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001823}
1824
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001825static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001826{
NeilBrowna55370a2005-06-23 22:03:52 -07001827 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
1829
1830static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001831same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1832{
1833 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
1835
1836static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001837same_clid(clientid_t *cl1, clientid_t *cl2)
1838{
1839 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840}
1841
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001842static bool groups_equal(struct group_info *g1, struct group_info *g2)
1843{
1844 int i;
1845
1846 if (g1->ngroups != g2->ngroups)
1847 return false;
1848 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001849 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001850 return false;
1851 return true;
1852}
1853
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001854/*
1855 * RFC 3530 language requires clid_inuse be returned when the
1856 * "principal" associated with a requests differs from that previously
1857 * used. We use uid, gid's, and gss principal string as our best
1858 * approximation. We also don't want to allow non-gss use of a client
1859 * established using gss: in theory cr_principal should catch that
1860 * change, but in practice cr_principal can be null even in the gss case
1861 * since gssd doesn't always pass down a principal string.
1862 */
1863static bool is_gss_cred(struct svc_cred *cr)
1864{
1865 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1866 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1867}
1868
1869
Vivek Trivedi5559b502012-07-24 21:18:20 +05301870static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001871same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1872{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001873 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001874 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1875 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001876 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1877 return false;
1878 if (cr1->cr_principal == cr2->cr_principal)
1879 return true;
1880 if (!cr1->cr_principal || !cr2->cr_principal)
1881 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301882 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884
J. Bruce Fields57266a62013-04-13 14:27:29 -04001885static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1886{
1887 struct svc_cred *cr = &rqstp->rq_cred;
1888 u32 service;
1889
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001890 if (!cr->cr_gss_mech)
1891 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001892 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1893 return service == RPC_GSS_SVC_INTEGRITY ||
1894 service == RPC_GSS_SVC_PRIVACY;
1895}
1896
1897static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1898{
1899 struct svc_cred *cr = &rqstp->rq_cred;
1900
1901 if (!cl->cl_mach_cred)
1902 return true;
1903 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1904 return false;
1905 if (!svc_rqst_integrity_protected(rqstp))
1906 return false;
1907 if (!cr->cr_principal)
1908 return false;
1909 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1910}
1911
Jeff Layton294ac322014-07-30 08:27:15 -04001912static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001913{
Chuck Leverab4684d2012-03-02 17:13:50 -05001914 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Jeff Laytonf4199922014-06-17 07:44:11 -04001916 /*
1917 * This is opaque to client, so no need to byte-swap. Use
1918 * __force to keep sparse happy
1919 */
1920 verf[0] = (__force __be32)get_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08001921 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001922 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
1924
Jeff Layton294ac322014-07-30 08:27:15 -04001925static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1926{
1927 clp->cl_clientid.cl_boot = nn->boot_time;
1928 clp->cl_clientid.cl_id = nn->clientid_counter++;
1929 gen_confirm(clp, nn);
1930}
1931
Jeff Layton4770d722014-07-29 21:34:10 -04001932static struct nfs4_stid *
1933find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001934{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001935 struct nfs4_stid *ret;
1936
1937 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1938 if (!ret || !ret->sc_type)
1939 return NULL;
1940 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001941}
1942
Jeff Layton4770d722014-07-29 21:34:10 -04001943static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001944find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001945{
1946 struct nfs4_stid *s;
1947
Jeff Layton4770d722014-07-29 21:34:10 -04001948 spin_lock(&cl->cl_lock);
1949 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001950 if (s != NULL) {
1951 if (typemask & s->sc_type)
1952 atomic_inc(&s->sc_count);
1953 else
1954 s = NULL;
1955 }
Jeff Layton4770d722014-07-29 21:34:10 -04001956 spin_unlock(&cl->cl_lock);
1957 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001958}
1959
Jeff Layton2216d442012-11-12 15:00:57 -05001960static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001961 struct svc_rqst *rqstp, nfs4_verifier *verf)
1962{
1963 struct nfs4_client *clp;
1964 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001965 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001966 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001967
1968 clp = alloc_client(name);
1969 if (clp == NULL)
1970 return NULL;
1971
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001972 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1973 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001974 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001975 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001976 }
Christoph Hellwig0162ac22014-09-24 12:19:19 +02001977 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Benny Halevy07cd4902010-05-12 00:13:41 +03001978 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001979 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001980 copy_verf(clp, verf);
1981 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001982 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001983 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001984 return clp;
1985}
1986
NeilBrownfd39ca92005-06-23 22:04:03 -07001987static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001988add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1989{
1990 struct rb_node **new = &(root->rb_node), *parent = NULL;
1991 struct nfs4_client *clp;
1992
1993 while (*new) {
1994 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1995 parent = *new;
1996
1997 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1998 new = &((*new)->rb_left);
1999 else
2000 new = &((*new)->rb_right);
2001 }
2002
2003 rb_link_node(&new_clp->cl_namenode, parent, new);
2004 rb_insert_color(&new_clp->cl_namenode, root);
2005}
2006
2007static struct nfs4_client *
2008find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2009{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002010 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002011 struct rb_node *node = root->rb_node;
2012 struct nfs4_client *clp;
2013
2014 while (node) {
2015 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2016 cmp = compare_blob(&clp->cl_name, name);
2017 if (cmp > 0)
2018 node = node->rb_left;
2019 else if (cmp < 0)
2020 node = node->rb_right;
2021 else
2022 return clp;
2023 }
2024 return NULL;
2025}
2026
2027static void
2028add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
2030 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002031 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Trond Myklebust0a880a22014-07-30 08:27:10 -04002033 lockdep_assert_held(&nn->client_lock);
2034
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002035 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002036 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002038 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002039 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
2041
NeilBrownfd39ca92005-06-23 22:04:03 -07002042static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043move_to_confirmed(struct nfs4_client *clp)
2044{
2045 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002046 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Trond Myklebust0a880a22014-07-30 08:27:10 -04002048 lockdep_assert_held(&nn->client_lock);
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002051 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002052 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002053 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002054 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002055 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056}
2057
2058static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002059find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
2061 struct nfs4_client *clp;
2062 unsigned int idhashval = clientid_hashval(clid->cl_id);
2063
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002064 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002065 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002066 if ((bool)clp->cl_minorversion != sessions)
2067 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002068 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
2072 return NULL;
2073}
2074
2075static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002076find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2077{
2078 struct list_head *tbl = nn->conf_id_hashtbl;
2079
Trond Myklebust0a880a22014-07-30 08:27:10 -04002080 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002081 return find_client_in_id_table(tbl, clid, sessions);
2082}
2083
2084static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002085find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002087 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Trond Myklebust0a880a22014-07-30 08:27:10 -04002089 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002090 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091}
2092
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002093static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002094{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002095 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002096}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002097
NeilBrown28ce6052005-06-23 22:03:56 -07002098static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002099find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002100{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002101 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002102 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002103}
2104
2105static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002106find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002107{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002108 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002109 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002110}
2111
NeilBrownfd39ca92005-06-23 22:04:03 -07002112static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002113gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002115 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002116 struct sockaddr *sa = svc_addr(rqstp);
2117 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002118 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Jeff Layton7077ecb2009-08-14 12:57:58 -04002120 /* Currently, we only support tcp and tcp6 for the callback channel */
2121 if (se->se_callback_netid_len == 3 &&
2122 !memcmp(se->se_callback_netid_val, "tcp", 3))
2123 expected_family = AF_INET;
2124 else if (se->se_callback_netid_len == 4 &&
2125 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2126 expected_family = AF_INET6;
2127 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 goto out_err;
2129
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002130 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002131 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002132 (struct sockaddr *)&conn->cb_addr,
2133 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002134
J. Bruce Fields07263f12010-05-31 19:09:40 -04002135 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002137
J. Bruce Fields07263f12010-05-31 19:09:40 -04002138 if (conn->cb_addr.ss_family == AF_INET6)
2139 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002140
J. Bruce Fields07263f12010-05-31 19:09:40 -04002141 conn->cb_prog = se->se_callback_prog;
2142 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002143 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return;
2145out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002146 conn->cb_addr.ss_family = AF_UNSPEC;
2147 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002148 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 "will not receive delegations\n",
2150 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 return;
2153}
2154
Andy Adamson074fe892009-04-03 08:28:15 +03002155/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002156 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002157 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002158static void
Andy Adamson074fe892009-04-03 08:28:15 +03002159nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2160{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002161 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002162 struct nfsd4_slot *slot = resp->cstate.slot;
2163 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002164
Andy Adamson557ce262009-08-28 08:45:04 -04002165 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002166
Andy Adamson557ce262009-08-28 08:45:04 -04002167 slot->sl_opcnt = resp->opcnt;
2168 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002169
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002170 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002171 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002172 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002173 return;
2174 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002175 base = resp->cstate.data_offset;
2176 slot->sl_datalen = buf->len - base;
2177 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04002178 WARN("%s: sessions DRC could not cache compound\n", __func__);
2179 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002180}
2181
2182/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002183 * Encode the replay sequence operation from the slot values.
2184 * If cachethis is FALSE encode the uncached rep error on the next
2185 * operation which sets resp->p and increments resp->opcnt for
2186 * nfs4svc_encode_compoundres.
2187 *
Andy Adamson074fe892009-04-03 08:28:15 +03002188 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002189static __be32
2190nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2191 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002192{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002193 struct nfsd4_op *op;
2194 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002195
Andy Adamsonabfabf82009-07-23 19:02:18 -04002196 /* Encode the replayed sequence operation */
2197 op = &args->ops[resp->opcnt - 1];
2198 nfsd4_encode_operation(resp, op);
2199
2200 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002201 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002202 op = &args->ops[resp->opcnt++];
2203 op->status = nfserr_retry_uncached_rep;
2204 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002205 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002206 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002207}
2208
2209/*
Andy Adamson557ce262009-08-28 08:45:04 -04002210 * The sequence operation is not cached because we can use the slot and
2211 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002212 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002213static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002214nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2215 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002216{
Andy Adamson557ce262009-08-28 08:45:04 -04002217 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002218 struct xdr_stream *xdr = &resp->xdr;
2219 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002220 __be32 status;
2221
Andy Adamson557ce262009-08-28 08:45:04 -04002222 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002223
Andy Adamsonabfabf82009-07-23 19:02:18 -04002224 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002225 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002226 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002227
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002228 p = xdr_reserve_space(xdr, slot->sl_datalen);
2229 if (!p) {
2230 WARN_ON_ONCE(1);
2231 return nfserr_serverfault;
2232 }
2233 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2234 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002235
Andy Adamson557ce262009-08-28 08:45:04 -04002236 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002237 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002238}
2239
Andy Adamson0733d212009-04-03 08:28:01 +03002240/*
2241 * Set the exchange_id flags returned by the server.
2242 */
2243static void
2244nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2245{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002246#ifdef CONFIG_NFSD_PNFS
2247 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2248#else
Andy Adamson0733d212009-04-03 08:28:01 +03002249 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002250#endif
Andy Adamson0733d212009-04-03 08:28:01 +03002251
2252 /* Referrals are supported, Migration is not. */
2253 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2254
2255 /* set the wire flags to return to client. */
2256 clid->flags = new->cl_exchange_flags;
2257}
2258
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002259static bool client_has_state(struct nfs4_client *clp)
2260{
2261 /*
2262 * Note clp->cl_openowners check isn't quite right: there's no
2263 * need to count owners without stateid's.
2264 *
2265 * Also note we should probably be using this in 4.0 case too.
2266 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002267 return !list_empty(&clp->cl_openowners)
Kinglong Mee47e970b2015-07-21 13:09:17 +08002268#ifdef CONFIG_NFSD_PNFS
2269 || !list_empty(&clp->cl_lo_states)
2270#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002271 || !list_empty(&clp->cl_delegations)
2272 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002273}
2274
Al Virob37ad282006-10-19 23:28:59 -07002275__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002276nfsd4_exchange_id(struct svc_rqst *rqstp,
2277 struct nfsd4_compound_state *cstate,
2278 struct nfsd4_exchange_id *exid)
2279{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002280 struct nfs4_client *conf, *new;
2281 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002282 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002283 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002284 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002285 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002286 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002287 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002288
Jeff Layton363168b2009-08-14 12:57:56 -04002289 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002290 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002291 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002292 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002293 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002294
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002295 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002296 return nfserr_inval;
2297
Andy Adamson0733d212009-04-03 08:28:01 +03002298 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002299 case SP4_MACH_CRED:
2300 if (!svc_rqst_integrity_protected(rqstp))
2301 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002302 case SP4_NONE:
2303 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002304 default: /* checked by xdr code */
2305 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002306 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002307 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002308 }
2309
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002310 new = create_client(exid->clname, rqstp, &verf);
2311 if (new == NULL)
2312 return nfserr_jukebox;
2313
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002314 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002315 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002316 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002317 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002318 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2319 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2320
J. Bruce Fields136e6582012-05-12 20:37:23 -04002321 if (update) {
2322 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002323 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002324 goto out;
2325 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002326 if (!mach_creds_match(conf, rqstp)) {
2327 status = nfserr_wrong_cred;
2328 goto out;
2329 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002330 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002331 status = nfserr_perm;
2332 goto out;
2333 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002334 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002335 status = nfserr_not_same;
2336 goto out;
2337 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002338 /* case 6 */
2339 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002340 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002341 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002342 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002343 if (client_has_state(conf)) {
2344 status = nfserr_clid_inuse;
2345 goto out;
2346 }
Andy Adamson0733d212009-04-03 08:28:01 +03002347 goto out_new;
2348 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002349 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002350 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002351 goto out_copy;
2352 }
2353 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002354 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002355 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002356 }
2357
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002358 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002359 status = nfserr_noent;
2360 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002361 }
2362
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002363 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002364 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002365 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002366
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002367 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002368out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002369 if (conf) {
2370 status = mark_client_expired_locked(conf);
2371 if (status)
2372 goto out;
2373 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002374 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002375 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002376
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002377 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002378 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002379 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002380out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002381 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2382 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002383
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002384 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2385 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002386
2387 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002388 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002389 status = nfs_ok;
2390
2391out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002392 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002393 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002394 expire_client(new);
2395 if (unconf)
2396 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002397 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002398}
2399
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002400static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002401check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002402{
Andy Adamson88e588d2009-07-23 19:02:15 -04002403 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2404 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002405
2406 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002407 if (slot_inuse) {
2408 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002409 return nfserr_jukebox;
2410 else
2411 return nfserr_seq_misordered;
2412 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002413 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002414 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002415 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002416 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002417 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002418 return nfserr_seq_misordered;
2419}
2420
Andy Adamson49557cc2009-07-23 19:02:16 -04002421/*
2422 * Cache the create session result into the create session single DRC
2423 * slot cache by saving the xdr structure. sl_seqid has been set.
2424 * Do this for solo or embedded create session operations.
2425 */
2426static void
2427nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002428 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002429{
2430 slot->sl_status = nfserr;
2431 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2432}
2433
2434static __be32
2435nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2436 struct nfsd4_clid_slot *slot)
2437{
2438 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2439 return slot->sl_status;
2440}
2441
Mi Jinlong1b74c252011-07-14 14:50:17 +08002442#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2443 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2444 1 + /* MIN tag is length with zero, only length */ \
2445 3 + /* version, opcount, opcode */ \
2446 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2447 /* seqid, slotID, slotID, cache */ \
2448 4 ) * sizeof(__be32))
2449
2450#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2451 2 + /* verifier: AUTH_NULL, length 0 */\
2452 1 + /* status */ \
2453 1 + /* MIN tag is length with zero, only length */ \
2454 3 + /* opcount, opcode, opstatus*/ \
2455 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2456 /* seqid, slotID, slotID, slotID, status */ \
2457 5 ) * sizeof(__be32))
2458
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002459static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002460{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002461 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2462
J. Bruce Fields373cd402013-04-08 15:42:12 -04002463 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2464 return nfserr_toosmall;
2465 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2466 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002467 ca->headerpadsz = 0;
2468 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2469 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2470 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2471 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2472 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2473 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2474 /*
2475 * Note decreasing slot size below client's request may make it
2476 * difficult for client to function correctly, whereas
2477 * decreasing the number of slots will (just?) affect
2478 * performance. When short on memory we therefore prefer to
2479 * decrease number of slots instead of their size. Clients that
2480 * request larger slots than they need will get poor results:
2481 */
2482 ca->maxreqs = nfsd4_get_drc_mem(ca);
2483 if (!ca->maxreqs)
2484 return nfserr_jukebox;
2485
J. Bruce Fields373cd402013-04-08 15:42:12 -04002486 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002487}
2488
Kinglong Mee8a891632013-12-23 18:11:02 +08002489#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2490 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2491#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2492 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2493
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002494static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2495{
2496 ca->headerpadsz = 0;
2497
2498 /*
2499 * These RPC_MAX_HEADER macros are overkill, especially since we
2500 * don't even do gss on the backchannel yet. But this is still
2501 * less than 1k. Tighten up this estimate in the unlikely event
2502 * it turns out to be a problem for some client:
2503 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002504 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002505 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002506 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002507 return nfserr_toosmall;
2508 ca->maxresp_cached = 0;
2509 if (ca->maxops < 2)
2510 return nfserr_toosmall;
2511
2512 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002513}
2514
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002515static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2516{
2517 switch (cbs->flavor) {
2518 case RPC_AUTH_NULL:
2519 case RPC_AUTH_UNIX:
2520 return nfs_ok;
2521 default:
2522 /*
2523 * GSS case: the spec doesn't allow us to return this
2524 * error. But it also doesn't allow us not to support
2525 * GSS.
2526 * I'd rather this fail hard than return some error the
2527 * client might think it can already handle:
2528 */
2529 return nfserr_encr_alg_unsupp;
2530 }
2531}
2532
Andy Adamson069b6ad2009-04-03 08:27:58 +03002533__be32
2534nfsd4_create_session(struct svc_rqst *rqstp,
2535 struct nfsd4_compound_state *cstate,
2536 struct nfsd4_create_session *cr_ses)
2537{
Jeff Layton363168b2009-08-14 12:57:56 -04002538 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002539 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002540 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002541 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002542 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002543 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002544 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002545 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002546
Mi Jinlonga62573d2011-03-23 17:57:07 +08002547 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2548 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002549 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2550 if (status)
2551 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002552 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002553 if (status)
2554 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002555 status = check_backchannel_attrs(&cr_ses->back_channel);
2556 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002557 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002558 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002559 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002560 if (!new)
2561 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002562 conn = alloc_conn_from_crses(rqstp, cr_ses);
2563 if (!conn)
2564 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002565
Jeff Laytond20c11d2014-07-30 08:27:07 -04002566 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002567 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002568 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002569 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002570
2571 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002572 status = nfserr_wrong_cred;
2573 if (!mach_creds_match(conf, rqstp))
2574 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002575 cs_slot = &conf->cl_cs_slot;
2576 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08002577 if (status) {
2578 if (status == nfserr_replay_cache)
2579 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002580 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002581 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002582 } else if (unconf) {
2583 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002584 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002585 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002586 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002587 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002588 status = nfserr_wrong_cred;
2589 if (!mach_creds_match(unconf, rqstp))
2590 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002591 cs_slot = &unconf->cl_cs_slot;
2592 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002593 if (status) {
2594 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002595 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002596 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002597 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002598 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002599 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002600 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002601 if (status) {
2602 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002603 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002604 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002605 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002606 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002607 conf = unconf;
2608 } else {
2609 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002610 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002611 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002612 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002613 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002614 * We do not support RDMA or persistent sessions
2615 */
2616 cr_ses->flags &= ~SESSION4_PERSIST;
2617 cr_ses->flags &= ~SESSION4_RDMA;
2618
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002619 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002620 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002621
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002622 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002623 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002624 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002625 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002626
Jeff Laytond20c11d2014-07-30 08:27:07 -04002627 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002628 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002629 spin_unlock(&nn->client_lock);
2630 /* init connection and backchannel */
2631 nfsd4_init_conn(rqstp, conn, new);
2632 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002633 if (old)
2634 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002635 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002636out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002637 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002638 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002639 if (old)
2640 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002641out_free_session:
2642 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002643out_release_drc_mem:
2644 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002645 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002646}
2647
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002648static __be32 nfsd4_map_bcts_dir(u32 *dir)
2649{
2650 switch (*dir) {
2651 case NFS4_CDFC4_FORE:
2652 case NFS4_CDFC4_BACK:
2653 return nfs_ok;
2654 case NFS4_CDFC4_FORE_OR_BOTH:
2655 case NFS4_CDFC4_BACK_OR_BOTH:
2656 *dir = NFS4_CDFC4_BOTH;
2657 return nfs_ok;
2658 };
2659 return nfserr_inval;
2660}
2661
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002662__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2663{
2664 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002665 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002666 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002667
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002668 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2669 if (status)
2670 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002671 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002672 session->se_cb_prog = bc->bc_cb_program;
2673 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002674 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002675
2676 nfsd4_probe_callback(session->se_client);
2677
2678 return nfs_ok;
2679}
2680
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002681__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2682 struct nfsd4_compound_state *cstate,
2683 struct nfsd4_bind_conn_to_session *bcts)
2684{
2685 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002686 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002687 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002688 struct net *net = SVC_NET(rqstp);
2689 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002690
2691 if (!nfsd4_last_compound_op(rqstp))
2692 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002693 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002694 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002695 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002696 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002697 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002698 status = nfserr_wrong_cred;
2699 if (!mach_creds_match(session->se_client, rqstp))
2700 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002701 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002702 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002703 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002704 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002705 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002706 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002707 goto out;
2708 nfsd4_init_conn(rqstp, conn, session);
2709 status = nfs_ok;
2710out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002711 nfsd4_put_session(session);
2712out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002713 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002714}
2715
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002716static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2717{
2718 if (!session)
2719 return 0;
2720 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2721}
2722
Andy Adamson069b6ad2009-04-03 08:27:58 +03002723__be32
2724nfsd4_destroy_session(struct svc_rqst *r,
2725 struct nfsd4_compound_state *cstate,
2726 struct nfsd4_destroy_session *sessionid)
2727{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002728 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002729 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002730 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002731 struct net *net = SVC_NET(r);
2732 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002733
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002734 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002735 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002736 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002737 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002738 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002739 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002740 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002741 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002742 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002743 if (!ses)
2744 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002745 status = nfserr_wrong_cred;
2746 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002747 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002748 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002749 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002750 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002751 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002752 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002753
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002754 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002755
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002756 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002757 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002758out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002759 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002760out_client_lock:
2761 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002762out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03002763 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002764}
2765
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002766static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002767{
2768 struct nfsd4_conn *c;
2769
2770 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002771 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002772 return c;
2773 }
2774 }
2775 return NULL;
2776}
2777
J. Bruce Fields57266a62013-04-13 14:27:29 -04002778static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002779{
2780 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002781 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002782 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002783 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002784
2785 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002786 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002787 if (c)
2788 goto out_free;
2789 status = nfserr_conn_not_bound_to_session;
2790 if (clp->cl_mach_cred)
2791 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002792 __nfsd4_hash_conn(new, ses);
2793 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002794 ret = nfsd4_register_conn(new);
2795 if (ret)
2796 /* oops; xprt is already down: */
2797 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002798 return nfs_ok;
2799out_free:
2800 spin_unlock(&clp->cl_lock);
2801 free_conn(new);
2802 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002803}
2804
Mi Jinlong868b89c2011-04-27 09:09:58 +08002805static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2806{
2807 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2808
2809 return args->opcnt > session->se_fchannel.maxops;
2810}
2811
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002812static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2813 struct nfsd4_session *session)
2814{
2815 struct xdr_buf *xb = &rqstp->rq_arg;
2816
2817 return xb->len > session->se_fchannel.maxreq_sz;
2818}
2819
Andy Adamson069b6ad2009-04-03 08:27:58 +03002820__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002821nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002822 struct nfsd4_compound_state *cstate,
2823 struct nfsd4_sequence *seq)
2824{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002825 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002826 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002827 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002828 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002829 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002830 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002831 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002832 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002833 struct net *net = SVC_NET(rqstp);
2834 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002835
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002836 if (resp->opcnt != 1)
2837 return nfserr_sequence_pos;
2838
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002839 /*
2840 * Will be either used or freed by nfsd4_sequence_check_conn
2841 * below.
2842 */
2843 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2844 if (!conn)
2845 return nfserr_jukebox;
2846
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002847 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002848 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002849 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002850 goto out_no_session;
2851 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002852
Mi Jinlong868b89c2011-04-27 09:09:58 +08002853 status = nfserr_too_many_ops;
2854 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002855 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002856
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002857 status = nfserr_req_too_big;
2858 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002859 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002860
Benny Halevyb85d4c02009-04-03 08:28:08 +03002861 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002862 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002863 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002864
Andy Adamson557ce262009-08-28 08:45:04 -04002865 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002866 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2867
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002868 /* We do not negotiate the number of slots yet, so set the
2869 * maxslots to the session maxreqs which is used to encode
2870 * sr_highest_slotid and the sr_target_slot id to maxslots */
2871 seq->maxslots = session->se_fchannel.maxreqs;
2872
J. Bruce Fields73e79482012-02-13 16:39:00 -05002873 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2874 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002875 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002876 status = nfserr_seq_misordered;
2877 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002878 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002879 cstate->slot = slot;
2880 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002881 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002882 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002883 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002884 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002885 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002886 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002887 }
2888 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002889 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002890
J. Bruce Fields57266a62013-04-13 14:27:29 -04002891 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002892 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002893 if (status)
2894 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002895
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002896 buflen = (seq->cachethis) ?
2897 session->se_fchannel.maxresp_cached :
2898 session->se_fchannel.maxresp_sz;
2899 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2900 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002901 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002902 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002903 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002904
2905 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002906 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002907 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002908 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002909 if (seq->cachethis)
2910 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002911 else
2912 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002913
2914 cstate->slot = slot;
2915 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002916 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002917
Benny Halevyb85d4c02009-04-03 08:28:08 +03002918out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002919 switch (clp->cl_cb_state) {
2920 case NFSD4_CB_DOWN:
2921 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2922 break;
2923 case NFSD4_CB_FAULT:
2924 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2925 break;
2926 default:
2927 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002928 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002929 if (!list_empty(&clp->cl_revoked))
2930 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002931out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002932 if (conn)
2933 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002934 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002935 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002936out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002937 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002938 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002939}
2940
Trond Myklebustb6076642014-06-30 11:48:35 -04002941void
2942nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2943{
2944 struct nfsd4_compound_state *cs = &resp->cstate;
2945
2946 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002947 if (cs->status != nfserr_replay_cache) {
2948 nfsd4_store_cache_entry(resp);
2949 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2950 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002951 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002952 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002953 } else if (cs->clp)
2954 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002955}
2956
Mi Jinlong345c2842011-10-20 17:51:39 +08002957__be32
2958nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2959{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002960 struct nfs4_client *conf, *unconf;
2961 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002962 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002963 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002964
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002965 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002966 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002967 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002968 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002969
2970 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002971 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002972 status = nfserr_clientid_busy;
2973 goto out;
2974 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04002975 status = mark_client_expired_locked(conf);
2976 if (status)
2977 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002978 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08002979 } else if (unconf)
2980 clp = unconf;
2981 else {
2982 status = nfserr_stale_clientid;
2983 goto out;
2984 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002985 if (!mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002986 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002987 status = nfserr_wrong_cred;
2988 goto out;
2989 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002990 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002991out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002992 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002993 if (clp)
2994 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002995 return status;
2996}
2997
Andy Adamson069b6ad2009-04-03 08:27:58 +03002998__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002999nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
3000{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003001 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003002
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003003 if (rc->rca_one_fs) {
3004 if (!cstate->current_fh.fh_dentry)
3005 return nfserr_nofilehandle;
3006 /*
3007 * We don't take advantage of the rca_one_fs case.
3008 * That's OK, it's optional, we can safely ignore it.
3009 */
3010 return nfs_ok;
3011 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003012
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003013 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04003014 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3015 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003016 goto out;
3017
3018 status = nfserr_stale_clientid;
3019 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003020 /*
3021 * The following error isn't really legal.
3022 * But we only get here if the client just explicitly
3023 * destroyed the client. Surely it no longer cares what
3024 * error it gets back on an operation for the dead
3025 * client.
3026 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003027 goto out;
3028
3029 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04003030 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003031out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003032 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003033}
3034
3035__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003036nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3037 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003039 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003041 struct nfs4_client *conf, *new;
3042 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003043 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003044 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3045
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003046 new = create_client(clname, rqstp, &clverifier);
3047 if (new == NULL)
3048 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003049 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003050 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003051 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07003052 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003053 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003055 if (clp_used_exchangeid(conf))
3056 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003057 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04003058 char addr_str[INET6_ADDRSTRLEN];
3059 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3060 sizeof(addr_str));
3061 dprintk("NFSD: setclientid: string in use by client "
3062 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 goto out;
3064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003066 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003067 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003068 unhash_client_locked(unconf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003069 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003070 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003072 gen_confirm(new, nn);
3073 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003074 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003075 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003076 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003077 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3079 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3080 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003081 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 status = nfs_ok;
3083out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003084 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003085 if (new)
3086 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003087 if (unconf)
3088 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 return status;
3090}
3091
3092
Al Virob37ad282006-10-19 23:28:59 -07003093__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003094nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3095 struct nfsd4_compound_state *cstate,
3096 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
NeilBrown21ab45a2005-06-23 22:04:14 -07003098 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003099 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3101 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003102 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003103 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003105 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003107
Jeff Laytond20c11d2014-07-30 08:27:07 -04003108 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003109 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003110 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003111 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003112 * We try hard to give out unique clientid's, so if we get an
3113 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003114 * the client may be buggy; this should never happen.
3115 *
3116 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003117 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003118 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04003119 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3120 goto out;
3121 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3122 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003123 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003124 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3125 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003127 else /* case 4: client hasn't noticed we rebooted yet? */
3128 status = nfserr_stale_clientid;
3129 goto out;
3130 }
3131 status = nfs_ok;
3132 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003133 old = unconf;
3134 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003135 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003136 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003137 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3138 if (old) {
3139 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003140 if (status) {
3141 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003142 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003143 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003144 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003145 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003146 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003147 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003148 get_client_locked(conf);
3149 spin_unlock(&nn->client_lock);
3150 nfsd4_probe_callback(conf);
3151 spin_lock(&nn->client_lock);
3152 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003154 spin_unlock(&nn->client_lock);
3155 if (old)
3156 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 return status;
3158}
3159
J. Bruce Fields32513b42011-10-13 16:00:16 -04003160static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003162 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3163}
3164
3165/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003166static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3167 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003168{
Trond Myklebust950e0112014-06-30 11:48:31 -04003169 lockdep_assert_held(&state_lock);
3170
J. Bruce Fields32513b42011-10-13 16:00:16 -04003171 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003172 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003173 INIT_LIST_HEAD(&fp->fi_stateids);
3174 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02003175 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003176 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003177 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003178 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003179 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003180 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3181 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003182#ifdef CONFIG_NFSD_PNFS
3183 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02003184 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003185#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04003186 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187}
3188
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003189void
NeilBrowne60d4392005-06-23 22:03:01 -07003190nfsd4_free_slabs(void)
3191{
Sachin Bhamare8287f002015-04-27 14:50:14 +02003192 kmem_cache_destroy(odstate_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003193 kmem_cache_destroy(openowner_slab);
3194 kmem_cache_destroy(lockowner_slab);
3195 kmem_cache_destroy(file_slab);
3196 kmem_cache_destroy(stateid_slab);
3197 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003198}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
Bryan Schumaker72083392011-11-01 15:24:59 -04003200int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201nfsd4_init_slabs(void)
3202{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003203 openowner_slab = kmem_cache_create("nfsd4_openowners",
3204 sizeof(struct nfs4_openowner), 0, 0, NULL);
3205 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003206 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003207 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003208 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003209 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003210 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003211 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003212 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003213 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003214 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003215 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003216 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003217 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003218 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003219 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003220 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003221 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003222 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003223 odstate_slab = kmem_cache_create("nfsd4_odstate",
3224 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3225 if (odstate_slab == NULL)
3226 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003228
Sachin Bhamare8287f002015-04-27 14:50:14 +02003229out_free_deleg_slab:
3230 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003231out_free_stateid_slab:
3232 kmem_cache_destroy(stateid_slab);
3233out_free_file_slab:
3234 kmem_cache_destroy(file_slab);
3235out_free_lockowner_slab:
3236 kmem_cache_destroy(lockowner_slab);
3237out_free_openowner_slab:
3238 kmem_cache_destroy(openowner_slab);
3239out:
NeilBrowne60d4392005-06-23 22:03:01 -07003240 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3241 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242}
3243
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003244static void init_nfs4_replay(struct nfs4_replay *rp)
3245{
3246 rp->rp_status = nfserr_serverfault;
3247 rp->rp_buflen = 0;
3248 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003249 mutex_init(&rp->rp_mutex);
3250}
3251
3252static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3253 struct nfs4_stateowner *so)
3254{
3255 if (!nfsd4_has_session(cstate)) {
3256 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003257 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04003258 }
3259}
3260
3261void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3262{
3263 struct nfs4_stateowner *so = cstate->replay_owner;
3264
3265 if (so != NULL) {
3266 cstate->replay_owner = NULL;
3267 mutex_unlock(&so->so_replay.rp_mutex);
3268 nfs4_put_stateowner(so);
3269 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003270}
3271
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003272static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273{
3274 struct nfs4_stateowner *sop;
3275
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003276 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003277 if (!sop)
3278 return NULL;
3279
3280 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3281 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003282 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003283 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003285 sop->so_owner.len = owner->len;
3286
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003287 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003288 sop->so_client = clp;
3289 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003290 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003291 return sop;
3292}
3293
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003294static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003295{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003296 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003297
Trond Myklebustd4f04892014-07-29 21:34:36 -04003298 list_add(&oo->oo_owner.so_strhash,
3299 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003300 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301}
3302
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003303static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3304{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003305 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003306}
3307
Jeff Layton6b180f02014-07-29 21:34:26 -04003308static void nfs4_free_openowner(struct nfs4_stateowner *so)
3309{
3310 struct nfs4_openowner *oo = openowner(so);
3311
3312 kmem_cache_free(openowner_slab, oo);
3313}
3314
3315static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003316 .so_unhash = nfs4_unhash_openowner,
3317 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003318};
3319
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003320static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003321alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003322 struct nfsd4_compound_state *cstate)
3323{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003324 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003325 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003327 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3328 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003330 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003331 oo->oo_owner.so_is_open_owner = 1;
3332 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003333 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003334 if (nfsd4_has_session(cstate))
3335 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003336 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003337 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003338 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003339 spin_lock(&clp->cl_lock);
3340 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003341 if (ret == NULL) {
3342 hash_openowner(oo, clp, strhashval);
3343 ret = oo;
3344 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08003345 nfs4_free_stateowner(&oo->oo_owner);
3346
Trond Myklebustd4f04892014-07-29 21:34:36 -04003347 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04003348 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349}
3350
J. Bruce Fields996e0932011-10-17 11:14:48 -04003351static 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 -04003352 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003354 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003355 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003356 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003357 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07003358 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003359 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 stp->st_access_bmap = 0;
3361 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003362 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003363 spin_lock(&oo->oo_owner.so_client->cl_lock);
3364 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003365 spin_lock(&fp->fi_lock);
3366 list_add(&stp->st_perfile, &fp->fi_stateids);
3367 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003368 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369}
3370
Jeff Laytond3134b12014-07-29 21:34:32 -04003371/*
3372 * In the 4.0 case we need to keep the owners around a little while to handle
3373 * CLOSE replay. We still do need to release any file access that is held by
3374 * them before returning however.
3375 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003377move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378{
Jeff Layton217526e2014-07-30 08:27:11 -04003379 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003380 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3381 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3382 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003383
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003384 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
Jeff Laytonb401be222014-07-29 21:34:33 -04003386 /*
3387 * We know that we hold one reference via nfsd4_close, and another
3388 * "persistent" reference for the client. If the refcount is higher
3389 * than 2, then there are still calls in progress that are using this
3390 * stateid. We can't put the sc_file reference until they are finished.
3391 * Wait for the refcount to drop to 2. Since it has been unhashed,
3392 * there should be no danger of the refcount going back up again at
3393 * this point.
3394 */
3395 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3396
Jeff Laytond3134b12014-07-29 21:34:32 -04003397 release_all_access(s);
3398 if (s->st_stid.sc_file) {
3399 put_nfs4_file(s->st_stid.sc_file);
3400 s->st_stid.sc_file = NULL;
3401 }
Jeff Layton217526e2014-07-30 08:27:11 -04003402
3403 spin_lock(&nn->client_lock);
3404 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003405 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003406 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003407 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003408 spin_unlock(&nn->client_lock);
3409 if (last)
3410 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411}
3412
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413/* search file_hashtbl[] for file */
3414static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04003415find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 struct nfs4_file *fp;
3418
Jeff Layton5b095e92014-10-23 08:01:02 -04003419 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02003420 if (fh_match(&fp->fi_fhandle, fh)) {
Jeff Layton5b095e92014-10-23 08:01:02 -04003421 if (atomic_inc_not_zero(&fp->fi_ref))
3422 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 }
3425 return NULL;
3426}
3427
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02003428struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003429find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003430{
3431 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003432 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003433
Jeff Layton5b095e92014-10-23 08:01:02 -04003434 rcu_read_lock();
3435 fp = find_file_locked(fh, hashval);
3436 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04003437 return fp;
3438}
3439
3440static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003441find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003442{
3443 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003444 unsigned int hashval = file_hashval(fh);
3445
3446 rcu_read_lock();
3447 fp = find_file_locked(fh, hashval);
3448 rcu_read_unlock();
3449 if (fp)
3450 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04003451
3452 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04003453 fp = find_file_locked(fh, hashval);
3454 if (likely(fp == NULL)) {
3455 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04003456 fp = new;
3457 }
3458 spin_unlock(&state_lock);
3459
3460 return fp;
3461}
3462
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003463/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 * Called to check deny when READ with all zero stateid or
3465 * WRITE with all zero or all one stateid
3466 */
Al Virob37ad282006-10-19 23:28:59 -07003467static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3469{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003471 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
Trond Myklebustca943212014-07-23 16:17:39 -04003473 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003474 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003475 return ret;
3476 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003477 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003478 if (fp->fi_share_deny & deny_type)
3479 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003480 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003481 put_nfs4_file(fp);
3482 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483}
3484
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003485static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003487 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04003488 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3489 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003490
Trond Myklebust11b91642014-07-29 21:34:08 -04003491 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003492
Jeff Layton02e12152014-07-16 10:31:57 -04003493 /*
3494 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003495 * already holding inode->i_lock.
3496 *
Jeff Laytondff13992014-07-08 14:02:49 -04003497 * If the dl_time != 0, then we know that it has already been
3498 * queued for a lease break. Don't queue it again.
3499 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003500 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003501 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003502 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003503 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003504 }
Jeff Layton02e12152014-07-16 10:31:57 -04003505 spin_unlock(&state_lock);
3506}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003508static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3509 struct rpc_task *task)
3510{
3511 struct nfs4_delegation *dp = cb_to_delegation(cb);
3512
3513 switch (task->tk_status) {
3514 case 0:
3515 return 1;
3516 case -EBADHANDLE:
3517 case -NFS4ERR_BAD_STATEID:
3518 /*
3519 * Race: client probably got cb_recall before open reply
3520 * granting delegation.
3521 */
3522 if (dp->dl_retries--) {
3523 rpc_delay(task, 2 * HZ);
3524 return 0;
3525 }
3526 /*FALLTHRU*/
3527 default:
3528 return -1;
3529 }
3530}
3531
3532static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3533{
3534 struct nfs4_delegation *dp = cb_to_delegation(cb);
3535
3536 nfs4_put_stid(&dp->dl_stid);
3537}
3538
3539static struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3540 .prepare = nfsd4_cb_recall_prepare,
3541 .done = nfsd4_cb_recall_done,
3542 .release = nfsd4_cb_recall_release,
3543};
3544
Jeff Layton02e12152014-07-16 10:31:57 -04003545static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3546{
3547 /*
3548 * We're assuming the state code never drops its reference
3549 * without first removing the lease. Since we're in this lease
3550 * callback (and since the lease code is serialized by the kernel
3551 * lock) we know the server hasn't removed the lease yet, we know
3552 * it's safe to take a reference.
3553 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003554 atomic_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02003555 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003556}
3557
Jeff Layton1c8c6012013-06-21 08:58:15 -04003558/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003559static bool
3560nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003561{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003562 bool ret = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003563 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3564 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003565
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003566 if (!fp) {
3567 WARN(1, "(%p)->fl_owner NULL\n", fl);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003568 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003569 }
3570 if (fp->fi_had_conflict) {
3571 WARN(1, "duplicate break on %p\n", fp);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003572 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003573 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003574 /*
3575 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003576 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003577 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003578 */
3579 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
Jeff Layton02e12152014-07-16 10:31:57 -04003581 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003582 fp->fi_had_conflict = true;
3583 /*
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003584 * If there are no delegations on the list, then return true
3585 * so that the lease code will go ahead and delete it.
Jeff Layton417c6622014-07-21 09:34:57 -04003586 */
3587 if (list_empty(&fp->fi_delegations))
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003588 ret = true;
Jeff Layton417c6622014-07-21 09:34:57 -04003589 else
3590 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3591 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003592 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003593 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594}
3595
Jeff Laytonc45198e2014-09-01 07:12:07 -04003596static int
Jeff Layton7448cc32015-01-16 15:05:57 -05003597nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3598 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599{
3600 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04003601 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 else
3603 return -EAGAIN;
3604}
3605
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003606static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003607 .lm_break = nfsd_break_deleg_cb,
3608 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609};
3610
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003611static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3612{
3613 if (nfsd4_has_session(cstate))
3614 return nfs_ok;
3615 if (seqid == so->so_seqid - 1)
3616 return nfserr_replay_me;
3617 if (seqid == so->so_seqid)
3618 return nfs_ok;
3619 return nfserr_bad_seqid;
3620}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621
Jeff Layton4b24ca72014-06-30 11:48:44 -04003622static __be32 lookup_clientid(clientid_t *clid,
3623 struct nfsd4_compound_state *cstate,
3624 struct nfsd_net *nn)
3625{
3626 struct nfs4_client *found;
3627
3628 if (cstate->clp) {
3629 found = cstate->clp;
3630 if (!same_clid(&found->cl_clientid, clid))
3631 return nfserr_stale_clientid;
3632 return nfs_ok;
3633 }
3634
3635 if (STALE_CLIENTID(clid, nn))
3636 return nfserr_stale_clientid;
3637
3638 /*
3639 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3640 * cached already then we know this is for is for v4.0 and "sessions"
3641 * will be false.
3642 */
3643 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003644 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003645 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003646 if (!found) {
3647 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003648 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003649 }
3650 atomic_inc(&found->cl_refcount);
3651 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003652
3653 /* Cache the nfs4_client in cstate! */
3654 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003655 return nfs_ok;
3656}
3657
Al Virob37ad282006-10-19 23:28:59 -07003658__be32
Andy Adamson66689582009-04-03 08:28:45 +03003659nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003660 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 clientid_t *clientid = &open->op_clientid;
3663 struct nfs4_client *clp = NULL;
3664 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003665 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003666 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003668 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003670 /*
3671 * In case we need it later, after we've already created the
3672 * file and don't want to risk a further failure:
3673 */
3674 open->op_file = nfsd4_alloc_file();
3675 if (open->op_file == NULL)
3676 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
Trond Myklebust2d91e892014-06-30 11:48:46 -04003678 status = lookup_clientid(clientid, cstate, nn);
3679 if (status)
3680 return status;
3681 clp = cstate->clp;
3682
Trond Myklebustd4f04892014-07-29 21:34:36 -04003683 strhashval = ownerstr_hashval(&open->op_owner);
3684 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003685 open->op_openowner = oo;
3686 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003687 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003689 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003690 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003691 release_openowner(oo);
3692 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003693 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003694 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003695 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3696 if (status)
3697 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003698 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003699new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003700 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003701 if (oo == NULL)
3702 return nfserr_jukebox;
3703 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003704alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003705 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003706 if (!open->op_stp)
3707 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003708
3709 if (nfsd4_has_session(cstate) &&
3710 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
3711 open->op_odstate = alloc_clnt_odstate(clp);
3712 if (!open->op_odstate)
3713 return nfserr_jukebox;
3714 }
3715
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003716 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717}
3718
Al Virob37ad282006-10-19 23:28:59 -07003719static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003720nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3721{
3722 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3723 return nfserr_openmode;
3724 else
3725 return nfs_ok;
3726}
3727
Daniel Mackc47d8322011-05-16 16:38:14 +02003728static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003729{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003730 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3731}
3732
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003733static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003734{
3735 struct nfs4_stid *ret;
3736
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003737 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003738 if (!ret)
3739 return NULL;
3740 return delegstateid(ret);
3741}
3742
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003743static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3744{
3745 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3746 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3747}
3748
Al Virob37ad282006-10-19 23:28:59 -07003749static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003750nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003751 struct nfs4_delegation **dp)
3752{
3753 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003754 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003755 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003756
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003757 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3758 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003759 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003760 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003761 status = nfs4_check_delegmode(deleg, flags);
3762 if (status) {
3763 nfs4_put_stid(&deleg->dl_stid);
3764 goto out;
3765 }
3766 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003767out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003768 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003769 return nfs_ok;
3770 if (status)
3771 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003772 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003773 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003774}
3775
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003776static struct nfs4_ol_stateid *
3777nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003779 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003780 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
Trond Myklebust1d31a252014-07-10 14:07:25 -04003782 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003783 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 /* ignore lock owners */
3785 if (local->st_stateowner->so_is_open_owner == 0)
3786 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003787 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003788 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003789 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003790 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003793 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003794 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795}
3796
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003797static inline int nfs4_access_to_access(u32 nfs4_access)
3798{
3799 int flags = 0;
3800
3801 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3802 flags |= NFSD_MAY_READ;
3803 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3804 flags |= NFSD_MAY_WRITE;
3805 return flags;
3806}
3807
Al Virob37ad282006-10-19 23:28:59 -07003808static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3810 struct nfsd4_open *open)
3811{
3812 struct iattr iattr = {
3813 .ia_valid = ATTR_SIZE,
3814 .ia_size = 0,
3815 };
3816 if (!open->op_truncate)
3817 return 0;
3818 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003819 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3821}
3822
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003823static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003824 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3825 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003826{
Trond Myklebustde186432014-07-10 14:07:26 -04003827 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003828 __be32 status;
3829 int oflag = nfs4_access_to_omode(open->op_share_access);
3830 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003831 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003832
Trond Myklebustde186432014-07-10 14:07:26 -04003833 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003834
3835 /*
3836 * Are we trying to set a deny mode that would conflict with
3837 * current access?
3838 */
3839 status = nfs4_file_check_deny(fp, open->op_share_deny);
3840 if (status != nfs_ok) {
3841 spin_unlock(&fp->fi_lock);
3842 goto out;
3843 }
3844
3845 /* set access to the file */
3846 status = nfs4_file_get_access(fp, open->op_share_access);
3847 if (status != nfs_ok) {
3848 spin_unlock(&fp->fi_lock);
3849 goto out;
3850 }
3851
3852 /* Set access bits in stateid */
3853 old_access_bmap = stp->st_access_bmap;
3854 set_access(open->op_share_access, stp);
3855
3856 /* Set new deny mask */
3857 old_deny_bmap = stp->st_deny_bmap;
3858 set_deny(open->op_share_deny, stp);
3859 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3860
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003861 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003862 spin_unlock(&fp->fi_lock);
3863 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003864 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003865 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003866 spin_lock(&fp->fi_lock);
3867 if (!fp->fi_fds[oflag]) {
3868 fp->fi_fds[oflag] = filp;
3869 filp = NULL;
3870 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003871 }
Trond Myklebustde186432014-07-10 14:07:26 -04003872 spin_unlock(&fp->fi_lock);
3873 if (filp)
3874 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003875
3876 status = nfsd4_truncate(rqstp, cur_fh, open);
3877 if (status)
3878 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003879out:
3880 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003881out_put_access:
3882 stp->st_access_bmap = old_access_bmap;
3883 nfs4_file_put_access(fp, open->op_share_access);
3884 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3885 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003886}
3887
Al Virob37ad282006-10-19 23:28:59 -07003888static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003889nfs4_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 -07003890{
Al Virob37ad282006-10-19 23:28:59 -07003891 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02003892 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003894 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003895 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003896
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003897 /* test and set deny mode */
3898 spin_lock(&fp->fi_lock);
3899 status = nfs4_file_check_deny(fp, open->op_share_deny);
3900 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003901 set_deny(open->op_share_deny, stp);
3902 fp->fi_share_deny |=
3903 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3904 }
3905 spin_unlock(&fp->fi_lock);
3906
3907 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003910 status = nfsd4_truncate(rqstp, cur_fh, open);
3911 if (status != nfs_ok)
3912 reset_union_bmap_deny(old_deny_bmap, stp);
3913 return status;
3914}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003916/* Should we give out recallable state?: */
3917static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3918{
3919 if (clp->cl_cb_state == NFSD4_CB_UP)
3920 return true;
3921 /*
3922 * In the sessions case, since we don't have to establish a
3923 * separate connection for callbacks, we assume it's OK
3924 * until we hear otherwise:
3925 */
3926 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3927}
3928
Jeff Laytond564fbe2014-07-16 10:31:58 -04003929static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003930{
3931 struct file_lock *fl;
3932
3933 fl = locks_alloc_lock();
3934 if (!fl)
3935 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003936 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003937 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003938 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3939 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003940 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003941 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003942 return fl;
3943}
3944
J. Bruce Fields99c41512013-05-21 16:21:25 -04003945static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003946{
Trond Myklebust11b91642014-07-29 21:34:08 -04003947 struct nfs4_file *fp = dp->dl_stid.sc_file;
Kinglong Meeefde6b42015-07-13 17:33:24 +08003948 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003949 struct file *filp;
3950 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003951
Jeff Laytond564fbe2014-07-16 10:31:58 -04003952 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003953 if (!fl)
3954 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003955 filp = find_readable_file(fp);
3956 if (!filp) {
3957 /* We should always have a readable file here */
3958 WARN_ON_ONCE(1);
Kinglong Meeaf9dbaf2015-07-13 17:30:51 +08003959 locks_free_lock(fl);
Jeff Layton417c6622014-07-21 09:34:57 -04003960 return -EBADF;
3961 }
3962 fl->fl_file = filp;
Jeff Laytone6f5c782014-08-22 10:40:25 -04003963 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003964 if (fl)
Jeff Layton417c6622014-07-21 09:34:57 -04003965 locks_free_lock(fl);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003966 if (status)
Jeff Layton417c6622014-07-21 09:34:57 -04003967 goto out_fput;
Benny Halevycdc97502014-05-30 09:09:30 -04003968 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003969 spin_lock(&fp->fi_lock);
3970 /* Did the lease get broken before we took the lock? */
3971 status = -EAGAIN;
3972 if (fp->fi_had_conflict)
3973 goto out_unlock;
3974 /* Race breaker */
Jeff Layton0c637be2014-08-22 12:05:43 -04003975 if (fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04003976 status = 0;
Jeff Layton67db1032014-12-13 09:11:40 -05003977 ++fp->fi_delegees;
Jeff Layton417c6622014-07-21 09:34:57 -04003978 hash_delegation_locked(dp, fp);
3979 goto out_unlock;
3980 }
Jeff Layton417c6622014-07-21 09:34:57 -04003981 fp->fi_deleg_file = filp;
Jeff Layton67db1032014-12-13 09:11:40 -05003982 fp->fi_delegees = 1;
Benny Halevy931ee562014-05-30 09:09:27 -04003983 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003984 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003985 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003986 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003987out_unlock:
3988 spin_unlock(&fp->fi_lock);
3989 spin_unlock(&state_lock);
3990out_fput:
3991 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003992 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003993}
3994
Jeff Layton0b266932014-07-25 07:34:25 -04003995static struct nfs4_delegation *
3996nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02003997 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003998{
Jeff Layton0b266932014-07-25 07:34:25 -04003999 int status;
4000 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04004001
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004002 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004003 return ERR_PTR(-EAGAIN);
4004
Sachin Bhamare8287f002015-04-27 14:50:14 +02004005 dp = alloc_init_deleg(clp, fh, odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004006 if (!dp)
4007 return ERR_PTR(-ENOMEM);
4008
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004009 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04004010 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004011 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04004012 dp->dl_stid.sc_file = fp;
Jeff Layton0c637be2014-08-22 12:05:43 -04004013 if (!fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04004014 spin_unlock(&fp->fi_lock);
4015 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004016 status = nfs4_setlease(dp);
4017 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04004018 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004019 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04004020 status = -EAGAIN;
4021 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004022 }
Jeff Layton67db1032014-12-13 09:11:40 -05004023 ++fp->fi_delegees;
Benny Halevy931ee562014-05-30 09:09:27 -04004024 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04004025 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04004026out_unlock:
4027 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04004028 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004029out:
4030 if (status) {
Sachin Bhamare8287f002015-04-27 14:50:14 +02004031 put_clnt_odstate(dp->dl_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04004032 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04004033 return ERR_PTR(status);
4034 }
4035 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004036}
4037
Benny Halevy4aa89132012-02-21 14:16:44 -08004038static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4039{
4040 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4041 if (status == -EAGAIN)
4042 open->op_why_no_deleg = WND4_CONTENTION;
4043 else {
4044 open->op_why_no_deleg = WND4_RESOURCE;
4045 switch (open->op_deleg_want) {
4046 case NFS4_SHARE_WANT_READ_DELEG:
4047 case NFS4_SHARE_WANT_WRITE_DELEG:
4048 case NFS4_SHARE_WANT_ANY_DELEG:
4049 break;
4050 case NFS4_SHARE_WANT_CANCEL:
4051 open->op_why_no_deleg = WND4_CANCELLED;
4052 break;
4053 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004054 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08004055 }
4056 }
4057}
4058
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059/*
4060 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04004061 *
4062 * Note we don't support write delegations, and won't until the vfs has
4063 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 */
4065static void
Jeff Layton4cf59222014-07-25 07:34:24 -04004066nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4067 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068{
4069 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04004070 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4071 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004072 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004073 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004075 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07004076 open->op_recall = 0;
4077 switch (open->op_claim_type) {
4078 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05004079 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07004080 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004081 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4082 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004083 break;
4084 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00004085 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004086 /*
4087 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004088 * had the chance to reclaim theirs, *and* until
4089 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004090 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004091 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004092 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004093 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004094 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04004095 /*
4096 * Also, if the file was opened for write or
4097 * create, there's a good chance the client's
4098 * about to write to it, resulting in an
4099 * immediate recall (since we don't support
4100 * write delegations):
4101 */
NeilBrown7b190fe2005-06-23 22:03:23 -07004102 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04004103 goto out_no_deleg;
4104 if (open->op_create == NFS4_OPEN_CREATE)
4105 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004106 break;
4107 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004108 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004109 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004110 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004111 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004112 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004114 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004116 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004117 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04004118 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04004119 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004120 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004121out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004122 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4123 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004124 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04004125 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004126 open->op_recall = 1;
4127 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004128
4129 /* 4.1 client asking for a delegation? */
4130 if (open->op_deleg_want)
4131 nfsd4_open_deleg_none_ext(open, status);
4132 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133}
4134
Benny Halevye27f49c2012-02-21 14:16:54 -08004135static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4136 struct nfs4_delegation *dp)
4137{
4138 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4139 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4140 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4141 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4142 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4143 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4144 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4145 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4146 }
4147 /* Otherwise the client must be confused wanting a delegation
4148 * it already has, therefore we don't return
4149 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4150 */
4151}
4152
Al Virob37ad282006-10-19 23:28:59 -07004153__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4155{
Andy Adamson66689582009-04-03 08:28:45 +03004156 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004157 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004159 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004160 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004161 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 /*
4164 * Lookup file; if found, lookup stateid and check open request,
4165 * and check for delegations in the process of being recalled.
4166 * If not found, create the nfs4_file struct
4167 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004168 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004169 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004170 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004171 if (status)
4172 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04004173 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004175 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004176 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004177 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004178 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 }
4180
4181 /*
4182 * OPEN the file, or upgrade an existing OPEN.
4183 * If truncate fails, the OPEN fails.
4184 */
4185 if (stp) {
4186 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004187 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 if (status)
4189 goto out;
4190 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004191 stp = open->op_stp;
4192 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004193 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004194 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4195 if (status) {
4196 release_open_stateid(stp);
4197 goto out;
4198 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004199
4200 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4201 open->op_odstate);
4202 if (stp->st_clnt_odstate == open->op_odstate)
4203 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004205 update_stateid(&stp->st_stid.sc_stateid);
4206 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207
Benny Halevyd24433c2012-02-16 20:57:17 +02004208 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004209 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4210 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4211 open->op_why_no_deleg = WND4_NOT_WANTED;
4212 goto nodeleg;
4213 }
4214 }
4215
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 /*
4217 * Attempt to hand out a delegation. No error return, because the
4218 * OPEN succeeds even if we fail.
4219 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004220 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004221nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 status = nfs_ok;
4223
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004224 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004225 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004227 /* 4.1 client trying to upgrade/downgrade delegation? */
4228 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004229 open->op_deleg_want)
4230 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004231
NeilBrown13cd2182005-06-23 22:03:10 -07004232 if (fp)
4233 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004234 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08004235 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 /*
4237 * To finish the open response, we just need to set the rflags.
4238 */
4239 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004240 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004241 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004243 if (dp)
4244 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004245 if (stp)
4246 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247
4248 return status;
4249}
4250
Jeff Layton58fb12e2014-07-29 21:34:27 -04004251void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04004252 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004253{
4254 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004255 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004256
Jeff Laytond3134b12014-07-29 21:34:32 -04004257 nfsd4_cstate_assign_replay(cstate, so);
4258 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004259 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004260 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04004261 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004262 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004263 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004264 if (open->op_odstate)
4265 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004266}
4267
Al Virob37ad282006-10-19 23:28:59 -07004268__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004269nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4270 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271{
4272 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004273 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004274 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 dprintk("process_renew(%08x/%08x): starting\n",
4277 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004278 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004279 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004281 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004283 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004284 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 goto out;
4286 status = nfs_ok;
4287out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 return status;
4289}
4290
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004291void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004292nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004293{
Jeff Layton33dcc482012-04-10 11:08:48 -04004294 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004295 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004296 return;
4297
NeilBrowna76b4312005-06-23 22:04:01 -07004298 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004299 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004300 /*
4301 * If the server goes down again right now, an NFSv4
4302 * client will still be allowed to reclaim after it comes back up,
4303 * even if it hasn't yet had a chance to reclaim state this time.
4304 *
4305 */
Jeff Layton919b8042014-09-12 16:40:20 -04004306 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004307 /*
4308 * At this point, NFSv4 clients can still reclaim. But if the
4309 * server crashes, any that have not yet reclaimed will be out
4310 * of luck on the next boot.
4311 *
4312 * (NFSv4.1+ clients are considered to have reclaimed once they
4313 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4314 * have reclaimed after their first OPEN.)
4315 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004316 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004317 /*
4318 * At this point, and once lockd and/or any other containers
4319 * exit their grace period, further reclaims will fail and
4320 * regular locking can resume.
4321 */
NeilBrowna76b4312005-06-23 22:04:01 -07004322}
4323
NeilBrownfd39ca92005-06-23 22:04:03 -07004324static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004325nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326{
4327 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004328 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004330 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004332 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004333 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004336 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004337 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004338 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004339 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 clp = list_entry(pos, struct nfs4_client, cl_lru);
4341 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4342 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004343 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 break;
4345 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004346 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004347 dprintk("NFSD: client in use (clientid %08x)\n",
4348 clp->cl_clientid.cl_id);
4349 continue;
4350 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004351 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004352 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004353 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004354 list_for_each_safe(pos, next, &reaplist) {
4355 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 dprintk("NFSD: purging unused client (clientid %08x)\n",
4357 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004358 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 expire_client(clp);
4360 }
Benny Halevycdc97502014-05-30 09:09:30 -04004361 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004362 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4364 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004365 t = dp->dl_time - cutoff;
4366 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 break;
4368 }
Jeff Layton3fcbbd22015-08-24 12:41:48 -04004369 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04004370 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 }
Benny Halevycdc97502014-05-30 09:09:30 -04004372 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004373 while (!list_empty(&reaplist)) {
4374 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4375 dl_recall_lru);
4376 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004377 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 }
Jeff Layton217526e2014-07-30 08:27:11 -04004379
4380 spin_lock(&nn->client_lock);
4381 while (!list_empty(&nn->close_lru)) {
4382 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4383 oo_close_lru);
4384 if (time_after((unsigned long)oo->oo_time,
4385 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004386 t = oo->oo_time - cutoff;
4387 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 break;
4389 }
Jeff Layton217526e2014-07-30 08:27:11 -04004390 list_del_init(&oo->oo_close_lru);
4391 stp = oo->oo_last_closed_stid;
4392 oo->oo_last_closed_stid = NULL;
4393 spin_unlock(&nn->client_lock);
4394 nfs4_put_stid(&stp->st_stid);
4395 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 }
Jeff Layton217526e2014-07-30 08:27:11 -04004397 spin_unlock(&nn->client_lock);
4398
Jeff Laytona832e7a2014-05-30 09:09:26 -04004399 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004400 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401}
4402
Harvey Harrisona254b242008-02-20 12:49:00 -08004403static struct workqueue_struct *laundry_wq;
4404static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004405
4406static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004407laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408{
4409 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004410 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4411 work);
4412 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4413 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004415 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004417 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418}
4419
Jeff Layton8fcd4612015-07-30 06:57:46 -04004420static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004421{
Jeff Layton8fcd4612015-07-30 06:57:46 -04004422 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004423 return nfserr_bad_stateid;
4424 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425}
4426
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004428access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004430 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4431 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4432 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433}
4434
4435static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004436access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004438 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4439 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440}
4441
4442static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004443__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444{
Al Virob37ad282006-10-19 23:28:59 -07004445 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
J. Bruce Fields02921912010-07-29 15:16:59 -04004447 /* For lock stateid's, we test the parent open, not the lock: */
4448 if (stp->st_openstp)
4449 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004450 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004452 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 goto out;
4454 status = nfs_ok;
4455out:
4456 return status;
4457}
4458
Al Virob37ad282006-10-19 23:28:59 -07004459static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004460check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004462 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004464 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004465 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 * conflicting state; so we must wait out the grace period. */
4467 return nfserr_grace;
4468 } else if (flags & WR_STATE)
4469 return nfs4_share_conflict(current_fh,
4470 NFS4_SHARE_DENY_WRITE);
4471 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4472 return nfs4_share_conflict(current_fh,
4473 NFS4_SHARE_DENY_READ);
4474}
4475
4476/*
4477 * Allow READ/WRITE during grace period on recovered state only for files
4478 * that are not able to provide mandatory locking.
4479 */
4480static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004481grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004483 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484}
4485
J. Bruce Fields81b82962011-08-23 11:03:29 -04004486/* Returns true iff a is later than b: */
4487static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4488{
Jim Rees1a9357f2013-05-17 17:33:00 -04004489 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004490}
4491
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004492static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004493{
Andy Adamson66689582009-04-03 08:28:45 +03004494 /*
4495 * When sessions are used the stateid generation number is ignored
4496 * when it is zero.
4497 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004498 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004499 return nfs_ok;
4500
4501 if (in->si_generation == ref->si_generation)
4502 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004503
J. Bruce Fields0836f582008-01-26 19:08:12 -05004504 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004505 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004506 return nfserr_bad_stateid;
4507 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004508 * However, we could see a stateid from the past, even from a
4509 * non-buggy client. For example, if the client sends a lock
4510 * while some IO is outstanding, the lock may bump si_generation
4511 * while the IO is still in flight. The client could avoid that
4512 * situation by waiting for responses on all the IO requests,
4513 * but better performance may result in retrying IO that
4514 * receives an old_stateid error if requests are rarely
4515 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004516 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004517 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004518}
4519
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004520static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4521{
4522 if (ols->st_stateowner->so_is_open_owner &&
4523 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4524 return nfserr_bad_stateid;
4525 return nfs_ok;
4526}
4527
Chuck Lever7df302f2012-05-29 13:56:37 -04004528static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004529{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004530 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004531 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004532
Chuck Lever7df302f2012-05-29 13:56:37 -04004533 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004534 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004535 /* Client debugging aid. */
4536 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4537 char addr_str[INET6_ADDRSTRLEN];
4538 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4539 sizeof(addr_str));
4540 pr_warn_ratelimited("NFSD: client %s testing state ID "
4541 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004542 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004543 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004544 spin_lock(&cl->cl_lock);
4545 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004546 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004547 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004548 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004549 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004550 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004551 switch (s->sc_type) {
4552 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004553 status = nfs_ok;
4554 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004555 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004556 status = nfserr_deleg_revoked;
4557 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004558 case NFS4_OPEN_STID:
4559 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004560 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04004561 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004562 default:
4563 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004564 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004565 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004566 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004567 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004568 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004569out_unlock:
4570 spin_unlock(&cl->cl_lock);
4571 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004572}
4573
Christoph Hellwigcd61c522014-08-14 08:44:57 +02004574__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004575nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4576 stateid_t *stateid, unsigned char typemask,
4577 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004578{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004579 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004580
4581 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4582 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004583 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004584 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004585 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004586 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004587 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004588 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004589 if (status)
4590 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004591 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004592 if (!*s)
4593 return nfserr_bad_stateid;
4594 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004595}
4596
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004597static struct file *
4598nfs4_find_file(struct nfs4_stid *s, int flags)
4599{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004600 if (!s)
4601 return NULL;
4602
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004603 switch (s->sc_type) {
4604 case NFS4_DELEG_STID:
4605 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
4606 return NULL;
4607 return get_file(s->sc_file->fi_deleg_file);
4608 case NFS4_OPEN_STID:
4609 case NFS4_LOCK_STID:
4610 if (flags & RD_STATE)
4611 return find_readable_file(s->sc_file);
4612 else
4613 return find_writeable_file(s->sc_file);
4614 break;
4615 }
4616
4617 return NULL;
4618}
4619
4620static __be32
4621nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
4622{
4623 __be32 status;
4624
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004625 status = nfsd4_check_openowner_confirmed(ols);
4626 if (status)
4627 return status;
4628 return nfs4_check_openmode(ols, flags);
4629}
4630
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004631static __be32
4632nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
4633 struct file **filpp, bool *tmp_file, int flags)
4634{
4635 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
4636 struct file *file;
4637 __be32 status;
4638
4639 file = nfs4_find_file(s, flags);
4640 if (file) {
4641 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
4642 acc | NFSD_MAY_OWNER_OVERRIDE);
4643 if (status) {
4644 fput(file);
4645 return status;
4646 }
4647
4648 *filpp = file;
4649 } else {
4650 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
4651 if (status)
4652 return status;
4653
4654 if (tmp_file)
4655 *tmp_file = true;
4656 }
4657
4658 return 0;
4659}
4660
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004662 * Checks for stateid operations
4663 */
Al Virob37ad282006-10-19 23:28:59 -07004664__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004665nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
4666 struct nfsd4_compound_state *cstate, stateid_t *stateid,
4667 int flags, struct file **filpp, bool *tmp_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004669 struct svc_fh *fhp = &cstate->current_fh;
4670 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004671 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004672 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004673 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004674 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 if (filpp)
4677 *filpp = NULL;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004678 if (tmp_file)
4679 *tmp_file = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004681 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 return nfserr_grace;
4683
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004684 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
4685 status = check_special_stateids(net, fhp, stateid, flags);
4686 goto done;
4687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004689 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004690 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004691 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004692 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04004693 return status;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004694 status = check_stateid_generation(stateid, &s->sc_stateid,
4695 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04004696 if (status)
4697 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004698
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004699 switch (s->sc_type) {
4700 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004701 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004702 break;
4703 case NFS4_OPEN_STID:
4704 case NFS4_LOCK_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004705 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004706 break;
4707 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004708 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004709 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04004711 if (status)
4712 goto out;
4713 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004714
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004715done:
4716 if (!status && filpp)
4717 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718out:
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004719 if (s)
4720 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 return status;
4722}
4723
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004724/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004725 * Test if the stateid is valid
4726 */
4727__be32
4728nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4729 struct nfsd4_test_stateid *test_stateid)
4730{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004731 struct nfsd4_test_stateid_id *stateid;
4732 struct nfs4_client *cl = cstate->session->se_client;
4733
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004734 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004735 stateid->ts_id_status =
4736 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004737
Bryan Schumaker17456802011-07-13 10:50:48 -04004738 return nfs_ok;
4739}
4740
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004741__be32
4742nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4743 struct nfsd4_free_stateid *free_stateid)
4744{
4745 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004746 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004747 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004748 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004749 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004750 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004751
Jeff Layton1af71cc2014-07-29 21:34:14 -04004752 spin_lock(&cl->cl_lock);
4753 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004754 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004755 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004756 switch (s->sc_type) {
4757 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004758 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004759 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004760 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004761 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4762 if (ret)
4763 break;
4764 ret = nfserr_locks_held;
4765 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004766 case NFS4_LOCK_STID:
4767 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4768 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004769 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004770 stp = openlockstateid(s);
4771 ret = nfserr_locks_held;
4772 if (check_for_locks(stp->st_stid.sc_file,
4773 lockowner(stp->st_stateowner)))
4774 break;
Jeff Laytone8568732015-08-24 12:41:47 -04004775 WARN_ON(!unhash_lock_stateid(stp));
Jeff Layton1af71cc2014-07-29 21:34:14 -04004776 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004777 nfs4_put_stid(s);
4778 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004779 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004780 case NFS4_REVOKED_DELEG_STID:
4781 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004782 list_del_init(&dp->dl_recall_lru);
4783 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004784 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004785 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004786 goto out;
4787 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004788 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004789out_unlock:
4790 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004791out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004792 return ret;
4793}
4794
NeilBrown4c4cd222005-07-07 17:59:27 -07004795static inline int
4796setlkflg (int type)
4797{
4798 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4799 RD_STATE : WR_STATE;
4800}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004802static __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 -04004803{
4804 struct svc_fh *current_fh = &cstate->current_fh;
4805 struct nfs4_stateowner *sop = stp->st_stateowner;
4806 __be32 status;
4807
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004808 status = nfsd4_check_seqid(cstate, sop, seqid);
4809 if (status)
4810 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004811 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4812 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004813 /*
4814 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004815 * nfserr_replay_me from the previous step, and
4816 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004817 */
4818 return nfserr_bad_stateid;
4819 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4820 if (status)
4821 return status;
Jeff Layton8fcd4612015-07-30 06:57:46 -04004822 return nfs4_check_fh(current_fh, &stp->st_stid);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004823}
4824
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825/*
4826 * Checks for sequence id mutating operations.
4827 */
Al Virob37ad282006-10-19 23:28:59 -07004828static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004829nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004830 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004831 struct nfs4_ol_stateid **stpp,
4832 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004834 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004835 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004836 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004838 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4839 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004840
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004842 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004843 if (status)
4844 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004845 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004846 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004848 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004849 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004850 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004851 else
4852 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004853 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004854}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004855
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004856static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4857 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004858{
4859 __be32 status;
4860 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004861 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004863 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004864 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004865 if (status)
4866 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004867 oo = openowner(stp->st_stateowner);
4868 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4869 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004870 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004871 }
4872 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004873 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874}
4875
Al Virob37ad282006-10-19 23:28:59 -07004876__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004877nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004878 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879{
Al Virob37ad282006-10-19 23:28:59 -07004880 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004881 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004882 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004883 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884
Al Viroa6a9f182013-09-16 10:57:01 -04004885 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4886 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004888 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004889 if (status)
4890 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004892 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004893 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004894 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004895 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004896 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004897 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004898 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004899 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004900 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004901 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004902 update_stateid(&stp->st_stid.sc_stateid);
4903 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004904 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004905 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004906
Jeff Layton2a4317c2012-03-21 16:42:43 -04004907 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004908 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004909put_stateid:
4910 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004912 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 return status;
4914}
4915
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004916static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004918 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004919 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004920 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004921 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004922}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004923
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004924static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4925{
4926 switch (to_access) {
4927 case NFS4_SHARE_ACCESS_READ:
4928 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4929 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4930 break;
4931 case NFS4_SHARE_ACCESS_WRITE:
4932 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4933 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4934 break;
4935 case NFS4_SHARE_ACCESS_BOTH:
4936 break;
4937 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004938 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 }
4940}
4941
Al Virob37ad282006-10-19 23:28:59 -07004942__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004943nfsd4_open_downgrade(struct svc_rqst *rqstp,
4944 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004945 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946{
Al Virob37ad282006-10-19 23:28:59 -07004947 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004948 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004949 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950
Al Viroa6a9f182013-09-16 10:57:01 -04004951 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4952 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004954 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004955 if (od->od_deleg_want)
4956 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4957 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004959 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004960 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004961 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004964 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004965 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004967 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004969 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004970 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004972 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004974 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975
Jeff Laytonce0fc432012-05-11 09:45:14 -04004976 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004978 update_stateid(&stp->st_stid.sc_stateid);
4979 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004981put_stateid:
4982 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004984 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 return status;
4986}
4987
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004988static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4989{
Trond Myklebustacf92952014-06-30 11:48:37 -04004990 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04004991 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04004992 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004993
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004994 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04004995 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04004996 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004997
Jeff Laytond83017f2014-07-29 21:34:42 -04004998 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04004999 if (unhashed)
5000 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04005001 spin_unlock(&clp->cl_lock);
5002 free_ol_stateid_reaplist(&reaplist);
5003 } else {
5004 spin_unlock(&clp->cl_lock);
5005 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04005006 if (unhashed)
5007 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04005008 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04005009}
5010
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011/*
5012 * nfs4_unlock_state() called after encode
5013 */
Al Virob37ad282006-10-19 23:28:59 -07005014__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005015nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005016 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017{
Al Virob37ad282006-10-19 23:28:59 -07005018 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005019 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005020 struct net *net = SVC_NET(rqstp);
5021 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022
Al Viroa6a9f182013-09-16 10:57:01 -04005023 dprintk("NFSD: nfsd4_close on file %pd\n",
5024 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005026 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5027 &close->cl_stateid,
5028 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005029 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005030 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005031 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005033 update_stateid(&stp->st_stid.sc_stateid);
5034 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005036 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005037
5038 /* put reference from nfs4_preprocess_seqid_op */
5039 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 return status;
5042}
5043
Al Virob37ad282006-10-19 23:28:59 -07005044__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005045nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5046 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005048 struct nfs4_delegation *dp;
5049 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005050 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07005051 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005052 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005054 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005055 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005057 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005058 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005059 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005060 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04005061 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005062 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04005063 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005064
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005065 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005066put_stateid:
5067 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068out:
5069 return status;
5070}
5071
Benny Halevy87df4de2008-12-15 19:42:03 +02005072static inline u64
5073end_offset(u64 start, u64 len)
5074{
5075 u64 end;
5076
5077 end = start + len;
5078 return end >= start ? end: NFS4_MAX_UINT64;
5079}
5080
5081/* last octet in a range */
5082static inline u64
5083last_byte_offset(u64 start, u64 len)
5084{
5085 u64 end;
5086
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005087 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02005088 end = start + len;
5089 return end > start ? end - 1: NFS4_MAX_UINT64;
5090}
5091
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092/*
5093 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5094 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5095 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5096 * locking, this prevents us from being completely protocol-compliant. The
5097 * real solution to this problem is to start using unsigned file offsets in
5098 * the VFS, but this is a very deep change!
5099 */
5100static inline void
5101nfs4_transform_lock_offset(struct file_lock *lock)
5102{
5103 if (lock->fl_start < 0)
5104 lock->fl_start = OFFSET_MAX;
5105 if (lock->fl_end < 0)
5106 lock->fl_end = OFFSET_MAX;
5107}
5108
Jeff Laytoncae80b32015-04-03 09:04:04 -04005109static fl_owner_t
5110nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005111{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005112 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5113
5114 nfs4_get_stateowner(&lo->lo_owner);
5115 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005116}
5117
Jeff Laytoncae80b32015-04-03 09:04:04 -04005118static void
5119nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005120{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005121 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005122
Jeff Laytoncae80b32015-04-03 09:04:04 -04005123 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04005124 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04005125}
5126
Alexey Dobriyan7b021962009-09-21 17:01:12 -07005127static const struct lock_manager_operations nfsd_posix_mng_ops = {
Kinglong Meeaef95832014-08-22 10:18:44 -04005128 .lm_get_owner = nfsd4_fl_get_owner,
5129 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07005130};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131
5132static inline void
5133nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5134{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005135 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136
NeilBrownd5b90262006-04-10 22:55:22 -07005137 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005138 lo = (struct nfs4_lockowner *) fl->fl_owner;
5139 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5140 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005141 if (!deny->ld_owner.data)
5142 /* We just don't care that much */
5143 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005144 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5145 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07005146 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005147nevermind:
5148 deny->ld_owner.len = 0;
5149 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07005150 deny->ld_clientid.cl_boot = 0;
5151 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 }
5153 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02005154 deny->ld_length = NFS4_MAX_UINT64;
5155 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5157 deny->ld_type = NFS4_READ_LT;
5158 if (fl->fl_type != F_RDLCK)
5159 deny->ld_type = NFS4_WRITE_LT;
5160}
5161
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005162static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005163find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164{
Trond Myklebustd4f04892014-07-29 21:34:36 -04005165 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005166 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167
Trond Myklebust0a880a22014-07-30 08:27:10 -04005168 lockdep_assert_held(&clp->cl_lock);
5169
Trond Myklebustd4f04892014-07-29 21:34:36 -04005170 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5171 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005172 if (so->so_is_open_owner)
5173 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005174 if (same_owner_str(so, owner))
5175 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 }
5177 return NULL;
5178}
5179
Trond Myklebustc58c6612014-07-29 21:34:35 -04005180static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005181find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04005182{
5183 struct nfs4_lockowner *lo;
5184
Trond Myklebustd4f04892014-07-29 21:34:36 -04005185 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005186 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005187 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005188 return lo;
5189}
5190
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005191static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5192{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005193 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005194}
5195
Jeff Layton6b180f02014-07-29 21:34:26 -04005196static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5197{
5198 struct nfs4_lockowner *lo = lockowner(sop);
5199
5200 kmem_cache_free(lockowner_slab, lo);
5201}
5202
5203static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005204 .so_unhash = nfs4_unhash_lockowner,
5205 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005206};
5207
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208/*
5209 * Alloc a lock owner structure.
5210 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005211 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005213 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005215static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005216alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5217 struct nfs4_ol_stateid *open_stp,
5218 struct nfsd4_lock *lock)
5219{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005220 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005222 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5223 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005225 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5226 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005227 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005228 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005229 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005230 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005231 if (ret == NULL) {
5232 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005233 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005234 ret = lo;
5235 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08005236 nfs4_free_stateowner(&lo->lo_owner);
5237
Trond Myklebustd4f04892014-07-29 21:34:36 -04005238 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04005239 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240}
5241
Jeff Layton356a95e2014-07-29 21:34:13 -04005242static void
5243init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5244 struct nfs4_file *fp, struct inode *inode,
5245 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005247 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248
Jeff Layton356a95e2014-07-29 21:34:13 -04005249 lockdep_assert_held(&clp->cl_lock);
5250
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005251 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005252 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005253 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07005254 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005255 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005256 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005257 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005259 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005260 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005261 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005262 spin_lock(&fp->fi_lock);
5263 list_add(&stp->st_perfile, &fp->fi_stateids);
5264 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265}
5266
Jeff Laytonc53530d2014-06-30 11:48:39 -04005267static struct nfs4_ol_stateid *
5268find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5269{
5270 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005271 struct nfs4_client *clp = lo->lo_owner.so_client;
5272
5273 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005274
5275 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005276 if (lst->st_stid.sc_file == fp) {
5277 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005278 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005279 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005280 }
5281 return NULL;
5282}
5283
Jeff Layton356a95e2014-07-29 21:34:13 -04005284static struct nfs4_ol_stateid *
5285find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5286 struct inode *inode, struct nfs4_ol_stateid *ost,
5287 bool *new)
5288{
5289 struct nfs4_stid *ns = NULL;
5290 struct nfs4_ol_stateid *lst;
5291 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5292 struct nfs4_client *clp = oo->oo_owner.so_client;
5293
5294 spin_lock(&clp->cl_lock);
5295 lst = find_lock_stateid(lo, fi);
5296 if (lst == NULL) {
5297 spin_unlock(&clp->cl_lock);
5298 ns = nfs4_alloc_stid(clp, stateid_slab);
5299 if (ns == NULL)
5300 return NULL;
5301
5302 spin_lock(&clp->cl_lock);
5303 lst = find_lock_stateid(lo, fi);
5304 if (likely(!lst)) {
5305 lst = openlockstateid(ns);
5306 init_lock_stateid(lst, lo, fi, inode, ost);
5307 ns = NULL;
5308 *new = true;
5309 }
5310 }
5311 spin_unlock(&clp->cl_lock);
5312 if (ns)
5313 nfs4_put_stid(ns);
5314 return lst;
5315}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005316
NeilBrownfd39ca92005-06-23 22:04:03 -07005317static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318check_lock_length(u64 offset, u64 length)
5319{
Kinglong Meee7969312015-07-13 17:34:19 +08005320 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5321 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322}
5323
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005324static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005325{
Trond Myklebust11b91642014-07-29 21:34:08 -04005326 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005327
Jeff Layton7214e862014-07-10 14:07:33 -04005328 lockdep_assert_held(&fp->fi_lock);
5329
Jeff Layton82c5ff12012-05-11 09:45:13 -04005330 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005331 return;
Jeff Layton12659652014-07-10 14:07:28 -04005332 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005333 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005334}
5335
Jeff Layton356a95e2014-07-29 21:34:13 -04005336static __be32
5337lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5338 struct nfs4_ol_stateid *ost,
5339 struct nfsd4_lock *lock,
5340 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005341{
Jeff Layton5db1c032014-07-29 21:34:28 -04005342 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005343 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005344 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5345 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00005346 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005347 struct nfs4_lockowner *lo;
5348 unsigned int strhashval;
5349
Kinglong Meec8623992015-07-13 17:35:37 +08005350 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005351 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005352 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005353 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5354 if (lo == NULL)
5355 return nfserr_jukebox;
5356 } else {
5357 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005358 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005359 if (!cstate->minorversion &&
5360 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005361 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005362 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005363
Jeff Layton356a95e2014-07-29 21:34:13 -04005364 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005365 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005366 status = nfserr_jukebox;
5367 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005368 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005369 status = nfs_ok;
5370out:
5371 nfs4_put_stateowner(&lo->lo_owner);
5372 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005373}
5374
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375/*
5376 * LOCK operation
5377 */
Al Virob37ad282006-10-19 23:28:59 -07005378__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005379nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005380 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005382 struct nfs4_openowner *open_sop = NULL;
5383 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005384 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005385 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005386 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005387 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005388 struct file_lock *file_lock = NULL;
5389 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005390 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005391 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005392 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005393 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005394 struct net *net = SVC_NET(rqstp);
5395 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396
5397 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5398 (long long) lock->lk_offset,
5399 (long long) lock->lk_length);
5400
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 if (check_lock_length(lock->lk_offset, lock->lk_length))
5402 return nfserr_inval;
5403
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005404 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005405 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005406 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5407 return status;
5408 }
5409
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005411 if (nfsd4_has_session(cstate))
5412 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005413 memcpy(&lock->lk_new_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04005414 &cstate->session->se_client->cl_clientid,
5415 sizeof(clientid_t));
5416
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005418 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005422 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 lock->lk_new_open_seqid,
5424 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005425 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005426 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005428 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005429 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005430 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005431 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005432 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005433 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005434 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005435 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005436 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005437 lock->lk_old_lock_seqid,
5438 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005439 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005440 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005441 if (status)
5442 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005443 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005445 lkflg = setlkflg(lock->lk_type);
5446 status = nfs4_check_openmode(lock_stp, lkflg);
5447 if (status)
5448 goto out;
5449
NeilBrown0dd395d2005-07-07 17:59:15 -07005450 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005451 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005452 goto out;
5453 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005454 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005455 goto out;
5456
Jeff Layton21179d82012-08-21 08:03:32 -04005457 file_lock = locks_alloc_lock();
5458 if (!file_lock) {
5459 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5460 status = nfserr_jukebox;
5461 goto out;
5462 }
5463
Trond Myklebust11b91642014-07-29 21:34:08 -04005464 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 switch (lock->lk_type) {
5466 case NFS4_READ_LT:
5467 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005468 spin_lock(&fp->fi_lock);
5469 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005470 if (filp)
5471 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005472 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005473 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005474 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 case NFS4_WRITE_LT:
5476 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005477 spin_lock(&fp->fi_lock);
5478 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005479 if (filp)
5480 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005481 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005482 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005483 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 default:
5485 status = nfserr_inval;
5486 goto out;
5487 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005488 if (!filp) {
5489 status = nfserr_openmode;
5490 goto out;
5491 }
Kinglong Meeaef95832014-08-22 10:18:44 -04005492
5493 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04005494 file_lock->fl_pid = current->tgid;
5495 file_lock->fl_file = filp;
5496 file_lock->fl_flags = FL_POSIX;
5497 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5498 file_lock->fl_start = lock->lk_offset;
5499 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5500 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501
Jeff Layton21179d82012-08-21 08:03:32 -04005502 conflock = locks_alloc_lock();
5503 if (!conflock) {
5504 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5505 status = nfserr_jukebox;
5506 goto out;
5507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508
Jeff Layton21179d82012-08-21 08:03:32 -04005509 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005510 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005512 update_stateid(&lock_stp->st_stid.sc_stateid);
5513 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005515 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005516 break;
5517 case (EAGAIN): /* conflock holds conflicting lock */
5518 status = nfserr_denied;
5519 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005520 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005521 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 case (EDEADLK):
5523 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005524 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005525 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005526 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005527 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005528 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530out:
Trond Myklebustde186432014-07-10 14:07:26 -04005531 if (filp)
5532 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005533 if (lock_stp) {
5534 /* Bump seqid manually if the 4.0 replay owner is openowner */
5535 if (cstate->replay_owner &&
5536 cstate->replay_owner != &lock_sop->lo_owner &&
5537 seqid_mutating_err(ntohl(status)))
5538 lock_sop->lo_owner.so_seqid++;
5539
5540 /*
5541 * If this is a new, never-before-used stateid, and we are
5542 * returning an error, then just go ahead and release it.
5543 */
5544 if (status && new)
5545 release_lock_stateid(lock_stp);
5546
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005547 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005548 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005549 if (open_stp)
5550 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005551 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005552 if (file_lock)
5553 locks_free_lock(file_lock);
5554 if (conflock)
5555 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 return status;
5557}
5558
5559/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005560 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5561 * so we do a temporary open here just to get an open file to pass to
5562 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5563 * inode operation.)
5564 */
Al Viro04da6e92012-04-13 00:00:04 -04005565static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005566{
5567 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005568 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5569 if (!err) {
5570 err = nfserrno(vfs_test_lock(file, lock));
Christoph Hellwigfd891452015-04-28 15:41:16 +02005571 fput(file);
Al Viro04da6e92012-04-13 00:00:04 -04005572 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005573 return err;
5574}
5575
5576/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 * LOCKT operation
5578 */
Al Virob37ad282006-10-19 23:28:59 -07005579__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005580nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5581 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582{
Jeff Layton21179d82012-08-21 08:03:32 -04005583 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005584 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005585 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005586 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005588 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 return nfserr_grace;
5590
5591 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5592 return nfserr_inval;
5593
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005594 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005595 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005596 if (status)
5597 goto out;
5598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005600 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602
Jeff Layton21179d82012-08-21 08:03:32 -04005603 file_lock = locks_alloc_lock();
5604 if (!file_lock) {
5605 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5606 status = nfserr_jukebox;
5607 goto out;
5608 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005609
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 switch (lockt->lt_type) {
5611 case NFS4_READ_LT:
5612 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005613 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 break;
5615 case NFS4_WRITE_LT:
5616 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005617 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 break;
5619 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005620 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 status = nfserr_inval;
5622 goto out;
5623 }
5624
Kinglong Meec8623992015-07-13 17:35:37 +08005625 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005626 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005627 file_lock->fl_owner = (fl_owner_t)lo;
5628 file_lock->fl_pid = current->tgid;
5629 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630
Jeff Layton21179d82012-08-21 08:03:32 -04005631 file_lock->fl_start = lockt->lt_offset;
5632 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633
Jeff Layton21179d82012-08-21 08:03:32 -04005634 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635
Jeff Layton21179d82012-08-21 08:03:32 -04005636 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005637 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005638 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005639
Jeff Layton21179d82012-08-21 08:03:32 -04005640 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005642 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 }
5644out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005645 if (lo)
5646 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04005647 if (file_lock)
5648 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 return status;
5650}
5651
Al Virob37ad282006-10-19 23:28:59 -07005652__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005653nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005654 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005656 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005658 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005659 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005660 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005661 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5662
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5664 (long long) locku->lu_offset,
5665 (long long) locku->lu_length);
5666
5667 if (check_lock_length(locku->lu_offset, locku->lu_length))
5668 return nfserr_inval;
5669
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005670 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005671 &locku->lu_stateid, NFS4_LOCK_STID,
5672 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005673 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005675 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005676 if (!filp) {
5677 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005678 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005679 }
Jeff Layton21179d82012-08-21 08:03:32 -04005680 file_lock = locks_alloc_lock();
5681 if (!file_lock) {
5682 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5683 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005684 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005685 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005686
Jeff Layton21179d82012-08-21 08:03:32 -04005687 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04005688 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04005689 file_lock->fl_pid = current->tgid;
5690 file_lock->fl_file = filp;
5691 file_lock->fl_flags = FL_POSIX;
5692 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5693 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694
Jeff Layton21179d82012-08-21 08:03:32 -04005695 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5696 locku->lu_length);
5697 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698
Jeff Layton21179d82012-08-21 08:03:32 -04005699 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005700 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005701 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 goto out_nfserr;
5703 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005704 update_stateid(&stp->st_stid.sc_stateid);
5705 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005706fput:
5707 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005708put_stateid:
5709 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005711 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005712 if (file_lock)
5713 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 return status;
5715
5716out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005717 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005718 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719}
5720
5721/*
5722 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005723 * true: locks held by lockowner
5724 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005726static bool
5727check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005729 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005730 int status = false;
5731 struct file *filp = find_any_file(fp);
5732 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005733 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005734
5735 if (!filp) {
5736 /* Any valid lock stateid should have some sort of access */
5737 WARN_ON_ONCE(1);
5738 return status;
5739 }
5740
5741 inode = file_inode(filp);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005742 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005744 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05005745 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05005746 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
5747 if (fl->fl_owner == (fl_owner_t)lowner) {
5748 status = true;
5749 break;
5750 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005751 }
Jeff Layton6109c852015-01-16 15:05:57 -05005752 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 }
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005754 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 return status;
5756}
5757
Al Virob37ad282006-10-19 23:28:59 -07005758__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005759nfsd4_release_lockowner(struct svc_rqst *rqstp,
5760 struct nfsd4_compound_state *cstate,
5761 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762{
5763 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005764 struct nfs4_stateowner *sop;
5765 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005766 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005768 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005769 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005770 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005771 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772
5773 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5774 clid->cl_boot, clid->cl_id);
5775
Jeff Layton4b24ca72014-06-30 11:48:44 -04005776 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005777 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04005778 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005779
Trond Myklebustd4f04892014-07-29 21:34:36 -04005780 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005781 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005782 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005783 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005784 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005785
5786 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005787 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005788
Jeff Layton882e9d22014-07-29 21:34:37 -04005789 /* see if there are still any locks associated with it */
5790 lo = lockowner(sop);
5791 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5792 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5793 status = nfserr_locks_held;
5794 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04005795 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04005796 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005797 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005798
Kinglong Meeb5971af2014-08-22 10:18:43 -04005799 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04005800 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005801 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005802 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005803 if (lo)
5804 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 return status;
5806}
5807
5808static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005809alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810{
NeilBrowna55370a2005-06-23 22:03:52 -07005811 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812}
5813
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005814bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005815nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005816{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005817 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005818
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005819 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005820 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005821}
5822
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823/*
5824 * failure => all reset bets are off, nfserr_no_grace...
5825 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005826struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005827nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828{
5829 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005830 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831
NeilBrowna55370a2005-06-23 22:03:52 -07005832 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5833 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005834 if (crp) {
5835 strhashval = clientstr_hashval(name);
5836 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005837 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005838 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005839 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005840 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005841 }
5842 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843}
5844
Jeff Layton2a4317c2012-03-21 16:42:43 -04005845void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005846nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005847{
5848 list_del(&crp->cr_strhash);
5849 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005850 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005851}
5852
5853void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005854nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855{
5856 struct nfs4_client_reclaim *crp = NULL;
5857 int i;
5858
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005860 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5861 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005863 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 }
5865 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005866 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867}
5868
5869/*
5870 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005871struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005872nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873{
5874 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 struct nfs4_client_reclaim *crp = NULL;
5876
Jeff Layton278c9312012-11-12 15:00:52 -05005877 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878
Jeff Layton278c9312012-11-12 15:00:52 -05005879 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005880 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005881 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 return crp;
5883 }
5884 }
5885 return NULL;
5886}
5887
5888/*
5889* Called from OPEN. Look for clientid in reclaim list.
5890*/
Al Virob37ad282006-10-19 23:28:59 -07005891__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005892nfs4_check_open_reclaim(clientid_t *clid,
5893 struct nfsd4_compound_state *cstate,
5894 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005896 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005897
5898 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005899 status = lookup_clientid(clid, cstate, nn);
5900 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005901 return nfserr_reclaim_bad;
5902
Jeff Layton3b3e7b72014-09-12 16:40:22 -04005903 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
5904 return nfserr_no_grace;
5905
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005906 if (nfsd4_client_record_check(cstate->clp))
5907 return nfserr_reclaim_bad;
5908
5909 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910}
5911
Bryan Schumaker65178db2011-11-01 13:35:21 -04005912#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04005913static inline void
5914put_client(struct nfs4_client *clp)
5915{
5916 atomic_dec(&clp->cl_refcount);
5917}
5918
Jeff Layton285abde2014-07-30 08:27:24 -04005919static struct nfs4_client *
5920nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5921{
5922 struct nfs4_client *clp;
5923 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5924 nfsd_net_id);
5925
5926 if (!nfsd_netns_ready(nn))
5927 return NULL;
5928
5929 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5930 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5931 return clp;
5932 }
5933 return NULL;
5934}
5935
Jeff Layton7ec0e362014-07-30 08:27:17 -04005936u64
Jeff Layton285abde2014-07-30 08:27:24 -04005937nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04005938{
5939 struct nfs4_client *clp;
5940 u64 count = 0;
5941 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5942 nfsd_net_id);
5943 char buf[INET6_ADDRSTRLEN];
5944
5945 if (!nfsd_netns_ready(nn))
5946 return 0;
5947
5948 spin_lock(&nn->client_lock);
5949 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5950 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5951 pr_info("NFS Client: %s\n", buf);
5952 ++count;
5953 }
5954 spin_unlock(&nn->client_lock);
5955
5956 return count;
5957}
Bryan Schumaker65178db2011-11-01 13:35:21 -04005958
Jeff Laytona0926d12014-07-30 08:27:18 -04005959u64
Jeff Layton285abde2014-07-30 08:27:24 -04005960nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04005961{
5962 u64 count = 0;
5963 struct nfs4_client *clp;
5964 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5965 nfsd_net_id);
5966
5967 if (!nfsd_netns_ready(nn))
5968 return count;
5969
5970 spin_lock(&nn->client_lock);
5971 clp = nfsd_find_client(addr, addr_size);
5972 if (clp) {
5973 if (mark_client_expired_locked(clp) == nfs_ok)
5974 ++count;
5975 else
5976 clp = NULL;
5977 }
5978 spin_unlock(&nn->client_lock);
5979
5980 if (clp)
5981 expire_client(clp);
5982
5983 return count;
5984}
5985
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005986u64
Jeff Layton285abde2014-07-30 08:27:24 -04005987nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005988{
5989 u64 count = 0;
5990 struct nfs4_client *clp, *next;
5991 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5992 nfsd_net_id);
5993 LIST_HEAD(reaplist);
5994
5995 if (!nfsd_netns_ready(nn))
5996 return count;
5997
5998 spin_lock(&nn->client_lock);
5999 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6000 if (mark_client_expired_locked(clp) == nfs_ok) {
6001 list_add(&clp->cl_lru, &reaplist);
6002 if (max != 0 && ++count >= max)
6003 break;
6004 }
6005 }
6006 spin_unlock(&nn->client_lock);
6007
6008 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6009 expire_client(clp);
6010
6011 return count;
6012}
6013
Bryan Schumaker184c1842012-11-29 11:40:44 -05006014static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6015 const char *type)
6016{
6017 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05006018 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05006019 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6020}
6021
Jeff Layton016200c2014-07-30 08:27:21 -04006022static void
6023nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6024 struct list_head *collect)
6025{
6026 struct nfs4_client *clp = lst->st_stid.sc_client;
6027 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6028 nfsd_net_id);
6029
6030 if (!collect)
6031 return;
6032
6033 lockdep_assert_held(&nn->client_lock);
6034 atomic_inc(&clp->cl_refcount);
6035 list_add(&lst->st_locks, collect);
6036}
6037
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006038static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04006039 struct list_head *collect,
Jeff Laytone8568732015-08-24 12:41:47 -04006040 bool (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05006041{
6042 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006043 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006044 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006045 u64 count = 0;
6046
Jeff Layton016200c2014-07-30 08:27:21 -04006047 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006048 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006049 list_for_each_entry_safe(stp, st_next,
6050 &oop->oo_owner.so_stateids, st_perstateowner) {
6051 list_for_each_entry_safe(lst, lst_next,
6052 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04006053 if (func) {
Jeff Laytone8568732015-08-24 12:41:47 -04006054 if (func(lst))
6055 nfsd_inject_add_lock_to_list(lst,
6056 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04006057 }
Jeff Layton016200c2014-07-30 08:27:21 -04006058 ++count;
6059 /*
6060 * Despite the fact that these functions deal
6061 * with 64-bit integers for "count", we must
6062 * ensure that it doesn't blow up the
6063 * clp->cl_refcount. Throw a warning if we
6064 * start to approach INT_MAX here.
6065 */
6066 WARN_ON_ONCE(count == (INT_MAX / 2));
6067 if (count == max)
6068 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006069 }
6070 }
6071 }
Jeff Layton016200c2014-07-30 08:27:21 -04006072out:
6073 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006074
6075 return count;
6076}
6077
Jeff Layton016200c2014-07-30 08:27:21 -04006078static u64
6079nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6080 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05006081{
Jeff Layton016200c2014-07-30 08:27:21 -04006082 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006083}
6084
Jeff Layton016200c2014-07-30 08:27:21 -04006085static u64
6086nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006087{
Jeff Layton016200c2014-07-30 08:27:21 -04006088 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006089 nfsd_print_count(clp, count, "locked files");
6090 return count;
6091}
6092
Jeff Layton016200c2014-07-30 08:27:21 -04006093u64
Jeff Layton285abde2014-07-30 08:27:24 -04006094nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04006095{
6096 struct nfs4_client *clp;
6097 u64 count = 0;
6098 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6099 nfsd_net_id);
6100
6101 if (!nfsd_netns_ready(nn))
6102 return 0;
6103
6104 spin_lock(&nn->client_lock);
6105 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6106 count += nfsd_print_client_locks(clp);
6107 spin_unlock(&nn->client_lock);
6108
6109 return count;
6110}
6111
6112static void
6113nfsd_reap_locks(struct list_head *reaplist)
6114{
6115 struct nfs4_client *clp;
6116 struct nfs4_ol_stateid *stp, *next;
6117
6118 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6119 list_del_init(&stp->st_locks);
6120 clp = stp->st_stid.sc_client;
6121 nfs4_put_stid(&stp->st_stid);
6122 put_client(clp);
6123 }
6124}
6125
6126u64
Jeff Layton285abde2014-07-30 08:27:24 -04006127nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04006128{
6129 unsigned int count = 0;
6130 struct nfs4_client *clp;
6131 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6132 nfsd_net_id);
6133 LIST_HEAD(reaplist);
6134
6135 if (!nfsd_netns_ready(nn))
6136 return count;
6137
6138 spin_lock(&nn->client_lock);
6139 clp = nfsd_find_client(addr, addr_size);
6140 if (clp)
6141 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6142 spin_unlock(&nn->client_lock);
6143 nfsd_reap_locks(&reaplist);
6144 return count;
6145}
6146
6147u64
Jeff Layton285abde2014-07-30 08:27:24 -04006148nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04006149{
6150 u64 count = 0;
6151 struct nfs4_client *clp;
6152 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6153 nfsd_net_id);
6154 LIST_HEAD(reaplist);
6155
6156 if (!nfsd_netns_ready(nn))
6157 return count;
6158
6159 spin_lock(&nn->client_lock);
6160 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6161 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6162 if (max != 0 && count >= max)
6163 break;
6164 }
6165 spin_unlock(&nn->client_lock);
6166 nfsd_reap_locks(&reaplist);
6167 return count;
6168}
6169
Jeff Layton82e05ef2014-07-30 08:27:22 -04006170static u64
6171nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6172 struct list_head *collect,
6173 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006174{
6175 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04006176 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6177 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006178 u64 count = 0;
6179
Jeff Layton82e05ef2014-07-30 08:27:22 -04006180 lockdep_assert_held(&nn->client_lock);
6181
6182 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006183 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04006184 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006185 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04006186 if (collect) {
6187 atomic_inc(&clp->cl_refcount);
6188 list_add(&oop->oo_perclient, collect);
6189 }
6190 }
6191 ++count;
6192 /*
6193 * Despite the fact that these functions deal with
6194 * 64-bit integers for "count", we must ensure that
6195 * it doesn't blow up the clp->cl_refcount. Throw a
6196 * warning if we start to approach INT_MAX here.
6197 */
6198 WARN_ON_ONCE(count == (INT_MAX / 2));
6199 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006200 break;
6201 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04006202 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006203
6204 return count;
6205}
6206
Jeff Layton82e05ef2014-07-30 08:27:22 -04006207static u64
6208nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006209{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006210 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6211
6212 nfsd_print_count(clp, count, "openowners");
6213 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006214}
6215
Jeff Layton82e05ef2014-07-30 08:27:22 -04006216static u64
6217nfsd_collect_client_openowners(struct nfs4_client *clp,
6218 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006219{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006220 return nfsd_foreach_client_openowner(clp, max, collect,
6221 unhash_openowner_locked);
6222}
6223
6224u64
Jeff Layton285abde2014-07-30 08:27:24 -04006225nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006226{
6227 struct nfs4_client *clp;
6228 u64 count = 0;
6229 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6230 nfsd_net_id);
6231
6232 if (!nfsd_netns_ready(nn))
6233 return 0;
6234
6235 spin_lock(&nn->client_lock);
6236 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6237 count += nfsd_print_client_openowners(clp);
6238 spin_unlock(&nn->client_lock);
6239
6240 return count;
6241}
6242
6243static void
6244nfsd_reap_openowners(struct list_head *reaplist)
6245{
6246 struct nfs4_client *clp;
6247 struct nfs4_openowner *oop, *next;
6248
6249 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6250 list_del_init(&oop->oo_perclient);
6251 clp = oop->oo_owner.so_client;
6252 release_openowner(oop);
6253 put_client(clp);
6254 }
6255}
6256
6257u64
Jeff Layton285abde2014-07-30 08:27:24 -04006258nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6259 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006260{
6261 unsigned int count = 0;
6262 struct nfs4_client *clp;
6263 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6264 nfsd_net_id);
6265 LIST_HEAD(reaplist);
6266
6267 if (!nfsd_netns_ready(nn))
6268 return count;
6269
6270 spin_lock(&nn->client_lock);
6271 clp = nfsd_find_client(addr, addr_size);
6272 if (clp)
6273 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6274 spin_unlock(&nn->client_lock);
6275 nfsd_reap_openowners(&reaplist);
6276 return count;
6277}
6278
6279u64
Jeff Layton285abde2014-07-30 08:27:24 -04006280nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006281{
6282 u64 count = 0;
6283 struct nfs4_client *clp;
6284 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6285 nfsd_net_id);
6286 LIST_HEAD(reaplist);
6287
6288 if (!nfsd_netns_ready(nn))
6289 return count;
6290
6291 spin_lock(&nn->client_lock);
6292 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6293 count += nfsd_collect_client_openowners(clp, &reaplist,
6294 max - count);
6295 if (max != 0 && count >= max)
6296 break;
6297 }
6298 spin_unlock(&nn->client_lock);
6299 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006300 return count;
6301}
6302
Bryan Schumaker269de302012-11-29 11:40:42 -05006303static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6304 struct list_head *victims)
6305{
6306 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006307 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6308 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006309 u64 count = 0;
6310
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006311 lockdep_assert_held(&nn->client_lock);
6312
6313 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006314 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006315 if (victims) {
6316 /*
6317 * It's not safe to mess with delegations that have a
6318 * non-zero dl_time. They might have already been broken
6319 * and could be processed by the laundromat outside of
6320 * the state_lock. Just leave them be.
6321 */
6322 if (dp->dl_time != 0)
6323 continue;
6324
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006325 atomic_inc(&clp->cl_refcount);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04006326 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04006327 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006328 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006329 ++count;
6330 /*
6331 * Despite the fact that these functions deal with
6332 * 64-bit integers for "count", we must ensure that
6333 * it doesn't blow up the clp->cl_refcount. Throw a
6334 * warning if we start to approach INT_MAX here.
6335 */
6336 WARN_ON_ONCE(count == (INT_MAX / 2));
6337 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006338 break;
6339 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006340 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006341 return count;
6342}
6343
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006344static u64
6345nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006346{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006347 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006348
6349 nfsd_print_count(clp, count, "delegations");
6350 return count;
6351}
6352
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006353u64
Jeff Layton285abde2014-07-30 08:27:24 -04006354nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006355{
6356 struct nfs4_client *clp;
6357 u64 count = 0;
6358 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6359 nfsd_net_id);
6360
6361 if (!nfsd_netns_ready(nn))
6362 return 0;
6363
6364 spin_lock(&nn->client_lock);
6365 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6366 count += nfsd_print_client_delegations(clp);
6367 spin_unlock(&nn->client_lock);
6368
6369 return count;
6370}
6371
6372static void
6373nfsd_forget_delegations(struct list_head *reaplist)
6374{
6375 struct nfs4_client *clp;
6376 struct nfs4_delegation *dp, *next;
6377
6378 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6379 list_del_init(&dp->dl_recall_lru);
6380 clp = dp->dl_stid.sc_client;
6381 revoke_delegation(dp);
6382 put_client(clp);
6383 }
6384}
6385
6386u64
Jeff Layton285abde2014-07-30 08:27:24 -04006387nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6388 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006389{
6390 u64 count = 0;
6391 struct nfs4_client *clp;
6392 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6393 nfsd_net_id);
6394 LIST_HEAD(reaplist);
6395
6396 if (!nfsd_netns_ready(nn))
6397 return count;
6398
6399 spin_lock(&nn->client_lock);
6400 clp = nfsd_find_client(addr, addr_size);
6401 if (clp)
6402 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6403 spin_unlock(&nn->client_lock);
6404
6405 nfsd_forget_delegations(&reaplist);
6406 return count;
6407}
6408
6409u64
Jeff Layton285abde2014-07-30 08:27:24 -04006410nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006411{
6412 u64 count = 0;
6413 struct nfs4_client *clp;
6414 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6415 nfsd_net_id);
6416 LIST_HEAD(reaplist);
6417
6418 if (!nfsd_netns_ready(nn))
6419 return count;
6420
6421 spin_lock(&nn->client_lock);
6422 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6423 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6424 if (max != 0 && count >= max)
6425 break;
6426 }
6427 spin_unlock(&nn->client_lock);
6428 nfsd_forget_delegations(&reaplist);
6429 return count;
6430}
6431
6432static void
6433nfsd_recall_delegations(struct list_head *reaplist)
6434{
6435 struct nfs4_client *clp;
6436 struct nfs4_delegation *dp, *next;
6437
6438 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6439 list_del_init(&dp->dl_recall_lru);
6440 clp = dp->dl_stid.sc_client;
6441 /*
6442 * We skipped all entries that had a zero dl_time before,
6443 * so we can now reset the dl_time back to 0. If a delegation
6444 * break comes in now, then it won't make any difference since
6445 * we're recalling it either way.
6446 */
6447 spin_lock(&state_lock);
6448 dp->dl_time = 0;
6449 spin_unlock(&state_lock);
6450 nfsd_break_one_deleg(dp);
6451 put_client(clp);
6452 }
6453}
6454
6455u64
Jeff Layton285abde2014-07-30 08:27:24 -04006456nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006457 size_t addr_size)
6458{
6459 u64 count = 0;
6460 struct nfs4_client *clp;
6461 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6462 nfsd_net_id);
6463 LIST_HEAD(reaplist);
6464
6465 if (!nfsd_netns_ready(nn))
6466 return count;
6467
6468 spin_lock(&nn->client_lock);
6469 clp = nfsd_find_client(addr, addr_size);
6470 if (clp)
6471 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6472 spin_unlock(&nn->client_lock);
6473
6474 nfsd_recall_delegations(&reaplist);
6475 return count;
6476}
6477
6478u64
Jeff Layton285abde2014-07-30 08:27:24 -04006479nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006480{
6481 u64 count = 0;
6482 struct nfs4_client *clp, *next;
6483 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6484 nfsd_net_id);
6485 LIST_HEAD(reaplist);
6486
6487 if (!nfsd_netns_ready(nn))
6488 return count;
6489
6490 spin_lock(&nn->client_lock);
6491 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6492 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6493 if (max != 0 && ++count >= max)
6494 break;
6495 }
6496 spin_unlock(&nn->client_lock);
6497 nfsd_recall_delegations(&reaplist);
6498 return count;
6499}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006500#endif /* CONFIG_NFSD_FAULT_INJECTION */
6501
Meelap Shahc2f1a552007-07-17 04:04:39 -07006502/*
6503 * Since the lifetime of a delegation isn't limited to that of an open, a
6504 * client may quite reasonably hang on to a delegation as long as it has
6505 * the inode cached. This becomes an obvious problem the first time a
6506 * client's inode cache approaches the size of the server's total memory.
6507 *
6508 * For now we avoid this problem by imposing a hard limit on the number
6509 * of delegations, which varies according to the server's memory size.
6510 */
6511static void
6512set_max_delegations(void)
6513{
6514 /*
6515 * Allow at most 4 delegations per megabyte of RAM. Quick
6516 * estimates suggest that in the worst case (where every delegation
6517 * is for a different inode), a delegation could take about 1.5K,
6518 * giving a worst case usage of about 6% of memory.
6519 */
6520 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6521}
6522
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006523static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006524{
6525 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6526 int i;
6527
6528 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6529 CLIENT_HASH_SIZE, GFP_KERNEL);
6530 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006531 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006532 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6533 CLIENT_HASH_SIZE, GFP_KERNEL);
6534 if (!nn->unconf_id_hashtbl)
6535 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006536 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6537 SESSION_HASH_SIZE, GFP_KERNEL);
6538 if (!nn->sessionid_hashtbl)
6539 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006540
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006541 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006542 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006543 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006544 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006545 for (i = 0; i < SESSION_HASH_SIZE; i++)
6546 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006547 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006548 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03006549 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03006550 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006551 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03006552 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006553
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006554 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006555 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006556
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006557 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006558
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006559err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03006560 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006561err_unconf_id:
6562 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006563err:
6564 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006565}
6566
6567static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006568nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006569{
6570 int i;
6571 struct nfs4_client *clp = NULL;
6572 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6573
6574 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6575 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6576 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6577 destroy_client(clp);
6578 }
6579 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006580
Kinglong Mee2b905632014-03-26 22:09:30 +08006581 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6582 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6583 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6584 destroy_client(clp);
6585 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006586 }
6587
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006588 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006589 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006590 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006591 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006592}
6593
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006594int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006595nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006596{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006597 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006598 int ret;
6599
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006600 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006601 if (ret)
6602 return ret;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006603 nn->boot_time = get_seconds();
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006604 nn->grace_ended = false;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04006605 nn->nfsd4_manager.block_opens = true;
Jeff Laytond4318ac2014-09-12 16:40:21 -04006606 locks_start_grace(net, &nn->nfsd4_manager);
6607 nfsd4_client_tracking_init(net);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006608 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006609 nn->nfsd4_grace, net);
6610 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006611 return 0;
6612}
6613
6614/* initialization to perform when the nfsd service is started: */
6615
6616int
6617nfs4_state_start(void)
6618{
6619 int ret;
6620
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006621 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006622 if (ret)
6623 return -ENOMEM;
Jeff Layton51a54562015-08-20 07:17:01 -04006624 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05006625 if (laundry_wq == NULL) {
6626 ret = -ENOMEM;
6627 goto out_recovery;
6628 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006629 ret = nfsd4_create_callback_queue();
6630 if (ret)
6631 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006632
Meelap Shahc2f1a552007-07-17 04:04:39 -07006633 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006634
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006635 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006636
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006637out_free_laundry:
6638 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006639out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006640 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641}
6642
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006643void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006644nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006648 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006649
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006650 cancel_delayed_work_sync(&nn->laundromat_work);
6651 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006652
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04006654 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006655 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04006657 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04006658 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 }
Benny Halevycdc97502014-05-30 09:09:30 -04006660 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006661 list_for_each_safe(pos, next, &reaplist) {
6662 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006663 list_del_init(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +02006664 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -04006665 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04006666 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667 }
6668
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006669 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006670 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671}
6672
6673void
6674nfs4_state_shutdown(void)
6675{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006676 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006677 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006679
6680static void
6681get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6682{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006683 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6684 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006685}
6686
6687static void
6688put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6689{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006690 if (cstate->minorversion) {
6691 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6692 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6693 }
6694}
6695
6696void
6697clear_current_stateid(struct nfsd4_compound_state *cstate)
6698{
6699 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006700}
6701
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006702/*
6703 * functions to set current state id
6704 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006705void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006706nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6707{
6708 put_stateid(cstate, &odp->od_stateid);
6709}
6710
6711void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006712nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6713{
6714 put_stateid(cstate, &open->op_stateid);
6715}
6716
6717void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006718nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6719{
6720 put_stateid(cstate, &close->cl_stateid);
6721}
6722
6723void
6724nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6725{
6726 put_stateid(cstate, &lock->lk_resp_stateid);
6727}
6728
6729/*
6730 * functions to consume current state id
6731 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006732
6733void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006734nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6735{
6736 get_stateid(cstate, &odp->od_stateid);
6737}
6738
6739void
6740nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6741{
6742 get_stateid(cstate, &drp->dr_stateid);
6743}
6744
6745void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006746nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6747{
6748 get_stateid(cstate, &fsp->fr_stateid);
6749}
6750
6751void
6752nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6753{
6754 get_stateid(cstate, &setattr->sa_stateid);
6755}
6756
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006757void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006758nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6759{
6760 get_stateid(cstate, &close->cl_stateid);
6761}
6762
6763void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006764nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006765{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006766 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006767}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006768
6769void
6770nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6771{
6772 get_stateid(cstate, &read->rd_stateid);
6773}
6774
6775void
6776nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6777{
6778 get_stateid(cstate, &write->wr_stateid);
6779}